KMS / Client / generate_random
generate_random¶
- KMS.Client.generate_random(**kwargs)¶
Returns a random byte string that is cryptographically secure.
You must use the
NumberOfBytes
parameter to specify the length of the random byte string. There is no default value for string length.By default, the random byte string is generated in KMS. To generate the byte string in the CloudHSM cluster associated with an CloudHSM key store, use the
CustomKeyStoreId
parameter.GenerateRandom
also supports Amazon Web Services Nitro Enclaves, which provide an isolated compute environment in Amazon EC2. To callGenerateRandom
for a Nitro enclave or NitroTPM, use the Amazon Web Services Nitro Enclaves SDK or any Amazon Web Services SDK. Use theRecipient
parameter to provide the attestation document for the attested environment. Instead of plaintext bytes, the response includes the plaintext bytes encrypted under the public key from the attestation document (CiphertextForRecipient
). For information about the interaction between KMS and Amazon Web Services Nitro Enclaves or Amazon Web Services NitroTPM, see Cryptographic attestation support in KMS in the Key Management Service Developer Guide.For more information about entropy and random number generation, see Entropy and random number generation in the Key Management Service Developer Guide.
Cross-account use: Not applicable.
GenerateRandom
does not use any account-specific resources, such as KMS keys.Required permissions: kms:GenerateRandom (IAM policy)
Eventual consistency: The KMS API follows an eventual consistency model. For more information, see KMS eventual consistency.
See also: AWS API Documentation
Request Syntax
response = client.generate_random( NumberOfBytes=123, CustomKeyStoreId='string', Recipient={ 'KeyEncryptionAlgorithm': 'RSAES_OAEP_SHA_256', 'AttestationDocument': b'bytes' } )
- Parameters:
NumberOfBytes (integer) – The length of the random byte string. This parameter is required.
CustomKeyStoreId (string) –
Generates the random byte string in the CloudHSM cluster that is associated with the specified CloudHSM key store. To find the ID of a custom key store, use the DescribeCustomKeyStores operation.
External key store IDs are not valid for this parameter. If you specify the ID of an external key store,
GenerateRandom
throws anUnsupportedOperationException
.Recipient (dict) –
A signed attestation document from an Amazon Web Services Nitro enclave or NitroTPM, and the encryption algorithm to use with the public key in the attestation document. The only valid encryption algorithm is
RSAES_OAEP_SHA_256
.This parameter supports the Amazon Web Services Nitro Enclaves SDK or any Amazon Web Services SDK for Amazon Web Services Nitro Enclaves. It supports any Amazon Web Services SDK for Amazon Web Services NitroTPM.
When you use this parameter, instead of returning plaintext bytes, KMS encrypts the plaintext bytes under the public key in the attestation document, and returns the resulting ciphertext in the
CiphertextForRecipient
field in the response. This ciphertext can be decrypted only with the private key in the attested environment. ThePlaintext
field in the response is null or empty.For information about the interaction between KMS and Amazon Web Services Nitro Enclaves or Amazon Web Services NitroTPM, see Cryptographic attestation support in KMS in the Key Management Service Developer Guide.
KeyEncryptionAlgorithm (string) –
The encryption algorithm that KMS should use with the public key for an Amazon Web Services Nitro Enclave or NitroTPM to encrypt plaintext values for the response. The only valid value is
RSAES_OAEP_SHA_256
.AttestationDocument (bytes) –
The attestation document for an Amazon Web Services Nitro Enclave or a NitroTPM. This document includes the enclave’s public key.
- Return type:
dict
- Returns:
Response Syntax
{ 'Plaintext': b'bytes', 'CiphertextForRecipient': b'bytes' }
Response Structure
(dict) –
Plaintext (bytes) –
The random byte string. When you use the HTTP API or the Amazon Web Services CLI, the value is Base64-encoded. Otherwise, it is not Base64-encoded.
If the response includes the
CiphertextForRecipient
field, thePlaintext
field is null or empty.CiphertextForRecipient (bytes) –
The plaintext random bytes encrypted with the public key from the attestation document. This ciphertext can be decrypted only by using a private key from the attested environment.
This field is included in the response only when the
Recipient
parameter in the request includes a valid attestation document from an Amazon Web Services Nitro enclave or NitroTPM. For information about the interaction between KMS and Amazon Web Services Nitro Enclaves or Amazon Web Services NitroTPM, see Cryptographic attestation support in KMS in the Key Management Service Developer Guide.
Exceptions
KMS.Client.exceptions.DependencyTimeoutException
KMS.Client.exceptions.KMSInternalException
KMS.Client.exceptions.UnsupportedOperationException
KMS.Client.exceptions.CustomKeyStoreNotFoundException
KMS.Client.exceptions.CustomKeyStoreInvalidStateException
Examples
The following example generates 32 bytes of random data.
response = client.generate_random( # The length of the random data, specified in number of bytes. NumberOfBytes=32, ) print(response)
Expected Output:
{ # The random data. 'Plaintext': '<binary data>', 'ResponseMetadata': { '...': '...', }, }