- Drop redundant code. I should read code carefully, mbed TLS will set operation when invoking mbedtls_cipher_setkey(), the last param const mbedtls_operation_t operation is what we want. - minor changes Signed-off-by: Syrone Wong <wong.syrone@gmail.com>tags/3.3
@@ -51,7 +51,7 @@ namespace Shadowsocks.Encryption | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info); | public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info); | ||||
// check operation | |||||
// XXX: Check operation before using it | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation); | public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation); | ||||
@@ -70,9 +70,6 @@ namespace Shadowsocks.Encryption | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public static extern void md5(byte[] input, uint ilen, byte[] output); | public static extern void md5(byte[] input, uint ilen, byte[] output); | ||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
public static extern void cipher_set_operation_ex(IntPtr ctx, int operation); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public static extern int cipher_get_size_ex(); | public static extern int cipher_get_size_ex(); | ||||
} | } | ||||
@@ -76,8 +76,17 @@ namespace Shadowsocks.Encryption | |||||
MbedTLS.cipher_init(ctx); | MbedTLS.cipher_init(ctx); | ||||
if (MbedTLS.cipher_setup( ctx, MbedTLS.cipher_info_from_string( _cipherMbedName ) ) != 0 ) | if (MbedTLS.cipher_setup( ctx, MbedTLS.cipher_info_from_string( _cipherMbedName ) ) != 0 ) | ||||
throw new Exception(); | throw new Exception(); | ||||
// MbedTLS takes key length by bit | |||||
// cipher_setkey() will set the correct key schedule | |||||
/* | |||||
* MbedTLS takes key length by bit | |||||
* cipher_setkey() will set the correct key schedule | |||||
* and operation | |||||
* | |||||
* MBEDTLS_AES_{EN,DE}CRYPT | |||||
* == MBEDTLS_BLOWFISH_{EN,DE}CRYPT | |||||
* == MBEDTLS_CAMELLIA_{EN,DE}CRYPT | |||||
* == MBEDTLS_{EN,DE}CRYPT | |||||
* | |||||
*/ | |||||
if (MbedTLS.cipher_setkey(ctx, realkey, keyLen * 8, isCipher ? MbedTLS.MBEDTLS_ENCRYPT : MbedTLS.MBEDTLS_DECRYPT) != 0 ) | if (MbedTLS.cipher_setkey(ctx, realkey, keyLen * 8, isCipher ? MbedTLS.MBEDTLS_ENCRYPT : MbedTLS.MBEDTLS_DECRYPT) != 0 ) | ||||
throw new Exception(); | throw new Exception(); | ||||
if (MbedTLS.cipher_set_iv(ctx, iv, ivLen) != 0) | if (MbedTLS.cipher_set_iv(ctx, iv, ivLen) != 0) | ||||
@@ -102,23 +111,6 @@ namespace Shadowsocks.Encryption | |||||
{ | { | ||||
ctx = _decryptCtx; | ctx = _decryptCtx; | ||||
} | } | ||||
if (_cipher == CIPHER_AES | |||||
|| _cipher == CIPHER_BLOWFISH | |||||
|| _cipher == CIPHER_CAMELLIA) | |||||
{ | |||||
/* | |||||
* operation workaround | |||||
* | |||||
* MBEDTLS_AES_{EN,DE}CRYPT | |||||
* == MBEDTLS_BLOWFISH_{EN,DE}CRYPT | |||||
* == MBEDTLS_CAMELLIA_{EN,DE}CRYPT | |||||
* == MBEDTLS_{EN,DE}CRYPT | |||||
* setter code in C: | |||||
* ctx->operation = operation; | |||||
*/ | |||||
MbedTLS.cipher_set_operation_ex(ctx, isCipher ? MbedTLS.MBEDTLS_ENCRYPT : MbedTLS.MBEDTLS_DECRYPT); | |||||
} | |||||
if (MbedTLS.cipher_update(ctx, buf, length, outbuf, ref length) != 0 ) | if (MbedTLS.cipher_update(ctx, buf, length, outbuf, ref length) != 0 ) | ||||
throw new Exception(); | throw new Exception(); | ||||
} | } | ||||
@@ -33,16 +33,16 @@ namespace Shadowsocks.Encryption | |||||
private static extern IntPtr LoadLibrary(string path); | private static extern IntPtr LoadLibrary(string path); | ||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public extern static int crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); | |||||
public static extern int crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public extern static int crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); | |||||
public static extern int crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public extern static int crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, uint ic, byte[] k); | |||||
public static extern int crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, uint ic, byte[] k); | |||||
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | ||||
public extern static void ss_sha1_hmac_ex(byte[] key, uint keylen, | |||||
public static extern void ss_sha1_hmac_ex(byte[] key, uint keylen, | |||||
byte[] input, int ioff, uint ilen, | byte[] input, int ioff, uint ilen, | ||||
byte[] output); | byte[] output); | ||||
} | } | ||||
@@ -211,7 +211,7 @@ | |||||
"bf-cfb", | "bf-cfb", | ||||
"camellia-128-cfb", | "camellia-128-cfb", | ||||
"camellia-192-cfb", | "camellia-192-cfb", | ||||
"camellia-256-cfb",}); | |||||
"camellia-256-cfb"}); | |||||
this.EncryptionSelect.Location = new System.Drawing.Point(83, 87); | this.EncryptionSelect.Location = new System.Drawing.Point(83, 87); | ||||
this.EncryptionSelect.Name = "EncryptionSelect"; | this.EncryptionSelect.Name = "EncryptionSelect"; | ||||
this.EncryptionSelect.Size = new System.Drawing.Size(160, 20); | this.EncryptionSelect.Size = new System.Drawing.Size(160, 20); | ||||