KeePassLib 2.37 tentatively working!!

Replaced WinRt hash providers with BouncyCastle in CryptoUtil
This commit is contained in:
2017-10-24 14:39:06 +02:00
committed by BONNEVILLE Geoffroy
parent 5b31d3ff72
commit ad0d8d6c97
9 changed files with 161 additions and 86 deletions

View File

@@ -33,6 +33,7 @@ using System.Security.Cryptography;
using ModernKeePassLib.Native;
using ModernKeePassLib.Utility;
using Org.BouncyCastle.Crypto.Digests;
namespace ModernKeePassLib.Cryptography
{
@@ -57,9 +58,13 @@ namespace ModernKeePassLib.Cryptography
byte[] pbHash;
#if ModernKeePassLib
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256)
/*var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256)
.HashData(CryptographicBuffer.CreateFromByteArray(pbData));
CryptographicBuffer.CopyToByteArray(h, out pbHash);
CryptographicBuffer.CopyToByteArray(h, out pbHash);*/
pbHash = new byte[32];
var h = new Sha256Digest();
h.BlockUpdate(pbData, iOffset, cbCount);
h.DoFinal(pbHash, iOffset);
#else
using(SHA256Managed h = new SHA256Managed())
{
@@ -96,16 +101,20 @@ namespace ModernKeePassLib.Cryptography
else
{
#if ModernKeePassLib
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
/*var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
.HashData(CryptographicBuffer.CreateFromByteArray(pbIn));
CryptographicBuffer.CopyToByteArray(h, out pbHash);
CryptographicBuffer.CopyToByteArray(h, out pbHash);*/
pbHash = new byte[64];
var h = new Sha512Digest();
h.BlockUpdate(pbIn, iInOffset, cbIn);
h.DoFinal(pbHash, iInOffset);
#else
using(SHA512Managed h = new SHA512Managed())
{
pbHash = h.ComputeHash(pbIn, iInOffset, cbIn);
}
#endif
}
}
if(cbOut == pbHash.Length) return pbHash;

View File

@@ -0,0 +1,31 @@
using System;
using Org.BouncyCastle.Crypto;
namespace ModernKeePassLib.Cryptography.Hash
{
public abstract class DigestManaged: IDisposable
{
protected IDigest Hash;
public byte[] ComputeHash(byte[] value)
{
return ComputeHash(value, 0, value.Length);
}
public byte[] ComputeHash(byte[] value, int offset, int length)
{
if (value == null) throw new ArgumentNullException(nameof(value));
byte[] resBuf = new byte[Hash.GetByteLength()];
Hash.BlockUpdate(value, 0, length);
Hash.DoFinal(resBuf, 0);
return resBuf;
}
public void Dispose()
{
Hash.Reset();
}
}
}

View File

@@ -0,0 +1,12 @@
using Org.BouncyCastle.Crypto.Digests;
namespace ModernKeePassLib.Cryptography.Hash
{
public class SHA256Managed : DigestManaged
{
public SHA256Managed()
{
Hash = new Sha256Digest();
}
}
}

View File

@@ -0,0 +1,12 @@
using Org.BouncyCastle.Crypto.Digests;
namespace ModernKeePassLib.Cryptography.Hash
{
public class SHA512Managed: DigestManaged
{
public SHA512Managed()
{
Hash = new Sha512Digest();
}
}
}

View File

@@ -185,7 +185,7 @@ namespace ModernKeePassLib.Cryptography
if((m_hash != null) && (nRead > 0))
#if ModernKeePassLib
m_hash.BlockUpdate(pbBuffer, nOffset, nRead);
m_hash.BlockUpdate(pbBuffer, nOffset, nRead);
#else
m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);
#endif
@@ -208,7 +208,7 @@ namespace ModernKeePassLib.Cryptography
if((m_hash != null) && (nCount > 0))
#if ModernKeePassLib
m_hash.BlockUpdate(pbBuffer, nOffset, nCount);
m_hash.BlockUpdate(pbBuffer, nOffset, nCount);
#else
m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset);
#endif

View File

@@ -19,31 +19,28 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Security;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using System.Text;
#if ModernKeePassLib || KeePassUAP
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
#else
using System.Security.Cryptography;
#endif
using System.Text;
using System.Globalization;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Native;
using ModernKeePassLib.Utility;
using ModernKeePassLib.Resources;
using ModernKeePassLib.Security;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Macs;
using Org.BouncyCastle.Crypto.Parameters;
using KdfParameters = Org.BouncyCastle.Crypto.Parameters.KdfParameters;
using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Cryptography
{
@@ -70,9 +67,8 @@ namespace ModernKeePassLib.Cryptography
TestHmac();
TestKeyTransform(r);
#if !ModernKeePassLib
TestNativeKeyTransform(r);
#endif
TestHmacOtp();
TestProtectedObjects(r);
@@ -99,14 +95,7 @@ namespace ModernKeePassLib.Cryptography
}
#endif
#if ModernKeePassLib
try
{
HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
}
#else
try { using(SHA256Managed h = new SHA256Managed()) { } }
#endif
catch(Exception exSha256)
{
throw new SecurityException("SHA-256: " + exSha256.Message);
@@ -771,10 +760,9 @@ namespace ModernKeePassLib.Cryptography
#endif
}
#if !ModernKeePassLib
private static void TestNativeKeyTransform()
private static void TestNativeKeyTransform(Random r)
{
#if DEBUG
#if !ModernKeePassLib && DEBUG
byte[] pbOrgKey = CryptoRandom.Instance.GetRandomBytes(32);
byte[] pbSeed = CryptoRandom.Instance.GetRandomBytes(32);
ulong uRounds = (ulong)r.Next(1, 0x3FFF);
@@ -793,7 +781,6 @@ namespace ModernKeePassLib.Cryptography
throw new SecurityException("AES-KDF-2");
#endif
}
#endif
private static void TestMemUtil(Random r)
{

View File

@@ -61,7 +61,10 @@
<Compile Include="Cryptography\Cipher\ICipherEngine.cs" />
<Compile Include="Cryptography\CryptoUtil.cs" />
<Compile Include="Cryptography\Hash\Blake2b.cs" />
<Compile Include="Cryptography\Hash\DigestManaged.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\KeyDerivation\AesKdf.cs" />
<Compile Include="Cryptography\KeyDerivation\AesKdf.GCrypt.cs" />

View File

@@ -22,10 +22,12 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Security;
using System.Drawing;
using System.Xml;
#if !ModernKeePassLib && !KeePassUAP
using System.Drawing;
#endif
using ModernKeePassLib;
using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography;
@@ -107,15 +109,15 @@ namespace ModernKeePassLib.Serialization
xrs.IgnoreProcessingInstructions = true;
xrs.IgnoreWhitespace = true;
#if !ModernKeePassLib
// these are default values, so no need to set them
#if !KeePassRT
#if ModernKeePassLib || KeePassUAP
xrs.DtdProcessing = DtdProcessing.Prohibit;
#else
#if !KeePassLibSD
xrs.ProhibitDtd = true;
// Also see PrepMonoDev.sh script
xrs.ProhibitDtd = true; // Obsolete in .NET 4, but still there
// xrs.DtdProcessing = DtdProcessing.Prohibit; // .NET 4 only
#endif
xrs.ValidationType = ValidationType.None;
#endif
#endif
return xrs;