Skip to content

Commit

Permalink
Dev (#43)
Browse files Browse the repository at this point in the history
+ Added `Asymmetric*Algorithm.MAX_KEY_USAGE_COUNT`
+ Added `Encryption*Algorithm.MAX_CIPHER_DATA_LENGTH`
+ Added `Encryption*Algorithm.MAX_KEY_USAGE_COUNT`
  • Loading branch information
nd1012 authored Sep 21, 2024
1 parent e40a70a commit 61c7087
Show file tree
Hide file tree
Showing 35 changed files with 253 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/wan24-Crypto-BC Tests/wan24-Crypto-BC Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="wan24-Crypto-Shared-Tests" Version="2.5.0" />
<PackageReference Include="wan24-Tests" Version="1.0.1" />
<PackageReference Include="wan24-Tests" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/wan24-Crypto-BC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {27F0CF2F-65DE-4E39-9D87-5632FA3F15CF}
EndGlobalSection
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\wan24-Core\src\Others\wan24-Core-Shared\src\wan24-Core-Shared\wan24-Core-Shared.projitems*{678b09fc-7c24-4801-af72-1800e4c7cd38}*SharedItemsImports = 5
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricBcEcDiffieHellmanAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public sealed record class AsymmetricBcEcDiffieHellmanAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 521;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -69,6 +73,9 @@ private AsymmetricBcEcDiffieHellmanAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override ECKeyGenerationParameters CreateKeyGenParameters(SecureRandom random, ECDomainParameters parameters, CryptoOptions options)
=> new(parameters, random);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
EnsureUndisposed();
Algorithm.EnsureAllowed();
EnsureAllowedCurve();
options?.KeySuite?.CountAsymmetricKeyUsage(this);
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 Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricBcEcDsaAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public sealed record class AsymmetricBcEcDsaAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 521;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -69,6 +73,9 @@ private AsymmetricBcEcDsaAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override ECKeyGenerationParameters CreateKeyGenParameters(SecureRandom random, ECDomainParameters parameters, CryptoOptions options)
=> new(parameters, random);
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricBikeAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricBikeAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 256;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricBikeAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override BikeParameters GetEngineParameters(CryptoOptions options) => AsymmetricBikeHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricDilithiumAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricDilithiumAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 1024;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricDilithiumAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override DilithiumParameters GetEngineParameters(CryptoOptions options) => AsymmetricDilithiumHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricEd25519Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public sealed record class AsymmetricEd25519Algorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 256;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -69,6 +73,9 @@ private AsymmetricEd25519Algorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
public override AsymmetricEd25519PrivateKey CreateKeyPair(CryptoOptions? options = null)
{
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricEd448Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public sealed record class AsymmetricEd448Algorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 456;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -70,6 +74,9 @@ private AsymmetricEd448Algorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
public override AsymmetricEd448PrivateKey CreateKeyPair(CryptoOptions? options = null)
{
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricFalconAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricFalconAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 1024;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -66,6 +70,9 @@ private AsymmetricFalconAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override FalconParameters GetEngineParameters(CryptoOptions options) => AsymmetricFalconHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricFrodoKemAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricFrodoKemAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 256;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricFrodoKemAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override FrodoParameters GetEngineParameters(CryptoOptions options) => AsymmetricFrodoKemHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricHqcAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricHqcAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 256;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricHqcAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override HqcParameters GetEngineParameters(CryptoOptions options) => AsymmetricHqcHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricKyberAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricKyberAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 1024;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricKyberAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override KyberParameters GetEngineParameters(CryptoOptions options) => AsymmetricKyberHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricNtruEncryptAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricNtruEncryptAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 701;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -68,6 +72,9 @@ private AsymmetricNtruEncryptAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => false;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override NtruParameters GetEngineParameters(CryptoOptions options) => AsymmetricNtruHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricPicnicAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricPicnicAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 128;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricPicnicAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override PicnicParameters GetEngineParameters(CryptoOptions options) => AsymmetricPicnicHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricSNtruPrimeAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public sealed record class AsymmetricSNtruPrimeAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 1277;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -71,6 +75,9 @@ private AsymmetricSNtruPrimeAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => false;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
public override AsymmetricSNtruPrimePrivateKey CreateKeyPair(CryptoOptions? options = null)
{
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricSphincsPlusAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public sealed record class AsymmetricSphincsPlusAlgorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 256;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.Signature;
Expand Down Expand Up @@ -67,6 +71,9 @@ private AsymmetricSphincsPlusAlgorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => false;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
protected override SphincsPlusParameters GetEngineParameters(CryptoOptions options) => AsymmetricSphincsPlusHelper.GetParameters(options.AsymmetricKeyBits);
}
Expand Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricX25519Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public sealed record class AsymmetricX25519Algorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 256;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -69,6 +73,9 @@ private AsymmetricX25519Algorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
public override AsymmetricX25519PrivateKey CreateKeyPair(CryptoOptions? options = null)
{
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricX25519PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
{
EnsureUndisposed();
Algorithm.EnsureAllowed();
options?.KeySuite?.CountAsymmetricKeyUsage(this);
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 Down
7 changes: 7 additions & 0 deletions src/wan24-Crypto-BC/AsymmetricX448Algorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public sealed record class AsymmetricX448Algorithm
/// </summary>
public const int DEFAULT_KEY_SIZE = 456;
/// <summary>
/// Maximum key usage count
/// </summary>
public const long MAX_KEY_USAGE_COUNT = long.MaxValue;
/// <summary>
/// Algorithm usages
/// </summary>
public const AsymmetricAlgorithmUsages USAGES = AsymmetricAlgorithmUsages.KeyExchange;
Expand Down Expand Up @@ -70,6 +74,9 @@ private AsymmetricX448Algorithm()
/// <inheritdoc/>
public override bool IsPublicKeyStandardFormat => true;

/// <inheritdoc/>
public override long MaxKeyUsageCount => MAX_KEY_USAGE_COUNT;

/// <inheritdoc/>
public override AsymmetricX448PrivateKey CreateKeyPair(CryptoOptions? options = null)
{
Expand Down
1 change: 1 addition & 0 deletions src/wan24-Crypto-BC/AsymmetricX448PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public override (byte[] Key, byte[] KeyExchangeData) GetKeyExchangeData(IAsymmet
{
EnsureUndisposed();
Algorithm.EnsureAllowed();
options?.KeySuite?.CountAsymmetricKeyUsage(this);
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 Down
Loading

0 comments on commit 61c7087

Please sign in to comment.