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.9 kB

10 years ago
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. namespace Shadowsocks.Encryption
  6. {
  7. public class SodiumEncryptor
  8. : IVEncryptor, IDisposable
  9. {
  10. const int CIPHER_SALSA20 = 1;
  11. const int CIPHER_CHACHA20 = 2;
  12. const int CIPHER_CHACHA20_IETF = 3;
  13. const int SODIUM_BLOCK_SIZE = 64;
  14. static byte[] sodiumBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE];
  15. protected int _encryptBytesRemaining;
  16. protected int _decryptBytesRemaining;
  17. protected ulong _encryptIC;
  18. protected ulong _decryptIC;
  19. public SodiumEncryptor(string method, string password, bool onetimeauth, bool isudp)
  20. : base(method, password, onetimeauth, isudp)
  21. {
  22. InitKey(method, password);
  23. }
  24. private static Dictionary<string, Dictionary<string, int[]>> _ciphers = new Dictionary<string, Dictionary<string, int[]>> {
  25. { "salsa20", new Dictionary<string, int[]> { { "salsa20", new int[] { 32, 8, CIPHER_SALSA20 } } } },
  26. { "chacha20", new Dictionary<string, int[]> { { "chacha20", new int[] { 32, 8, CIPHER_CHACHA20 } } } },
  27. { "chacha20-ietf", new Dictionary<string, int[]> { { "chacha20-ietf", new int[] { 32, 12, CIPHER_CHACHA20_IETF } } } }
  28. };
  29. protected override Dictionary<string, Dictionary<string, int[]>> 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[] iv;
  43. // I'm tired. just add a big lock
  44. // let's optimize for RAM instead of CPU
  45. lock(sodiumBuf)
  46. {
  47. if (isCipher)
  48. {
  49. bytesRemaining = _encryptBytesRemaining;
  50. ic = _encryptIC;
  51. iv = _encryptIV;
  52. }
  53. else
  54. {
  55. bytesRemaining = _decryptBytesRemaining;
  56. ic = _decryptIC;
  57. iv = _decryptIV;
  58. }
  59. int padding = bytesRemaining;
  60. Buffer.BlockCopy(buf, 0, sodiumBuf, padding, length);
  61. switch (_cipher)
  62. {
  63. case CIPHER_SALSA20:
  64. Sodium.crypto_stream_salsa20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key);
  65. break;
  66. case CIPHER_CHACHA20:
  67. Sodium.crypto_stream_chacha20_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, ic, _key);
  68. break;
  69. case CIPHER_CHACHA20_IETF:
  70. Sodium.crypto_stream_chacha20_ietf_xor_ic(sodiumBuf, sodiumBuf, (ulong)(padding + length), iv, (uint)ic, _key);
  71. break;
  72. }
  73. Buffer.BlockCopy(sodiumBuf, padding, outbuf, 0, length);
  74. padding += length;
  75. ic += (ulong)padding / SODIUM_BLOCK_SIZE;
  76. bytesRemaining = padding % SODIUM_BLOCK_SIZE;
  77. if (isCipher)
  78. {
  79. _encryptBytesRemaining = bytesRemaining;
  80. _encryptIC = ic;
  81. }
  82. else
  83. {
  84. _decryptBytesRemaining = bytesRemaining;
  85. _decryptIC = ic;
  86. }
  87. }
  88. }
  89. public override void Dispose()
  90. {
  91. }
  92. }
  93. }