Inhaltsverzeichnis
NativeCryptoProvider
Namespace: WvdS.System.Security.Cryptography.Providers
P/Invoke-based crypto provider for server and desktop applications. Communicates directly with OpenSSL 3.6 via Platform Invocation Services.
Overview
The NativeCryptoProvider is the default provider for:
- Blazor Server
- ASP.NET Core
- Desktop applications (Windows, Linux, macOS)
- Console applications
- Windows Services / Linux Daemons
Properties
| Property | Type | Description |
|---|---|---|
Name | string | "Native (P/Invoke)" |
IsAvailable | bool | true if OpenSSL 3.6 is reachable |
Initialization
using WvdS.System.Security.Cryptography.Providers; // Create provider var provider = new NativeCryptoProvider(); // Initialize (loads OpenSSL) await provider.InitializeAsync(); // Check if available if (provider.IsAvailable) { Console.WriteLine($"Provider: {provider.Name}"); Console.WriteLine($"OpenSSL: {provider.GetOpenSslVersion()}"); }
ML-DSA Operations
GenerateMlDsaKeyPairAsync
Generates an ML-DSA key pair.
var (publicKey, privateKey) = await provider.GenerateMlDsaKeyPairAsync("ML-DSA-65"); // Supported algorithms: // - "ML-DSA-44" (NIST Level 1) // - "ML-DSA-65" (NIST Level 3, recommended) // - "ML-DSA-87" (NIST Level 5)
SignMlDsaAsync
Signs data with ML-DSA.
byte[] data = Encoding.UTF8.GetBytes("Important data"); byte[] signature = await provider.SignMlDsaAsync(data, privateKey);
VerifyMlDsaAsync
Verifies an ML-DSA signature.
bool isValid = await provider.VerifyMlDsaAsync(data, signature, publicKey);
ML-KEM Operations
GenerateMlKemKeyPairAsync
Generates an ML-KEM key pair.
var (publicKey, privateKey) = await provider.GenerateMlKemKeyPairAsync("ML-KEM-768"); // Supported algorithms: // - "ML-KEM-512" (NIST Level 1) // - "ML-KEM-768" (NIST Level 3, recommended) // - "ML-KEM-1024" (NIST Level 5)
EncapsulateAsync
Encapsulates a shared secret with the public key.
var (sharedSecret, ciphertext) = await provider.EncapsulateAsync(recipientPublicKey); // sharedSecret: 32 bytes symmetric key // ciphertext: Send to recipient
DecapsulateAsync
Decapsulates the shared secret.
byte[] sharedSecret = await provider.DecapsulateAsync(ciphertext, privateKey);
Certificate Operations
CreateEphemeralCertificateAsync
Creates an ephemeral ML-DSA certificate.
var (pubKey, privKey) = await provider.GenerateMlDsaKeyPairAsync(); byte[] certBytes = await provider.CreateEphemeralCertificateAsync( "CN=Ephemeral Test", TimeSpan.FromHours(24), privKey); var cert = new X509Certificate2(certBytes);
SignCertificateAsync
Signs TBS certificate data with ML-DSA.
byte[] tbsCertificate = GetTbsCertificate(); byte[] signedCert = await provider.SignCertificateAsync(tbsCertificate, privateKey);
Method Overview
| Method | Parameters | Return |
|---|---|---|
InitializeAsync() | - | Task |
GetOpenSslVersion() | - | string |
GenerateMlDsaKeyPairAsync | string algorithm | Task<(byte[], byte[])> |
SignMlDsaAsync | byte[] data, byte[] privateKey | Task<byte[]> |
VerifyMlDsaAsync | byte[] data, byte[] signature, byte[] publicKey | Task<bool> |
GenerateMlKemKeyPairAsync | string algorithm | Task<(byte[], byte[])> |
EncapsulateAsync | byte[] publicKey | Task<(byte[], byte[])> |
DecapsulateAsync | byte[] ciphertext, byte[] privateKey | Task<byte[]> |
CreateEphemeralCertificateAsync | string subject, TimeSpan validity, byte[] privateKey | Task<byte[]> |
SignCertificateAsync | byte[] tbsCertificate, byte[] privateKey | Task<byte[]> |
OpenSSL Path Configuration
// Set the path before InitializeAsync() CryptoConfig.OpenSslPath = @"C:\OpenSSL\bin"; var provider = new NativeCryptoProvider(); await provider.InitializeAsync();
Default search paths:
| Operating System | Paths |
|---|---|
| Windows | .\, C:\OpenSSL\bin, PATH |
| Linux | /usr/local/lib64, /usr/lib/x86_64-linux-gnu |
| macOS | /opt/homebrew/lib, /usr/local/lib |
Performance Notes
P/Invoke Performance:
- Synchronous execution in native code
- Task wrapper for API consistency with WasmCryptoProvider
- Minimal overhead through direct memory access
- Thread-safe through OpenSSL's internal synchronization
Complete Example
using WvdS.System.Security.Cryptography.Providers; // 1. Initialize provider var provider = new NativeCryptoProvider(); await provider.InitializeAsync(); Console.WriteLine($"OpenSSL: {provider.GetOpenSslVersion()}"); // 2. ML-KEM Key Exchange var (alicePublic, alicePrivate) = await provider.GenerateMlKemKeyPairAsync(); var (bobPublic, bobPrivate) = await provider.GenerateMlKemKeyPairAsync(); // Alice encapsulates for Bob var (aliceSecret, ciphertext) = await provider.EncapsulateAsync(bobPublic); // Bob decapsulates var bobSecret = await provider.DecapsulateAsync(ciphertext, bobPrivate); // Shared secrets are identical Console.WriteLine($"Keys match: {aliceSecret.SequenceEqual(bobSecret)}"); // 3. ML-DSA Signature var (sigPubKey, sigPrivKey) = await provider.GenerateMlDsaKeyPairAsync(); byte[] message = Encoding.UTF8.GetBytes("Important message"); byte[] signature = await provider.SignMlDsaAsync(message, sigPrivKey); bool isValid = await provider.VerifyMlDsaAsync(message, signature, sigPubKey); Console.WriteLine($"Signature valid: {isValid}");
Security Notes
- Requires OpenSSL 3.6.0 or higher with PQ algorithms
- Private keys are held in process memory
- Keys are not automatically deleted at application end
- For highest security: explicitly delete keys with
CryptographicOperations.ZeroMemory
See Also
Wolfgang van der Stille @ EMSR DATA d.o.o. - Post-Quantum Cryptography Professional
Zuletzt geändert: on 2026/01/30 at 01:13 AM