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

@@ -23,7 +23,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -37,6 +37,8 @@ using ModernKeePassLibPCL.Native;
using ModernKeePassLibPCL.Resources;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;
namespace ModernKeePassLibPCL.Keys
{
@@ -178,13 +180,17 @@ namespace ModernKeePassLibPCL.Keys
}
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbHash = sha256.HashData(ms.ToArray());
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbHash = sha256.HashData(ms.ToArray());*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(ms.ToArray()));
byte[] pbHash;
CryptographicBuffer.CopyToByteArray(buffer, out pbHash);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] pbHash = sha256.ComputeHash(ms.ToArray());
#endif
ms.Dispose();
ms.Dispose();
return pbHash;
}
@@ -284,13 +290,18 @@ namespace ModernKeePassLibPCL.Keys
return null;
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
return sha256.HashData(pbNewKey);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
return sha256.HashData(pbNewKey);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
byte[] result;
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbNewKey));
CryptographicBuffer.CopyToByteArray(buffer, out result);
return result;
#else
SHA256Managed sha256 = new SHA256Managed();
return sha256.ComputeHash(pbNewKey);
#endif
}
}
public static bool TransformKeyManaged(byte[] pbNewKey32, byte[] pbKeySeed32,
ulong uNumRounds)
@@ -307,9 +318,12 @@ namespace ModernKeePassLibPCL.Keys
}
#else
#if ModernKeePassLibPCL
var aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesEcb);
/*var aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesEcb);
var key = aes.CreateSymmetricKey(pbKeySeed32);
var iCrypt = WinRTCrypto.CryptographicEngine.CreateEncryptor(key);
var iCrypt = WinRTCrypto.CryptographicEngine.CreateEncryptor(key);*/
var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);
var key = aes.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(pbKeySeed32));
#else
byte[] pbIV = new byte[16];
Array.Clear(pbIV, 0, pbIV.Length);
@@ -328,8 +342,8 @@ namespace ModernKeePassLibPCL.Keys
ICryptoTransform iCrypt = r.CreateEncryptor();
#endif
// !iCrypt.CanReuseTransform -- doesn't work with Mono
if((iCrypt == null) || (iCrypt.InputBlockSize != 16) ||
// !iCrypt.CanReuseTransform -- doesn't work with Mono
if ((iCrypt == null) || (iCrypt.InputBlockSize != 16) ||
(iCrypt.OutputBlockSize != 16))
{
Debug.Assert(false, "Invalid ICryptoTransform.");
@@ -384,9 +398,12 @@ namespace ModernKeePassLibPCL.Keys
aes.Init(true, kp);
#else
#if ModernKeePassLibPCL
var aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesEcb);
/*var aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesEcb);
var key = aes.CreateSymmetricKey(pbKey);
var iCrypt = WinRTCrypto.CryptographicEngine.CreateEncryptor(key);
var iCrypt = WinRTCrypto.CryptographicEngine.CreateEncryptor(key);*/
var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);
var key = aes.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(pbKey));
#else
byte[] pbIV = new byte[16];
Array.Clear(pbIV, 0, pbIV.Length);

View File

@@ -22,13 +22,14 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Keys
{
@@ -60,13 +61,17 @@ namespace ModernKeePassLibPCL.Keys
if(bPerformHash)
{
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbRaw = sha256.HashData(pbKeyData);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbRaw = sha256.HashData(pbKeyData);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbKeyData));
byte[] pbRaw;
CryptographicBuffer.CopyToByteArray(buffer, out pbRaw);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] pbRaw = sha256.ComputeHash(pbKeyData);
#endif
m_pbKey = new ProtectedBinary(true, pbRaw);
m_pbKey = new ProtectedBinary(true, pbRaw);
}
else m_pbKey = new ProtectedBinary(true, pbKeyData);
}

View File

@@ -26,7 +26,7 @@ using System.Security;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
@@ -37,6 +37,7 @@ using ModernKeePassLibPCL.Resources;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Serialization;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Keys
{
@@ -139,13 +140,16 @@ namespace ModernKeePassLibPCL.Keys
if(pbKey == null)
{
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
pbKey = sha256.HashData(pbFileData);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
pbKey = sha256.HashData(pbFileData);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbFileData));
CryptographicBuffer.CopyToByteArray(buffer, out pbKey);
#else
SHA256Managed sha256 = new SHA256Managed();
pbKey = sha256.ComputeHash(pbFileData);
#endif
}
}
return pbKey;
}
@@ -203,13 +207,16 @@ namespace ModernKeePassLibPCL.Keys
ms.Write(pbKey32, 0, 32);
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
pbFinalKey32 = sha256.HashData(ms.ToArray());
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
pbFinalKey32 = sha256.HashData(ms.ToArray());*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(ms.ToArray()));
CryptographicBuffer.CopyToByteArray(buffer, out pbFinalKey32);
#else
SHA256Managed sha256 = new SHA256Managed();
pbFinalKey32 = sha256.ComputeHash(ms.ToArray());
#endif
ms.Dispose();
ms.Dispose();
}
CreateXmlKeyFile(strFilePath, pbFinalKey32);

View File

@@ -21,13 +21,14 @@ using System;
using System.Text;
using System.Diagnostics;
#if ModernKeePassLibPCL
using PCLCrypto;
using Windows.Security.Cryptography;
#else
using System.Security.Cryptography;
#endif
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
using Windows.Security.Cryptography.Core;
namespace ModernKeePassLibPCL.Keys
{
@@ -73,14 +74,18 @@ namespace ModernKeePassLibPCL.Keys
if(pbPasswordUtf8 == null) throw new ArgumentNullException("pbPasswordUtf8");
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbRaw = sha256.HashData(pbPasswordUtf8);
/*var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbRaw = sha256.HashData(pbPasswordUtf8);*/
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var buffer = sha256.HashData(CryptographicBuffer.CreateFromByteArray(pbPasswordUtf8));
byte[] pbRaw;
CryptographicBuffer.CopyToByteArray(buffer, out pbRaw);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] pbRaw = sha256.ComputeHash(pbPasswordUtf8);
#endif
m_psPassword = new ProtectedString(true, pbPasswordUtf8);
m_psPassword = new ProtectedString(true, pbPasswordUtf8);
m_pbKeyData = new ProtectedBinary(true, pbRaw);
}