From fd8f2e4424b20b78e523ad0af5b0cedb825345be Mon Sep 17 00:00:00 2001 From: Aaron Kaiser Date: Tue, 6 Feb 2024 15:51:18 +0100 Subject: [PATCH] style: apply rustfmt --- rosenpass/src/lib.rs | 1 - rosenpass/src/msgs.rs | 17 +++++------ rosenpass/src/protocol.rs | 64 ++++++++++++++------------------------- 3 files changed, 30 insertions(+), 52 deletions(-) diff --git a/rosenpass/src/lib.rs b/rosenpass/src/lib.rs index 6791cd8..29e7b49 100644 --- a/rosenpass/src/lib.rs +++ b/rosenpass/src/lib.rs @@ -14,4 +14,3 @@ pub enum RosenpassError { #[error("invalid message type")] InvalidMessageType(u8), } - diff --git a/rosenpass/src/msgs.rs b/rosenpass/src/msgs.rs index 30721ee..a400891 100644 --- a/rosenpass/src/msgs.rs +++ b/rosenpass/src/msgs.rs @@ -9,15 +9,14 @@ //! To achieve this we utilize the zerocopy library. use super::RosenpassError; -use std::mem::size_of; use rosenpass_cipher_traits::Kem; use rosenpass_ciphers::kem::{EphemeralKem, StaticKem}; use rosenpass_ciphers::{aead, xaead, KEY_LEN}; +use std::mem::size_of; use zerocopy::{AsBytes, FromBytes, FromZeroes}; // Macro magic //////////////////////////////////////////////////////////////// - #[repr(packed)] #[derive(AsBytes, FromBytes, FromZeroes)] pub struct Envelope { @@ -30,8 +29,8 @@ pub struct Envelope { /// Message Authentication Code (mac) over all bytes until (exclusive) /// `mac` itself pub mac: [u8; 16], - /// Currently unused, TODO: do something with this - pub cookie: [u8; 16] + /// Currently unused, TODO: do something with this + pub cookie: [u8; 16], } #[repr(packed)] @@ -76,7 +75,7 @@ pub struct InitConf { /// Responders handshake state in encrypted form pub biscuit: [u8; BISCUIT_CT_LEN], /// Empty encrypted message (just an auth tag) - pub auth: [u8; aead::TAG_LEN] + pub auth: [u8; aead::TAG_LEN], } #[repr(packed)] @@ -87,7 +86,7 @@ pub struct EmptyData { /// Nonce pub ctr: [u8; 8], /// Empty encrypted message (just an auth tag) - pub auth: [u8; aead::TAG_LEN] + pub auth: [u8; aead::TAG_LEN], } #[repr(packed)] @@ -98,19 +97,19 @@ pub struct Biscuit { /// The biscuit number (replay protection) pub biscuit_no: [u8; 12], /// Chaining key - pub ck: [u8; KEY_LEN] + pub ck: [u8; KEY_LEN], } #[repr(packed)] #[derive(AsBytes, FromBytes, FromZeroes)] pub struct DataMsg { - pub dummy: [u8; 4] + pub dummy: [u8; 4], } #[repr(packed)] #[derive(AsBytes, FromBytes, FromZeroes)] pub struct CookieReply { - pub dummy: [u8; 4] + pub dummy: [u8; 4], } // Traits ///////////////////////////////////////////////////////////////////// diff --git a/rosenpass/src/protocol.rs b/rosenpass/src/protocol.rs index 1562816..6a15a3b 100644 --- a/rosenpass/src/protocol.rs +++ b/rosenpass/src/protocol.rs @@ -70,7 +70,7 @@ use std::collections::hash_map::{ HashMap, }; use std::convert::Infallible; -use std::mem::{size_of,offset_of}; +use std::mem::{offset_of, size_of}; use anyhow::{bail, ensure, Context, Result}; @@ -90,7 +90,7 @@ use crate::{hash_domains, msgs::*, RosenpassError}; /// Size required to fit any message in binary form pub const RTX_BUFFER_SIZE: usize = max_usize( size_of::>(), - size_of::>() + size_of::>(), ); /// A type for time, e.g. for backoff before re-tries @@ -795,26 +795,22 @@ impl CryptoServer { let peer = match rx_buf[0].try_into() { Ok(MsgType::InitHello) => { - let msg_in: Ref<&[u8], Envelope> = Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; + let msg_in: Ref<&[u8], Envelope> = + Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; ensure!(msg_in.check_seal(self)?, seal_broken); let mut msg_out = truncating_cast_into::>(tx_buf)?; - let peer = self.handle_init_hello( - &msg_in.payload, - &mut msg_out.payload, - )?; + let peer = self.handle_init_hello(&msg_in.payload, &mut msg_out.payload)?; len = self.seal_and_commit_msg(peer, MsgType::RespHello, &mut msg_out)?; peer } Ok(MsgType::RespHello) => { - let msg_in: Ref<&[u8], Envelope> = Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; + let msg_in: Ref<&[u8], Envelope> = + Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; ensure!(msg_in.check_seal(self)?, seal_broken); let mut msg_out = truncating_cast_into::>(tx_buf)?; - let peer = self.handle_resp_hello( - &msg_in.payload, - &mut msg_out.payload, - )?; + let peer = self.handle_resp_hello(&msg_in.payload, &mut msg_out.payload)?; len = self.seal_and_commit_msg(peer, MsgType::InitConf, &mut msg_out)?; peer.hs() .store_msg_for_retransmission(self, &msg_out.as_bytes()[..len])?; @@ -822,20 +818,19 @@ impl CryptoServer { peer } Ok(MsgType::InitConf) => { - let msg_in: Ref<&[u8], Envelope> = Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; + let msg_in: Ref<&[u8], Envelope> = + Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; ensure!(msg_in.check_seal(self)?, seal_broken); let mut msg_out = truncating_cast_into::>(tx_buf)?; - let peer = self.handle_init_conf( - &msg_in.payload, - &mut msg_out.payload, - )?; + let peer = self.handle_init_conf(&msg_in.payload, &mut msg_out.payload)?; len = self.seal_and_commit_msg(peer, MsgType::EmptyData, &mut msg_out)?; exchanged = true; peer } Ok(MsgType::EmptyData) => { - let msg_in: Ref<&[u8], Envelope> = Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; + let msg_in: Ref<&[u8], Envelope> = + Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?; ensure!(msg_in.check_seal(self)?, seal_broken); self.handle_resp_conf(&msg_in.payload)? @@ -1181,8 +1176,7 @@ where let mac = hash_domains::mac()? .mix(peer.get(srv).spkt.secret())? .mix(&self.as_bytes()[..offset_of!(Self, mac)])?; - self.mac - .copy_from_slice(mac.into_value()[..16].as_ref()); + self.mac.copy_from_slice(mac.into_value()[..16].as_ref()); Ok(()) } } @@ -1284,7 +1278,8 @@ impl HandshakeState { biscuit_ct: &mut [u8], ) -> Result<&mut Self> { let mut biscuit = Secret::::zero(); // pt buffer - let mut biscuit: Ref<&mut [u8], Biscuit> = Ref::new(biscuit.secret_mut().as_mut_slice()).unwrap(); + let mut biscuit: Ref<&mut [u8], Biscuit> = + Ref::new(biscuit.secret_mut().as_mut_slice()).unwrap(); // calculate pt contents biscuit @@ -1339,7 +1334,8 @@ impl HandshakeState { // Allocate and decrypt the biscuit data let mut biscuit = Secret::::zero(); // pt buf - let mut biscuit: Ref<&mut [u8], Biscuit> = Ref::new(biscuit.secret_mut().as_mut_slice()).unwrap(); + let mut biscuit: Ref<&mut [u8], Biscuit> = + Ref::new(biscuit.secret_mut().as_mut_slice()).unwrap(); xaead::decrypt( biscuit.as_bytes_mut(), bk.get(srv).key.secret(), @@ -1414,11 +1410,7 @@ impl CryptoServer { impl CryptoServer { /// Implementation of the cryptographic protocol using the already /// established primitives - pub fn handle_initiation( - &mut self, - peer: PeerPtr, - ih: &mut InitHello, - ) -> Result { + pub fn handle_initiation(&mut self, peer: PeerPtr, ih: &mut InitHello) -> Result { let mut hs = InitiatorHandshake::zero_with_timestamp(self); // IHI1 @@ -1460,11 +1452,7 @@ impl CryptoServer { Ok(peer) } - pub fn handle_init_hello( - &mut self, - ih: &InitHello, - rh: &mut RespHello, - ) -> Result { + pub fn handle_init_hello(&mut self, ih: &InitHello, rh: &mut RespHello) -> Result { let mut core = HandshakeState::zero(); core.sidi = SessionId::from_slice(&ih.sidi); @@ -1523,11 +1511,7 @@ impl CryptoServer { Ok(peer) } - pub fn handle_resp_hello( - &mut self, - rh: &RespHello, - ic: &mut InitConf, - ) -> Result { + pub fn handle_resp_hello(&mut self, rh: &RespHello, ic: &mut InitConf) -> Result { // RHI2 let peer = self .lookup_handshake(SessionId::from_slice(&rh.sidi)) @@ -1619,11 +1603,7 @@ impl CryptoServer { Ok(peer) } - pub fn handle_init_conf( - &mut self, - ic: &InitConf, - rc: &mut EmptyData, - ) -> Result { + pub fn handle_init_conf(&mut self, ic: &InitConf, rc: &mut EmptyData) -> Result { // (peer, bn) ← LoadBiscuit(InitConf.biscuit) // ICR1 let (peer, biscuit_no, mut core) = HandshakeState::load_biscuit(