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.
The NativeCryptoProvider is the default provider for:
| Property | Type | Description |
|---|---|---|
Name | string | "Native (P/Invoke)" |
IsAvailable | bool | true if OpenSSL 3.6 is reachable |
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()}"); }
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)
Signs data with ML-DSA.
byte[] data = Encoding.UTF8.GetBytes("Important data"); byte[] signature = await provider.SignMlDsaAsync(data, privateKey);
Verifies an ML-DSA signature.
bool isValid = await provider.VerifyMlDsaAsync(data, signature, publicKey);
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)
Encapsulates a shared secret with the public key.
var (sharedSecret, ciphertext) = await provider.EncapsulateAsync(recipientPublicKey); // sharedSecret: 32 bytes symmetric key // ciphertext: Send to recipient
Decapsulates the shared secret.
byte[] sharedSecret = await provider.DecapsulateAsync(ciphertext, privateKey);
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);
Signs TBS certificate data with ML-DSA.
byte[] tbsCertificate = GetTbsCertificate(); byte[] signedCert = await provider.SignCertificateAsync(tbsCertificate, privateKey);
| 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[]> |
// 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 |
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}");
CryptographicOperations.ZeroMemoryWolfgang van der Stille @ EMSR DATA d.o.o. - Post-Quantum Cryptography Professional