2
2
# SPDX-License-Identifier: Apache-2.0
3
3
"""
4
4
This example sets up the KMS Keyring
5
+
6
+ The AWS KMS keyring uses symmetric encryption KMS keys to generate, encrypt and
7
+ decrypt data keys. This example creates a KMS Keyring and then encrypts a custom input EXAMPLE_DATA
8
+ with an encryption context. This example also demonstrates some sanity checks for demonstration.
9
+ 1. Ciphertext and plaintext data are not the same
10
+ 2. Encryption context is correct in the decrypted message header
11
+ 3. Decrypted plaintext value matches EXAMPLE_DATA
12
+
13
+ AWS KMS keyrings can be used independently or in a multi-keyring with other keyrings
14
+ of the same or a different type.
15
+
16
+ For more info on how to use KMS keyring, see
17
+ https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/use-kms-keyring.html
5
18
"""
6
19
import sys
7
-
20
+ from typing import Dict
8
21
import boto3
9
22
10
23
from aws_cryptographic_materialproviders .mpl import AwsCryptographicMaterialProviders
11
24
from aws_cryptographic_materialproviders .mpl .config import MaterialProvidersConfig
12
25
from aws_cryptographic_materialproviders .mpl .models import CreateAwsKmsKeyringInput
13
26
from aws_cryptographic_materialproviders .mpl .references import IKeyring
14
- from typing import Dict
15
27
16
28
import aws_encryption_sdk
17
29
from aws_encryption_sdk import CommitmentPolicy
18
30
19
31
20
32
# TODO-MPL: Remove this as part of removing PYTHONPATH hacks.
21
- module_root_dir = '/' .join (__file__ .split ("/" )[:- 1 ])
33
+ MODULE_ROOT_DIR = '/' .join (__file__ .split ("/" )[:- 1 ])
22
34
23
- sys .path .append (module_root_dir )
35
+ sys .path .append (MODULE_ROOT_DIR )
24
36
25
37
EXAMPLE_DATA : bytes = b"Hello World"
26
38
39
+
27
40
def encrypt_and_decrypt_with_keyring (
28
41
kms_key_id : str
29
42
):
30
- """Demonstrate an encrypt/decrypt cycle using an AWS KMS keyring."""
43
+ """Demonstrate an encrypt/decrypt cycle using an AWS KMS keyring.
31
44
45
+ Usage: encrypt_and_decrypt_with_keyring(kms_key_id)
46
+ :param kms_key_id: KMS Key identifier for the KMS key you want to use for encryption and
47
+ decryption of your data keys.
48
+ :type kms_key_id: string
49
+
50
+ For more info on KMS Key identifiers, see
51
+ https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#key-id
32
52
"""
33
- 1. Instantiate the encryption SDK client.
34
- This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
35
- which enforces that this client only encrypts using committing algorithm suites and enforces
36
- that this client will only decrypt encrypted messages that were created with a committing
37
- algorithm suite.
38
- This is the default commitment policy if you were to build the client as
39
- `client = aws_encryption_sdk.EncryptionSDKClient()`.
40
- """
53
+
54
+ # 1. Instantiate the encryption SDK client.
55
+ # This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy,
56
+ # which enforces that this client only encrypts using committing algorithm suites and enforces
57
+ # that this client will only decrypt encrypted messages that were created with a committing
58
+ # algorithm suite.
59
+ # This is the default commitment policy if you were to build the client as
60
+ # `client = aws_encryption_sdk.EncryptionSDKClient()`.
41
61
client = aws_encryption_sdk .EncryptionSDKClient (
42
62
commitment_policy = CommitmentPolicy .REQUIRE_ENCRYPT_REQUIRE_DECRYPT
43
63
)
44
64
45
- """
46
- 2. Create boto3 clients for KMS.
47
- """
65
+ # 2. Create a boto3 client for KMS.
48
66
kms_client = boto3 .client ('kms' , region_name = "us-west-2" )
49
67
50
- """
51
- 3. Instantiate the Material Providers
52
- """
53
- mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
54
- config = MaterialProvidersConfig ()
55
- )
56
-
57
- """
58
- 4. Create encryption context
59
- Remember that your encryption context is NOT SECRET.
60
- https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
61
- """
68
+ # 3. Create encryption context.
69
+ # Remember that your encryption context is NOT SECRET.
70
+ # For more information, see
71
+ # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
62
72
encryption_context : Dict [str , str ] = {
63
73
"encryption" : "context" ,
64
74
"is not" : "secret" ,
@@ -67,9 +77,11 @@ def encrypt_and_decrypt_with_keyring(
67
77
"the data you are handling" : "is what you think it is" ,
68
78
}
69
79
70
- """
71
- 5. Create the KMS keyring
72
- """
80
+ # 4. Create a KMS keyring
81
+ mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
82
+ config = MaterialProvidersConfig ()
83
+ )
84
+
73
85
keyring_input : CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput (
74
86
kms_key_id = kms_key_id ,
75
87
kms_client = kms_client
@@ -79,37 +91,29 @@ def encrypt_and_decrypt_with_keyring(
79
91
input = keyring_input
80
92
)
81
93
82
- """
83
- 6. Encrypt the data for the encryptionContext
84
- """
94
+ # 5. Encrypt the data for the encryptionContext
85
95
ciphertext , _ = client .encrypt (
86
96
source = EXAMPLE_DATA ,
87
97
keyring = kms_keyring ,
88
98
encryption_context = encryption_context
89
99
)
90
100
91
- """
92
- 7. Demonstrate that the ciphertext and plaintext are different.
93
- """
94
- assert ciphertext != EXAMPLE_DATA , "Ciphertext and plaintext data are the same. Invalid encryption"
95
-
96
- """
97
- 8. Decrypt your encrypted data using the same keyring you used on encrypt.
98
- You do not need to specify the encryption context on decrypt
99
- because the header of the encrypted message includes the encryption context.
100
- """
101
+ # 6. Demonstrate that the ciphertext and plaintext are different.
102
+ # (This is an example for demonstration; you do not need to do this in your own code.)
103
+ assert ciphertext != EXAMPLE_DATA , \
104
+ "Ciphertext and plaintext data are the same. Invalid encryption"
105
+
106
+ # 7. Decrypt your encrypted data using the same keyring you used on encrypt.
101
107
plaintext_bytes , dec_header = client .decrypt (
102
108
source = ciphertext ,
103
109
keyring = kms_keyring
104
110
)
105
111
106
- """
107
- 9. Demonstrate that the encryption context is correct in the decrypted message header
108
- """
112
+ # 8. Demonstrate that the encryption context is correct in the decrypted message header
113
+ # (This is an example for demonstration; you do not need to do this in your own code.)
109
114
for k , v in encryption_context .items ():
110
- assert v == dec_header .encryption_context [k ], "Encryption context does not match expected values"
115
+ assert v == dec_header .encryption_context [k ], \
116
+ "Encryption context does not match expected values"
111
117
112
- """
113
- 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
114
- """
115
- assert plaintext_bytes == EXAMPLE_DATA
118
+ # 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
119
+ assert plaintext_bytes == EXAMPLE_DATA
0 commit comments