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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. throw new System.Exception("method not found");
  49. }
  50. keyLen = CipherInfo.KeySize;
  51. ivLen = CipherInfo.IvSize;
  52. }
  53. private void InitKey(string password)
  54. {
  55. byte[] passbuf = Encoding.UTF8.GetBytes(password);
  56. if (_key == null) _key = new byte[keyLen];
  57. if (_key.Length != keyLen) Array.Resize(ref _key, keyLen);
  58. LegacyDeriveKey(passbuf, _key, keyLen);
  59. }
  60. public static void LegacyDeriveKey(byte[] password, byte[] key, int keylen)
  61. {
  62. byte[] result = new byte[password.Length + MD5_LEN];
  63. int i = 0;
  64. byte[] md5sum = null;
  65. while (i < keylen) {
  66. if (i == 0) {
  67. md5sum = MbedTLS.MD5(password);
  68. } else {
  69. Array.Copy(md5sum, 0, result, 0, MD5_LEN);
  70. Array.Copy(password, 0, result, MD5_LEN, password.Length);
  71. md5sum = MbedTLS.MD5(result);
  72. }
  73. Array.Copy(md5sum, 0, key, i, Math.Min(MD5_LEN, keylen - i));
  74. i += MD5_LEN;
  75. }
  76. }
  77. protected virtual void initCipher(byte[] iv, bool isEncrypt)
  78. {
  79. if (isEncrypt) {
  80. _encryptIV = new byte[ivLen];
  81. Array.Copy(iv, _encryptIV, ivLen);
  82. } else {
  83. _decryptIV = new byte[ivLen];
  84. Array.Copy(iv, _decryptIV, ivLen);
  85. }
  86. }
  87. protected abstract void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf);
  88. protected static void randBytes(byte[] buf, int length) { RNG.GetBytes(buf, length); }
  89. #region TCP
  90. public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
  91. {
  92. int cipherOffset = 0;
  93. Debug.Assert(_encCircularBuffer != null, "_encCircularBuffer != null");
  94. _encCircularBuffer.Put(buf, 0, length);
  95. if (! _encryptIVSent) {
  96. // Generate IV
  97. byte[] ivBytes = new byte[ivLen];
  98. randBytes(ivBytes, ivLen);
  99. initCipher(ivBytes, true);
  100. Array.Copy(ivBytes, 0, outbuf, 0, ivLen);
  101. cipherOffset = ivLen;
  102. _encryptIVSent = true;
  103. }
  104. int size = _encCircularBuffer.Size;
  105. byte[] plain = _encCircularBuffer.Get(size);
  106. byte[] cipher = new byte[size];
  107. cipherUpdate(true, size, plain, cipher);
  108. Buffer.BlockCopy(cipher, 0, outbuf, cipherOffset, size);
  109. outlength = size + cipherOffset;
  110. }
  111. public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
  112. {
  113. Debug.Assert(_decCircularBuffer != null, "_circularBuffer != null");
  114. _decCircularBuffer.Put(buf, 0, length);
  115. if (! _decryptIVReceived) {
  116. if (_decCircularBuffer.Size <= ivLen) {
  117. // we need more data
  118. outlength = 0;
  119. return;
  120. }
  121. // start decryption
  122. _decryptIVReceived = true;
  123. byte[] iv = _decCircularBuffer.Get(ivLen);
  124. initCipher(iv, false);
  125. }
  126. byte[] cipher = _decCircularBuffer.ToArray();
  127. cipherUpdate(false, cipher.Length, cipher, outbuf);
  128. // move pointer only
  129. _decCircularBuffer.Skip(_decCircularBuffer.Size);
  130. outlength = cipher.Length;
  131. // done the decryption
  132. }
  133. #endregion
  134. #region UDP
  135. public override void EncryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength)
  136. {
  137. // Generate IV
  138. randBytes(outbuf, ivLen);
  139. initCipher(outbuf, true);
  140. lock (_udpTmpBuf) {
  141. cipherUpdate(true, length, buf, _udpTmpBuf);
  142. outlength = length + ivLen;
  143. Buffer.BlockCopy(_udpTmpBuf, 0, outbuf, ivLen, length);
  144. }
  145. }
  146. public override void DecryptUDP(byte[] buf, int length, byte[] outbuf, out int outlength)
  147. {
  148. // Get IV from first pos
  149. initCipher(buf, false);
  150. outlength = length - ivLen;
  151. lock (_udpTmpBuf) {
  152. // C# could be multi-threaded
  153. Buffer.BlockCopy(buf, ivLen, _udpTmpBuf, 0, length - ivLen);
  154. cipherUpdate(false, length - ivLen, _udpTmpBuf, outbuf);
  155. }
  156. }
  157. #endregion
  158. }
  159. }