Class PQCKeyFactory
PQCKeyFactory is a preview API of the Java Card platform.
This class provides utilities for building PQC key objects through
buildKey(short, short), for populating those objects
with freshly generated key material using
generateKeyPair(PublicKey, PrivateKey), and for expanding seed private
keys into their full representation (and optionally deriving the public key)
with the overloaded generateKeyPair(PrivateKey, PublicKey, PrivateKey).
In particular, this class supports ML-KEM and ML-DSA keys.
Key pair generation is also available through the legacy
KeyPair API, by constructing a KeyPair instance with
KeyPair(PublicKey, PrivateKey) and invoking
KeyPair.genKeyPair(). The transaction, tear or reset, and exception
behavior is the same as described for
generateKeyPair(PublicKey, PrivateKey) for supported PQC keys.
- Since:
- preview
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final shortKey attribute flag indicating that a private key is created using its seed representation, for algorithms that define such a representation. -
Method Summary
Modifier and TypeMethodDescriptionstatic KeybuildKey(short algName, short attributes) Creates an uninitialized PQC key instance.static voidgenerateKeyPair(PrivateKey privateKeySeed, PublicKey publicKeyExpanded, PrivateKey privateKeyExpanded) Generates a PQC key pair (public and private keys) in their expanded representations from a seed, or generates either the public key or the private key individually.static voidgenerateKeyPair(PublicKey publicKey, PrivateKey privateKey) Generates a PQC key pair using the provided key container objects.
-
Field Details
-
ATTR_SEED
public static final short ATTR_SEEDKey attribute flag indicating that a private key is created using its seed representation, for algorithms that define such a representation.This attribute is only valid when combined with
KeyBuilder.ATTR_PRIVATEin calls tobuildKey(short, short).For ML-KEM and ML-DSA private keys, this attribute selects the private key seed representation. Without this attribute, an ML-KEM or ML-DSA private key is created using its expanded representation.
- See Also:
-
-
Method Details
-
buildKey
Creates an uninitialized PQC key instance.The
algNameparameter selects the PQC algorithm parameters. The supported values are:NamedParameterSpec.ML_KEM_512PREVIEWNamedParameterSpec.ML_KEM_768PREVIEWNamedParameterSpec.ML_KEM_1024PREVIEWNamedParameterSpec.ML_DSA_44PREVIEWNamedParameterSpec.ML_DSA_65PREVIEWNamedParameterSpec.ML_DSA_87PREVIEW
It enforces the following attribute constraint:
ATTR_SEEDcan be combined exclusively withKeyBuilder.ATTR_PRIVATE.- Parameters:
algName- the PQC algorithm parameter nameattributes- the key attributes. Use bitwiseORto select relevant attributes:- type of memory for key data storage:
JCSystem.MEMORY_TYPE_*constants, for exampleMEMORY_TYPE_PERSISTENT. - asymmetric key type:
KeyBuilder.ATTR_PUBLICcreates a public key andKeyBuilder.ATTR_PRIVATEcreates a private key. The created key will respectively implement thePublicKeyorPrivateKeyinterface. - private key material representation:
ATTR_SEEDto create a private key with its seed representation, for algorithms that define such a representation, when combined withKeyBuilder.ATTR_PRIVATE.
- type of memory for key data storage:
- Returns:
- a key object implementing a supported PQC key interface; for ML-KEM and ML-DSA,
one of the interfaces
MLKEMPrivateKeyPREVIEW,MLKEMPublicKeyPREVIEW,MLDSAPrivateKeyPREVIEW,MLDSAPublicKeyPREVIEW dictated byattributes - Throws:
CryptoException- with the following reason codes:CryptoException.NO_SUCH_ALGORITHMifalgNameor the requested combination is unsupportedCryptoException.ILLEGAL_VALUEifattributesare incorrect.
-
generateKeyPair
public static void generateKeyPair(PublicKey publicKey, PrivateKey privateKey) throws CryptoException Generates a PQC key pair using the provided key container objects.The supplied objects must have been created via
buildKey(short, short)for the same PQC parameter set. An internal secure random number generator is used during new key pair generation.- For ML-KEM keys, this method corresponds to Algorithm 19
ML-KEM.KeyGen()of FIPS 203, "Chapter 7.1 ML-KEM Key Generation". - For ML-DSA keys, this method corresponds to Algorithm 1
ML-DSA.KeyGen()of FIPS 204, "Chapter 5.1 ML-DSA Key Generation".
For ML-KEM and ML-DSA, the private key may carry the ML-KEM or ML-DSA private key seed representation or the ML-KEM or ML-DSA private key expanded representation.
On success, the key objects
publicKeyandprivateKeybecome initialized (callingisInitialized()on those keys returnstrue).Transaction and atomicity
This method never leaves a key in a state where
isInitialized()returnstruewhile the key value is partially updated or undefined.The private key is written before the public key. Conceptually, this method executes the following steps in this order:
- clear
publicKey, - clear
privateKey, - generate the new key pair values,
- set
privateKeywith the new private value, - set
publicKeywith the new public value.
If the application requires an atomic all-or-nothing update, including preserving both previous initialized destination keys instead of allowing an uninitialized outcome on failure, this method should be called inside a transaction.
When a transaction is in progress:
- The update of both destination key values and their initialized states participate in the transaction and are committed or rolled back all together.
- This transactional update is subject to commit capacity limitations.
If the commit capacity is exceeded,
TransactionExceptionis thrown. - Any exception thrown by this method may leave both destination keys unchanged, may make both destination keys uninitialized, or may result in the destination private key being initialized with the new value while the destination public key is unchanged or uninitialized. It cannot result in both destination keys being initialized with the new values. If this method throws any exception while a transaction is in progress, the caller should abort the transaction before using the keys.
When no transaction is in progress:
- A tear or reset during this method may leave both destination keys unchanged, may make both destination keys uninitialized, may result in both destination keys being initialized with the new values, or may result in the destination private key being initialized with the new value while the destination public key is unchanged or uninitialized.
- Any exception thrown by this method may leave both destination keys unchanged, may make both destination keys uninitialized, or may result in the destination private key being initialized with the new value while the destination public key is unchanged or uninitialized. It cannot result in both destination keys being initialized with the new values.
- Parameters:
publicKey- the public key container to receive the generated public value; must not benullprivateKey- the private key container to receive the generated private value; must not benull- Throws:
NullPointerException- ifpublicKeyorprivateKeyisnullCryptoException- with the following reason codes:CryptoException.ILLEGAL_VALUEif:- the key objects do not share the same parameter set.
CryptoException.NO_SUCH_ALGORITHMif key pair generation is not supported by the implementation.
- For ML-KEM keys, this method corresponds to Algorithm 19
-
generateKeyPair
public static void generateKeyPair(PrivateKey privateKeySeed, PublicKey publicKeyExpanded, PrivateKey privateKeyExpanded) throws CryptoException Generates a PQC key pair (public and private keys) in their expanded representations from a seed, or generates either the public key or the private key individually. This involves performing an expansion operation from the provided seed.The supplied objects must have been created via
buildKey(short, short)for the same PQC parameter set.For ML-KEM and ML-DSA keys, implementations read the private key seed representation stored in
privateKeySeed(an object created withKeyBuilder.ATTR_PRIVATEcombined withATTR_SEED) and perform expansion into the corresponding ML-KEM or ML-DSA private key expanded representation and/or public key. TheprivateKeySeedkey is an input only and is not modified by this method.- For ML-KEM keys, this method corresponds to Algorithm 16
ML-KEM.KeyGen_internal(d, z)of FIPS 203, "Chapter 6.1 Internal Key Generation". - For ML-DSA keys, this method corresponds to Algorithm 6
ML-DSA.KeyGen_internal(ξ)of FIPS 204, "Chapter 6.1 ML-DSA Key Generation (Internal)".
Only the following argument combinations are accepted; any other combination results in
CryptoException.ILLEGAL_VALUE:(privateKeySeed, publicKeyExpanded, privateKeyExpanded)- both expanded private key and public key are populated(privateKeySeed, null, privateKeyExpanded)- only the expanded private key is populated(privateKeySeed, publicKeyExpanded, null)- only the public key is populated
On success, each non-
nulldestination key becomes initialized (callingisInitialized()on that key returnstrue).Transaction and atomicity
This method never leaves a key in a state where
isInitialized()returnstruewhile the key value is partially updated or undefined.The private key is written before the public key. Conceptually, this method executes the following steps in this order:
- clear
publicKeyExpanded, when provided, - clear
privateKeyExpanded, when provided, - generate the requested new expanded key values
- set
privateKeyExpandedwith the new private value, when provided, - set
publicKeyExpandedwith the new public value, when provided.
If the application requires an atomic all-or-nothing update, including preserving the previous initialized provided destination keys instead of allowing an uninitialized outcome on failure, this method should be called inside a transaction.
When a transaction is in progress:
- The update of the provided destination key values and their initialized states participate in the transaction and are committed or rolled back all together.
- This transactional update is subject to commit capacity limitations.
If the commit capacity is exceeded,
TransactionExceptionis thrown. - Any exception thrown by this method may leave all provided
destination keys unchanged, may make all provided destination keys
uninitialized, or may result in
privateKeyExpandedbeing initialized with the new value whilepublicKeyExpandedis unchanged or uninitialized. It cannot result in all provided destination keys being initialized with the new values. If this method throws any exception while a transaction is in progress, the caller should abort the transaction before using the keys. - If only one destination key is provided, references to all provided destination keys apply to that single destination key, and states involving the other destination key do not apply.
When no transaction is in progress:
- A tear or reset during this method may leave all provided destination
keys unchanged, may make all provided destination keys uninitialized, may
result in all provided destination keys being initialized with the new
values, or may result in
privateKeyExpandedbeing initialized with the new value whilepublicKeyExpandedis unchanged or uninitialized. - Any exception thrown by this method may leave all provided
destination keys unchanged, may make all provided destination keys
uninitialized, or may result in
privateKeyExpandedbeing initialized with the new value whilepublicKeyExpandedis unchanged or uninitialized. It cannot result in all provided destination keys being initialized with the new values. - If only one destination key is provided, references to all provided destination keys apply to that single destination key, and states involving the other destination key do not apply.
- Parameters:
privateKeySeed- the input private key object containing the seed; it is read and is not modified by this method; must not benullpublicKeyExpanded- the public key object that will receive the derived public key; may benullprivateKeyExpanded- the private key object that will receive the expanded private key; may benull- Throws:
NullPointerException- ifprivateKeySeedisnullCryptoException- with the following reason codes:CryptoException.UNINITIALIZED_KEYifprivateKeySeedis not initialized.CryptoException.ILLEGAL_VALUEif the provided key objects do not match one of the supported combinations, do not share the same parameter set, or have incompatible formats.CryptoException.NO_SUCH_ALGORITHMif expansion from seed is not supported by the implementation.
- For ML-KEM keys, this method corresponds to Algorithm 16
-
PQCKeyFactorywhen preview features are enabled.