From b336a0d2641112a179288a66924cace09ae9c248 Mon Sep 17 00:00:00 2001 From: Prabhpreet Dua <615318+prabhpreet@users.noreply.github.com> Date: Tue, 12 Dec 2023 07:24:08 +0530 Subject: [PATCH] Separate cookie message from envelope encapsulation, remove mac, cookie field --- rosenpass/benches/handshake.rs | 3 ++- rosenpass/src/app_server.rs | 19 +-------------- rosenpass/src/msgs.rs | 18 +++++++++++--- rosenpass/src/protocol.rs | 43 ++++++++++++++++++++-------------- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/rosenpass/benches/handshake.rs b/rosenpass/benches/handshake.rs index 0ab1c1fb..1a46df53 100644 --- a/rosenpass/benches/handshake.rs +++ b/rosenpass/benches/handshake.rs @@ -3,6 +3,7 @@ use rosenpass::pqkem::KEM; use rosenpass::{ pqkem::StaticKEM, protocol::{CryptoServer, HandleMsgResult, MsgBuf, PeerPtr, SPk, SSk, SymKey}, + sodium::sodium_init, }; use criterion::{black_box, criterion_group, criterion_main, Criterion}; @@ -57,7 +58,7 @@ fn make_server_pair() -> Result<(CryptoServer, CryptoServer)> { } fn criterion_benchmark(c: &mut Criterion) { - rosenpass_sodium::init().unwrap(); + sodium_init().unwrap(); let (mut a, mut b) = make_server_pair().unwrap(); c.bench_function("cca_secret_alloc", |bench| { bench.iter(|| { diff --git a/rosenpass/src/app_server.rs b/rosenpass/src/app_server.rs index 9c5d4a04..0e49e397 100644 --- a/rosenpass/src/app_server.rs +++ b/rosenpass/src/app_server.rs @@ -615,23 +615,6 @@ impl AppServer { .first() .ok_or(anyhow::anyhow!("No socket address for endpoint"))?; - //TODO: Modify AppPeer to store endpoints to enable O(1) search (hash) for addresses - //If no endpoint matches address, return error - //Currently, there is no sensible mechanism to map "anonymous" endpoints to PeerPtr - let index = self - .peers - .iter() - .enumerate() - .find(|(_num, p)| { - if let Some(ep) = p.endpoint() { - ep.addresses().contains(socket_addr) - } else { - false - } - }) - .ok_or(anyhow::anyhow!("Received message from unknown endpoint"))? - .0; - let mut len = 0; let mut ip_addr_port = [0u8; 18]; @@ -650,7 +633,7 @@ impl AppServer { len += 2; self.crypt - .handle_msg_under_load(&rx[..len], &mut *tx, PeerPtr(index), &ip_addr_port[..len]) + .handle_msg_under_load(&rx[..len], &mut *tx, &ip_addr_port[..len]) } pub fn output_key( diff --git a/rosenpass/src/msgs.rs b/rosenpass/src/msgs.rs index 37d0ab31..700d6d7c 100644 --- a/rosenpass/src/msgs.rs +++ b/rosenpass/src/msgs.rs @@ -49,6 +49,8 @@ use rosenpass_cipher_traits::Kem; use rosenpass_ciphers::kem::{EphemeralKem, StaticKem}; use rosenpass_ciphers::{aead, xaead, KEY_LEN}; use rosenpass_lenses::{lense, LenseView}; +pub const MSG_SIZE_LEN: usize = 1; +pub const RESERVED_LEN: usize = 3; pub const MAC_SIZE: usize = 16; pub const COOKIE_SIZE: usize = 16; pub const SID_LEN: usize = 4; @@ -57,9 +59,9 @@ pub const SID_LEN: usize = 4; lense! { Envelope := /// [MsgType] of this message - msg_type: 1, + msg_type: MSG_SIZE_LEN, /// Reserved for future use - reserved: 3, + reserved: RESERVED_LEN, /// The actual Paylod payload: M::LEN, /// Message Authentication Code (mac) over all bytes until (exclusive) @@ -131,6 +133,10 @@ lense! { DataMsg := } lense! { CookieReply := + /// [MsgType] of this message + msg_type: MSG_SIZE_LEN, + /// Reserved for future use + reserved: RESERVED_LEN, /// Session ID of the sender (initiator) sid: SID_LEN, /// Nonce @@ -138,7 +144,7 @@ lense! { CookieReply := /// Encrypted cookie with authenticated initiator `mac` cookie_encrypted: MAC_SIZE + xaead::TAG_LEN, /// Padding (make it same size as InitHello) - padding: (SID_LEN + EphemeralKem::PK_LEN + StaticKem::CT_LEN + aead::TAG_LEN + aead::TAG_LEN + 32) - (SID_LEN + xaead::NONCE_LEN + MAC_SIZE + xaead::TAG_LEN) + padding: (MSG_SIZE_LEN + RESERVED_LEN + SID_LEN + EphemeralKem::PK_LEN + StaticKem::CT_LEN + aead::TAG_LEN + aead::TAG_LEN + 32 + MAC_SIZE + MAC_SIZE) - (MSG_SIZE_LEN + RESERVED_LEN + SID_LEN + xaead::NONCE_LEN + MAC_SIZE + xaead::TAG_LEN) } // Traits ///////////////////////////////////////////////////////////////////// @@ -187,6 +193,12 @@ impl TryFrom for MsgType { } } +impl Into for MsgType { + fn into(self) -> u8 { + self as u8 + } +} + /// length in bytes of an unencrypted Biscuit (plain text) pub const BISCUIT_PT_LEN: usize = Biscuit::<()>::LEN; diff --git a/rosenpass/src/protocol.rs b/rosenpass/src/protocol.rs index 5dc47d9f..a94fac91 100644 --- a/rosenpass/src/protocol.rs +++ b/rosenpass/src/protocol.rs @@ -864,7 +864,6 @@ impl CryptoServer { &mut self, rx_buf: &[u8], tx_buf: &mut [u8], - peer: PeerPtr, ip_addr_port: &[u8], ) -> Result { let mut cookie_value = [0u8; 16]; @@ -928,30 +927,40 @@ impl CryptoServer { } //Otherwise send cookie reply else { - let mut msg_out = tx_buf.envelope_truncating::>()?; + let mut msg_out = tx_buf.cookie_reply_truncating()?; let cookie_key = hash_domains::cookie_key()? .mix(self.spkm.secret())? .into_value(); - let mut cookie_reply_lens = msg_out.payload_mut().cookie_reply()?; let nonce_val = XAEADNonce::random(); + { + let msg_type_slice: [u8; 1] = [MsgType::CookieReply.into()]; + let msg_type = msg_out.msg_type_mut(); + msg_type.copy_from_slice(&msg_type_slice); + } // Copy sender's session id to cookie reply message { - let sid = cookie_reply_lens.sid_mut(); + let sid = msg_out.sid_mut(); sid.copy_from_slice(&rx_sid[..]); } // Generate random nonce, copy it to message and nonce_val { - let nonce = cookie_reply_lens.nonce_mut(); + let nonce = msg_out.nonce_mut(); nonce.copy_from_slice(&nonce_val.value); } // Encrypt cookie { - let cookie_ciphertext = &mut cookie_reply_lens.all_bytes_mut() - [SID_LEN..SID_LEN + xaead::NONCE_LEN + MAC_SIZE + xaead::TAG_LEN]; + let cookie_ciphertext = + &mut msg_out.all_bytes_mut()[MSG_SIZE_LEN + RESERVED_LEN + SID_LEN + ..MSG_SIZE_LEN + + RESERVED_LEN + + SID_LEN + + xaead::NONCE_LEN + + MAC_SIZE + + xaead::TAG_LEN]; xaead::encrypt( cookie_ciphertext, &cookie_key, @@ -962,7 +971,7 @@ impl CryptoServer { } // length of the response - let len = Some(self.seal_and_commit_msg(peer, MsgType::CookieReply, msg_out)?); + let len = Some(CookieReply::<&mut [u8]>::LEN); Ok(HandleMsgResult { exchanged_with: None, @@ -1057,10 +1066,8 @@ impl CryptoServer { } Ok(MsgType::DataMsg) => bail!("DataMsg handling not implemented!"), Ok(MsgType::CookieReply) => { - let msg_in = rx_buf.envelope::>()?; - ensure!(msg_in.check_seal(self)?, seal_broken); - - let peer = self.handle_cookie_reply(msg_in.payload().cookie_reply()?)?; + let msg_in = rx_buf.cookie_reply()?; + let peer = self.handle_cookie_reply(msg_in)?; len = 0; peer } @@ -2042,8 +2049,13 @@ impl CryptoServer { cookie_value, &cookie_key, &mac, - &cr.all_bytes() - [SID_LEN..SID_LEN + xaead::NONCE_LEN + MAC_SIZE + xaead::TAG_LEN], + &cr.all_bytes()[MSG_SIZE_LEN + RESERVED_LEN + SID_LEN + ..MSG_SIZE_LEN + + RESERVED_LEN + + SID_LEN + + xaead::NONCE_LEN + + MAC_SIZE + + xaead::TAG_LEN], )?; // Immediately retransmit on recieving a cookie reply message @@ -2190,7 +2202,6 @@ mod test { .handle_msg_under_load( &a_to_b_buf.as_slice()[..init_hello_len], &mut *b_to_a_buf, - PeerPtr(0), &ip_addr_port_a, ) .unwrap(); @@ -2237,7 +2248,6 @@ mod test { .handle_msg_under_load( &a_to_b_buf.as_slice()[..retx_init_hello_len], &mut *b_to_a_buf, - PeerPtr(0), &ip_addr_port_a, ) .unwrap(); @@ -2299,7 +2309,6 @@ mod test { .handle_msg_under_load( &b_to_a_buf[..resp_hello_len], &mut *a_to_b_buf, - PeerPtr(0), &ip_addr_port_b[..ip_addr_port_b_len] ) .is_err());