WIP - Implementing HMAC with BouncyCastle

This commit is contained in:
bg45
2017-10-22 06:18:57 -04:00
committed by BONNEVILLE Geoffroy
parent c82d6d001d
commit 84e7afc819
9 changed files with 56 additions and 37 deletions

View File

@@ -253,8 +253,8 @@
<HintPath>..\packages\Microsoft.Toolkit.Uwp.Notifications.2.0.0\lib\dotnet\Microsoft.Toolkit.Uwp.Notifications.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ModernKeePassLib, Version=2.28.1.4000, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ModernKeePassLib.2.28.4000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath>
<Reference Include="ModernKeePassLib, Version=2.37.0.1000, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ModernKeePassLib.2.37.1000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Splat, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">

View File

@@ -3,7 +3,7 @@
<package id="Microsoft.NETCore.Platforms" version="2.0.0" targetFramework="win81" />
<package id="Microsoft.NETCore.Portable.Compatibility" version="1.0.2" targetFramework="win81" />
<package id="Microsoft.Toolkit.Uwp.Notifications" version="2.0.0" targetFramework="win81" />
<package id="ModernKeePassLib" version="2.28.4000" targetFramework="win81" />
<package id="ModernKeePassLib" version="2.37.1000" targetFramework="win81" />
<package id="NETStandard.Library" version="2.0.0" targetFramework="win81" />
<package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="win81" />
<package id="Splat" version="2.0.0" targetFramework="win81" />

View File

@@ -21,10 +21,10 @@ namespace ModernKeePassLib.Test.Shared.Utility
"QMdny4VogAAAABJRU5ErkJggg==";
[Test ()]
public async void TestLoadImage ()
public void TestLoadImage ()
{
var testData = Convert.FromBase64String (testImageData);
var image = await GfxUtil.LoadImage (testData);
var image = GfxUtil.ScaleImage(testData, 16, 16).GetAwaiter().GetResult();
Assert.That (image.Width, Is.EqualTo (16));
Assert.That (image.Height, Is.EqualTo (16));
}

View File

@@ -25,8 +25,10 @@ using System.Text;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Native;
using ModernKeePassLib.Utility;
using Org.BouncyCastle.Asn1.Pkcs;
namespace ModernKeePassLib.Cryptography
{
@@ -111,20 +113,7 @@ namespace ModernKeePassLib.Cryptography
while(iPos < cbOut)
{
Debug.Assert(pbHash.Length == 64);
byte[] pbR = MemUtil.UInt64ToBytes(r);
#if ModernKeePassLib
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbR));
byte[] pbPart;
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbPart);
int cbCopy = Math.Min(cbOut - iPos, pbPart.Length);
Debug.Assert(cbCopy > 0);
Array.Copy(pbPart, 0, pbRet, iPos, cbCopy);
iPos += cbCopy;
++r;
MemUtil.ZeroByteArray(pbPart);
#else
using (HMACSHA256 h = new HMACSHA256(pbHash))
using(HMACSHA256 h = new HMACSHA256(pbHash))
{
byte[] pbR = MemUtil.UInt64ToBytes(r);
byte[] pbPart = h.ComputeHash(pbR);
@@ -138,7 +127,6 @@ namespace ModernKeePassLib.Cryptography
MemUtil.ZeroByteArray(pbPart);
}
#endif
}
Debug.Assert(iPos == cbOut);
}

View File

@@ -0,0 +1,35 @@
using System;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Parameters;
namespace ModernKeePassLib.Cryptography.Hash
{
public class HMACSHA256: IDisposable
{
private readonly HMac _hmac;
public HMACSHA256(byte[] key)
{
_hmac = new HMac(new Sha256Digest());
_hmac.Init(new KeyParameter(key));
}
public byte[] ComputeHash(byte[] value)
{
if (value == null) throw new ArgumentNullException("value");
byte[] resBuf = new byte[_hmac.GetMacSize()];
_hmac.BlockUpdate(value, 0, value.Length);
_hmac.DoFinal(resBuf, 0);
return resBuf;
}
public void Dispose()
{
_hmac.Reset();
}
}
}

View File

@@ -687,8 +687,8 @@ namespace ModernKeePassLib.Cryptography
private static void HmacEval(byte[] pbKey, byte[] pbMsg,
byte[] pbExpc, string strID)
{
// WinRT
#if ModernKeePassLib
// WinRT
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbKey));
h.Append(CryptographicBuffer.CreateFromByteArray(pbMsg));
var pbHash = h.GetValueAndReset().ToArray();

View File

@@ -95,11 +95,11 @@ namespace ModernKeePassLib.Keys
{
try
{
string str = StrUtil.Utf8.GetString(pb, 0, pb.Length);
#if ModernKeePassLib
// TODO: find a way to implement this
return true;
#else
string str = StrUtil.Utf8.GetString(pb);
return str.IsNormalized(NormalizationForm.FormC);
#endif
}

View File

@@ -61,6 +61,7 @@
<Compile Include="Cryptography\Cipher\ICipherEngine.cs" />
<Compile Include="Cryptography\CryptoUtil.cs" />
<Compile Include="Cryptography\Hash\Blake2b.cs" />
<Compile Include="Cryptography\Hash\HMACSHA256.cs" />
<Compile Include="Cryptography\HmacOtp.cs" />
<Compile Include="Cryptography\KeyDerivation\AesKdf.cs" />
<Compile Include="Cryptography\KeyDerivation\AesKdf.GCrypt.cs" />

View File

@@ -32,13 +32,13 @@ using System.IO.Compression;
#endif
#if ModernKeePassLib
//using PCLStorage;
using Windows.Storage;
#endif
using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Delegates;
using ModernKeePassLib.Interfaces;
@@ -459,15 +459,10 @@ namespace ModernKeePassLib.Serialization
byte[] pbHeaderHmac;
byte[] pbBlockKey = HmacBlockStream.GetHmacKey64(
pbKey, ulong.MaxValue);
#if ModernKeePassLib
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbHeader));
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHeaderHmac);
#else
using (HMACSHA256 h = new HMACSHA256(pbBlockKey))
{
pbHeaderHmac = h.ComputeHash(pbHeader);
}
#endif
MemUtil.ZeroByteArray(pbBlockKey);
return pbHeaderHmac;