Table of Contents

Class MLKem1024

Namespace
CryptoHives.Foundation.Security.Cryptography.Kem
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

ML-KEM-1024 Key Encapsulation Mechanism (NIST security category 5).

public sealed class MLKem1024 : IKem, IDisposable
Inheritance
MLKem1024
Implements
Inherited Members

Remarks

ML-KEM-1024 is a post-quantum key encapsulation mechanism standardized in FIPS 203. It provides NIST security category 5 (equivalent to AES-256).

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 = 4, η₁ = 2, η₂ = 2
  • Encapsulation key: 1568 bytes
  • Decapsulation key: 3168 bytes
  • Ciphertext: 1568 bytes
  • Shared secret: 32 bytes

Example usage:

using var kem = MLKem1024.Create();

// Key generation
byte[] ek = new byte[MLKem1024.EncapsulationKeySizeBytesConst];
byte[] dk = new byte[MLKem1024.DecapsulationKeySizeBytesConst];
kem.GenerateKeyPair(ek, dk);

// Encapsulation (sender)
byte[] ct = new byte[MLKem1024.CiphertextSizeBytesConst];
byte[] ss1 = new byte[MLKem1024.SharedSecretSizeBytesConst];
kem.Encapsulate(ek, ct, ss1);

// Decapsulation (receiver)
byte[] ss2 = new byte[MLKem1024.SharedSecretSizeBytesConst];
kem.Decapsulate(dk, ct, ss2);
// ss1 and ss2 are now identical 32-byte shared secrets

Constructors

MLKem1024()

Initializes a new instance of the MLKem1024 class.

public MLKem1024()

Fields

CiphertextSizeBytesConst

The ciphertext size in bytes (1568 bytes).

public const int CiphertextSizeBytesConst = 1568

Field Value

int

DecapsulationKeySizeBytesConst

The decapsulation key size in bytes (3168 bytes).

public const int DecapsulationKeySizeBytesConst = 3168

Field Value

int

EncapsSeedSizeBytesConst

The encapsulation seed size in bytes (32 bytes).

public const int EncapsSeedSizeBytesConst = 32

Field Value

int

EncapsulationKeySizeBytesConst

The encapsulation key size in bytes (1568 bytes).

public const int EncapsulationKeySizeBytesConst = 1568

Field Value

int

KeyGenSeedSizeBytesConst

The key generation seed size in bytes (64 bytes).

public const int KeyGenSeedSizeBytesConst = 64

Field Value

int

SharedSecretSizeBytesConst

The shared secret size in bytes (32 bytes).

public const int SharedSecretSizeBytesConst = 32

Field Value

int

Properties

AlgorithmName

Gets the algorithm name.

public string AlgorithmName { get; }

Property Value

string

CiphertextSizeBytes

Gets the ciphertext size in bytes.

public int CiphertextSizeBytes { get; }

Property Value

int

DecapsulationKeySizeBytes

Gets the decapsulation key size in bytes.

public int DecapsulationKeySizeBytes { get; }

Property Value

int

EncapsulationKeySizeBytes

Gets the encapsulation key size in bytes.

public int EncapsulationKeySizeBytes { get; }

Property Value

int

SharedSecretSizeBytes

Gets the shared secret size in bytes.

public int SharedSecretSizeBytes { get; }

Property Value

int

Methods

Create()

Creates a new ML-KEM-1024 instance.

public static MLKem1024 Create()

Returns

MLKem1024

A new ML-KEM-1024 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

decapsulationKey ReadOnlySpan<byte>

The decapsulation (private) key.

ciphertext ReadOnlySpan<byte>

The ciphertext produced by encapsulation.

sharedSecret Span<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, or sharedSecret has 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

encapsulationKey ReadOnlySpan<byte>

The encapsulation (public) key.

seed ReadOnlySpan<byte>

The 32-byte random seed (m) for deterministic encapsulation.

ciphertext Span<byte>

The buffer to receive the ciphertext.

sharedSecret Span<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

encapsulationKey ReadOnlySpan<byte>

The encapsulation (public) key.

ciphertext Span<byte>

The buffer to receive the ciphertext.

sharedSecret Span<byte>

The buffer to receive the shared secret.

Exceptions

ArgumentException

encapsulationKey has an invalid size, or ciphertext or sharedSecret is 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

seed ReadOnlySpan<byte>

The deterministic seed (d ‖ z) for key generation.

encapsulationKey Span<byte>

The buffer to receive the encapsulation (public) key.

decapsulationKey Span<byte>

The buffer to receive the decapsulation (private) key.

Exceptions

ArgumentException

seed, encapsulationKey, or decapsulationKey has 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

seed ReadOnlySpan<byte>

The deterministic seed (d ‖ z) for key generation.

encapsulationKey Span<byte>

The buffer to receive the encapsulation (public) key.

decapsulationKey Span<byte>

The buffer to receive the decapsulation (private) key.

pairwiseConsistencyTest bool

true to verify the generated key pair; false to skip it.

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

encapsulationKey Span<byte>

The buffer to receive the encapsulation (public) key.

decapsulationKey Span<byte>

The buffer to receive the decapsulation (private) key.

Exceptions

ArgumentException

encapsulationKey or decapsulationKey is 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

encapsulationKey Span<byte>

The buffer to receive the encapsulation (public) key.

decapsulationKey Span<byte>

The buffer to receive the decapsulation (private) key.

pairwiseConsistencyTest bool

true 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.