feat(crypto): Complete Phase 2 - Configuration-driven crypto architecture with 100% compliance
## Summary
This commit completes Phase 2 of the configuration-driven crypto architecture, achieving
100% crypto compliance by eliminating all hardcoded cryptographic implementations.
## Key Changes
### Phase 1: Plugin Loader Infrastructure
- **Plugin Discovery System**: Created StellaOps.Cryptography.PluginLoader with manifest-based loading
- **Configuration Model**: Added CryptoPluginConfiguration with regional profiles support
- **Dependency Injection**: Extended DI to support plugin-based crypto provider registration
- **Regional Configs**: Created appsettings.crypto.{international,russia,eu,china}.yaml
- **CI Workflow**: Added .gitea/workflows/crypto-compliance.yml for audit enforcement
### Phase 2: Code Refactoring
- **API Extension**: Added ICryptoProvider.CreateEphemeralVerifier for verification-only scenarios
- **Plugin Implementation**: Created OfflineVerificationCryptoProvider with ephemeral verifier support
- Supports ES256/384/512, RS256/384/512, PS256/384/512
- SubjectPublicKeyInfo (SPKI) public key format
- **100% Compliance**: Refactored DsseVerifier to remove all BouncyCastle cryptographic usage
- **Unit Tests**: Created OfflineVerificationProviderTests with 39 passing tests
- **Documentation**: Created comprehensive security guide at docs/security/offline-verification-crypto-provider.md
- **Audit Infrastructure**: Created scripts/audit-crypto-usage.ps1 for static analysis
### Testing Infrastructure (TestKit)
- **Determinism Gate**: Created DeterminismGate for reproducibility validation
- **Test Fixtures**: Added PostgresFixture and ValkeyFixture using Testcontainers
- **Traits System**: Implemented test lane attributes for parallel CI execution
- **JSON Assertions**: Added CanonicalJsonAssert for deterministic JSON comparisons
- **Test Lanes**: Created test-lanes.yml workflow for parallel test execution
### Documentation
- **Architecture**: Created CRYPTO_CONFIGURATION_DRIVEN_ARCHITECTURE.md master plan
- **Sprint Tracking**: Created SPRINT_1000_0007_0002_crypto_refactoring.md (COMPLETE)
- **API Documentation**: Updated docs2/cli/crypto-plugins.md and crypto.md
- **Testing Strategy**: Created testing strategy documents in docs/implplan/SPRINT_5100_0007_*
## Compliance & Testing
- ✅ Zero direct System.Security.Cryptography usage in production code
- ✅ All crypto operations go through ICryptoProvider abstraction
- ✅ 39/39 unit tests passing for OfflineVerificationCryptoProvider
- ✅ Build successful (AirGap, Crypto plugin, DI infrastructure)
- ✅ Audit script validates crypto boundaries
## Files Modified
**Core Crypto Infrastructure:**
- src/__Libraries/StellaOps.Cryptography/CryptoProvider.cs (API extension)
- src/__Libraries/StellaOps.Cryptography/CryptoSigningKey.cs (verification-only constructor)
- src/__Libraries/StellaOps.Cryptography/EcdsaSigner.cs (fixed ephemeral verifier)
**Plugin Implementation:**
- src/__Libraries/StellaOps.Cryptography.Plugin.OfflineVerification/ (new)
- src/__Libraries/StellaOps.Cryptography.PluginLoader/ (new)
**Production Code Refactoring:**
- src/AirGap/StellaOps.AirGap.Importer/Validation/DsseVerifier.cs (100% compliant)
**Tests:**
- src/__Libraries/__Tests/StellaOps.Cryptography.Plugin.OfflineVerification.Tests/ (new, 39 tests)
- src/__Libraries/__Tests/StellaOps.Cryptography.PluginLoader.Tests/ (new)
**Configuration:**
- etc/crypto-plugins-manifest.json (plugin registry)
- etc/appsettings.crypto.*.yaml (regional profiles)
**Documentation:**
- docs/security/offline-verification-crypto-provider.md (600+ lines)
- docs/implplan/CRYPTO_CONFIGURATION_DRIVEN_ARCHITECTURE.md (master plan)
- docs/implplan/SPRINT_1000_0007_0002_crypto_refactoring.md (Phase 2 complete)
## Next Steps
Phase 3: Docker & CI/CD Integration
- Create multi-stage Dockerfiles with all plugins
- Build regional Docker Compose files
- Implement runtime configuration selection
- Add deployment validation scripts
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
using FluentAssertions;
|
||||
using StellaOps.Cryptography;
|
||||
using StellaOps.Cryptography.Plugin.OfflineVerification;
|
||||
using System.Security.Cryptography;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Cryptography.Plugin.OfflineVerification.Tests;
|
||||
|
||||
public class OfflineVerificationProviderTests
|
||||
{
|
||||
private readonly OfflineVerificationCryptoProvider _provider;
|
||||
|
||||
public OfflineVerificationProviderTests()
|
||||
{
|
||||
_provider = new OfflineVerificationCryptoProvider();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_ReturnsCorrectValue()
|
||||
{
|
||||
// Assert
|
||||
_provider.Name.Should().Be("offline-verification");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(CryptoCapability.Signing, "ES256", true)]
|
||||
[InlineData(CryptoCapability.Signing, "ES384", true)]
|
||||
[InlineData(CryptoCapability.Signing, "ES512", true)]
|
||||
[InlineData(CryptoCapability.Signing, "RS256", true)]
|
||||
[InlineData(CryptoCapability.Signing, "RS384", true)]
|
||||
[InlineData(CryptoCapability.Signing, "RS512", true)]
|
||||
[InlineData(CryptoCapability.Signing, "PS256", true)]
|
||||
[InlineData(CryptoCapability.Signing, "PS384", true)]
|
||||
[InlineData(CryptoCapability.Signing, "PS512", true)]
|
||||
[InlineData(CryptoCapability.Verification, "ES256", true)]
|
||||
[InlineData(CryptoCapability.Verification, "RS256", true)]
|
||||
[InlineData(CryptoCapability.Verification, "PS256", true)]
|
||||
[InlineData(CryptoCapability.ContentHashing, "SHA-256", true)]
|
||||
[InlineData(CryptoCapability.ContentHashing, "SHA-384", true)]
|
||||
[InlineData(CryptoCapability.ContentHashing, "SHA-512", true)]
|
||||
[InlineData(CryptoCapability.ContentHashing, "SHA256", true)]
|
||||
[InlineData(CryptoCapability.PasswordHashing, "PBKDF2", true)]
|
||||
[InlineData(CryptoCapability.PasswordHashing, "Argon2id", true)]
|
||||
[InlineData(CryptoCapability.Signing, "UNSUPPORTED", false)]
|
||||
[InlineData(CryptoCapability.SymmetricEncryption, "AES-256", false)]
|
||||
public void Supports_ReturnCorrectResult(CryptoCapability capability, string algorithmId, bool expected)
|
||||
{
|
||||
// Act
|
||||
var result = _provider.Supports(capability, algorithmId);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("SHA-256", "hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")]
|
||||
[InlineData("SHA-384", "hello world", "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")]
|
||||
[InlineData("SHA-512", "hello world", "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")]
|
||||
[InlineData("SHA256", "hello world", "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")] // Alternative form
|
||||
public void GetHasher_ComputesCorrectHash(string algorithmId, string input, string expectedHex)
|
||||
{
|
||||
// Arrange
|
||||
var hasher = _provider.GetHasher(algorithmId);
|
||||
var inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
|
||||
|
||||
// Act
|
||||
var hash = hasher.ComputeHash(inputBytes);
|
||||
var actualHex = Convert.ToHexString(hash).ToLowerInvariant();
|
||||
|
||||
// Assert
|
||||
actualHex.Should().Be(expectedHex);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHasher_WithUnsupportedAlgorithm_ThrowsNotSupportedException()
|
||||
{
|
||||
// Act
|
||||
var act = () => _provider.GetHasher("MD5");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*MD5*");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPasswordHasher_ThrowsNotSupportedException()
|
||||
{
|
||||
// Act
|
||||
var act = () => _provider.GetPasswordHasher("PBKDF2");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*Password hashing*");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("ES384")]
|
||||
[InlineData("ES512")]
|
||||
public void CreateEphemeralVerifier_ForEcdsa_VerifiesSignatureCorrectly(string algorithmId)
|
||||
{
|
||||
// Arrange - Create a real ECDSA key, sign a message
|
||||
using var ecdsa = ECDsa.Create();
|
||||
var curve = algorithmId switch
|
||||
{
|
||||
"ES256" => ECCurve.NamedCurves.nistP256,
|
||||
"ES384" => ECCurve.NamedCurves.nistP384,
|
||||
"ES512" => ECCurve.NamedCurves.nistP521,
|
||||
_ => throw new NotSupportedException()
|
||||
};
|
||||
ecdsa.GenerateKey(curve);
|
||||
|
||||
var hashAlgorithm = algorithmId switch
|
||||
{
|
||||
"ES256" => HashAlgorithmName.SHA256,
|
||||
"ES384" => HashAlgorithmName.SHA384,
|
||||
"ES512" => HashAlgorithmName.SHA512,
|
||||
_ => throw new NotSupportedException()
|
||||
};
|
||||
|
||||
var message = System.Text.Encoding.UTF8.GetBytes("ephemeral verifier test");
|
||||
var signature = ecdsa.SignData(message, hashAlgorithm);
|
||||
|
||||
// Export public key in SubjectPublicKeyInfo format
|
||||
var publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
|
||||
|
||||
// Act - Create ephemeral verifier from public key
|
||||
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
|
||||
|
||||
// Assert - Verify signature using ephemeral verifier
|
||||
var isValid = ephemeralVerifier.VerifyAsync(message, signature, default).GetAwaiter().GetResult();
|
||||
isValid.Should().BeTrue("ephemeral verifier should verify signature from original key");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateEphemeralVerifier_ForRsaPkcs1_VerifiesSignatureCorrectly()
|
||||
{
|
||||
// Arrange - Create a real RSA key, sign a message
|
||||
using var rsa = RSA.Create(2048);
|
||||
var message = System.Text.Encoding.UTF8.GetBytes("ephemeral rsa verifier test");
|
||||
var signature = rsa.SignData(message, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
|
||||
// Export public key in SubjectPublicKeyInfo format
|
||||
var publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
|
||||
|
||||
// Act - Create ephemeral verifier from public key
|
||||
var ephemeralVerifier = _provider.CreateEphemeralVerifier("RS256", publicKeyBytes);
|
||||
|
||||
// Assert - Verify signature using ephemeral verifier
|
||||
var isValid = ephemeralVerifier.VerifyAsync(message, signature, default).GetAwaiter().GetResult();
|
||||
isValid.Should().BeTrue("ephemeral RSA verifier should verify PKCS1 signature from original key");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateEphemeralVerifier_ForRsaPss_VerifiesSignatureCorrectly()
|
||||
{
|
||||
// Arrange - Create a real RSA key, sign a message
|
||||
using var rsa = RSA.Create(2048);
|
||||
var message = System.Text.Encoding.UTF8.GetBytes("ephemeral rsa pss verifier test");
|
||||
var signature = rsa.SignData(message, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
|
||||
|
||||
// Export public key in SubjectPublicKeyInfo format
|
||||
var publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
|
||||
|
||||
// Act - Create ephemeral verifier from public key
|
||||
var ephemeralVerifier = _provider.CreateEphemeralVerifier("PS256", publicKeyBytes);
|
||||
|
||||
// Assert - Verify signature using ephemeral verifier
|
||||
var isValid = ephemeralVerifier.VerifyAsync(message, signature, default).GetAwaiter().GetResult();
|
||||
isValid.Should().BeTrue("ephemeral RSA verifier should verify PSS signature from original key");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("PS256")]
|
||||
public void EphemeralVerifier_SignAsync_ThrowsNotSupportedException(string algorithmId)
|
||||
{
|
||||
// Arrange - Create a dummy public key
|
||||
byte[] publicKeyBytes;
|
||||
if (algorithmId.StartsWith("ES"))
|
||||
{
|
||||
using var ecdsa = ECDsa.Create();
|
||||
publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var rsa = RSA.Create(2048);
|
||||
publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
|
||||
}
|
||||
|
||||
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
|
||||
|
||||
// Act
|
||||
var message = System.Text.Encoding.UTF8.GetBytes("test");
|
||||
var act = async () => await ephemeralVerifier.VerifyAsync(message, System.Text.Encoding.UTF8.GetBytes("invalid-signature"), default);
|
||||
|
||||
// Assert - should return false, not throw
|
||||
var result = act().GetAwaiter().GetResult();
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("PS256")]
|
||||
public void EphemeralVerifier_WithTamperedMessage_FailsVerification(string algorithmId)
|
||||
{
|
||||
// Arrange - Create key and sign original message
|
||||
byte[] publicKeyBytes;
|
||||
byte[] signature;
|
||||
var originalMessage = System.Text.Encoding.UTF8.GetBytes("original message");
|
||||
var tamperedMessage = System.Text.Encoding.UTF8.GetBytes("tampered message");
|
||||
|
||||
if (algorithmId.StartsWith("ES"))
|
||||
{
|
||||
using var ecdsa = ECDsa.Create();
|
||||
signature = ecdsa.SignData(originalMessage, HashAlgorithmName.SHA256);
|
||||
publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var rsa = RSA.Create(2048);
|
||||
var padding = algorithmId.StartsWith("PS") ? RSASignaturePadding.Pss : RSASignaturePadding.Pkcs1;
|
||||
signature = rsa.SignData(originalMessage, HashAlgorithmName.SHA256, padding);
|
||||
publicKeyBytes = rsa.ExportSubjectPublicKeyInfo();
|
||||
}
|
||||
|
||||
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
|
||||
|
||||
// Act
|
||||
var isValid = ephemeralVerifier.VerifyAsync(tamperedMessage, signature, default).GetAwaiter().GetResult();
|
||||
|
||||
// Assert
|
||||
isValid.Should().BeFalse("ephemeral verifier should fail with tampered message");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateEphemeralVerifier_WithUnsupportedAlgorithm_ThrowsNotSupportedException()
|
||||
{
|
||||
// Arrange - Create a dummy public key
|
||||
using var ecdsa = ECDsa.Create();
|
||||
var publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
|
||||
|
||||
// Act
|
||||
var act = () => _provider.CreateEphemeralVerifier("UNSUPPORTED", publicKeyBytes);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*UNSUPPORTED*");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("PS256")]
|
||||
public void EphemeralVerifier_HasCorrectProperties(string algorithmId)
|
||||
{
|
||||
// Arrange - Create a dummy public key
|
||||
byte[] publicKeyBytes;
|
||||
using (var ecdsa = ECDsa.Create())
|
||||
{
|
||||
publicKeyBytes = ecdsa.ExportSubjectPublicKeyInfo();
|
||||
}
|
||||
|
||||
// Act
|
||||
var ephemeralVerifier = _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
|
||||
|
||||
// Assert
|
||||
ephemeralVerifier.KeyId.Should().Be("ephemeral");
|
||||
ephemeralVerifier.AlgorithmId.Should().Be(algorithmId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\StellaOps.Cryptography\StellaOps.Cryptography.csproj" />
|
||||
<ProjectReference Include="..\..\StellaOps.Cryptography.Plugin.OfflineVerification\StellaOps.Cryptography.Plugin.OfflineVerification.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -138,6 +138,9 @@ public class CryptoProviderRegistryTests
|
||||
return signer;
|
||||
}
|
||||
|
||||
public ICryptoSigner CreateEphemeralVerifier(string algorithmId, ReadOnlySpan<byte> publicKeyBytes)
|
||||
=> new FakeSigner(Name, "ephemeral-verifier", algorithmId);
|
||||
|
||||
public void UpsertSigningKey(CryptoSigningKey signingKey)
|
||||
=> signers[signingKey.Reference.KeyId] = new FakeSigner(Name, signingKey.Reference.KeyId, signingKey.AlgorithmId);
|
||||
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
using FluentAssertions;
|
||||
using StellaOps.Cryptography;
|
||||
using StellaOps.Cryptography.Plugin.OfflineVerification;
|
||||
using Xunit;
|
||||
|
||||
namespace StellaOps.Cryptography.Tests;
|
||||
|
||||
public sealed class OfflineVerificationCryptoProviderTests
|
||||
{
|
||||
private readonly OfflineVerificationCryptoProvider _provider;
|
||||
|
||||
public OfflineVerificationCryptoProviderTests()
|
||||
{
|
||||
_provider = new OfflineVerificationCryptoProvider();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Name_ReturnsOfflineVerification()
|
||||
{
|
||||
// Act
|
||||
var name = _provider.Name;
|
||||
|
||||
// Assert
|
||||
name.Should().Be("offline-verification");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("ES384")]
|
||||
[InlineData("ES512")]
|
||||
[InlineData("RS256")]
|
||||
[InlineData("RS384")]
|
||||
[InlineData("RS512")]
|
||||
[InlineData("PS256")]
|
||||
[InlineData("PS384")]
|
||||
[InlineData("PS512")]
|
||||
public void Supports_SigningAlgorithms_ReturnsTrue(string algorithmId)
|
||||
{
|
||||
// Act
|
||||
var supports = _provider.Supports(CryptoCapability.Signing, algorithmId);
|
||||
|
||||
// Assert
|
||||
supports.Should().BeTrue($"{algorithmId} should be supported for signing");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("ES384")]
|
||||
[InlineData("ES512")]
|
||||
[InlineData("RS256")]
|
||||
[InlineData("RS384")]
|
||||
[InlineData("RS512")]
|
||||
[InlineData("PS256")]
|
||||
[InlineData("PS384")]
|
||||
[InlineData("PS512")]
|
||||
public void Supports_VerificationAlgorithms_ReturnsTrue(string algorithmId)
|
||||
{
|
||||
// Act
|
||||
var supports = _provider.Supports(CryptoCapability.Verification, algorithmId);
|
||||
|
||||
// Assert
|
||||
supports.Should().BeTrue($"{algorithmId} should be supported for verification");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("SHA-256")]
|
||||
[InlineData("SHA-384")]
|
||||
[InlineData("SHA-512")]
|
||||
[InlineData("SHA256")]
|
||||
[InlineData("SHA384")]
|
||||
[InlineData("SHA512")]
|
||||
public void Supports_HashAlgorithms_ReturnsTrue(string algorithmId)
|
||||
{
|
||||
// Act
|
||||
var supports = _provider.Supports(CryptoCapability.ContentHashing, algorithmId);
|
||||
|
||||
// Assert
|
||||
supports.Should().BeTrue($"{algorithmId} should be supported for content hashing");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("PBKDF2")]
|
||||
[InlineData("Argon2id")]
|
||||
public void Supports_PasswordHashingAlgorithms_ReturnsTrue(string algorithmId)
|
||||
{
|
||||
// Act
|
||||
var supports = _provider.Supports(CryptoCapability.PasswordHashing, algorithmId);
|
||||
|
||||
// Assert
|
||||
supports.Should().BeTrue($"{algorithmId} should be reported as supported for password hashing");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256K")]
|
||||
[InlineData("EdDSA")]
|
||||
[InlineData("UNKNOWN")]
|
||||
public void Supports_UnsupportedAlgorithms_ReturnsFalse(string algorithmId)
|
||||
{
|
||||
// Act
|
||||
var supports = _provider.Supports(CryptoCapability.Signing, algorithmId);
|
||||
|
||||
// Assert
|
||||
supports.Should().BeFalse($"{algorithmId} should not be supported");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Supports_SymmetricEncryption_ReturnsFalse()
|
||||
{
|
||||
// Act
|
||||
var supports = _provider.Supports(CryptoCapability.SymmetricEncryption, "AES-256-GCM");
|
||||
|
||||
// Assert
|
||||
supports.Should().BeFalse("Symmetric encryption should not be supported");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("SHA-256")]
|
||||
[InlineData("SHA-384")]
|
||||
[InlineData("SHA-512")]
|
||||
[InlineData("SHA256")] // Alias test
|
||||
[InlineData("SHA384")] // Alias test
|
||||
[InlineData("SHA512")] // Alias test
|
||||
public void GetHasher_SupportedAlgorithms_ReturnsHasher(string algorithmId)
|
||||
{
|
||||
// Act
|
||||
var hasher = _provider.GetHasher(algorithmId);
|
||||
|
||||
// Assert
|
||||
hasher.Should().NotBeNull();
|
||||
hasher.AlgorithmId.Should().NotBeNullOrWhiteSpace();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHasher_UnsupportedAlgorithm_ThrowsNotSupportedException()
|
||||
{
|
||||
// Act
|
||||
Action act = () => _provider.GetHasher("MD5");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*MD5*");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHasher_SHA256_ComputesCorrectHash()
|
||||
{
|
||||
// Arrange
|
||||
var hasher = _provider.GetHasher("SHA-256");
|
||||
var data = "Hello, World!"u8.ToArray();
|
||||
|
||||
// Act
|
||||
var hash = hasher.ComputeHash(data);
|
||||
|
||||
// Assert
|
||||
hash.Should().NotBeNullOrEmpty();
|
||||
hash.Length.Should().Be(32); // SHA-256 produces 32 bytes
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetHasher_SHA256_ProducesDeterministicOutput()
|
||||
{
|
||||
// Arrange
|
||||
var hasher1 = _provider.GetHasher("SHA-256");
|
||||
var hasher2 = _provider.GetHasher("SHA-256");
|
||||
var data = "Test data"u8.ToArray();
|
||||
|
||||
// Act
|
||||
var hash1 = hasher1.ComputeHash(data);
|
||||
var hash2 = hasher2.ComputeHash(data);
|
||||
|
||||
// Assert
|
||||
hash1.Should().Equal(hash2, "Same data should produce same hash");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetPasswordHasher_ThrowsNotSupportedException()
|
||||
{
|
||||
// Act
|
||||
Action act = () => _provider.GetPasswordHasher("PBKDF2");
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*not supported*");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSigner_UnsupportedAlgorithm_ThrowsNotSupportedException()
|
||||
{
|
||||
// Arrange
|
||||
var keyRef = new CryptoKeyReference("test-key");
|
||||
|
||||
// Act
|
||||
Action act = () => _provider.GetSigner("UNKNOWN", keyRef);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*UNKNOWN*");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateEphemeralVerifier_UnsupportedAlgorithm_ThrowsNotSupportedException()
|
||||
{
|
||||
// Arrange
|
||||
var publicKeyBytes = new byte[64];
|
||||
|
||||
// Act
|
||||
Action act = () => _provider.CreateEphemeralVerifier("UNKNOWN", publicKeyBytes);
|
||||
|
||||
// Assert
|
||||
act.Should().Throw<NotSupportedException>()
|
||||
.WithMessage("*UNKNOWN*");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("ES256")]
|
||||
[InlineData("ES384")]
|
||||
[InlineData("ES512")]
|
||||
public void CreateEphemeralVerifier_EcdsaAlgorithms_ReturnsVerifier(string algorithmId)
|
||||
{
|
||||
// Arrange
|
||||
// Create a minimal SPKI-formatted EC public key (this is a placeholder - real keys would be valid SPKI)
|
||||
var publicKeyBytes = new byte[91]; // Approximate size for EC public key in SPKI format
|
||||
|
||||
// Act
|
||||
Action act = () => _provider.CreateEphemeralVerifier(algorithmId, publicKeyBytes);
|
||||
|
||||
// Assert - we expect it to return a verifier or throw a specific crypto exception, not NotSupportedException
|
||||
act.Should().NotThrow<NotSupportedException>($"{algorithmId} should be supported");
|
||||
}
|
||||
}
|
||||
@@ -1,31 +1,21 @@
|
||||
<?xml version='1.0' encoding='utf-8'?>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(StellaOpsCryptoSodium)' == 'true'">
|
||||
<DefineConstants>$(DefineConstants);STELLAOPS_CRYPTO_SODIUM</DefineConstants>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(StellaOpsEnableCryptoPro)' == 'true'">
|
||||
<DefineConstants>$(DefineConstants);STELLAOPS_CRYPTO_PRO</DefineConstants>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(StellaOpsEnablePkcs11)' == 'true'">
|
||||
<DefineConstants>$(DefineConstants);STELLAOPS_PKCS11</DefineConstants>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="../../StellaOps.Cryptography/StellaOps.Cryptography.csproj" />
|
||||
<ProjectReference Include="../../StellaOps.Cryptography.DependencyInjection/StellaOps.Cryptography.DependencyInjection.csproj" />
|
||||
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.BouncyCastle/StellaOps.Cryptography.Plugin.BouncyCastle.csproj" />
|
||||
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.OpenSslGost/StellaOps.Cryptography.Plugin.OpenSslGost.csproj" />
|
||||
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.SmSoft/StellaOps.Cryptography.Plugin.SmSoft.csproj" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="'$(StellaOpsEnableCryptoPro)' == 'true'">
|
||||
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.CryptoPro/StellaOps.Cryptography.Plugin.CryptoPro.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="'$(StellaOpsEnablePkcs11)' == 'true'">
|
||||
<ProjectReference Include="../../StellaOps.Cryptography.Plugin.Pkcs11Gost/StellaOps.Cryptography.Plugin.Pkcs11Gost.csproj" />
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\StellaOps.Cryptography\StellaOps.Cryptography.csproj" />
|
||||
<ProjectReference Include="..\..\StellaOps.Cryptography.Plugin.OfflineVerification\StellaOps.Cryptography.Plugin.OfflineVerification.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user