mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 08:00:16 -04:00
WIP - Implementing HMAC with BouncyCastle
This commit is contained in:

committed by
BONNEVILLE Geoffroy

parent
c82d6d001d
commit
84e7afc819
@@ -253,8 +253,8 @@
|
|||||||
<HintPath>..\packages\Microsoft.Toolkit.Uwp.Notifications.2.0.0\lib\dotnet\Microsoft.Toolkit.Uwp.Notifications.dll</HintPath>
|
<HintPath>..\packages\Microsoft.Toolkit.Uwp.Notifications.2.0.0\lib\dotnet\Microsoft.Toolkit.Uwp.Notifications.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="ModernKeePassLib, Version=2.28.1.4000, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="ModernKeePassLib, Version=2.37.0.1000, Culture=neutral, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\ModernKeePassLib.2.28.4000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath>
|
<HintPath>..\packages\ModernKeePassLib.2.37.1000\lib\netstandard1.2\ModernKeePassLib.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="Splat, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
<Reference Include="Splat, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
@@ -3,7 +3,7 @@
|
|||||||
<package id="Microsoft.NETCore.Platforms" version="2.0.0" targetFramework="win81" />
|
<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.NETCore.Portable.Compatibility" version="1.0.2" targetFramework="win81" />
|
||||||
<package id="Microsoft.Toolkit.Uwp.Notifications" version="2.0.0" 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="NETStandard.Library" version="2.0.0" targetFramework="win81" />
|
||||||
<package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="win81" />
|
<package id="Portable.BouncyCastle" version="1.8.1.3" targetFramework="win81" />
|
||||||
<package id="Splat" version="2.0.0" targetFramework="win81" />
|
<package id="Splat" version="2.0.0" targetFramework="win81" />
|
||||||
|
@@ -21,10 +21,10 @@ namespace ModernKeePassLib.Test.Shared.Utility
|
|||||||
"QMdny4VogAAAABJRU5ErkJggg==";
|
"QMdny4VogAAAABJRU5ErkJggg==";
|
||||||
|
|
||||||
[Test ()]
|
[Test ()]
|
||||||
public async void TestLoadImage ()
|
public void TestLoadImage ()
|
||||||
{
|
{
|
||||||
var testData = Convert.FromBase64String (testImageData);
|
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.Width, Is.EqualTo (16));
|
||||||
Assert.That (image.Height, Is.EqualTo (16));
|
Assert.That (image.Height, Is.EqualTo (16));
|
||||||
}
|
}
|
||||||
|
@@ -25,8 +25,10 @@ using System.Text;
|
|||||||
|
|
||||||
using Windows.Security.Cryptography;
|
using Windows.Security.Cryptography;
|
||||||
using Windows.Security.Cryptography.Core;
|
using Windows.Security.Cryptography.Core;
|
||||||
|
using ModernKeePassLib.Cryptography.Hash;
|
||||||
using ModernKeePassLib.Native;
|
using ModernKeePassLib.Native;
|
||||||
using ModernKeePassLib.Utility;
|
using ModernKeePassLib.Utility;
|
||||||
|
using Org.BouncyCastle.Asn1.Pkcs;
|
||||||
|
|
||||||
namespace ModernKeePassLib.Cryptography
|
namespace ModernKeePassLib.Cryptography
|
||||||
{
|
{
|
||||||
@@ -111,20 +113,7 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
while(iPos < cbOut)
|
while(iPos < cbOut)
|
||||||
{
|
{
|
||||||
Debug.Assert(pbHash.Length == 64);
|
Debug.Assert(pbHash.Length == 64);
|
||||||
byte[] pbR = MemUtil.UInt64ToBytes(r);
|
using(HMACSHA256 h = new HMACSHA256(pbHash))
|
||||||
#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))
|
|
||||||
{
|
{
|
||||||
byte[] pbR = MemUtil.UInt64ToBytes(r);
|
byte[] pbR = MemUtil.UInt64ToBytes(r);
|
||||||
byte[] pbPart = h.ComputeHash(pbR);
|
byte[] pbPart = h.ComputeHash(pbR);
|
||||||
@@ -138,7 +127,6 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
|
|
||||||
MemUtil.ZeroByteArray(pbPart);
|
MemUtil.ZeroByteArray(pbPart);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
Debug.Assert(iPos == cbOut);
|
Debug.Assert(iPos == cbOut);
|
||||||
}
|
}
|
||||||
|
35
ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs
Normal file
35
ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -687,8 +687,8 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
private static void HmacEval(byte[] pbKey, byte[] pbMsg,
|
private static void HmacEval(byte[] pbKey, byte[] pbMsg,
|
||||||
byte[] pbExpc, string strID)
|
byte[] pbExpc, string strID)
|
||||||
{
|
{
|
||||||
// WinRT
|
|
||||||
#if ModernKeePassLib
|
#if ModernKeePassLib
|
||||||
|
// WinRT
|
||||||
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbKey));
|
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbKey));
|
||||||
h.Append(CryptographicBuffer.CreateFromByteArray(pbMsg));
|
h.Append(CryptographicBuffer.CreateFromByteArray(pbMsg));
|
||||||
var pbHash = h.GetValueAndReset().ToArray();
|
var pbHash = h.GetValueAndReset().ToArray();
|
||||||
|
@@ -95,11 +95,11 @@ namespace ModernKeePassLib.Keys
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string str = StrUtil.Utf8.GetString(pb, 0, pb.Length);
|
|
||||||
#if ModernKeePassLib
|
#if ModernKeePassLib
|
||||||
// TODO: find a way to implement this
|
// TODO: find a way to implement this
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
|
string str = StrUtil.Utf8.GetString(pb);
|
||||||
return str.IsNormalized(NormalizationForm.FormC);
|
return str.IsNormalized(NormalizationForm.FormC);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@@ -61,6 +61,7 @@
|
|||||||
<Compile Include="Cryptography\Cipher\ICipherEngine.cs" />
|
<Compile Include="Cryptography\Cipher\ICipherEngine.cs" />
|
||||||
<Compile Include="Cryptography\CryptoUtil.cs" />
|
<Compile Include="Cryptography\CryptoUtil.cs" />
|
||||||
<Compile Include="Cryptography\Hash\Blake2b.cs" />
|
<Compile Include="Cryptography\Hash\Blake2b.cs" />
|
||||||
|
<Compile Include="Cryptography\Hash\HMACSHA256.cs" />
|
||||||
<Compile Include="Cryptography\HmacOtp.cs" />
|
<Compile Include="Cryptography\HmacOtp.cs" />
|
||||||
<Compile Include="Cryptography\KeyDerivation\AesKdf.cs" />
|
<Compile Include="Cryptography\KeyDerivation\AesKdf.cs" />
|
||||||
<Compile Include="Cryptography\KeyDerivation\AesKdf.GCrypt.cs" />
|
<Compile Include="Cryptography\KeyDerivation\AesKdf.GCrypt.cs" />
|
||||||
|
@@ -32,13 +32,13 @@ using System.IO.Compression;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ModernKeePassLib
|
#if ModernKeePassLib
|
||||||
//using PCLStorage;
|
|
||||||
using Windows.Storage;
|
using Windows.Storage;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using ModernKeePassLib.Collections;
|
using ModernKeePassLib.Collections;
|
||||||
using ModernKeePassLib.Cryptography;
|
using ModernKeePassLib.Cryptography;
|
||||||
using ModernKeePassLib.Cryptography.Cipher;
|
using ModernKeePassLib.Cryptography.Cipher;
|
||||||
|
using ModernKeePassLib.Cryptography.Hash;
|
||||||
using ModernKeePassLib.Cryptography.KeyDerivation;
|
using ModernKeePassLib.Cryptography.KeyDerivation;
|
||||||
using ModernKeePassLib.Delegates;
|
using ModernKeePassLib.Delegates;
|
||||||
using ModernKeePassLib.Interfaces;
|
using ModernKeePassLib.Interfaces;
|
||||||
@@ -459,15 +459,10 @@ namespace ModernKeePassLib.Serialization
|
|||||||
byte[] pbHeaderHmac;
|
byte[] pbHeaderHmac;
|
||||||
byte[] pbBlockKey = HmacBlockStream.GetHmacKey64(
|
byte[] pbBlockKey = HmacBlockStream.GetHmacKey64(
|
||||||
pbKey, ulong.MaxValue);
|
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))
|
using (HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
||||||
{
|
{
|
||||||
pbHeaderHmac = h.ComputeHash(pbHeader);
|
pbHeaderHmac = h.ComputeHash(pbHeader);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
MemUtil.ZeroByteArray(pbBlockKey);
|
MemUtil.ZeroByteArray(pbBlockKey);
|
||||||
|
|
||||||
return pbHeaderHmac;
|
return pbHeaderHmac;
|
||||||
|
Reference in New Issue
Block a user