All Products
Search
Document Center

Key Management Service:GenerateDataKey

Last Updated:Mar 31, 2026

Generates a data key encrypted under the initial version of a Key Management Service (KMS) key, and returns both the plaintext and ciphertext of the data key.

Operation description

Use this operation to implement envelope encryption: encrypt your data locally with the plaintext data key, then store the ciphertext data key alongside your encrypted data. Only the ciphertext data key needs to be stored — discard the plaintext after use.

After the operation completes, store all of the following fields. They are required to decrypt the data key later:

  • CiphertextBlob — the encrypted data key

  • Iv — the initialization vector (IV) used during encryption

  • Algorithm — the encryption algorithm

  • Aad — the additional authenticated data (AAD), if provided

Only Galois/Counter Mode (GCM) is supported when encrypting the data key. KMS uses a high-quality random number generator to produce the data key, which is independent of the KMS key material.

For information about key types and specifications, see Key types and specifications.

GenerateDataKey vs. AdvanceGenerateDataKey

Both operations generate a data key, but differ in which key version is used for encryption and which decrypt operations are supported.

AttributeGenerateDataKeyAdvanceGenerateDataKey
Key version usedInitial versionPrimary version
Fields to storeCiphertextBlob, Iv, Algorithm, AadCiphertextBlob, Aad
Supported decrypt operationsDecrypt or AdvanceDecryptAdvanceDecrypt only
ScopeAll symmetric keysSoftware key management type symmetric keys only
Important

If you use a symmetric key in a software key management KMS instance with automatic key rotation enabled, call AdvanceGenerateDataKey instead. Using GenerateDataKey in this configuration causes the key rotation feature to stop working. For more information, see Configure key rotation.

Request parameters

ParameterTypeRequiredExampleDescription
KeyIdstringYeskey-hzz62f1cb66fa42qo****The globally unique ID of the KMS key. Also accepts an alias bound to the key.
NumberOfBytesintYes32The length of the data key to generate, in bytes.
AadbinaryNoBinary dataThe additional authenticated data (AAD) used in GCM mode when encrypting the data key. If specified, provide the same value when calling the Decrypt operation to decrypt the data key.

Response elements

ParameterTypeExampleDescription
KeyIdstringkey-hzz62f1cb66fa42qo****The globally unique ID of the KMS key. If the request used an alias, the ID of the key bound to that alias is returned.
IvbytesBinary dataThe initialization vector (IV) used when encrypting the data key. Provide this value when calling the Decrypt operation.
PlaintextbytesBinary plaintextThe plaintext data key. Use this to encrypt your data, then discard it from memory.
CiphertextBlobbytesBinary ciphertextThe ciphertext data key. Store this alongside your encrypted data.
AlgorithmstringAES_GCMThe encryption algorithm used to encrypt the data key.
RequestIdstring475f1620-b9d3-4d35-b5c6-3fbdd941423dThe request ID.

Error codes

For a list of error codes, see Service error codes.