derive_shared_secret
¶
derive_shared_secret(
key_id: KeyIdType,
key_agreement_algorithm: KeyAgreementAlgorithmSpec,
public_key: PublicKeyType,
*,
config_overrides: Optional[KMSClientConfig] = None,
grant_tokens: Optional[GrantTokenList] = None,
dry_run: Optional[NullableBooleanType] = None,
recipient: Optional[RecipientInfo] = None,
) -> DeriveSharedSecretResponse
Derives a shared secret using a key agreement algorithm.
You must use an asymmetric NIST-standard 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
KeyUsagevalue ofKEY_AGREEMENT.The asymmetric KMS key must use a NIST-standard 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-standard 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-standard elliptic curve or SM2 (China Regions only) KMS key pair, and the public key from your peer's NIST-standard 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:
Eventual consistency: The KMS API follows an eventual consistency model. For more information, see KMS eventual consistency.
Parameters:
-
key_id(KeyIdType) –Identifies an asymmetric NIST-standard 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
KEY_AGREEMENT. To find theKeyUsageof a KMS key, use the DescribeKey operation.To specify a KMS key, use its key ID, key ARN, alias name, or alias ARN. When using an alias name, prefix it with
"alias/". To specify a KMS key in a different Amazon Web Services account, you must use the key ARN or alias ARN.For example:
-
Key ID:
1234abcd-12ab-34cd-56ef-1234567890ab -
Key ARN:
arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab -
Alias name:
alias/ExampleAlias -
Alias ARN:
arn:aws:kms:us-east-2:111122223333:alias/ExampleAlias
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.
-
-
key_agreement_algorithm(KeyAgreementAlgorithmSpec) –Specifies the key agreement algorithm used to derive the shared secret. The only valid value is
ECDH. -
public_key(PublicKeyType) –Specifies the public key in your peer's NIST-standard 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
SubjectPublicKeyInfo(SPKI), as defined in RFC 5280.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
ValidationException.You can specify the public key as binary data in a file using fileb (
fileb://) or in-line using a Base64 encoded string. -
grant_tokens(Optional[GrantTokenList], default:None) –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.
-
dry_run(Optional[NullableBooleanType], default:None) –Checks if your request will succeed.
DryRunis an optional parameter.To learn more about how to use this parameter, see Testing your permissions in the Key Management Service Developer Guide.
-
recipient(Optional[RecipientInfo], default:None) –A signed attestation document 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 only supports attestation documents for Amazon Web Services Nitro Enclaves or Amazon Web Services NitroTPM. To call DeriveSharedSecret generate an attestation document use either Amazon Web Services Nitro Enclaves SDK for an Amazon Web Services Nitro Enclaves or Amazon Web Services NitroTPM tools for Amazon Web Services NitroTPM. Then use the Recipient parameter from any Amazon Web Services SDK to provide the attestation document for the attested environment.
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
CiphertextForRecipientfield in the response. This ciphertext can be decrypted only with the private key in the attested environment. TheCiphertextBlobfield in the response contains the encrypted shared secret derived from the KMS key specified by theKeyIdparameter and public key specified by thePublicKeyparameter. TheSharedSecretfield 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 in the Key Management Service Developer Guide.
Raises:
-
DependencyTimeoutException–The system timed out while trying to fulfill the request. You can retry the request.
-
DisabledException–The request was rejected because the specified KMS key is not enabled.
-
DryRunOperationException–The request was rejected because the DryRun parameter was specified.
-
InvalidGrantTokenException–The request was rejected because the specified grant token is not valid.
-
InvalidKeyUsageException–The request was rejected for one of the following reasons:
-
The
KeyUsagevalue of the KMS key is incompatible with the API operation. -
The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key
(KeySpec).
For encrypting, decrypting, re-encrypting, and generating data keys, the
KeyUsagemust beENCRYPT_DECRYPT. For signing and verifying messages, theKeyUsagemust beSIGN_VERIFY. For generating and verifying message authentication codes (MACs), theKeyUsagemust beGENERATE_VERIFY_MAC. For deriving key agreement secrets, theKeyUsagemust beKEY_AGREEMENT. To find theKeyUsageof a KMS key, use the DescribeKey operation.To find the encryption or signing algorithms supported for a particular KMS key, use the DescribeKey operation.
-
-
KeyUnavailableException–The request was rejected because the specified KMS key was not available. You can retry the request.
-
KMSInternalException–The request was rejected because an internal exception occurred. The request can be retried.
-
KMSInvalidStateException–The request was rejected because the state of the specified resource is not valid for this request.
This exceptions means one of the following:
-
The key state of the KMS key is not compatible with the operation.
To find the key state, use the DescribeKey operation. For more information about which key states are compatible with each KMS operation, see Key states of KMS keys in the Key Management Service Developer Guide .
-
For cryptographic operations on KMS keys in custom key stores, this exception represents a general failure with many possible causes. To identify the cause, see the error message that accompanies the exception.
-
-
NotFoundException–The request was rejected because the specified entity or resource could not be found.
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To derive a shared secret The following example derives a shared secret using a key agreement algorithm.
>>> client.derive_shared_secret(key_id='1234abcd-12ab-34cd-56ef-1234567890ab', key_agreement_algorithm='ECDH', public_key='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvH3Yj0wbkLEpUl95Cv1cJVjsVNSjwGq3tCLnzXfhVwVvmzGN8pYj3U8nKwgouaHbBWNJYjP5VutbbkKS4Kv4GojwZBJyHN17kmxo8yTjRmjR15SKIQ8cqRA2uaERMLnpztIXdZp232PQPbWGxDyXYJ0aJ5EFSag')
Source code in docs_src/capo_kms/_services/kms.py
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 | |