Refine encryption(#655)

* Refine encryption

- Add AES-CTR, blowfish and camellia ciphers

aes-256-ctr
aes-192-ctr
aes-128-ctr
bf-cfb
camellia-128-cfb
camellia-192-cfb
camellia-256-cfb

- Merge the previous PolarSSL and MbedTLS
- Switch to MbedTLS's cipher layer functions
- Add workaround to set cipher operation(encrypt/decrypt)

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>

* Add function to get MbedTLS cipher context size

and drop hard-coded ctx sizes

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
This commit is contained in:
Syrone Wong
2016-08-13 14:28:09 +08:00
committed by GitHub
parent 3268c03d21
commit 2fafc52def
11 changed files with 246 additions and 284 deletions
+11 -7
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Net;
@@ -23,17 +24,18 @@ namespace Shadowsocks.Encryption
protected static byte[] tempbuf = new byte[MAX_INPUT_SIZE];
protected Dictionary<string, int[]> ciphers;
protected Dictionary<string, Dictionary<string, int[]>> ciphers;
protected Dictionary<string, int[]> ciphersDetail;
private static readonly ConcurrentDictionary<string, byte[]> CachedKeys = new ConcurrentDictionary<string, byte[]>();
protected byte[] _encryptIV;
protected byte[] _decryptIV;
protected bool _decryptIVReceived;
protected bool _encryptIVSent;
protected int _encryptIVOffset = 0;
protected int _decryptIVOffset = 0;
protected string _method;
protected int _cipher;
// cipher name in MbedTLS, useless when using LibSodium
protected string _cipherMbedName;
protected int[] _cipherInfo;
protected byte[] _key;
protected int keyLen;
@@ -47,7 +49,7 @@ namespace Shadowsocks.Encryption
InitKey(method, password);
}
protected abstract Dictionary<string, int[]> getCiphers();
protected abstract Dictionary<string, Dictionary<string, int[]>> getCiphers();
protected void InitKey(string method, string password)
{
@@ -55,14 +57,16 @@ namespace Shadowsocks.Encryption
_method = method;
string k = method + ":" + password;
ciphers = getCiphers();
_cipherInfo = ciphers[_method];
ciphersDetail = ciphers[_method];
_cipherMbedName = ciphersDetail.Keys.FirstOrDefault();
_cipherInfo = ciphers[_method][_cipherMbedName];
_cipher = _cipherInfo[2];
if (_cipher == 0)
{
throw new Exception("method not found");
}
keyLen = ciphers[_method][0];
ivLen = ciphers[_method][1];
keyLen = _cipherInfo[0];
ivLen = _cipherInfo[1];
_key = CachedKeys.GetOrAdd(k, (nk) =>
{
byte[] passbuf = Encoding.UTF8.GetBytes(password);