Class KEM
KEM is a preview API of the Java Card platform.
KEM class provides methods for obtaining objects that implement Key Encapsulation Mechanism (KEM) operations,
such as DHKEM (RFC 9180) and ML-KEM (FIPS 203).
KEMs are used to securely transport symmetric key material (shared secrets) between a sender and a receiver using asymmetric cryptography.
In a typical usage, the sender calls the encapsulate method with the receiver’s public key to generate a key encapsulation message (ciphertext) along with a shared secret. The receiver then calls the decapsulate method with the matching private key to recover the shared secret from the key encapsulation message.
The main steps for using a KEM instance are:
Instance Creation
Create an instance using the factory methodgetInstance(short, AlgorithmParameterSpec, short).- Depending on the selected mode (
KEM.MODE_ENCAPSULATIONorKEM.MODE_DECAPSULATION), the returned instance implements theKEM.EncapsulatorPREVIEW orKEM.DecapsulatorPREVIEW interface, respectively. - Additionaly,
KEM.MODE_DECAPSULATIONcan be combined withKEM.MODE_EXPANDto return an instance supporting key expansion.
- Depending on the selected mode (
Key Encapsulation or Decapsulation
Use the appropriate encapsulate or decapsulate method, providing all required keys and parameters in the method call.
- Since:
- preview
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacePreview.static interface -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final shortDH Key Encapsulation Mechanism (DHKEM), as defined in RFC 9180 chapters 4.1 and 7.1.static final shortModule-Lattice-Based Key Encapsulation Mechanism (ML-KEM) as defined in FIPS 203.static final shortSpecifies decapsulation mode for use as themodeparameter in thegetInstance(short, AlgorithmParameterSpec, short)factory method.static final shortSpecifies encapsulation mode for use as themodeparameter in thegetInstance(short, AlgorithmParameterSpec, short)factory method.static final shortSpecifies that the KEM instance supports key expansion. -
Method Summary
Modifier and TypeMethodDescriptiongetInstance(short algorithm, AlgorithmParameterSpec params, short mode) Obtains aKEMobject instance for the selected algorithm, algorithm parameters, and mode.
-
Field Details
-
ALG_DHKEM
public static final short ALG_DHKEMDH Key Encapsulation Mechanism (DHKEM), as defined in RFC 9180 chapters 4.1 and 7.1.This constant is used as the
algorithmparameter for the factory methodgetInstance(short, AlgorithmParameterSpec, short)when requesting a DHKEM instance.- See Also:
-
ALG_ML_KEM
public static final short ALG_ML_KEMModule-Lattice-Based Key Encapsulation Mechanism (ML-KEM) as defined in FIPS 203.This constant is used as the
algorithmparameter for the factory methodgetInstance(short, AlgorithmParameterSpec, short)when requesting an ML-KEM instance.- See Also:
-
MODE_ENCAPSULATION
public static final short MODE_ENCAPSULATIONSpecifies encapsulation mode for use as themodeparameter in thegetInstance(short, AlgorithmParameterSpec, short)factory method.In this mode, the returned
KEMinstance will implement theKEM.EncapsulatorPREVIEW interface, allowing generation of key encapsulation messages and corresponding shared secrets using a recipient’s public key.- See Also:
-
MODE_DECAPSULATION
public static final short MODE_DECAPSULATIONSpecifies decapsulation mode for use as themodeparameter in thegetInstance(short, AlgorithmParameterSpec, short)factory method.In this mode, the returned
KEMinstance will implement theKEM.DecapsulatorPREVIEW interface, enabling recovery of the shared secret from a key encapsulation message using the private key.- See Also:
-
MODE_EXPAND
public static final short MODE_EXPANDSpecifies that the KEM instance supports key expansion. This mode applies only when combined (using bitwise OR) withMODE_DECAPSULATIONas themodeparameter ingetInstance(short, AlgorithmParameterSpec, short).- See Also:
-
-
Method Details
-
getInstance
Obtains aKEMobject instance for the selected algorithm, algorithm parameters, and mode.The returned instance is a permanent JCRE Entry Point Object, which may either be pre-allocated by the implementation or allocated at the time of this call.
For ML-KEM algorithm (e.g., when
algorithmisALG_ML_KEM), theparamsargument must match one of the predefinedNamedParameterSpecinstances:NamedParameterSpec.ML_KEM_512PREVIEWNamedParameterSpec.ML_KEM_768PREVIEWNamedParameterSpec.ML_KEM_1024PREVIEW
For DHKEM algorithm (e.g., when
algorithmisALG_DHKEM), theparamsargument must match one of the predefinedNamedParameterSpecinstances:NamedParameterSpec.SECP256R1NamedParameterSpec.SECP384R1NamedParameterSpec.SECP521R1NamedParameterSpec.X25519NamedParameterSpec.X448
The returned
KEMinstance will implement:KEM.EncapsulatorPREVIEW ifMODE_ENCAPSULATIONis specified.KEM.DecapsulatorPREVIEW ifMODE_DECAPSULATIONis specified.- Both interfaces if both modes are specified (using a bitwise OR).
MODE_EXPANDcan only be set in combination withMODE_DECAPSULATION. In this case, the returnedKEMinstance supports decapsulation with either expanded or seed representation of an ML-KEM private key. If the private key is provided in seed representation, it is expanded automatically before decapsulation.Note: This method reserves or allocates the memory and resources needed for subsequent key expansion, decapsulation and encapsulation operations. Enabling
MODE_EXPANDmay require additional memory/resources to support private-key expansion before decapsulation.- Parameters:
algorithm- the desired KEM algorithm (seeALG_*constants).params- algorithm-specific parameters.mode- one or a combination (bitwise OR) of:- Returns:
- a
KEMobject instance for the requested algorithm, mode, and resource bounds. - Throws:
CryptoException- with the following reason codes:-
CryptoException.NO_SUCH_ALGORITHMif the requestedalgorithm,mode, or algorithm-specific parameters are not supported. -
CryptoException.ILLEGAL_VALUEif the mode is invalid, represents an invalid combination of mode flags, or is not supported by the specified algorithm.
-
SystemException- with the following reason codes:SystemException.NO_RESOURCEif there is not enough resources to allocate the instance
-
KEMwhen preview features are enabled.