generateRandom method

Future<GenerateRandomResponse> generateRandom({
  1. String? customKeyStoreId,
  2. int? numberOfBytes,
  3. RecipientInfo? recipient,
})

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 call GenerateRandom for a Nitro enclave or NitroTPM, use the Amazon Web Services Nitro Enclaves SDK or any Amazon Web Services SDK. Use the Recipient 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.

May throw CustomKeyStoreInvalidStateException. May throw CustomKeyStoreNotFoundException. May throw DependencyTimeoutException. May throw KMSInternalException. May throw UnsupportedOperationException.

Parameter customKeyStoreId : 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 an UnsupportedOperationException.

Parameter numberOfBytes : The length of the random byte string. This parameter is required.

Parameter recipient : 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. The Plaintext 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.

Implementation

Future<GenerateRandomResponse> generateRandom({
  String? customKeyStoreId,
  int? numberOfBytes,
  RecipientInfo? recipient,
}) async {
  _s.validateNumRange(
    'numberOfBytes',
    numberOfBytes,
    1,
    1024,
  );
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.1',
    'X-Amz-Target': 'TrentService.GenerateRandom'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      if (customKeyStoreId != null) 'CustomKeyStoreId': customKeyStoreId,
      if (numberOfBytes != null) 'NumberOfBytes': numberOfBytes,
      if (recipient != null) 'Recipient': recipient,
    },
  );

  return GenerateRandomResponse.fromJson(jsonResponse.body);
}