def from_bytes(cls, data: bytes): box = unpack_dynamic('VVIVVVVV', data) raw_nodes: List[bytes] = unpack_byte_list(box[3]) nodes: List[TreeNode] = [] # if there are no nodes in the tree, the raw_nodes list contains just one empty entry if raw_nodes != [] and raw_nodes[0] != b'': for raw_node in raw_nodes: if raw_node == b"NOTHING": nodes.append(None) else: nodes.append(TreeNode.from_bytes(raw_node)) # pylint: disable=unexpected-keyword-arg inst = cls(protocol_version=box[0], group_id=box[1], epoch=box[2], tree=nodes, interim_transcript_hash=box[4], init_secret=box[5], key=box[6], nounce=box[7]) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('V', data) user_bytes: bytes = box[0] users = user_bytes.decode('UTF-8').split('\n') # pylint: disable=unexpected-keyword-arg return cls(user_names=users)
def test_normal_format_chars_unpack(): cases: Dict = {'c': [b'a'], 'L': [1337], '2s': [b'aa'], '2l': [1337, 7331]} for fmt_char, value in cases.items(): assert list( unpack_dynamic(fmt_char, struct.pack(MP_BYTE_ORDERING + fmt_char, *value))) == value
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('V', data) # pylint: disable=unexpected-keyword-arg inst: MLSPlaintextApplicationData = cls(application_data=box[0]) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('II', data) # pylint: disable=unexpected-keyword-arg inst: MLSSenderData = cls(sender=box[0], generation=box[1]) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('32sV', data) # pylint: disable=unexpected-keyword-arg inst: HPKECiphertext = cls(ephemeral_key=box[0], cipher_text=box[1]) if not inst.validate(): raise RuntimeError() return inst
def test_empty_vector(): packed = pack_dynamic('V', b'') assert len(packed) == struct.calcsize(MP_BYTE_ORDERING + 'L') assert struct.unpack(MP_BYTE_ORDERING + 'L', packed)[0] == 0 unpacked = unpack_dynamic('V', packed) assert len(unpacked) == 1 assert isinstance(unpacked[0], bytes) assert unpacked[0] == b''
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('IVV', data) # pylint: disable=unexpected-keyword-arg inst: AddMessage = cls(index=box[0], init_key=box[1], welcome_info_hash=box[2]) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes) -> 'TreeNode': box: tuple = unpack_dynamic('VVV', data) private_key: Optional[bytes] = box[1] if box[1] != b'' else None credential: Optional[bytes] = box[2] if box[2] != b'' else None inst: TreeNode = cls(public_key=box[0], private_key=private_key, credentials=credential) if not inst.validate(): raise RuntimeError() return inst
def test_stacked_payloads(): payload = b'' + pack_dynamic('V', b'a' * 32) + pack_dynamic('V', b'b' * 64) payload_payload = pack_dynamic('V', payload) unpacked_payload = unpack_dynamic('V', payload_payload)[0] assert payload == unpacked_payload payload_list = unpack_byte_list(unpacked_payload) assert len(payload_list) == 2 assert payload_list[0] == b'a' * 32 assert payload_list[1] == b'b' * 64
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('BV', data) msg_type = ChatProtocolMessageType(box[0]) if msg_type == ChatProtocolMessageType.MESSAGE: content = ChatMessage.from_bytes(box[1]) elif msg_type == ChatProtocolMessageType.USER_LIST: content = ChatUserListMessage.from_bytes(box[1]) else: raise ValueError() # pylint: disable=unexpected-keyword-arg return cls(msg_type=msg_type, contents=content)
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('IV', data) group_op: GroupOperation = GroupOperation.from_bytes(data=box[1]) # pylint: disable=unexpected-keyword-arg inst: MLSPlaintextHandshake = cls(confirmation=box[0], group_operation=group_op) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('VIBVVV', data) # pylint: disable=unexpected-keyword-arg inst: MLSCiphertext = cls(group_id=box[0], epoch=box[1], content_type=ContentType(box[2]), sender_data_nounce=box[3], encrypted_sender_data=box[4], ciphertext=box[5]) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): node_bytes: List[bytes] = unpack_byte_list( unpack_dynamic('V', data)[0]) direct_path: List[DirectPathNode] = [] for entry in node_bytes: direct_path.append(DirectPathNode.from_bytes(entry)) # pylint: disable=unexpected-keyword-arg inst: UpdateMessage = cls(direct_path=direct_path) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('32sV', data) public_key: bytes = box[0] encrypted_messages_bytes: List[bytes] = unpack_byte_list(box[1]) encrypted_messages: List[HPKECiphertext] = [] for entry in encrypted_messages_bytes: encrypted_messages.append(HPKECiphertext.from_bytes(entry)) # pylint: disable=unexpected-keyword-arg inst: DirectPathNode = cls(public_key=public_key, encrypted_path_secret=encrypted_messages) if not inst.validate(): raise RuntimeError() return inst
def test_pack_unpack_combinations(): dyn_payload = b'a' * 10 cases: Dict[string, List] = { 'V': [dyn_payload], 'Vc': [dyn_payload, b'a'], 'LLV': [1337, 7331, dyn_payload], 'LVL': [1337, b'b' * 16, 7331], '32sV': [b'a' * 32, pack_dynamic('V', b'a' * 72)] } for fmt_string, arguments in cases.items(): packed_unpacked_values = unpack_dynamic( fmt_string, pack_dynamic(fmt_string, *arguments)) for i, arg in enumerate(arguments): assert arg == packed_unpacked_values[i]
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('VIIBVV', data) content_bytes = box[4] content_type = ContentType(box[3]) if content_type == ContentType.HANDSHAKE: content = MLSPlaintextHandshake.from_bytes(content_bytes) elif content_type == ContentType.APPLICATION: content = MLSPlaintextApplicationData.from_bytes(content_bytes) else: raise RuntimeError() # pylint: disable=unexpected-keyword-arg inst: MLSPlaintext = cls(group_id=box[0], epoch=box[1], sender=box[2], content_type=content_type, content=content, signature=box[5]) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('BV', data) group_operation_type: GroupOperationType = GroupOperationType(box[0]) if group_operation_type == GroupOperationType.ADD: group_operation = AddMessage.from_bytes(data=box[1]) elif group_operation_type == GroupOperationType.UPDATE: group_operation = UpdateMessage.from_bytes(data=box[1]) elif group_operation_type == GroupOperationType.INIT: raise NotImplementedError() elif group_operation_type == GroupOperationType.REMOVE: raise NotImplementedError() else: raise ValueError() # pylint: disable=unexpected-keyword-arg inst: GroupOperation = cls(msg_type=group_operation_type, operation=group_operation) if not inst.validate(): raise RuntimeError() return inst
def from_bytes(cls, data: bytes): box: tuple = unpack_dynamic('V', data) message: str = box[0].decode('UTF-8') # pylint: disable=unexpected-keyword-arg return cls(message=message)