Interface KEM.Encapsulator

Enclosing class:
KEMPREVIEW

public static interface KEM.Encapsulator
Encapsulator is a preview API of the Java Card platform.
Programs can only use Encapsulator 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 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 Type
    Method
    Description
    short
    Returns the size of the key encapsulation message (ciphertext).
    short
    encapsulate(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.
    short
    encapsulate(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.
    short
    encapsulate(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.
    short
    encapsulate(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.
    short
    Returns 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 encKey buffer starting at encKeyOffset. 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, or XECPublicKey.getEncoded() or ECPublicKey.getW(), as applicable, for elliptic curve public keys.

      The encapsulation message is written to the cipherText output buffer (starting at cipherTextOffset), and the shared secret bytes are written to the sharedSecret output buffer (starting at sharedSecretOffset).

      In addition to returning the number of bytes written to the cipherText buffer, this method updates a global state register for additional verification using assertion methods from the SensitiveResult class, if this class is available on the platform.

      Parameters:
      encKey - buffer containing the encoded encapsulation public key
      encKeyOffset - the offset where the encapsulation key begins
      encKeyLength - the encapsulation key length
      cipherText - buffer for the key encapsulation message output
      cipherTextOffset - offset in the cipherText buffer where the output begins
      sharedSecret - buffer for the shared secret output
      sharedSecretOffset - offset in the sharedSecret buffer where the output begins
      Returns:
      number of bytes written to the cipherText output buffer
      Throws:
      CryptoException - with the following reason codes:
      • CryptoException.ILLEGAL_VALUE if any of the following conditions are met:
        • encKeyLength is 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 encKey buffer starting at encKeyOffset. 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, or XECPublicKey.getEncoded() or ECPublicKey.getW(), as applicable, for elliptic curve public keys.

      The encapsulation message is written to the cipherText output buffer (starting at cipherTextOffset). The generated shared secret is set into the provided SecretKey object. Only the portion of the generated secret in the specified range (from, inclusive, to to, exclusive) is used to set the value of the SecretKey object.

      In addition to returning the number of bytes written to the cipherText buffer, this method updates a global state register for additional verification using assertion methods from the SensitiveResult class, if this class is available on the platform.

      Parameters:
      encKey - buffer containing the encoded encapsulation public key
      encKeyOffset - the offset where the encapsulation key begins
      encKeyLength - the encapsulation key length
      cipherText - buffer for the key encapsulation message output
      cipherTextOffset - offset in the cipherText buffer where the output begins
      sharedSecret - the SecretKey object to be initialized with the generated shared secret
      from - initial index (inclusive) of the generated secret to use for the SecretKey
      to - final index (exclusive) of the generated secret to use for the SecretKey
      Returns:
      number of bytes written to the cipherText output buffer
      Throws:
      CryptoException - with the following reason codes:
      • CryptoException.ILLEGAL_VALUE if any of the following conditions are met:
        • encKeyLength is 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, or to > sharedSecretSize().
        • to - from does not equal the number of bytes required to initialize the SecretKey.
    • 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 cipherText output buffer (starting at cipherTextOffset), and the shared secret bytes are written to the sharedSecret output buffer (starting at sharedSecretOffset).

      In addition to returning the number of bytes written to the cipherText buffer, this method updates a global state register for additional verification using assertion methods from the SensitiveResult class, if this class is available on the platform.

      Parameters:
      publicKey - the public key used for encapsulation
      cipherText - buffer for the key encapsulation message output
      cipherTextOffset - offset in the cipherText buffer where the output begins
      sharedSecret - buffer for the shared secret output
      sharedSecretOffset - offset in the sharedSecret buffer where the output begins
      Returns:
      number of bytes written to the cipherText output buffer
      Throws:
      CryptoException - with the following reason codes:
      • CryptoException.UNINITIALIZED_KEY if the public key is not initialized.
      • CryptoException.ILLEGAL_VALUE if 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 cipherText output buffer (starting at cipherTextOffset). The generated shared secret is set into the provided SecretKey object. Only the portion of the generated secret in the specified range (from, inclusive, to to, exclusive) is used to set the value of the SecretKey object.

      In addition to returning the number of bytes written to the cipherText buffer, this method updates a global state register for additional verification using assertion methods from the SensitiveResult class, if this class is available on the platform.

      Parameters:
      publicKey - the public key used for encapsulation
      cipherText - buffer for the key encapsulation message output
      cipherTextOffset - offset in the cipherText buffer where the output begins
      sharedSecret - the SecretKey object to be initialized with the generated shared secret
      from - initial index (inclusive) of the generated secret to use for the SecretKey
      to - final index (exclusive) of the generated secret to use for the SecretKey
      Returns:
      number of bytes written to the cipherText output buffer
      Throws:
      CryptoException - with the following reason codes:
      • CryptoException.UNINITIALIZED_KEY if the public key is not initialized.
      • CryptoException.ILLEGAL_VALUE if 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, or to > sharedSecretSize().
        • to - from does not equal the number of bytes required to initialize the SecretKey.
    • sharedSecretSize

      short sharedSecretSize()
      Returns the size of the shared secret.

      This method can be called to find out the length of the shared secret before encapsulate is called.

      Returns:
      the size in bytes of the shared secret
    • 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 encapsulate is called.

      Returns:
      the size in bytes of the key ciphertext