Interface KEM.Encapsulator
Encapsulator is a preview API of the Java Card platform.
Encapsulator interface is implemented by KEMPREVIEW instances that support the key encapsulation
operation of Key Encapsulation Mechanism (KEM) algorithms.
Instances supporting this interface are obtained by creating a KEMPREVIEW object in encapsulation mode
(see KEM.getInstance(short, AlgorithmParameterSpec, short)PREVIEW factory method).
The encapsulation operation enables a sender to securely generate a secret key and a corresponding key encapsulation message, using the recipient's public key. The encapsulation message is then transmitted to the receiver, who can use the corresponding decapsulation operation to recover the same secret key.
- Since:
- preview
-
Method Summary
Modifier and TypeMethodDescriptionshortReturns the size of the key encapsulation message (ciphertext).shortencapsulate(byte[] encKey, short encKeyOffset, short encKeyLength, byte[] cipherText, short cipherTextOffset, byte[] sharedSecret, short sharedSecretOffset) Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret, as specified by the KEM algorithm and parameters.shortencapsulate(byte[] encKey, short encKeyOffset, short encKeyLength, byte[] cipherText, short cipherTextOffset, SecretKey sharedSecret, short from, short to) Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret key, as specified by the KEM algorithm and parameters.shortencapsulate(PublicKey publicKey, byte[] cipherText, short cipherTextOffset, byte[] sharedSecret, short sharedSecretOffset) Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret, as specified by the KEM algorithm and parameters.shortencapsulate(PublicKey publicKey, byte[] cipherText, short cipherTextOffset, SecretKey sharedSecret, short from, short to) Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret key, as specified by the KEM algorithm and parameters.shortReturns the size of the shared secret.
-
Method Details
-
encapsulate
short encapsulate(byte[] encKey, short encKeyOffset, short encKeyLength, byte[] cipherText, short cipherTextOffset, byte[] sharedSecret, short sharedSecretOffset) throws CryptoException Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret, as specified by the KEM algorithm and parameters.The (public) encapsulation key is read from the
encKeybuffer starting atencKeyOffset. The encoded encapsulation public key must use the same format as the one returned by the equivalent public key object:EncodableKey.getEncoded()for ML-KEM public keys, orXECPublicKey.getEncoded()orECPublicKey.getW(), as applicable, for elliptic curve public keys.The encapsulation message is written to the
cipherTextoutput buffer (starting atcipherTextOffset), and the shared secret bytes are written to thesharedSecretoutput buffer (starting atsharedSecretOffset).In addition to returning the number of bytes written to the
cipherTextbuffer, this method updates a global state register for additional verification using assertion methods from theSensitiveResultclass, if this class is available on the platform.- Parameters:
encKey- buffer containing the encoded encapsulation public keyencKeyOffset- the offset where the encapsulation key beginsencKeyLength- the encapsulation key lengthcipherText- buffer for the key encapsulation message outputcipherTextOffset- offset in thecipherTextbuffer where the output beginssharedSecret- buffer for the shared secret outputsharedSecretOffset- offset in thesharedSecretbuffer where the output begins- Returns:
- number of bytes written to the
cipherTextoutput buffer - Throws:
CryptoException- with the following reason codes:CryptoException.ILLEGAL_VALUEif any of the following conditions are met:encKeyLengthis not compatible with the algorithm parameters.- the encoded encapsulation public key is invalid because key consistency checks, if performed by the implementation, fail.
-
encapsulate
short encapsulate(byte[] encKey, short encKeyOffset, short encKeyLength, byte[] cipherText, short cipherTextOffset, SecretKey sharedSecret, short from, short to) throws CryptoException Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret key, as specified by the KEM algorithm and parameters.The (public) encapsulation key is read from the
encKeybuffer starting atencKeyOffset. The encoded encapsulation public key must use the same format as the one returned by the equivalent public key object:EncodableKey.getEncoded()for ML-KEM public keys, orXECPublicKey.getEncoded()orECPublicKey.getW(), as applicable, for elliptic curve public keys.The encapsulation message is written to the
cipherTextoutput buffer (starting atcipherTextOffset). The generated shared secret is set into the providedSecretKeyobject. Only the portion of the generated secret in the specified range (from, inclusive, toto, exclusive) is used to set the value of theSecretKeyobject.In addition to returning the number of bytes written to the
cipherTextbuffer, this method updates a global state register for additional verification using assertion methods from theSensitiveResultclass, if this class is available on the platform.- Parameters:
encKey- buffer containing the encoded encapsulation public keyencKeyOffset- the offset where the encapsulation key beginsencKeyLength- the encapsulation key lengthcipherText- buffer for the key encapsulation message outputcipherTextOffset- offset in thecipherTextbuffer where the output beginssharedSecret- theSecretKeyobject to be initialized with the generated shared secretfrom- initial index (inclusive) of the generated secret to use for theSecretKeyto- final index (exclusive) of the generated secret to use for theSecretKey- Returns:
- number of bytes written to the
cipherTextoutput buffer - Throws:
CryptoException- with the following reason codes:CryptoException.ILLEGAL_VALUEif any of the following conditions are met:encKeyLengthis not compatible with the algorithm parameters.- the encoded encapsulation public key is invalid because key consistency checks, if performed by the implementation, fail.
from < 0,from > to, orto > sharedSecretSize().to - fromdoes not equal the number of bytes required to initialize theSecretKey.
-
encapsulate
short encapsulate(PublicKey publicKey, byte[] cipherText, short cipherTextOffset, byte[] sharedSecret, short sharedSecretOffset) throws CryptoException Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret, as specified by the KEM algorithm and parameters.The encapsulation message is written to the
cipherTextoutput buffer (starting atcipherTextOffset), and the shared secret bytes are written to thesharedSecretoutput buffer (starting atsharedSecretOffset).In addition to returning the number of bytes written to the
cipherTextbuffer, this method updates a global state register for additional verification using assertion methods from theSensitiveResultclass, if this class is available on the platform.- Parameters:
publicKey- the public key used for encapsulationcipherText- buffer for the key encapsulation message outputcipherTextOffset- offset in thecipherTextbuffer where the output beginssharedSecret- buffer for the shared secret outputsharedSecretOffset- offset in thesharedSecretbuffer where the output begins- Returns:
- number of bytes written to the
cipherTextoutput buffer - Throws:
CryptoException- with the following reason codes:CryptoException.UNINITIALIZED_KEYif the public key is not initialized.CryptoException.ILLEGAL_VALUEif the encapsulation public key is invalid because key consistency checks, if performed by the implementation, fail
-
encapsulate
short encapsulate(PublicKey publicKey, byte[] cipherText, short cipherTextOffset, SecretKey sharedSecret, short from, short to) throws CryptoException Performs the key encapsulation operation, generating both a key encapsulation message (ciphertext to be sent to the recipient) and the corresponding shared secret key, as specified by the KEM algorithm and parameters.The encapsulation message is written to the
cipherTextoutput buffer (starting atcipherTextOffset). The generated shared secret is set into the providedSecretKeyobject. Only the portion of the generated secret in the specified range (from, inclusive, toto, exclusive) is used to set the value of theSecretKeyobject.In addition to returning the number of bytes written to the
cipherTextbuffer, this method updates a global state register for additional verification using assertion methods from theSensitiveResultclass, if this class is available on the platform.- Parameters:
publicKey- the public key used for encapsulationcipherText- buffer for the key encapsulation message outputcipherTextOffset- offset in thecipherTextbuffer where the output beginssharedSecret- theSecretKeyobject to be initialized with the generated shared secretfrom- initial index (inclusive) of the generated secret to use for theSecretKeyto- final index (exclusive) of the generated secret to use for theSecretKey- Returns:
- number of bytes written to the
cipherTextoutput buffer - Throws:
CryptoException- with the following reason codes:CryptoException.UNINITIALIZED_KEYif the public key is not initialized.CryptoException.ILLEGAL_VALUEif any of the following conditions are met:- the encapsulation public key is invalid because key consistency checks, if performed by the implementation, fail
from < 0,from > to, orto > sharedSecretSize().to - fromdoes not equal the number of bytes required to initialize theSecretKey.
-
cipherTextSize
short cipherTextSize()Returns the size of the key encapsulation message (ciphertext).This method can be called to find out the length of the ciphertext before
encapsulateis called.- Returns:
- the size in bytes of the key ciphertext
-
Encapsulatorwhen preview features are enabled.