Skip to content

Commit

Permalink
Merge pull request #28 from openwallet-foundation-labs/nonce_removal
Browse files Browse the repository at this point in the history
remove nonce from REL_CANCEL, add nonce to NEW_NEST_REL
  • Loading branch information
tweedegolf-marc authored Jul 9, 2024
2 parents 92c67a7 + 1e4dfd4 commit 7059465
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 48 deletions.
36 changes: 16 additions & 20 deletions tsp/src/cesr/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@ pub enum Payload<'a, Bytes: AsRef<[u8]>, Vid> {
/// A TSP message confiming a relationship
DirectRelationAffirm { reply: &'a Sha256Digest },
/// A TSP message requesting a nested relationship
NestedRelationProposal { new_vid: Vid },
NestedRelationProposal { nonce: Nonce, new_vid: Vid },
/// A TSP message confiming a relationship
NestedRelationAffirm {
new_vid: Vid,
connect_to_vid: Vid,
reply: &'a Sha256Digest,
},
/// A TSP cancellation message
RelationshipCancel {
nonce: Nonce,
reply: &'a Sha256Digest,
},
RelationshipCancel { reply: &'a Sha256Digest },
}

impl<'a, Bytes: AsRef<[u8]>, Vid> Payload<'a, Bytes, Vid> {
Expand Down Expand Up @@ -217,9 +214,10 @@ pub fn encode_payload(
encode_fixed_data(TSP_TYPECODE, &msgtype::NEW_REL_REPLY, output);
encode_fixed_data(TSP_SHA256, reply.as_slice(), output);
}
Payload::NestedRelationProposal { new_vid } => {
Payload::NestedRelationProposal { new_vid, nonce } => {
encode_fixed_data(TSP_TYPECODE, &msgtype::NEW_NEST_REL, output);
checked_encode_variable_data(TSP_DEVELOPMENT_VID, new_vid.as_ref(), output)?;
encode_fixed_data(TSP_NONCE, &nonce.0, output);
}
Payload::NestedRelationAffirm {
new_vid,
Expand All @@ -231,9 +229,8 @@ pub fn encode_payload(
checked_encode_variable_data(TSP_DEVELOPMENT_VID, connect_to_vid.as_ref(), output)?;
encode_fixed_data(TSP_SHA256, reply.as_slice(), output);
}
Payload::RelationshipCancel { nonce, reply } => {
Payload::RelationshipCancel { reply } => {
encode_fixed_data(TSP_TYPECODE, &msgtype::REL_CANCEL, output);
encode_fixed_data(TSP_NONCE, &nonce.0, output);
encode_fixed_data(TSP_SHA256, reply.as_slice(), output);
}
}
Expand Down Expand Up @@ -323,7 +320,10 @@ pub fn decode_payload(mut stream: &[u8]) -> Result<DecodedPayload, DecodeError>
let new_vid = decode_variable_data(TSP_DEVELOPMENT_VID, &mut stream)
.ok_or(DecodeError::UnexpectedData)?;

Some(Payload::NestedRelationProposal { new_vid })
decode_fixed_data(TSP_NONCE, &mut stream).map(|nonce| Payload::NestedRelationProposal {
new_vid,
nonce: Nonce(*nonce),
})
}
msgtype::NEW_NEST_REL_REPLY => {
let new_vid = decode_variable_data(TSP_DEVELOPMENT_VID, &mut stream)
Expand All @@ -337,12 +337,8 @@ pub fn decode_payload(mut stream: &[u8]) -> Result<DecodedPayload, DecodeError>
reply,
})
}
msgtype::REL_CANCEL => decode_fixed_data(TSP_NONCE, &mut stream).and_then(|nonce| {
decode_fixed_data(TSP_SHA256, &mut stream).map(|reply| Payload::RelationshipCancel {
nonce: Nonce(*nonce),
reply,
})
}),
msgtype::REL_CANCEL => decode_fixed_data(TSP_SHA256, &mut stream)
.map(|reply| Payload::RelationshipCancel { reply }),
_ => return Err(DecodeError::UnexpectedMsgType),
};

Expand Down Expand Up @@ -1171,17 +1167,17 @@ mod test {
});
test_turn_around(Payload::DirectRelationAffirm { reply: nonce });
let new_vid = &[];
test_turn_around(Payload::NestedRelationProposal { new_vid });
test_turn_around(Payload::NestedRelationProposal {
new_vid,
nonce: Nonce(*nonce),
});
test_turn_around(Payload::NestedRelationAffirm {
new_vid,
connect_to_vid: new_vid,
reply: nonce,
});

test_turn_around(Payload::RelationshipCancel {
reply: nonce,
nonce: Nonce(*nonce),
});
test_turn_around(Payload::RelationshipCancel { reply: nonce });
}

#[test]
Expand Down
30 changes: 14 additions & 16 deletions tsp/src/cesr/packet/fuzzing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ impl<'a> arbitrary::Arbitrary<'a> for Wrapper {
},
Variants::DirectRelationAffirm => Payload::DirectRelationAffirm { reply: &DIGEST },
Variants::NestedRelationProposal => Payload::NestedRelationProposal {
nonce: Nonce(Arbitrary::arbitrary(u)?),
new_vid: Arbitrary::arbitrary(u)?,
},
Variants::NestedRelationAffirm => Payload::NestedRelationAffirm {
reply: &DIGEST,
new_vid: Arbitrary::arbitrary(u)?,
connect_to_vid: Arbitrary::arbitrary(u)?,
},
Variants::RelationshipCancel => Payload::RelationshipCancel {
nonce: Nonce(Arbitrary::arbitrary(u)?),
reply: &DIGEST,
},
Variants::RelationshipCancel => Payload::RelationshipCancel { reply: &DIGEST },
};

Ok(Wrapper(payload))
Expand Down Expand Up @@ -97,9 +95,15 @@ impl<'a> PartialEq<Payload<'a, &'a [u8], &'a [u8]>> for Wrapper {
Payload::DirectRelationAffirm { reply: r_reply },
) => l_reply == r_reply,
(
Payload::NestedRelationProposal { new_vid: l_vid },
Payload::NestedRelationProposal { new_vid: r_vid },
) => l_vid == r_vid,
Payload::NestedRelationProposal {
new_vid: l_vid,
nonce: l_nonce,
},
Payload::NestedRelationProposal {
new_vid: r_vid,
nonce: r_nonce,
},
) => l_nonce.0 == r_nonce.0 && l_vid == r_vid,
(
Payload::NestedRelationAffirm {
reply: l_reply,
Expand All @@ -114,15 +118,9 @@ impl<'a> PartialEq<Payload<'a, &'a [u8], &'a [u8]>> for Wrapper {
) => l_reply == r_reply && l_vid == r_vid && l_vid2 == r_vid2,

(
Payload::RelationshipCancel {
nonce: l_nonce,
reply: l_reply,
},
Payload::RelationshipCancel {
nonce: r_nonce,
reply: r_reply,
},
) => l_nonce.0 == r_nonce.0 && l_reply == r_reply,
Payload::RelationshipCancel { reply: l_reply },
Payload::RelationshipCancel { reply: r_reply },
) => l_reply == r_reply,
_ => false,
}
}
Expand Down
14 changes: 8 additions & 6 deletions tsp/src/crypto/tsp_hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ where
crate::cesr::Payload::DirectRelationAffirm { reply: thread_id }
}
Payload::RequestNestedRelationship { vid } => {
crate::cesr::Payload::NestedRelationProposal { new_vid: vid }
crate::cesr::Payload::NestedRelationProposal {
nonce: fresh_nonce(&mut csprng),
new_vid: vid,
}
}
Payload::AcceptNestedRelationship {
ref thread_id,
Expand All @@ -55,10 +58,9 @@ where
new_vid: vid,
connect_to_vid,
},
Payload::CancelRelationship { ref thread_id } => crate::cesr::Payload::RelationshipCancel {
nonce: fresh_nonce(&mut csprng),
reply: thread_id,
},
Payload::CancelRelationship { ref thread_id } => {
crate::cesr::Payload::RelationshipCancel { reply: thread_id }
}
Payload::NestedMessage(data) => crate::cesr::Payload::NestedMessage(data),
Payload::RoutedMessage(hops, data) => crate::cesr::Payload::RoutedMessage(hops, data),
};
Expand Down Expand Up @@ -215,7 +217,7 @@ where
crate::cesr::Payload::DirectRelationAffirm { reply: &thread_id } => {
Payload::AcceptRelationship { thread_id }
}
crate::cesr::Payload::NestedRelationProposal { new_vid } => {
crate::cesr::Payload::NestedRelationProposal { new_vid, .. } => {
Payload::RequestNestedRelationship { vid: new_vid }
}
crate::cesr::Payload::NestedRelationAffirm {
Expand Down
14 changes: 8 additions & 6 deletions tsp/src/crypto/tsp_nacl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ pub(crate) fn seal(
crate::cesr::Payload::DirectRelationAffirm { reply: thread_id }
}
Payload::RequestNestedRelationship { vid } => {
crate::cesr::Payload::NestedRelationProposal { new_vid: vid }
crate::cesr::Payload::NestedRelationProposal {
nonce: fresh_nonce(&mut csprng),
new_vid: vid,
}
}
Payload::AcceptNestedRelationship {
ref thread_id,
Expand All @@ -53,10 +56,9 @@ pub(crate) fn seal(
new_vid: vid,
connect_to_vid,
},
Payload::CancelRelationship { ref thread_id } => crate::cesr::Payload::RelationshipCancel {
nonce: fresh_nonce(&mut csprng),
reply: thread_id,
},
Payload::CancelRelationship { ref thread_id } => {
crate::cesr::Payload::RelationshipCancel { reply: thread_id }
}
Payload::NestedMessage(data) => crate::cesr::Payload::NestedMessage(data),
Payload::RoutedMessage(hops, data) => crate::cesr::Payload::RoutedMessage(hops, data),
};
Expand Down Expand Up @@ -167,7 +169,7 @@ pub(crate) fn open<'a>(
crate::cesr::Payload::DirectRelationAffirm { reply: &thread_id } => {
Payload::AcceptRelationship { thread_id }
}
crate::cesr::Payload::NestedRelationProposal { new_vid } => {
crate::cesr::Payload::NestedRelationProposal { new_vid, .. } => {
Payload::RequestNestedRelationship { vid: new_vid }
}
crate::cesr::Payload::NestedRelationAffirm {
Expand Down

0 comments on commit 7059465

Please sign in to comment.