This commit is contained in:
Jędrzej Stuczyński
2026-02-27 08:12:30 +00:00
parent 1ddbb970d0
commit 1c8178a966
3 changed files with 103 additions and 108 deletions
-1
View File
@@ -68,7 +68,6 @@ pub enum LpError {
// /// Ed25519 to X25519 conversion error.
// #[error("Ed25519 key conversion error: {0}")]
// Ed25519RecoveryError(#[from] Ed25519RecoveryError),
#[error("attempted to create an LP responder without providing a valid KEM keys")]
ResponderWithMissingKEMKeys,
+103 -7
View File
@@ -1,17 +1,19 @@
// Copyright 2026 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
pub mod error;
pub mod header;
pub mod message;
pub mod packet;
pub mod replay;
pub mod utils;
use crate::packet::utils::format_debug_bytes;
use bytes::{BufMut, BytesMut};
use std::fmt::{Debug, Formatter};
pub use error::MalformedLpPacketError;
pub use header::{InnerHeader, LpHeader, OuterHeader};
pub use message::{ApplicationData, ForwardPacketData, LpMessage, MessageType};
pub use packet::{EncryptedLpPacket, LpPacket};
pub mod error;
pub mod header;
pub mod message;
pub mod replay;
pub mod utils;
pub mod version {
/// The current version of the Lewes Protocol that is put into each new constructed header.
@@ -28,3 +30,97 @@ pub(crate) const MTU: usize = 1500;
pub(crate) const UDP_OVERHEAD: usize = UDP_HEADER_LEN + IP_HEADER_LEN;
#[allow(dead_code)]
pub(crate) const UDP_PAYLOAD_SIZE: usize = MTU - UDP_OVERHEAD;
#[derive(Clone)]
pub struct EncryptedLpPacket {
// The outer header that's sent in plaintext
pub(crate) outer_header: OuterHeader,
// The ciphertext containing the inner header and the payload
pub(crate) ciphertext: Vec<u8>,
}
impl std::fmt::Debug for EncryptedLpPacket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format_debug_bytes(&self.debug_bytes())?)
}
}
impl EncryptedLpPacket {
pub fn new(outer_header: OuterHeader, ciphertext: Vec<u8>) -> EncryptedLpPacket {
EncryptedLpPacket {
outer_header,
ciphertext,
}
}
pub fn encoded_length(&self) -> usize {
OuterHeader::SIZE + self.ciphertext.len()
}
pub(crate) fn debug_bytes(&self) -> Vec<u8> {
let mut bytes = BytesMut::new();
self.encode(&mut bytes);
bytes.freeze().to_vec()
}
pub fn encode(&self, dst: &mut BytesMut) {
self.outer_header.encode(dst);
dst.put_slice(&self.ciphertext)
}
pub fn ciphertext(&self) -> &[u8] {
&self.ciphertext
}
pub fn outer_header(&self) -> OuterHeader {
self.outer_header
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct LpPacket {
pub(crate) header: LpHeader,
pub(crate) message: LpMessage,
}
impl Debug for LpPacket {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format_debug_bytes(&self.debug_bytes())?)
}
}
impl LpPacket {
pub fn new(header: LpHeader, message: LpMessage) -> Self {
Self { header, message }
}
pub fn typ(&self) -> MessageType {
self.message.typ()
}
pub fn message(&self) -> &LpMessage {
&self.message
}
pub fn into_message(self) -> LpMessage {
self.message
}
pub fn header(&self) -> &LpHeader {
&self.header
}
pub(crate) fn debug_bytes(&self) -> Vec<u8> {
let mut bytes = BytesMut::new();
self.dbg_encode(&mut bytes);
bytes.freeze().to_vec()
}
pub(crate) fn dbg_encode(&self, dst: &mut BytesMut) {
self.header.dbg_encode(dst);
dst.put_slice(&(self.message.typ() as u16).to_le_bytes());
self.message.encode_content(dst);
}
}
-100
View File
@@ -1,100 +0,0 @@
// Copyright 2026 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::packet::{LpHeader, LpMessage, MessageType, OuterHeader, utils::format_debug_bytes};
use bytes::{BufMut, BytesMut};
use std::fmt::{Debug, Formatter};
#[derive(Clone)]
pub struct EncryptedLpPacket {
// The outer header that's sent in plaintext
pub(crate) outer_header: OuterHeader,
// The ciphertext containing the inner header and the payload
pub(crate) ciphertext: Vec<u8>,
}
impl std::fmt::Debug for EncryptedLpPacket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format_debug_bytes(&self.debug_bytes())?)
}
}
impl EncryptedLpPacket {
pub fn new(outer_header: OuterHeader, ciphertext: Vec<u8>) -> EncryptedLpPacket {
EncryptedLpPacket {
outer_header,
ciphertext,
}
}
pub fn encoded_length(&self) -> usize {
OuterHeader::SIZE + self.ciphertext.len()
}
pub(crate) fn debug_bytes(&self) -> Vec<u8> {
let mut bytes = BytesMut::new();
self.encode(&mut bytes);
bytes.freeze().to_vec()
}
pub fn encode(&self, dst: &mut BytesMut) {
self.outer_header.encode(dst);
dst.put_slice(&self.ciphertext)
}
pub fn ciphertext(&self) -> &[u8] {
&self.ciphertext
}
pub fn outer_header(&self) -> OuterHeader {
self.outer_header
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct LpPacket {
pub(crate) header: LpHeader,
pub(crate) message: LpMessage,
}
impl Debug for LpPacket {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", format_debug_bytes(&self.debug_bytes())?)
}
}
impl LpPacket {
pub fn new(header: LpHeader, message: LpMessage) -> Self {
Self { header, message }
}
pub fn typ(&self) -> MessageType {
self.message.typ()
}
pub fn message(&self) -> &LpMessage {
&self.message
}
pub fn into_message(self) -> LpMessage {
self.message
}
pub fn header(&self) -> &LpHeader {
&self.header
}
pub(crate) fn debug_bytes(&self) -> Vec<u8> {
let mut bytes = BytesMut::new();
self.dbg_encode(&mut bytes);
bytes.freeze().to_vec()
}
pub(crate) fn dbg_encode(&self, dst: &mut BytesMut) {
self.header.dbg_encode(dst);
dst.put_slice(&(self.message.typ() as u16).to_le_bytes());
self.message.encode_content(dst);
}
}