mirror of
https://github.com/wismna/ModernKeePassLib.git
synced 2025-10-03 15:40:20 -04:00
Downgrade to net standard 1.2
This commit is contained in:
8
ModernKeePassLib/Cryptography/Cipher/CryptoStreamMode.cs
Normal file
8
ModernKeePassLib/Cryptography/Cipher/CryptoStreamMode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace ModernKeePassLib.Cryptography.Cipher
|
||||
{
|
||||
public enum CryptoStreamMode
|
||||
{
|
||||
Write,
|
||||
Read
|
||||
}
|
||||
}
|
@@ -24,17 +24,17 @@ using System.IO;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Resources;
|
||||
#else
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Resources;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Cipher
|
||||
{
|
||||
public sealed class StandardAesEngine : ICipherEngine
|
||||
{
|
||||
#if !KeePassUAP
|
||||
#if !ModernKeePassLib && !KeePassUAP
|
||||
private const CipherMode SaeCipherMode = CipherMode.CBC;
|
||||
private const PaddingMode SaePaddingMode = PaddingMode.PKCS7;
|
||||
#endif
|
||||
@@ -97,7 +97,7 @@ namespace ModernKeePassLib.Cryptography.Cipher
|
||||
{
|
||||
StandardAesEngine.ValidateArguments(s, bEncrypt, pbKey, pbIV);
|
||||
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
return StandardAesEngineExt.CreateStream(s, bEncrypt, pbKey, pbIV);
|
||||
#else
|
||||
SymmetricAlgorithm a = CryptoUtil.CreateAes();
|
||||
|
26
ModernKeePassLib/Cryptography/Cipher/StandardAesEngineExt.cs
Normal file
26
ModernKeePassLib/Cryptography/Cipher/StandardAesEngineExt.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.IO;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.IO;
|
||||
using Org.BouncyCastle.Crypto.Modes;
|
||||
using Org.BouncyCastle.Crypto.Paddings;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Cipher
|
||||
{
|
||||
public class StandardAesEngineExt
|
||||
{
|
||||
internal static Stream CreateStream(Stream s, bool bEncrypt, byte[] pbKey, byte[] pbIV)
|
||||
{
|
||||
var cbc = new CbcBlockCipher(new AesEngine());
|
||||
//var cbc = new CbcBlockCipher(new RijndaelEngine());
|
||||
var bc = new PaddedBufferedBlockCipher(cbc, new Pkcs7Padding());
|
||||
var kp = new KeyParameter(pbKey);
|
||||
var prmIV = new ParametersWithIV(kp, pbIV);
|
||||
bc.Init(bEncrypt, prmIV);
|
||||
|
||||
var cpRead = bEncrypt ? null : bc;
|
||||
var cpWrite = bEncrypt ? bc : null;
|
||||
return new CipherStream(s, cpRead, cpWrite);
|
||||
}
|
||||
}
|
||||
}
|
@@ -23,12 +23,11 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
|
||||
#if !KeePassUAP
|
||||
using System.Drawing;
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
using Windows.Security.Cryptography;
|
||||
#else
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
#if !ModernKeePassLib
|
||||
using System.Windows.Forms;
|
||||
#endif
|
||||
|
||||
@@ -190,7 +189,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
fI32(Environment.TickCount);
|
||||
fI64(DateTime.UtcNow.ToBinary());
|
||||
|
||||
#if !KeePassLibSD && !ModernKeePassLib
|
||||
#if (!ModernKeePassLib && !KeePassLibSD)
|
||||
// In try-catch for systems without GUI;
|
||||
// https://sourceforge.net/p/keepass/discussion/329221/thread/20335b73/
|
||||
try
|
||||
@@ -207,20 +206,21 @@ namespace ModernKeePassLib.Cryptography
|
||||
fI32((int)NativeLib.GetPlatformID());
|
||||
#if KeePassUAP
|
||||
fStr(EnvironmentExt.OSVersion.VersionString);
|
||||
#else
|
||||
#elif !ModernKeePassLib
|
||||
fStr(Environment.OSVersion.VersionString);
|
||||
#endif
|
||||
|
||||
fI32(Environment.ProcessorCount);
|
||||
|
||||
#if !KeePassUAP
|
||||
#if !ModernKeePassLib && !KeePassUAP
|
||||
fStr(Environment.CommandLine);
|
||||
fI64(Environment.WorkingSet);
|
||||
#endif
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
try
|
||||
#if !ModernKeePassLib
|
||||
try
|
||||
{
|
||||
foreach(DictionaryEntry de in Environment.GetEnvironmentVariables())
|
||||
{
|
||||
@@ -229,12 +229,13 @@ namespace ModernKeePassLib.Cryptography
|
||||
}
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
#if KeePassUAP
|
||||
f(DiagnosticsExt.GetProcessEntropy(), true);
|
||||
#elif !KeePassLibSD
|
||||
f(DiagnosticsExt.GetProcessEntropy(), true);
|
||||
#elif !KeePassLibSD && !ModernKeePassLib
|
||||
using(Process p = Process.GetCurrentProcess())
|
||||
{
|
||||
fI64(p.Handle.ToInt64());
|
||||
@@ -281,6 +282,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
{
|
||||
byte[] pb = new byte[32];
|
||||
|
||||
|
||||
try { m_rng.GetBytes(pb); }
|
||||
catch(Exception)
|
||||
{
|
||||
|
@@ -20,7 +20,9 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
|
@@ -23,7 +23,7 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if !ModernKeePassLib && !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography
|
||||
|
@@ -23,8 +23,10 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
using ModernKeePassLib.Cryptography.Cipher;
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
@@ -105,7 +107,8 @@ namespace ModernKeePassLib.Cryptography
|
||||
return pbHash;
|
||||
}
|
||||
|
||||
internal static byte[] HashSha256(string strFilePath)
|
||||
#if !ModernKeePassLib
|
||||
internal static byte[] HashSha256(string strFilePath)
|
||||
{
|
||||
byte[] pbHash = null;
|
||||
|
||||
@@ -120,6 +123,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
|
||||
return pbHash;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Create a cryptographic key of length <paramref name="cbOut" />
|
||||
@@ -181,7 +185,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
return pbRet;
|
||||
}
|
||||
|
||||
#if !KeePassUAP
|
||||
#if !ModernKeePassLib
|
||||
private static bool? g_obAesCsp = null;
|
||||
public static SymmetricAlgorithm CreateAes()
|
||||
{
|
||||
|
@@ -23,9 +23,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if ModernKeePassLib
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
@@ -33,8 +35,14 @@ using ModernKeePassLib.Utility;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public sealed class Blake2b : HashAlgorithm
|
||||
public sealed class Blake2b : IDisposable
|
||||
{
|
||||
protected int HashSizeValue;
|
||||
protected internal byte[] HashValue;
|
||||
protected int State = 0;
|
||||
|
||||
private bool m_bDisposed = false;
|
||||
|
||||
private const int NbRounds = 12;
|
||||
private const int NbBlockBytes = 128;
|
||||
private const int NbMaxOutBytes = 64;
|
||||
@@ -72,6 +80,23 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
private ulong[] m_m = new ulong[16];
|
||||
private ulong[] m_v = new ulong[16];
|
||||
|
||||
public int HashSize
|
||||
{
|
||||
get { return HashSizeValue; }
|
||||
}
|
||||
|
||||
public byte[] Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_bDisposed)
|
||||
throw new ObjectDisposedException(null);
|
||||
if (State != 0)
|
||||
throw new InvalidOperationException("Blak2B Cryptography Hash Not Yet Finalized");
|
||||
return (byte[])HashValue.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
public Blake2b()
|
||||
{
|
||||
m_cbHashLength = NbMaxOutBytes;
|
||||
@@ -91,7 +116,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
Initialize();
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
public void Initialize()
|
||||
{
|
||||
Debug.Assert(m_h.Length == g_vIV.Length);
|
||||
Array.Copy(g_vIV, m_h, m_h.Length);
|
||||
@@ -166,7 +191,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
if(m_t[0] < cb) ++m_t[1];
|
||||
}
|
||||
|
||||
protected override void HashCore(byte[] array, int ibStart, int cbSize)
|
||||
private void HashCore(byte[] array, int ibStart, int cbSize)
|
||||
{
|
||||
Debug.Assert(m_f[0] == 0);
|
||||
|
||||
@@ -201,7 +226,7 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
}
|
||||
}
|
||||
|
||||
protected override byte[] HashFinal()
|
||||
private byte[] HashFinal()
|
||||
{
|
||||
if(m_f[0] != 0) { Debug.Assert(false); throw new InvalidOperationException(); }
|
||||
Debug.Assert(((m_t[1] == 0) && (m_t[0] == 0)) ||
|
||||
@@ -228,5 +253,142 @@ namespace ModernKeePassLib.Cryptography.Hash
|
||||
MemUtil.ZeroByteArray(pbHash);
|
||||
return pbShort;
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(Stream inputStream)
|
||||
{
|
||||
if (m_bDisposed)
|
||||
throw new ObjectDisposedException(null);
|
||||
|
||||
// Default the buffer size to 4K.
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
do
|
||||
{
|
||||
bytesRead = inputStream.Read(buffer, 0, 4096);
|
||||
if (bytesRead > 0)
|
||||
{
|
||||
HashCore(buffer, 0, bytesRead);
|
||||
}
|
||||
} while (bytesRead > 0);
|
||||
|
||||
HashValue = HashFinal();
|
||||
byte[] Tmp = (byte[])HashValue.Clone();
|
||||
Initialize();
|
||||
return (Tmp);
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] buffer)
|
||||
{
|
||||
if (m_bDisposed)
|
||||
throw new ObjectDisposedException(null);
|
||||
|
||||
// Do some validation
|
||||
if (buffer == null) throw new ArgumentNullException("buffer");
|
||||
|
||||
HashCore(buffer, 0, buffer.Length);
|
||||
HashValue = HashFinal();
|
||||
byte[] Tmp = (byte[])HashValue.Clone();
|
||||
Initialize();
|
||||
return (Tmp);
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] buffer, int offset, int count)
|
||||
{
|
||||
// Do some validation
|
||||
if (buffer == null)
|
||||
throw new ArgumentNullException("buffer");
|
||||
if (offset < 0)
|
||||
throw new ArgumentOutOfRangeException("offset", "ArgumentOutOfRange_NeedNonNegNum");
|
||||
if (count < 0 || (count > buffer.Length))
|
||||
throw new ArgumentException("Argument_InvalidValue");
|
||||
if ((buffer.Length - count) < offset)
|
||||
throw new ArgumentException("Argument_InvalidOffLen");
|
||||
|
||||
if (m_bDisposed)
|
||||
throw new ObjectDisposedException(null);
|
||||
|
||||
HashCore(buffer, offset, count);
|
||||
HashValue = HashFinal();
|
||||
byte[] Tmp = (byte[])HashValue.Clone();
|
||||
Initialize();
|
||||
return (Tmp);
|
||||
}
|
||||
|
||||
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
||||
{
|
||||
// Do some validation, we let BlockCopy do the destination array validation
|
||||
if (inputBuffer == null)
|
||||
throw new ArgumentNullException("inputBuffer");
|
||||
if (inputOffset < 0)
|
||||
throw new ArgumentOutOfRangeException("inputOffset", "ArgumentOutOfRange_NeedNonNegNum");
|
||||
if (inputCount < 0 || (inputCount > inputBuffer.Length))
|
||||
throw new ArgumentException("Argument_InvalidValue");
|
||||
if ((inputBuffer.Length - inputCount) < inputOffset)
|
||||
throw new ArgumentException("Argument_InvalidOffLen");
|
||||
|
||||
if (m_bDisposed)
|
||||
throw new ObjectDisposedException(null);
|
||||
|
||||
// Change the State value
|
||||
State = 1;
|
||||
HashCore(inputBuffer, inputOffset, inputCount);
|
||||
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
|
||||
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
|
||||
return inputCount;
|
||||
}
|
||||
|
||||
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
|
||||
{
|
||||
// Do some validation
|
||||
if (inputBuffer == null)
|
||||
throw new ArgumentNullException("inputBuffer");
|
||||
if (inputOffset < 0)
|
||||
throw new ArgumentOutOfRangeException("inputOffset", "ArgumentOutOfRange_NeedNonNegNum");
|
||||
if (inputCount < 0 || (inputCount > inputBuffer.Length))
|
||||
throw new ArgumentException("Argument_InvalidValue");
|
||||
if ((inputBuffer.Length - inputCount) < inputOffset)
|
||||
throw new ArgumentException("Argument_InvalidOffLen");
|
||||
|
||||
if (m_bDisposed)
|
||||
throw new ObjectDisposedException(null);
|
||||
|
||||
HashCore(inputBuffer, inputOffset, inputCount);
|
||||
HashValue = HashFinal();
|
||||
byte[] outputBytes;
|
||||
if (inputCount != 0)
|
||||
{
|
||||
outputBytes = new byte[inputCount];
|
||||
Array.Copy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
outputBytes = MemUtil.EmptyByteArray;
|
||||
}
|
||||
// reset the State value
|
||||
State = 0;
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
(this as IDisposable).Dispose();
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (HashValue != null)
|
||||
Array.Clear(HashValue, 0, HashValue.Length);
|
||||
HashValue = null;
|
||||
m_bDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
62
ModernKeePassLib/Cryptography/Hash/HMAC.cs
Normal file
62
ModernKeePassLib/Cryptography/Hash/HMAC.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using Org.BouncyCastle.Crypto.Macs;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public class HMAC : IDisposable
|
||||
{
|
||||
protected HMac Hmac;
|
||||
|
||||
public byte[] Hash
|
||||
{
|
||||
get
|
||||
{
|
||||
var result = new byte[Hmac.GetMacSize()];
|
||||
Hmac.DoFinal(result, 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] value)
|
||||
{
|
||||
return ComputeHash(value, 0, value.Length);
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] value, int offset, int length)
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
|
||||
byte[] resBuf = new byte[Hmac.GetMacSize()];
|
||||
Hmac.BlockUpdate(value, offset, length);
|
||||
Hmac.DoFinal(resBuf, offset);
|
||||
|
||||
return resBuf;
|
||||
}
|
||||
|
||||
public void TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
||||
{
|
||||
Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
if ((outputBuffer != null) && ((inputBuffer != outputBuffer) || (inputOffset != outputOffset)))
|
||||
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
|
||||
}
|
||||
|
||||
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
|
||||
{
|
||||
Hmac.BlockUpdate(inputBuffer, inputOffset, inputCount);
|
||||
byte[] outputBytes = new byte[inputCount];
|
||||
if (inputCount != 0)
|
||||
Buffer.BlockCopy(inputBuffer, inputOffset, outputBytes, 0, inputCount);
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Hmac.Reset();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Hmac.Reset();
|
||||
}
|
||||
}
|
||||
}
|
10
ModernKeePassLib/Cryptography/Hash/HMACSHA1.cs
Normal file
10
ModernKeePassLib/Cryptography/Hash/HMACSHA1.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public class HMACSHA1: HashAlgorithm
|
||||
{
|
||||
public HMACSHA1(byte[] key) : base (MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1).CreateHash(key.AsBuffer())) {}
|
||||
}
|
||||
}
|
10
ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs
Normal file
10
ModernKeePassLib/Cryptography/Hash/HMACSHA256.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public class HMACSHA256: HashAlgorithm
|
||||
{
|
||||
public HMACSHA256(byte[] key) : base (MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256).CreateHash(key.AsBuffer())) {}
|
||||
}
|
||||
}
|
105
ModernKeePassLib/Cryptography/Hash/HashAlgorithm.cs
Normal file
105
ModernKeePassLib/Cryptography/Hash/HashAlgorithm.cs
Normal file
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
using Validation;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public abstract class HashAlgorithm: IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// The platform-specific hash object.
|
||||
/// </summary>
|
||||
private readonly CryptographicHash _hash;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HashAlgorithm"/> class.
|
||||
/// </summary>
|
||||
/// <param name="hash">The platform hash.</param>
|
||||
internal HashAlgorithm(CryptographicHash hash)
|
||||
{
|
||||
Requires.NotNull(hash, "Hash");
|
||||
_hash = hash;
|
||||
}
|
||||
|
||||
public bool CanReuseTransform => true;
|
||||
public bool CanTransformMultipleBlocks => true;
|
||||
|
||||
public byte[] Hash => _hash.GetValueAndReset().ToArray();
|
||||
|
||||
public void Append(byte[] data)
|
||||
{
|
||||
_hash.Append(data.AsBuffer());
|
||||
}
|
||||
|
||||
public byte[] GetValueAndReset()
|
||||
{
|
||||
return _hash.GetValueAndReset().ToArray();
|
||||
}
|
||||
|
||||
#region ICryptoTransform methods
|
||||
|
||||
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
|
||||
{
|
||||
byte[] buffer;
|
||||
if (inputCount < inputBuffer.Length)
|
||||
{
|
||||
buffer = new byte[inputCount];
|
||||
Array.Copy(inputBuffer, inputOffset, buffer, 0, inputCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = inputBuffer;
|
||||
}
|
||||
|
||||
Append(buffer);
|
||||
if (outputBuffer != null)
|
||||
{
|
||||
Array.Copy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
|
||||
}
|
||||
|
||||
return inputCount;
|
||||
}
|
||||
|
||||
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
|
||||
{
|
||||
this.TransformBlock(inputBuffer, inputOffset, inputCount, null, 0);
|
||||
if (inputCount == inputBuffer.Length)
|
||||
{
|
||||
return inputBuffer;
|
||||
}
|
||||
var buffer = new byte[inputCount];
|
||||
Array.Copy(inputBuffer, inputOffset, buffer, 0, inputCount);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] value)
|
||||
{
|
||||
return ComputeHash(value, 0, value.Length);
|
||||
}
|
||||
|
||||
public byte[] ComputeHash(byte[] value, int offset, int length)
|
||||
{
|
||||
if (value == null) throw new ArgumentNullException(nameof(value));
|
||||
|
||||
TransformFinalBlock(value, offset, length);
|
||||
var resBuf = GetValueAndReset();
|
||||
|
||||
return resBuf;
|
||||
}
|
||||
public void Initialize()
|
||||
{
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_hash.GetValueAndReset();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
9
ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs
Normal file
9
ModernKeePassLib/Cryptography/Hash/SHA256Managed.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public class SHA256Managed : HashAlgorithm
|
||||
{
|
||||
public SHA256Managed() : base(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256).CreateHash()) {}
|
||||
}
|
||||
}
|
10
ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs
Normal file
10
ModernKeePassLib/Cryptography/Hash/SHA512Managed.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using Windows.Security.Cryptography.Core;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography.Hash
|
||||
{
|
||||
public class SHA512Managed: HashAlgorithm
|
||||
{
|
||||
public SHA512Managed() : base(HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha512).CreateHash()) {}
|
||||
}
|
||||
}
|
@@ -22,8 +22,9 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
|
@@ -21,8 +21,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
|
@@ -22,7 +22,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
@@ -122,6 +122,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
try
|
||||
{
|
||||
#if !ModernKeePassLib
|
||||
// Try to use the native library first
|
||||
if(NativeLib.TransformKey256(pbNewKey, pbKeySeed32, uNumRounds))
|
||||
return CryptoUtil.HashSha256(pbNewKey);
|
||||
#endif
|
||||
@@ -140,18 +141,16 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
public static bool TransformKeyManaged(byte[] pbNewKey32, byte[] pbKeySeed32,
|
||||
ulong uNumRounds)
|
||||
{
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
KeyParameter kp = new KeyParameter(pbKeySeed32);
|
||||
AesEngine aes = new AesEngine();
|
||||
aes.Init(true, kp);
|
||||
|
||||
for(ulong u = 0; u < uNumRounds; ++u)
|
||||
for(ulong i = 0; i < uNumRounds; ++i)
|
||||
{
|
||||
aes.ProcessBlock(pbNewKey32, 0, pbNewKey32, 0);
|
||||
aes.ProcessBlock(pbNewKey32, 16, pbNewKey32, 16);
|
||||
}
|
||||
|
||||
aes.Reset();
|
||||
#else
|
||||
byte[] pbIV = new byte[16];
|
||||
|
||||
@@ -213,7 +212,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
pbNewKey[i] = (byte)i;
|
||||
}
|
||||
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
KeyParameter kp = new KeyParameter(pbKey);
|
||||
AesEngine aes = new AesEngine();
|
||||
aes.Init(true, kp);
|
||||
@@ -248,7 +247,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
{
|
||||
for(ulong j = 0; j < BenchStep; ++j)
|
||||
{
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
aes.ProcessBlock(pbNewKey, 0, pbNewKey, 0);
|
||||
aes.ProcessBlock(pbNewKey, 16, pbNewKey, 16);
|
||||
#else
|
||||
@@ -269,7 +268,7 @@ namespace ModernKeePassLib.Cryptography.KeyDerivation
|
||||
}
|
||||
|
||||
p.SetUInt64(ParamRounds, uRounds);
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
aes.Reset();
|
||||
#else
|
||||
}
|
||||
|
@@ -21,11 +21,13 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
#if !KeePassUAP
|
||||
#if ModernKeePassLib
|
||||
using ModernKeePassLib.Cryptography.Hash;
|
||||
#elif !KeePassUAP
|
||||
using System.Security.Cryptography;
|
||||
#endif
|
||||
|
||||
using ModernKeePassLib.Resources;
|
||||
using ModernKeePassLib.Security;
|
||||
using ModernKeePassLib.Utility;
|
||||
|
||||
@@ -163,5 +165,58 @@ namespace ModernKeePassLib.Cryptography.PasswordGenerator
|
||||
psOut = pwd;
|
||||
return PwgError.Success;
|
||||
}
|
||||
|
||||
internal static string ErrorToString(PwgError e, bool bHeader)
|
||||
{
|
||||
if(e == PwgError.Success) { Debug.Assert(false); return string.Empty; }
|
||||
if((e == PwgError.Unknown) && bHeader) return KLRes.PwGenFailed;
|
||||
|
||||
string str = KLRes.UnknownError;
|
||||
switch(e)
|
||||
{
|
||||
// case PwgError.Success:
|
||||
// break;
|
||||
|
||||
case PwgError.Unknown:
|
||||
break;
|
||||
|
||||
case PwgError.TooFewCharacters:
|
||||
str = KLRes.CharSetTooFewChars;
|
||||
break;
|
||||
|
||||
case PwgError.UnknownAlgorithm:
|
||||
str = KLRes.AlgorithmUnknown;
|
||||
break;
|
||||
|
||||
case PwgError.InvalidCharSet:
|
||||
str = KLRes.CharSetInvalid;
|
||||
break;
|
||||
|
||||
case PwgError.InvalidPattern:
|
||||
str = KLRes.PatternInvalid;
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.Assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
if(bHeader)
|
||||
str = KLRes.PwGenFailed + MessageService.NewParagraph + str;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
internal static string ErrorToString(Exception ex, bool bHeader)
|
||||
{
|
||||
string str = KLRes.UnknownError;
|
||||
if((ex != null) && !string.IsNullOrEmpty(ex.Message))
|
||||
str = ex.Message;
|
||||
|
||||
if(bHeader)
|
||||
str = KLRes.PwGenFailed + MessageService.NewParagraph + str;
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -65,7 +65,14 @@ namespace ModernKeePassLib.Cryptography
|
||||
if(vPassword.Length == 0) { uDictSize = 0; return false; }
|
||||
|
||||
#if DEBUG
|
||||
#if ModernKeePassLib
|
||||
foreach (var ch in vPassword)
|
||||
{
|
||||
Debug.Assert(ch == char.ToLower(ch));
|
||||
}
|
||||
#else
|
||||
Array.ForEach(vPassword, ch => Debug.Assert(ch == char.ToLower(ch)));
|
||||
#endif
|
||||
#endif
|
||||
|
||||
try { return IsPopularPasswordPriv(vPassword, out uDictSize); }
|
||||
|
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using ModernKeePassLib.Native;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography
|
||||
|
17
ModernKeePassLib/Cryptography/RNGCryptoServiceProvider.cs
Normal file
17
ModernKeePassLib/Cryptography/RNGCryptoServiceProvider.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace ModernKeePassLib.Cryptography
|
||||
{
|
||||
public class RNGCryptoServiceProvider: IDisposable
|
||||
{
|
||||
public void GetBytes(byte[] pb)
|
||||
{
|
||||
var random = new Random();
|
||||
random.NextBytes(pb);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@@ -26,7 +26,7 @@ using System.Runtime.InteropServices;
|
||||
using System.Security;
|
||||
using System.Text;
|
||||
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Engines;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
@@ -105,7 +105,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
|
||||
internal static void TestFipsComplianceProblems()
|
||||
{
|
||||
#if !KeePassUAP
|
||||
#if !ModernKeePassLib
|
||||
try { using(RijndaelManaged r = new RijndaelManaged()) { } }
|
||||
catch(Exception exAes)
|
||||
{
|
||||
@@ -131,7 +131,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
0x75, 0xD1, 0x1B, 0x0E, 0x3A, 0x68, 0xC4, 0x22,
|
||||
0x3D, 0x88, 0xDB, 0xF0, 0x17, 0x97, 0x7D, 0xD7 };
|
||||
|
||||
#if KeePassUAP
|
||||
#if ModernKeePassLib || KeePassUAP
|
||||
AesEngine aes = new AesEngine();
|
||||
aes.Init(true, new KeyParameter(pbKey));
|
||||
if(aes.GetBlockSize() != pbData.Length)
|
||||
@@ -550,7 +550,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
// (test vector for Argon2d 1.3); also on
|
||||
// https://tools.ietf.org/html/draft-irtf-cfrg-argon2-00
|
||||
|
||||
KdfParameters p = kdf.GetDefaultParameters();
|
||||
var p = kdf.GetDefaultParameters();
|
||||
kdf.Randomize(p);
|
||||
|
||||
Debug.Assert(p.GetUInt32(Argon2Kdf.ParamVersion, 0) == 0x13U);
|
||||
@@ -766,7 +766,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
pbMan = CryptoUtil.HashSha256(pbMan);
|
||||
|
||||
AesKdf kdf = new AesKdf();
|
||||
KdfParameters p = kdf.GetDefaultParameters();
|
||||
var p = kdf.GetDefaultParameters();
|
||||
p.SetUInt64(AesKdf.ParamRounds, uRounds);
|
||||
p.SetByteArray(AesKdf.ParamSeed, pbSeed);
|
||||
byte[] pbKdf = kdf.Transform(pbKey, p);
|
||||
@@ -778,7 +778,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
|
||||
private static void TestNativeKeyTransform(Random r)
|
||||
{
|
||||
#if DEBUG
|
||||
#if !ModernKeePassLib && DEBUG
|
||||
byte[] pbOrgKey = CryptoRandom.Instance.GetRandomBytes(32);
|
||||
byte[] pbSeed = CryptoRandom.Instance.GetRandomBytes(32);
|
||||
ulong uRounds = (ulong)r.Next(1, 0x3FFF);
|
||||
@@ -915,7 +915,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
|
||||
private static void TestNativeLib()
|
||||
{
|
||||
#if DEBUG && !ModernKeePassLib
|
||||
#if DEBUG
|
||||
if(NativeLib.IsUnix())
|
||||
{
|
||||
if(NativeLib.EncodeDataToArgs("A\"B C\\D") !=
|
||||
@@ -973,31 +973,31 @@ namespace ModernKeePassLib.Cryptography
|
||||
throw new InvalidOperationException("MemUtil-7");
|
||||
|
||||
byte[] pbRes = MemUtil.ParseBase32("MY======");
|
||||
byte[] pbExp = Encoding.ASCII.GetBytes("f");
|
||||
byte[] pbExp = Encoding.UTF8.GetBytes("f");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-1");
|
||||
|
||||
pbRes = MemUtil.ParseBase32("MZXQ====");
|
||||
pbExp = Encoding.ASCII.GetBytes("fo");
|
||||
pbExp = Encoding.UTF8.GetBytes("fo");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-2");
|
||||
|
||||
pbRes = MemUtil.ParseBase32("MZXW6===");
|
||||
pbExp = Encoding.ASCII.GetBytes("foo");
|
||||
pbExp = Encoding.UTF8.GetBytes("foo");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-3");
|
||||
|
||||
pbRes = MemUtil.ParseBase32("MZXW6YQ=");
|
||||
pbExp = Encoding.ASCII.GetBytes("foob");
|
||||
pbExp = Encoding.UTF8.GetBytes("foob");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-4");
|
||||
|
||||
pbRes = MemUtil.ParseBase32("MZXW6YTB");
|
||||
pbExp = Encoding.ASCII.GetBytes("fooba");
|
||||
pbExp = Encoding.UTF8.GetBytes("fooba");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-5");
|
||||
|
||||
pbRes = MemUtil.ParseBase32("MZXW6YTBOI======");
|
||||
pbExp = Encoding.ASCII.GetBytes("foobar");
|
||||
pbExp = Encoding.UTF8.GetBytes("foobar");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-6");
|
||||
|
||||
pbRes = MemUtil.ParseBase32("JNSXSIDQOJXXM2LEMVZCAYTBONSWIIDPNYQG63TFFV2GS3LFEBYGC43TO5XXEZDTFY======");
|
||||
pbExp = Encoding.ASCII.GetBytes("Key provider based on one-time passwords.");
|
||||
pbExp = Encoding.UTF8.GetBytes("Key provider based on one-time passwords.");
|
||||
if(!MemUtil.ArraysEqual(pbRes, pbExp)) throw new Exception("Base32-7");
|
||||
|
||||
int i = 0 - 0x10203040;
|
||||
@@ -1150,7 +1150,7 @@ namespace ModernKeePassLib.Cryptography
|
||||
private static void TestUrlUtil()
|
||||
{
|
||||
#if DEBUG
|
||||
#if !KeePassUAP
|
||||
#if !ModernKeePassLib && !KeePassUAP
|
||||
Debug.Assert(Uri.UriSchemeHttp.Equals("http", StrUtil.CaseIgnoreCmp));
|
||||
Debug.Assert(Uri.UriSchemeHttps.Equals("https", StrUtil.CaseIgnoreCmp));
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user