Remove wrong encoding
This commit is contained in:
parent
c1f6054b3c
commit
cb4e1115c6
@ -3,7 +3,7 @@ from typing import cast
|
|||||||
from noise.connection import NoiseConnection as NoiseState
|
from noise.connection import NoiseConnection as NoiseState
|
||||||
|
|
||||||
from libp2p.io.abc import EncryptedMsgReadWriter, MsgReadWriteCloser, ReadWriteCloser
|
from libp2p.io.abc import EncryptedMsgReadWriter, MsgReadWriteCloser, ReadWriteCloser
|
||||||
from libp2p.io.msgio import FixedSizeLenMsgReadWriter, encode_msg_with_length
|
from libp2p.io.msgio import FixedSizeLenMsgReadWriter
|
||||||
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
from libp2p.network.connection.raw_connection_interface import IRawConnection
|
||||||
|
|
||||||
SIZE_NOISE_MESSAGE_LEN = 2
|
SIZE_NOISE_MESSAGE_LEN = 2
|
||||||
@ -15,37 +15,12 @@ BYTE_ORDER = "big"
|
|||||||
# | Noise packet |
|
# | Noise packet |
|
||||||
# < 2 bytes -><- 65535 ->
|
# < 2 bytes -><- 65535 ->
|
||||||
# | noise msg len | noise msg |
|
# | noise msg len | noise msg |
|
||||||
# | body len | body | padding |
|
|
||||||
# <-2 bytes-><- max=65533 bytes ->
|
|
||||||
|
|
||||||
|
|
||||||
class NoisePacketReadWriter(FixedSizeLenMsgReadWriter):
|
class NoisePacketReadWriter(FixedSizeLenMsgReadWriter):
|
||||||
size_len_bytes = SIZE_NOISE_MESSAGE_LEN
|
size_len_bytes = SIZE_NOISE_MESSAGE_LEN
|
||||||
|
|
||||||
|
|
||||||
def encode_msg_body(msg_body: bytes) -> bytes:
|
|
||||||
encoded_msg_body = encode_msg_with_length(msg_body, SIZE_NOISE_MESSAGE_BODY_LEN)
|
|
||||||
if len(encoded_msg_body) > MAX_NOISE_MESSAGE_BODY_LEN:
|
|
||||||
raise ValueError(
|
|
||||||
f"msg_body is too long: {len(msg_body)}, "
|
|
||||||
f"maximum={MAX_NOISE_MESSAGE_BODY_LEN}"
|
|
||||||
)
|
|
||||||
# NOTE: Improvements:
|
|
||||||
# 1. Senders *SHOULD* use a source of random data to populate the padding field.
|
|
||||||
# 2. and *may* use any length of padding that does not cause the total length of
|
|
||||||
# the Noise message to exceed 65535 bytes.
|
|
||||||
# Ref: https://github.com/libp2p/specs/tree/master/noise#encrypted-payloads
|
|
||||||
return encoded_msg_body # + padding
|
|
||||||
|
|
||||||
|
|
||||||
def decode_msg_body(noise_msg: bytes) -> bytes:
|
|
||||||
len_body = int.from_bytes(noise_msg[:SIZE_NOISE_MESSAGE_BODY_LEN], BYTE_ORDER)
|
|
||||||
# Just ignore the padding
|
|
||||||
return noise_msg[
|
|
||||||
SIZE_NOISE_MESSAGE_BODY_LEN : (SIZE_NOISE_MESSAGE_BODY_LEN + len_body)
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class BaseNoiseMsgReadWriter(EncryptedMsgReadWriter):
|
class BaseNoiseMsgReadWriter(EncryptedMsgReadWriter):
|
||||||
"""
|
"""
|
||||||
The base implementation of noise message reader/writer.
|
The base implementation of noise message reader/writer.
|
||||||
@ -62,14 +37,18 @@ class BaseNoiseMsgReadWriter(EncryptedMsgReadWriter):
|
|||||||
self.noise_state = noise_state
|
self.noise_state = noise_state
|
||||||
|
|
||||||
async def write_msg(self, data: bytes) -> None:
|
async def write_msg(self, data: bytes) -> None:
|
||||||
noise_msg = encode_msg_body(data)
|
data_encrypted = self.encrypt(data)
|
||||||
data_encrypted = self.encrypt(noise_msg)
|
# FIXME: Decide whether this prefix should be added or not.
|
||||||
|
# if not first:
|
||||||
|
# data_encrypted = b"\x00" * 32 + data_encrypted
|
||||||
await self.read_writer.write_msg(data_encrypted)
|
await self.read_writer.write_msg(data_encrypted)
|
||||||
|
|
||||||
async def read_msg(self) -> bytes:
|
async def read_msg(self) -> bytes:
|
||||||
noise_msg_encrypted = await self.read_writer.read_msg()
|
noise_msg_encrypted = await self.read_writer.read_msg()
|
||||||
noise_msg = self.decrypt(noise_msg_encrypted)
|
# FIXME: Decide whether this prefix should be added or not.
|
||||||
return decode_msg_body(noise_msg)
|
# if not first:
|
||||||
|
# noise_msg_encrypted = noise_msg_encrypted[32:]
|
||||||
|
return self.decrypt(noise_msg_encrypted)
|
||||||
|
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
await self.read_writer.close()
|
await self.read_writer.close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user