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, use the Amazon Web Services Nitro Enclaves SDK or any Amazon Web Services SDK. Use theRecipient
parameter to provide the attestation document for the enclave. 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, see How Amazon Web Services Nitro Enclaves uses KMS in the Key Management Service Developer Guide.For more information about entropy and random number generation, see Key Management Service Cryptographic Details.
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 and the encryption algorithm to use with the enclave’s public key. The only valid encryption algorithm is
RSAES_OAEP_SHA_256
.This parameter only supports attestation documents for Amazon Web Services Nitro Enclaves. To include this parameter, use the Amazon Web Services Nitro Enclaves SDK or any Amazon Web Services SDK.
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 enclave. ThePlaintext
field in the response is null or empty.For information about the interaction between KMS and Amazon Web Services Nitro Enclaves, see How Amazon Web Services Nitro Enclaves uses 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 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. 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 Nitro enclave. This ciphertext can be decrypted only by using a private key in the Nitro enclave.
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. For information about the interaction between KMS and Amazon Web Services Nitro Enclaves, see How Amazon Web Services Nitro Enclaves uses 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': { '...': '...', }, }