:doc:`KMS <../../kms>` / Client / generate_random

***************
generate_random
***************



.. py:method:: 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 <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nitro-enclave.html>`__, 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 <https://docs.aws.amazon.com/enclaves/latest/user/developing-applications.html#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 <https://docs.aws.amazon.com/kms/latest/developerguide/cryptographic-attestation.html>`__ in the *Key Management Service Developer Guide*.

   

  For more information about entropy and random number generation, see `Entropy and random number generation <https://docs.aws.amazon.com/kms/latest/developerguide/kms-cryptography.html#entropy-and-random-numbers>`__ 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 <https://docs.aws.amazon.com/kms/latest/developerguide/kms-api-permissions-reference.html>`__ (IAM policy)

   

  **Eventual consistency**: The KMS API follows an eventual consistency model. For more information, see `KMS eventual consistency <https://docs.aws.amazon.com/kms/latest/developerguide/accessing-kms.html#programming-eventual-consistency>`__.

  

  See also: `AWS API Documentation <https://docs.aws.amazon.com/goto/WebAPI/kms-2014-11-01/GenerateRandom>`_  


  **Request Syntax**
  ::

    response = client.generate_random(
        NumberOfBytes=123,
        CustomKeyStoreId='string',
        Recipient={
            'KeyEncryptionAlgorithm': 'RSAES_OAEP_SHA_256',
            'AttestationDocument': b'bytes'
        }
    )
    
  :type NumberOfBytes: integer
  :param NumberOfBytes: 

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

    

  
  :type CustomKeyStoreId: string
  :param 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``.

    

  
  :type Recipient: dict
  :param Recipient: 

    A signed `attestation document <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nitro-enclave-how.html#term-attestdoc>`__ 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 <https://docs.aws.amazon.com/enclaves/latest/user/developing-applications.html#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 <https://docs.aws.amazon.com/kms/latest/developerguide/cryptographic-attestation.html>`__ 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.

      

    
  
  
  :rtype: 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, the ``Plaintext`` 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 <https://docs.aws.amazon.com/kms/latest/developerguide/cryptographic-attestation.html>`__ in the *Key Management Service Developer Guide*.

        
  
  **Exceptions**
  
  *   :py:class:`KMS.Client.exceptions.DependencyTimeoutException`

  
  *   :py:class:`KMS.Client.exceptions.KMSInternalException`

  
  *   :py:class:`KMS.Client.exceptions.UnsupportedOperationException`

  
  *   :py:class:`KMS.Client.exceptions.CustomKeyStoreNotFoundException`

  
  *   :py:class:`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': {
            '...': '...',
        },
    }

  