Skip to content

Commit 1df7dbc

Browse files
fix(Examples): Validate EC on decrypt (#697)
1 parent 1c73a65 commit 1df7dbc

21 files changed

+203
-168
lines changed

examples/src/aws_kms_discovery_keyring_example.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,14 @@ def encrypt_and_decrypt_with_keyring(
153153
# successfully decrypted. The resulting data key is used to decrypt the
154154
# ciphertext's message.
155155
# If all calls to KMS fail, the decryption fails.
156-
plaintext_bytes, dec_header = client.decrypt(
156+
plaintext_bytes, _ = client.decrypt(
157157
source=ciphertext,
158-
keyring=discovery_keyring
158+
keyring=discovery_keyring,
159+
# Provide the encryption context that was supplied to the encrypt method
160+
encryption_context=encryption_context,
159161
)
160162

161-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
162-
# (This is an example for demonstration; you do not need to do this in your own code.)
163-
for k, v in encryption_context.items():
164-
assert v == dec_header.encryption_context[k], \
165-
"Encryption context does not match expected values"
166-
167-
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
163+
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
168164
# (This is an example for demonstration; you do not need to do this in your own code.)
169165
assert plaintext_bytes == EXAMPLE_DATA, \
170166
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
@@ -192,7 +188,10 @@ def encrypt_and_decrypt_with_keyring(
192188
try:
193189
plaintext_bytes, _ = client.decrypt(
194190
source=ciphertext,
195-
keyring=discovery_keyring_bob
191+
keyring=discovery_keyring_bob,
192+
# Verify that the encryption context in the result contains the
193+
# encryption context supplied to the encrypt method
194+
encryption_context=encryption_context,
196195
)
197196

198197
raise AssertionError("Decrypt using discovery keyring with wrong AWS Account ID should"

examples/src/aws_kms_discovery_multi_keyring_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,14 @@ def encrypt_and_decrypt_with_keyring(
151151
# All of this is done serially, until a success occurs or all keyrings have
152152
# failed all (filtered) EDKs.
153153
# KMS Discovery Keyrings will attempt to decrypt Multi Region Keys (MRKs) and regular KMS Keys.
154-
plaintext_bytes, dec_header = client.decrypt(
154+
plaintext_bytes, _ = client.decrypt(
155155
source=ciphertext,
156-
keyring=discovery_multi_keyring
156+
keyring=discovery_multi_keyring,
157+
# Provide the encryption context that was supplied to the encrypt method
158+
encryption_context=encryption_context,
157159
)
158160

159-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
160-
# (This is an example for demonstration; you do not need to do this in your own code.)
161-
for k, v in encryption_context.items():
162-
assert v == dec_header.encryption_context[k], \
163-
"Encryption context does not match expected values"
164-
165-
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
161+
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
166162
# (This is an example for demonstration; you do not need to do this in your own code.)
167163
assert plaintext_bytes == EXAMPLE_DATA, \
168164
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/aws_kms_keyring_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,14 @@ def encrypt_and_decrypt_with_keyring(
9797
"Ciphertext and plaintext data are the same. Invalid encryption"
9898

9999
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
100-
plaintext_bytes, dec_header = client.decrypt(
100+
plaintext_bytes, _ = client.decrypt(
101101
source=ciphertext,
102-
keyring=kms_keyring
102+
keyring=kms_keyring,
103+
# Provide the encryption context that was supplied to the encrypt method
104+
encryption_context=encryption_context,
103105
)
104106

105-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
106-
# (This is an example for demonstration; you do not need to do this in your own code.)
107-
for k, v in encryption_context.items():
108-
assert v == dec_header.encryption_context[k], \
109-
"Encryption context does not match expected values"
110-
111-
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
107+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
112108
# (This is an example for demonstration; you do not need to do this in your own code.)
113109
assert plaintext_bytes == EXAMPLE_DATA, \
114110
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/aws_kms_mrk_discovery_keyring_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,13 @@ def encrypt_and_decrypt_with_keyring(
163163
)
164164

165165
# 7. Decrypt your encrypted data using the discovery keyring.
166-
plaintext_bytes, dec_header = client.decrypt(
166+
plaintext_bytes, _ = client.decrypt(
167167
source=ciphertext,
168-
keyring=decrypt_discovery_keyring
168+
keyring=decrypt_discovery_keyring,
169+
# Provide the encryption context that was supplied to the encrypt method
170+
encryption_context=encryption_context,
169171
)
170172

171-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
172-
# (This is an example for demonstration; you do not need to do this in your own code.)
173-
for k, v in encryption_context.items():
174-
assert v == dec_header.encryption_context[k], \
175-
"Encryption context does not match expected values"
176-
177-
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
173+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
178174
# (This is an example for demonstration; you do not need to do this in your own code.)
179175
assert plaintext_bytes == EXAMPLE_DATA

examples/src/aws_kms_mrk_discovery_multi_keyring_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,13 @@ def encrypt_and_decrypt_with_keyring(
172172
# All of this is done serially, until a success occurs or all keyrings have failed
173173
# all (filtered) EDKs. KMS MRK Discovery Keyrings will attempt to decrypt
174174
# Multi Region Keys (MRKs) and regular KMS Keys.
175-
plaintext_bytes, dec_header = client.decrypt(
175+
plaintext_bytes, _ = client.decrypt(
176176
source=ciphertext,
177-
keyring=decrypt_discovery_keyring
177+
keyring=decrypt_discovery_keyring,
178+
# Provide the encryption context that was supplied to the encrypt method
179+
encryption_context=encryption_context,
178180
)
179181

180-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
181-
# (This is an example for demonstration; you do not need to do this in your own code.)
182-
for k, v in encryption_context.items():
183-
assert v == dec_header.encryption_context[k], \
184-
"Encryption context does not match expected values"
185-
186-
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
182+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
187183
# (This is an example for demonstration; you do not need to do this in your own code.)
188184
assert plaintext_bytes == EXAMPLE_DATA

examples/src/aws_kms_mrk_keyring_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,14 @@ def encrypt_and_decrypt_with_keyring(
132132
)
133133

134134
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
135-
plaintext_bytes, dec_header = client.decrypt(
135+
plaintext_bytes, _ = client.decrypt(
136136
source=ciphertext,
137-
keyring=decrypt_keyring
137+
keyring=decrypt_keyring,
138+
# Provide the encryption context that was supplied to the encrypt method
139+
encryption_context=encryption_context,
138140
)
139141

140-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
141-
# (This is an example for demonstration; you do not need to do this in your own code.)
142-
for k, v in encryption_context.items():
143-
assert v == dec_header.encryption_context[k], \
144-
"Encryption context does not match expected values"
145-
146-
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
142+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
147143
# (This is an example for demonstration; you do not need to do this in your own code.)
148144
assert plaintext_bytes == EXAMPLE_DATA, \
149145
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/aws_kms_mrk_multi_keyring_example.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,14 @@ def encrypt_and_decrypt_with_keyring(
124124
# 6. Decrypt your encrypted data using the same AwsKmsMrkMultiKeyring you used on encrypt.
125125
# It will decrypt the data using the generator key (in this case, the MRK), since that is
126126
# the first available KMS key on the keyring that is capable of decrypting the data.
127-
plaintext_bytes, dec_header = client.decrypt(
127+
plaintext_bytes, _ = client.decrypt(
128128
source=ciphertext,
129-
keyring=kms_mrk_multi_keyring
129+
keyring=kms_mrk_multi_keyring,
130+
# Provide the encryption context that was supplied to the encrypt method
131+
encryption_context=encryption_context,
130132
)
131133

132-
# 7. Demonstrate that the encryption context is correct in the decrypted message header
133-
# (This is an example for demonstration; you do not need to do this in your own code.)
134-
for k, v in encryption_context.items():
135-
assert v == dec_header.encryption_context[k], \
136-
"Encryption context does not match expected values"
137-
138-
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
134+
# 7. Demonstrate that the decrypted plaintext is identical to the original plaintext.
139135
# (This is an example for demonstration; you do not need to do this in your own code.)
140136
assert plaintext_bytes == EXAMPLE_DATA, \
141137
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
@@ -144,7 +140,7 @@ def encrypt_and_decrypt_with_keyring(
144140
# multi-keyring used to encrypt the data is also capable of decrypting the data.
145141
# (This is an example for demonstration; you do not need to do this in your own code.)
146142

147-
# 9. Create a single AwsKmsMrkKeyring with the replica KMS MRK from the second region.
143+
# 8. Create a single AwsKmsMrkKeyring with the replica KMS MRK from the second region.
148144

149145
# Create a boto3 client for KMS in the second region which is the region for mrk_replica_key_id.
150146
second_region_kms_client = boto3.client('kms', region_name=mrk_replica_decrypt_region)
@@ -158,19 +154,15 @@ def encrypt_and_decrypt_with_keyring(
158154
input=second_region_mrk_keyring_input
159155
)
160156

161-
# 10. Decrypt your encrypted data using the second region AwsKmsMrkKeyring
162-
plaintext_bytes_second_region, dec_header_second_region = client.decrypt(
157+
# 9. Decrypt your encrypted data using the second region AwsKmsMrkKeyring
158+
plaintext_bytes_second_region, _ = client.decrypt(
163159
source=ciphertext,
164-
keyring=second_region_mrk_keyring
160+
keyring=second_region_mrk_keyring,
161+
# Provide the encryption context that was supplied to the encrypt method
162+
encryption_context=encryption_context,
165163
)
166164

167-
# 11. Demonstrate that the encryption context is correct in the decrypted message header
168-
# (This is an example for demonstration; you do not need to do this in your own code.)
169-
for k, v in encryption_context.items():
170-
assert v == dec_header_second_region.encryption_context[k], \
171-
"Encryption context does not match expected values"
172-
173-
# 12. Demonstrate that the decrypted plaintext is identical to the original plaintext.
165+
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
174166
# (This is an example for demonstration; you do not need to do this in your own code.)
175167
assert plaintext_bytes_second_region == EXAMPLE_DATA
176168

examples/src/aws_kms_multi_keyring_example.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ def encrypt_and_decrypt_with_keyring(
133133
# 6a. Decrypt your encrypted data using the same multi_keyring you used on encrypt.
134134
plaintext_bytes_multi_keyring, _ = client.decrypt(
135135
source=ciphertext,
136-
keyring=kms_multi_keyring
136+
keyring=kms_multi_keyring,
137+
# Provide the encryption context that was supplied to the encrypt method
138+
encryption_context=encryption_context,
137139
)
138140

139141
# 6b. Demonstrate that the decrypted plaintext is identical to the original plaintext.
@@ -164,7 +166,9 @@ def encrypt_and_decrypt_with_keyring(
164166
# 7c. Decrypt your encrypted data using the default_region_kms_keyring.
165167
plaintext_bytes_default_region_kms_keyring, _ = client.decrypt(
166168
source=ciphertext,
167-
keyring=default_region_kms_keyring
169+
keyring=default_region_kms_keyring,
170+
# Provide the encryption context that was supplied to the encrypt method
171+
encryption_context=encryption_context,
168172
)
169173

170174
# 7d. Demonstrate that the decrypted plaintext is identical to the original plaintext.
@@ -192,7 +196,9 @@ def encrypt_and_decrypt_with_keyring(
192196
# 8c. Decrypt your encrypted data using the second_region_kms_keyring.
193197
plaintext_bytes_second_region_kms_keyring, _ = client.decrypt(
194198
source=ciphertext,
195-
keyring=second_region_kms_keyring
199+
keyring=second_region_kms_keyring,
200+
# Provide the encryption context that was supplied to the encrypt method
201+
encryption_context=encryption_context,
196202
)
197203

198204
# 8d. Demonstrate that the decrypted plaintext is identical to the original plaintext.

examples/src/aws_kms_rsa_keyring_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,14 @@ def encrypt_and_decrypt_with_keyring(
103103
"Ciphertext and plaintext data are the same. Invalid encryption"
104104

105105
# 7. Decrypt your encrypted data using the same keyring you used on encrypt.
106-
plaintext_bytes, dec_header = client.decrypt(
106+
plaintext_bytes, _ = client.decrypt(
107107
source=ciphertext,
108-
keyring=kms_rsa_keyring
108+
keyring=kms_rsa_keyring,
109+
# Provide the encryption context that was supplied to the encrypt method
110+
encryption_context=encryption_context,
109111
)
110112

111-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
112-
# (This is an example for demonstration; you do not need to do this in your own code.)
113-
for k, v in encryption_context.items():
114-
assert v == dec_header.encryption_context[k], \
115-
"Encryption context does not match expected values"
116-
117-
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
113+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
118114
# (This is an example for demonstration; you do not need to do this in your own code.)
119115
assert plaintext_bytes == EXAMPLE_DATA, \
120116
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/default_cryptographic_materials_manager_example.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,14 @@ def encrypt_and_decrypt_with_default_cmm(
109109
"Ciphertext and plaintext data are the same. Invalid encryption"
110110

111111
# 7. Decrypt your encrypted data using the same cmm you used on encrypt.
112-
plaintext_bytes, dec_header = client.decrypt(
112+
plaintext_bytes, _ = client.decrypt(
113113
source=ciphertext,
114-
materials_manager=cmm
114+
materials_manager=cmm,
115+
# Provide the encryption context that was supplied to the encrypt method
116+
encryption_context=encryption_context,
115117
)
116118

117-
# 8. Demonstrate that the encryption context is correct in the decrypted message header
118-
# (This is an example for demonstration; you do not need to do this in your own code.)
119-
for k, v in encryption_context.items():
120-
assert v == dec_header.encryption_context[k], \
121-
"Encryption context does not match expected values"
122-
123-
# 9. Demonstrate that the decrypted plaintext is identical to the original plaintext.
119+
# 8. Demonstrate that the decrypted plaintext is identical to the original plaintext.
124120
# (This is an example for demonstration; you do not need to do this in your own code.)
125121
assert plaintext_bytes == EXAMPLE_DATA, \
126122
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

examples/src/file_streaming_example.py

-6
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,6 @@ def encrypt_and_decrypt_with_keyring(
134134
for chunk in decryptor:
135135
pt_file.write(chunk)
136136

137-
# 9. Demonstrate that the encryption context is correct in the decrypted message header
138-
# (This is an example for demonstration; you do not need to do this in your own code.)
139-
for k, v in encryption_context.items():
140-
assert v == decryptor.header.encryption_context[k], \
141-
"Encryption context does not match expected values"
142-
143137
# 10. Demonstrate that the decrypted plaintext is identical to the original plaintext.
144138
# (This is an example for demonstration; you do not need to do this in your own code.)
145139
assert filecmp.cmp(plaintext_filename, decrypted_filename), \

examples/src/hierarchical_keyring_example.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ def encrypt_and_decrypt_with_keyring(
200200
try:
201201
client.decrypt(
202202
source=ciphertext_a,
203-
keyring=hierarchical_keyring_b
203+
keyring=hierarchical_keyring_b,
204+
# Verify that the encryption context in the result contains the
205+
# encryption context supplied to the encrypt method
206+
encryption_context=encryption_context_a,
204207
)
205208
except AWSEncryptionSDKClientError:
206209
pass
@@ -210,22 +213,30 @@ def encrypt_and_decrypt_with_keyring(
210213
try:
211214
client.decrypt(
212215
source=ciphertext_b,
213-
keyring=hierarchical_keyring_a
216+
keyring=hierarchical_keyring_a,
217+
# Verify that the encryption context in the result contains the
218+
# encryption context supplied to the encrypt method
219+
encryption_context=encryption_context_b,
214220
)
215221
except AWSEncryptionSDKClientError:
216222
pass
217223

218-
# 10. Demonstrate that data encrypted by one tenant's branch key can be decrypted by that tenant,
224+
# 11. Demonstrate that data encrypted by one tenant's branch key can be decrypted by that tenant,
219225
# and that the decrypted data matches the input data.
220226
plaintext_bytes_a, _ = client.decrypt(
221227
source=ciphertext_a,
222-
keyring=hierarchical_keyring_a
228+
keyring=hierarchical_keyring_a,
229+
# Provide the encryption context that was supplied to the encrypt method
230+
encryption_context=encryption_context_a,
223231
)
224232
assert plaintext_bytes_a == EXAMPLE_DATA, \
225233
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"
234+
226235
plaintext_bytes_b, _ = client.decrypt(
227236
source=ciphertext_b,
228-
keyring=hierarchical_keyring_b
237+
keyring=hierarchical_keyring_b,
238+
# Provide the encryption context that was supplied to the encrypt method
239+
encryption_context=encryption_context_b,
229240
)
230241
assert plaintext_bytes_b == EXAMPLE_DATA, \
231242
"Decrypted plaintext should be identical to the original plaintext. Invalid decryption"

0 commit comments

Comments
 (0)