def test_oversize_auth_tag_packet_encoding():
    packet = AuthTagPacket(
        tag=b"\x00" * TAG_SIZE,
        auth_tag=b"\x00" * NONCE_SIZE,
        encrypted_message=b"\x00" * (MAX_PACKET_SIZE - (1 + TAG_SIZE) - NONCE_SIZE + 1),
    )
    with pytest.raises(ValidationError):
        packet.to_wire_bytes()
def test_auth_tag_packet_encoding_decoding(tag, auth_tag, encrypted_message_size):
    encrypted_message = b"\x00" * encrypted_message_size
    original_packet = AuthTagPacket(
        tag=tag,
        auth_tag=auth_tag,
        encrypted_message=encrypted_message,
    )
    encoded_packet = original_packet.to_wire_bytes()
    decoded_packet = decode_message_packet(encoded_packet)
    assert isinstance(decoded_packet, AuthTagPacket)
    assert decoded_packet == original_packet
def test_random_packet_preparation(tag, auth_tag, random_data):
    packet = AuthTagPacket.prepare_random(
        tag=tag,
        auth_tag=auth_tag,
        random_data=random_data,
    )
    assert packet.tag == tag
    assert packet.auth_tag == auth_tag
    assert packet.encrypted_message == random_data
def test_auth_tag_message_decryption(tag, auth_tag, key, message):
    packet = AuthTagPacket.prepare(
        tag=tag,
        auth_tag=auth_tag,
        message=message,
        key=key,
    )

    decrypted_message = packet.decrypt_message(key)
    assert decrypted_message == message
Beispiel #5
0
    async def handle_outgoing_message_post_handshake(self,
                                                     outgoing_message: OutgoingMessage,
                                                     ) -> None:
        if not self.is_post_handshake:
            raise ValueError("Can only handle message post handshake")
        if self.session_keys is None:
            raise TypeError("session_keys are None even though handshake has been completed")

        packet = AuthTagPacket.prepare(
            tag=compute_tag(self.local_node_id, self.remote_node_id),
            auth_tag=get_random_auth_tag(),
            message=outgoing_message.message,
            key=self.session_keys.encryption_key,
        )
        outgoing_packet = OutgoingPacket(
            packet,
            outgoing_message.receiver_endpoint,
        )
        self.logger.debug("Sending %s", outgoing_message)
        await self.outgoing_packet_send_channel.send(outgoing_packet)
def test_auth_tag_packet_preparation(tag, auth_tag, key):
    message = PingMessage(
        request_id=5,
        enr_seq=3,
    )

    packet = AuthTagPacket.prepare(
        tag=tag,
        auth_tag=auth_tag,
        message=message,
        key=key,
    )
    assert packet.tag == tag
    assert packet.auth_tag == auth_tag
    decrypted_message = aesgcm_decrypt(
        key=key,
        nonce=auth_tag,
        cipher_text=packet.encrypted_message,
        authenticated_data=tag,
    )
    assert decrypted_message[0] == message.message_type
    assert rlp.decode(decrypted_message[1:], PingMessage) == message
Beispiel #7
0
    def __init__(
        self,
        *,
        local_private_key: bytes,
        local_enr: ENR,
        remote_enr: ENR,
        initial_message: BaseMessage,
    ) -> None:
        super().__init__(
            is_initiator=True,
            local_enr=local_enr,
            local_private_key=local_private_key,
            remote_node_id=remote_enr.node_id,
        )
        self.remote_enr = remote_enr
        self.initial_message = initial_message

        self.initiating_packet = AuthTagPacket.prepare_random(
            tag=self.tag,
            auth_tag=get_random_auth_tag(),
            random_data=get_random_encrypted_data(),
        )