def test_message_frame_body_fails(iv, ciphertext, tag, sequence_number, final_frame): with pytest.raises(TypeError): MessageFrameBody(iv=iv, ciphertext=ciphertext, tag=tag, sequence_number=sequence_number, final_frame=final_frame)
def deserialize_frame(stream, header, verifier=None): """Deserializes a frame from a body. :param stream: Source data stream :type stream: io.BytesIO :param header: Deserialized header :type header: aws_encryption_sdk.structures.MessageHeader :param verifier: Signature verifier object (optional) :type verifier: aws_encryption_sdk.internal.crypto.Verifier :returns: Deserialized frame and a boolean stating if this is the final frame :rtype: :class:`aws_encryption_sdk.internal.structures.MessageFrameBody` and bool """ _LOGGER.debug("Starting frame deserialization") frame_data = {} final_frame = False (sequence_number, ) = unpack_values(">I", stream, verifier) if sequence_number == SequenceIdentifier.SEQUENCE_NUMBER_END.value: _LOGGER.debug("Deserializing final frame") (sequence_number, ) = unpack_values(">I", stream, verifier) final_frame = True else: _LOGGER.debug("Deserializing frame sequence number %d", int(sequence_number)) frame_data["final_frame"] = final_frame frame_data["sequence_number"] = sequence_number (frame_iv, ) = unpack_values( ">{iv_len}s".format(iv_len=header.algorithm.iv_len), stream, verifier) frame_data["iv"] = frame_iv if final_frame is True: (content_length, ) = unpack_values(">I", stream, verifier) if content_length >= header.frame_length: raise SerializationError( "Invalid final frame length: {final} >= {normal}".format( final=content_length, normal=header.frame_length)) else: content_length = header.frame_length (frame_content, frame_tag) = unpack_values( ">{content_len}s{auth_len}s".format( content_len=content_length, auth_len=header.algorithm.auth_len), stream, verifier, ) frame_data["ciphertext"] = frame_content frame_data["tag"] = frame_tag return MessageFrameBody(**frame_data), final_frame
), ) VALUES["deserialized_header_auth_block"] = MessageHeaderAuthentication( iv=VALUES["header_auth_base"].iv, tag=VALUES["header_auth_base"].tag) VALUES["deserialized_header_auth_block_v2"] = MessageHeaderAuthentication( iv=b"\x00" * 12, tag=VALUES["header_auth_base"].tag) VALUES["deserialized_body_block"] = MessageNoFrameBody( iv=VALUES["non_framed_base"].iv, ciphertext=VALUES["non_framed_base"].ciphertext, tag=VALUES["non_framed_base"].tag) VALUES["deserialized_footer"] = MessageFooter(VALUES["signature"]) VALUES["deserialized_empty_footer"] = MessageFooter(b"") VALUES["deserialized_body_final_frame_single"] = MessageFrameBody( iv=VALUES["final_frame_base"].iv, ciphertext=VALUES["final_frame_base"].ciphertext, tag=VALUES["final_frame_base"].tag, sequence_number=1, final_frame=True, ) VALUES["deserialized_body_frame_1"] = MessageFrameBody( iv=VALUES["frame_base"].iv, ciphertext=VALUES["frame_base"].ciphertext, tag=VALUES["frame_base"].tag, sequence_number=1, final_frame=False, ) VALUES["deserialized_body_frame_2"] = MessageFrameBody( iv=VALUES["frame_base"].iv, ciphertext=VALUES["frame_base"].ciphertext, tag=VALUES["frame_base"].tag, sequence_number=2,
) VALUES['deserialized_header_auth_block'] = MessageHeaderAuthentication( iv=VALUES['header_auth_base'].iv, tag=VALUES['header_auth_base'].tag ) VALUES['deserialized_body_block'] = MessageNoFrameBody( iv=VALUES['non_framed_base'].iv, ciphertext=VALUES['non_framed_base'].ciphertext, tag=VALUES['non_framed_base'].tag ) VALUES['deserialized_footer'] = MessageFooter(VALUES['signature']) VALUES['deserialized_empty_footer'] = MessageFooter(b'') VALUES['deserialized_body_final_frame_single'] = MessageFrameBody( iv=VALUES['final_frame_base'].iv, ciphertext=VALUES['final_frame_base'].ciphertext, tag=VALUES['final_frame_base'].tag, sequence_number=1, final_frame=True ) VALUES['deserialized_body_frame_1'] = MessageFrameBody( iv=VALUES['frame_base'].iv, ciphertext=VALUES['frame_base'].ciphertext, tag=VALUES['frame_base'].tag, sequence_number=1, final_frame=False ) VALUES['deserialized_body_frame_2'] = MessageFrameBody( iv=VALUES['frame_base'].iv, ciphertext=VALUES['frame_base'].ciphertext, tag=VALUES['frame_base'].tag, sequence_number=2,
def test_message_frame_body_succeeds(): MessageFrameBody(iv=b'iv', ciphertext=b'ciphertext', tag=b'tag', sequence_number=1, final_frame=False)