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 3.0 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. static Sodium()
  15. {
  16. string dllPath = Utils.GetTempPath(DLLNAME);
  17. try
  18. {
  19. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  20. }
  21. catch (IOException)
  22. {
  23. }
  24. catch (System.Exception e)
  25. {
  26. Logging.LogUsefulException(e);
  27. }
  28. LoadLibrary(dllPath);
  29. lock (_initLock)
  30. {
  31. if (!_initialized)
  32. {
  33. if (sodium_init() == -1)
  34. {
  35. throw new System.Exception("Failed to initialize sodium");
  36. }
  37. else /* 1 means already initialized; 0 means success */
  38. {
  39. _initialized = true;
  40. }
  41. }
  42. }
  43. }
  44. [DllImport("Kernel32.dll")]
  45. private static extern IntPtr LoadLibrary(string path);
  46. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  47. private static extern int sodium_init();
  48. #region AEAD
  49. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  50. public static extern int sodium_increment(byte[] n, int nlen);
  51. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  52. public static extern int crypto_aead_chacha20poly1305_ietf_encrypt(byte[] c, ref ulong clen_p, byte[] m,
  53. ulong mlen, byte[] ad, ulong adlen, byte[] nsec, byte[] npub, byte[] k);
  54. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  55. public static extern int crypto_aead_chacha20poly1305_ietf_decrypt(byte[] m, ref ulong mlen_p,
  56. byte[] nsec, byte[] c, ulong clen, byte[] ad, ulong adlen, byte[] npub, byte[] k);
  57. #endregion
  58. #region Stream
  59. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  60. public static extern int crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic,
  61. byte[] k);
  62. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  63. public static extern int crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic,
  64. byte[] k);
  65. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  66. public static extern int crypto_stream_chacha20_ietf_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, uint ic,
  67. byte[] k);
  68. #endregion
  69. }
  70. }