@@ -189,7 +189,13 @@ def serialize_header(header, signer=None):
189
189
raise SerializationError ("Unrecognized message format version: {}" .format (header .version ))
190
190
191
191
192
- def _serialize_header_auth_v1 (algorithm , header , data_encryption_key , signer = None ):
192
+ def _serialize_header_auth_v1 (
193
+ algorithm ,
194
+ header ,
195
+ data_encryption_key ,
196
+ signer = None ,
197
+ required_ec_bytes = None
198
+ ):
193
199
"""Creates serialized header authentication data for messages in serialization version V1.
194
200
195
201
:param algorithm: Algorithm to use for encryption
@@ -198,16 +204,35 @@ def _serialize_header_auth_v1(algorithm, header, data_encryption_key, signer=Non
198
204
:param bytes data_encryption_key: Data key with which to encrypt message
199
205
:param signer: Cryptographic signer object (optional)
200
206
:type signer: aws_encryption_sdk.Signer
207
+ :param required_encryption_context_bytes: Serialized encryption context items
208
+ for all items whose keys are in the required_encryption_context list.
209
+ This is ONLY processed if using the aws-cryptographic-materialproviders library
210
+ AND its required encryption context CMM. (optional)
211
+ :type required_encryption_context_bytes: bytes
201
212
:returns: Serialized header authentication data
202
213
:rtype: bytes
203
214
"""
204
- header_auth = encrypt (
205
- algorithm = algorithm ,
206
- key = data_encryption_key ,
207
- plaintext = b"" ,
208
- associated_data = header ,
209
- iv = header_auth_iv (algorithm ),
210
- )
215
+ if required_ec_bytes is None :
216
+ header_auth = encrypt (
217
+ algorithm = algorithm ,
218
+ key = data_encryption_key ,
219
+ plaintext = b"" ,
220
+ associated_data = header ,
221
+ iv = header_auth_iv (algorithm ),
222
+ )
223
+ else :
224
+ header_auth = encrypt (
225
+ algorithm = algorithm ,
226
+ key = data_encryption_key ,
227
+ plaintext = b"" ,
228
+ # The AAD MUST be the concatenation of the serialized message header body and the serialization
229
+ # of encryption context to only authenticate. The encryption context to only authenticate MUST
230
+ # be the encryption context in the encryption materials filtered to only contain key value
231
+ # pairs listed in the encryption material's required encryption context keys serialized
232
+ # according to the encryption context serialization specification.
233
+ associated_data = header + required_ec_bytes ,
234
+ iv = header_auth_iv (algorithm ),
235
+ )
211
236
output = struct .pack (
212
237
">{iv_len}s{tag_len}s" .format (iv_len = algorithm .iv_len , tag_len = algorithm .tag_len ),
213
238
header_auth .iv ,
@@ -292,8 +317,7 @@ def serialize_header_auth(
292
317
:param required_encryption_context_bytes: Serialized encryption context items
293
318
for all items whose keys are in the required_encryption_context list.
294
319
This is ONLY processed if using the aws-cryptographic-materialproviders library
295
- AND its required encryption context CMM
296
- AND if using the v2 message format. (optional)
320
+ AND its required encryption context CMM. (optional)
297
321
:type required_encryption_context_bytes: bytes
298
322
:returns: Serialized header authentication data
299
323
:rtype: bytes
0 commit comments