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.

PolarSSL.cs 2.3 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
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
10 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 class PolarSSL
  10. {
  11. const string DLLNAME = "libsscrypto";
  12. public const int AES_CTX_SIZE = 8 + 4 * 68;
  13. public const int AES_ENCRYPT = 1;
  14. public const int AES_DECRYPT = 0;
  15. static PolarSSL()
  16. {
  17. string dllPath = Utils.GetTempPath("libsscrypto.dll");
  18. try
  19. {
  20. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  21. }
  22. catch (IOException)
  23. {
  24. }
  25. catch (Exception e)
  26. {
  27. Logging.LogUsefulException(e);
  28. }
  29. LoadLibrary(dllPath);
  30. }
  31. [DllImport("Kernel32.dll")]
  32. private static extern IntPtr LoadLibrary(string path);
  33. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  34. public extern static void aes_init(IntPtr ctx);
  35. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  36. public extern static void aes_free(IntPtr ctx);
  37. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  38. public extern static int aes_setkey_enc(IntPtr ctx, byte[] key, int keysize);
  39. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  40. public extern static int aes_crypt_cfb128(IntPtr ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output);
  41. public const int ARC4_CTX_SIZE = 264;
  42. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  43. public extern static void arc4_init(IntPtr ctx);
  44. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  45. public extern static void arc4_free(IntPtr ctx);
  46. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  47. public extern static void arc4_setup(IntPtr ctx, byte[] key, int keysize);
  48. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  49. public extern static int arc4_crypt(IntPtr ctx, int length, byte[] input, byte[] output);
  50. }
  51. }