WIP KeePassLibPCL

This commit is contained in:
bg45
2017-09-23 09:42:48 -04:00
parent 668afbe817
commit 9d78d59a15
108 changed files with 3283 additions and 181 deletions

View File

@@ -25,7 +25,8 @@ using System.Security;
using System.Diagnostics;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
#else
#if !KeePassRT
@@ -124,7 +125,7 @@ namespace ModernKeePassLibPCL.Cryptography.Cipher
Array.Copy(pbKey, pbLocalKey, 32);
#if ModernKeePassLibPCL
var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.
/*var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.
OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(pbLocalKey);
if (bEncrypt)
@@ -138,6 +139,27 @@ namespace ModernKeePassLibPCL.Cryptography.Cipher
key, pbLocalIV);
return new CryptoStream(s, decryptor, CryptoStreamMode.Read);
}
*/
var provider = SymmetricKeyAlgorithmProvider.
OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(pbLocalKey));
using (var ms = new MemoryStream())
{
s.CopyTo(ms);
var data = CryptographicBuffer.CreateFromByteArray(ms.ToArray());
byte[] resultByteArray;
if (bEncrypt)
{
var encrypted = CryptographicEngine.Encrypt(key, data, CryptographicBuffer.CreateFromByteArray(pbLocalIV));
CryptographicBuffer.CopyToByteArray(encrypted, out resultByteArray);
}
else
{
var decrypted = CryptographicEngine.Decrypt(key, data, CryptographicBuffer.CreateFromByteArray(pbLocalIV));
CryptographicBuffer.CopyToByteArray(decrypted, out resultByteArray);
}
return new MemoryStream(resultByteArray, true);
}
#else
#if !KeePassRT
@@ -175,7 +197,7 @@ namespace ModernKeePassLibPCL.Cryptography.Cipher
#endif
#endif
}
}
public Stream EncryptStream(Stream sPlainText, byte[] pbKey, byte[] pbIV)
{

View File

@@ -20,7 +20,7 @@
using System;
using System.Security;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -29,6 +29,7 @@ using System.Diagnostics;
using ModernKeePassLibPCL.Native;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Cryptography
{
@@ -42,7 +43,7 @@ namespace ModernKeePassLibPCL.Cryptography
private byte[] m_pbEntropyPool = new byte[64];
private uint m_uCounter;
#if ModernKeePassLibPCL
private IRandomNumberGenerator m_rng = NetFxCrypto.RandomNumberGenerator;
//private IRandomNumberGenerator m_rng = NetFxCrypto.RandomNumberGenerator;
#else
private RNGCryptoServiceProvider m_rng = new RNGCryptoServiceProvider();
#endif
@@ -107,8 +108,11 @@ namespace ModernKeePassLibPCL.Cryptography
if(pbEntropy.Length >= 64)
{
#if ModernKeePassLibPCL
var shaNew = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512);
pbNewData = shaNew.HashData(pbEntropy);
/*var shaNew = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512);
pbNewData = shaNew.HashData(pbEntropy);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbEntropy));
CryptographicBuffer.CopyToByteArray(buffer, out pbNewData);
#else
#if !KeePassLibSD
@@ -119,7 +123,7 @@ namespace ModernKeePassLibPCL.Cryptography
pbNewData = shaNew.ComputeHash(pbEntropy);
#endif
}
}
MemoryStream ms = new MemoryStream();
lock(m_oSyncRoot)
@@ -129,8 +133,11 @@ namespace ModernKeePassLibPCL.Cryptography
byte[] pbFinal = ms.ToArray();
#if ModernKeePassLibPCL
var shaPool = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512);
m_pbEntropyPool = shaPool.HashData(pbFinal);
/*var shaPool = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512);
m_pbEntropyPool = shaPool.HashData(pbFinal);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbFinal));
CryptographicBuffer.CopyToByteArray(buffer, out m_pbEntropyPool);
#else
#if !KeePassLibSD
@@ -142,7 +149,7 @@ namespace ModernKeePassLibPCL.Cryptography
m_pbEntropyPool = shaPool.ComputeHash(pbFinal);
#endif
}
}
ms.Dispose();
}
@@ -250,7 +257,8 @@ namespace ModernKeePassLibPCL.Cryptography
private byte[] GetCspData()
{
byte[] pbCspRandom = new byte[32];
m_rng.GetBytes(pbCspRandom);
//m_rng.GetBytes(pbCspRandom);
CryptographicBuffer.CopyToByteArray(CryptographicBuffer.GenerateRandom(32), out pbCspRandom);
return pbCspRandom;
}
@@ -280,13 +288,18 @@ namespace ModernKeePassLibPCL.Cryptography
}
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
return sha256.HashData(pbFinal);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
return sha256.HashData(pbFinal);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbFinal));
byte[] result;
CryptographicBuffer.CopyToByteArray(buffer, out result);
return result;
#else
SHA256Managed sha256 = new SHA256Managed();
return sha256.ComputeHash(pbFinal);
#endif
}
}
/// <summary>
/// Get a number of cryptographically strong random bytes.

View File

@@ -19,8 +19,9 @@
using System;
using System.Diagnostics;
using Windows.Security.Cryptography.Core;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -116,13 +117,17 @@ namespace ModernKeePassLibPCL.Cryptography
else if(genAlgorithm == CrsAlgorithm.Salsa20)
{
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbKey32 = sha256.HashData(pbKey);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbKey32 = sha256.HashData(pbKey);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbKey));
byte[] pbKey32;
CryptographicBuffer.CopyToByteArray(buffer, out pbKey32);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] pbKey32 = sha256.ComputeHash(pbKey);
#endif
byte[] pbIV = new byte[8] { 0xE8, 0x30, 0x09, 0x4B,
byte[] pbIV = new byte[8] { 0xE8, 0x30, 0x09, 0x4B,
0x97, 0x20, 0x5D, 0x2A }; // Unique constant
m_salsa20 = new Salsa20Cipher(pbKey32, pbIV);

View File

@@ -22,13 +22,14 @@ using System.Collections.Generic;
using System.Text;
using System.IO;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
using System.Diagnostics;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Cryptography
{
@@ -37,7 +38,8 @@ namespace ModernKeePassLibPCL.Cryptography
private Stream m_sBaseStream;
private bool m_bWriting;
#if ModernKeePassLibPCL
private ICryptoTransform m_hash;
//private ICryptoTransform m_hash;
private CryptographicHash m_hash;
#else
private HashAlgorithm m_hash;
#endif
@@ -76,18 +78,20 @@ namespace ModernKeePassLibPCL.Cryptography
}
#if ModernKeePassLibPCL
public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm? hashAlgorithm)
//public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm? hashAlgorithm)
public HashingStreamEx(Stream sBaseStream, bool bWriting, string hashAlgorithm)
#else
public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm hashAlgorithm)
#endif
{
{
if(sBaseStream == null)
throw new ArgumentNullException("sBaseStream");
m_sBaseStream = sBaseStream;
m_bWriting = bWriting;
#if ModernKeePassLibPCL
m_hash = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithm.Sha256).CreateHash();
//m_hash = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithm.Sha256).CreateHash();
m_hash = HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithmNames.Sha256).CreateHash();
#elif !KeePassLibSD
m_hash = (hashAlgorithm ?? new SHA256Managed());
#else // KeePassLibSD
@@ -98,17 +102,17 @@ namespace ModernKeePassLibPCL.Cryptography
try { if(m_hash == null) m_hash = HashAlgorithm.Create(); }
catch(Exception) { }
#endif
if(m_hash == null) { Debug.Assert(false); return; }
if (m_hash == null) { Debug.Assert(false); return; }
// Validate hash algorithm
if((!m_hash.CanReuseTransform) || (!m_hash.CanTransformMultipleBlocks) ||
/*if((!m_hash.CanReuseTransform) || (!m_hash.CanTransformMultipleBlocks) ||
(m_hash.InputBlockSize != 1) || (m_hash.OutputBlockSize != 1))
{
#if false && DEBUG
MessageService.ShowWarning("Broken HashAlgorithm object in HashingStreamEx.");
#endif
m_hash = null;
}
}*/
}
public override void Flush()
@@ -128,13 +132,14 @@ namespace ModernKeePassLibPCL.Cryptography
{
try
{
m_hash.TransformFinalBlock(new byte[0], 0, 0);
//m_hash.TransformFinalBlock(new byte[0], 0, 0);
#if ModernKeePassLibPCL
m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset ();
//m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset ();
CryptographicBuffer.CopyToByteArray(m_hash.GetValueAndReset(), out m_pbFinalHash);
#else
m_pbFinalHash = m_hash.Hash;
#endif
}
}
catch(Exception) { Debug.Assert(false); }
m_hash = null;
@@ -172,10 +177,11 @@ namespace ModernKeePassLibPCL.Cryptography
#endif
if((m_hash != null) && (nRead > 0))
m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);
//m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);
m_hash.Append(CryptographicBuffer.CreateFromByteArray(pbBuffer));
#if DEBUG
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));
#endif
return nRead;
@@ -190,8 +196,9 @@ namespace ModernKeePassLibPCL.Cryptography
Array.Copy(pbBuffer, pbOrg, pbBuffer.Length);
#endif
if((m_hash != null) && (nCount > 0))
m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset);
if ((m_hash != null) && (nCount > 0))
//m_hash.TransformBlock(pbBuffer, nOffset, nCount, pbBuffer, nOffset);
m_hash.Append(CryptographicBuffer.CreateFromByteArray(pbBuffer));
#if DEBUG
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));

View File

@@ -21,13 +21,14 @@ using System;
using System.Collections.Generic;
using System.Text;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
using System.Globalization;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
#if (!KeePassLibSD && !KeePassRT)
namespace ModernKeePassLibPCL.Cryptography
@@ -47,15 +48,19 @@ namespace ModernKeePassLibPCL.Cryptography
Array.Reverse(pbText); // Big-Endian
#if ModernKeePassLibPCL
var hsha1 = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1).CreateHash(pbSecret);
/*var hsha1 = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1).CreateHash(pbSecret);
hsha1.Append(pbText);
var pbHash = hsha1.GetValueAndReset();
var pbHash = hsha1.GetValueAndReset();*/
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);
uint uOffset = (uint)(pbHash[pbHash.Length - 1] & 0xF);
if((iTruncationOffset >= 0) && (iTruncationOffset < (pbHash.Length - 4)))
uOffset = (uint)iTruncationOffset;

View File

@@ -21,7 +21,7 @@ using System;
using System.Collections.Generic;
using System.Security;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif