Refine encryption(#655)

* Refine encryption

- Add AES-CTR, blowfish and camellia ciphers

aes-256-ctr
aes-192-ctr
aes-128-ctr
bf-cfb
camellia-128-cfb
camellia-192-cfb
camellia-256-cfb

- Merge the previous PolarSSL and MbedTLS
- Switch to MbedTLS's cipher layer functions
- Add workaround to set cipher operation(encrypt/decrypt)

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>

* Add function to get MbedTLS cipher context size

and drop hard-coded ctx sizes

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
Syrone Wong
2016-08-13 14:28:09 +08:00
committed by GitHub
parent 3268c03d21
commit 2fafc52def
11 changed files with 246 additions and 284 deletions
+42 -10
View File
@@ -12,6 +12,9 @@ namespace Shadowsocks.Encryption
{
const string DLLNAME = "libsscrypto";
public const int MBEDTLS_ENCRYPT = 1;
public const int MBEDTLS_DECRYPT = 0;
static MbedTLS()
{
string dllPath = Utils.GetTempPath("libsscrypto.dll");
@@ -24,24 +27,53 @@ namespace Shadowsocks.Encryption
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Logging.LogUsefulException(e);
}
LoadLibrary(dllPath);
}
public static byte[] MD5(byte[] input)
{
byte[] output = new byte[16];
md5(input, (uint)input.Length, output);
return output;
}
[DllImport("Kernel32.dll")]
private static extern IntPtr LoadLibrary(string path);
public const int MD5_CTX_SIZE = 88;
public static byte[] MD5(byte[] input)
{
byte[] output = new byte[16];
MbedTLS.md5(input, (uint)input.Length, output);
return output;
}
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr cipher_info_from_string(string cipher_name);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public extern static void md5(byte[] input, uint ilen, byte[] output);
public static extern void cipher_init(IntPtr ctx);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info);
// check operation
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int cipher_set_iv(IntPtr ctx, byte[] iv, int iv_len);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int cipher_reset(IntPtr ctx);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int cipher_update(IntPtr ctx, byte[] input, int ilen, byte[] output, ref int olen);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void cipher_free(IntPtr ctx);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
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)]
public static extern int cipher_get_size_ex();
}
}