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

@@ -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
{
@@ -54,15 +56,15 @@ namespace ModernKeePassLib.Cryptography
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash();
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
#else
using(SHA256Managed h = new SHA256Managed())
using(SHA256Managed h = new SHA256Managed())
{
pbHash = h.ComputeHash(pbData, iOffset, cbCount);
}
#endif
#if DEBUG
// Ensure the data has not been modified
Debug.Assert(MemUtil.ArraysEqual(pbData, pbCopy));
// Ensure the data has not been modified
Debug.Assert(MemUtil.ArraysEqual(pbData, pbCopy));
Debug.Assert((pbHash != null) && (pbHash.Length == 32));
byte[] pbZero = new byte[32];
@@ -92,12 +94,12 @@ namespace ModernKeePassLib.Cryptography
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash();
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
#else
using(SHA512Managed h = new SHA512Managed())
using(SHA512Managed h = new SHA512Managed())
{
pbHash = h.ComputeHash(pbIn, iInOffset, cbIn);
}
#endif
}
}
if(cbOut == pbHash.Length) return pbHash;
@@ -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);
}
@@ -152,7 +140,7 @@ namespace ModernKeePassLib.Cryptography
}
#if !ModernKeePassLib
private static bool? g_obAesCsp = null;
private static bool? g_obAesCsp = null;
internal static SymmetricAlgorithm CreateAes()
{
if(g_obAesCsp.HasValue)
@@ -187,5 +175,5 @@ namespace ModernKeePassLib.Cryptography
return null;
}
#endif
}
}
}

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,12 +95,12 @@ 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
return str.IsNormalized(NormalizationForm.FormC);
string str = StrUtil.Utf8.GetString(pb);
return str.IsNormalized(NormalizationForm.FormC);
#endif
}
catch(Exception) { Debug.Assert(false); }
@@ -108,5 +108,5 @@ namespace ModernKeePassLib.Keys
return false;
}
#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;