mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
Added lots of tests from Lib SelfTest.cs to Test project
Code cleanup in KeePassLib Code cleanup in ModernKeePass.Tests Argon2Kdf files can now be opened WIP - Argon2kdf files are corrupted on write
This commit is contained in:
@@ -69,7 +69,7 @@ namespace ModernKeePassLib.Cryptography.Cipher
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ChaCha20Stream : Stream
|
||||
public sealed class ChaCha20Stream : Stream
|
||||
{
|
||||
private Stream m_sBase;
|
||||
private readonly bool m_bWriting;
|
||||
|
@@ -5,7 +5,17 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public abstract class DigestManaged: IDisposable
|
||||
{
|
||||
protected IDigest Hash;
|
||||
protected IDigest Digest;
|
||||
|
||||
public byte[] Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
var result = new byte[Digest.GetDigestSize()];
|
||||
Digest.DoFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] value)
|
||||
{
|
||||
@@ -16,16 +26,29 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
|
||||
byte[] resBuf = new byte[Hash.GetDigestSize()];
|
||||
Hash.BlockUpdate(value, 0, length);
|
||||
Hash.DoFinal(resBuf, 0);
|
||||
byte[] resBuf = new byte[Digest.GetDigestSize()];
|
||||
Digest.BlockUpdate(value, 0, length);
|
||||
Digest.DoFinal(resBuf, 0);
|
||||
|
||||
return resBuf;
|
||||
}
|
||||
|
||||
|
||||
public void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
||||
{
|
||||
Digest.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
|
||||
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
|
||||
}
|
||||
|
||||
public void TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
|
||||
{
|
||||
Digest.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Hash.Reset();
|
||||
Digest.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,17 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public class HMAC : IDisposable
|
||||
{
|
||||
protected HMac _hmac;
|
||||
protected HMac Hmac;
|
||||
|
||||
public byte[] Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
var result = new byte[Hmac.GetMacSize()];
|
||||
Hmac.DoFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] value)
|
||||
{
|
||||
@@ -16,16 +26,33 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
|
||||
byte[] resBuf = new byte[_hmac.GetMacSize()];
|
||||
_hmac.BlockUpdate(value, 0, length);
|
||||
_hmac.DoFinal(resBuf, 0);
|
||||
byte[] resBuf = new byte[Hmac.GetMacSize()];
|
||||
Hmac.BlockUpdate(value, 0, length);
|
||||
Hmac.DoFinal(resBuf, 0);
|
||||
|
||||
return resBuf;
|
||||
}
|
||||
|
||||
public void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
||||
{
|
||||
Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
|
||||
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
|
||||
}
|
||||
|
||||
public void TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
|
||||
{
|
||||
Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Hmac.Reset();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_hmac.Reset();
|
||||
Hmac.Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,8 +8,8 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public HMACSHA1(byte[] key)
|
||||
{
|
||||
_hmac = new HMac(new Sha1Digest());
|
||||
_hmac.Init(new KeyParameter(key));
|
||||
Hmac = new HMac(new Sha1Digest());
|
||||
Hmac.Init(new KeyParameter(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,23 +8,8 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public HMACSHA256(byte[] key)
|
||||
{
|
||||
_hmac = new HMac(new Sha256Digest());
|
||||
_hmac.Init(new KeyParameter(key));
|
||||
Hmac = new HMac(new Sha256Digest());
|
||||
Hmac.Init(new KeyParameter(key));
|
||||
}
|
||||
|
||||
/*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();
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public SHA256Managed()
|
||||
{
|
||||
Hash = new Sha256Digest();
|
||||
Digest = new Sha256Digest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public SHA512Managed()
|
||||
{
|
||||
Hash = new Sha512Digest();
|
||||
Digest = new Sha512Digest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -134,7 +134,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static bool TransformKeyManaged(byte[] pbNewKey32, byte[] pbKeySeed32,
|
||||
public static bool TransformKeyManaged(byte[] pbNewKey32, byte[] pbKeySeed32,
|
||||
ulong uNumRounds)
|
||||
{
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
|
@@ -34,7 +34,7 @@ using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
@@ -465,7 +465,10 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
ti.Pass = r;
|
||||
ti.Lane = (ulong)l;
|
||||
ti.Slice = s;
|
||||
#if !ModernKeePassLib
|
||||
|
||||
#if ModernKeePassLib
|
||||
Task.Factory.StartNew(FillSegmentThr, ti);
|
||||
#else
|
||||
if(!ThreadPool.QueueUserWorkItem(FillSegmentThr, ti))
|
||||
{
|
||||
Debug.Assert(false);
|
||||
@@ -483,8 +486,8 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void FillSegmentThr(object o)
|
||||
|
||||
private static void FillSegmentThr(object o)
|
||||
{
|
||||
Argon2ThreadInfo ti = (o as Argon2ThreadInfo);
|
||||
if(ti == null) { Debug.Assert(false); return; }
|
||||
|
@@ -417,7 +417,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
|
||||
private static void TestBlake2b(Random r)
|
||||
{
|
||||
#if !ModernKeePassLib && DEBUG
|
||||
#if DEBUG
|
||||
Blake2b h = new Blake2b();
|
||||
|
||||
// ======================================================
|
||||
@@ -676,35 +676,6 @@ namespace ModernKeePassLib.Cryptography
|
||||
private static void HmacEval(byte[] pbKey, byte[] pbMsg,
|
||||
byte[] pbExpc, string strID)
|
||||
{
|
||||
#if ModernKeePassLib
|
||||
/*
|
||||
// WinRT
|
||||
var h = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(CryptographicBuffer.CreateFromByteArray(pbKey));
|
||||
h.Append(CryptographicBuffer.CreateFromByteArray(pbMsg));
|
||||
var pbHash = h.GetValueAndReset().ToArray();
|
||||
if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID);
|
||||
|
||||
h.Append(CryptographicBuffer.CreateFromByteArray(pbMsg));
|
||||
pbHash = h.GetValueAndReset().ToArray();
|
||||
if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||
|
||||
// BouncyCastle
|
||||
// var h = new HMac(new Sha256Digest());
|
||||
//h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||
//byte[] pbHash = MemUtil.EmptyByteArray;
|
||||
//h.DoFinal(pbHash, 0);
|
||||
//if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
// throw new SecurityException("HMAC-SHA-256-" + strID);
|
||||
|
||||
// h.Reset();
|
||||
//h.BlockUpdate(pbMsg, 0, pbMsg.Length);
|
||||
//h.DoFinal(pbHash, 0);
|
||||
//if (!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
// throw new SecurityException("HMAC-SHA-256-" + strID + "-R");*/
|
||||
#else
|
||||
// Original
|
||||
using(HMACSHA256 h = new HMACSHA256(pbKey))
|
||||
{
|
||||
h.TransformBlock(pbMsg, 0, pbMsg.Length, pbMsg, 0);
|
||||
@@ -723,7 +694,6 @@ namespace ModernKeePassLib.Cryptography
|
||||
if(!MemUtil.ArraysEqual(pbHash, pbExpc))
|
||||
throw new SecurityException("HMAC-SHA-256-" + strID + "-R");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@@ -10,7 +10,7 @@
|
||||
<projectUrl>https://github.com/wismna/ModernKeePass</projectUrl>
|
||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||
<description>Portable KeePass Password Management Library that targets .Net Standard and WinRT. Allows reading, editing and writing to KeePass 2.x databases.</description>
|
||||
<releaseNotes>HMAC and Blake2B re-implemented.</releaseNotes>
|
||||
<releaseNotes>KDBX 4 file format supported</releaseNotes>
|
||||
<copyright>Copyright © 2017 Geoffroy Bonneville</copyright>
|
||||
<tags>KeePass KeePassLib Portable PCL NetStandard</tags>
|
||||
<dependencies>
|
||||
|
@@ -22,14 +22,15 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Windows.Security.Cryptography;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Utility;
|
||||
using Org.BouncyCastle.Crypto.Digests;
|
||||
using Org.BouncyCastle.Crypto.Macs;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace ModernKeePassLib.Serialization
|
||||
{
|
||||
@@ -149,15 +150,7 @@ namespace ModernKeePassLib.Serialization
|
||||
// We are computing the HMAC using SHA-256, whose internal
|
||||
// block size is 512 bits; thus create a key that is 512
|
||||
// bits long (using SHA-512)
|
||||
#if ModernKeePassLib
|
||||
byte[] pbBlockKey = MemUtil.EmptyByteArray;
|
||||
byte[] pbIndex = MemUtil.UInt64ToBytes(uBlockIndex);
|
||||
var h = new Sha512Digest();
|
||||
h.BlockUpdate(pbIndex, 0, pbIndex.Length);
|
||||
h.BlockUpdate(pbKey, 0, pbKey.Length);
|
||||
h.DoFinal(pbBlockKey, 0);
|
||||
h.Reset();
|
||||
#else
|
||||
|
||||
byte[] pbBlockKey;
|
||||
using(SHA512Managed h = new SHA512Managed())
|
||||
{
|
||||
@@ -169,7 +162,6 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
pbBlockKey = h.Hash;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DEBUG
|
||||
byte[] pbZero = new byte[64];
|
||||
@@ -244,18 +236,6 @@ namespace ModernKeePassLib.Serialization
|
||||
{
|
||||
byte[] pbCmpHmac;
|
||||
byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex);
|
||||
|
||||
#if ModernKeePassLib
|
||||
var h = new HMac(new Sha256Digest());
|
||||
h.Init(new KeyParameter(pbBlockKey));
|
||||
h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length);
|
||||
h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length);
|
||||
if (m_pbBuffer.Length > 0)
|
||||
h.BlockUpdate(m_pbBuffer, 0, m_pbBuffer.Length);
|
||||
pbCmpHmac = MemUtil.EmptyByteArray;
|
||||
h.DoFinal(pbCmpHmac, 0);
|
||||
h.Reset();
|
||||
#else
|
||||
using(HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
||||
{
|
||||
h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length,
|
||||
@@ -271,7 +251,6 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
pbCmpHmac = h.Hash;
|
||||
}
|
||||
#endif
|
||||
MemUtil.ZeroByteArray(pbBlockKey);
|
||||
|
||||
if(!MemUtil.ArraysEqual(pbCmpHmac, pbStoredHmac))
|
||||
@@ -318,19 +297,6 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
byte[] pbBlockHmac;
|
||||
byte[] pbBlockKey = GetHmacKey64(m_pbKey, m_uBlockIndex);
|
||||
|
||||
#if ModernKeePassLib
|
||||
var h = new HMac(new Sha256Digest());
|
||||
h.Init(new KeyParameter(pbBlockKey));
|
||||
h.BlockUpdate(pbBlockIndex, 0, pbBlockIndex.Length);
|
||||
h.BlockUpdate(pbBlockSize, 0, pbBlockSize.Length);
|
||||
if (cbBlockSize > 0)
|
||||
h.BlockUpdate(m_pbBuffer, 0, cbBlockSize);
|
||||
|
||||
pbBlockHmac = MemUtil.EmptyByteArray;
|
||||
h.DoFinal(pbBlockHmac, 0);
|
||||
h.Reset();
|
||||
#else
|
||||
using(HMACSHA256 h = new HMACSHA256(pbBlockKey))
|
||||
{
|
||||
h.TransformBlock(pbBlockIndex, 0, pbBlockIndex.Length,
|
||||
@@ -345,7 +311,6 @@ namespace ModernKeePassLib.Serialization
|
||||
|
||||
pbBlockHmac = h.Hash;
|
||||
}
|
||||
#endif
|
||||
MemUtil.ZeroByteArray(pbBlockKey);
|
||||
|
||||
MemUtil.Write(m_sBase, pbBlockHmac);
|
||||
|
@@ -37,7 +37,7 @@ namespace ModernKeePassLib.Utility
|
||||
/// </summary>
|
||||
public static class MemUtil
|
||||
{
|
||||
internal static readonly byte[] EmptyByteArray = new byte[0];
|
||||
public static readonly byte[] EmptyByteArray = new byte[0];
|
||||
|
||||
private static readonly uint[] m_vSBox = new uint[256] {
|
||||
0xCD2FACB3, 0xE78A7F5C, 0x6F0803FC, 0xBCF6E230,
|
||||
|
Reference in New Issue
Block a user