Interface EncodableKey
- All Superinterfaces:
Key
- All Known Subinterfaces:
MLDSAPrivateKeyPREVIEW, MLDSAPublicKeyPREVIEW, MLKEMPrivateKeyPREVIEW, MLKEMPublicKeyPREVIEW
EncodableKey is a preview API of the Java Card platform.
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:
pkforMLDSAPublicKeyPREVIEWskforMLDSAPrivateKeyPREVIEW with its expanded representationξforMLDSAPrivateKeyPREVIEW with its seed representation
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:
ekforMLKEMPublicKeyPREVIEWdkforMLKEMPrivateKeyPREVIEW with its expanded representationd||z, i.e. the concatenation of the two 32-byte seed values forMLKEMPrivateKeyPREVIEW with its seed representation
- Since:
- preview
-
Method Summary
Modifier and TypeMethodDescriptionvoidclearKey()Clears the key and sets its initialized state to false.shortgetEncoded(byte[] value, short offset) Gets the key in its primary encoding format.shortReturns the length, in bytes, of the primary encoded representation, corresponding to the buffer length expected bygetEncoded(byte[], short)andsetEncoded(byte[], short, short).Returns the parameter specification associated with this key instance.voidsetEncoded(byte[] value, short offset, short length) Sets the key from its primary encoding format.
-
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()returnsfalse.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,
TransactionExceptionis 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.
-
getEncodingLength
short getEncodingLength()Returns the length, in bytes, of the primary encoded representation, corresponding to the buffer length expected bygetEncoded(byte[], short)andsetEncoded(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 intovaluestarting at the specifiedoffset. The number of bytes copied equalsgetEncodingLength()and the returned value isoffset + getEncodingLength().- Parameters:
value- the destination buffer for the encoded keyoffset- the starting offset invalue- Returns:
- offset + number of bytes copied
-
setEncoded
Sets the key from its primary encoding format. Initializes this key using the provided encoded representation. Thelengthparameter must matchgetEncodingLength().On success, the key object becomes initialized (
isInitialized()returnstrue).Transaction and atomicity
This method never leaves the key in a state where
isInitialized()returnstruewhile 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,
TransactionExceptionis 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 keyoffset- the offset invaluewhere the key encoding startslength- the length of the encoded key- Throws:
CryptoException- with the following reason codes:- CryptoException.ILLEGAL_VALUE if any of
the following conditions are met:
lengthis not compatible with the algorithm parameters.- the encoded key is invalid because key consistency checks, if performed by the implementation, fail.
- CryptoException.ILLEGAL_VALUE if any of
the following conditions are met:
-
EncodableKeywhen preview features are enabled.