Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #32

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/wan24-Crypto-BC/AsymmetricBcEcDiffieHellmanAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ protected override ECKeyGenerationParameters CreateKeyGenParameters(SecureRandom
=> new(parameters, random);

/// <inheritdoc/>
protected override ECDomainParameters GetEngineParameters(CryptoOptions options) => BcEllipticCurves.GetCurve(options.AsymmetricKeyBits);
protected override ECDomainParameters GetEngineParameters(CryptoOptions options)
{
EnsureAllowedCurve(options.AsymmetricKeyBits);
return BcEllipticCurves.GetCurve(options.AsymmetricKeyBits);
}
}
}
7 changes: 4 additions & 3 deletions src/wan24-Crypto-BC/AsymmetricBcEcDiffieHellmanPrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
Algorithm.EnsureAllowed();
EnsureAllowedCurve();
publicKey ??= options?.PublicKey ?? options?.PrivateKey?.PublicKey ?? PublicKey;
if (publicKey is not AsymmetricBcEcDiffieHellmanPublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
return (DeriveKey(publicKey), PublicKey.KeyData.Array.CloneArray());
Expand All @@ -67,7 +68,7 @@ public override byte[] DeriveKey(byte[] keyExchangeData)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
EnsurePqcRequirement();
using AsymmetricBcEcDiffieHellmanPublicKey publicKey = new(keyExchangeData);
return DeriveKey(publicKey as IAsymmetricPublicKey);
}
Expand All @@ -83,7 +84,7 @@ public override byte[] DeriveKey(IAsymmetricPublicKey publicKey)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
EnsurePqcRequirement();
if (publicKey is not AsymmetricBcEcDiffieHellmanPublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
ECDHBasicAgreement agreement = new();
agreement.Init(PrivateKey);
Expand Down
6 changes: 5 additions & 1 deletion src/wan24-Crypto-BC/AsymmetricBcEcDsaAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ protected override ECKeyGenerationParameters CreateKeyGenParameters(SecureRandom
=> new(parameters, random);

/// <inheritdoc/>
protected override ECDomainParameters GetEngineParameters(CryptoOptions options) => BcEllipticCurves.GetCurve(options.AsymmetricKeyBits);
protected override ECDomainParameters GetEngineParameters(CryptoOptions options)
{
EnsureAllowedCurve(options.AsymmetricKeyBits);
return BcEllipticCurves.GetCurve(options.AsymmetricKeyBits);
}
}
}
3 changes: 2 additions & 1 deletion src/wan24-Crypto-BC/AsymmetricBcEcDsaPrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public override byte[] SignHashRaw(byte[] hash)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
Algorithm.EnsureAllowed();
EnsureAllowedCurve();
DsaDigestSigner signer = new(new ECDsaSigner(), new NullDigest());
signer.Init(forSigning: true, PrivateKey);
signer.BlockUpdate(hash);
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricEd25519Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override AsymmetricEd25519PrivateKey CreateKeyPair(CryptoOptions? options
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
Ed25519KeyPairGenerator keyGen = new();
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricEd448Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public override AsymmetricEd448PrivateKey CreateKeyPair(CryptoOptions? options =
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
Ed448KeyPairGenerator keyGen = new();
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricSNtruPrimeAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public override AsymmetricSNtruPrimePrivateKey CreateKeyPair(CryptoOptions? opti
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
SNtruPrimeKeyPairGenerator keyGen = new();
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricX25519Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override AsymmetricX25519PrivateKey CreateKeyPair(CryptoOptions? options
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
X25519KeyPairGenerator keyGen = new();
Expand Down
6 changes: 3 additions & 3 deletions src/wan24-Crypto-BC/AsymmetricX25519PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
Algorithm.EnsureAllowed();
publicKey ??= options?.PublicKey ?? options?.PrivateKey?.PublicKey ?? PublicKey;
if (publicKey is not AsymmetricX25519PublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
return (DeriveKey(publicKey), PublicKey.KeyData.Array.CloneArray());
Expand All @@ -66,7 +66,7 @@ public override byte[] DeriveKey(byte[] keyExchangeData)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
EnsurePqcRequirement();
using AsymmetricX25519PublicKey publicKey = new(keyExchangeData);
return DeriveKey(publicKey as IAsymmetricPublicKey);
}
Expand All @@ -82,7 +82,7 @@ public override byte[] DeriveKey(IAsymmetricPublicKey publicKey)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
EnsurePqcRequirement();
if (publicKey is not AsymmetricX25519PublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
X25519Agreement agreement = new();
agreement.Init(PrivateKey);
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricX448Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public override AsymmetricX448PrivateKey CreateKeyPair(CryptoOptions? options =
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
X448KeyPairGenerator keyGen = new();
Expand Down
6 changes: 3 additions & 3 deletions src/wan24-Crypto-BC/AsymmetricX448PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
Algorithm.EnsureAllowed();
publicKey ??= options?.PublicKey ?? options?.PrivateKey?.PublicKey ?? PublicKey;
if (publicKey is not AsymmetricX448PublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
return (DeriveKey(publicKey), PublicKey.KeyData.Array.CloneArray());
Expand All @@ -87,7 +87,7 @@ public override byte[] DeriveKey(byte[] keyExchangeData)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
EnsurePqcRequirement();
using AsymmetricX448PublicKey publicKey = new(keyExchangeData);
return DeriveKey(publicKey as IAsymmetricPublicKey);
}
Expand All @@ -103,7 +103,7 @@ public override byte[] DeriveKey(IAsymmetricPublicKey publicKey)
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
EnsurePqcRequirement();
if (publicKey is not AsymmetricX448PublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
X448Agreement agreement = new();
agreement.Init(PrivateKey);
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricXEd25519Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override AsymmetricXEd25519PrivateKey CreateKeyPair(CryptoOptions? option
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
Ed25519KeyPairGenerator keyGen = new();
Expand Down
2 changes: 1 addition & 1 deletion src/wan24-Crypto-BC/AsymmetricXEd25519PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
Algorithm.EnsureAllowed();
publicKey ??= options?.PublicKey ?? options?.PrivateKey?.PublicKey ?? PublicKey;
if (publicKey is not AsymmetricXEd25519PublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
return GetX25519Key().GetKeyExchangeData(key._PublicKey2 ?? throw new InvalidOperationException(), options);
Expand Down
2 changes: 2 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricXEd448Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System.Collections.Frozen;
using System.Security;
using wan24.Core;

namespace wan24.Crypto.BC
Expand Down Expand Up @@ -75,6 +76,7 @@ public override AsymmetricXEd448PrivateKey CreateKeyPair(CryptoOptions? options
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
Ed448KeyPairGenerator keyGen = new();
Expand Down
3 changes: 2 additions & 1 deletion src/wan24-Crypto-BC/AsymmetricXEd448PrivateKey.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Signers;
using System.Security;
using wan24.Core;

namespace wan24.Crypto.BC
Expand Down Expand Up @@ -95,7 +96,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
try
{
EnsureUndisposed();
if (CryptoHelper.StrictPostQuantumSafety) throw new InvalidOperationException($"Post quantum safety-forced - {Algorithm.Name} isn't post quantum");
Algorithm.EnsureAllowed();
publicKey ??= options?.PublicKey ?? options?.PrivateKey?.PublicKey ?? PublicKey;
if (publicKey is not AsymmetricXEd448PublicKey key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
return GetX448Key().GetKeyExchangeData(key._PublicKey2 ?? throw new InvalidOperationException(), options);
Expand Down
17 changes: 15 additions & 2 deletions src/wan24-Crypto-BC/BcEllipticCurves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class BcEllipticCurves
/// </summary>
/// <param name="curve">Curve name</param>
/// <returns>Key size in bits</returns>
public static int GetKeySize(ECDomainParameters curve)
public static int GetKeySize(in ECDomainParameters curve)
{
if (curve.Equals(SECP256R1_CURVE)) return EllipticCurves.SECP256R1_KEY_SIZE;
if (curve.Equals(SECP384R1_CURVE)) return EllipticCurves.SECP384R1_KEY_SIZE;
Expand All @@ -39,12 +39,25 @@ public static int GetKeySize(ECDomainParameters curve)
/// </summary>
/// <param name="bits">Key size in bits</param>
/// <returns>Curve name</returns>
public static ECDomainParameters GetCurve(int bits) => bits switch
public static ECDomainParameters GetCurve(in int bits) => bits switch
{
EllipticCurves.SECP256R1_KEY_SIZE => SECP256R1_CURVE,
EllipticCurves.SECP384R1_KEY_SIZE => SECP384R1_CURVE,
EllipticCurves.SECP521R1_KEY_SIZE => SECP521R1_CURVE,
_ => throw new ArgumentException("Unknown key size", nameof(bits))
};

/// <summary>
/// Determine if an elliptic curve is allowed
/// </summary>
/// <param name="curve">Curve</param>
/// <returns>If the elliptic curve is allowed</returns>
public static bool IsCurveAllowed(in ECDomainParameters curve) => EllipticCurves.IsCurveAllowed(GetKeySize(curve));

/// <summary>
/// Deny an elliptic curve
/// </summary>
/// <param name="curve">Curve</param>
public static void DenyCurve(in ECDomainParameters curve) => EllipticCurves.DenyCurve(GetKeySize(curve));
}
}
6 changes: 5 additions & 1 deletion src/wan24-Crypto-BC/BouncyCastle.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace wan24.Crypto.BC

//TODO Add v2 SEIPD encryption algorithms as an alternate to AEAD
//TODO Add Argon2 S2K KDF algorithm

namespace wan24.Crypto.BC
{
/// <summary>
/// Bouncy Castle helper
Expand Down
2 changes: 2 additions & 0 deletions src/wan24-Crypto-BC/BouncyCastleAeadCipherAlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ protected sealed override ICryptoTransform GetEncryptor(Stream cipherData, Crypt
{
try
{
EnsureAllowed();
IBufferedCipher cipher = CreateCipher(forEncryption: true, options);
byte[] iv = CreateIvBytes();
cipher.Init(forEncryption: true, CreateParameters(iv, options));
Expand All @@ -52,6 +53,7 @@ protected sealed override async Task<ICryptoTransform> GetEncryptorAsync(Stream
{
try
{
EnsureAllowed();
IBufferedCipher cipher = CreateCipher(forEncryption: true, options);
byte[] iv = CreateIvBytes();
cipher.Init(forEncryption: true, CreateParameters(iv, options));
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/BouncyCastleAsymmetricAlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public override tPrivate CreateKeyPair(CryptoOptions? options = null)
{
try
{
EnsureAllowed();
options ??= DefaultOptions;
if (!options.AsymmetricKeyBits.In(AllowedKeySizes)) throw new ArgumentException("Invalid key size", nameof(options));
tKeyGen keyGen = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public override byte[] SignHashRaw(byte[] hash)
try
{
EnsureUndisposed();
Algorithm.EnsureAllowed();
EnsureAllowedCurve();
tSigner signer = new();
signer.Init(forSigning: true, PrivateKey);
signer.BlockUpdate(hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public override byte[] SignHashRaw(byte[] hash)
try
{
EnsureUndisposed();
Algorithm.EnsureAllowed();
EnsureAllowedCurve();
tSigner signer = Activator.CreateInstance(typeof(tSigner), Array.Empty<byte>()) as tSigner
?? throw CryptographicException.From(new InvalidProgramException($"Failed to instance {typeof(tSigner)}"));
signer.Init(forSigning: true, PrivateKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
try
{
EnsureUndisposed();
Algorithm.EnsureAllowed();
publicKey ??= options?.PublicKey ?? options?.PrivateKey?.PublicKey ?? PublicKey;
if (publicKey is not tPublic key) throw new ArgumentException($"Public {Algorithm.Name} key required", nameof(publicKey));
tGenerator generator = Activator.CreateInstance(typeof(tGenerator), new SecureRandom(BouncyCastleRandomGenerator.Instance())) as tGenerator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public sealed override byte[] SignHashRaw(byte[] hash)
try
{
EnsureUndisposed();
Algorithm.EnsureAllowed();
tSigner signer = new();
signer.Init(forSigning: true, PrivateKey);
return signer.GenerateSignature(hash);
Expand Down
2 changes: 2 additions & 0 deletions src/wan24-Crypto-BC/BouncyCastleBlockCipherAlgorithmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected sealed override ICryptoTransform GetEncryptor(Stream cipherData, Crypt
{
try
{
EnsureAllowed();
IBlockCipher cipher = CreateCipher(forEncryption: true, options);
byte[] iv = CreateIvBytes();
cipher.Init(forEncryption: true, CreateParameters(iv, options));
Expand All @@ -66,6 +67,7 @@ protected sealed override async Task<ICryptoTransform> GetEncryptorAsync(Stream
{
try
{
EnsureAllowed();
IBlockCipher cipher = CreateCipher(forEncryption: true, options);
byte[] iv = CreateIvBytes();
cipher.Init(forEncryption: true, CreateParameters(iv, options));
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/StreamCipherRng.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public StreamCipherRng(
Algorithm = algorithm;
try
{
algorithm.EnsureAllowed();
if (algorithm.BlockSize != 1) throw new ArgumentException("Stream cipher required", nameof(algorithm));
if (bufferSize.HasValue && bufferSize.Value < Algorithm.IvSize)
throw new ArgumentOutOfRangeException(nameof(bufferSize), $"Min. buffer size for {algorithm.DisplayName} is {algorithm.IvSize} byte");
Expand Down
6 changes: 3 additions & 3 deletions src/wan24-Crypto-BC/wan24-Crypto-BC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageId>wan24-Crypto-BC</PackageId>
<Title>wan24-Crypto-BC</Title>
<Version>3.3.0</Version>
<Version>3.4.0</Version>
<Authors>nd1012</Authors>
<Company>Andreas Zimmermann, wan24.de</Company>
<Product>wan24-Crypto-BC</Product>
Expand All @@ -33,8 +33,8 @@

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
<PackageReference Include="wan24-Core" Version="2.9.2" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Crypto" Version="2.6.0" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Core" Version="2.10.0" Condition="'$(Configuration)' != 'Trunk'" />
<PackageReference Include="wan24-Crypto" Version="2.7.0" Condition="'$(Configuration)' != 'Trunk'" />
<ProjectReference Include="..\..\..\wan24-Core\src\Wan24-Core\Wan24-Core.csproj" Condition="'$(Configuration)' == 'Trunk'" />
<ProjectReference Include="..\..\..\wan24-Crypto\src\wan24-Crypto\wan24-Crypto.csproj" Condition="'$(Configuration)' == 'Trunk'" />
</ItemGroup>
Expand Down
Loading