| @@ -24,7 +24,7 @@ namespace Shadowsocks.Encryption | |||||
| protected byte[] GetPasswordHash() | protected byte[] GetPasswordHash() | ||||
| { | { | ||||
| byte[] inputBytes = Encoding.UTF8.GetBytes(Password); | byte[] inputBytes = Encoding.UTF8.GetBytes(Password); | ||||
| byte[] hash = MD5.Create().ComputeHash(inputBytes); | |||||
| byte[] hash = MbedTLS.MD5(inputBytes); | |||||
| return hash; | return hash; | ||||
| } | } | ||||
| @@ -83,16 +83,15 @@ namespace Shadowsocks.Encryption | |||||
| byte[] md5sum = null; | byte[] md5sum = null; | ||||
| while (i < key.Length) | while (i < key.Length) | ||||
| { | { | ||||
| MD5 md5 = MD5.Create(); | |||||
| if (i == 0) | if (i == 0) | ||||
| { | { | ||||
| md5sum = md5.ComputeHash(password); | |||||
| md5sum = MbedTLS.MD5(password); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| md5sum.CopyTo(result, 0); | md5sum.CopyTo(result, 0); | ||||
| password.CopyTo(result, md5sum.Length); | password.CopyTo(result, md5sum.Length); | ||||
| md5sum = md5.ComputeHash(result); | |||||
| md5sum = MbedTLS.MD5(result); | |||||
| } | } | ||||
| md5sum.CopyTo(key, i); | md5sum.CopyTo(key, i); | ||||
| i += md5sum.Length; | i += md5sum.Length; | ||||
| @@ -0,0 +1,68 @@ | |||||
| using Shadowsocks.Controller; | |||||
| using Shadowsocks.Properties; | |||||
| using Shadowsocks.Util; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.IO; | |||||
| using System.Runtime.InteropServices; | |||||
| using System.Text; | |||||
| namespace Shadowsocks.Encryption | |||||
| { | |||||
| public class MbedTLS | |||||
| { | |||||
| const string DLLNAME = "libsscrypto"; | |||||
| static MbedTLS() | |||||
| { | |||||
| string tempPath = Utils.GetTempPath(); | |||||
| string dllPath = tempPath + "/libsscrypto.dll"; | |||||
| try | |||||
| { | |||||
| FileManager.UncompressFile(dllPath, Resources.libsscrypto_dll); | |||||
| } | |||||
| catch (IOException ex) | |||||
| { | |||||
| } | |||||
| catch (Exception e) | |||||
| { | |||||
| Console.WriteLine(e.ToString()); | |||||
| } | |||||
| LoadLibrary(dllPath); | |||||
| } | |||||
| [DllImport("Kernel32.dll")] | |||||
| private static extern IntPtr LoadLibrary(string path); | |||||
| public const int MD5_CTX_SIZE = 88; | |||||
| public static byte[] MD5(byte[] input) | |||||
| { | |||||
| IntPtr ctx = Marshal.AllocHGlobal(MD5_CTX_SIZE); | |||||
| byte[] output = new byte[16]; | |||||
| MbedTLS.md5_init(ctx); | |||||
| MbedTLS.md5_starts(ctx); | |||||
| MbedTLS.md5_update(ctx, input, (uint)input.Length); | |||||
| MbedTLS.md5_finish(ctx, output); | |||||
| MbedTLS.md5_free(ctx); | |||||
| Marshal.FreeHGlobal(ctx); | |||||
| return output; | |||||
| } | |||||
| [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
| public extern static void md5_init(IntPtr ctx); | |||||
| [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
| public extern static void md5_free(IntPtr ctx); | |||||
| [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
| public extern static void md5_starts(IntPtr ctx); | |||||
| [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
| public extern static void md5_update(IntPtr ctx, byte[] input, uint ilen ); | |||||
| [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] | |||||
| public extern static void md5_finish(IntPtr ctx, byte[] output); | |||||
| } | |||||
| } | |||||
| @@ -60,7 +60,7 @@ namespace Shadowsocks.Encryption | |||||
| realkey = new byte[keyLen]; | realkey = new byte[keyLen]; | ||||
| Array.Copy(_key, 0, temp, 0, keyLen); | Array.Copy(_key, 0, temp, 0, keyLen); | ||||
| Array.Copy(iv, 0, temp, keyLen, ivLen); | Array.Copy(iv, 0, temp, keyLen, ivLen); | ||||
| realkey = MD5.Create().ComputeHash(temp); | |||||
| realkey = MbedTLS.MD5(temp); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| @@ -184,6 +184,7 @@ | |||||
| <Compile Include="Encryption\EncryptorBase.cs" /> | <Compile Include="Encryption\EncryptorBase.cs" /> | ||||
| <Compile Include="Encryption\EncryptorFactory.cs" /> | <Compile Include="Encryption\EncryptorFactory.cs" /> | ||||
| <Compile Include="Encryption\IVEncryptor.cs" /> | <Compile Include="Encryption\IVEncryptor.cs" /> | ||||
| <Compile Include="Encryption\MbedTLS.cs" /> | |||||
| <Compile Include="Encryption\PolarSSL.cs" /> | <Compile Include="Encryption\PolarSSL.cs" /> | ||||
| <Compile Include="Encryption\PolarSSLEncryptor.cs" /> | <Compile Include="Encryption\PolarSSLEncryptor.cs" /> | ||||
| <Compile Include="Encryption\Sodium.cs" /> | <Compile Include="Encryption\Sodium.cs" /> | ||||
| @@ -22,6 +22,21 @@ namespace test | |||||
| Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3.2", "1.3.1") > 0); | Assert.IsTrue(UpdateChecker.Asset.CompareVersion("1.3.2", "1.3.1") > 0); | ||||
| } | } | ||||
| [TestMethod] | |||||
| public void TestMD5() | |||||
| { | |||||
| for (int len = 1; len < 64; len++) | |||||
| { | |||||
| System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); | |||||
| byte[] bytes = new byte[len]; | |||||
| var random = new Random(); | |||||
| random.NextBytes(bytes); | |||||
| string md5str = Convert.ToBase64String(md5.ComputeHash(bytes)); | |||||
| string md5str2 = Convert.ToBase64String(MbedTLS.MD5(bytes)); | |||||
| Assert.IsTrue(md5str == md5str2); | |||||
| } | |||||
| } | |||||
| private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor) | private void RunEncryptionRound(IEncryptor encryptor, IEncryptor decryptor) | ||||
| { | { | ||||
| byte[] plain = new byte[16384]; | byte[] plain = new byte[16384]; | ||||