Browse Source

Refine Encryption module (#717)

- use EncryptorInfo struct instead of Dictionary<string, int[]>>
tags/3.3.1
破娃酱 Syrone Wong 8 years ago
parent
commit
ea9c7b093a
4 changed files with 42 additions and 28 deletions
  1. +16
    -0
      shadowsocks-csharp/Encryption/EncryptorBase.cs
  2. +8
    -10
      shadowsocks-csharp/Encryption/IVEncryptor.cs
  3. +13
    -13
      shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs
  4. +5
    -5
      shadowsocks-csharp/Encryption/SodiumEncryptor.cs

+ 16
- 0
shadowsocks-csharp/Encryption/EncryptorBase.cs View File

@@ -2,6 +2,22 @@
namespace Shadowsocks.Encryption 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 public abstract class EncryptorBase
: IEncryptor : IEncryptor
{ {


+ 8
- 10
shadowsocks-csharp/Encryption/IVEncryptor.cs View File

@@ -16,8 +16,7 @@ namespace Shadowsocks.Encryption
protected static byte[] tempbuf = new byte[MAX_INPUT_SIZE]; protected static byte[] tempbuf = new byte[MAX_INPUT_SIZE];
protected Dictionary<string, Dictionary<string, int[]>> ciphers;
protected Dictionary<string, int[]> ciphersDetail;
protected Dictionary<string, EncryptorInfo> ciphers;
private static readonly ConcurrentDictionary<string, byte[]> CachedKeys = new ConcurrentDictionary<string, byte[]>(); private static readonly ConcurrentDictionary<string, byte[]> CachedKeys = new ConcurrentDictionary<string, byte[]>();
protected byte[] _encryptIV; protected byte[] _encryptIV;
@@ -28,7 +27,7 @@ namespace Shadowsocks.Encryption
protected int _cipher; protected int _cipher;
// cipher name in MbedTLS, useless when using LibSodium // cipher name in MbedTLS, useless when using LibSodium
protected string _cipherMbedName; protected string _cipherMbedName;
protected int[] _cipherInfo;
protected EncryptorInfo _cipherInfo;
protected byte[] _key; protected byte[] _key;
protected int keyLen; protected int keyLen;
protected int ivLen; protected int ivLen;
@@ -39,7 +38,7 @@ namespace Shadowsocks.Encryption
InitKey(method, password); InitKey(method, password);
} }
protected abstract Dictionary<string, Dictionary<string, int[]>> getCiphers();
protected abstract Dictionary<string, EncryptorInfo> getCiphers();
private void InitKey(string method, string password) private void InitKey(string method, string password)
{ {
@@ -47,16 +46,15 @@ namespace Shadowsocks.Encryption
_method = method; _method = method;
string k = method + ":" + password; string k = method + ":" + password;
ciphers = getCiphers(); 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) if (_cipher == 0)
{ {
throw new Exception("method not found"); 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) => _key = CachedKeys.GetOrAdd(k, (nk) =>
{ {
byte[] passbuf = Encoding.UTF8.GetBytes(password); byte[] passbuf = Encoding.UTF8.GetBytes(password);


+ 13
- 13
shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs View File

@@ -20,18 +20,18 @@ namespace Shadowsocks.Encryption
{ {
} }
private static Dictionary<string, Dictionary<string, int[]>> _ciphers = new Dictionary<string, Dictionary<string, int[]>> {
{ "aes-128-cfb", new Dictionary<string, int[]> { { "AES-128-CFB128", new int[] { 16, 16, CIPHER_AES } } } },
{ "aes-192-cfb", new Dictionary<string, int[]> { { "AES-192-CFB128", new int[] { 24, 16, CIPHER_AES } } } },
{ "aes-256-cfb", new Dictionary<string, int[]> { { "AES-256-CFB128", new int[] { 32, 16, CIPHER_AES } } } },
{ "aes-128-ctr", new Dictionary<string, int[]> { { "AES-128-CTR", new int[] { 16, 16, CIPHER_AES } } } },
{ "aes-192-ctr", new Dictionary<string, int[]> { { "AES-192-CTR", new int[] { 24, 16, CIPHER_AES } } } },
{ "aes-256-ctr", new Dictionary<string, int[]> { { "AES-256-CTR", new int[] { 32, 16, CIPHER_AES } } } },
{ "bf-cfb", new Dictionary<string, int[]> { { "BLOWFISH-CFB64", new int[] { 16, 8, CIPHER_BLOWFISH } } } },
{ "camellia-128-cfb", new Dictionary<string, int[]> { { "CAMELLIA-128-CFB128", new int[] { 16, 16, CIPHER_CAMELLIA } } } },
{ "camellia-192-cfb", new Dictionary<string, int[]> { { "CAMELLIA-192-CFB128", new int[] { 24, 16, CIPHER_CAMELLIA } } } },
{ "camellia-256-cfb", new Dictionary<string, int[]> { { "CAMELLIA-256-CFB128", new int[] { 32, 16, CIPHER_CAMELLIA } } } },
{ "rc4-md5", new Dictionary<string, int[]> { { "ARC4-128", new int[] { 16, 16, CIPHER_RC4 } } } }
private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> {
{ "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<string> SupportedCiphers() public static List<string> SupportedCiphers()
@@ -39,7 +39,7 @@ namespace Shadowsocks.Encryption
return new List<string>(_ciphers.Keys); return new List<string>(_ciphers.Keys);
} }
protected override Dictionary<string, Dictionary<string, int[]>> getCiphers()
protected override Dictionary<string, EncryptorInfo> getCiphers()
{ {
return _ciphers; return _ciphers;
} }


+ 5
- 5
shadowsocks-csharp/Encryption/SodiumEncryptor.cs View File

@@ -24,13 +24,13 @@ namespace Shadowsocks.Encryption
{ {
} }
private static Dictionary<string, Dictionary<string, int[]>> _ciphers = new Dictionary<string, Dictionary<string, int[]>> {
{ "salsa20", new Dictionary<string, int[]> { { "salsa20", new int[] { 32, 8, CIPHER_SALSA20 } } } },
{ "chacha20", new Dictionary<string, int[]> { { "chacha20", new int[] { 32, 8, CIPHER_CHACHA20 } } } },
{ "chacha20-ietf", new Dictionary<string, int[]> { { "chacha20-ietf", new int[] { 32, 12, CIPHER_CHACHA20_IETF } } } }
private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> {
{ "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<string, Dictionary<string, int[]>> getCiphers()
protected override Dictionary<string, EncryptorInfo> getCiphers()
{ {
return _ciphers; return _ciphers;
} }


Loading…
Cancel
Save