Class PQCKeyFactory

java.lang.Object
javacardx.security.pqc.PQCKeyFactory

public final class PQCKeyFactory extends Object
PQCKeyFactory is a preview API of the Java Card platform.
Programs can only use PQCKeyFactory when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java Card platform.
Factory for creating post-quantum cryptography keys.

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

    Fields
    Modifier and Type
    Field
    Description
    static final short
    Key attribute flag indicating that a private key is created using its seed representation, for algorithms that define such a representation.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Key
    buildKey(short algName, short attributes)
    Creates an uninitialized PQC key instance.
    static void
    generateKeyPair(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 void
    generateKeyPair(PublicKey publicKey, PrivateKey privateKey)
    Generates a PQC key pair using the provided key container objects.

    Methods inherited from class Object

    equals
    Modifier and Type
    Method
    Description
    boolean
    Compares two Objects for equality.
  • Field Details

    • ATTR_SEED

      public static final short ATTR_SEED
      Key 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_PRIVATE in calls to buildKey(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

      public static Key buildKey(short algName, short attributes) throws CryptoException
      Creates an uninitialized PQC key instance.

      The algName parameter selects the PQC algorithm parameters. The supported values are:

      It enforces the following attribute constraint: ATTR_SEED can be combined exclusively with KeyBuilder.ATTR_PRIVATE.

      Parameters:
      algName - the PQC algorithm parameter name
      attributes - the key attributes. Use bitwise OR to select relevant attributes:
      • type of memory for key data storage: JCSystem.MEMORY_TYPE_* constants, for example MEMORY_TYPE_PERSISTENT.
      • asymmetric key type: KeyBuilder.ATTR_PUBLIC creates a public key and KeyBuilder.ATTR_PRIVATE creates a private key. The created key will respectively implement the PublicKey or PrivateKey interface.
      • private key material representation: ATTR_SEED to create a private key with its seed representation, for algorithms that define such a representation, when combined with KeyBuilder.ATTR_PRIVATE.
      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 by attributes
      Throws:
      CryptoException - with the following reason codes:
    • 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 publicKey and privateKey become initialized (calling isInitialized() on those keys returns true).

      Transaction and atomicity

      This method never leaves a key in a state where isInitialized() returns true while 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:

      1. clear publicKey,
      2. clear privateKey,
      3. generate the new key pair values,
      4. set privateKey with the new private value,
      5. set publicKey with 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, TransactionException is 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 be null
      privateKey - the private key container to receive the generated private value; must not be null
      Throws:
      NullPointerException - if publicKey or privateKey is null
      CryptoException - with the following reason codes:
    • 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 with KeyBuilder.ATTR_PRIVATE combined with ATTR_SEED) and perform expansion into the corresponding ML-KEM or ML-DSA private key expanded representation and/or public key. The privateKeySeed key 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
      Each provided destination key must reference the same parameter set as the seed and must be compatible with the requested representation.

      On success, each non-null destination key becomes initialized (calling isInitialized() on that key returns true).

      Transaction and atomicity

      This method never leaves a key in a state where isInitialized() returns true while 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:

      1. clear publicKeyExpanded, when provided,
      2. clear privateKeyExpanded, when provided,
      3. generate the requested new expanded key values
      4. set privateKeyExpanded with the new private value, when provided,
      5. set publicKeyExpanded with 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, TransactionException is 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 privateKeyExpanded being initialized with the new value while publicKeyExpanded is 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 privateKeyExpanded being initialized with the new value while publicKeyExpanded is 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 privateKeyExpanded being initialized with the new value while publicKeyExpanded is 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 be null
      publicKeyExpanded - the public key object that will receive the derived public key; may be null
      privateKeyExpanded - the private key object that will receive the expanded private key; may be null
      Throws:
      NullPointerException - if privateKeySeed is null
      CryptoException - with the following reason codes: