All Products
Search
Document Center

Key Management Service:AdvanceGenerateDataKey

Last Updated:Mar 31, 2026

Generates a data key for envelope encryption using Key Management Service (KMS). The operation returns both the plaintext data key and the ciphertext data key. Use the plaintext data key to encrypt data outside KMS, then store the ciphertext data key and authentication data (Aad) for later decryption.

How it works

The operation uses a high-quality random number generator to produce a data key, independent of the KMS key material. It then encrypts the data key using the primary key version of the specified KMS key in Galois/Counter Mode (GCM).

Important

If automatic key rotation is enabled, call AdvanceGenerateDataKey (not GenerateDataKey) to generate data keys. Using GenerateDataKey when key rotation is enabled causes the rotation feature to stop working. For details, see Configure key rotation.

AdvanceGenerateDataKey vs GenerateDataKey

Both operations generate a data key, but they differ in which key version is used for encryption and what data you must store afterward.

AspectAdvanceGenerateDataKeyGenerateDataKey
Key version used for encryptionPrimary key versionInitial key version
Data to store after encryptionCiphertextBlob, AadCiphertextBlob, Iv, Algorithm, Aad
Supported decrypt operationAdvanceDecryptDecrypt or AdvanceDecrypt

Limits

This operation supports only symmetric keys in KMS instances of the software key management type. For supported key specifications, see Key types and specifications.

Important

Only GCM mode is supported for data key encryption.

Request parameters

ParameterTypeRequiredExampleDescription
KeyIdstringYeskey-hzz62f1cb66fa42qo****The globally unique ID of the KMS key, or an alias bound to the key. The key must be a symmetric key in a software key management type KMS instance.
NumberOfBytesintYes32The length of the data key to generate.
AadbinaryNoBinary dataThe authentication data for GCM-mode encryption. If specified, you must also pass the same Aad value when calling AdvanceDecrypt.

Response elements

ParameterTypeExampleDescription
CiphertextBlobbytesBinary ciphertextThe ciphertext data key. This field encodes the key ID (KeyId), encryption algorithm (Algorithm), and initial vector (Iv) used during encryption. To decrypt, pass only CiphertextBlob and Aad to AdvanceDecrypt.
PlaintextbytesBinary plaintextThe plaintext data key. Use this to encrypt data outside KMS.
KeyIdstringkey-hzz62f1cb66fa42qo****The globally unique ID of the KMS key. If the request KeyId was an alias, this field returns the ID of the key the alias is bound to.
KeyVersionIdstringkey-hzz62f1cb66fa42qo****-17kedv****The ID of the key version used to encrypt the data key.
AlgorithmstringAES_GCMThe encryption algorithm used to encrypt the data key.
IvbytesBinary dataThe initial vector used to encrypt the data key.
RequestIdstringc0065a6d-7784-4ef2-a692-288fdcbc7b9dThe request ID.

Error codes

HTTP status codeError codeError messageDescription
404Forbidden.OnlySymmetricKeySupportedThe key %s is not a symmetric key. The API only supports symmetric keys.Only symmetric keys are supported.

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