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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using NLog;
  5. using Shadowsocks.Controller;
  6. using Shadowsocks.Properties;
  7. using Shadowsocks.Util;
  8. namespace Shadowsocks.Encryption
  9. {
  10. public static class MbedTLS
  11. {
  12. private static Logger logger = LogManager.GetCurrentClassLogger();
  13. private const string DLLNAME = "libsscrypto.dll";
  14. public const int MBEDTLS_ENCRYPT = 1;
  15. public const int MBEDTLS_DECRYPT = 0;
  16. static MbedTLS()
  17. {
  18. string dllPath = Utils.GetTempPath(DLLNAME);
  19. try
  20. {
  21. FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll);
  22. }
  23. catch (IOException)
  24. {
  25. }
  26. catch (System.Exception e)
  27. {
  28. logger.LogUsefulException(e);
  29. }
  30. LoadLibrary(dllPath);
  31. }
  32. public static byte[] MD5(byte[] input)
  33. {
  34. byte[] output = new byte[16];
  35. if (md5_ret(input, (uint) input.Length, output) != 0)
  36. throw new System.Exception("mbedtls: MD5 failure");
  37. return output;
  38. }
  39. [DllImport("Kernel32.dll")]
  40. private static extern IntPtr LoadLibrary(string path);
  41. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  42. public static extern int md5_ret(byte[] input, uint ilen, byte[] output);
  43. /// <summary>
  44. /// Get cipher ctx size for unmanaged memory allocation
  45. /// </summary>
  46. /// <returns></returns>
  47. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  48. public static extern int cipher_get_size_ex();
  49. #region Cipher layer wrappers
  50. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  51. public static extern IntPtr cipher_info_from_string(string cipher_name);
  52. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  53. public static extern void cipher_init(IntPtr ctx);
  54. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  55. public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info);
  56. // XXX: Check operation before using it
  57. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  58. public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation);
  59. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  60. public static extern int cipher_set_iv(IntPtr ctx, byte[] iv, int iv_len);
  61. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  62. public static extern int cipher_reset(IntPtr ctx);
  63. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  64. public static extern int cipher_update(IntPtr ctx, byte[] input, int ilen, byte[] output, ref int olen);
  65. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  66. public static extern void cipher_free(IntPtr ctx);
  67. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  68. public static extern int cipher_auth_encrypt(IntPtr ctx,
  69. byte[] iv, uint iv_len,
  70. IntPtr ad, uint ad_len,
  71. byte[] input, uint ilen,
  72. byte[] output, ref uint olen,
  73. byte[] tag, uint tag_len);
  74. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  75. public static extern int cipher_auth_decrypt(IntPtr ctx,
  76. byte[] iv, uint iv_len,
  77. IntPtr ad, uint ad_len,
  78. byte[] input, uint ilen,
  79. byte[] output, ref uint olen,
  80. byte[] tag, uint tag_len);
  81. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  82. public static extern int hkdf(byte[] salt,
  83. int salt_len, byte[] ikm, int ikm_len,
  84. byte[] info, int info_len, byte[] okm,
  85. int okm_len);
  86. #endregion
  87. }
  88. }