introduce a constructor to store inner method name Signed-off-by: Syrone Wong <wong.syrone@gmail.com>tags/3.3.2
@@ -4,17 +4,27 @@ namespace Shadowsocks.Encryption | |||||
{ | { | ||||
public struct EncryptorInfo | public struct EncryptorInfo | ||||
{ | { | ||||
public string name; | |||||
public int key_size; | |||||
public int iv_size; | |||||
public int type; | |||||
public int KeySize; | |||||
public int IvSize; | |||||
public int Type; | |||||
public string InnerLibName; | |||||
// For those who make use of internal crypto method name | |||||
// e.g. mbed TLS | |||||
public EncryptorInfo(string innerLibName, int keySize, int ivSize, int type) | |||||
{ | |||||
this.KeySize = keySize; | |||||
this.IvSize = ivSize; | |||||
this.Type = type; | |||||
this.InnerLibName = innerLibName; | |||||
} | |||||
public EncryptorInfo(string name, int key_size, int iv_size, int type) | |||||
public EncryptorInfo(int keySize, int ivSize, int type) | |||||
{ | { | ||||
this.name = name; | |||||
this.key_size = key_size; | |||||
this.iv_size = iv_size; | |||||
this.type = type; | |||||
this.KeySize = keySize; | |||||
this.IvSize = ivSize; | |||||
this.Type = type; | |||||
this.InnerLibName = string.Empty; | |||||
} | } | ||||
} | } | ||||
@@ -25,9 +25,9 @@ namespace Shadowsocks.Encryption | |||||
protected bool _encryptIVSent; | protected bool _encryptIVSent; | ||||
protected string _method; | protected string _method; | ||||
protected int _cipher; | protected int _cipher; | ||||
// cipher name in MbedTLS, useless when using LibSodium | |||||
protected string _cipherMbedName; | |||||
protected EncryptorInfo _cipherInfo; | |||||
// internal name in the crypto library | |||||
protected string _innerLibName; | |||||
protected EncryptorInfo CipherInfo; | |||||
protected byte[] _key; | protected byte[] _key; | ||||
protected int keyLen; | protected int keyLen; | ||||
protected int ivLen; | protected int ivLen; | ||||
@@ -46,15 +46,15 @@ namespace Shadowsocks.Encryption | |||||
_method = method; | _method = method; | ||||
string k = method + ":" + password; | string k = method + ":" + password; | ||||
ciphers = getCiphers(); | ciphers = getCiphers(); | ||||
_cipherInfo = ciphers[_method]; | |||||
_cipherMbedName = _cipherInfo.name; | |||||
_cipher = _cipherInfo.type; | |||||
CipherInfo = ciphers[_method]; | |||||
_innerLibName = CipherInfo.InnerLibName; | |||||
_cipher = CipherInfo.Type; | |||||
if (_cipher == 0) | if (_cipher == 0) | ||||
{ | { | ||||
throw new Exception("method not found"); | throw new Exception("method not found"); | ||||
} | } | ||||
keyLen = _cipherInfo.key_size; | |||||
ivLen = _cipherInfo.iv_size; | |||||
keyLen = CipherInfo.KeySize; | |||||
ivLen = CipherInfo.IvSize; | |||||
_key = CachedKeys.GetOrAdd(k, (nk) => | _key = CachedKeys.GetOrAdd(k, (nk) => | ||||
{ | { | ||||
byte[] passbuf = Encoding.UTF8.GetBytes(password); | byte[] passbuf = Encoding.UTF8.GetBytes(password); | ||||
@@ -70,7 +70,7 @@ namespace Shadowsocks.Encryption | |||||
realkey = _key; | realkey = _key; | ||||
} | } | ||||
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( _innerLibName ) ) != 0 ) | |||||
throw new Exception("Cannot initialize mbed TLS cipher context"); | throw new Exception("Cannot initialize mbed TLS cipher context"); | ||||
/* | /* | ||||
* MbedTLS takes key length by bit | * MbedTLS takes key length by bit | ||||
@@ -25,9 +25,9 @@ namespace Shadowsocks.Encryption | |||||
} | } | ||||
private static Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> { | 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) } | |||||
{ "salsa20", new EncryptorInfo(32, 8, CIPHER_SALSA20) }, | |||||
{ "chacha20", new EncryptorInfo(32, 8, CIPHER_CHACHA20) }, | |||||
{ "chacha20-ietf", new EncryptorInfo(32, 12, CIPHER_CHACHA20_IETF) } | |||||
}; | }; | ||||
protected override Dictionary<string, EncryptorInfo> getCiphers() | protected override Dictionary<string, EncryptorInfo> getCiphers() | ||||