Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Nov 29, 2024
2 parents 94abb22 + aa11a23 commit c28c98f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fiber-sphinx"
version = "2.0.0"
version = "2.1.0"
edition = "2021"
license-file = "COPYING.md"
description = "A Rust implementation of the Sphinx mix network."
Expand Down
37 changes: 33 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,17 @@ impl OnionErrorPacket {
/// the error packet.
///
/// The shared secret can be obtained via `OnionPacket::shared_secret`.
pub fn create(shared_secret: &[u8; 32], mut payload: Vec<u8>) -> Self {
pub fn create(shared_secret: &[u8; 32], payload: Vec<u8>) -> Self {
let ReturnKeys { ammag, um } = ReturnKeys::new(shared_secret);
let mut packet_data = compute_hmac(&um, &payload, None).to_vec();
packet_data.append(&mut payload);
let hmac = compute_hmac(&um, &payload, None);
Self::concat(hmac, payload).xor_cipher_stream_with_ammag(ammag)
}

(OnionErrorPacket { packet_data }).xor_cipher_stream_with_ammag(ammag)
/// Concatenates HMAC and the payload without encryption.
pub fn concat(hmac: [u8; 32], mut payload: Vec<u8>) -> Self {
let mut packet_data = hmac.to_vec();
packet_data.append(&mut payload);
OnionErrorPacket { packet_data }
}

fn xor_cipher_stream_with_ammag(self, ammag: [u8; 32]) -> Self {
Expand Down Expand Up @@ -356,6 +361,19 @@ impl OnionErrorPacket {
None
}

/// Splits into HMAC and payload without decryption.
pub fn split(self) -> ([u8; 32], Vec<u8>) {
let mut hmac = [0u8; 32];
if self.packet_data.len() >= 32 {
hmac.copy_from_slice(&self.packet_data[..32]);
let payload = self.packet_data[32..].to_vec();
(hmac, payload)
} else {
hmac.copy_from_slice(&self.packet_data[..]);
(hmac, Vec::new())
}
}

/// Converts the onion packet into a byte vector.
pub fn into_bytes(self) -> Vec<u8> {
self.packet_data
Expand Down Expand Up @@ -1057,4 +1075,15 @@ mod tests {
assert!(error.is_none());
}
}

#[test]
fn test_onion_error_packet_concat_split() {
let expected_hmac = [0x11; 32];
let expected_payload = vec![0x22];
let packet = OnionErrorPacket::concat(expected_hmac.clone(), expected_payload.clone());
let (hmac, payload) = packet.split();

assert_eq!(hmac, expected_hmac);
assert_eq!(payload, expected_payload);
}
}

0 comments on commit c28c98f

Please sign in to comment.