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; using System.Diagnostics;
#if ModernKeePassLib #if ModernKeePassLib
using Windows.Security.Cryptography; using ModernKeePassLib.Cryptography.Hash;
using Windows.Security.Cryptography.Core;
#elif !KeePassUAP #elif !KeePassUAP
using System.Security.Cryptography; using System.Security.Cryptography;
#endif #endif
@@ -102,16 +101,7 @@ namespace ModernKeePassLib.Cryptography
{ {
byte[] pbKey32 = new byte[32]; byte[] pbKey32 = new byte[32];
byte[] pbIV12 = new byte[12]; 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()) using(SHA512Managed h = new SHA512Managed())
{ {
byte[] pbHash = h.ComputeHash(pbKey); byte[] pbHash = h.ComputeHash(pbKey);
@@ -119,7 +109,6 @@ namespace ModernKeePassLib.Cryptography
Array.Copy(pbHash, 32, pbIV12, 0, 12); Array.Copy(pbHash, 32, pbIV12, 0, 12);
MemUtil.ZeroByteArray(pbHash); MemUtil.ZeroByteArray(pbHash);
} }
#endif
m_chacha20 = new ChaCha20Cipher(pbKey32, pbIV12, true); m_chacha20 = new ChaCha20Cipher(pbKey32, pbIV12, true);
} }

View File

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

View File

@@ -16,7 +16,7 @@ namespace ModernKeePassLib.Cryptography.Hash
{ {
if (value == null) throw new ArgumentNullException(nameof(value)); 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.BlockUpdate(value, 0, length);
Hash.DoFinal(resBuf, 0); Hash.DoFinal(resBuf, 0);

View File

@@ -21,10 +21,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
#if ModernKeePassLib #if ModernKeePassLib
using Windows.Security.Cryptography; using ModernKeePassLib.Cryptography.Hash;
using Windows.Security.Cryptography.Core;
#elif !KeePassUAP #elif !KeePassUAP
using System.Security.Cryptography; using System.Security.Cryptography;
#endif #endif
@@ -87,19 +85,11 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
Debug.Assert(pbKey.Length >= 64); Debug.Assert(pbKey.Length >= 64);
if((pbAdditionalEntropy != null) && (pbAdditionalEntropy.Length > 0)) 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()) using(SHA512Managed h = new SHA512Managed())
{ {
byte[] pbHash = h.ComputeHash(pbAdditionalEntropy); byte[] pbHash = h.ComputeHash(pbAdditionalEntropy);
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length); MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
} }
#endif
} }
return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey); return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey);

View File

@@ -2,7 +2,7 @@
<package > <package >
<metadata> <metadata>
<id>ModernKeePassLib</id> <id>ModernKeePassLib</id>
<version>2.37.3000</version> <version>2.37.4000</version>
<title>ModernKeePassLib</title> <title>ModernKeePassLib</title>
<authors>Geoffroy Bonneville</authors> <authors>Geoffroy Bonneville</authors>
<owners>Geoffroy Bonneville</owners> <owners>Geoffroy Bonneville</owners>
@@ -10,7 +10,7 @@
<projectUrl>https://github.com/wismna/ModernKeePass</projectUrl> <projectUrl>https://github.com/wismna/ModernKeePass</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance> <requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Portable KeePass Password Management Library that targets .Net Standard and WinRT</description> <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> <copyright>Copyright © 2017 Geoffroy Bonneville</copyright>
<tags>KeePass KeePassLib Portable PCL NetStandard</tags> <tags>KeePass KeePassLib Portable PCL NetStandard</tags>
<dependencies> <dependencies>

View File

@@ -28,14 +28,12 @@ using System.Xml;
#if ModernKeePassLib #if ModernKeePassLib
using Windows.Storage; using Windows.Storage;
using Windows.Security.Cryptography; using ModernKeePassLib.Cryptography.Hash;
using Windows.Security.Cryptography.Core;
#endif #endif
using ModernKeePassLib.Collections; using ModernKeePassLib.Collections;
using ModernKeePassLib.Cryptography; using ModernKeePassLib.Cryptography;
using ModernKeePassLib.Cryptography.Cipher; using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Cryptography.Hash;
using ModernKeePassLib.Cryptography.KeyDerivation; using ModernKeePassLib.Cryptography.KeyDerivation;
using ModernKeePassLib.Delegates; using ModernKeePassLib.Delegates;
using ModernKeePassLib.Interfaces; using ModernKeePassLib.Interfaces;
@@ -391,16 +389,10 @@ namespace ModernKeePassLib.Serialization
pbCipherKey = CryptoUtil.ResizeKey(pbCmp, 0, 64, cbCipherKey); pbCipherKey = CryptoUtil.ResizeKey(pbCmp, 0, 64, cbCipherKey);
pbCmp[64] = 1; 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()) using(SHA512Managed h = new SHA512Managed())
{ {
pbHmacKey64 = h.ComputeHash(pbCmp); pbHmacKey64 = h.ComputeHash(pbCmp);
} }
#endif
} }
finally { MemUtil.ZeroByteArray(pbCmp); } finally { MemUtil.ZeroByteArray(pbCmp); }
} }