7
7
that are used to encrypt and decrypt data. The cryptographic materials include
8
8
plaintext and encrypted data keys, and an optional message signing key.
9
9
This example creates a CMM and then encrypts a custom input EXAMPLE_DATA
10
- with an encryption context. This example also includes some sanity checks for demonstration:
10
+ with an encryption context. Creating a CMM involves taking a keyring as input,
11
+ and we use an AWS KMS Keyring for this example.
12
+ This example also includes some sanity checks for demonstration:
11
13
1. Ciphertext and plaintext data are not the same
12
14
2. Encryption context is correct in the decrypted message header
13
15
3. Decrypted plaintext value matches EXAMPLE_DATA
42
44
def encrypt_and_decrypt_with_cmm (
43
45
kms_key_id : str
44
46
):
45
- """Demonstrate an encrypt/decrypt cycle using an AWS KMS keyring .
47
+ """Demonstrate an encrypt/decrypt cycle using an AWS Cryptographic Material Managers .
46
48
47
- Usage: encrypt_and_decrypt_with_keyring (kms_key_id)
49
+ Usage: encrypt_and_decrypt_with_cmm (kms_key_id)
48
50
:param kms_key_id: KMS Key identifier for the KMS key you want to use for encryption and
49
51
decryption of your data keys.
50
52
:type kms_key_id: string
@@ -63,10 +65,7 @@ def encrypt_and_decrypt_with_cmm(
63
65
commitment_policy = CommitmentPolicy .REQUIRE_ENCRYPT_REQUIRE_DECRYPT
64
66
)
65
67
66
- # 2. Create a boto3 client for KMS.
67
- kms_client = boto3 .client ('kms' , region_name = "us-west-2" )
68
-
69
- # 3. Create encryption context.
68
+ # 2. Create encryption context.
70
69
# Remember that your encryption context is NOT SECRET.
71
70
# For more information, see
72
71
# https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context
@@ -79,6 +78,8 @@ def encrypt_and_decrypt_with_cmm(
79
78
}
80
79
81
80
# 4. Create a KMS keyring to use with the CryptographicMaterialsManager
81
+ kms_client = boto3 .client ('kms' , region_name = "us-west-2" )
82
+
82
83
mat_prov : AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders (
83
84
config = MaterialProvidersConfig ()
84
85
)
@@ -102,31 +103,31 @@ def encrypt_and_decrypt_with_cmm(
102
103
input = cmm_input
103
104
)
104
105
105
- # 5 . Encrypt the data with the encryptionContext.
106
+ # 6 . Encrypt the data with the encryptionContext.
106
107
ciphertext , _ = client .encrypt (
107
108
source = EXAMPLE_DATA ,
108
109
materials_manager = cmm ,
109
110
encryption_context = encryption_context
110
111
)
111
112
112
- # 6 . Demonstrate that the ciphertext and plaintext are different.
113
+ # 7 . Demonstrate that the ciphertext and plaintext are different.
113
114
# (This is an example for demonstration; you do not need to do this in your own code.)
114
115
assert ciphertext != EXAMPLE_DATA , \
115
116
"Ciphertext and plaintext data are the same. Invalid encryption"
116
117
117
- # 7 . Decrypt your encrypted data using the same keyring you used on encrypt.
118
+ # 8 . Decrypt your encrypted data using the same cmm you used on encrypt.
118
119
plaintext_bytes , dec_header = client .decrypt (
119
120
source = ciphertext ,
120
121
materials_manager = cmm
121
122
)
122
123
123
- # 8 . Demonstrate that the encryption context is correct in the decrypted message header
124
+ # 9 . Demonstrate that the encryption context is correct in the decrypted message header
124
125
# (This is an example for demonstration; you do not need to do this in your own code.)
125
126
for k , v in encryption_context .items ():
126
127
assert v == dec_header .encryption_context [k ], \
127
128
"Encryption context does not match expected values"
128
129
129
- # 9 . Demonstrate that the decrypted plaintext is identical to the original plaintext.
130
+ # 10 . Demonstrate that the decrypted plaintext is identical to the original plaintext.
130
131
# (This is an example for demonstration; you do not need to do this in your own code.)
131
132
assert plaintext_bytes == EXAMPLE_DATA , \
132
133
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
0 commit comments