Interface EncodableKey

All Superinterfaces:
Key
All Known Subinterfaces:
MLDSAPrivateKeyPREVIEW, MLDSAPublicKeyPREVIEW, MLKEMPrivateKeyPREVIEW, MLKEMPublicKeyPREVIEW

public interface EncodableKey extends Key
EncodableKey is a preview API of the Java Card platform.
Programs can only use EncodableKey when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java Card platform.
The EncodableKey interface defines the common behaviors of post-quantum cryptography keys whose encoded representation can be queried or updated directly.

Implementations expose a length-stable encoding format associated with a specific NamedParameterSpec identified when the key instance is created. The key is initialized and ready for use once the encoded form has been set.

The memory type of an EncodableKey can be retrieved by calling KeyBuilder.getMemoryType(Key) for the key instance.

This interface is used in particular by ML-KEM and ML-DSA keys. The primary encoding format for ML-KEM and ML-DSA private keys, with either expanded or seed representation, and for ML-KEM and ML-DSA public keys, is the format defined by the corresponding constants described in NamedParameterSpec.

The primary encoding of an ML-DSA key used by setEncoded() and returned by getEncoded() must conform to FIPS 204 specifications in "Chapter 5.1 ML-DSA Key Generation". It is:

The primary encoding of an ML-KEM key used by setEncoded() and returned by getEncoded() must conform to FIPS 203 specifications in "Chapter 7.1 ML-KEM Key Generation". It is:

Since:
preview
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the key and sets its initialized state to false.
    short
    getEncoded(byte[] value, short offset)
    Gets the key in its primary encoding format.
    short
    Returns the length, in bytes, of the primary encoded representation, corresponding to the buffer length expected by getEncoded(byte[], short) and setEncoded(byte[], short, short).
    Returns the parameter specification associated with this key instance.
    void
    setEncoded(byte[] value, short offset, short length)
    Sets the key from its primary encoding format.

    Methods inherited from interface Key

    getSize, getType, isInitialized
    Modifier and Type
    Method
    Description
    short
    Returns the key size in number of bits.
    byte
    Returns the key interface type.
    boolean
    Reports the initialized state of the key.
  • Method Details

    • getParams

      NamedParameterSpec getParams()
      Returns the parameter specification associated with this key instance.
      Returns:
      the parameter specification used when the key was created
    • clearKey

      void clearKey()
      Clears the key and sets its initialized state to false.

      After this method completes successfully, Key.isInitialized() returns false.

      Transaction and atomicity

      When a transaction is in progress:

      • The changes made by this method participate in the transaction and are committed or rolled back with the transaction.
      • If the transaction is committed, the key becomes uninitialized.
      • If the transaction is aborted, the key state is restored to the state it had before this method was called. If the key was initialized before the call, it is restored as initialized and remains usable.
      • This transactional update is subject to commit capacity limitations. If the commit capacity is exceeded, TransactionException is thrown.
      • If this method throws an exception while a transaction is in progress, the caller should abort the transaction before using the key.

      When no transaction is in progress:

      • After this method completes successfully, the key is uninitialized.
      • A tear or reset during this method may leave the key unchanged or make it uninitialized.

      Specified by:
      clearKey in interface Key
    • getEncodingLength

      short getEncodingLength()
      Returns the length, in bytes, of the primary encoded representation, corresponding to the buffer length expected by getEncoded(byte[], short) and setEncoded(byte[], short, short).
      Returns:
      the encoding length
    • getEncoded

      short getEncoded(byte[] value, short offset)
      Gets the key in its primary encoding format. Copies the encoded form of this key into value starting at the specified offset. The number of bytes copied equals getEncodingLength() and the returned value is offset + getEncodingLength().
      Parameters:
      value - the destination buffer for the encoded key
      offset - the starting offset in value
      Returns:
      offset + number of bytes copied
    • setEncoded

      void setEncoded(byte[] value, short offset, short length) throws CryptoException
      Sets the key from its primary encoding format. Initializes this key using the provided encoded representation. The length parameter must match getEncodingLength().

      On success, the key object becomes initialized (isInitialized() returns true).

      Transaction and atomicity

      This method never leaves the key in a state where isInitialized() returns true while the key value is partially updated or undefined.

      If the application requires an atomic (all-or-nothing) update, including preserving the previous initialized key 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 key value and its initialized state participate in the transaction and are committed or rolled back 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 cannot result in the key being initialized with the new value. If the key was uninitialized before the call, it remains uninitialized. If this method throws any exception while a transaction is in progress, the caller should abort the transaction before using the key.

      When no transaction is in progress:

      • A tear or reset during this method may leave the key unchanged, may make it uninitialized, or may result in the key being initialized with the new value if it occurs after the new value has been fully written.
      • Any exception thrown by this method cannot result in the key being initialized with the new value. If the key was uninitialized before the call, it remains uninitialized.

      Parameters:
      value - the buffer containing the encoded key
      offset - the offset in value where the key encoding starts
      length - the length of the encoded key
      Throws:
      CryptoException - with the following reason codes:
      • CryptoException.ILLEGAL_VALUE if any of the following conditions are met:
        • length is not compatible with the algorithm parameters.
        • the encoded key is invalid because key consistency checks, if performed by the implementation, fail.