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.

MbedTLS.cs 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 MbedTLS
  10. {
  11. const string DLLNAME = "libsscrypto";
  12. public const int MBEDTLS_ENCRYPT = 1;
  13. public const int MBEDTLS_DECRYPT = 0;
  14. static MbedTLS()
  15. {
  16. string dllPath = Utils.GetTempPath("libsscrypto.dll");
  17. try
  18. {
  19. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  20. }
  21. catch (IOException)
  22. {
  23. }
  24. catch (Exception e)
  25. {
  26. Logging.LogUsefulException(e);
  27. }
  28. LoadLibrary(dllPath);
  29. }
  30. public static byte[] MD5(byte[] input)
  31. {
  32. byte[] output = new byte[16];
  33. md5(input, (uint)input.Length, output);
  34. return output;
  35. }
  36. [DllImport("Kernel32.dll")]
  37. private static extern IntPtr LoadLibrary(string path);
  38. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  39. public static extern IntPtr cipher_info_from_string(string cipher_name);
  40. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  41. public static extern void cipher_init(IntPtr ctx);
  42. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  43. public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info);
  44. // XXX: Check operation before using it
  45. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  46. public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation);
  47. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  48. public static extern int cipher_set_iv(IntPtr ctx, byte[] iv, int iv_len);
  49. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  50. public static extern int cipher_reset(IntPtr ctx);
  51. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  52. public static extern int cipher_update(IntPtr ctx, byte[] input, int ilen, byte[] output, ref int olen);
  53. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  54. public static extern void cipher_free(IntPtr ctx);
  55. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  56. public static extern void md5(byte[] input, uint ilen, byte[] output);
  57. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  58. public static extern int cipher_get_size_ex();
  59. }
  60. }