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
|
||||
|
||||
|
Reference in New Issue
Block a user