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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if (md5_ret(input, (uint) input.Length, output) != 0)
  34. throw new System.Exception("mbedtls: MD5 failure");
  35. return output;
  36. }
  37. [DllImport("Kernel32.dll")]
  38. private static extern IntPtr LoadLibrary(string path);
  39. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  40. public static extern int md5_ret(byte[] input, uint ilen, byte[] output);
  41. /// <summary>
  42. /// Get cipher ctx size for unmanaged memory allocation
  43. /// </summary>
  44. /// <returns></returns>
  45. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  46. public static extern int cipher_get_size_ex();
  47. #region Cipher layer wrappers
  48. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  49. public static extern IntPtr cipher_info_from_string(string cipher_name);
  50. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  51. public static extern void cipher_init(IntPtr ctx);
  52. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  53. public static extern int cipher_setup(IntPtr ctx, IntPtr cipher_info);
  54. // XXX: Check operation before using it
  55. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  56. public static extern int cipher_setkey(IntPtr ctx, byte[] key, int key_bitlen, int operation);
  57. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  58. public static extern int cipher_set_iv(IntPtr ctx, byte[] iv, int iv_len);
  59. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  60. public static extern int cipher_reset(IntPtr ctx);
  61. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  62. public static extern int cipher_update(IntPtr ctx, byte[] input, int ilen, byte[] output, ref int olen);
  63. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  64. public static extern void cipher_free(IntPtr ctx);
  65. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  66. public static extern int cipher_auth_encrypt(IntPtr ctx,
  67. byte[] iv, uint iv_len,
  68. IntPtr ad, uint ad_len,
  69. byte[] input, uint ilen,
  70. byte[] output, ref uint olen,
  71. byte[] tag, uint tag_len);
  72. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  73. public static extern int cipher_auth_decrypt(IntPtr ctx,
  74. byte[] iv, uint iv_len,
  75. IntPtr ad, uint ad_len,
  76. byte[] input, uint ilen,
  77. byte[] output, ref uint olen,
  78. byte[] tag, uint tag_len);
  79. [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
  80. public static extern int hkdf(byte[] salt,
  81. int salt_len, byte[] ikm, int ikm_len,
  82. byte[] info, int info_len, byte[] okm,
  83. int okm_len);
  84. #endregion
  85. }
  86. }