Skip to content

verify_mac async

verify_mac(
    message: PlaintextType,
    key_id: KeyIdType,
    mac_algorithm: MacAlgorithmSpec,
    mac: CiphertextType,
    *,
    config_overrides: Optional[AsyncKMSClientConfig] = None,
    grant_tokens: Optional[GrantTokenList] = None,
    dry_run: Optional[NullableBooleanType] = None,
) -> VerifyMacResponse

Verifies the hash-based message authentication code (HMAC) for a specified message, HMAC KMS key, and MAC algorithm. To verify the HMAC, VerifyMac computes an HMAC using the message, HMAC KMS key, and MAC algorithm that you specify, and compares the computed HMAC to the HMAC that you specify. If the HMACs are identical, the verification succeeds; otherwise, it fails. Verification indicates that the message hasn't changed since the HMAC was calculated, and the specified key was used to generate and verify the HMAC.

HMAC KMS keys and the HMAC algorithms that KMS uses conform to industry standards defined in RFC 2104.

This operation is part of KMS support for HMAC KMS keys. For details, see HMAC keys in KMS in the Key Management Service Developer Guide.

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:VerifyMac (key policy)

Related operations: GenerateMac

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

Parameters:

  • message (PlaintextType) –

    The message that will be used in the verification. Enter the same message that was used to generate the HMAC.

    GenerateMac and VerifyMac do not provide special handling for message digests. If you generated an HMAC for a hash digest of a message, you must verify the HMAC for the same hash digest.

  • key_id (KeyIdType) –

    The KMS key that will be used in the verification.

    Enter a key ID of the KMS key that was used to generate the HMAC. If you identify a different KMS key, the VerifyMac operation fails.

  • mac_algorithm (MacAlgorithmSpec) –

    The MAC algorithm that will be used in the verification. Enter the same MAC algorithm that was used to compute the HMAC. This algorithm must be supported by the HMAC KMS key identified by the KeyId parameter.

  • mac (CiphertextType) –

    The HMAC to verify. Enter the HMAC that was generated by the GenerateMac operation when you specified the same message, HMAC KMS key, and MAC algorithm as the values specified in this request.

  • 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:

  • DisabledException –

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

  • DryRunOperationException –

    The request was rejected because the DryRun parameter was specified.

  • 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. 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.

  • KMSInvalidMacException –

    The request was rejected because the HMAC verification failed. HMAC verification fails when the HMAC computed by using the specified message, HMAC KMS key, and MAC algorithm does not match the HMAC specified in the request.

  • 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 verify an HMAC This example verifies an HMAC for a particular message, HMAC KMS keys, and MAC algorithm. A value of 'true' in the MacValid value in the response indicates that the HMAC is valid.

>>> await client.verify_mac(message='Hello World', key_id='1234abcd-12ab-34cd-56ef-1234567890ab', mac_algorithm='HMAC_SHA_384', mac='<HMAC_TAG>')
Source code in docs_src/capo_kms/_services/async_kms.py
async def verify_mac(
    self,
    message: "capo_kms.types.plaintext_type.PlaintextType",
    key_id: "capo_kms.types.key_id_type.KeyIdType",
    mac_algorithm: "capo_kms.types.mac_algorithm_spec.MacAlgorithmSpec",
    mac: "capo_kms.types.ciphertext_type.CiphertextType",
    *,
    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.verify_mac_response.VerifyMacResponse":
    """<p>Verifies the hash-based message authentication code (HMAC) for a specified message, HMAC KMS key, and MAC algorithm. To verify the HMAC, <code>VerifyMac</code> computes an HMAC using the message, HMAC KMS key, and MAC algorithm that you specify, and compares the computed HMAC to the HMAC that you specify. If the HMACs are identical, the verification succeeds; otherwise, it fails. Verification indicates that the message hasn't changed since the HMAC was calculated, and the specified key was used to generate and verify the HMAC.</p> <p>HMAC KMS keys and the HMAC algorithms that KMS uses conform to industry standards defined in <a href="https://datatracker.ietf.org/doc/html/rfc2104">RFC 2104</a>.</p> <p>This operation is part of KMS support for HMAC KMS keys. For details, see <a href="https://docs.aws.amazon.com/kms/latest/developerguide/hmac.html">HMAC keys in KMS</a> in the <i>Key Management Service Developer Guide</i>.</p> <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:VerifyMac</a> (key policy)</p> <p> <b>Related operations</b>: <a>GenerateMac</a> </p> <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:
        message: <p>The message that will be used in the verification. Enter the same message that was used to generate the HMAC.</p> <p> <a>GenerateMac</a> and <code>VerifyMac</code> do not provide special handling for message digests. If you generated an HMAC for a hash digest of a message, you must verify the HMAC for the same hash digest.</p>
        key_id: <p>The KMS key that will be used in the verification.</p> <p>Enter a key ID of the KMS key that was used to generate the HMAC. If you identify a different KMS key, the <code>VerifyMac</code> operation fails.</p>
        mac_algorithm: <p>The MAC algorithm that will be used in the verification. Enter the same MAC algorithm that was used to compute the HMAC. This algorithm must be supported by the HMAC KMS key identified by the <code>KeyId</code> parameter.</p>
        mac: <p>The HMAC to verify. Enter the HMAC that was generated by the <a>GenerateMac</a> operation when you specified the same message, HMAC KMS key, and MAC algorithm as the values specified in this request.</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.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_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>. 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_mac_exception.KMSInvalidMacException: <p>The request was rejected because the HMAC verification failed. HMAC verification fails when the HMAC computed by using the specified message, HMAC KMS key, and MAC algorithm does not match the HMAC specified in the request.</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 verify an HMAC
        This example verifies an HMAC for a particular message, HMAC KMS keys, and MAC algorithm. A value of 'true' in the MacValid value in the response indicates that the HMAC is valid.

        >>> await client.verify_mac(message='Hello World', key_id='1234abcd-12ab-34cd-56ef-1234567890ab', mac_algorithm='HMAC_SHA_384', mac='<HMAC_TAG>')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_kms.types.verify_mac_request.VerifyMacRequest]",
    ) -> AsyncOperationResponse[
        "capo_kms.types.verify_mac_response.VerifyMacResponse"
    ]:
        import capo_kms._operations.trent_service.verify_mac

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

    def _redact_input(
        input_: "capo_kms.types.verify_mac_request.VerifyMacRequest",
    ) -> dict[str, Any]:
        import capo_kms.types.verify_mac_request

        return capo_kms.types.verify_mac_request.redact_sensitive(input_)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("VerifyMac", redact_input=_redact_input)
    )
    input_: capo_kms.types.verify_mac_request.VerifyMacRequest = {
        "message": message,
        "key_id": key_id,
        "mac_algorithm": mac_algorithm,
        "mac": mac,
    }
    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