WIP Lib 2.37 - databases created by ModernKeepass work fine, but no interoperability...

This commit is contained in:
2017-10-23 18:48:46 +02:00
committed by BONNEVILLE Geoffroy
parent 2bbd931b1a
commit 5b31d3ff72
30 changed files with 250 additions and 244 deletions

View File

@@ -122,17 +122,16 @@ namespace ModernKeePassLib.Cryptography.Cipher
Array.Copy(pbKey, pbLocalKey, 32);
#if ModernKeePassLib
AesEngine aes = new AesEngine();
CbcBlockCipher cbc = new CbcBlockCipher(aes);
PaddedBufferedBlockCipher bc = new PaddedBufferedBlockCipher(cbc,
var cbc = new CbcBlockCipher(new AesEngine());
var bc = new PaddedBufferedBlockCipher(cbc,
new Pkcs7Padding());
KeyParameter kp = new KeyParameter(pbLocalKey);
ParametersWithIV prmIV = new ParametersWithIV(kp, pbLocalIV);
var kp = new KeyParameter(pbLocalKey);
var prmIV = new ParametersWithIV(kp, pbLocalIV);
bc.Init(bEncrypt, prmIV);
IBufferedCipher cpRead = (bEncrypt ? null : bc);
IBufferedCipher cpWrite = (bEncrypt ? bc : null);
return new CipherStream(s, cpRead, cpWrite);
var cpRead = (bEncrypt ? null : bc);
var cpWrite = (bEncrypt ? bc : null);
return new CipherStream(s, cpRead, cpWrite);
#elif KeePassUAP
return StandardAesEngineExt.CreateStream(s, bEncrypt, pbLocalKey, pbLocalIV);
#else

View File

@@ -19,18 +19,20 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using ModernKeePassLib.Utility;
using Windows.Security.Cryptography.Core;
#else
using System.Security.Cryptography;
#endif
using System.IO;
using System.Diagnostics;
using System.Globalization;
using ModernKeePassLib.Native;
using ModernKeePassLib.Native;
using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Cryptography
{
@@ -44,7 +46,7 @@ namespace ModernKeePassLib.Cryptography
private byte[] m_pbEntropyPool = new byte[64];
private ulong m_uCounter;
#if !ModernKeePassLib
private RNGCryptoServiceProvider m_rng = new RNGCryptoServiceProvider();
private RNGCryptoServiceProvider m_rng = new RNGCryptoServiceProvider();
#endif
private ulong m_uGeneratedBytesCount = 0;

View File

@@ -20,8 +20,13 @@
using System;
using System.Diagnostics;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Utility;

View File

@@ -22,13 +22,17 @@ 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.Asn1.Pkcs;
namespace ModernKeePassLib.Cryptography
{

View File

@@ -21,17 +21,19 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
#else
using Org.BouncyCastle.Crypto.Tls;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
using System.Runtime.InteropServices.ComTypes;
using ModernKeePassLib.Utility;
using Org.BouncyCastle.Crypto.Tls;
namespace ModernKeePassLib.Cryptography
{
@@ -39,7 +41,7 @@ namespace ModernKeePassLib.Cryptography
{
private readonly Stream m_sBaseStream;
private readonly bool m_bWriting;
#if ModernKeePassLib
#if ModernKeePassLib
//private ICryptoTransform m_hash;
//private CryptographicHash m_hash;
private IDigest m_hash;
@@ -81,20 +83,19 @@ namespace ModernKeePassLib.Cryptography
}
#if ModernKeePassLib
//public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm? hashAlgorithm)
public HashingStreamEx(Stream sBaseStream, bool bWriting, string hashAlgorithm)
public HashingStreamEx(Stream sBaseStream, bool bWriting, IDigest hashAlgorithm)
#else
public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm hashAlgorithm)
#endif
{
if (sBaseStream == null) throw new ArgumentNullException("sBaseStream");
{
if(sBaseStream == null) throw new ArgumentNullException("sBaseStream");
m_sBaseStream = sBaseStream;
m_bWriting = bWriting;
#if ModernKeePassLib
//m_hash = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithm.Sha256).CreateHash();
//m_hash = HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithmNames.Sha256).CreateHash();
m_hash = new Sha256Digest();
m_hash = hashAlgorithm ?? new Sha256Digest();
#elif !KeePassLibSD
m_hash = (hashAlgorithm ?? new SHA256Managed());
#else // KeePassLibSD
@@ -108,46 +109,48 @@ namespace ModernKeePassLib.Cryptography
if(m_hash == null) { Debug.Assert(false); return; }
// Validate hash algorithm
/*if(!m_hash.CanReuseTransform || !m_hash.CanTransformMultipleBlocks)
#if ModernKeePassLib
#else
if(!m_hash.CanReuseTransform || !m_hash.CanTransformMultipleBlocks)
{
Debug.Assert(false);
m_hash = null;
}*/
}
#endif
}
#if ModernKeePassLib || KeePassRT
protected override void Dispose(bool disposing)
{
if (!disposing) return;
#else
public override void Close()
protected override void Dispose(bool disposing)
{
#endif
if (m_hash != null)
{
try
{
//m_hash.TransformFinalBlock(new byte[0], 0, 0);
if(disposing)
{
if(m_hash != null)
{
try
{
#if ModernKeePassLib
//m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset ();
//CryptographicBuffer.CopyToByteArray(m_hash.GetValueAndReset(), out m_pbFinalHash);
m_pbFinalHash = new byte[32];
m_hash.DoFinal(m_pbFinalHash, 0);
m_hash.Reset();
//m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset ();
//CryptographicBuffer.CopyToByteArray(m_hash.GetValueAndReset(), out m_pbFinalHash);
m_pbFinalHash = new byte[32];
m_hash.DoFinal(m_pbFinalHash, 0);
m_hash.Reset();
#else
m_pbFinalHash = m_hash.Hash;
m_hash.TransformFinalBlock(MemUtil.EmptyByteArray, 0, 0);
m_pbFinalHash = m_hash.Hash;
#endif
}
catch (Exception)
{
Debug.Assert(false);
}
}
catch(Exception) { Debug.Assert(false); }
base.Dispose(disposing);
}
}
m_hash = null;
}
public override void Flush()
m_sBaseStream.Dispose();
}
base.Dispose(disposing);
}
public override void Flush()
{
m_sBaseStream.Flush();
}
@@ -181,8 +184,11 @@ namespace ModernKeePassLib.Cryptography
#endif
if((m_hash != null) && (nRead > 0))
//m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);
#if ModernKeePassLib
m_hash.BlockUpdate(pbBuffer, nOffset, nRead);
#else
m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);
#endif
#if DEBUG
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));
@@ -201,8 +207,11 @@ namespace ModernKeePassLib.Cryptography
#endif
if((m_hash != null) && (nCount > 0))
//m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset);
#if ModernKeePassLib
m_hash.BlockUpdate(pbBuffer, nOffset, nCount);
#else
m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset);
#endif
#if DEBUG
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));

View File

@@ -19,18 +19,18 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
#if ModernKeePassLib
using Windows.Security.Cryptography;
#else
using Windows.Security.Cryptography.Core;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
using System.Globalization;
using ModernKeePassLib.Utility;
using Windows.Security.Cryptography.Core;
#if (!KeePassLibSD && !KeePassRT)
#if !KeePassLibSD
namespace ModernKeePassLib.Cryptography
{
/// <summary>
@@ -47,11 +47,15 @@ namespace ModernKeePassLib.Cryptography
byte[] pbText = MemUtil.UInt64ToBytes(uFactor);
Array.Reverse(pbText); // To big-endian
#if ModernKeePassLib
var hsha1 = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1).CreateHash(CryptographicBuffer.CreateFromByteArray(pbSecret));
hsha1.Append(CryptographicBuffer.CreateFromByteArray(pbText));
byte[] pbHash;
CryptographicBuffer.CopyToByteArray(hsha1.GetValueAndReset(), out pbHash);
#else
HMACSHA1 hsha1 = new HMACSHA1(pbSecret);
byte[] pbHash = hsha1.ComputeHash(pbText);
#endif
uint uOffset = (uint)(pbHash[pbHash.Length - 1] & 0xF);
if((iTruncationOffset >= 0) && (iTruncationOffset < (pbHash.Length - 4)))
uOffset = (uint)iTruncationOffset;

View File

@@ -114,7 +114,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
ctx.LaneLength = ctx.SegmentLength * NbSyncPoints;
Debug.Assert(NbBlockSize == (NbBlockSizeInQW *
#if KeePassUAP
#if ModernKeePassLib || KeePassUAP
(ulong)Marshal.SizeOf<ulong>()
#else
(ulong)Marshal.SizeOf(typeof(ulong))

View File

@@ -21,9 +21,14 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Diagnostics;
#if ModernKeePassLib
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
#elif !KeePassUAP
using System.Security.Cryptography;
#endif
using ModernKeePassLib.Security;
using ModernKeePassLib.Utility;
@@ -88,7 +93,6 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
byte[] pbHash;
CryptographicBuffer.CopyToByteArray(h, out pbHash);
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
#else
using(SHA512Managed h = new SHA512Managed())
{

View File

@@ -265,7 +265,7 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
else pcs.Add(ch);
}
Array.Clear(vChars, 0, vChars.Length);
MemUtil.ZeroArray<char>(vChars);
MemUtil.ZeroByteArray(pbUtf8);
return pp;
}