fix aes
This commit is contained in:
@@ -91,20 +91,23 @@ namespace Shadowsocks.Encrypt
|
||||
}
|
||||
byte[] iv;
|
||||
int ivOffset;
|
||||
IntPtr ctx;
|
||||
if (isCipher)
|
||||
{
|
||||
iv = _encryptIV;
|
||||
ivOffset = _encryptIVOffset;
|
||||
ctx = _encryptCtx;
|
||||
}
|
||||
else
|
||||
{
|
||||
iv = _decryptIV;
|
||||
ivOffset = _decryptIVOffset;
|
||||
ctx = _decryptCtx;
|
||||
}
|
||||
switch (_cipher)
|
||||
{
|
||||
case CIPHER_AES:
|
||||
PolarSSL.aes_crypt_cfb128(_encryptCtx, isCipher ? PolarSSL.AES_ENCRYPT : PolarSSL.AES_DECRYPT, length, ref ivOffset, iv, buf, outbuf);
|
||||
PolarSSL.aes_crypt_cfb128(ctx, isCipher ? PolarSSL.AES_ENCRYPT : PolarSSL.AES_DECRYPT, length, ref ivOffset, iv, buf, outbuf);
|
||||
if (isCipher)
|
||||
{
|
||||
_encryptIVOffset = ivOffset;
|
||||
|
||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
||||
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("2.0.11")]
|
||||
[assembly: AssemblyVersion("2.1")]
|
||||
// [assembly: AssemblyFileVersion("2.0.0")]
|
||||
|
||||
+73
-4
@@ -23,14 +23,14 @@ namespace test
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestEncryption()
|
||||
public void TestPolarSSLEncryption()
|
||||
{
|
||||
// run it once before the multi-threading test to initialize global tables
|
||||
RunSingleEncryptionThread();
|
||||
RunSinglePolarSSLEncryptionThread();
|
||||
List<Thread> threads = new List<Thread>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Thread t = new Thread(new ThreadStart(RunSingleEncryptionThread));
|
||||
Thread t = new Thread(new ThreadStart(RunSinglePolarSSLEncryptionThread));
|
||||
threads.Add(t);
|
||||
t.Start();
|
||||
}
|
||||
@@ -44,7 +44,7 @@ namespace test
|
||||
private static bool encryptionFailed = false;
|
||||
private static object locker = new object();
|
||||
|
||||
private void RunSingleEncryptionThread()
|
||||
private void RunSinglePolarSSLEncryptionThread()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -93,5 +93,74 @@ namespace test
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestSodiumEncryption()
|
||||
{
|
||||
// run it once before the multi-threading test to initialize global tables
|
||||
RunSingleSodiumEncryptionThread();
|
||||
List<Thread> threads = new List<Thread>();
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Thread t = new Thread(new ThreadStart(RunSingleSodiumEncryptionThread));
|
||||
threads.Add(t);
|
||||
t.Start();
|
||||
}
|
||||
foreach (Thread t in threads)
|
||||
{
|
||||
t.Join();
|
||||
}
|
||||
Assert.IsFalse(encryptionFailed);
|
||||
}
|
||||
|
||||
private void RunSingleSodiumEncryptionThread()
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
var random = new Random();
|
||||
IEncryptor encryptor;
|
||||
IEncryptor decryptor;
|
||||
encryptor = new SodiumEncryptor("salsa20", "barfoo!");
|
||||
decryptor = new SodiumEncryptor("salsa20", "barfoo!");
|
||||
byte[] plain = new byte[16384];
|
||||
byte[] cipher = new byte[plain.Length + 16];
|
||||
byte[] plain2 = new byte[plain.Length + 16];
|
||||
int outLen = 0;
|
||||
int outLen2 = 0;
|
||||
random.NextBytes(plain);
|
||||
//lock (locker)
|
||||
//{
|
||||
encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
|
||||
decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
|
||||
Assert.AreEqual(plain.Length, outLen2);
|
||||
for (int j = 0; j < plain.Length; j++)
|
||||
{
|
||||
Assert.AreEqual(plain[j], plain2[j]);
|
||||
}
|
||||
encryptor.Encrypt(plain, 1000, cipher, out outLen);
|
||||
decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
|
||||
Assert.AreEqual(1000, outLen2);
|
||||
for (int j = 0; j < outLen2; j++)
|
||||
{
|
||||
Assert.AreEqual(plain[j], plain2[j]);
|
||||
}
|
||||
encryptor.Encrypt(plain, 12333, cipher, out outLen);
|
||||
decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
|
||||
Assert.AreEqual(12333, outLen2);
|
||||
for (int j = 0; j < outLen2; j++)
|
||||
{
|
||||
Assert.AreEqual(plain[j], plain2[j]);
|
||||
}
|
||||
//}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
encryptionFailed = true;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user