From 1dc992e806d3fd4e640acb1d3468f31cba0fc0a2 Mon Sep 17 00:00:00 2001 From: Syrone Wong Date: Wed, 28 Sep 2016 10:46:19 +0800 Subject: [PATCH] Refine encryption introduce a constructor to store inner method name Signed-off-by: Syrone Wong --- .../Encryption/EncryptorBase.cs | 28 +++++++++++++------ shadowsocks-csharp/Encryption/IVEncryptor.cs | 16 +++++------ .../Encryption/MbedTLSEncryptor.cs | 2 +- .../Encryption/SodiumEncryptor.cs | 6 ++-- 4 files changed, 31 insertions(+), 21 deletions(-) diff --git a/shadowsocks-csharp/Encryption/EncryptorBase.cs b/shadowsocks-csharp/Encryption/EncryptorBase.cs index f79dbff7..d3692660 100644 --- a/shadowsocks-csharp/Encryption/EncryptorBase.cs +++ b/shadowsocks-csharp/Encryption/EncryptorBase.cs @@ -4,17 +4,27 @@ namespace Shadowsocks.Encryption { 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; } } diff --git a/shadowsocks-csharp/Encryption/IVEncryptor.cs b/shadowsocks-csharp/Encryption/IVEncryptor.cs index 016045ab..12845c0b 100755 --- a/shadowsocks-csharp/Encryption/IVEncryptor.cs +++ b/shadowsocks-csharp/Encryption/IVEncryptor.cs @@ -25,9 +25,9 @@ namespace Shadowsocks.Encryption protected bool _encryptIVSent; protected string _method; 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 int keyLen; protected int ivLen; @@ -46,15 +46,15 @@ namespace Shadowsocks.Encryption _method = method; string k = method + ":" + password; ciphers = getCiphers(); - _cipherInfo = ciphers[_method]; - _cipherMbedName = _cipherInfo.name; - _cipher = _cipherInfo.type; + CipherInfo = ciphers[_method]; + _innerLibName = CipherInfo.InnerLibName; + _cipher = CipherInfo.Type; if (_cipher == 0) { 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) => { byte[] passbuf = Encoding.UTF8.GetBytes(password); diff --git a/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs b/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs index 56669133..4de20e70 100644 --- a/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs +++ b/shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs @@ -70,7 +70,7 @@ namespace Shadowsocks.Encryption realkey = _key; } 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"); /* * MbedTLS takes key length by bit diff --git a/shadowsocks-csharp/Encryption/SodiumEncryptor.cs b/shadowsocks-csharp/Encryption/SodiumEncryptor.cs index afa794a9..661a90af 100755 --- a/shadowsocks-csharp/Encryption/SodiumEncryptor.cs +++ b/shadowsocks-csharp/Encryption/SodiumEncryptor.cs @@ -25,9 +25,9 @@ namespace Shadowsocks.Encryption } 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) } + { "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 getCiphers()