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 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Shadowsocks.Encryption;
  4. using System.Threading;
  5. using System.Collections.Generic;
  6. using Shadowsocks.Encryption.Stream;
  7. using System.Diagnostics;
  8. namespace Shadowsocks.Test
  9. {
  10. [TestClass]
  11. public class CryptographyTest
  12. {
  13. [TestMethod]
  14. public void TestMD5()
  15. {
  16. for (int len = 1; len < 64; len++)
  17. {
  18. System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
  19. byte[] bytes = new byte[len];
  20. var random = new Random();
  21. random.NextBytes(bytes);
  22. string md5str = Convert.ToBase64String(md5.ComputeHash(bytes));
  23. string md5str2 = Convert.ToBase64String(MbedTLS.MD5(bytes));
  24. Assert.IsTrue(md5str == md5str2);
  25. }
  26. }
  27. private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor)
  28. {
  29. RNG.Reload();
  30. byte[] plain = new byte[16384];
  31. byte[] cipher = new byte[plain.Length + 16];
  32. byte[] plain2 = new byte[plain.Length + 16];
  33. int outLen = 0;
  34. int outLen2 = 0;
  35. var random = new Random();
  36. random.NextBytes(plain);
  37. encryptor.Encrypt(plain, plain.Length, cipher, out outLen);
  38. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  39. Assert.AreEqual(plain.Length, outLen2);
  40. for (int j = 0; j < plain.Length; j++)
  41. {
  42. Assert.AreEqual(plain[j], plain2[j]);
  43. }
  44. encryptor.Encrypt(plain, 1000, cipher, out outLen);
  45. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  46. Assert.AreEqual(1000, outLen2);
  47. for (int j = 0; j < outLen2; j++)
  48. {
  49. Assert.AreEqual(plain[j], plain2[j]);
  50. }
  51. encryptor.Encrypt(plain, 12333, cipher, out outLen);
  52. decryptor.Decrypt(cipher, outLen, plain2, out outLen2);
  53. Assert.AreEqual(12333, outLen2);
  54. for (int j = 0; j < outLen2; j++)
  55. {
  56. Assert.AreEqual(plain[j], plain2[j]);
  57. }
  58. }
  59. private static bool encryptionFailed = false;
  60. private static object locker = new object();
  61. [TestMethod]
  62. public void TestMbedTLSEncryption()
  63. {
  64. encryptionFailed = false;
  65. // run it once before the multi-threading test to initialize global tables
  66. RunSingleMbedTLSEncryptionThread();
  67. List<Thread> threads = new List<Thread>();
  68. for (int i = 0; i < 10; i++)
  69. {
  70. Thread t = new Thread(new ThreadStart(RunSingleMbedTLSEncryptionThread));
  71. threads.Add(t);
  72. t.Start();
  73. }
  74. foreach (Thread t in threads)
  75. {
  76. t.Join();
  77. }
  78. RNG.Close();
  79. Assert.IsFalse(encryptionFailed);
  80. }
  81. private void RunSingleMbedTLSEncryptionThread()
  82. {
  83. try
  84. {
  85. for (int i = 0; i < 100; i++)
  86. {
  87. IEncryptor encryptor;
  88. IEncryptor decryptor;
  89. encryptor = new StreamMbedTLSEncryptor("aes-256-cfb", "barfoo!");
  90. decryptor = new StreamMbedTLSEncryptor("aes-256-cfb", "barfoo!");
  91. RunEncryptionRound(encryptor, decryptor);
  92. }
  93. }
  94. catch
  95. {
  96. encryptionFailed = true;
  97. throw;
  98. }
  99. }
  100. [TestMethod]
  101. public void TestRC4Encryption()
  102. {
  103. encryptionFailed = false;
  104. // run it once before the multi-threading test to initialize global tables
  105. RunSingleRC4EncryptionThread();
  106. List<Thread> threads = new List<Thread>();
  107. for (int i = 0; i < 10; i++)
  108. {
  109. Thread t = new Thread(new ThreadStart(RunSingleRC4EncryptionThread));
  110. threads.Add(t);
  111. t.Start();
  112. }
  113. foreach (Thread t in threads)
  114. {
  115. t.Join();
  116. }
  117. RNG.Close();
  118. Assert.IsFalse(encryptionFailed);
  119. }
  120. private void RunSingleRC4EncryptionThread()
  121. {
  122. try
  123. {
  124. for (int i = 0; i < 100; i++)
  125. {
  126. var random = new Random();
  127. IEncryptor encryptor;
  128. IEncryptor decryptor;
  129. encryptor = new StreamMbedTLSEncryptor("rc4-md5", "barfoo!");
  130. decryptor = new StreamMbedTLSEncryptor("rc4-md5", "barfoo!");
  131. RunEncryptionRound(encryptor, decryptor);
  132. }
  133. }
  134. catch
  135. {
  136. encryptionFailed = true;
  137. throw;
  138. }
  139. }
  140. [TestMethod]
  141. public void TestSodiumEncryption()
  142. {
  143. encryptionFailed = false;
  144. // run it once before the multi-threading test to initialize global tables
  145. RunSingleSodiumEncryptionThread();
  146. List<Thread> threads = new List<Thread>();
  147. for (int i = 0; i < 10; i++)
  148. {
  149. Thread t = new Thread(new ThreadStart(RunSingleSodiumEncryptionThread));
  150. threads.Add(t);
  151. t.Start();
  152. }
  153. foreach (Thread t in threads)
  154. {
  155. t.Join();
  156. }
  157. RNG.Close();
  158. Assert.IsFalse(encryptionFailed);
  159. }
  160. private void RunSingleSodiumEncryptionThread()
  161. {
  162. try
  163. {
  164. for (int i = 0; i < 100; i++)
  165. {
  166. var random = new Random();
  167. IEncryptor encryptor;
  168. IEncryptor decryptor;
  169. encryptor = new StreamSodiumEncryptor("salsa20", "barfoo!");
  170. decryptor = new StreamSodiumEncryptor("salsa20", "barfoo!");
  171. RunEncryptionRound(encryptor, decryptor);
  172. }
  173. }
  174. catch
  175. {
  176. encryptionFailed = true;
  177. throw;
  178. }
  179. }
  180. [TestMethod]
  181. public void TestOpenSSLEncryption()
  182. {
  183. encryptionFailed = false;
  184. // run it once before the multi-threading test to initialize global tables
  185. RunSingleOpenSSLEncryptionThread();
  186. List<Thread> threads = new List<Thread>();
  187. for (int i = 0; i < 10; i++)
  188. {
  189. Thread t = new Thread(new ThreadStart(RunSingleOpenSSLEncryptionThread));
  190. threads.Add(t);
  191. t.Start();
  192. }
  193. foreach (Thread t in threads)
  194. {
  195. t.Join();
  196. }
  197. RNG.Close();
  198. Assert.IsFalse(encryptionFailed);
  199. }
  200. private void RunSingleOpenSSLEncryptionThread()
  201. {
  202. try
  203. {
  204. for (int i = 0; i < 100; i++)
  205. {
  206. var random = new Random();
  207. IEncryptor encryptor;
  208. IEncryptor decryptor;
  209. encryptor = new StreamOpenSSLEncryptor("aes-256-cfb", "barfoo!");
  210. decryptor = new StreamOpenSSLEncryptor("aes-256-cfb", "barfoo!");
  211. RunEncryptionRound(encryptor, decryptor);
  212. }
  213. }
  214. catch
  215. {
  216. encryptionFailed = true;
  217. throw;
  218. }
  219. }
  220. }
  221. }