Browse Source

Refine encryption

introduce a constructor to store inner method name

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
tags/3.3.2
Syrone Wong 8 years ago
parent
commit
1dc992e806
4 changed files with 31 additions and 21 deletions
  1. +19
    -9
      shadowsocks-csharp/Encryption/EncryptorBase.cs
  2. +8
    -8
      shadowsocks-csharp/Encryption/IVEncryptor.cs
  3. +1
    -1
      shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs
  4. +3
    -3
      shadowsocks-csharp/Encryption/SodiumEncryptor.cs

+ 19
- 9
shadowsocks-csharp/Encryption/EncryptorBase.cs View File

@@ -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;
}
}


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

@@ -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);


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

@@ -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


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

@@ -25,9 +25,9 @@ namespace Shadowsocks.Encryption
}
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()


Loading…
Cancel
Save