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.

StreamSodiumEncryptor.cs 3.7 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using Shadowsocks.Encryption.Exception;
  4. namespace Shadowsocks.Encryption.Stream
  5. {
  6. public class StreamSodiumEncryptor
  7. : StreamEncryptor, IDisposable
  8. {
  9. const int CIPHER_SALSA20 = 1;
  10. const int CIPHER_CHACHA20 = 2;
  11. const int CIPHER_CHACHA20_IETF = 3;
  12. const int SODIUM_BLOCK_SIZE = 64;
  13. protected int _encryptBytesRemaining;
  14. protected int _decryptBytesRemaining;
  15. protected ulong _encryptIC;
  16. protected ulong _decryptIC;
  17. protected byte[] _encryptBuf;
  18. protected byte[] _decryptBuf;
  19. public StreamSodiumEncryptor(string method, string password)
  20. : base(method, password)
  21. {
  22. _encryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE];
  23. _decryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE];
  24. }
  25. private static readonly Dictionary<string, EncryptorInfo> _ciphers = new Dictionary<string, EncryptorInfo> {
  26. { "salsa20", new EncryptorInfo(32, 8, CIPHER_SALSA20) },
  27. { "chacha20", new EncryptorInfo(32, 8, CIPHER_CHACHA20) },
  28. { "chacha20-ietf", new EncryptorInfo(32, 12, CIPHER_CHACHA20_IETF) }
  29. };
  30. protected override Dictionary<string, EncryptorInfo> getCiphers()
  31. {
  32. return _ciphers;
  33. }
  34. public static List<string> SupportedCiphers()
  35. {
  36. return new List<string>(_ciphers.Keys);
  37. }
  38. protected override void cipherUpdate(bool isEncrypt, int length, byte[] buf, byte[] outbuf)
  39. {
  40. // TODO write a unidirection cipher so we don't have to if if if
  41. int bytesRemaining;
  42. ulong ic;
  43. byte[] sodiumBuf;
  44. byte[] iv;
  45. int ret = -1;
  46. if (isEncrypt)
  47. {
  48. bytesRemaining = _encryptBytesRemaining;
  49. ic = _encryptIC;
  50. sodiumBuf = _encryptBuf;
  51. iv = _encryptIV;
  52. }
  53. else
  54. {
  55. bytesRemaining = _decryptBytesRemaining;
  56. ic = _decryptIC;
  57. sodiumBuf = _decryptBuf;
  58. iv = _decryptIV;
  59. }
  60. int padding = bytesRemaining;
  61. Buffer.BlockCopy(buf, 0, sodiumBuf, padding, length);
  62. switch (_cipher)
  63. {
  64. case CIPHER_SALSA20:
  65. ret = Sodium.crypto_stream_salsa20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key);
  66. break;
  67. case CIPHER_CHACHA20:
  68. ret = Sodium.crypto_stream_chacha20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key);
  69. break;
  70. case CIPHER_CHACHA20_IETF:
  71. ret = Sodium.crypto_stream_chacha20_ietf_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, (uint)ic, _key);
  72. break;
  73. }
  74. if (ret != 0) throw new CryptoErrorException();
  75. Buffer.BlockCopy(sodiumBuf, padding, outbuf, 0, length);
  76. padding += length;
  77. ic += (ulong)padding / SODIUM_BLOCK_SIZE;
  78. bytesRemaining = padding % SODIUM_BLOCK_SIZE;
  79. if (isEncrypt)
  80. {
  81. _encryptBytesRemaining = bytesRemaining;
  82. _encryptIC = ic;
  83. }
  84. else
  85. {
  86. _decryptBytesRemaining = bytesRemaining;
  87. _decryptIC = ic;
  88. }
  89. }
  90. public override void Dispose()
  91. {
  92. }
  93. }
  94. }