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.

Sodium.cs 4.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using Shadowsocks.Controller;
  5. using Shadowsocks.Properties;
  6. using Shadowsocks.Util;
  7. namespace Shadowsocks.Encryption
  8. {
  9. public static class Sodium
  10. {
  11. private const string DLLNAME = "libsscrypto.dll";
  12. private static bool _initialized = false;
  13. private static readonly object _initLock = new object();
  14. public static bool AES256GCMAvailable { get; private set; } = false;
  15. static Sodium()
  16. {
  17. string dllPath = Utils.GetTempPath(DLLNAME);
  18. try
  19. {
  20. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  21. }
  22. catch (IOException)
  23. {
  24. }
  25. catch (System.Exception e)
  26. {
  27. Logging.LogUsefulException(e);
  28. }
  29. LoadLibrary(dllPath);
  30. lock (_initLock)
  31. {
  32. if (!_initialized)
  33. {
  34. if (sodium_init() == -1)
  35. {
  36. throw new System.Exception("Failed to initialize sodium");
  37. }
  38. else /* 1 means already initialized; 0 means success */
  39. {
  40. _initialized = true;
  41. }
  42. AES256GCMAvailable = crypto_aead_aes256gcm_is_available() == 1;
  43. Logging.Debug($"sodium: AES256GCMAvailable is {AES256GCMAvailable}");
  44. }
  45. }
  46. }
  47. [DllImport("Kernel32.dll")]
  48. private static extern IntPtr LoadLibrary(string path);
  49. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  50. private static extern int sodium_init();
  51. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  52. private static extern int crypto_aead_aes256gcm_is_available();
  53. #region AEAD
  54. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  55. public static extern int sodium_increment(byte[] n, int nlen);
  56. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  57. public static extern int crypto_aead_chacha20poly1305_ietf_encrypt(byte[] c, ref ulong clen_p, byte[] m,
  58. ulong mlen, byte[] ad, ulong adlen, byte[] nsec, byte[] npub, byte[] k);
  59. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  60. public static extern int crypto_aead_chacha20poly1305_ietf_decrypt(byte[] m, ref ulong mlen_p,
  61. byte[] nsec, byte[] c, ulong clen, byte[] ad, ulong adlen, byte[] npub, byte[] k);
  62. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  63. public static extern int crypto_aead_xchacha20poly1305_ietf_encrypt(byte[] c, ref ulong clen_p, byte[] m, ulong mlen,
  64. byte[] ad, ulong adlen, byte[] nsec, byte[] npub, byte[] k);
  65. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  66. public static extern int crypto_aead_xchacha20poly1305_ietf_decrypt(byte[] m, ref ulong mlen_p, byte[] nsec, byte[] c,
  67. ulong clen, byte[] ad, ulong adlen, byte[] npub, byte[] k);
  68. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  69. public static extern int crypto_aead_aes256gcm_encrypt(byte[] c, ref ulong clen_p, byte[] m, ulong mlen,
  70. byte[] ad, ulong adlen, byte[] nsec, byte[] npub, byte[] k);
  71. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  72. public static extern int crypto_aead_aes256gcm_decrypt(byte[] m, ref ulong mlen_p, byte[] nsec, byte[] c,
  73. ulong clen, byte[] ad, ulong adlen, byte[] npub, byte[] k);
  74. #endregion
  75. #region Stream
  76. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  77. public static extern int crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic,
  78. byte[] k);
  79. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  80. public static extern int crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic,
  81. byte[] k);
  82. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  83. public static extern int crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, uint ic,
  84. byte[] k);
  85. #endregion
  86. }
  87. }