Code cleanup in KeePassLib

This commit is contained in:
2017-10-24 14:53:22 +02:00
committed by BONNEVILLE Geoffroy
parent ad0d8d6c97
commit 52e08d8c98
6 changed files with 9 additions and 63 deletions

View File

@@ -21,8 +21,7 @@ using System;
using System.Diagnostics;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Cryptography.Hash;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
@@ -102,16 +101,7 @@ namespace ModernKeePassLib.Cryptography
{
byte[] pbKey32 = new byte[32];
byte[] pbIV12 = new byte[12];
#if ModernKeePassLib
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
.HashData(CryptographicBuffer.CreateFromByteArray(pbKey));
byte[] pbHash;
CryptographicBuffer.CopyToByteArray(h, out pbHash);
Array.Copy(pbHash, pbKey32, 32);
Array.Copy(pbHash, 32, pbIV12, 0, 12);
MemUtil.ZeroByteArray(pbHash);
#else
using(SHA512Managed h = new SHA512Managed())
{
byte[] pbHash = h.ComputeHash(pbKey);
@@ -119,7 +109,6 @@ namespace ModernKeePassLib.Cryptography
Array.Copy(pbHash, 32, pbIV12, 0, 12);
MemUtil.ZeroByteArray(pbHash);
}
#endif
m_chacha20 = new ChaCha20Cipher(pbKey32, pbIV12, true);
}

View File

@@ -22,18 +22,14 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using ModernKeePassLib.Cryptography.Hash;
#if ModernKeePassLib
using Org.BouncyCastle.Asn1.Pkcs;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Cryptography.Hash;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
using ModernKeePassLib.Native;
using ModernKeePassLib.Utility;
using Org.BouncyCastle.Crypto.Digests;
namespace ModernKeePassLib.Cryptography
{
@@ -56,21 +52,10 @@ namespace ModernKeePassLib.Cryptography
#endif
byte[] pbHash;
#if ModernKeePassLib
/*var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256)
.HashData(CryptographicBuffer.CreateFromByteArray(pbData));
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())
{
pbHash = h.ComputeHash(pbData, iOffset, cbCount);
}
#endif
#if DEBUG
// Ensure the data has not been modified
@@ -100,20 +85,10 @@ namespace ModernKeePassLib.Cryptography
if(cbOut <= 32) pbHash = HashSha256(pbIn, iInOffset, cbIn);
else
{
#if ModernKeePassLib
/*var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
.HashData(CryptographicBuffer.CreateFromByteArray(pbIn));
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

@@ -16,7 +16,7 @@ namespace ModernKeePassLib.Cryptography.Hash
{
if (value == null) throw new ArgumentNullException(nameof(value));
byte[] resBuf = new byte[Hash.GetByteLength()];
byte[] resBuf = new byte[Hash.GetDigestSize()];
Hash.BlockUpdate(value, 0, length);
Hash.DoFinal(resBuf, 0);

View File

@@ -21,10 +21,8 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Cryptography.Hash;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
@@ -87,19 +85,11 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
Debug.Assert(pbKey.Length >= 64);
if((pbAdditionalEntropy != null) && (pbAdditionalEntropy.Length > 0))
{
#if ModernKeePassLib
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
.HashData(CryptographicBuffer.CreateFromByteArray(pbAdditionalEntropy));
byte[] pbHash;
CryptographicBuffer.CopyToByteArray(h, out pbHash);
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
#else
using(SHA512Managed h = new SHA512Managed())
{
byte[] pbHash = h.ComputeHash(pbAdditionalEntropy);
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
}
#endif
}
return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey);

View File

@@ -2,7 +2,7 @@
<package >
<metadata>
<id>ModernKeePassLib</id>
<version>2.37.3000</version>
<version>2.37.4000</version>
<title>ModernKeePassLib</title>
<authors>Geoffroy Bonneville</authors>
<owners>Geoffroy Bonneville</owners>
@@ -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</description>
<releaseNotes>Hashblockstream write works - now tests results on par with 2.28</releaseNotes>
<releaseNotes>Opening and writing back to working order! There is still some untested stuff, mainly related to KDBX file format 4</releaseNotes>
<copyright>Copyright © 2017 Geoffroy Bonneville</copyright>
<tags>KeePass KeePassLib Portable PCL NetStandard</tags>
<dependencies>

View File

@@ -28,14 +28,12 @@ using System.Xml;
#if ModernKeePassLib
using Windows.Storage;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Cryptography.Hash;
#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;
@@ -391,16 +389,10 @@ namespace ModernKeePassLib.Serialization
pbCipherKey = CryptoUtil.ResizeKey(pbCmp, 0, 64, cbCipherKey);
pbCmp[64] = 1;
#if ModernKeePassLib
var h = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512)
.HashData(CryptographicBuffer.CreateFromByteArray(pbCmp));
CryptographicBuffer.CopyToByteArray(h, out pbHmacKey64);
#else
using(SHA512Managed h = new SHA512Managed())
{
pbHmacKey64 = h.ComputeHash(pbCmp);
}
#endif
}
finally { MemUtil.ZeroByteArray(pbCmp); }
}