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 2.6 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Shadowsocks.Controller;
  2. using Shadowsocks.Properties;
  3. using Shadowsocks.Util;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. namespace Shadowsocks.Encryption
  10. {
  11. public class Sodium
  12. {
  13. const string DLLNAME = "libsscrypto";
  14. const string DLLNAME2 = "libsscrypto2";
  15. static Sodium()
  16. {
  17. LoadSSCryptoLibrary();
  18. LoadSSCrypto2Library();
  19. }
  20. static void LoadSSCryptoLibrary()
  21. {
  22. string tempPath = Utils.GetTempPath();
  23. string dllPath = tempPath + "/libsscrypto.dll";
  24. try
  25. {
  26. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  27. LoadLibrary(dllPath);
  28. }
  29. catch (IOException)
  30. {
  31. }
  32. catch (Exception e)
  33. {
  34. Console.WriteLine(e.ToString());
  35. }
  36. }
  37. static void LoadSSCrypto2Library()
  38. {
  39. string tempPath = Utils.GetTempPath();
  40. string dllPath = tempPath + "/libsscrypto2.dll";
  41. try
  42. {
  43. FileManager.UncompressFile(dllPath, Resources.libsscrypto2_dll);
  44. LoadLibrary(dllPath);
  45. }
  46. catch (IOException)
  47. {
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine(e.ToString());
  52. }
  53. }
  54. [DllImport("Kernel32.dll")]
  55. private static extern IntPtr LoadLibrary(string path);
  56. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  57. public extern static void crypto_stream_salsa20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k);
  58. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  59. public extern static void crypto_stream_chacha20_xor_ic(byte[] c, byte[] m, ulong mlen, byte[] n, ulong ic, byte[] k);
  60. [DllImport(DLLNAME2, CallingConvention = CallingConvention.Cdecl)]
  61. public extern static int ss_gen_crc(byte[] buf, ref int buf_offset, ref int data_len,
  62. byte[] crc_buf, ref int crc_idx, int buf_size);
  63. [DllImport(DLLNAME2, CallingConvention = CallingConvention.Cdecl)]
  64. public extern static int ss_onetimeauth(byte[] auth,
  65. byte[] msg, int msg_len,
  66. byte[] iv, int iv_len,
  67. byte[] key, int key_len);
  68. }
  69. }