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

committed by
BONNEVILLE Geoffroy

parent
84e7afc819
commit
e95e62f184
@@ -52,15 +52,15 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
|
|
||||||
byte[] pbHash;
|
byte[] pbHash;
|
||||||
|
|
||||||
#if ModernKeePassLib
|
/*#if ModernKeePassLib
|
||||||
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash();
|
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash();
|
||||||
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
|
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
|
||||||
#else
|
#else*/
|
||||||
using(SHA256Managed h = new SHA256Managed())
|
using(SHA256Managed h = new SHA256Managed())
|
||||||
{
|
{
|
||||||
pbHash = h.ComputeHash(pbData, iOffset, cbCount);
|
pbHash = h.ComputeHash(pbData, iOffset, cbCount);
|
||||||
}
|
}
|
||||||
#endif
|
//#endif
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// Ensure the data has not been modified
|
// Ensure the data has not been modified
|
||||||
@@ -90,15 +90,15 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
if(cbOut <= 32) pbHash = HashSha256(pbIn, iInOffset, cbIn);
|
if(cbOut <= 32) pbHash = HashSha256(pbIn, iInOffset, cbIn);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#if ModernKeePassLib
|
/*#if ModernKeePassLib
|
||||||
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash();
|
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash();
|
||||||
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
|
CryptographicBuffer.CopyToByteArray(h.GetValueAndReset(), out pbHash);
|
||||||
#else
|
#else*/
|
||||||
using(SHA512Managed h = new SHA512Managed())
|
using(SHA512Managed h = new SHA512Managed())
|
||||||
{
|
{
|
||||||
pbHash = h.ComputeHash(pbIn, iInOffset, cbIn);
|
pbHash = h.ComputeHash(pbIn, iInOffset, cbIn);
|
||||||
}
|
}
|
||||||
#endif
|
//#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if(cbOut == pbHash.Length) return pbHash;
|
if(cbOut == pbHash.Length) return pbHash;
|
||||||
|
26
ModernKeePassLib/Cryptography/Hash/DigestManaged.cs
Normal file
26
ModernKeePassLib/Cryptography/Hash/DigestManaged.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
using Org.BouncyCastle.Crypto;
|
||||||
|
|
||||||
|
namespace ModernKeePassLib.Cryptography.Hash
|
||||||
|
{
|
||||||
|
public abstract class DigestManaged : IDisposable
|
||||||
|
{
|
||||||
|
protected IDigest _hash;
|
||||||
|
|
||||||
|
public byte[] ComputeHash(byte[] value, int offset, int length)
|
||||||
|
{
|
||||||
|
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||||
|
|
||||||
|
byte[] resBuf = new byte[_hash.GetDigestSize()];
|
||||||
|
_hash.BlockUpdate(value, offset, length);
|
||||||
|
_hash.DoFinal(resBuf, 0);
|
||||||
|
|
||||||
|
return resBuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_hash.Reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -6,10 +6,19 @@ using Org.BouncyCastle.Crypto.Parameters;
|
|||||||
namespace ModernKeePassLib.Cryptography.Hash
|
namespace ModernKeePassLib.Cryptography.Hash
|
||||||
{
|
{
|
||||||
public class HMACSHA256: IDisposable
|
public class HMACSHA256: IDisposable
|
||||||
|
|
||||||
{
|
{
|
||||||
private readonly HMac _hmac;
|
private readonly HMac _hmac;
|
||||||
|
|
||||||
|
public byte[] Hash
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
byte[] resBuf = new byte[_hmac.GetMacSize()];
|
||||||
|
_hmac.DoFinal(resBuf, 0);
|
||||||
|
return resBuf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public HMACSHA256(byte[] key)
|
public HMACSHA256(byte[] key)
|
||||||
{
|
{
|
||||||
_hmac = new HMac(new Sha256Digest());
|
_hmac = new HMac(new Sha256Digest());
|
||||||
@@ -18,7 +27,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
|||||||
|
|
||||||
public byte[] ComputeHash(byte[] value)
|
public byte[] ComputeHash(byte[] value)
|
||||||
{
|
{
|
||||||
if (value == null) throw new ArgumentNullException("value");
|
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||||
|
|
||||||
byte[] resBuf = new byte[_hmac.GetMacSize()];
|
byte[] resBuf = new byte[_hmac.GetMacSize()];
|
||||||
_hmac.BlockUpdate(value, 0, value.Length);
|
_hmac.BlockUpdate(value, 0, value.Length);
|
||||||
@@ -31,5 +40,20 @@ namespace ModernKeePassLib.Cryptography.Hash
|
|||||||
{
|
{
|
||||||
_hmac.Reset();
|
_hmac.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset )
|
||||||
|
{
|
||||||
|
_hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
|
||||||
|
{
|
||||||
|
_hmac.DoFinal(inputBuffer, inputOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal void Initialize()
|
||||||
|
{
|
||||||
|
_hmac.Reset();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs
Normal file
13
ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using Org.BouncyCastle.Crypto.Digests;
|
||||||
|
|
||||||
|
namespace ModernKeePassLib.Cryptography.Hash
|
||||||
|
{
|
||||||
|
public class SHA256Managed : DigestManaged
|
||||||
|
{
|
||||||
|
public SHA256Managed()
|
||||||
|
{
|
||||||
|
_hash = new Sha256Digest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs
Normal file
13
ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using Org.BouncyCastle.Crypto.Digests;
|
||||||
|
|
||||||
|
namespace ModernKeePassLib.Cryptography.Hash
|
||||||
|
{
|
||||||
|
public class SHA512Managed : DigestManaged
|
||||||
|
{
|
||||||
|
public SHA512Managed()
|
||||||
|
{
|
||||||
|
_hash = new Sha512Digest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -687,7 +687,7 @@ 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)
|
||||||
{
|
{
|
||||||
#if ModernKeePassLib
|
/*#if ModernKeePassLib
|
||||||
// WinRT
|
// 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));
|
||||||
@@ -701,19 +701,19 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||||
|
|
||||||
// BouncyCastle
|
// BouncyCastle
|
||||||
/*var h = new HMac(new Sha256Digest());
|
// var h = new HMac(new Sha256Digest());
|
||||||
h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
//h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||||
byte[] pbHash = MemUtil.EmptyByteArray;
|
//byte[] pbHash = MemUtil.EmptyByteArray;
|
||||||
h.DoFinal(pbHash, 0);
|
//h.DoFinal(pbHash, 0);
|
||||||
if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
//if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||||
throw new SecurityException("HMAC-SHA-256-" + strID);
|
// throw new SecurityException("HMAC-SHA-256-" + strID);
|
||||||
|
|
||||||
h.Reset();
|
// h.Reset();
|
||||||
h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
//h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||||
h.DoFinal(pbHash, 0);
|
//h.DoFinal(pbHash, 0);
|
||||||
if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
//if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");*/
|
// throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||||
#else
|
#else*/
|
||||||
// Original
|
// Original
|
||||||
using(HMACSHA256 h = new HMACSHA256(pbKey))
|
using(HMACSHA256 h = new HMACSHA256(pbKey))
|
||||||
{
|
{
|
||||||
@@ -733,7 +733,7 @@ namespace ModernKeePassLib.Cryptography
|
|||||||
if(!MemUtil.ArraysEqual(pbHash, pbExpc))
|
if(!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||||
}
|
}
|
||||||
#endif
|
//#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -61,7 +61,10 @@
|
|||||||
<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\DigestManaged.cs" />
|
||||||
<Compile Include="Cryptography\Hash\HMACSHA256.cs" />
|
<Compile Include="Cryptography\Hash\HMACSHA256.cs" />
|
||||||
|
<Compile Include="Cryptography\Hash\SHA256Managed.cs" />
|
||||||
|
<Compile Include="Cryptography\Hash\SHA512Managed.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" />
|
||||||
|
@@ -24,6 +24,7 @@ using System.IO;
|
|||||||
using System.Text;
|
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.Resources;
|
using ModernKeePassLib.Resources;
|
||||||
using ModernKeePassLib.Utility;
|
using ModernKeePassLib.Utility;
|
||||||
using Org.BouncyCastle.Crypto.Digests;
|
using Org.BouncyCastle.Crypto.Digests;
|
||||||
@@ -114,7 +115,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
Flush();
|
Flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
//m_sBase.Close();
|
m_sBase.Dispose();
|
||||||
m_sBase = null;
|
m_sBase = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +148,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
// We are computing the HMAC using SHA-256, whose internal
|
// We are computing the HMAC using SHA-256, whose internal
|
||||||
// block size is 512 bits; thus create a key that is 512
|
// block size is 512 bits; thus create a key that is 512
|
||||||
// bits long (using SHA-512)
|
// bits long (using SHA-512)
|
||||||
|
#if ModernKeePassLib
|
||||||
byte[] pbBlockKey = MemUtil.EmptyByteArray;
|
byte[] pbBlockKey = MemUtil.EmptyByteArray;
|
||||||
byte[] pbIndex = MemUtil.UInt64ToBytes(uBlockIndex);
|
byte[] pbIndex = MemUtil.UInt64ToBytes(uBlockIndex);
|
||||||
var h = new Sha512Digest();
|
var h = new Sha512Digest();
|
||||||
@@ -155,8 +156,8 @@ namespace ModernKeePassLib.Serialization
|
|||||||
h.BlockUpdate(pbKey, 0, pbKey.Length);
|
h.BlockUpdate(pbKey, 0, pbKey.Length);
|
||||||
h.DoFinal(pbBlockKey, 0);
|
h.DoFinal(pbBlockKey, 0);
|
||||||
h.Reset();
|
h.Reset();
|
||||||
|
#else
|
||||||
/*byte[] pbBlockKey;
|
byte[] pbBlockKey;
|
||||||
using(SHA512Managed h = new SHA512Managed())
|
using(SHA512Managed h = new SHA512Managed())
|
||||||
{
|
{
|
||||||
byte[] pbIndex = MemUtil.UInt64ToBytes(uBlockIndex);
|
byte[] pbIndex = MemUtil.UInt64ToBytes(uBlockIndex);
|
||||||
@@ -167,7 +168,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
|
|
||||||
pbBlockKey = h.Hash;
|
pbBlockKey = h.Hash;
|
||||||
}
|
}
|
||||||
*/
|
#endif
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
byte[] pbZero = new byte[64];
|
byte[] pbZero = new byte[64];
|
||||||
@@ -240,10 +241,10 @@ namespace ModernKeePassLib.Serialization
|
|||||||
|
|
||||||
if(m_bVerify)
|
if(m_bVerify)
|
||||||
{
|
{
|
||||||
byte[] pbCmpHmac = MemUtil.EmptyByteArray;
|
byte[] pbCmpHmac;
|
||||||
byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex);
|
byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex);
|
||||||
|
|
||||||
#if ModernKeePassLib
|
/*#if ModernKeePassLib
|
||||||
var h = new HMac(new Sha256Digest());
|
var h = new HMac(new Sha256Digest());
|
||||||
h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length);
|
h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length);
|
||||||
h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length);
|
h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length);
|
||||||
@@ -252,7 +253,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
|
|
||||||
h.DoFinal(pbCmpHmac, 0);
|
h.DoFinal(pbCmpHmac, 0);
|
||||||
h.Reset();
|
h.Reset();
|
||||||
#else
|
#else*/
|
||||||
using(HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
using(HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
||||||
{
|
{
|
||||||
h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length,
|
h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length,
|
||||||
@@ -268,7 +269,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
|
|
||||||
pbCmpHmac = h.Hash;
|
pbCmpHmac = h.Hash;
|
||||||
}
|
}
|
||||||
#endif
|
//#endif
|
||||||
MemUtil.ZeroByteArray(pbBlockKey);
|
MemUtil.ZeroByteArray(pbBlockKey);
|
||||||
|
|
||||||
if(!MemUtil.ArraysEqual(pbCmpHmac, pbStoredHmac))
|
if(!MemUtil.ArraysEqual(pbCmpHmac, pbStoredHmac))
|
||||||
@@ -313,10 +314,10 @@ namespace ModernKeePassLib.Serialization
|
|||||||
int cbBlockSize = m_iBufferPos;
|
int cbBlockSize = m_iBufferPos;
|
||||||
byte[] pbBlockSize = MemUtil.Int32ToBytes(cbBlockSize);
|
byte[] pbBlockSize = MemUtil.Int32ToBytes(cbBlockSize);
|
||||||
|
|
||||||
byte[] pbBlockHmac = MemUtil.EmptyByteArray;
|
byte[] pbBlockHmac;
|
||||||
byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex);
|
byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex);
|
||||||
|
|
||||||
#if ModernKeePassLib
|
/*#if ModernKeePassLib
|
||||||
var h = new HMac(new Sha256Digest());
|
var h = new HMac(new Sha256Digest());
|
||||||
h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length);
|
h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length);
|
||||||
h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length);
|
h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length);
|
||||||
@@ -325,7 +326,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
|
|
||||||
h.DoFinal(pbBlockHmac, 0);
|
h.DoFinal(pbBlockHmac, 0);
|
||||||
h.Reset();
|
h.Reset();
|
||||||
#else
|
#else*/
|
||||||
using(HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
using(HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
||||||
{
|
{
|
||||||
h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length,
|
h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length,
|
||||||
@@ -340,7 +341,7 @@ namespace ModernKeePassLib.Serialization
|
|||||||
|
|
||||||
pbBlockHmac = h.Hash;
|
pbBlockHmac = h.Hash;
|
||||||
}
|
}
|
||||||
#endif
|
//#endif
|
||||||
MemUtil.ZeroByteArray(pbBlockKey);
|
MemUtil.ZeroByteArray(pbBlockKey);
|
||||||
|
|
||||||
MemUtil.Write(m_sBase, pbBlockHmac);
|
MemUtil.Write(m_sBase, pbBlockHmac);
|
||||||
|
Reference in New Issue
Block a user