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 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 MbedTLS
  10. {
  11. private const string DLLNAME = "libsscrypto.dll";
  12. public const int MBEDTLS_ENCRYPT = 1;
  13. public const int MBEDTLS_DECRYPT = 0;
  14. static MbedTLS()
  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. }
  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 void md5(byte[] input, uint ilen, byte[] output);
  40. /// <summary>
  41. /// Get cipher ctx size for unmanaged memory allocation
  42. /// </summary>
  43. /// <returns></returns>
  44. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  45. public static extern int cipher_get_size_ex();
  46. #region Cipher layer wrappers
  47. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  48. public static extern IntPtr cipher_info_from_string(string cipher_name);
  49. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  50. public static extern void cipher_init(IntPtr ctx);
  51. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  52. public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info);
  53. // XXX: Check operation before using it
  54. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  55. public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation);
  56. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  57. public static extern int cipher_set_iv(IntPtr ctx, byte[] iv, int iv_len);
  58. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  59. public static extern int cipher_reset(IntPtr ctx);
  60. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  61. public static extern int cipher_update(IntPtr ctx, byte[] input, int ilen, byte[] output, ref int olen);
  62. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  63. public static extern void cipher_free(IntPtr ctx);
  64. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  65. public static extern int cipher_auth_encrypt(IntPtr ctx,
  66. byte[] iv, uint iv_len,
  67. IntPtr ad, uint ad_len,
  68. byte[] input, uint ilen,
  69. byte[] output, ref uint olen,
  70. byte[] tag, uint tag_len);
  71. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  72. public static extern int cipher_auth_decrypt(IntPtr ctx,
  73. byte[] iv, uint iv_len,
  74. IntPtr ad, uint ad_len,
  75. byte[] input, uint ilen,
  76. byte[] output, ref uint olen,
  77. byte[] tag, uint tag_len);
  78. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  79. public static extern int hkdf(byte[] salt,
  80. int salt_len, byte[] ikm, int ikm_len,
  81. byte[] info, int info_len, byte[] okm,
  82. int okm_len);
  83. #endregion
  84. }
  85. }