From ea9c7b093aaaa40e85697d2876baada5d1fecf9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A0=B4=E5=A8=83=E9=85=B1?= Date: Wed, 14 Sep 2016 11:16:37 +0800 Subject: [PATCH] Refine Encryption module (#717) - use EncryptorInfo struct instead of Dictionary> --- .../Encryption/EncryptorBase.cs | 16 ++++++++++++ shadowsocks-csharp/Encryption/IVEncryptor.cs | 18 ++++++------- .../Encryption/MbedTLSEncryptor.cs | 26 +++++++++---------- .../Encryption/SodiumEncryptor.cs | 10 +++---- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/shadowsocks-csharp/Encryption/EncryptorBase.cs b/shadowsocks-csharp/Encryption/EncryptorBase.cs index b7466788..f79dbff7 100644 --- a/shadowsocks-csharp/Encryption/EncryptorBase.cs +++ b/shadowsocks-csharp/Encryption/EncryptorBase.cs @@ -2,6 +2,22 @@ namespace Shadowsocks.Encryption { + public struct EncryptorInfo + { + public string name; + public int key_size; + public int iv_size; + public int type; + + public EncryptorInfo(string name, int key_size, int iv_size, int type) + { + this.name = name; + this.key_size = key_size; + this.iv_size = iv_size; + this.type = type; + } + } + public abstract class EncryptorBase : IEncryptor { diff --git a/shadowsocks-csharp/Encryption/IVEncryptor.cs b/shadowsocks-csharp/Encryption/IVEncryptor.cs index 4e8a72e0..016045ab 100755 --- a/shadowsocks-csharp/Encryption/IVEncryptor.cs +++ b/shadowsocks-csharp/Encryption/IVEncryptor.cs @@ -16,8 +16,7 @@ namespace Shadowsocks.Encryption protected static byte[] tempbuf = new byte[MAX_INPUT_SIZE]; - protected Dictionary> ciphers; - protected Dictionary ciphersDetail; + protected Dictionary ciphers; private static readonly ConcurrentDictionary CachedKeys = new ConcurrentDictionary(); protected byte[] _encryptIV; @@ -28,7 +27,7 @@ namespace Shadowsocks.Encryption protected int _cipher; // cipher name in MbedTLS, useless when using LibSodium protected string _cipherMbedName; - protected int[] _cipherInfo; + protected EncryptorInfo _cipherInfo; protected byte[] _key; protected int keyLen; protected int ivLen; @@ -39,7 +38,7 @@ namespace Shadowsocks.Encryption InitKey(method, password); } - protected abstract Dictionary> getCiphers(); + protected abstract Dictionary getCiphers(); private void InitKey(string method, string password) { @@ -47,16 +46,15 @@ namespace Shadowsocks.Encryption _method = method; string k = method + ":" + password; ciphers = getCiphers(); - ciphersDetail = ciphers[_method]; - _cipherMbedName = ciphersDetail.Keys.FirstOrDefault(); - _cipherInfo = ciphers[_method][_cipherMbedName]; - _cipher = _cipherInfo[2]; + _cipherInfo = ciphers[_method]; + _cipherMbedName = _cipherInfo.name; + _cipher = _cipherInfo.type; if (_cipher == 0) { throw new Exception("method not found"); } - keyLen = _cipherInfo[0]; - ivLen = _cipherInfo[1]; + keyLen = _cipherInfo.key_size; + ivLen = _cipherInfo.iv_size; _key = CachedKeys.GetOrAdd(k, (nk) => { byte[] passbuf = Encoding.UTF8.GetBytes(password); diff --git a/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs b/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs index d5d3728c..56669133 100644 --- a/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs +++ b/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs @@ -20,18 +20,18 @@ namespace Shadowsocks.Encryption { } - private static Dictionary> _ciphers = new Dictionary> { - { "aes-128-cfb", new Dictionary { { "AES-128-CFB128", new int[] { 16, 16, CIPHER_AES } } } }, - { "aes-192-cfb", new Dictionary { { "AES-192-CFB128", new int[] { 24, 16, CIPHER_AES } } } }, - { "aes-256-cfb", new Dictionary { { "AES-256-CFB128", new int[] { 32, 16, CIPHER_AES } } } }, - { "aes-128-ctr", new Dictionary { { "AES-128-CTR", new int[] { 16, 16, CIPHER_AES } } } }, - { "aes-192-ctr", new Dictionary { { "AES-192-CTR", new int[] { 24, 16, CIPHER_AES } } } }, - { "aes-256-ctr", new Dictionary { { "AES-256-CTR", new int[] { 32, 16, CIPHER_AES } } } }, - { "bf-cfb", new Dictionary { { "BLOWFISH-CFB64", new int[] { 16, 8, CIPHER_BLOWFISH } } } }, - { "camellia-128-cfb", new Dictionary { { "CAMELLIA-128-CFB128", new int[] { 16, 16, CIPHER_CAMELLIA } } } }, - { "camellia-192-cfb", new Dictionary { { "CAMELLIA-192-CFB128", new int[] { 24, 16, CIPHER_CAMELLIA } } } }, - { "camellia-256-cfb", new Dictionary { { "CAMELLIA-256-CFB128", new int[] { 32, 16, CIPHER_CAMELLIA } } } }, - { "rc4-md5", new Dictionary { { "ARC4-128", new int[] { 16, 16, CIPHER_RC4 } } } } + private static Dictionary _ciphers = new Dictionary { + { "aes-128-cfb", new EncryptorInfo("AES-128-CFB128", 16, 16, CIPHER_AES) }, + { "aes-192-cfb", new EncryptorInfo("AES-192-CFB128", 24, 16, CIPHER_AES) }, + { "aes-256-cfb", new EncryptorInfo("AES-256-CFB128", 32, 16, CIPHER_AES) }, + { "aes-128-ctr", new EncryptorInfo("AES-128-CTR", 16, 16, CIPHER_AES) }, + { "aes-192-ctr", new EncryptorInfo("AES-192-CTR", 24, 16, CIPHER_AES) }, + { "aes-256-ctr", new EncryptorInfo("AES-256-CTR", 32, 16, CIPHER_AES) }, + { "bf-cfb", new EncryptorInfo("BLOWFISH-CFB64", 16, 8, CIPHER_BLOWFISH) }, + { "camellia-128-cfb", new EncryptorInfo("CAMELLIA-128-CFB128", 16, 16, CIPHER_CAMELLIA) }, + { "camellia-192-cfb", new EncryptorInfo("CAMELLIA-192-CFB128", 24, 16, CIPHER_CAMELLIA) }, + { "camellia-256-cfb", new EncryptorInfo("CAMELLIA-256-CFB128", 32, 16, CIPHER_CAMELLIA) }, + { "rc4-md5", new EncryptorInfo("ARC4-128", 16, 16, CIPHER_RC4) } }; public static List SupportedCiphers() @@ -39,7 +39,7 @@ namespace Shadowsocks.Encryption return new List(_ciphers.Keys); } - protected override Dictionary> getCiphers() + protected override Dictionary getCiphers() { return _ciphers; } diff --git a/shadowsocks-csharp/Encryption/SodiumEncryptor.cs b/shadowsocks-csharp/Encryption/SodiumEncryptor.cs index 476df828..afa794a9 100755 --- a/shadowsocks-csharp/Encryption/SodiumEncryptor.cs +++ b/shadowsocks-csharp/Encryption/SodiumEncryptor.cs @@ -24,13 +24,13 @@ namespace Shadowsocks.Encryption { } - private static Dictionary> _ciphers = new Dictionary> { - { "salsa20", new Dictionary { { "salsa20", new int[] { 32, 8, CIPHER_SALSA20 } } } }, - { "chacha20", new Dictionary { { "chacha20", new int[] { 32, 8, CIPHER_CHACHA20 } } } }, - { "chacha20-ietf", new Dictionary { { "chacha20-ietf", new int[] { 32, 12, CIPHER_CHACHA20_IETF } } } } + private static Dictionary _ciphers = new Dictionary { + { "salsa20", new EncryptorInfo("salsa20", 32, 8, CIPHER_SALSA20) }, + { "chacha20", new EncryptorInfo("chacha20", 32, 8, CIPHER_CHACHA20) }, + { "chacha20-ietf", new EncryptorInfo("chacha20-ietf", 32, 12, CIPHER_CHACHA20_IETF) } }; - protected override Dictionary> getCiphers() + protected override Dictionary getCiphers() { return _ciphers; }