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.

StreamEncryptor.cs 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using Shadowsocks.Encryption.CircularBuffer;
  6. using Shadowsocks.Controller;
  7. namespace Shadowsocks.Encryption.Stream
  8. {
  9. public abstract class StreamEncryptor
  10. : EncryptorBase
  11. {
  12. // for UDP only
  13. protected static byte[] _udpTmpBuf = new byte[65536];
  14. // every connection should create its own buffer
  15. private ByteCircularBuffer _encCircularBuffer = new ByteCircularBuffer(TCPHandler.BufferSize * 2);
  16. private ByteCircularBuffer _decCircularBuffer = new ByteCircularBuffer(TCPHandler.BufferSize * 2);
  17. protected Dictionary<string, EncryptorInfo> ciphers;
  18. protected byte[] _encryptIV;
  19. protected byte[] _decryptIV;
  20. // Is first packet
  21. protected bool _decryptIVReceived;
  22. protected bool _encryptIVSent;
  23. protected string _method;
  24. protected int _cipher;
  25. // internal name in the crypto library
  26. protected string _innerLibName;
  27. protected EncryptorInfo CipherInfo;
  28. // long-time master key
  29. protected static byte[] _key = null;
  30. protected int keyLen;
  31. protected int ivLen;
  32. public StreamEncryptor(string method, string password)
  33. : base(method, password)
  34. {
  35. InitEncryptorInfo(method);
  36. InitKey(password);
  37. }
  38. protected abstract Dictionary<string, EncryptorInfo> getCiphers();
  39. private void InitEncryptorInfo(string method)
  40. {
  41. method = method.ToLower();
  42. _method = method;
  43. ciphers = getCiphers();
  44. CipherInfo = ciphers[_method];
  45. _innerLibName = CipherInfo.InnerLibName;
  46. _cipher = CipherInfo.Type;
  47. if (_cipher == 0)
  48. {
  49. throw new System.Exception("method not found");
  50. }
  51. keyLen = CipherInfo.KeySize;
  52. ivLen = CipherInfo.IvSize;
  53. }
  54. private void InitKey(string password)
  55. {
  56. byte[] passbuf = Encoding.UTF8.GetBytes(password);
  57. if (_key == null) _key = new byte[keyLen];
  58. if (_key.Length != keyLen) Array.Resize(ref _key, keyLen);
  59. LegacyDeriveKey(passbuf, _key, keyLen);
  60. }
  61. public static void LegacyDeriveKey(byte[] password, byte[] key, int keylen)
  62. {
  63. byte[] result = new byte[password.Length + MD5_LEN];
  64. int i = 0;
  65. byte[] md5sum = null;
  66. while (i < keylen)
  67. {
  68. if (i == 0)
  69. {
  70. md5sum = CryptoUtils.MD5(password);
  71. }
  72. else
  73. {
  74. Array.Copy(md5sum, 0, result, 0, MD5_LEN);
  75. Array.Copy(password, 0, result, MD5_LEN, password.Length);
  76. md5sum = CryptoUtils.MD5(result);
  77. }
  78. Array.Copy(md5sum, 0, key, i, Math.Min(MD5_LEN, keylen - i));
  79. i += MD5_LEN;
  80. }
  81. }
  82. protected virtual void initCipher(byte[] iv, bool isEncrypt)
  83. {
  84. if (isEncrypt)
  85. {
  86. _encryptIV = new byte[ivLen];
  87. Array.Copy(iv, _encryptIV, ivLen);
  88. }
  89. else
  90. {
  91. _decryptIV = new byte[ivLen];
  92. Array.Copy(iv, _decryptIV, ivLen);
  93. }
  94. }
  95. protected abstract void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf);
  96. protected static void randBytes(byte[] buf, int length) { RNG.GetBytes(buf, length); }
  97. #region TCP
  98. public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
  99. {
  100. int cipherOffset = 0;
  101. Debug.Assert(_encCircularBuffer != null, "_encCircularBuffer != null");
  102. _encCircularBuffer.Put(buf, 0, length);
  103. if (!_encryptIVSent)
  104. {
  105. // Generate IV
  106. byte[] ivBytes = new byte[ivLen];
  107. randBytes(ivBytes, ivLen);
  108. initCipher(ivBytes, true);
  109. Array.Copy(ivBytes, 0, outbuf, 0, ivLen);
  110. cipherOffset = ivLen;
  111. _encryptIVSent = true;
  112. }
  113. int size = _encCircularBuffer.Size;
  114. byte[] plain = _encCircularBuffer.Get(size);
  115. byte[] cipher = new byte[size];
  116. cipherUpdate(true, size, plain, cipher);
  117. Buffer.BlockCopy(cipher, 0, outbuf, cipherOffset, size);
  118. outlength = size + cipherOffset;
  119. }
  120. public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
  121. {
  122. Debug.Assert(_decCircularBuffer != null, "_circularBuffer != null");
  123. _decCircularBuffer.Put(buf, 0, length);
  124. if (!_decryptIVReceived)
  125. {
  126. if (_decCircularBuffer.Size <= ivLen)
  127. {
  128. // we need more data
  129. outlength = 0;
  130. return;
  131. }
  132. // start decryption
  133. _decryptIVReceived = true;
  134. if (ivLen > 0)
  135. {
  136. byte[] iv = _decCircularBuffer.Get(ivLen);
  137. initCipher(iv, false);
  138. }
  139. else initCipher(Array.Empty<byte>(), false);
  140. }
  141. byte[] cipher = _decCircularBuffer.ToArray();
  142. cipherUpdate(false, cipher.Length, cipher, outbuf);
  143. // move pointer only
  144. _decCircularBuffer.Skip(_decCircularBuffer.Size);
  145. outlength = cipher.Length;
  146. // done the decryption
  147. }
  148. #endregion
  149. #region UDP
  150. public override void EncryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength)
  151. {
  152. // Generate IV
  153. randBytes(outbuf, ivLen);
  154. initCipher(outbuf, true);
  155. lock (_udpTmpBuf)
  156. {
  157. cipherUpdate(true, length, buf, _udpTmpBuf);
  158. outlength = length + ivLen;
  159. Buffer.BlockCopy(_udpTmpBuf, 0, outbuf, ivLen, length);
  160. }
  161. }
  162. public override void DecryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength)
  163. {
  164. // Get IV from first pos
  165. initCipher(buf, false);
  166. outlength = length - ivLen;
  167. lock (_udpTmpBuf)
  168. {
  169. // C# could be multi-threaded
  170. Buffer.BlockCopy(buf, ivLen, _udpTmpBuf, 0, length - ivLen);
  171. cipherUpdate(false, length - ivLen, _udpTmpBuf, outbuf);
  172. }
  173. }
  174. #endregion
  175. }
  176. }