- Navigation GuideYou are on a Command (operation) page with structural examples. Use the navigation breadcrumb if you would like to return to the Client landing page.
DeriveSharedSecretCommand
Derives a shared secret using a key agreement algorithm.
You must use an asymmetric NIST-recommended elliptic curve (ECC) or SM2 (China Regions only) KMS key pair with a KeyUsage
value of KEY_AGREEMENT
to call DeriveSharedSecret.
DeriveSharedSecret uses the Elliptic Curve Cryptography Cofactor Diffie-Hellman Primitive (ECDH) to establish a key agreement between two peers by deriving a shared secret from their elliptic curve public-private key pairs. You can use the raw shared secret that DeriveSharedSecret returns to derive a symmetric key that can encrypt and decrypt data that is sent between the two peers, or that can generate and verify HMACs. KMS recommends that you follow NIST recommendations for key derivation when using the raw shared secret to derive a symmetric key.
The following workflow demonstrates how to establish key agreement over an insecure communication channel using DeriveSharedSecret.
-
Alice calls CreateKey to create an asymmetric KMS key pair with a
KeyUsage
value ofKEY_AGREEMENT
.The asymmetric KMS key must use a NIST-recommended elliptic curve (ECC) or SM2 (China Regions only) key spec.
-
Bob creates an elliptic curve key pair.
Bob can call CreateKey to create an asymmetric KMS key pair or generate a key pair outside of KMS. Bob's key pair must use the same NIST-recommended elliptic curve (ECC) or SM2 (China Regions ony) curve as Alice.
-
Alice and Bob exchange their public keys through an insecure communication channel (like the internet).
Use GetPublicKey to download the public key of your asymmetric KMS key pair.
KMS strongly recommends verifying that the public key you receive came from the expected party before using it to derive a shared secret.
-
Alice calls DeriveSharedSecret.
KMS uses the private key from the KMS key pair generated in Step 1, Bob's public key, and the Elliptic Curve Cryptography Cofactor Diffie-Hellman Primitive to derive the shared secret. The private key in your KMS key pair never leaves KMS unencrypted. DeriveSharedSecret returns the raw shared secret.
-
Bob uses the Elliptic Curve Cryptography Cofactor Diffie-Hellman Primitive to calculate the same raw secret using his private key and Alice's public key.
To derive a shared secret you must provide a key agreement algorithm, the private key of the caller's asymmetric NIST-recommended elliptic curve or SM2 (China Regions only) KMS key pair, and the public key from your peer's NIST-recommended elliptic curve or SM2 (China Regions only) key pair. The public key can be from another asymmetric KMS key pair or from a key pair generated outside of KMS, but both key pairs must be on the same elliptic curve.
The KMS key that you use for this operation must be in a compatible key state. For details, see Key states of KMS keys in the Key Management Service Developer Guide.
Cross-account use: Yes. To perform this operation with a KMS key in a different Amazon Web Services account, specify the key ARN or alias ARN in the value of the KeyId
parameter.
Required permissions: kms:DeriveSharedSecret (key policy)
Related operations:
-
CreateKey
-
GetPublicKey
-
DescribeKey
Eventual consistency: The KMS API follows an eventual consistency model. For more information, see KMS eventual consistency .
Example Syntax
Use a bare-bones client and the command you need to make an API call.
import { KMSClient, DeriveSharedSecretCommand } from "@aws-sdk/client-kms"; // ES Modules import
// const { KMSClient, DeriveSharedSecretCommand } = require("@aws-sdk/client-kms"); // CommonJS import
const client = new KMSClient(config);
const input = { // DeriveSharedSecretRequest
KeyId: "STRING_VALUE", // required
KeyAgreementAlgorithm: "ECDH", // required
PublicKey: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("") // required
GrantTokens: [ // GrantTokenList
"STRING_VALUE",
],
DryRun: true || false,
Recipient: { // RecipientInfo
KeyEncryptionAlgorithm: "RSAES_OAEP_SHA_256",
AttestationDocument: new Uint8Array(), // e.g. Buffer.from("") or new TextEncoder().encode("")
},
};
const command = new DeriveSharedSecretCommand(input);
const response = await client.send(command);
// { // DeriveSharedSecretResponse
// KeyId: "STRING_VALUE",
// SharedSecret: new Uint8Array(),
// CiphertextForRecipient: new Uint8Array(),
// KeyAgreementAlgorithm: "ECDH",
// KeyOrigin: "AWS_KMS" || "EXTERNAL" || "AWS_CLOUDHSM" || "EXTERNAL_KEY_STORE",
// };
Example Usage
DeriveSharedSecretCommand Input
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
KeyAgreementAlgorithm Required | KeyAgreementAlgorithmSpec | undefined | Specifies the key agreement algorithm used to derive the shared secret. The only valid value is |
KeyId Required | string | undefined | Identifies an asymmetric NIST-recommended ECC or SM2 (China Regions only) KMS key. KMS uses the private key in the specified key pair to derive the shared secret. The key usage of the KMS key must be To specify a KMS key, use its key ID, key ARN, alias name, or alias ARN. When using an alias name, prefix it with For example:
To get the key ID and key ARN for a KMS key, use ListKeys or DescribeKey. To get the alias name and alias ARN, use ListAliases. |
PublicKey Required | Uint8Array | undefined | Specifies the public key in your peer's NIST-recommended elliptic curve (ECC) or SM2 (China Regions only) key pair. The public key must be a DER-encoded X.509 public key, also known as GetPublicKey returns the public key of an asymmetric KMS key pair in the required DER-encoded format. If you use Amazon Web Services CLI version 1 , you must provide the DER-encoded X.509 public key in a file. Otherwise, the Amazon Web Services CLI Base64-encodes the public key a second time, resulting in a You can specify the public key as binary data in a file using fileb ( |
DryRun | boolean | undefined | Checks if your request will succeed. To learn more about how to use this parameter, see Testing your KMS API calls in the Key Management Service Developer Guide. |
GrantTokens | string[] | undefined | A list of grant tokens. Use a grant token when your permission to call this operation comes from a new grant that has not yet achieved eventual consistency. For more information, see Grant token and Using a grant token in the Key Management Service Developer Guide. |
Recipient | RecipientInfo | undefined | A signed attestation document from an Amazon Web Services Nitro enclave and the encryption algorithm to use with the enclave's public key. The only valid encryption algorithm is This parameter only supports attestation documents for Amazon Web Services Nitro Enclaves. To call DeriveSharedSecret for an Amazon Web Services Nitro Enclaves, use the Amazon Web Services Nitro Enclaves SDK to generate the attestation document and then use the Recipient parameter from any Amazon Web Services SDK to provide the attestation document for the enclave. When you use this parameter, instead of returning a plaintext copy of the shared secret, KMS encrypts the plaintext shared secret under the public key in the attestation document, and returns the resulting ciphertext in the For information about the interaction between KMS and Amazon Web Services Nitro Enclaves, see How Amazon Web Services Nitro Enclaves uses KMS in the Key Management Service Developer Guide. |
DeriveSharedSecretCommand Output
Parameter | Type | Description |
---|
Parameter | Type | Description |
---|---|---|
$metadata Required | ResponseMetadata | Metadata pertaining to this request. |
CiphertextForRecipient | Uint8Array | undefined | The plaintext shared secret encrypted with the public key in the attestation document. This field is included in the response only when the |
KeyAgreementAlgorithm | KeyAgreementAlgorithmSpec | undefined | Identifies the key agreement algorithm used to derive the shared secret. |
KeyId | string | undefined | Identifies the KMS key used to derive the shared secret. |
KeyOrigin | OriginType | undefined | The source of the key material for the specified KMS key. When this value is The only valid values for DeriveSharedSecret are |
SharedSecret | Uint8Array | undefined | The raw secret derived from the specified key agreement algorithm, private key in the asymmetric KMS key, and your peer's public key. If the response includes the |
Throws
Name | Fault | Details |
---|
Name | Fault | Details |
---|---|---|
DependencyTimeoutException | server | The system timed out while trying to fulfill the request. You can retry the request. |
DisabledException | client | The request was rejected because the specified KMS key is not enabled. |
DryRunOperationException | client | The request was rejected because the DryRun parameter was specified. |
InvalidGrantTokenException | client | The request was rejected because the specified grant token is not valid. |
InvalidKeyUsageException | client | The request was rejected for one of the following reasons:
For encrypting, decrypting, re-encrypting, and generating data keys, the To find the encryption or signing algorithms supported for a particular KMS key, use the DescribeKey operation. |
KeyUnavailableException | server | The request was rejected because the specified KMS key was not available. You can retry the request. |
KMSInternalException | server | The request was rejected because an internal exception occurred. The request can be retried. |
KMSInvalidStateException | client | The request was rejected because the state of the specified resource is not valid for this request. This exceptions means one of the following:
|
NotFoundException | client | The request was rejected because the specified entity or resource could not be found. |
KMSServiceException | Base exception class for all service exceptions from KMS service. |