Skip to content

Commit

Permalink
add optional mutability to crypto layer (uses an unsafe hack)
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Schoolderman <[email protected]>
  • Loading branch information
tweedegolf-marc committed Jul 23, 2024
1 parent e391e0e commit bd41e3a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
40 changes: 26 additions & 14 deletions tsp/src/cesr/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ type Sha256Digest = crate::definitions::Digest;
#[repr(u32)]
#[derive(Debug)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(PartialEq, Eq, Clone))]
pub enum Payload<'a, Bytes: AsRef<[u8]>, Vid> {
pub enum Payload<'a, Bytes: AsRef<[u8]>, Vid, NestedBytes: AsRef<[u8]> = Bytes> {
/// A TSP message which consists only of a message which will be protected using HPKE
GenericMessage(Bytes),
/// A payload that consists of a TSP Envelope+Message
NestedMessage(Bytes),
NestedMessage(NestedBytes),
/// A routed payload; same as above but with routing information attached
RoutedMessage(Vec<Vid>, Bytes),
/// A TSP message requesting a relationship
Expand Down Expand Up @@ -299,13 +299,25 @@ fn decode_hops<'a, Vid: TryFrom<&'a [u8]>>(stream: &mut &'a [u8]) -> Result<Vec<
Ok(hop_list)
}

pub struct DecodedPayload<'a> {
pub payload: Payload<'a, &'a [u8], &'a [u8]>,
// "NestedBytes" to support both mutable and non-mutable data
/// A decoded payload + optional ESSR data
pub struct DecodedPayload<'a, Bytes: AsRef<[u8]> = &'a mut [u8]> {
pub payload: Payload<'a, Bytes, &'a [u8]>,
pub sender_identity: Option<&'a [u8]>,
}

// temporary
fn fudge(x: &[u8]) -> &mut [u8] {
unsafe { std::mem::transmute(x as *const _) }
}

mod from_mut;
use from_mut::FromMut;

/// Decode a TSP Payload
pub fn decode_payload(mut stream: &[u8]) -> Result<DecodedPayload, DecodeError> {
pub fn decode_payload<'a, Bytes: AsRef<[u8]> + FromMut<'a, [u8]>>(
mut stream: &'a [u8],
) -> Result<DecodedPayload<'a, Bytes>, DecodeError> {
let sender_identity = match decode_count(TSP_PAYLOAD, &mut stream) {
Some(2) => Some(
decode_variable_data(TSP_DEVELOPMENT_VID, &mut stream)
Expand All @@ -321,10 +333,11 @@ pub fn decode_payload(mut stream: &[u8]) -> Result<DecodedPayload, DecodeError>
msgtype::GEN_MSG => {
let hop_list = decode_hops(&mut stream)?;
if hop_list.is_empty() {
decode_variable_data(TSP_PLAINTEXT, &mut stream).map(Payload::GenericMessage)
decode_variable_data(TSP_PLAINTEXT, &mut stream)
.map(|msg| Payload::GenericMessage(FromMut::from_mut(fudge(msg))))
} else {
decode_variable_data(TSP_PLAINTEXT, &mut stream)
.map(|msg| Payload::RoutedMessage(hop_list, msg))
.map(|msg| Payload::RoutedMessage(hop_list, FromMut::from_mut(fudge(msg))))
}
}
msgtype::NEW_REL => {
Expand All @@ -335,9 +348,8 @@ pub fn decode_payload(mut stream: &[u8]) -> Result<DecodedPayload, DecodeError>
hops: hop_list,
})
}
msgtype::NEST_MSG => {
decode_variable_data(TSP_PLAINTEXT, &mut stream).map(Payload::NestedMessage)
}
msgtype::NEST_MSG => decode_variable_data(TSP_PLAINTEXT, &mut stream)
.map(|msg| Payload::NestedMessage(FromMut::from_mut(fudge(msg)))),
msgtype::NEW_REL_REPLY => decode_fixed_data(TSP_SHA256, &mut stream)
.map(|reply| Payload::DirectRelationAffirm { reply }),
msgtype::NEW_NEST_REL => {
Expand Down Expand Up @@ -933,8 +945,8 @@ mod test {
assert_eq!(env.receiver, Some(&b"Bobbi"[..]));
assert_eq!(env.nonconfidential_data, None);

let DecodedPayload {
payload: Payload::<_, &[u8]>::GenericMessage(data),
let DecodedPayload::<&[u8]> {
payload: Payload::GenericMessage(data),
..
} = decode_payload(dummy_crypt(ciphertext.unwrap())).unwrap()
else {
Expand Down Expand Up @@ -982,8 +994,8 @@ mod test {
assert_eq!(env.receiver, Some(&b"Bobbi"[..]));
assert_eq!(env.nonconfidential_data, Some(&b"treasure"[..]));

let DecodedPayload {
payload: Payload::<_, &[u8]>::GenericMessage(data),
let DecodedPayload::<&[u8]> {
payload: Payload::GenericMessage(data),
..
} = decode_payload(dummy_crypt(ciphertext.unwrap())).unwrap()
else {
Expand Down
17 changes: 17 additions & 0 deletions tsp/src/cesr/packet/from_mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// A little trait to support a limited form of 'mut polymorphism'
pub trait FromMut<'a, T: ?Sized> {
fn from_mut(x: &'a mut T) -> Self;
}

impl<'a, T: ?Sized> FromMut<'a, T> for &'a T {
fn from_mut(x: &'a mut T) -> Self {
x
}
}

impl<'a, T: ?Sized> FromMut<'a, T> for &'a mut T {
fn from_mut(x: &'a mut T) -> Self {
x
}
}
6 changes: 3 additions & 3 deletions tsp/src/crypto/tsp_hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ where
}

let secret_payload = match payload {
crate::cesr::Payload::GenericMessage(data) => Payload::Content(data),
crate::cesr::Payload::GenericMessage(data) => Payload::Content(data as _),
crate::cesr::Payload::DirectRelationProposal { hops, .. } => Payload::RequestRelationship {
route: if hops.is_empty() { None } else { Some(hops) },
},
Expand All @@ -249,8 +249,8 @@ where
crate::cesr::Payload::RelationshipCancel {
reply: &thread_id, ..
} => Payload::CancelRelationship { thread_id },
crate::cesr::Payload::NestedMessage(data) => todo!(), // Payload::NestedMessage(data),
crate::cesr::Payload::RoutedMessage(hops, data) => Payload::RoutedMessage(hops, data),
crate::cesr::Payload::NestedMessage(data) => Payload::NestedMessage(data),
crate::cesr::Payload::RoutedMessage(hops, data) => Payload::RoutedMessage(hops, data as _),
crate::cesr::Payload::NewIdentifierProposal {
thread_id: &thread_id,
new_vid,
Expand Down

0 comments on commit bd41e3a

Please sign in to comment.