Class MLKem512
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Kem
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
ML-KEM-512 Key Encapsulation Mechanism (NIST security category 1).
public sealed class MLKem512 : IKem, IDisposable
- Inheritance
-
MLKem512
- Implements
- Inherited Members
Remarks
ML-KEM-512 is a post-quantum key encapsulation mechanism standardized in FIPS 203. It provides NIST security category 1 (equivalent to AES-128).
ML-KEM is based on the Module-Lattice problem and is designed to be secure against both classical and quantum adversaries.
Parameters (FIPS 203 Table 1):
- k = 2, η₁ = 3, η₂ = 2
- Encapsulation key: 800 bytes
- Decapsulation key: 1632 bytes
- Ciphertext: 768 bytes
- Shared secret: 32 bytes
Example usage:
using var kem = MLKem512.Create();
// Key generation
byte[] ek = new byte[MLKem512.EncapsulationKeySizeBytesConst];
byte[] dk = new byte[MLKem512.DecapsulationKeySizeBytesConst];
kem.GenerateKeyPair(ek, dk);
// Encapsulation (sender)
byte[] ct = new byte[MLKem512.CiphertextSizeBytesConst];
byte[] ss1 = new byte[MLKem512.SharedSecretSizeBytesConst];
kem.Encapsulate(ek, ct, ss1);
// Decapsulation (receiver)
byte[] ss2 = new byte[MLKem512.SharedSecretSizeBytesConst];
kem.Decapsulate(dk, ct, ss2);
// ss1 and ss2 are now identical 32-byte shared secrets
Constructors
MLKem512()
Initializes a new instance of the MLKem512 class.
public MLKem512()
Fields
CiphertextSizeBytesConst
The ciphertext size in bytes (768 bytes).
public const int CiphertextSizeBytesConst = 768
Field Value
DecapsulationKeySizeBytesConst
The decapsulation key size in bytes (1632 bytes).
public const int DecapsulationKeySizeBytesConst = 1632
Field Value
EncapsSeedSizeBytesConst
The encapsulation seed size in bytes (32 bytes).
public const int EncapsSeedSizeBytesConst = 32
Field Value
EncapsulationKeySizeBytesConst
The encapsulation key size in bytes (800 bytes).
public const int EncapsulationKeySizeBytesConst = 800
Field Value
KeyGenSeedSizeBytesConst
The key generation seed size in bytes (64 bytes).
public const int KeyGenSeedSizeBytesConst = 64
Field Value
SharedSecretSizeBytesConst
The shared secret size in bytes (32 bytes).
public const int SharedSecretSizeBytesConst = 32
Field Value
Properties
AlgorithmName
Gets the algorithm name.
public string AlgorithmName { get; }
Property Value
CiphertextSizeBytes
Gets the ciphertext size in bytes.
public int CiphertextSizeBytes { get; }
Property Value
DecapsulationKeySizeBytes
Gets the decapsulation key size in bytes.
public int DecapsulationKeySizeBytes { get; }
Property Value
EncapsulationKeySizeBytes
Gets the encapsulation key size in bytes.
public int EncapsulationKeySizeBytes { get; }
Property Value
SharedSecretSizeBytes
Gets the shared secret size in bytes.
public int SharedSecretSizeBytes { get; }
Property Value
Methods
Create()
Creates a new ML-KEM-512 instance.
public static MLKem512 Create()
Returns
- MLKem512
A new ML-KEM-512 instance.
Decapsulate(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>)
Decapsulates the shared secret from the ciphertext using the decapsulation key.
public void Decapsulate(ReadOnlySpan<byte> decapsulationKey, ReadOnlySpan<byte> ciphertext, Span<byte> sharedSecret)
Parameters
decapsulationKeyReadOnlySpan<byte>The decapsulation (private) key.
ciphertextReadOnlySpan<byte>The ciphertext produced by encapsulation.
sharedSecretSpan<byte>The buffer to receive the shared secret.
Remarks
ML-KEM uses implicit rejection: if decapsulation fails, a pseudorandom value derived from the ciphertext and a secret value is returned instead of an error.
Exceptions
- ArgumentException
decapsulationKey,ciphertext, orsharedSecrethas an invalid size.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Encapsulate(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, Span<byte>)
Encapsulates a shared secret using the encapsulation key with a deterministic seed.
public void Encapsulate(ReadOnlySpan<byte> encapsulationKey, ReadOnlySpan<byte> seed, Span<byte> ciphertext, Span<byte> sharedSecret)
Parameters
encapsulationKeyReadOnlySpan<byte>The encapsulation (public) key.
seedReadOnlySpan<byte>The 32-byte random seed (m) for deterministic encapsulation.
ciphertextSpan<byte>The buffer to receive the ciphertext.
sharedSecretSpan<byte>The buffer to receive the shared secret.
Exceptions
- ArgumentException
Any parameter has an invalid size.
Encapsulate(ReadOnlySpan<byte>, Span<byte>, Span<byte>)
Encapsulates a shared secret using the encapsulation key.
public void Encapsulate(ReadOnlySpan<byte> encapsulationKey, Span<byte> ciphertext, Span<byte> sharedSecret)
Parameters
encapsulationKeyReadOnlySpan<byte>The encapsulation (public) key.
ciphertextSpan<byte>The buffer to receive the ciphertext.
sharedSecretSpan<byte>The buffer to receive the shared secret.
Exceptions
- ArgumentException
encapsulationKeyhas an invalid size, orciphertextorsharedSecretis too small.
GenerateKeyPair(ReadOnlySpan<byte>, Span<byte>, Span<byte>)
Generates an encapsulation key and a decapsulation key using the provided seed.
public void GenerateKeyPair(ReadOnlySpan<byte> seed, Span<byte> encapsulationKey, Span<byte> decapsulationKey)
Parameters
seedReadOnlySpan<byte>The deterministic seed (d ‖ z) for key generation.
encapsulationKeySpan<byte>The buffer to receive the encapsulation (public) key.
decapsulationKeySpan<byte>The buffer to receive the decapsulation (private) key.
Exceptions
- ArgumentException
seed,encapsulationKey, ordecapsulationKeyhas an invalid size.
GenerateKeyPair(ReadOnlySpan<byte>, Span<byte>, Span<byte>, bool)
Generates a key pair from a seed, optionally skipping the pairwise consistency test.
public void GenerateKeyPair(ReadOnlySpan<byte> seed, Span<byte> encapsulationKey, Span<byte> decapsulationKey, bool pairwiseConsistencyTest)
Parameters
seedReadOnlySpan<byte>The deterministic seed (d ‖ z) for key generation.
encapsulationKeySpan<byte>The buffer to receive the encapsulation (public) key.
decapsulationKeySpan<byte>The buffer to receive the decapsulation (private) key.
pairwiseConsistencyTestbool
Exceptions
- ArgumentException
A parameter has an invalid size.
GenerateKeyPair(Span<byte>, Span<byte>)
Generates an encapsulation key and a decapsulation key.
public void GenerateKeyPair(Span<byte> encapsulationKey, Span<byte> decapsulationKey)
Parameters
encapsulationKeySpan<byte>The buffer to receive the encapsulation (public) key.
decapsulationKeySpan<byte>The buffer to receive the decapsulation (private) key.
Exceptions
- ArgumentException
encapsulationKeyordecapsulationKeyis too small.
GenerateKeyPair(Span<byte>, Span<byte>, bool)
Generates a key pair, optionally skipping the pairwise consistency test.
public void GenerateKeyPair(Span<byte> encapsulationKey, Span<byte> decapsulationKey, bool pairwiseConsistencyTest)
Parameters
encapsulationKeySpan<byte>The buffer to receive the encapsulation (public) key.
decapsulationKeySpan<byte>The buffer to receive the decapsulation (private) key.
pairwiseConsistencyTestbooltrue to verify the generated key pair with an encapsulate/decapsulate round trip; false to skip it. See the remarks on the KEM type.
Exceptions
- ArgumentException
A buffer is too small.