java.lang.Object
org.bouncycastle.pqc.crypto.mlkem.MLKEMEngine
Core engine for ML-KEM (Module-Lattice Key Encapsulation Mechanism) operations. Implements the cryptographic primitives for key generation, encapsulation, and decapsulation.
Field Summary
Modifier and TypeFieldDescriptionprivate final intSize named by the crypto suffix of the variant.private final intCiphertext length named by the crypto suffix.private final intPublic key length named by the crypto suffix.private final intSecret key length named by the crypto suffix.private MLKEMIndCpaThe IND-CPA core of the KEM.private final intCiphertext length, in bytes.private final intEta1 noise parameter value.private static final intEta2 noise parameter value.private final intIND-CPA scheme size, in bytes.private static final intMessage length of the IND-CPA scheme, in bytes.private final intIND-CPA public key length, in bytes.private final intIND-CPA secret key length, in bytes.private final intThe k parameter of the Kyber variant.static final intKyber N parameterstatic final intKyber polynomial bytesprivate final intCompressed polynomial length, in bytes.private final intPolynomial vector length, in bytes.private final intCompressed polynomial vector length, in bytes.private final intEncapsulating key length, in bytes.static final intKyber Q parameterstatic final intKyber Q inverse parameterprivate final intDecapsulating key length, in bytes.private static final intShared secret length, in bytes.static final intNumber of bytes for Hashes and Seedsprivate SecureRandomRandom source for the key generation and encapsulation.private final intSession key length derived per variant, in bytes.private final SymmetricThe symmetric primitives used by the scheme.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate voidcmov(byte[] r, byte[] x, int xlen, boolean b) Conditionally copies x into r, constant time.byte[][]Generate a KEM key pair, drawing both seeds from the bound entropy source.byte[][]generateKemKeyPairInternal(byte[] d, byte[] z) Expand the two seeds into the full ML-KEM key pair.intTotal key material length in bytes at the KEM layer.intCiphertext length in bytes at the KEM layer.intPublic key length in bytes at the KEM layer.intSecret key length in bytes at the KEM layer.intCiphertext length in bytes produced by the IND-CPA scheme.intThe eta1 noise parameter.static intThe eta2 noise parameter.intSeed length in bytes for the IND-CPA scheme.static intMessage length in bytes that the IND-CPA scheme accepts.intPublic key length in bytes for the IND-CPA scheme.intSecret key length in bytes for the IND-CPA scheme.intThe security parameter: 2, 3 or 4.intSize in bytes of one compressed polynomial.intSize in bytes of one uncompressed polynomial vector.intSize in bytes of one compressed polynomial vector.intPublic key length in bytes before the KEM layer wraps it.intSecret key length in bytes for the IND-CPA scheme.voidgetRandomBytes(byte[] buf) Fill a buffer with bytes from the bound entropy source.The symmetric primitives bound to this parameter set.voidinit(SecureRandom random) Bind the entropy source used for key generation and encapsulation.byte[]kemDecrypt(byte[] secretKey, byte[] cipherText) Recover the shared secret from a ciphertext.byte[]kemDecryptInternal(byte[] secretKey, byte[] cipherText) Recover the shared secret from a ciphertext.byte[][]kemEncrypt(byte[] publicKeyInput, byte[] randBytes) Encapsulate a shared secret, drawing the randomness from this engine.byte[][]kemEncryptInternal(byte[] publicKeyInput, byte[] randBytes) Encapsulate a shared secret against the recipient's public key.
Field Details
random
Random source for the key generation and encapsulation.indCpa
The IND-CPA core of the KEM.KyberN
KyberQ
KyberQinv
KyberSymBytes
public static final int KyberSymBytesNumber of bytes for Hashes and Seeds- See Also:
KyberPolyBytes
KyberEta2
KyberIndCpaMsgBytes
private static final int KyberIndCpaMsgBytesMessage length of the IND-CPA scheme, in bytes.- See Also:
KyberK
private final int KyberKThe k parameter of the Kyber variant.KyberPolyVecBytes
private final int KyberPolyVecBytesPolynomial vector length, in bytes.KyberPolyCompressedBytes
private final int KyberPolyCompressedBytesCompressed polynomial length, in bytes.KyberPolyVecCompressedBytes
private final int KyberPolyVecCompressedBytesCompressed polynomial vector length, in bytes.KyberEta1
private final int KyberEta1Eta1 noise parameter value.KyberIndCpaPublicKeyBytes
private final int KyberIndCpaPublicKeyBytesIND-CPA public key length, in bytes.KyberIndCpaSecretKeyBytes
private final int KyberIndCpaSecretKeyBytesIND-CPA secret key length, in bytes.KyberIndCpaBytes
private final int KyberIndCpaBytesIND-CPA scheme size, in bytes.KyberPublicKeyBytes
private final int KyberPublicKeyBytesEncapsulating key length, in bytes.KyberSecretKeyBytes
private final int KyberSecretKeyBytesDecapsulating key length, in bytes.KyberCipherTextBytes
private final int KyberCipherTextBytesCiphertext length, in bytes.CryptoBytes
private final int CryptoBytesSize named by the crypto suffix of the variant.CryptoSecretKeyBytes
private final int CryptoSecretKeyBytesSecret key length named by the crypto suffix.CryptoPublicKeyBytes
private final int CryptoPublicKeyBytesPublic key length named by the crypto suffix.CryptoCipherTextBytes
private final int CryptoCipherTextBytesCiphertext length named by the crypto suffix.sessionKeyLength
private final int sessionKeyLengthSession key length derived per variant, in bytes.symmetric
The symmetric primitives used by the scheme.
Constructor Details
MLKEMEngine
public MLKEMEngine(int k) Build the parameter set for the given security level.- Parameters:
k- the security parameter: 2, 3 or 4
Method Details
getSymmetric
The symmetric primitives bound to this parameter set.- Returns:
- the symmetric primitive set this engine was built with
getKyberEta2
public static int getKyberEta2()The eta2 noise parameter.- Returns:
- KyberEta2, the coefficient bound used when sampling messages
getKyberIndCpaMsgBytes
public static int getKyberIndCpaMsgBytes()Message length in bytes that the IND-CPA scheme accepts.- Returns:
- KyberIndCpaMsgBytes, the limit in bytes
getCryptoCipherTextBytes
public int getCryptoCipherTextBytes()Ciphertext length in bytes at the KEM layer.- Returns:
- CryptoCipherTextBytes, the length in bytes
getCryptoPublicKeyBytes
public int getCryptoPublicKeyBytes()Public key length in bytes at the KEM layer.- Returns:
- CryptoPublicKeyBytes, the length in bytes
getCryptoSecretKeyBytes
public int getCryptoSecretKeyBytes()Secret key length in bytes at the KEM layer.- Returns:
- CryptoSecretKeyBytes, the length in bytes
getCryptoBytes
public int getCryptoBytes()Total key material length in bytes at the KEM layer.- Returns:
- CryptoBytes, the combined length in bytes
getKyberCipherTextBytes
public int getKyberCipherTextBytes()Ciphertext length in bytes produced by the IND-CPA scheme.- Returns:
- KyberCipherTextBytes, the length in bytes
getKyberSecretKeyBytes
public int getKyberSecretKeyBytes()Secret key length in bytes for the IND-CPA scheme.- Returns:
- KyberSecretKeyBytes, the length in bytes
getKyberIndCpaPublicKeyBytes
public int getKyberIndCpaPublicKeyBytes()Public key length in bytes for the IND-CPA scheme.- Returns:
- KyberIndCpaPublicKeyBytes, the length in bytes
getKyberIndCpaSecretKeyBytes
public int getKyberIndCpaSecretKeyBytes()Secret key length in bytes for the IND-CPA scheme.- Returns:
- KyberIndCpaSecretKeyBytes, the length in bytes
getKyberIndCpaBytes
public int getKyberIndCpaBytes()Seed length in bytes for the IND-CPA scheme.- Returns:
- KyberIndCpaBytes, the length in bytes
getKyberPublicKeyBytes
public int getKyberPublicKeyBytes()Public key length in bytes before the KEM layer wraps it.- Returns:
- KyberPublicKeyBytes, the length in bytes
getKyberPolyCompressedBytes
public int getKyberPolyCompressedBytes()Size in bytes of one compressed polynomial.- Returns:
- KyberPolyCompressedBytes, the length in bytes
getKyberK
public int getKyberK()The security parameter: 2, 3 or 4.- Returns:
- KyberK, the level: 2, 3 or 4
getKyberPolyVecBytes
public int getKyberPolyVecBytes()Size in bytes of one uncompressed polynomial vector.- Returns:
- KyberPolyVecBytes, the length in bytes
getKyberPolyVecCompressedBytes
public int getKyberPolyVecCompressedBytes()Size in bytes of one compressed polynomial vector.- Returns:
- KyberPolyVecCompressedBytes, the length in bytes
getKyberEta1
public int getKyberEta1()The eta1 noise parameter.- Returns:
- KyberEta1, the coefficient bound used when sampling ciphertext
init
Bind the entropy source used for key generation and encapsulation.- Parameters:
random- the source to draw randomness from
generateKemKeyPair
public byte[][] generateKemKeyPair()Generate a KEM key pair, drawing both seeds from the bound entropy source.- Returns:
- the key pair components, laid out as generateKemKeyPairInternal does
generateKemKeyPairInternal
public byte[][] generateKemKeyPairInternal(byte[] d, byte[] z) Expand the two seeds into the full ML-KEM key pair.- Parameters:
d- the seed for the internal IND-CPA key pairz- the seed kept for implicit rejection- Returns:
- the key pair: encapsulated public key, hashed public key, secret key, hashed public key, z, and the concatenated seeds
kemEncryptInternal
public byte[][] kemEncryptInternal(byte[] publicKeyInput, byte[] randBytes) Encapsulate a shared secret against the recipient's public key.- Parameters:
publicKeyInput- the recipient's encapsulated public keyrandBytes- the 32 bytes of randomness the message hiding samples need- Returns:
- the ciphertext and the shared secret
kemDecryptInternal
public byte[] kemDecryptInternal(byte[] secretKey, byte[] cipherText) Recover the shared secret from a ciphertext.- Parameters:
secretKey- the recipient's secret keycipherText- the ciphertext to decapsulate- Returns:
- the shared secret
kemEncrypt
public byte[][] kemEncrypt(byte[] publicKeyInput, byte[] randBytes) Encapsulate a shared secret, drawing the randomness from this engine.- Parameters:
publicKeyInput- the recipient's encapsulated public keyrandBytes- the 32 bytes of randomness to encapsulate with- Returns:
- the ciphertext and the shared secret
kemDecrypt
public byte[] kemDecrypt(byte[] secretKey, byte[] cipherText) Recover the shared secret from a ciphertext.- Parameters:
secretKey- the recipient's secret keycipherText- the ciphertext to decapsulate- Returns:
- the shared secret
cmov
private void cmov(byte[] r, byte[] x, int xlen, boolean b) Conditionally copies x into r, constant time.- Parameters:
r- the destination arrayx- the source arrayxlen- the source length, in bytesb- true to copy
getRandomBytes
public void getRandomBytes(byte[] buf) Fill a buffer with bytes from the bound entropy source.- Parameters:
buf- the buffer to fill, sized by the caller