Skip to content

Commit 40fecc0

Browse files
all message format versions
1 parent aba7ccc commit 40fecc0

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/aws_encryption_sdk/internal/formatting/serialize.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,13 @@ def serialize_header(header, signer=None):
189189
raise SerializationError("Unrecognized message format version: {}".format(header.version))
190190

191191

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+
):
193199
"""Creates serialized header authentication data for messages in serialization version V1.
194200
195201
:param algorithm: Algorithm to use for encryption
@@ -198,16 +204,35 @@ def _serialize_header_auth_v1(algorithm, header, data_encryption_key, signer=Non
198204
:param bytes data_encryption_key: Data key with which to encrypt message
199205
:param signer: Cryptographic signer object (optional)
200206
: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
201212
:returns: Serialized header authentication data
202213
:rtype: bytes
203214
"""
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+
)
211236
output = struct.pack(
212237
">{iv_len}s{tag_len}s".format(iv_len=algorithm.iv_len, tag_len=algorithm.tag_len),
213238
header_auth.iv,
@@ -292,8 +317,7 @@ def serialize_header_auth(
292317
:param required_encryption_context_bytes: Serialized encryption context items
293318
for all items whose keys are in the required_encryption_context list.
294319
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)
297321
:type required_encryption_context_bytes: bytes
298322
:returns: Serialized header authentication data
299323
:rtype: bytes

0 commit comments

Comments
 (0)