Managed data keys

Updated at:

Use managed data keys to enable centralized key management and protection through KMS for local offline encryption and decryption scenarios. Managed data keys are persistently stored in KMS, and you can retrieve key plaintext at any time through the console or API to perform local encryption and decryption operations.

How it works

Managed data keys are implemented based on KMS Secrets Manager. Data keys are stored in KMS secrets for subsequent retrieval. The workflow is as follows:

  1. When you create a data key, KMS encrypts the data key with the specified customer master key (CMK) to generate the data key ciphertext.

    Note

    When you import a data key, you must manually encrypt the key material to generate the data key ciphertext.

  2. After the data key ciphertext is generated, KMS automatically creates a data key secret to store the ciphertext.

  3. Call the GetManagedDataKey API operation by using an SDK to retrieve the decrypted data key plaintext from the data key secret.

  4. Use the retrieved data key plaintext for local encryption and decryption operations.

Important
  • Unlike one-time data keys generated by the GenerateDataKey API operation, managed data keys support repeated plaintext retrieval and are suitable for offline encryption and decryption scenarios that require persistent keys.

  • The data key secret created by creating or importing a data key consumes only the data key quota, not the secret quota.

Before you begin

Before you use managed data keys, make sure that the following conditions are met:

  • KMS instance type: Only subscription-based instances are supported.

    Note

    Pay-as-you-go instances and cross-region synchronization instances (replica instances) are not supported.

  • Instance status: The KMS instance is connected and not expired.

  • Quota limit: The number of data keys in the instance has not reached the upper limit. If the limit is reached, you must first purchase additional data key quota.

  • Gateway limit: Only shared gateways are supported for API calls.

Create or import a data key

KMS provides two methods to obtain managed data keys:

Method

Key material source

Automatic rotation

Scenario

Create

Auto-generated by KMS

Supported

You have no existing key material and require full KMS management with periodic rotation support.

Import

User-provided

Not supported

You have existing key material and require control over the key source.

Create a managed data key

When you create a data key, KMS automatically generates the key material and protects it with the specified CMK. Data keys created this way support automatic rotation.

  1. Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose Resources > Keys.

  2. On the Data Keys tab, select the target KMS instance and click Create Data Key.

  3. In the Create Data Key panel, configure the following parameters.

    Parameter

    Description

    Data Key Name

    The unique identifier of the data key. Maximum length: 128 characters.

    Data Key Length (Bytes)

    The length of the data key in bytes. Valid values: 1 to 1024. Default value: 32. Common lengths include:

    • 16 bytes: applicable to AES-128 and SM4.

    • 24 bytes: applicable to AES-192

    • 32 bytes: applicable to AES-256.

    Master Key Type

    The source of the CMK used to protect the data key material.

    • Select Existing Master Key: Select from the symmetric CMK list of the current instance.

    • Create New Master Key: The system automatically creates a new symmetric CMK for the current instance.

      Note

      You can view and manage the created keys on the Keys > Customer Master Keys tab.

    CMK

    This parameter is displayed when you set Master Key Type to Select Existing Master Key. Search and select a symmetric Customer Master Keys from the same instance in the drop-down list. You can also click Advanced Search to select from a table view.

    Important

    Only symmetric keys are supported.

  4. Click OK.

Import a managed data key (not recommended)

If you have existing key material, you can import it to KMS for management. Imported data keys do not support automatic rotation.

Step 1: Process the key material

  1. Encode the original key material in Base64.

  2. Encrypt the encoded result by using a CMK:

    1. On the Keys > Customer Master Keys tab, identify the CMK that you want to use to protect the data key material and record its key ID.

    2. Call the Encrypt operation of the CMK to encrypt the Base64-encoded result and obtain the key material ciphertext. For more information, see Encryption and decryption examples.

Step 2: Configure the data key

  1. Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose Resources > Keys.

  2. On the Data Keys tab, select the target KMS instance and click Import Data Key.

  3. In the Import Data Key panel, configure the following parameters.

    Parameter

    Description

    Data Key Name

    The unique identifier of the data key. Maximum length: 128 characters.

    Data Key Length (Bytes)

    The length of the data key in bytes. Valid values: 1 to 1024. Default value: 32. Common lengths include:

    • 16 bytes: applicable to AES-128 and SM4.

    • 24 bytes: applicable to AES-192

    • 32 bytes: applicable to AES-256.

    CMK

    Select the Customer Master Keys used to protect the data key material from the symmetric CMK list of the current instance. The CMK must be the same as the one used for encryption in Step 1.

    Note

    You can view and manage the created keys on the Keys > Customer Master Keys tab.

    Data Key Content

    The key material ciphertext obtained in Step 1.

  4. Click OK.

Application integration

Applications can retrieve managed data key plaintext through APIs to perform local encryption and decryption. Currently, only shared gateways are supported for API calls.

Related API operations:

Step 1: Configure permissions

When applications operate managed data keys through APIs, you must configure permissions for the RAM identities of the applications. The following examples show the least privilege policies.

Permission to retrieve data key plaintext and query versions

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "kms:ListManagedDataKeyVersions",
        "kms:GetSecretValue",
        "kms:GetManagedDataKey"
      ],
      "Resource": [
        "acs:kms:{regionId}:{aliuid}:secret/kms-datakey!{dataKeyName}",
        "acs:kms:{regionId}:{aliuid}:secret/kms-datakeyversion!{dataKeyName}!*"
      ]
    }
  ]
}

Permission to decrypt data key material

When retrieving the data key plaintext, KMS must use the CMK to decrypt the key material. Therefore, the application also needs the Decrypt permission on the corresponding CMK.

{
  "Version": "1",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "kms:Decrypt"
      ],
      "Resource": [
        "acs:kms:{regionId}:{aliuid}:key/{keyId}"
      ]
    }
  ]
}

Parameters

  • {regionId}: The region ID of the KMS instance, such as cn-hangzhou.

  • {aliuid}: The Alibaba Cloud account ID.

  • {dataKeyName}: The data key name. The first line in Resource grants permission for the primary secret, and the second line grants permission for all version secrets by using the wildcard *.

  • {keyId}: The ID of the CMK.

Note

You can authorize different applications to access different managed data keys by specifying different {dataKeyName} values, achieving fine-grained permission isolation.

Step 2: Integrate by using an SDK

Use KMS SDK to call the GetManagedDataKey API operation to retrieve the data key plaintext and perform local encryption and decryption. The following examples use the Go SDK to demonstrate data encryption and decryption with the AES-GCM algorithm. To manually download the data key plaintext, see the Manually download the data key plaintext section.

Encrypt data

During encryption, the latest version of the data key is used. The version identifier is prepended to the ciphertext header for key version identification during decryption.

package main

import (
    "crypto/aes"
    "crypto/cipher"
    cryptorand "crypto/rand"
    "encoding/base64"
    "io"
    "os"

    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    kmsclient "github.com/alibabacloud-go/kms-20160120/v4/client"
    "github.com/alibabacloud-go/tea/tea"
)

func EncryptWithManagedDataKey(plaintext []byte, dataKeyName string) ([]byte, error) {
    kms, err := kmsclient.NewClient(&openapi.Config{
        AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
        AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
        Endpoint:        tea.String("kms.cn-hangzhou.aliyuncs.com"),
    })
    if err != nil {
        return nil, err
    }

    // Retrieve the latest version of the managed data key plaintext. You can also manually download the key plaintext.
    getResp, err := kms.GetManagedDataKey(&kmsclient.GetManagedDataKeyRequest{
        DataKeyName: tea.String(dataKeyName),
        UseLatest:   tea.Bool(true),
    })
    if err != nil {
        return nil, err
    }
    dataKeyVersionId := tea.StringValue(getResp.Body.DataKeyVersionId)
    b64PlainDataKey := tea.StringValue(getResp.Body.Plaintext)
    // Decode the Base64-encoded string.
    plainDataKey, err := base64.StdEncoding.DecodeString(b64PlainDataKey)
    if err != nil {
        return nil, err
    }

    // Encrypt the data by using AES-GCM.
    block, err := aes.NewCipher(plainDataKey)
    if err != nil {
        return nil, err
    }
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        return nil, err
    }
    nonce := make([]byte, gcm.NonceSize())
    _, err = io.ReadFull(cryptorand.Reader, nonce)
    if err != nil {
        return nil, err
    }

    ciphertext := gcm.Seal(nonce, nonce, plaintext, nil)
    // Prepend the version identifier (fixed 8 bytes) to the ciphertext for key version identification during decryption.
    output := append([]byte(dataKeyVersionId), ciphertext...)
    return output, nil
}

Decrypt data

During decryption, the version identifier is parsed from the ciphertext header to retrieve the corresponding data key version for decryption.

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "fmt"
    "os"

    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    kmsclient "github.com/alibabacloud-go/kms-20160120/v4/client"
    "github.com/alibabacloud-go/tea/tea"
)

func DecryptWithManagedDataKey(encrypted []byte, dataKeyName string) ([]byte, error) {
    kms, err := kmsclient.NewClient(&openapi.Config{
        AccessKeyId:     tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")),
        AccessKeySecret: tea.String(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")),
        Endpoint:        tea.String("kms.cn-hangzhou.aliyuncs.com"),
    })
    if err != nil {
        return nil, err
    }

    // Parse the version identifier (first 8 bytes) from the ciphertext header.
    dataKeyVersionId := string(encrypted[:8])
    ciphertext := encrypted[8:]

    // Retrieve the managed data key plaintext for the specified version.
    getResp, err := kms.GetManagedDataKey(&kmsclient.GetManagedDataKeyRequest{
        DataKeyName:      tea.String(dataKeyName),
        DataKeyVersionId: tea.String(dataKeyVersionId),
    })
    if err != nil {
        return nil, err
    }
    b64PlainDataKey := tea.StringValue(getResp.Body.Plaintext)
    plainDataKey, err := base64.StdEncoding.DecodeString(b64PlainDataKey)
    if err != nil {
        return nil, err
    }

    // Decrypt the data by using AES-GCM.
    block, err := aes.NewCipher(plainDataKey)
    if err != nil {
        return nil, err
    }
    gcm, err := cipher.NewGCM(block)
    if err != nil {
        return nil, err
    }
    nonceSize := gcm.NonceSize()
    if len(ciphertext) < nonceSize {
        return nil, fmt.Errorf("ciphertext too short")
    }

    nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
    return gcm.Open(nil, nonce, ciphertext, nil)
}

Configure automatic rotation (optional)

Automatic rotation periodically generates new versions of the key material while keeping old versions available.

Important

Imported data keys do not support automatic rotation.

  1. Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose Resources > Keys.

  2. On the Data Keys tab, select the target KMS instance and click the target data key name to go to the details page.

  3. In the Versions section, click Configure Rotation. Refer to the following descriptions to complete the configuration, and then click OK.

    Parameter

    Description

    Automatic Rotation

    Enable or disable automatic rotation.

    Rotation period

    The rotation interval. Valid values: 7 to 365 days. Default value: 7 days.

    Warning

    Each rotation consumes one data key quota. Plan your rotation strategy carefully to avoid unnecessary quota consumption.

Other operations

Manually download the data key plaintext

Download the plaintext key material from the console for local offline encryption and decryption. When automatic rotation is enabled, a data key has multiple versions. Downloading from the list retrieves the latest version by default. To download a historical version, go to the details page and operate from the version list.

Important

The downloaded material is Base64-encoded. You must decode it before using it for encryption and decryption.

Download from the data key list

  • Download a single data key plaintext:

    1. On the Data Keys tab, select the target KMS instance, and then click Actions in the Download Plaintext column of the target data key.

    2. The browser automatically downloads the plaintext file. The file name format is PlainDataKey_{dataKeyName}_{versionId}.txt.

  • Bulk download:

    1. On the Data Keys tab, select the target KMS instance and select multiple data keys.

    2. Click Bulk Download Plaintext below the list.

    3. The system packages the latest version plaintext of all selected data keys into a ZIP file for download (file name format: dataKeys_{regionId}_{timestamp}.zip).

Download from the data key details page

From the data key details page, you can download the plaintext of any version.

  1. On the Data Keys tab, select the target KMS instance and click the target data key name to go to the details page.

  2. In the Versions section, click Download Plaintext in the Actions column of the target version.

    Note

    All versions in the version list are available for use.

Schedule data key deletion

Deleting a data key is irreversible. To prevent accidental deletion, KMS uses a scheduled deletion mechanism: a pending deletion period (7 to 30 days) during which you can cancel the deletion at any time. After the period ends, the data key is permanently deleted.

Warning

Deleted data keys cannot be recovered. Data encrypted with a deleted key cannot be decrypted. Confirm that the key is no longer needed before scheduling deletion.

Schedule deletion

  1. On the Data Keys tab, select the target KMS instance and click Schedule Deletion in the Actions column of the target data key.

  2. In the dialog box, set the pending deletion period (7 to 30 days, default: 30 days) and click OK.

Cancel deletion

During the pending deletion period, you can cancel the deletion at any time to restore the data key to its normal state.

  1. On the Data Keys tab, select the target KMS instance and find the data key in the pending deletion state.

  2. Click Cancel Deletion in the Actions column of the target data key.

  3. In the confirmation dialog box, click OK.

View version and rotation information

  1. Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose Resources > Keys.

  2. On the Data Keys tab, select the target KMS instance and click the target data key name to go to the details page.

    • View the version information after rotation in the Versions list.

    • View Last Rotation Time, Next Rotation Time, and Rotation Period in the Basic Information section.