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 9437918
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/payload_decode_garbage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
match tsp::cesr::decode_payload(data) {
match tsp::cesr::decode_payload::<&[u8]>(data) {
Ok(decoded) => {
let mut buf = Vec::new();
tsp::cesr::encode_payload(&decoded.payload, None, &mut buf).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/payload_encode_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fuzz_target!(|data: tsp::cesr::fuzzing::Wrapper| {
let mut buf = Vec::new();
match tsp::cesr::encode_payload(&data.0, None, &mut buf) {
Ok(()) => {
let result = tsp::cesr::decode_payload(&buf).unwrap();
let result = tsp::cesr::decode_payload::<&[u8]>(&buf).unwrap();

assert_eq!(data, result.payload);
}
Expand Down
36 changes: 24 additions & 12 deletions tsp/src/cesr/packet.rs
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions tsp/src/crypto/tsp_nacl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub(crate) fn open<'a>(
}

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 Down Expand Up @@ -199,7 +199,7 @@ pub(crate) fn open<'a>(
reply: &thread_id, ..
} => Payload::CancelRelationship { thread_id },
crate::cesr::Payload::NestedMessage(data) => Payload::NestedMessage(data),
crate::cesr::Payload::RoutedMessage(hops, data) => Payload::RoutedMessage(hops, data),
crate::cesr::Payload::RoutedMessage(hops, data) => Payload::RoutedMessage(hops, data as _),
};

Ok((envelope.nonconfidential_data, secret_payload, ciphertext))
Expand Down

0 comments on commit 9437918

Please sign in to comment.