From f6c490fc6282823af0de606ead83865a88acf62f Mon Sep 17 00:00:00 2001 From: David Sparer Date: Thu, 13 Oct 2016 17:17:34 -0600 Subject: [PATCH] Created a key derivation interface and initial Pkcs5 implementation --- .../KeyDerivation/IKeyDerivationFunction.cs | 8 +++++ .../KeyDerivation/Pkcs5S2KeyGenerator.cs | 31 +++++++++++++++++++ mRemoteV1/mRemoteV1.csproj | 2 ++ 3 files changed, 41 insertions(+) create mode 100644 mRemoteV1/Security/KeyDerivation/IKeyDerivationFunction.cs create mode 100644 mRemoteV1/Security/KeyDerivation/Pkcs5S2KeyGenerator.cs diff --git a/mRemoteV1/Security/KeyDerivation/IKeyDerivationFunction.cs b/mRemoteV1/Security/KeyDerivation/IKeyDerivationFunction.cs new file mode 100644 index 00000000..5b081e93 --- /dev/null +++ b/mRemoteV1/Security/KeyDerivation/IKeyDerivationFunction.cs @@ -0,0 +1,8 @@ + +namespace mRemoteNG.Security.KeyDerivation +{ + public interface IKeyDerivationFunction + { + byte[] DeriveKey(string password, byte[] salt); + } +} \ No newline at end of file diff --git a/mRemoteV1/Security/KeyDerivation/Pkcs5S2KeyGenerator.cs b/mRemoteV1/Security/KeyDerivation/Pkcs5S2KeyGenerator.cs new file mode 100644 index 00000000..2bc3c907 --- /dev/null +++ b/mRemoteV1/Security/KeyDerivation/Pkcs5S2KeyGenerator.cs @@ -0,0 +1,31 @@ +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.Crypto.Generators; +using Org.BouncyCastle.Crypto.Parameters; + + +namespace mRemoteNG.Security.KeyDerivation +{ + public class Pkcs5S2KeyGenerator : IKeyDerivationFunction + { + private readonly int _iterations; + private readonly int _keyBitSize; + + public Pkcs5S2KeyGenerator(int keyBitSize = 256, int iterations = 1000) + { + _keyBitSize = keyBitSize; + _iterations = iterations; + } + + public byte[] DeriveKey(string password, byte[] salt) + { + var passwordInBytes = PbeParametersGenerator.Pkcs5PasswordToBytes(password.ToCharArray()); + + var keyGenerator = new Pkcs5S2ParametersGenerator(); + keyGenerator.Init(passwordInBytes, salt, _iterations); + + var keyParameter = (KeyParameter) keyGenerator.GenerateDerivedMacParameters(_keyBitSize); + var keyBytes = keyParameter.GetKey(); + return keyBytes; + } + } +} \ No newline at end of file diff --git a/mRemoteV1/mRemoteV1.csproj b/mRemoteV1/mRemoteV1.csproj index 7ea62e8e..1cc36857 100644 --- a/mRemoteV1/mRemoteV1.csproj +++ b/mRemoteV1/mRemoteV1.csproj @@ -192,6 +192,8 @@ + +