sodium works
This commit is contained in:
@@ -6,6 +6,8 @@ namespace Shadowsocks.Encrypt
|
||||
public abstract class EncryptorBase
|
||||
: IEncryptor
|
||||
{
|
||||
public const int MAX_INPUT_SIZE = 32768;
|
||||
|
||||
protected EncryptorBase(string method, string password)
|
||||
{
|
||||
Method = method;
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace Shadowsocks.Encrypt
|
||||
return new TableEncryptor(method, password);
|
||||
}
|
||||
|
||||
return new PolarSSLEncryptor(method, password);
|
||||
return new SodiumEncryptor(method, password);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Shadowsocks.Encrypt
|
||||
public abstract class IVEncryptor
|
||||
: EncryptorBase
|
||||
{
|
||||
protected static byte[] tempbuf = new byte[32768];
|
||||
protected static byte[] tempbuf = new byte[MAX_INPUT_SIZE];
|
||||
|
||||
protected Dictionary<string, int[]> ciphers;
|
||||
|
||||
|
||||
@@ -10,15 +10,21 @@ namespace Shadowsocks.Encrypt
|
||||
const int CIPHER_SALSA20 = 1;
|
||||
const int CIPHER_CHACHA20 = 2;
|
||||
|
||||
protected uint _encryptBytesRemaining;
|
||||
protected uint _decryptBytesRemaining;
|
||||
const int SODIUM_BLOCK_SIZE = 64;
|
||||
|
||||
protected int _encryptBytesRemaining;
|
||||
protected int _decryptBytesRemaining;
|
||||
protected ulong _encryptIC;
|
||||
protected ulong _decryptIC;
|
||||
protected byte[] _encryptBuf;
|
||||
protected byte[] _decryptBuf;
|
||||
|
||||
public SodiumEncryptor(string method, string password)
|
||||
: base(method, password)
|
||||
{
|
||||
InitKey(method, password);
|
||||
_encryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE];
|
||||
_decryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE];
|
||||
}
|
||||
|
||||
protected override Dictionary<string, int[]> getCiphers()
|
||||
@@ -31,18 +37,41 @@ namespace Shadowsocks.Encrypt
|
||||
|
||||
protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf)
|
||||
{
|
||||
uint bytesRemaining;
|
||||
// TODO write a unidirection cipher so we don't have to if if if
|
||||
int bytesRemaining;
|
||||
ulong ic;
|
||||
byte[] sodiumBuf;
|
||||
byte[] iv;
|
||||
if (isCipher)
|
||||
{
|
||||
bytesRemaining = _encryptBytesRemaining;
|
||||
ic = _encryptIC;
|
||||
sodiumBuf = _encryptBuf;
|
||||
iv = _encryptIV;
|
||||
}
|
||||
else
|
||||
{
|
||||
bytesRemaining = _decryptBytesRemaining;
|
||||
ic = _decryptIC;
|
||||
sodiumBuf = _decryptBuf;
|
||||
iv = _decryptIV;
|
||||
}
|
||||
int padding = bytesRemaining;
|
||||
Buffer.BlockCopy(buf, 0, sodiumBuf, padding, length);
|
||||
|
||||
switch (_cipher)
|
||||
{
|
||||
case CIPHER_SALSA20:
|
||||
Sodium.crypto_stream_salsa20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key);
|
||||
break;
|
||||
case CIPHER_CHACHA20:
|
||||
Sodium.crypto_stream_chacha20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key);
|
||||
break;
|
||||
}
|
||||
Buffer.BlockCopy(sodiumBuf, padding, outbuf, 0, length);
|
||||
padding += length;
|
||||
ic += (ulong)padding / SODIUM_BLOCK_SIZE;
|
||||
bytesRemaining = padding % SODIUM_BLOCK_SIZE;
|
||||
|
||||
if (isCipher)
|
||||
{
|
||||
|
||||
+2
@@ -206,6 +206,8 @@
|
||||
this.EncryptionSelect.Items.AddRange(new object[] {
|
||||
"table",
|
||||
"rc4-md5",
|
||||
"salsa20",
|
||||
"chacha20",
|
||||
"aes-256-cfb",
|
||||
"aes-192-cfb",
|
||||
"aes-128-cfb",
|
||||
|
||||
Reference in New Issue
Block a user