This commit is contained in:
Karolin Varner
2025-11-01 20:49:47 +01:00
parent 0c960d57bc
commit 63511465de
8 changed files with 47 additions and 28 deletions

View File

@@ -1,6 +1,6 @@
use hex_literal::hex; use hex_literal::hex;
use rosenpass_util::zerocopy::RefMaker; use rosenpass_util::zerocopy::RefMaker;
use zerocopy::{SplitByteSlice}; use zerocopy::SplitByteSlice;
use crate::RosenpassError::{self, InvalidApiMessageType}; use crate::RosenpassError::{self, InvalidApiMessageType};

View File

@@ -1,5 +1,5 @@
use rosenpass_util::zerocopy::ZerocopyMutSliceExt; use rosenpass_util::zerocopy::ZerocopyMutSliceExt;
use zerocopy::{SplitByteSliceMut, FromBytes, Immutable, IntoBytes, KnownLayout, Ref}; use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref, SplitByteSliceMut};
use super::{Message, RawMsgType, RequestMsgType, ResponseMsgType}; use super::{Message, RawMsgType, RequestMsgType, ResponseMsgType};

View File

@@ -125,7 +125,8 @@ impl<B: SplitByteSlice> RequestRefMaker<B> {
self.ensure_fit()?; self.ensure_fit()?;
let point = self.target_size(); let point = self.target_size();
let Self { buf, msg_type } = self; let Self { buf, msg_type } = self;
let (buf, _) = buf.split_at(point) let (buf, _) = buf
.split_at(point)
.map_err(|_| anyhow!("Failed to split buffer"))?; .map_err(|_| anyhow!("Failed to split buffer"))?;
Ok(Self { buf, msg_type }) Ok(Self { buf, msg_type })
} }
@@ -135,7 +136,8 @@ impl<B: SplitByteSlice> RequestRefMaker<B> {
self.ensure_fit()?; self.ensure_fit()?;
let point = self.buf.len() - self.target_size(); let point = self.buf.len() - self.target_size();
let Self { buf, msg_type } = self; let Self { buf, msg_type } = self;
let (buf, _) = buf.split_at(point) let (buf, _) = buf
.split_at(point)
.map_err(|_| anyhow!("Failed to split buffer"))?; .map_err(|_| anyhow!("Failed to split buffer"))?;
Ok(Self { buf, msg_type }) Ok(Self { buf, msg_type })
} }

View File

@@ -129,7 +129,8 @@ impl<B: SplitByteSlice> ResponseRefMaker<B> {
self.ensure_fit()?; self.ensure_fit()?;
let point = self.target_size(); let point = self.target_size();
let Self { buf, msg_type } = self; let Self { buf, msg_type } = self;
let (buf, _) = buf.split_at(point) let (buf, _) = buf
.split_at(point)
.map_err(|_| anyhow!("Failed to split buffer!"))?; .map_err(|_| anyhow!("Failed to split buffer!"))?;
Ok(Self { buf, msg_type }) Ok(Self { buf, msg_type })
} }
@@ -139,7 +140,8 @@ impl<B: SplitByteSlice> ResponseRefMaker<B> {
self.ensure_fit()?; self.ensure_fit()?;
let point = self.buf.len() - self.target_size(); let point = self.buf.len() - self.target_size();
let Self { buf, msg_type } = self; let Self { buf, msg_type } = self;
let (buf, _) = buf.split_at(point) let (buf, _) = buf
.split_at(point)
.map_err(|_| anyhow!("Failed to split buffer!"))?; .map_err(|_| anyhow!("Failed to split buffer!"))?;
Ok(Self { buf, msg_type }) Ok(Self { buf, msg_type })
} }

View File

@@ -508,7 +508,10 @@ impl KnownResponseHasher {
/// # Panic & Safety /// # Panic & Safety
/// ///
/// Panics in case of a problem with this underlying hash function /// Panics in case of a problem with this underlying hash function
pub fn hash<Msg: IntoBytes + FromBytes + Immutable>(&self, msg: &Envelope<Msg>) -> KnownResponseHash { pub fn hash<Msg: IntoBytes + FromBytes + Immutable>(
&self,
msg: &Envelope<Msg>,
) -> KnownResponseHash {
let data = &msg.as_bytes()[span_of!(Envelope<Msg>, msg_type..cookie)]; let data = &msg.as_bytes()[span_of!(Envelope<Msg>, msg_type..cookie)];
// This function is only used internally and results are not propagated // This function is only used internally and results are not propagated
// to outside the peer. Thus, it uses SHAKE256 exclusively. // to outside the peer. Thus, it uses SHAKE256 exclusively.
@@ -2188,8 +2191,9 @@ impl CryptoServer {
let peer = match msg_type { let peer = match msg_type {
Ok(MsgType::InitHello) => { Ok(MsgType::InitHello) => {
let msg_in: Ref<&[u8], Envelope<InitHello>> = let msg_in: Ref<&[u8], Envelope<InitHello>> = Ref::from_bytes(rx_buf)
Ref::from_bytes(rx_buf).ok().ok_or(RosenpassError::BufferSizeMismatch)?; .ok()
.ok_or(RosenpassError::BufferSizeMismatch)?;
// At this point, we do not know the hash functon used by the peer, thus we try both, // At this point, we do not know the hash functon used by the peer, thus we try both,
// with a preference for SHAKE256. // with a preference for SHAKE256.
@@ -2222,8 +2226,9 @@ impl CryptoServer {
peer peer
} }
Ok(MsgType::RespHello) => { Ok(MsgType::RespHello) => {
let msg_in: Ref<&[u8], Envelope<RespHello>> = let msg_in: Ref<&[u8], Envelope<RespHello>> = Ref::from_bytes(rx_buf)
Ref::from_bytes(rx_buf).ok().ok_or(RosenpassError::BufferSizeMismatch)?; .ok()
.ok_or(RosenpassError::BufferSizeMismatch)?;
let mut msg_out = truncating_cast_into::<Envelope<InitConf>>(tx_buf)?; let mut msg_out = truncating_cast_into::<Envelope<InitConf>>(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)?;
@@ -2239,8 +2244,9 @@ impl CryptoServer {
peer peer
} }
Ok(MsgType::InitConf) => { Ok(MsgType::InitConf) => {
let msg_in: Ref<&[u8], Envelope<InitConf>> = let msg_in: Ref<&[u8], Envelope<InitConf>> = Ref::from_bytes(rx_buf)
Ref::from_bytes(rx_buf).ok().ok_or(RosenpassError::BufferSizeMismatch)?; .ok()
.ok_or(RosenpassError::BufferSizeMismatch)?;
let mut msg_out = truncating_cast_into::<Envelope<EmptyData>>(tx_buf)?; let mut msg_out = truncating_cast_into::<Envelope<EmptyData>>(tx_buf)?;
@@ -2271,7 +2277,7 @@ impl CryptoServer {
&msg_in.payload, &msg_in.payload,
&mut msg_out.payload, &mut msg_out.payload,
KeyedHash::keyed_shake256(), KeyedHash::keyed_shake256(),
); );
let (peer, peer_hash_choice) = match peer_shake256 { let (peer, peer_hash_choice) = match peer_shake256 {
Ok(peer) => (peer, KeyedHash::keyed_shake256()), Ok(peer) => (peer, KeyedHash::keyed_shake256()),
Err(_) => { Err(_) => {
@@ -2307,14 +2313,16 @@ impl CryptoServer {
peer peer
} }
Ok(MsgType::EmptyData) => { Ok(MsgType::EmptyData) => {
let msg_in: Ref<&[u8], Envelope<EmptyData>> = let msg_in: Ref<&[u8], Envelope<EmptyData>> = Ref::from_bytes(rx_buf)
Ref::from_bytes(rx_buf).ok().ok_or(RosenpassError::BufferSizeMismatch)?; .ok()
.ok_or(RosenpassError::BufferSizeMismatch)?;
self.handle_resp_conf(&msg_in, seal_broken.to_string())? self.handle_resp_conf(&msg_in, seal_broken.to_string())?
} }
Ok(MsgType::CookieReply) => { Ok(MsgType::CookieReply) => {
let msg_in: Ref<&[u8], CookieReply> = let msg_in: Ref<&[u8], CookieReply> = Ref::from_bytes(rx_buf)
Ref::from_bytes(rx_buf).ok().ok_or(RosenpassError::BufferSizeMismatch)?; .ok()
.ok_or(RosenpassError::BufferSizeMismatch)?;
let peer = self.handle_cookie_reply(&msg_in)?; let peer = self.handle_cookie_reply(&msg_in)?;
len = 0; len = 0;
peer peer

View File

@@ -10,12 +10,16 @@ use crate::RosenpassError;
pub fn truncating_cast_into<T: FromBytes + KnownLayout + Immutable>( pub fn truncating_cast_into<T: FromBytes + KnownLayout + Immutable>(
buf: &mut [u8], buf: &mut [u8],
) -> Result<Ref<&mut [u8], T>, RosenpassError> { ) -> Result<Ref<&mut [u8], T>, RosenpassError> {
Ref::from_bytes(&mut buf[..size_of::<T>()]).ok().ok_or(RosenpassError::BufferSizeMismatch) Ref::from_bytes(&mut buf[..size_of::<T>()])
.ok()
.ok_or(RosenpassError::BufferSizeMismatch)
} }
/// Used to parse a network message using [zerocopy], mutably /// Used to parse a network message using [zerocopy], mutably
pub fn truncating_cast_into_nomut<T: FromBytes + KnownLayout + Immutable>( pub fn truncating_cast_into_nomut<T: FromBytes + KnownLayout + Immutable>(
buf: &[u8], buf: &[u8],
) -> Result<Ref<&[u8], T>, RosenpassError> { ) -> Result<Ref<&[u8], T>, RosenpassError> {
Ref::from_bytes(&buf[..size_of::<T>()]).ok().ok_or(RosenpassError::BufferSizeMismatch) Ref::from_bytes(&buf[..size_of::<T>()])
.ok()
.ok_or(RosenpassError::BufferSizeMismatch)
} }

View File

@@ -171,7 +171,7 @@ where
let typ = res.first().ok_or(invalid_msg_poller())?; let typ = res.first().ok_or(invalid_msg_poller())?;
let typ = msgs::MsgType::try_from(*typ)?; let typ = msgs::MsgType::try_from(*typ)?;
let msgs::MsgType::SetPsk = typ; // Assert type let msgs::MsgType::SetPsk = typ; // Assert type
let res = zerocopy::Ref::<&[u8], Envelope<SetPskResponse>>::from_bytes(res) let res = zerocopy::Ref::<&[u8], Envelope<SetPskResponse>>::from_bytes(res)
.ok() .ok()
.ok_or(invalid_msg_poller())?; .ok_or(invalid_msg_poller())?;
@@ -203,9 +203,10 @@ where
let mut req = [0u8; BUF_SIZE]; let mut req = [0u8; BUF_SIZE];
// Construct message view // Construct message view
let mut req = zerocopy::Ref::<&mut [u8], Envelope<msgs::SetPskRequest>>::from_bytes(&mut req) let mut req =
.ok() zerocopy::Ref::<&mut [u8], Envelope<msgs::SetPskRequest>>::from_bytes(&mut req)
.ok_or(MsgError)?; .ok()
.ok_or(MsgError)?;
// Populate envelope // Populate envelope
req.msg_type = msgs::MsgType::SetPsk as u8; req.msg_type = msgs::MsgType::SetPsk as u8;

View File

@@ -79,10 +79,12 @@ where
let typ = msgs::MsgType::try_from(*typ)?; let typ = msgs::MsgType::try_from(*typ)?;
let msgs::MsgType::SetPsk = typ; // Assert type let msgs::MsgType::SetPsk = typ; // Assert type
let req = let req = zerocopy::Ref::<&[u8], Envelope<SetPskRequest>>::from_bytes(req)
zerocopy::Ref::<&[u8], Envelope<SetPskRequest>>::from_bytes(req).ok().ok_or(InvalidMessage)?; .ok()
let mut res = .ok_or(InvalidMessage)?;
zerocopy::Ref::<&mut [u8], Envelope<SetPskResponse>>::from_bytes(res).ok().ok_or(InvalidMessage)?; let mut res = zerocopy::Ref::<&mut [u8], Envelope<SetPskResponse>>::from_bytes(res)
.ok()
.ok_or(InvalidMessage)?;
res.msg_type = msgs::MsgType::SetPsk as u8; res.msg_type = msgs::MsgType::SetPsk as u8;
self.handle_set_psk(&req.payload, &mut res.payload)?; self.handle_set_psk(&req.payload, &mut res.payload)?;