Skip to content

decapsulate async

decapsulate(
    key_id: KeyIdType,
    shared_secret_ciphertext_blob: CiphertextType,
    encapsulation_algorithm: EncapsulationAlgorithmSpec,
    *,
    config_overrides: Optional[AsyncKMSClientConfig] = None,
    grant_tokens: Optional[GrantTokenList] = None,
    dry_run: Optional[NullableBooleanType] = None,
) -> DecapsulateResponse

Recovers the shared secret that was encapsulated with the public key of an ML-KEM KMS key. Module-Lattice-Based Key-Encapsulation Mechanism (ML-KEM), specified in FIPS 203 on the NIST website, is a post-quantum algorithm for establishing a shared secret between two parties. For an example of how two parties establish a shared secret over an insecure channel using ML-KEM, see ML-KEM keys in KMS in the Key Management Service Developer Guide.

The Decapsulate operation is not available in China Regions.

Decapsulate returns the shared secret as a 32-byte value. You can use it directly as key material, or run it through a key derivation function to derive multiple keys and nonces.

Decapsulate returns an InvalidCiphertextException when the length of the SharedSecretCiphertextBlob is inconsistent with the ML-KEM key spec. In all other cases, Decapsulate returns a 32-byte pseudo-random value. This is true even when the SharedSecretCiphertextBlob was created using a different ML-KEM key or altered from its original value. This behavior is a security feature called implicit rejection that helps protect against chosen-ciphertext attacks.

Because a response does not indicate that the ciphertext and key are valid, confirm the two parties derived the same shared secret before using it.

The KMS key that you use for this operation must be in a compatible key state. For details, see Key states of KMS keys in the Key Management Service Developer Guide.

Cross-account use: Yes. To perform this operation with a KMS key in a different Amazon Web Services account, specify the key ARN or alias ARN in the value of the KeyId parameter.

Required permissions: kms:Decapsulate (key policy)

Related operations:

Eventual consistency: The KMS API follows an eventual consistency model. For more information, see KMS eventual consistency.

Parameters:

  • key_id (KeyIdType) –

    Identifies the asymmetric ML-KEM KMS key whose public key was used to produce the encapsulated shared secret. KMS uses the private key in the specified key pair to decapsulate the shared secret.

    To specify a KMS key, use its key ID, key ARN, alias name, or alias ARN. When using an alias name, prefix it with "alias/". To specify a KMS key in a different Amazon Web Services account, you must use the key ARN or alias ARN.

    For example:

    • Key ID: 1234abcd-12ab-34cd-56ef-1234567890ab

    • Key ARN: arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab

    • Alias name: alias/ExampleAlias

    • Alias ARN: arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias

    To get the key ID and key ARN for a KMS key, use ListKeys or DescribeKey. To get the alias name and alias ARN, use ListAliases.

  • shared_secret_ciphertext_blob (CiphertextType) –

    The encapsulated (ciphertext) form of the shared secret to recover. This is the SharedSecretCiphertextBlob value that the Encapsulate operation returns. When you use the HTTP API or the Amazon Web Services CLI, the value is Base64-encoded. Otherwise, it is not Base64-encoded.

  • encapsulation_algorithm (EncapsulationAlgorithmSpec) –

    Specifies the key encapsulation algorithm used to recover the shared secret. The only valid value is ML_KEM.

  • grant_tokens (Optional[GrantTokenList], default: None ) –

    A list of grant tokens.

    Use a grant token when your permission to call this operation comes from a new grant that has not yet achieved eventual consistency. For more information, see Grant token and Using a grant token in the Key Management Service Developer Guide.

  • dry_run (Optional[NullableBooleanType], default: None ) –

    Checks if your request will succeed. DryRun is an optional parameter.

    To learn more about how to use this parameter, see Testing your permissions in the Key Management Service Developer Guide.

Raises:

  • DependencyTimeoutException –

    The system timed out while trying to fulfill the request. You can retry the request.

  • DisabledException –

    The request was rejected because the specified KMS key is not enabled.

  • DryRunOperationException –

    The request was rejected because the DryRun parameter was specified.

  • InvalidCiphertextException –

    From the Decrypt or ReEncrypt operation, the request was rejected because the specified ciphertext, or additional authenticated data incorporated into the ciphertext, such as the encryption context, is corrupted, missing, or otherwise invalid.

    From the ImportKeyMaterial operation, the request was rejected because KMS could not decrypt the encrypted (wrapped) key material.

    From the Decapsulate operation, the request was rejected because the length of the SharedSecretCiphertextBlob is inconsistent with the specified ML-KEM key spec.

  • InvalidGrantTokenException –

    The request was rejected because the specified grant token is not valid.

  • InvalidKeyUsageException –

    The request was rejected for one of the following reasons:

    • The KeyUsage value of the KMS key is incompatible with the API operation.

    • The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (KeySpec).

    For encrypting, decrypting, re-encrypting, and generating data keys, the KeyUsage must be ENCRYPT_DECRYPT. For signing and verifying messages, the KeyUsage must be SIGN_VERIFY. For generating and verifying message authentication codes (MACs), the KeyUsage must be GENERATE_VERIFY_MAC. For deriving key agreement secrets, the KeyUsage must be KEY_AGREEMENT. For encapsulating and decapsulating shared secrets, the KeyUsage must be ENCAPSULATE_DECAPSULATE. To find the KeyUsage of a KMS key, use the DescribeKey operation.

    To find the encryption or signing algorithms supported for a particular KMS key, use the DescribeKey operation.

  • KeyUnavailableException –

    The request was rejected because the specified KMS key was not available. You can retry the request.

  • KMSInternalException –

    The request was rejected because an internal exception occurred. The request can be retried.

  • KMSInvalidStateException –

    The request was rejected because the state of the specified resource is not valid for this request.

    This exceptions means one of the following:

    • The key state of the KMS key is not compatible with the operation.

      To find the key state, use the DescribeKey operation. For more information about which key states are compatible with each KMS operation, see Key states of KMS keys in the Key Management Service Developer Guide .

    • For cryptographic operations on KMS keys in custom key stores, this exception represents a general failure with many possible causes. To identify the cause, see the error message that accompanies the exception.

  • NotFoundException –

    The request was rejected because the specified entity or resource could not be found.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

To recover a shared secret from its encapsulated ciphertext Recovers a shared secret from its encapsulated ciphertext (SharedSecretCiphertextBlob) using the private key in an asymmetric ML-KEM KMS key. Your peer calls Encapsulate to generate the shared secret and ciphertext.

>>> await client.decapsulate(key_id='1234abcd-12ab-34cd-56ef-1234567890ab', shared_secret_ciphertext_blob='<binary data>', encapsulation_algorithm='ML_KEM')
Source code in docs_src/capo_kms/_services/async_kms.py
async def decapsulate(
    self,
    key_id: "capo_kms.types.key_id_type.KeyIdType",
    shared_secret_ciphertext_blob: "capo_kms.types.ciphertext_type.CiphertextType",
    encapsulation_algorithm: "capo_kms.types.encapsulation_algorithm_spec.EncapsulationAlgorithmSpec",
    *,
    config_overrides: Optional[AsyncKMSClientConfig] = None,
    grant_tokens: Optional["capo_kms.types.grant_token_list.GrantTokenList"] = None,
    dry_run: Optional[
        "capo_kms.types.nullable_boolean_type.NullableBooleanType"
    ] = None,
) -> "capo_kms.types.decapsulate_response.DecapsulateResponse":
    """<p>Recovers the shared secret that was encapsulated with the public key of an ML-KEM KMS key. Module-Lattice-Based Key-Encapsulation Mechanism (ML-KEM), specified in <a href="https://csrc.nist.gov/pubs/fips/203/final">FIPS 203</a> on the NIST website, is a post-quantum algorithm for establishing a shared secret between two parties. For an example of how two parties establish a shared secret over an insecure channel using ML-KEM, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/pqkeys.html#mlkem">ML-KEM keys in KMS</a> in the <i>Key Management Service Developer Guide</i>.</p> <p>The Decapsulate operation is not available in China Regions.</p> <p> <code>Decapsulate</code> returns the shared secret as a 32-byte value. You can use it directly as key material, or run it through a key derivation function to derive multiple keys and nonces.</p> <important> <p> <code>Decapsulate</code> returns an <code>InvalidCiphertextException</code> when the length of the <code>SharedSecretCiphertextBlob</code> is inconsistent with the ML-KEM key spec. In all other cases, <code>Decapsulate</code> returns a 32-byte pseudo-random value. This is true even when the <code>SharedSecretCiphertextBlob</code> was created using a different ML-KEM key or altered from its original value. This behavior is a security feature called <i>implicit rejection</i> that helps protect against chosen-ciphertext attacks.</p> <p>Because a response does not indicate that the ciphertext and key are valid, confirm the two parties derived the same shared secret before using it.</p> </important> <p>The KMS key that you use for this operation must be in a compatible key state. For details, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/key-state.html">Key states of KMS keys</a> in the <i>Key Management Service Developer Guide</i>.</p> <p> <b>Cross-account use</b>: Yes. To perform this operation with a KMS key in a different Amazon Web Services account, specify the key ARN or alias ARN in the value of the <code>KeyId</code> parameter.</p> <p> <b>Required permissions</b>: <a href="https://docs.aws.amazon.com/kms/latest/developerguide/kms-api-permissions-reference.html">kms:Decapsulate</a> (key policy)</p> <p> <b>Related operations:</b> </p> <ul> <li> <p> <a>Encapsulate</a> </p> </li> <li> <p> <a>CreateKey</a> </p> </li> <li> <p> <a>GetPublicKey</a> </p> </li> <li> <p> <a>DescribeKey</a> </p> </li> </ul> <p> <b>Eventual consistency</b>: The KMS API follows an eventual consistency model. For more information, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/accessing-kms.html#programming-eventual-consistency">KMS eventual consistency</a>.</p>

    Args:
        key_id: <p>Identifies the asymmetric ML-KEM KMS key whose public key was used to produce the encapsulated shared secret. KMS uses the private key in the specified key pair to decapsulate the shared secret. </p> <p>To specify a KMS key, use its key ID, key ARN, alias name, or alias ARN. When using an alias name, prefix it with <code>"alias/"</code>. To specify a KMS key in a different Amazon Web Services account, you must use the key ARN or alias ARN.</p> <p>For example:</p> <ul> <li> <p>Key ID: <code>1234abcd-12ab-34cd-56ef-1234567890ab</code> </p> </li> <li> <p>Key ARN: <code>arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab</code> </p> </li> <li> <p>Alias name: <code>alias/ExampleAlias</code> </p> </li> <li> <p>Alias ARN: <code>arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias</code> </p> </li> </ul> <p>To get the key ID and key ARN for a KMS key, use <a>ListKeys</a> or <a>DescribeKey</a>. To get the alias name and alias ARN, use <a>ListAliases</a>.</p>
        shared_secret_ciphertext_blob: <p>The encapsulated (ciphertext) form of the shared secret to recover. This is the <code>SharedSecretCiphertextBlob</code> value that the <a>Encapsulate</a> operation returns. When you use the HTTP API or the Amazon Web Services CLI, the value is Base64-encoded. Otherwise, it is not Base64-encoded.</p>
        encapsulation_algorithm: <p>Specifies the key encapsulation algorithm used to recover the shared secret. The only valid value is <code>ML_KEM</code>.</p>
        grant_tokens: <p>A list of grant tokens.</p> <p>Use a grant token when your permission to call this operation comes from a new grant that has not yet achieved <i>eventual consistency</i>. For more information, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/grants.html#grant_token">Grant token</a> and <a href="https://docs.aws.amazon.com/kms/latest/developerguide/using-grant-token.html">Using a grant token</a> in the <i>Key Management Service Developer Guide</i>.</p>
        dry_run: <p>Checks if your request will succeed. <code>DryRun</code> is an optional parameter. </p> <p>To learn more about how to use this parameter, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/testing-permissions.html">Testing your permissions</a> in the <i>Key Management Service Developer Guide</i>.</p>

    Raises:
        capo_kms.errors.dependency_timeout_exception.DependencyTimeoutException: <p>The system timed out while trying to fulfill the request. You can retry the request.</p>
        capo_kms.errors.disabled_exception.DisabledException: <p>The request was rejected because the specified KMS key is not enabled.</p>
        capo_kms.errors.dry_run_operation_exception.DryRunOperationException: <p> The request was rejected because the DryRun parameter was specified. </p>
        capo_kms.errors.invalid_ciphertext_exception.InvalidCiphertextException: <p>From the <a>Decrypt</a> or <a>ReEncrypt</a> operation, the request was rejected because the specified ciphertext, or additional authenticated data incorporated into the ciphertext, such as the encryption context, is corrupted, missing, or otherwise invalid.</p> <p>From the <a>ImportKeyMaterial</a> operation, the request was rejected because KMS could not decrypt the encrypted (wrapped) key material. </p> <p>From the <a>Decapsulate</a> operation, the request was rejected because the length of the <code>SharedSecretCiphertextBlob</code> is inconsistent with the specified ML-KEM key spec.</p>
        capo_kms.errors.invalid_grant_token_exception.InvalidGrantTokenException: <p>The request was rejected because the specified grant token is not valid.</p>
        capo_kms.errors.invalid_key_usage_exception.InvalidKeyUsageException: <p>The request was rejected for one of the following reasons: </p> <ul> <li> <p>The <code>KeyUsage</code> value of the KMS key is incompatible with the API operation.</p> </li> <li> <p>The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key <code>(KeySpec</code>).</p> </li> </ul> <p>For encrypting, decrypting, re-encrypting, and generating data keys, the <code>KeyUsage</code> must be <code>ENCRYPT_DECRYPT</code>. For signing and verifying messages, the <code>KeyUsage</code> must be <code>SIGN_VERIFY</code>. For generating and verifying message authentication codes (MACs), the <code>KeyUsage</code> must be <code>GENERATE_VERIFY_MAC</code>. For deriving key agreement secrets, the <code>KeyUsage</code> must be <code>KEY_AGREEMENT</code>. For encapsulating and decapsulating shared secrets, the <code>KeyUsage</code> must be <code>ENCAPSULATE_DECAPSULATE</code>. To find the <code>KeyUsage</code> of a KMS key, use the <a>DescribeKey</a> operation.</p> <p>To find the encryption or signing algorithms supported for a particular KMS key, use the <a>DescribeKey</a> operation.</p>
        capo_kms.errors.key_unavailable_exception.KeyUnavailableException: <p>The request was rejected because the specified KMS key was not available. You can retry the request.</p>
        capo_kms.errors.kms_internal_exception.KMSInternalException: <p>The request was rejected because an internal exception occurred. The request can be retried.</p>
        capo_kms.errors.kms_invalid_state_exception.KMSInvalidStateException: <p>The request was rejected because the state of the specified resource is not valid for this request.</p> <p>This exceptions means one of the following:</p> <ul> <li> <p>The key state of the KMS key is not compatible with the operation. </p> <p>To find the key state, use the <a>DescribeKey</a> operation. For more information about which key states are compatible with each KMS operation, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/key-state.html">Key states of KMS keys</a> in the <i> <i>Key Management Service Developer Guide</i> </i>.</p> </li> <li> <p>For cryptographic operations on KMS keys in custom key stores, this exception represents a general failure with many possible causes. To identify the cause, see the error message that accompanies the exception.</p> </li> </ul>
        capo_kms.errors.not_found_exception.NotFoundException: <p>The request was rejected because the specified entity or resource could not be found.</p>
        capo_kms.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To recover a shared secret from its encapsulated ciphertext
        Recovers a shared secret from its encapsulated ciphertext (SharedSecretCiphertextBlob) using the private key in an asymmetric ML-KEM KMS key. Your peer calls Encapsulate to generate the shared secret and ciphertext.

        >>> await client.decapsulate(key_id='1234abcd-12ab-34cd-56ef-1234567890ab', shared_secret_ciphertext_blob='<binary data>', encapsulation_algorithm='ML_KEM')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_kms.types.decapsulate_request.DecapsulateRequest]",
    ) -> AsyncOperationResponse[
        "capo_kms.types.decapsulate_response.DecapsulateResponse"
    ]:
        import capo_kms._operations.trent_service.decapsulate

        (
            output,
            http_response,
        ) = await capo_kms._operations.trent_service.decapsulate.async_decapsulate(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    def _redact_output(
        output: "capo_kms.types.decapsulate_response.DecapsulateResponse",
    ) -> dict[str, Any]:
        import capo_kms.types.decapsulate_response

        return capo_kms.types.decapsulate_response.redact_sensitive(output)

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("Decapsulate", redact_output=_redact_output),
    )
    input_: capo_kms.types.decapsulate_request.DecapsulateRequest = {
        "key_id": key_id,
        "shared_secret_ciphertext_blob": shared_secret_ciphertext_blob,
        "encapsulation_algorithm": encapsulation_algorithm,
    }
    if grant_tokens is not None:
        input_["grant_tokens"] = grant_tokens
    if dry_run is not None:
        input_["dry_run"] = dry_run

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output