You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CryptographyTest.cs 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Shadowsocks.Encryption;
  3. using Shadowsocks.Encryption.Stream;
  4. using Shadowsocks.Encryption.AEAD;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. namespace Shadowsocks.Test
  9. {
  10. [TestClass]
  11. public class CryptographyTest
  12. {
  13. Random random = new Random();
  14. private void ArrayEqual<T>(IEnumerable<T> expected, IEnumerable<T> actual, string msg = "")
  15. {
  16. var e1 = expected.GetEnumerator();
  17. var e2 = actual.GetEnumerator();
  18. int ctr = 0;
  19. while (true)
  20. {
  21. var e1next = e1.MoveNext();
  22. var e2next = e2.MoveNext();
  23. if (e1next && e2next)
  24. {
  25. Assert.AreEqual(e1.Current, e2.Current, "at " + ctr);
  26. }
  27. else if (!e1next && !e2next)
  28. {
  29. return;
  30. }
  31. else if (!e1next)
  32. {
  33. Assert.Fail($"actual longer than expected ({ctr}) {msg}");
  34. }
  35. else
  36. {
  37. Assert.Fail($"actual shorter than expected ({ctr}) {msg}");
  38. }
  39. }
  40. }
  41. [TestMethod]
  42. public void TestMD5()
  43. {
  44. for (int len = 1; len < 64; len++)
  45. {
  46. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  47. byte[] bytes = new byte[len];
  48. random.NextBytes(bytes);
  49. string md5str = Convert.ToBase64String(md5.ComputeHash(bytes));
  50. string md5str2 = Convert.ToBase64String(CryptoUtils.MD5(bytes));
  51. Assert.IsTrue(md5str == md5str2);
  52. }
  53. }
  54. private void SingleEncryptionTestCase(IEncryptor encryptor, IEncryptor decryptor, int length)
  55. {
  56. RNG.Reload();
  57. byte[] plain = new byte[length];
  58. byte[] cipher = new byte[plain.Length + 100];// AEAD with IPv4 address type needs +100
  59. byte[] plain2 = new byte[plain.Length + 16];
  60. random.NextBytes(plain);
  61. encryptor.Encrypt(plain, length, cipher, out int outLen);
  62. decryptor.Decrypt(cipher, outLen, plain2, out int outLen2);
  63. Assert.AreEqual(length, outLen2);
  64. ArrayEqual<byte>(plain.AsSpan().Slice(0, length).ToArray(), plain2.AsSpan().Slice(0, length).ToArray());
  65. }
  66. private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor)
  67. {
  68. SingleEncryptionTestCase(encryptor, decryptor, 16384);
  69. SingleEncryptionTestCase(encryptor, decryptor, 7);
  70. SingleEncryptionTestCase(encryptor, decryptor, 1000);
  71. SingleEncryptionTestCase(encryptor, decryptor, 12333);
  72. }
  73. const string password = "barfoo!";
  74. private void RunSingleEncryptionThread(Type enc, Type dec, string method)
  75. {
  76. var ector = enc.GetConstructor(new Type[] { typeof(string), typeof(string) });
  77. var dctor = dec.GetConstructor(new Type[] { typeof(string), typeof(string) });
  78. try
  79. {
  80. IEncryptor encryptor = (IEncryptor)ector.Invoke(new object[] { method, password });
  81. IEncryptor decryptor = (IEncryptor)dctor.Invoke(new object[] { method, password });
  82. encryptor.AddressBufferLength = 1 + 4 + 2;// ADDR_ATYP_LEN + 4 + ADDR_PORT_LEN;
  83. decryptor.AddressBufferLength = 1 + 4 + 2;// ADDR_ATYP_LEN + 4 + ADDR_PORT_LEN;
  84. for (int i = 0; i < 16; i++)
  85. {
  86. RunEncryptionRound(encryptor, decryptor);
  87. }
  88. }
  89. catch
  90. {
  91. encryptionFailed = true;
  92. throw;
  93. }
  94. }
  95. private static bool encryptionFailed = false;
  96. private void TestEncryptionMethod(Type enc, string method)
  97. {
  98. TestEncryptionMethod(enc, enc, method);
  99. }
  100. private void TestEncryptionMethod(Type enc, Type dec, string method)
  101. {
  102. encryptionFailed = false;
  103. // run it once before the multi-threading test to initialize global tables
  104. RunSingleEncryptionThread(enc, dec, method);
  105. List<Thread> threads = new List<Thread>();
  106. for (int i = 0; i < 8; i++)
  107. {
  108. Thread t = new Thread(new ThreadStart(() => RunSingleEncryptionThread(enc, dec, method))); threads.Add(t);
  109. t.Start();
  110. }
  111. foreach (Thread t in threads)
  112. {
  113. t.Join();
  114. }
  115. Assert.IsFalse(encryptionFailed);
  116. }
  117. [TestMethod]
  118. public void TestAesGcmNativeAEADEncryption()
  119. {
  120. TestEncryptionMethod(typeof(AEADAesGcmNativeEncryptor), "aes-128-gcm");
  121. TestEncryptionMethod(typeof(AEADAesGcmNativeEncryptor), "aes-192-gcm");
  122. TestEncryptionMethod(typeof(AEADAesGcmNativeEncryptor), "aes-256-gcm");
  123. }
  124. [TestMethod]
  125. public void TestNaClAEADEncryption()
  126. {
  127. TestEncryptionMethod(typeof(AEADNaClEncryptor), "chacha20-ietf-poly1305");
  128. TestEncryptionMethod(typeof(AEADNaClEncryptor), "xchacha20-ietf-poly1305");
  129. }
  130. [TestMethod]
  131. public void TestNativeEncryption()
  132. {
  133. TestEncryptionMethod(typeof(StreamTableNativeEncryptor), "plain");
  134. TestEncryptionMethod(typeof(StreamRc4NativeEncryptor), "rc4");
  135. TestEncryptionMethod(typeof(StreamRc4NativeEncryptor), "rc4-md5");
  136. }
  137. [TestMethod]
  138. public void TestNativeTableEncryption()
  139. {
  140. TestEncryptionMethod(typeof(StreamTableNativeEncryptor), "table");
  141. }
  142. [TestMethod]
  143. public void TestStreamAesBouncyCastleEncryption()
  144. {
  145. TestEncryptionMethod(typeof(StreamAesBouncyCastleEncryptor), "aes-256-cfb");
  146. }
  147. [TestMethod]
  148. public void TestStreamChachaNaClEncryption()
  149. {
  150. TestEncryptionMethod(typeof(StreamChachaNaClEncryptor), "chacha20-ietf");
  151. }
  152. }
  153. }