From 56365fdf96a027d64fa34c35b029b201cefcc795 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Thu, 6 Nov 2014 01:31:48 +0800 Subject: [PATCH] add blowfish --- polarssl/polarssl.dll | Bin 40677 -> 40533 bytes shadowsocks-csharp/Encrypt/PolarSSL.cs | 17 +++++++ .../Encrypt/PolarSSLEncryptor.cs | 44 +++++++++++++++--- shadowsocks-csharp/Local.cs | 4 +- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/polarssl/polarssl.dll b/polarssl/polarssl.dll index d64fa6dca4725e4610c98e4d17c65c69bd04d177..aa107b935b3932aeef89908c302a4ac69417c642 100755 GIT binary patch delta 495 zcmaF5m+9&rrU@OK%rQ|R3@Ho@)e|SWGhgUo+<3d4lSxc=a|+iMW~Kzm%~E{!TukZw zo1-P-7@4N?ZQd<;gm?3PoleHhHR=wmn?3aQs4_9fOlAv-Q@dl%!0^U`f#HB91H%n7 z28J1?3=9oGyakABj2Re0Oc)rB7;o+hso>@4GTvqU&D6zgGgs0EcE*&=-dUlHj75{B zbLH6%G69t*z#Q1mR-kWUM`kV0$NMLe6+XJS5jGK+xkMT@ypAs>~CsUZx<`k|i%uEa9HcRo@b1^L! z*&Hnq$H;VDc=K+_BfOirHEmfp*Qoa}ZuZdIqsruFIGHUZPA$NUfg!=1fx*Orfx*X= zfq}<_f#HiW1A_*T-UAeyV#&b3V!631q=J_t%VdYiDT`kgo4JxUu&YHfFfjc4&%lt* zn3!6em#LQw_ka|S3bM#spooUb)sSDHG$t&42TG85z${ z<}Um_c~=qFWPzepj1MR8F6w9exY@f{o{=$Qb9PBA2V>dflQq8?ZvaW@%@1l%urWTH nyt3st;}?*?d4W12(%);o|}TnUIV7 diff --git a/shadowsocks-csharp/Encrypt/PolarSSL.cs b/shadowsocks-csharp/Encrypt/PolarSSL.cs index b20117b5..70dc75e5 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSL.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSL.cs @@ -40,5 +40,22 @@ namespace shadowsocks_csharp.Encrypt [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static int arc4_crypt(byte[] ctx, int length, byte[] input, byte[] output); + + public const int BLOWFISH_CTX_SIZE = 4168; + public const int BLOWFISH_ENCRYPT = 1; + public const int BLOWFISH_DECRYPT = 0; + + [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] + public extern static void blowfish_init(byte[] ctx); + + [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] + public extern static void blowfish_free(byte[] ctx); + + [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] + public extern static int blowfish_setkey(byte[] ctx, byte[] key, int keysize); + + [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] + public extern static int blowfish_crypt_cfb64(byte[] ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output); + } } diff --git a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs index 966f5ac4..e14b26ab 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs @@ -17,7 +17,7 @@ namespace shadowsocks_csharp.Encrypt {"aes-128-cfb", new int[]{16, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, {"aes-192-cfb", new int[]{24, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, {"aes-256-cfb", new int[]{32, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, - //{"bf-cfb", new int[]{16, 8, CIPHER_BF, PolarSSL.BF_CTX_SIZE}}, + {"bf-cfb", new int[]{16, 8, CIPHER_BF, PolarSSL.BLOWFISH_CTX_SIZE}}, {"rc4", new int[]{16, 0, CIPHER_RC4, PolarSSL.ARC4_CTX_SIZE}}, {"rc4-md5", new int[]{16, 16, CIPHER_RC4, PolarSSL.ARC4_CTX_SIZE}}, }; @@ -118,18 +118,32 @@ namespace shadowsocks_csharp.Encrypt if (_cipher == CIPHER_AES) { PolarSSL.aes_init(ctx); + // PolarSSL takes key length by bit + // since we'll use CFB mode, here we both do enc, not dec + PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); + if (isCipher) + { + _encryptIV = new byte[ivLen]; + Array.Copy(iv, _encryptIV, ivLen); + } + else + { + _decryptIV = new byte[ivLen]; + Array.Copy(iv, _decryptIV, ivLen); + } + } + else if (_cipher == CIPHER_BF) + { + PolarSSL.blowfish_init(ctx); + // PolarSSL takes key length by bit + PolarSSL.blowfish_setkey(ctx, realkey, keyLen * 8); if (isCipher) { - // PolarSSL takes key length by bit - PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); _encryptIV = new byte[ivLen]; Array.Copy(iv, _encryptIV, ivLen); } else { - // PolarSSL takes key length by bit - // since we'll use CFB mode, here we also do enc, not dec - PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); _decryptIV = new byte[ivLen]; Array.Copy(iv, _decryptIV, ivLen); } @@ -157,6 +171,9 @@ namespace shadowsocks_csharp.Encrypt case CIPHER_AES: PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf); break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf); + break; case CIPHER_RC4: PolarSSL.arc4_crypt(_encryptCtx, length, buf, tempbuf); break; @@ -172,6 +189,9 @@ namespace shadowsocks_csharp.Encrypt case CIPHER_AES: PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf); break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf); + break; case CIPHER_RC4: PolarSSL.arc4_crypt(_encryptCtx, length, buf, outbuf); break; @@ -191,6 +211,9 @@ namespace shadowsocks_csharp.Encrypt case CIPHER_AES: PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf); break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf); + break; case CIPHER_RC4: PolarSSL.arc4_crypt(_decryptCtx, length - ivLen, tempbuf, outbuf); break; @@ -204,6 +227,9 @@ namespace shadowsocks_csharp.Encrypt case CIPHER_AES: PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf); break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf); + break; case CIPHER_RC4: PolarSSL.arc4_crypt(_decryptCtx, length, buf, outbuf); break; @@ -241,6 +267,9 @@ namespace shadowsocks_csharp.Encrypt case CIPHER_AES: PolarSSL.aes_free(_encryptCtx); break; + case CIPHER_BF: + PolarSSL.blowfish_free(_encryptCtx); + break; case CIPHER_RC4: PolarSSL.arc4_free(_encryptCtx); break; @@ -253,6 +282,9 @@ namespace shadowsocks_csharp.Encrypt case CIPHER_AES: PolarSSL.aes_free(_decryptCtx); break; + case CIPHER_BF: + PolarSSL.blowfish_free(_decryptCtx); + break; case CIPHER_RC4: PolarSSL.arc4_free(_decryptCtx); break; diff --git a/shadowsocks-csharp/Local.cs b/shadowsocks-csharp/Local.cs index 0ad8b6b7..d4cb6d45 100755 --- a/shadowsocks-csharp/Local.cs +++ b/shadowsocks-csharp/Local.cs @@ -34,7 +34,7 @@ namespace shadowsocks_csharp // Start an asynchronous socket to listen for connections. - Console.WriteLine("Waiting for a connection..."); + Console.WriteLine("Shadowsocks started"); listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); @@ -85,7 +85,7 @@ namespace shadowsocks_csharp } catch (Exception e) { - Console.WriteLine(e.ToString()); + //Console.WriteLine(e.ToString()); } }