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.

SodiumEncryptor.cs 3.6 kB

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