WIP KeePassLibPCL - problem with awaitables

This commit is contained in:
2017-09-22 18:48:09 +02:00
parent a43bc20eb3
commit 668afbe817
108 changed files with 1205 additions and 4203 deletions

View File

@@ -24,7 +24,7 @@ using System.IO;
using System.Diagnostics;
using System.Security;
namespace ModernKeePassLib.Cryptography.Cipher
namespace ModernKeePassLibPCL.Cryptography.Cipher
{
/// <summary>
/// Pool of encryption/decryption algorithms (ciphers).

View File

@@ -20,7 +20,7 @@
using System;
using System.IO;
namespace ModernKeePassLib.Cryptography.Cipher
namespace ModernKeePassLibPCL.Cryptography.Cipher
{
/// <summary>
/// Interface of an encryption/decryption class.

View File

@@ -22,9 +22,9 @@
using System;
using System.Diagnostics;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLib.Cryptography.Cipher
namespace ModernKeePassLibPCL.Cryptography.Cipher
{
public sealed class Salsa20Cipher : IDisposable
{

View File

@@ -23,10 +23,9 @@ using System.Text;
using System.IO;
using System.Security;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
#if PCL
using Windows.Security.Cryptography;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
#if !KeePassRT
@@ -42,16 +41,16 @@ using Org.BouncyCastle.Crypto.Parameters;
#endif
using ModernKeePassLib.Resources;
using ModernKeePassLibPCL.Resources;
namespace ModernKeePassLib.Cryptography.Cipher
namespace ModernKeePassLibPCL.Cryptography.Cipher
{
/// <summary>
/// Standard AES cipher implementation.
/// </summary>
public sealed class StandardAesEngine : ICipherEngine
{
#if !PCL && !KeePassRT
#if !ModernKeePassLibPCL && !KeePassRT
private const CipherMode m_rCipherMode = CipherMode.CBC;
private const PaddingMode m_rCipherPadding = PaddingMode.PKCS7;
#endif
@@ -114,28 +113,31 @@ namespace ModernKeePassLib.Cryptography.Cipher
}
}
private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
{
ValidateArguments(s, bEncrypt, pbKey, pbIV);
#if PCL
var provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(CryptographicBuffer.GenerateRandom(32));
using (var ms = new MemoryStream())
{
s.CopyTo(ms);
if (bEncrypt)
{
return CryptographicEngine.Encrypt(key, ms.GetWindowsRuntimeBuffer(), CryptographicBuffer.GenerateRandom(16)).AsStream();
}
/*var encryptor = CryptographicEngine.CreateEncryptor(
private static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
{
StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV);
byte[] pbLocalIV = new byte[16];
Array.Copy(pbIV, pbLocalIV, 16);
byte[] pbLocalKey = new byte[32];
Array.Copy(pbKey, pbLocalKey, 32);
#if ModernKeePassLibPCL
var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.
OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
var key = provider.CreateSymmetricKey(pbLocalKey);
if (bEncrypt)
{
var encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(
key, pbLocalIV);
return new CryptoStream(s, encryptor, CryptoStreamMode.Write);*/
return CryptographicEngine.Decrypt(key, ms.GetWindowsRuntimeBuffer(), CryptographicBuffer.GenerateRandom(16)).AsStream();
/*var decryptor = CryptographicEngine.CreateDecryptor(
key, pbLocalIV);
return new CryptoStream(s, decryptor, CryptoStreamMode.Read);*/
}
return new CryptoStream(s, encryptor, CryptoStreamMode.Write);
} else
{
var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(
key, pbLocalIV);
return new CryptoStream(s, decryptor, CryptoStreamMode.Read);
}
#else
#if !KeePassRT

View File

@@ -19,18 +19,18 @@
using System;
using System.Security;
#if PCL
using Windows.Security.Cryptography;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
using System.Security.Cryptography;
#endif
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Utility;
namespace ModernKeePassLib.Cryptography
using ModernKeePassLibPCL.Native;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLibPCL.Cryptography
{
/// <summary>
/// Cryptographically strong random number generator. The returned values
@@ -41,8 +41,8 @@ namespace ModernKeePassLib.Cryptography
{
private byte[] m_pbEntropyPool = new byte[64];
private uint m_uCounter;
#if PCL
//private IRandomNumberGenerator m_rng = NetFxCrypto.RandomNumberGenerator;
#if ModernKeePassLibPCL
private IRandomNumberGenerator m_rng = NetFxCrypto.RandomNumberGenerator;
#else
private RNGCryptoServiceProvider m_rng = new RNGCryptoServiceProvider();
#endif
@@ -106,9 +106,9 @@ namespace ModernKeePassLib.Cryptography
byte[] pbNewData = pbEntropy;
if(pbEntropy.Length >= 64)
{
#if PCL
var shaNew = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512);
pbNewData = shaNew.HashData(pbEntropy.AsBuffer()).ToArray();
#if ModernKeePassLibPCL
var shaNew = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512);
pbNewData = shaNew.HashData(pbEntropy);
#else
#if !KeePassLibSD
@@ -128,9 +128,9 @@ namespace ModernKeePassLib.Cryptography
ms.Write(pbNewData, 0, pbNewData.Length);
byte[] pbFinal = ms.ToArray();
#if PCL
var shaPool = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512);
m_pbEntropyPool = shaPool.HashData(pbFinal.AsBuffer()).ToArray();
#if ModernKeePassLibPCL
var shaPool = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha512);
m_pbEntropyPool = shaPool.HashData(pbFinal);
#else
#if !KeePassLibSD
@@ -157,7 +157,7 @@ namespace ModernKeePassLib.Cryptography
pb = TimeUtil.PackTime(DateTime.Now);
ms.Write(pb, 0, pb.Length);
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
// In try-catch for systems without GUI;
// https://sourceforge.net/p/keepass/discussion/329221/thread/20335b73/
try
@@ -174,7 +174,7 @@ namespace ModernKeePassLib.Cryptography
pb = MemUtil.UInt32ToBytes((uint)rWeak.Next());
ms.Write(pb, 0, pb.Length);
#if PCL
#if ModernKeePassLibPCL
pb = MemUtil.UInt32ToBytes((uint)Environment.ProcessorCount);
ms.Write(pb, 0, pb.Length);
@@ -185,7 +185,7 @@ namespace ModernKeePassLib.Cryptography
ms.Write(pb, 0, pb.Length);
#endif
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
Process p = null;
try
{
@@ -249,10 +249,9 @@ namespace ModernKeePassLib.Cryptography
private byte[] GetCspData()
{
//byte[] pbCspRandom = new byte[32];
var pbCspRandom = CryptographicBuffer.GenerateRandom(32);
//m_rng.GetBytes(pbCspRandom);
return pbCspRandom.ToArray();
byte[] pbCspRandom = new byte[32];
m_rng.GetBytes(pbCspRandom);
return pbCspRandom;
}
private byte[] GenerateRandom256()
@@ -280,9 +279,9 @@ namespace ModernKeePassLib.Cryptography
m_uGeneratedBytesCount += 32;
}
#if PCL
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
return sha256.HashData(pbFinal.AsBuffer()).ToArray();
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
return sha256.HashData(pbFinal);
#else
SHA256Managed sha256 = new SHA256Managed();
return sha256.ComputeHash(pbFinal);
@@ -310,7 +309,7 @@ namespace ModernKeePassLib.Cryptography
long lCopy = (long)((uRequestedBytes < 32) ? uRequestedBytes : 32);
#if (!PCL && !KeePassLibSD && !KeePassRT)
#if (!ModernKeePassLibPCL && !KeePassLibSD && !KeePassRT)
Array.Copy(pbRandom256, 0, pbRes, lPos, lCopy);
#else
Array.Copy(pbRandom256, 0, pbRes, (int)lPos, (int)lCopy);

View File

@@ -19,17 +19,15 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
#if PCL
using Windows.Security.Cryptography;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
using System.Security.Cryptography;
#endif
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLibPCL.Cryptography.Cipher;
namespace ModernKeePassLib.Cryptography
namespace ModernKeePassLibPCL.Cryptography
{
/// <summary>
/// Algorithms supported by <c>CryptoRandomStream</c>.
@@ -117,9 +115,9 @@ namespace ModernKeePassLib.Cryptography
}
else if(genAlgorithm == CrsAlgorithm.Salsa20)
{
#if PCL
var sha256 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var pbKey32 = sha256.HashData(pbKey.AsBuffer()).ToArray();
#if ModernKeePassLibPCL
var sha256 = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Sha256);
var pbKey32 = sha256.HashData(pbKey);
#else
SHA256Managed sha256 = new SHA256Managed();
byte[] pbKey32 = sha256.ComputeHash(pbKey);

View File

@@ -21,25 +21,23 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
#if PCL
using Windows.Security.Cryptography;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
using System.Security.Cryptography;
#endif
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Core;
using ModernKeePassLib.Utility;
using CryptographicHash = Windows.Security.Cryptography.Core.CryptographicHash;
namespace ModernKeePassLib.Cryptography
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLibPCL.Cryptography
{
public sealed class HashingStreamEx : Stream
{
private Stream m_sBaseStream;
private bool m_bWriting;
#if PCL
private CryptographicHash m_hash;
#if ModernKeePassLibPCL
private ICryptoTransform m_hash;
#else
private HashAlgorithm m_hash;
#endif
@@ -77,8 +75,8 @@ namespace ModernKeePassLib.Cryptography
set { throw new NotSupportedException(); }
}
#if PCL
public HashingStreamEx(Stream sBaseStream, bool bWriting, string hashAlgorithm)
#if ModernKeePassLibPCL
public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm? hashAlgorithm)
#else
public HashingStreamEx(Stream sBaseStream, bool bWriting, HashAlgorithm hashAlgorithm)
#endif
@@ -88,8 +86,8 @@ namespace ModernKeePassLib.Cryptography
m_sBaseStream = sBaseStream;
m_bWriting = bWriting;
#if PCL
m_hash = HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithmNames.Sha256).CreateHash();
#if ModernKeePassLibPCL
m_hash = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(hashAlgorithm ?? HashAlgorithm.Sha256).CreateHash();
#elif !KeePassLibSD
m_hash = (hashAlgorithm ?? new SHA256Managed());
#else // KeePassLibSD
@@ -103,14 +101,14 @@ namespace ModernKeePassLib.Cryptography
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()
@@ -118,7 +116,7 @@ namespace ModernKeePassLib.Cryptography
m_sBaseStream.Flush();
}
#if PCL || KeePassRT
#if ModernKeePassLibPCL || KeePassRT
protected override void Dispose(bool disposing)
{
if(!disposing) return;
@@ -130,9 +128,9 @@ namespace ModernKeePassLib.Cryptography
{
try
{
//m_hash.TransformFinalBlock(new byte[0], 0, 0);
#if PCL
m_pbFinalHash = m_hash.GetValueAndReset().ToArray();
m_hash.TransformFinalBlock(new byte[0], 0, 0);
#if ModernKeePassLibPCL
m_pbFinalHash = (m_hash as CryptographicHash).GetValueAndReset ();
#else
m_pbFinalHash = m_hash.Hash;
#endif
@@ -173,8 +171,8 @@ namespace ModernKeePassLib.Cryptography
Array.Copy(pbBuffer, pbOrg, pbBuffer.Length);
#endif
/*if((m_hash != null) && (nRead > 0))
m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);*/
if((m_hash != null) && (nRead > 0))
m_hash.TransformBlock(pbBuffer, nOffset, nRead, pbBuffer, nOffset);
#if DEBUG
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));
@@ -192,8 +190,8 @@ namespace ModernKeePassLib.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);
#if DEBUG
Debug.Assert(MemUtil.ArraysEqual(pbBuffer, pbOrg));

View File

@@ -18,17 +18,19 @@
*/
using System;
#if PCL
using Windows.Security.Cryptography.Core;
using System.Collections.Generic;
using System.Text;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
using System.Security.Cryptography;
#endif
using System.Globalization;
using System.Runtime.InteropServices.WindowsRuntime;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Utility;
#if (!KeePassLibSD && !KeePassRT)
namespace ModernKeePassLib.Cryptography
namespace ModernKeePassLibPCL.Cryptography
{
/// <summary>
/// Generate HMAC-based one-time passwords as specified in RFC 4226.
@@ -44,10 +46,10 @@ namespace ModernKeePassLib.Cryptography
byte[] pbText = MemUtil.UInt64ToBytes(uFactor);
Array.Reverse(pbText); // Big-Endian
#if PCL
var hsha1 = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1).CreateHash(pbSecret.AsBuffer());
hsha1.Append(pbText.AsBuffer());
var pbHash = hsha1.GetValueAndReset().ToArray();
#if ModernKeePassLibPCL
var hsha1 = WinRTCrypto.MacAlgorithmProvider.OpenAlgorithm(MacAlgorithm.HmacSha1).CreateHash(pbSecret);
hsha1.Append(pbText);
var pbHash = hsha1.GetValueAndReset();
#else
HMACSHA1 hsha1 = new HMACSHA1(pbSecret);
byte[] pbHash = hsha1.ComputeHash(pbText);

View File

@@ -22,10 +22,10 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using ModernKeePassLib.Security;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
internal static class CharSetBasedGenerator
{

View File

@@ -21,10 +21,10 @@ using System;
using System.Collections.Generic;
using System.Text;
using ModernKeePassLib;
using ModernKeePassLib.Security;
using ModernKeePassLibPCL;
using ModernKeePassLibPCL.Security;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
public abstract class CustomPwGenerator
{

View File

@@ -22,7 +22,7 @@ using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
public sealed class CustomPwGeneratorPool : IEnumerable<CustomPwGenerator>
{

View File

@@ -22,10 +22,10 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using ModernKeePassLib.Security;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
internal static class PatternBasedGenerator
{

View File

@@ -22,7 +22,7 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
public sealed class PwCharSet
{

View File

@@ -22,9 +22,9 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using ModernKeePassLib.Security;
using ModernKeePassLibPCL.Security;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
public enum PwgError
{

View File

@@ -24,11 +24,11 @@ using System.Xml.Serialization;
using System.ComponentModel;
using System.Diagnostics;
using ModernKeePassLib.Interfaces;
using ModernKeePassLib.Security;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Interfaces;
using ModernKeePassLibPCL.Security;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLib.Cryptography.PasswordGenerator
namespace ModernKeePassLibPCL.Cryptography.PasswordGenerator
{
/// <summary>
/// Type of the password generator. Different types like generators

View File

@@ -22,9 +22,9 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLib.Cryptography
namespace ModernKeePassLibPCL.Cryptography
{
public static class PopularPasswords
{

View File

@@ -22,10 +22,10 @@ using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using ModernKeePassLib.Cryptography.PasswordGenerator;
using ModernKeePassLib.Utility;
using ModernKeePassLibPCL.Cryptography.PasswordGenerator;
using ModernKeePassLibPCL.Utility;
namespace ModernKeePassLib.Cryptography
namespace ModernKeePassLibPCL.Cryptography
{
/// <summary>
/// A class that offers static functions to estimate the quality of

View File

@@ -20,8 +20,8 @@
using System;
using System.Collections.Generic;
using System.Security;
#if PCL
using Windows.Security.Cryptography;
#if ModernKeePassLibPCL
using PCLCrypto;
#else
using System.Security.Cryptography;
#endif
@@ -29,13 +29,14 @@ using System.Text;
using System.Globalization;
using System.Diagnostics;
using ModernKeePassLib.Cryptography.Cipher;
using ModernKeePassLib.Keys;
using ModernKeePassLib.Utility;
using ModernKeePassLib.Resources;
using ModernKeePassLib.Security;
using ModernKeePassLibPCL.Cryptography.Cipher;
using ModernKeePassLibPCL.Keys;
using ModernKeePassLibPCL.Native;
using ModernKeePassLibPCL.Utility;
using ModernKeePassLibPCL.Resources;
using ModernKeePassLibPCL.Security;
namespace ModernKeePassLib.Cryptography
namespace ModernKeePassLibPCL.Cryptography
{
/* /// <summary>
/// Return values of the <c>SelfTest.Perform</c> method.
@@ -63,7 +64,7 @@ namespace ModernKeePassLib.Cryptography
TestRijndael();
TestSalsa20();
#if !PCL
#if !ModernKeePassLibPCL
TestNativeKeyTransform();
#endif
@@ -81,7 +82,7 @@ namespace ModernKeePassLib.Cryptography
internal static void TestFipsComplianceProblems()
{
#if !PCL && !KeePassRT
#if !ModernKeePassLibPCL && !KeePassRT
try { new RijndaelManaged(); }
catch(Exception exAes)
{
@@ -98,7 +99,7 @@ namespace ModernKeePassLib.Cryptography
private static void TestRijndael()
{
#if !PCL && !KeePassRT
#if !ModernKeePassLibPCL && !KeePassRT
// Test vector (official ECB test vector #356)
byte[] pbIV = new byte[16];
byte[] pbTestKey = new byte[32];
@@ -210,7 +211,7 @@ namespace ModernKeePassLib.Cryptography
}
#endif
#if !PCL
#if !ModernKeePassLibPCL
private static void TestNativeKeyTransform()
{
#if DEBUG
@@ -519,7 +520,7 @@ namespace ModernKeePassLib.Cryptography
if(UrlUtil.GetHost(@"s://u:p@d.tld:p/p?q#f") != "d.tld")
throw new InvalidOperationException("UrlUtil-H7");
//if(NativeLib.IsUnix()) return;
if(NativeLib.IsUnix()) return;
string strBase = "\\\\HOMESERVER\\Apps\\KeePass\\KeePass.exe";
string strDoc = "\\\\HOMESERVER\\Documents\\KeePass\\NewDatabase.kdbx";