mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-07-28 14:26:59 -07:00
Migrate zerocopy API usage to 0.8
This commit is contained in:
@@ -316,7 +316,7 @@ where
|
||||
use crate::app_server::BrokerStorePtr;
|
||||
//
|
||||
use rosenpass_secret_memory::Public;
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::IntoBytes;
|
||||
(self.app_server().brokers.store.len() - 1)
|
||||
.apply(|x| x as u64)
|
||||
.apply(|x| Public::from_slice(x.as_bytes()))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use zerocopy::{ByteSlice, Ref};
|
||||
use zerocopy::{ByteSlice, Ref, SplitByteSlice};
|
||||
|
||||
use rosenpass_util::zerocopy::{RefMaker, ZerocopySliceExt};
|
||||
|
||||
@@ -22,7 +22,10 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_prefix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_request_msg_type].
|
||||
fn request_msg_type_from_prefix(self) -> anyhow::Result<RequestMsgType> {
|
||||
fn request_msg_type_from_prefix(self) -> anyhow::Result<RequestMsgType>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.msg_type_maker()
|
||||
.from_prefix()?
|
||||
.parse_request_msg_type()
|
||||
@@ -31,7 +34,10 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_suffix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_request_msg_type].
|
||||
fn request_msg_type_from_suffix(self) -> anyhow::Result<RequestMsgType> {
|
||||
fn request_msg_type_from_suffix(self) -> anyhow::Result<RequestMsgType>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.msg_type_maker()
|
||||
.from_suffix()?
|
||||
.parse_request_msg_type()
|
||||
@@ -46,7 +52,10 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_prefix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_response_msg_type].
|
||||
fn response_msg_type_from_prefix(self) -> anyhow::Result<ResponseMsgType> {
|
||||
fn response_msg_type_from_prefix(self) -> anyhow::Result<ResponseMsgType>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.msg_type_maker()
|
||||
.from_prefix()?
|
||||
.parse_response_msg_type()
|
||||
@@ -55,7 +64,10 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_suffix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_response_msg_type].
|
||||
fn response_msg_type_from_suffix(self) -> anyhow::Result<ResponseMsgType> {
|
||||
fn response_msg_type_from_suffix(self) -> anyhow::Result<ResponseMsgType>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.msg_type_maker()
|
||||
.from_suffix()?
|
||||
.parse_response_msg_type()
|
||||
@@ -67,12 +79,18 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [RequestRef::parse_from_prefix] in chaining.
|
||||
fn parse_request_from_prefix(self) -> anyhow::Result<RequestRef<Self>> {
|
||||
fn parse_request_from_prefix(self) -> anyhow::Result<RequestRef<Self>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
RequestRef::parse_from_prefix(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [RequestRef::parse_from_suffix] in chaining.
|
||||
fn parse_request_from_suffix(self) -> anyhow::Result<RequestRef<Self>> {
|
||||
fn parse_request_from_suffix(self) -> anyhow::Result<RequestRef<Self>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
RequestRef::parse_from_suffix(self)
|
||||
}
|
||||
|
||||
@@ -82,12 +100,18 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [ResponseRef::parse_from_prefix] in chaining.
|
||||
fn parse_response_from_prefix(self) -> anyhow::Result<ResponseRef<Self>> {
|
||||
fn parse_response_from_prefix(self) -> anyhow::Result<ResponseRef<Self>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
ResponseRef::parse_from_prefix(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [ResponseRef::parse_from_suffix] in chaining.
|
||||
fn parse_response_from_suffix(self) -> anyhow::Result<ResponseRef<Self>> {
|
||||
fn parse_response_from_suffix(self) -> anyhow::Result<ResponseRef<Self>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
ResponseRef::parse_from_suffix(self)
|
||||
}
|
||||
|
||||
@@ -102,12 +126,18 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn ping_request_from_prefix(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
fn ping_request_from_prefix(self) -> anyhow::Result<Ref<Self, PingRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn ping_request_from_suffix(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
fn ping_request_from_suffix(self) -> anyhow::Result<Ref<Self, PingRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -122,12 +152,18 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn ping_response_from_prefix(self) -> anyhow::Result<Ref<Self, PingResponse>> {
|
||||
fn ping_response_from_prefix(self) -> anyhow::Result<Ref<Self, PingResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn ping_response_from_suffix(self) -> anyhow::Result<Ref<Self, PingResponse>> {
|
||||
fn ping_response_from_suffix(self) -> anyhow::Result<Ref<Self, PingResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -137,12 +173,18 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn supply_keypair_request_from_prefix(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>> {
|
||||
fn supply_keypair_request_from_prefix(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn supply_keypair_request_from_suffix(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>> {
|
||||
fn supply_keypair_request_from_suffix(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -159,14 +201,20 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn supply_keypair_response_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>> {
|
||||
) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn supply_keypair_response_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>> {
|
||||
) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -178,14 +226,20 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_listen_socket_request_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_listen_socket_request_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -204,14 +258,20 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_listen_socket_response_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_listen_socket_response_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -223,14 +283,20 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_psk_broker_request_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_psk_broker_request_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
@@ -247,14 +313,20 @@ pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_psk_broker_response_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_psk_broker_response_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>> {
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
{
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use hex_literal::hex;
|
||||
use rosenpass_util::zerocopy::RefMaker;
|
||||
use zerocopy::ByteSlice;
|
||||
use zerocopy::{ByteSlice, Ref};
|
||||
|
||||
use crate::RosenpassError::{self, InvalidApiMessageType};
|
||||
|
||||
@@ -171,10 +171,10 @@ pub trait RefMakerRawMsgTypeExt {
|
||||
|
||||
impl<B: ByteSlice> RefMakerRawMsgTypeExt for RefMaker<B, RawMsgType> {
|
||||
fn parse_request_msg_type(self) -> anyhow::Result<RequestMsgType> {
|
||||
Ok(self.parse()?.read().try_into()?)
|
||||
Ok(Ref::read(&self.parse()?).try_into()?)
|
||||
}
|
||||
|
||||
fn parse_response_msg_type(self) -> anyhow::Result<ResponseMsgType> {
|
||||
Ok(self.parse()?.read().try_into()?)
|
||||
Ok(Ref::read(&self.parse()?).try_into()?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use rosenpass_util::zerocopy::ZerocopyMutSliceExt;
|
||||
use zerocopy::{AsBytes, ByteSliceMut, FromBytes, FromZeroes, Ref};
|
||||
use zerocopy::{ByteSliceMut, FromBytes, Immutable, IntoBytes, KnownLayout, Ref};
|
||||
|
||||
use super::{Message, RawMsgType, RequestMsgType, ResponseMsgType};
|
||||
|
||||
@@ -12,8 +12,8 @@ pub const MAX_REQUEST_FDS: usize = 2;
|
||||
|
||||
/// Message envelope for API messages
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct Envelope<M: IntoBytes + FromBytes + KnownLayout + Immutable> {
|
||||
/// Which message this is
|
||||
pub msg_type: RawMsgType,
|
||||
/// The actual Paylod
|
||||
@@ -27,7 +27,7 @@ pub type ResponseEnvelope<M> = Envelope<M>;
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct PingRequestPayload {
|
||||
/// Randomly generated connection id
|
||||
pub echo: [u8; 256],
|
||||
@@ -68,7 +68,7 @@ impl Message for PingRequest {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct PingResponsePayload {
|
||||
/// Randomly generated connection id
|
||||
pub echo: [u8; 256],
|
||||
@@ -109,7 +109,7 @@ impl Message for PingResponse {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct SupplyKeypairRequestPayload {}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
@@ -169,7 +169,7 @@ pub mod supply_keypair_response_status {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct SupplyKeypairResponsePayload {
|
||||
#[allow(missing_docs)]
|
||||
pub status: u128,
|
||||
@@ -210,7 +210,7 @@ impl Message for SupplyKeypairResponse {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct AddListenSocketRequestPayload {}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
@@ -264,7 +264,7 @@ pub mod add_listen_socket_response_status {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct AddListenSocketResponsePayload {
|
||||
pub status: u128,
|
||||
}
|
||||
@@ -304,7 +304,7 @@ impl Message for AddListenSocketResponse {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct AddPskBrokerRequestPayload {}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
@@ -359,7 +359,7 @@ pub mod add_psk_broker_response_status {
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
#[derive(Debug, Copy, Clone, Hash, IntoBytes, FromBytes, KnownLayout, Immutable, PartialEq, Eq)]
|
||||
pub struct AddPskBrokerResponsePayload {
|
||||
pub status: u128,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use anyhow::ensure;
|
||||
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref, SplitByteSlice};
|
||||
|
||||
use super::{ByteSliceRefExt, MessageAttributes, PingRequest, RequestMsgType};
|
||||
|
||||
@@ -20,7 +20,7 @@ impl<B: ByteSlice> RequestRef<B> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zerocopy::AsBytes;
|
||||
/// use zerocopy::IntoBytes;
|
||||
///
|
||||
/// use rosenpass::api::{PingRequest, RequestRef, RequestMsgType};
|
||||
///
|
||||
@@ -46,13 +46,19 @@ impl<B: ByteSlice> RequestRef<B> {
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_prefix(buf: B) -> anyhow::Result<Self> {
|
||||
pub fn parse_from_prefix(buf: B) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
RequestRefMaker::new(buf)?.from_prefix()?.parse()
|
||||
}
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_suffix(buf: B) -> anyhow::Result<Self> {
|
||||
pub fn parse_from_suffix(buf: B) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
RequestRefMaker::new(buf)?.from_suffix()?.parse()
|
||||
}
|
||||
|
||||
@@ -121,20 +127,30 @@ impl<B: ByteSlice> RequestRefMaker<B> {
|
||||
}
|
||||
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn from_prefix(self) -> anyhow::Result<Self> {
|
||||
fn from_prefix(self) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
self.ensure_fit()?;
|
||||
let point = self.target_size();
|
||||
let Self { buf, msg_type } = self;
|
||||
let (buf, _) = buf.split_at(point);
|
||||
let (buf, _) = buf
|
||||
.split_at(point)
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))?;
|
||||
Ok(Self { buf, msg_type })
|
||||
}
|
||||
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn from_suffix(self) -> anyhow::Result<Self> {
|
||||
fn from_suffix(self) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
self.ensure_fit()?;
|
||||
let point = self.buf.len() - self.target_size();
|
||||
let Self { buf, msg_type } = self;
|
||||
let (buf, _) = buf.split_at(point);
|
||||
let (_, buf) = buf
|
||||
.split_at(point)
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))?;
|
||||
Ok(Self { buf, msg_type })
|
||||
}
|
||||
|
||||
@@ -168,10 +184,10 @@ where
|
||||
/// See [Self::parse].
|
||||
pub fn bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes(),
|
||||
Self::SupplyKeypair(r) => r.bytes(),
|
||||
Self::AddListenSocket(r) => r.bytes(),
|
||||
Self::AddPskBroker(r) => r.bytes(),
|
||||
Self::Ping(r) => Ref::bytes(r),
|
||||
Self::SupplyKeypair(r) => Ref::bytes(r),
|
||||
Self::AddListenSocket(r) => Ref::bytes(r),
|
||||
Self::AddPskBroker(r) => Ref::bytes(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,10 +199,10 @@ where
|
||||
/// Access the byte data of this reference; mutably
|
||||
pub fn bytes_mut(&mut self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes_mut(),
|
||||
Self::SupplyKeypair(r) => r.bytes_mut(),
|
||||
Self::AddListenSocket(r) => r.bytes_mut(),
|
||||
Self::AddPskBroker(r) => r.bytes_mut(),
|
||||
Self::Ping(r) => Ref::bytes_mut(r),
|
||||
Self::SupplyKeypair(r) => Ref::bytes_mut(r),
|
||||
Self::AddListenSocket(r) => Ref::bytes_mut(r),
|
||||
Self::AddPskBroker(r) => Ref::bytes_mut(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use rosenpass_util::zerocopy::{
|
||||
RefMaker, ZerocopyEmancipateExt, ZerocopyEmancipateMutExt, ZerocopySliceExt,
|
||||
};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref, SplitByteSlice};
|
||||
|
||||
use super::{Message, PingRequest, PingResponse};
|
||||
use super::{RequestRef, ResponseRef, ZerocopyResponseMakerSetupMessageExt};
|
||||
@@ -22,14 +22,14 @@ pub trait RequestMsg: Sized + Message {
|
||||
}
|
||||
|
||||
/// Setup a response maker from a buffer prefix (through [Message::setup]) for this request message type
|
||||
fn setup_response_from_prefix<B: ByteSliceMut>(
|
||||
fn setup_response_from_prefix<B: ByteSliceMut + SplitByteSlice>(
|
||||
buf: B,
|
||||
) -> anyhow::Result<Ref<B, Self::ResponseMsg>> {
|
||||
Self::zk_response_maker(buf).from_prefix()?.setup_msg()
|
||||
}
|
||||
|
||||
/// Setup a response maker from a buffer suffix (through [Message::setup]) for this request message type
|
||||
fn setup_response_from_suffix<B: ByteSliceMut>(
|
||||
fn setup_response_from_suffix<B: ByteSliceMut + SplitByteSlice>(
|
||||
buf: B,
|
||||
) -> anyhow::Result<Ref<B, Self::ResponseMsg>> {
|
||||
Self::zk_response_maker(buf).from_prefix()?.setup_msg()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// TODO: This is copied verbatim from ResponseRef…not pretty
|
||||
use anyhow::ensure;
|
||||
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref, SplitByteSlice};
|
||||
|
||||
use super::{ByteSliceRefExt, MessageAttributes, PingResponse, ResponseMsgType};
|
||||
|
||||
@@ -23,7 +23,7 @@ impl<B: ByteSlice> ResponseRef<B> {
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zerocopy::AsBytes;
|
||||
/// use zerocopy::IntoBytes;
|
||||
///
|
||||
/// use rosenpass::api::{PingResponse, ResponseRef, ResponseMsgType};
|
||||
/// // Produce the original PingResponse
|
||||
@@ -50,13 +50,19 @@ impl<B: ByteSlice> ResponseRef<B> {
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_prefix(buf: B) -> anyhow::Result<Self> {
|
||||
pub fn parse_from_prefix(buf: B) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
ResponseRefMaker::new(buf)?.from_prefix()?.parse()
|
||||
}
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_suffix(buf: B) -> anyhow::Result<Self> {
|
||||
pub fn parse_from_suffix(buf: B) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
ResponseRefMaker::new(buf)?.from_suffix()?.parse()
|
||||
}
|
||||
|
||||
@@ -125,20 +131,30 @@ impl<B: ByteSlice> ResponseRefMaker<B> {
|
||||
}
|
||||
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn from_prefix(self) -> anyhow::Result<Self> {
|
||||
fn from_prefix(self) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
self.ensure_fit()?;
|
||||
let point = self.target_size();
|
||||
let Self { buf, msg_type } = self;
|
||||
let (buf, _) = buf.split_at(point);
|
||||
let (buf, _) = buf
|
||||
.split_at(point)
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))?;
|
||||
Ok(Self { buf, msg_type })
|
||||
}
|
||||
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn from_suffix(self) -> anyhow::Result<Self> {
|
||||
fn from_suffix(self) -> anyhow::Result<Self>
|
||||
where
|
||||
B: SplitByteSlice,
|
||||
{
|
||||
self.ensure_fit()?;
|
||||
let point = self.buf.len() - self.target_size();
|
||||
let Self { buf, msg_type } = self;
|
||||
let (buf, _) = buf.split_at(point);
|
||||
let (_, buf) = buf
|
||||
.split_at(point)
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))?;
|
||||
Ok(Self { buf, msg_type })
|
||||
}
|
||||
|
||||
@@ -172,10 +188,10 @@ where
|
||||
/// See [Self::parse].
|
||||
pub fn bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes(),
|
||||
Self::SupplyKeypair(r) => r.bytes(),
|
||||
Self::AddListenSocket(r) => r.bytes(),
|
||||
Self::AddPskBroker(r) => r.bytes(),
|
||||
Self::Ping(r) => Ref::bytes(r),
|
||||
Self::SupplyKeypair(r) => Ref::bytes(r),
|
||||
Self::AddListenSocket(r) => Ref::bytes(r),
|
||||
Self::AddPskBroker(r) => Ref::bytes(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -187,10 +203,10 @@ where
|
||||
/// Access the byte data of this reference; mutably
|
||||
pub fn bytes_mut(&mut self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes_mut(),
|
||||
Self::SupplyKeypair(r) => r.bytes_mut(),
|
||||
Self::AddListenSocket(r) => r.bytes_mut(),
|
||||
Self::AddPskBroker(r) => r.bytes_mut(),
|
||||
Self::Ping(r) => Ref::bytes_mut(r),
|
||||
Self::SupplyKeypair(r) => Ref::bytes_mut(r),
|
||||
Self::AddListenSocket(r) => Ref::bytes_mut(r),
|
||||
Self::AddPskBroker(r) => Ref::bytes_mut(r),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::{ByteSliceRefExt, Message, PingRequest, PingResponse, RequestRef, RequestResponsePair};
|
||||
use std::{collections::VecDeque, os::fd::OwnedFd};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, SplitByteSlice};
|
||||
|
||||
/// The rosenpass API implementation functions.
|
||||
///
|
||||
@@ -152,8 +152,8 @@ pub trait Server {
|
||||
req_fds: &mut VecDeque<OwnedFd>,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
ReqBuf: ByteSlice,
|
||||
ResBuf: ByteSliceMut,
|
||||
ReqBuf: ByteSlice + SplitByteSlice,
|
||||
ResBuf: ByteSliceMut + SplitByteSlice,
|
||||
{
|
||||
match p {
|
||||
RequestResponsePair::Ping((req, res)) => self.ping(req, req_fds, res),
|
||||
@@ -182,8 +182,8 @@ pub trait Server {
|
||||
res: ResBuf,
|
||||
) -> anyhow::Result<usize>
|
||||
where
|
||||
ReqBuf: ByteSlice,
|
||||
ResBuf: ByteSliceMut,
|
||||
ReqBuf: ByteSlice + SplitByteSlice,
|
||||
ResBuf: ByteSliceMut + SplitByteSlice,
|
||||
{
|
||||
let req = req.parse_request_from_prefix()?;
|
||||
// TODO: This is not pretty; This match should be moved into RequestRef
|
||||
|
||||
@@ -13,7 +13,7 @@ use signal_hook_mio::v1_0 as signal_hook_mio;
|
||||
use anyhow::{Context, Result, bail};
|
||||
use derive_builder::Builder;
|
||||
use log::{error, info, warn};
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::IntoBytes;
|
||||
|
||||
use rosenpass_util::attempt;
|
||||
use rosenpass_util::fmt::debug::NullDebug;
|
||||
|
||||
+15
-15
@@ -9,7 +9,7 @@
|
||||
//! To achieve this we utilize the zerocopy library.
|
||||
//!
|
||||
use std::mem::size_of;
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
|
||||
use super::RosenpassError;
|
||||
use rosenpass_cipher_traits::primitives::{Aead as _, Kem};
|
||||
@@ -51,7 +51,7 @@ pub type MsgEnvelopeCookie = [u8; COOKIE_SIZE];
|
||||
///
|
||||
/// ```
|
||||
/// use rosenpass::msgs::{Envelope, InitHello};
|
||||
/// use zerocopy::{AsBytes, FromBytes, Ref, FromZeroes};
|
||||
/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable};
|
||||
/// use memoffset::offset_of;
|
||||
///
|
||||
/// // Zero-initialization
|
||||
@@ -76,8 +76,8 @@ pub type MsgEnvelopeCookie = [u8; COOKIE_SIZE];
|
||||
/// assert_eq!(ih3.msg_type, 42);
|
||||
/// ```
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes, Clone)]
|
||||
pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable, Clone)]
|
||||
pub struct Envelope<M: IntoBytes + FromBytes + KnownLayout + Immutable> {
|
||||
/// [MsgType] of this message
|
||||
pub msg_type: u8,
|
||||
/// Reserved for future use
|
||||
@@ -106,7 +106,7 @@ pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
///
|
||||
/// ```
|
||||
/// use rosenpass::msgs::{Envelope, InitHello};
|
||||
/// use zerocopy::{AsBytes, FromBytes, Ref, FromZeroes};
|
||||
/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable};
|
||||
/// use memoffset::span_of;
|
||||
///
|
||||
/// // Zero initialization
|
||||
@@ -126,7 +126,7 @@ pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
/// assert_eq!(ih.payload.sidi, [1,2,3,4]);
|
||||
/// ```
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct InitHello {
|
||||
/// Randomly generated connection id
|
||||
pub sidi: [u8; 4],
|
||||
@@ -155,7 +155,7 @@ pub struct InitHello {
|
||||
///
|
||||
/// ```
|
||||
/// use rosenpass::msgs::{Envelope, RespHello};
|
||||
/// use zerocopy::{AsBytes, FromBytes, Ref, FromZeroes};
|
||||
/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable};
|
||||
/// use memoffset::span_of;
|
||||
///
|
||||
/// // Zero initialization
|
||||
@@ -175,7 +175,7 @@ pub struct InitHello {
|
||||
/// assert_eq!(ih.payload.sidi, [1,2,3,4]);
|
||||
/// ```
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct RespHello {
|
||||
/// Randomly generated connection id
|
||||
pub sidr: [u8; 4],
|
||||
@@ -206,7 +206,7 @@ pub struct RespHello {
|
||||
///
|
||||
/// ```
|
||||
/// use rosenpass::msgs::{Envelope, InitConf};
|
||||
/// use zerocopy::{AsBytes, FromBytes, Ref, FromZeroes};
|
||||
/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable};
|
||||
/// use memoffset::span_of;
|
||||
///
|
||||
/// // Zero initialization
|
||||
@@ -226,7 +226,7 @@ pub struct RespHello {
|
||||
/// assert_eq!(ih.payload.sidi, [1,2,3,4]);
|
||||
/// ```
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes, Debug)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable, Debug)]
|
||||
pub struct InitConf {
|
||||
/// Copied from InitHello
|
||||
pub sidi: [u8; 4],
|
||||
@@ -264,7 +264,7 @@ pub struct InitConf {
|
||||
///
|
||||
/// ```
|
||||
/// use rosenpass::msgs::{Envelope, EmptyData};
|
||||
/// use zerocopy::{AsBytes, FromBytes, Ref, FromZeroes};
|
||||
/// use zerocopy::{IntoBytes, FromBytes, Ref, FromZeros, KnownLayout, Immutable};
|
||||
/// use memoffset::span_of;
|
||||
///
|
||||
/// // Zero initialization
|
||||
@@ -284,7 +284,7 @@ pub struct InitConf {
|
||||
/// assert_eq!(ih.payload.sid, [1,2,3,4]);
|
||||
/// ```
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes, Clone, Copy)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable, Clone, Copy)]
|
||||
pub struct EmptyData {
|
||||
/// Copied from RespHello
|
||||
pub sid: [u8; 4],
|
||||
@@ -311,7 +311,7 @@ pub struct EmptyData {
|
||||
///
|
||||
/// [Envelope] and [InitHello] contain some extra examples on how to use structures from the [::zerocopy] crate.
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct Biscuit {
|
||||
/// H(spki) – Ident ifies the initiator
|
||||
pub pidi: [u8; KEY_LEN],
|
||||
@@ -336,7 +336,7 @@ pub struct Biscuit {
|
||||
///
|
||||
/// [Envelope] and [InitHello] contain some extra examples on how to use structures from the [::zerocopy] crate.
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct CookieReplyInner {
|
||||
/// [MsgType] of this message
|
||||
pub msg_type: u8,
|
||||
@@ -363,7 +363,7 @@ pub struct CookieReplyInner {
|
||||
///
|
||||
/// [Envelope] and [InitHello] contain some extra examples on how to use structures from the [::zerocopy] crate.
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct CookieReply {
|
||||
pub inner: CookieReplyInner,
|
||||
pub padding: [u8; size_of::<Envelope<InitHello>>() - size_of::<CookieReplyInner>()],
|
||||
|
||||
@@ -17,7 +17,7 @@ use std::{
|
||||
use anyhow::{Context, Result, bail, ensure};
|
||||
use assert_tv::{TestVector, TestVectorNOP};
|
||||
use memoffset::span_of;
|
||||
use zerocopy::{AsBytes, FromBytes, Ref};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Ref};
|
||||
|
||||
use rosenpass_cipher_traits::primitives::{
|
||||
Aead as _, AeadWithNonceInCiphertext, Kem, KeyedHashInstance,
|
||||
@@ -413,7 +413,7 @@ pub struct InitiatorHandshake {
|
||||
///
|
||||
/// Used as [KnownInitConfResponse] for now cache [EmptyData] (responder confirmation)
|
||||
/// responses to [InitConf]
|
||||
pub struct KnownResponse<ResponseType: AsBytes + FromBytes> {
|
||||
pub struct KnownResponse<ResponseType: IntoBytes + FromBytes + KnownLayout + Immutable> {
|
||||
/// When the response was initially computed
|
||||
pub received_at: Timing,
|
||||
/// Hash of the message that triggered the response; created using
|
||||
@@ -423,7 +423,7 @@ pub struct KnownResponse<ResponseType: AsBytes + FromBytes> {
|
||||
pub response: Envelope<ResponseType>,
|
||||
}
|
||||
|
||||
impl<ResponseType: AsBytes + FromBytes> Debug for KnownResponse<ResponseType> {
|
||||
impl<ResponseType: IntoBytes + FromBytes + KnownLayout + Immutable> Debug for KnownResponse<ResponseType> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("KnownResponse")
|
||||
.field("received_at", &self.received_at)
|
||||
@@ -435,7 +435,7 @@ impl<ResponseType: AsBytes + FromBytes> Debug for KnownResponse<ResponseType> {
|
||||
|
||||
#[test]
|
||||
fn known_response_format() {
|
||||
use zerocopy::FromZeroes;
|
||||
use zerocopy::FromZeros;
|
||||
|
||||
let v = KnownResponse::<[u8; 32]> {
|
||||
received_at: 42.0,
|
||||
@@ -464,7 +464,7 @@ pub type KnownResponseHash = Public<16>;
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zerocopy::FromZeroes;
|
||||
/// use zerocopy::FromZeros;
|
||||
/// use rosenpass::protocol::KnownResponseHasher;
|
||||
/// use rosenpass::msgs::{Envelope, InitConf};
|
||||
///
|
||||
@@ -508,7 +508,7 @@ impl KnownResponseHasher {
|
||||
/// # Panic & Safety
|
||||
///
|
||||
/// Panics in case of a problem with this underlying hash function
|
||||
pub fn hash<Msg: AsBytes + FromBytes>(&self, msg: &Envelope<Msg>) -> KnownResponseHash {
|
||||
pub fn hash<Msg: IntoBytes + FromBytes + KnownLayout + Immutable>(&self, msg: &Envelope<Msg>) -> KnownResponseHash {
|
||||
let data = &msg.as_bytes()[span_of!(Envelope<Msg>, msg_type..cookie)];
|
||||
// This function is only used internally and results are not propagated
|
||||
// to outside the peer. Thus, it uses SHAKE256 exclusively.
|
||||
@@ -2030,8 +2030,8 @@ impl CryptoServer {
|
||||
|
||||
let mut expected = [0u8; COOKIE_SIZE];
|
||||
|
||||
let msg_in = Ref::<&[u8], Envelope<InitHello>>::new(rx_buf)
|
||||
.ok_or(RosenpassError::BufferSizeMismatch)?;
|
||||
let msg_in = Ref::<&[u8], Envelope<InitHello>>::from_bytes(rx_buf)
|
||||
.map_err(|_| RosenpassError::BufferSizeMismatch)?;
|
||||
expected.copy_from_slice(
|
||||
&hash_domains::cookie(KeyedHash::keyed_shake256())?
|
||||
.mix(&cookie_value)?
|
||||
@@ -2175,7 +2175,7 @@ impl CryptoServer {
|
||||
let peer = match msg_type {
|
||||
Ok(MsgType::InitHello) => {
|
||||
let msg_in: Ref<&[u8], Envelope<InitHello>> =
|
||||
Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?;
|
||||
Ref::from_bytes(rx_buf).map_err(|_| RosenpassError::BufferSizeMismatch)?;
|
||||
|
||||
// At this point, we do not know the hash functon used by the peer, thus we try both,
|
||||
// with a preference for SHAKE256.
|
||||
@@ -2209,7 +2209,7 @@ impl CryptoServer {
|
||||
}
|
||||
Ok(MsgType::RespHello) => {
|
||||
let msg_in: Ref<&[u8], Envelope<RespHello>> =
|
||||
Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?;
|
||||
Ref::from_bytes(rx_buf).map_err(|_| RosenpassError::BufferSizeMismatch)?;
|
||||
|
||||
let mut msg_out = truncating_cast_into::<Envelope<InitConf>>(tx_buf)?;
|
||||
let peer = self.handle_resp_hello(&msg_in.payload, &mut msg_out.payload)?;
|
||||
@@ -2226,7 +2226,7 @@ impl CryptoServer {
|
||||
}
|
||||
Ok(MsgType::InitConf) => {
|
||||
let msg_in: Ref<&[u8], Envelope<InitConf>> =
|
||||
Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?;
|
||||
Ref::from_bytes(rx_buf).map_err(|_| RosenpassError::BufferSizeMismatch)?;
|
||||
|
||||
let mut msg_out = truncating_cast_into::<Envelope<EmptyData>>(tx_buf)?;
|
||||
|
||||
@@ -2245,7 +2245,7 @@ impl CryptoServer {
|
||||
.map(|v| v.response.borrow())
|
||||
// Invalid! Found peer no with cache in index but the cache does not exist
|
||||
.unwrap();
|
||||
copy_slice(cached.as_bytes()).to(msg_out.as_bytes_mut());
|
||||
copy_slice(cached.as_bytes()).to(msg_out.as_mut_bytes());
|
||||
peer
|
||||
}
|
||||
|
||||
@@ -2294,13 +2294,13 @@ impl CryptoServer {
|
||||
}
|
||||
Ok(MsgType::EmptyData) => {
|
||||
let msg_in: Ref<&[u8], Envelope<EmptyData>> =
|
||||
Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?;
|
||||
Ref::from_bytes(rx_buf).map_err(|_| RosenpassError::BufferSizeMismatch)?;
|
||||
|
||||
self.handle_resp_conf(&msg_in, seal_broken.to_string())?
|
||||
}
|
||||
Ok(MsgType::CookieReply) => {
|
||||
let msg_in: Ref<&[u8], CookieReply> =
|
||||
Ref::new(rx_buf).ok_or(RosenpassError::BufferSizeMismatch)?;
|
||||
Ref::from_bytes(rx_buf).map_err(|_| RosenpassError::BufferSizeMismatch)?;
|
||||
let peer = self.handle_cookie_reply(&msg_in)?;
|
||||
len = 0;
|
||||
peer
|
||||
@@ -2341,8 +2341,8 @@ impl CryptoServer {
|
||||
///
|
||||
/// To save some code, the function returns the size of the message,
|
||||
/// but the same could be easily achieved by calling [size_of] with the
|
||||
/// message type or by calling [AsBytes::as_bytes] on the message reference.
|
||||
pub fn seal_and_commit_msg<M: AsBytes + FromBytes>(
|
||||
/// message type or by calling [IntoBytes::as_bytes] on the message reference.
|
||||
pub fn seal_and_commit_msg<M: IntoBytes + FromBytes + KnownLayout + Immutable>(
|
||||
&mut self,
|
||||
peer: PeerPtr,
|
||||
msg_type: MsgType,
|
||||
@@ -3065,7 +3065,7 @@ impl IniHsPtr {
|
||||
|
||||
impl<M> Envelope<M>
|
||||
where
|
||||
M: AsBytes + FromBytes,
|
||||
M: IntoBytes + FromBytes + KnownLayout + Immutable,
|
||||
{
|
||||
/// Internal business logic: Calculate the message authentication code (`mac`) and also append cookie value
|
||||
pub fn seal(&mut self, peer: PeerPtr, srv: &CryptoServer) -> Result<()> {
|
||||
@@ -3094,7 +3094,7 @@ where
|
||||
|
||||
impl<M> Envelope<M>
|
||||
where
|
||||
M: AsBytes + FromBytes,
|
||||
M: IntoBytes + FromBytes + KnownLayout + Immutable,
|
||||
{
|
||||
/// Internal business logic: Check the message authentication code produced by [Self::seal]
|
||||
pub fn check_seal(&self, srv: &CryptoServer, shake_or_blake: KeyedHash) -> Result<bool> {
|
||||
@@ -3309,7 +3309,7 @@ impl HandshakeState {
|
||||
let test_values: StoreBiscuitTestValues = TV::initialize_values();
|
||||
let mut biscuit = Secret::<BISCUIT_PT_LEN>::zero(); // pt buffer
|
||||
let mut biscuit: Ref<&mut [u8], Biscuit> =
|
||||
Ref::new(biscuit.secret_mut().as_mut_slice()).unwrap();
|
||||
Ref::from_bytes(biscuit.secret_mut().as_mut_slice()).unwrap();
|
||||
|
||||
// calculate pt contents
|
||||
biscuit
|
||||
@@ -3371,9 +3371,9 @@ impl HandshakeState {
|
||||
// Allocate and decrypt the biscuit data
|
||||
let mut biscuit = Secret::<BISCUIT_PT_LEN>::zero(); // pt buf
|
||||
let mut biscuit: Ref<&mut [u8], Biscuit> =
|
||||
Ref::new(biscuit.secret_mut().as_mut_slice()).unwrap();
|
||||
Ref::from_bytes(biscuit.secret_mut().as_mut_slice()).unwrap();
|
||||
XAead.decrypt_with_nonce_in_ctxt(
|
||||
biscuit.as_bytes_mut(),
|
||||
biscuit.as_mut_bytes(),
|
||||
bk.get(srv).value.secret(),
|
||||
&ad,
|
||||
biscuit_ct,
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::{borrow::BorrowMut, fmt::Display, net::SocketAddrV4, ops::DerefMut};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use serial_test::serial;
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
use zerocopy::{FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Ref};
|
||||
|
||||
use rosenpass_cipher_traits::primitives::Kem;
|
||||
use rosenpass_ciphers::StaticKem;
|
||||
@@ -539,10 +539,13 @@ fn init_conf_retransmission(protocol_version: ProtocolVersion) -> anyhow::Result
|
||||
srv.initiate_handshake(peer, buf.as_mut_slice())?
|
||||
.discard_result();
|
||||
let msg = truncating_cast_into::<Envelope<InitHello>>(buf.borrow_mut())?;
|
||||
Ok(msg.read())
|
||||
Ok(Ref::read(&msg))
|
||||
}
|
||||
|
||||
fn proc_msg<Rx: AsBytes + FromBytes, Tx: AsBytes + FromBytes>(
|
||||
fn proc_msg<
|
||||
Rx: IntoBytes + FromBytes + KnownLayout + Immutable,
|
||||
Tx: IntoBytes + FromBytes + KnownLayout + Immutable,
|
||||
>(
|
||||
srv: &mut CryptoServer,
|
||||
rx: &Envelope<Rx>,
|
||||
) -> anyhow::Result<Envelope<Tx>> {
|
||||
@@ -552,7 +555,7 @@ fn init_conf_retransmission(protocol_version: ProtocolVersion) -> anyhow::Result
|
||||
.context("Failed to produce RespHello message")?
|
||||
.discard_result();
|
||||
let msg = truncating_cast_into::<Envelope<Tx>>(buf.borrow_mut())?;
|
||||
Ok(msg.read())
|
||||
Ok(Ref::read(&msg))
|
||||
}
|
||||
|
||||
fn proc_init_hello(
|
||||
@@ -583,17 +586,21 @@ fn init_conf_retransmission(protocol_version: ProtocolVersion) -> anyhow::Result
|
||||
}
|
||||
|
||||
// TODO: Implement Clone on our message types
|
||||
fn clone_msg<Msg: AsBytes + FromBytes>(msg: &Msg) -> anyhow::Result<Msg> {
|
||||
Ok(truncating_cast_into_nomut::<Msg>(msg.as_bytes())?.read())
|
||||
fn clone_msg<Msg: IntoBytes + FromBytes + KnownLayout + Immutable>(
|
||||
msg: &Msg,
|
||||
) -> anyhow::Result<Msg> {
|
||||
Ok(Ref::read(&truncating_cast_into_nomut::<Msg>(
|
||||
msg.as_bytes(),
|
||||
)?))
|
||||
}
|
||||
|
||||
fn break_payload<Msg: AsBytes + FromBytes>(
|
||||
fn break_payload<Msg: IntoBytes + FromBytes + KnownLayout + Immutable>(
|
||||
srv: &mut CryptoServer,
|
||||
peer: PeerPtr,
|
||||
msg: &Envelope<Msg>,
|
||||
) -> anyhow::Result<Envelope<Msg>> {
|
||||
let mut msg = clone_msg(msg)?;
|
||||
msg.as_bytes_mut()[memoffset::offset_of!(Envelope<Msg>, payload)] ^= 0x01;
|
||||
msg.as_mut_bytes()[memoffset::offset_of!(Envelope<Msg>, payload)] ^= 0x01;
|
||||
msg.seal(peer, srv)?; // Recalculate seal; we do not want to focus on "seal broken" errs
|
||||
Ok(msg)
|
||||
}
|
||||
|
||||
@@ -2,20 +2,20 @@
|
||||
|
||||
use std::mem::size_of;
|
||||
|
||||
use zerocopy::{FromBytes, Ref};
|
||||
use zerocopy::{FromBytes, Immutable, KnownLayout, Ref};
|
||||
|
||||
use crate::RosenpassError;
|
||||
|
||||
/// Used to parse a network message using [zerocopy]
|
||||
pub fn truncating_cast_into<T: FromBytes>(
|
||||
pub fn truncating_cast_into<T: FromBytes + KnownLayout + Immutable>(
|
||||
buf: &mut [u8],
|
||||
) -> Result<Ref<&mut [u8], T>, RosenpassError> {
|
||||
Ref::new(&mut buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
|
||||
Ref::from_bytes(&mut buf[..size_of::<T>()]).map_err(|_| RosenpassError::BufferSizeMismatch)
|
||||
}
|
||||
|
||||
/// Used to parse a network message using [zerocopy], mutably
|
||||
pub fn truncating_cast_into_nomut<T: FromBytes>(
|
||||
pub fn truncating_cast_into_nomut<T: FromBytes + KnownLayout + Immutable>(
|
||||
buf: &[u8],
|
||||
) -> Result<Ref<&[u8], T>, RosenpassError> {
|
||||
Ref::new(&buf[..size_of::<T>()]).ok_or(RosenpassError::BufferSizeMismatch)
|
||||
Ref::from_bytes(&buf[..size_of::<T>()]).map_err(|_| RosenpassError::BufferSizeMismatch)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ use rosenpass_util::{
|
||||
};
|
||||
use std::os::fd::{AsFd, AsRawFd};
|
||||
use tempfile::TempDir;
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::IntoBytes;
|
||||
|
||||
struct KillChild(std::process::Child);
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ use rosenpass_util::{
|
||||
};
|
||||
use rosenpass_util::{mem::DiscardResultExt, zerocopy::ZerocopySliceExt};
|
||||
use tempfile::TempDir;
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::IntoBytes;
|
||||
|
||||
use rosenpass::config::ProtocolVersion;
|
||||
use rosenpass::protocol::basic_types::SymKey;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! A module providing the [`RefMaker`] type and its associated methods for constructing
|
||||
//! [`zerocopy::Ref`] references from byte buffers.
|
||||
|
||||
use anyhow::{Context, ensure};
|
||||
use anyhow::ensure;
|
||||
use std::marker::PhantomData;
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Immutable, KnownLayout, Ref, SplitByteSlice};
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use crate::zeroize::ZeroizedExt;
|
||||
@@ -19,10 +19,10 @@ use crate::zeroize::ZeroizedExt;
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes, Ref};///
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable, Ref};///
|
||||
/// # use rosenpass_util::zerocopy::RefMaker;
|
||||
///
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Header {
|
||||
/// field1: u32,
|
||||
@@ -82,7 +82,10 @@ impl<B, T> RefMaker<B, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
impl<B: ByteSlice, T> RefMaker<B, T>
|
||||
where
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
/// Parses the buffer into a [`zerocopy::Ref<B, T>`].
|
||||
///
|
||||
/// This will fail if the buffer is smaller than `size_of::<T>`.
|
||||
@@ -94,10 +97,10 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes, Ref};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable, Ref};
|
||||
/// # use rosenpass_util::zerocopy::RefMaker;
|
||||
///
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes, Debug)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Debug)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data(u32);
|
||||
///
|
||||
@@ -119,8 +122,24 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
/// assert_eq!(parse_error.to_string(), "Parser error!");
|
||||
/// ```
|
||||
pub fn parse(self) -> anyhow::Result<Ref<B, T>> {
|
||||
let have = self.buf.len();
|
||||
let need = Self::target_size();
|
||||
ensure!(
|
||||
need <= have,
|
||||
"Buffer is undersized at {have} bytes (need {need} bytes)!"
|
||||
);
|
||||
Ref::<B, T>::from_bytes(self.buf).map_err(|_| anyhow::anyhow!("Parser error!"))
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: SplitByteSlice, T> RefMaker<B, T> {
|
||||
/// Splits the buffer at the target type size, preserving the byte-slice
|
||||
/// wrapper type on both halves.
|
||||
fn split_at_target(self) -> anyhow::Result<(B, B)> {
|
||||
self.ensure_fit()?;
|
||||
Ref::<B, T>::new(self.buf).context("Parser error!")
|
||||
self.buf
|
||||
.split_at(Self::target_size())
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))
|
||||
}
|
||||
|
||||
/// Splits the internal buffer into a `RefMaker` containing a buffer with
|
||||
@@ -141,8 +160,7 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
/// assert_eq!(tail, &[5,6,7,8]);
|
||||
/// ```
|
||||
pub fn from_prefix_with_tail(self) -> anyhow::Result<(Self, B)> {
|
||||
self.ensure_fit()?;
|
||||
let (head, tail) = self.buf.split_at(Self::target_size());
|
||||
let (head, tail) = self.split_at_target()?;
|
||||
Ok((Self::new(head), tail))
|
||||
}
|
||||
|
||||
@@ -164,8 +182,7 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
/// assert_eq!(tail.bytes(), &[5,6,7,8,9,10]);
|
||||
/// ```
|
||||
pub fn split_prefix(self) -> anyhow::Result<(Self, Self)> {
|
||||
self.ensure_fit()?;
|
||||
let (head, tail) = self.buf.split_at(Self::target_size());
|
||||
let (head, tail) = self.split_at_target()?;
|
||||
Ok((Self::new(head), Self::new(tail)))
|
||||
}
|
||||
|
||||
@@ -204,7 +221,10 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
pub fn from_suffix_with_head(self) -> anyhow::Result<(Self, B)> {
|
||||
self.ensure_fit()?;
|
||||
let point = self.bytes().len() - Self::target_size();
|
||||
let (head, tail) = self.buf.split_at(point);
|
||||
let (head, tail) = self
|
||||
.buf
|
||||
.split_at(point)
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))?;
|
||||
Ok((Self::new(tail), head))
|
||||
}
|
||||
|
||||
@@ -227,7 +247,10 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
pub fn split_suffix(self) -> anyhow::Result<(Self, Self)> {
|
||||
self.ensure_fit()?;
|
||||
let point = self.bytes().len() - Self::target_size();
|
||||
let (head, tail) = self.buf.split_at(point);
|
||||
let (head, tail) = self
|
||||
.buf
|
||||
.split_at(point)
|
||||
.map_err(|_| anyhow::anyhow!("Buffer is undersized!"))?;
|
||||
Ok((Self::new(head), Self::new(tail)))
|
||||
}
|
||||
|
||||
@@ -282,7 +305,10 @@ impl<B: ByteSlice, T> RefMaker<B, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: ByteSliceMut, T> RefMaker<B, T> {
|
||||
impl<B: ByteSliceMut, T> RefMaker<B, T>
|
||||
where
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
/// Creates a zeroized reference of type `T` from the buffer.
|
||||
///
|
||||
/// # Errors
|
||||
@@ -292,9 +318,9 @@ impl<B: ByteSliceMut, T> RefMaker<B, T> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes, Ref}; ///
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable, Ref}; ///
|
||||
/// # use rosenpass_util::zerocopy::RefMaker;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data([u8; 4]);
|
||||
///
|
||||
@@ -305,7 +331,9 @@ impl<B: ByteSliceMut, T> RefMaker<B, T> {
|
||||
pub fn make_zeroized(self) -> anyhow::Result<Ref<B, T>> {
|
||||
self.zeroized().parse()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: ByteSliceMut, T> RefMaker<B, T> {
|
||||
/// Returns a mutable reference to the underlying bytes.
|
||||
pub fn bytes_mut(&mut self) -> &mut [u8] {
|
||||
self.buf_mut().deref_mut()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Extension traits for converting `Ref<B, T>` into references backed by
|
||||
//! standard slices.
|
||||
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Immutable, KnownLayout, Ref};
|
||||
|
||||
/// A trait for converting a `Ref<B, T>` into a `Ref<&[u8], T>`.
|
||||
///
|
||||
@@ -16,9 +16,9 @@ pub trait ZerocopyEmancipateExt<B, T> {
|
||||
///
|
||||
/// ```
|
||||
/// # use std::ops::Deref;
|
||||
/// # use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeroes, Ref};
|
||||
/// # use zerocopy::{IntoBytes, ByteSlice, FromBytes, Ref, FromZeros, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopyEmancipateExt;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data(u32);
|
||||
/// #[repr(align(4))]
|
||||
@@ -44,9 +44,9 @@ pub trait ZerocopyEmancipateMutExt<B, T> {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes, Ref};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable, Ref};
|
||||
/// # use rosenpass_util::zerocopy::{ZerocopyEmancipateMutExt};
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data(u32);
|
||||
/// #[repr(align(4))]
|
||||
@@ -65,17 +65,19 @@ pub trait ZerocopyEmancipateMutExt<B, T> {
|
||||
impl<B, T> ZerocopyEmancipateExt<B, T> for Ref<B, T>
|
||||
where
|
||||
B: ByteSlice,
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
fn emancipate(&self) -> Ref<&[u8], T> {
|
||||
Ref::new(self.bytes()).unwrap()
|
||||
Ref::from_bytes(Ref::bytes(self)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, T> ZerocopyEmancipateMutExt<B, T> for Ref<B, T>
|
||||
where
|
||||
B: ByteSliceMut,
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
fn emancipate_mut(&mut self) -> Ref<&mut [u8], T> {
|
||||
Ref::new(self.bytes_mut()).unwrap()
|
||||
Ref::from_bytes(Ref::bytes_mut(self)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Extension traits for parsing slices into [`zerocopy::Ref`] values using the
|
||||
//! [`RefMaker`] abstraction.
|
||||
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut, Immutable, KnownLayout, Ref, SplitByteSlice};
|
||||
|
||||
use super::RefMaker;
|
||||
|
||||
@@ -15,10 +15,10 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::{RefMaker, ZerocopySliceExt};
|
||||
///
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data(u32);
|
||||
///
|
||||
@@ -39,10 +39,10 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopySliceExt;
|
||||
///
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data(u16, u16);
|
||||
/// #[repr(align(4))]
|
||||
@@ -52,7 +52,10 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// assert_eq!(data_ref.0, 0x0201);
|
||||
/// assert_eq!(data_ref.1, 0x0403);
|
||||
/// ```
|
||||
fn zk_parse<T>(self) -> anyhow::Result<Ref<Self, T>> {
|
||||
fn zk_parse<T>(self) -> anyhow::Result<Ref<Self, T>>
|
||||
where
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
self.zk_ref_maker().parse()
|
||||
}
|
||||
|
||||
@@ -67,9 +70,9 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopySliceExt;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Header(u32);
|
||||
/// #[repr(align(4))]
|
||||
@@ -80,7 +83,11 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// let header_ref = bytes.0.zk_parse_prefix::<Header>().unwrap();
|
||||
/// assert_eq!(header_ref.0, 0xDDCCBBAA);
|
||||
/// ```
|
||||
fn zk_parse_prefix<T>(self) -> anyhow::Result<Ref<Self, T>> {
|
||||
fn zk_parse_prefix<T>(self) -> anyhow::Result<Ref<Self, T>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
self.zk_ref_maker().from_prefix()?.parse()
|
||||
}
|
||||
|
||||
@@ -95,9 +102,9 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopySliceExt;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Header(u32);
|
||||
/// #[repr(align(4))]
|
||||
@@ -108,7 +115,11 @@ pub trait ZerocopySliceExt: Sized + ByteSlice {
|
||||
/// let header_ref = bytes.0.zk_parse_suffix::<Header>().unwrap();
|
||||
/// assert_eq!(header_ref.0, 0x30201000);
|
||||
/// ```
|
||||
fn zk_parse_suffix<T>(self) -> anyhow::Result<Ref<Self, T>> {
|
||||
fn zk_parse_suffix<T>(self) -> anyhow::Result<Ref<Self, T>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
self.zk_ref_maker().from_suffix()?.parse()
|
||||
}
|
||||
}
|
||||
@@ -131,9 +142,9 @@ pub trait ZerocopyMutSliceExt: ZerocopySliceExt + Sized + ByteSliceMut {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopyMutSliceExt;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data([u8; 4]);
|
||||
/// #[repr(align(4))]
|
||||
@@ -143,7 +154,10 @@ pub trait ZerocopyMutSliceExt: ZerocopySliceExt + Sized + ByteSliceMut {
|
||||
/// assert_eq!(data_ref.0, [0,0,0,0]);
|
||||
/// assert_eq!(bytes.0, [0, 0, 0, 0]);
|
||||
/// ```
|
||||
fn zk_zeroized<T>(self) -> anyhow::Result<Ref<Self, T>> {
|
||||
fn zk_zeroized<T>(self) -> anyhow::Result<Ref<Self, T>>
|
||||
where
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
self.zk_ref_maker().make_zeroized()
|
||||
}
|
||||
|
||||
@@ -159,9 +173,9 @@ pub trait ZerocopyMutSliceExt: ZerocopySliceExt + Sized + ByteSliceMut {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopyMutSliceExt;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data([u8; 4]);
|
||||
/// #[repr(align(4))]
|
||||
@@ -171,7 +185,11 @@ pub trait ZerocopyMutSliceExt: ZerocopySliceExt + Sized + ByteSliceMut {
|
||||
/// assert_eq!(data_ref.0, [0,0,0,0]);
|
||||
/// assert_eq!(bytes.0, [0, 0, 0, 0, 0xFF, 0xFF]);
|
||||
/// ```
|
||||
fn zk_zeroized_from_prefix<T>(self) -> anyhow::Result<Ref<Self, T>> {
|
||||
fn zk_zeroized_from_prefix<T>(self) -> anyhow::Result<Ref<Self, T>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
self.zk_ref_maker().from_prefix()?.make_zeroized()
|
||||
}
|
||||
|
||||
@@ -187,9 +205,9 @@ pub trait ZerocopyMutSliceExt: ZerocopySliceExt + Sized + ByteSliceMut {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
/// # use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
/// # use rosenpass_util::zerocopy::ZerocopyMutSliceExt;
|
||||
/// #[derive(FromBytes, FromZeroes, AsBytes)]
|
||||
/// #[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
|
||||
/// #[repr(C)]
|
||||
/// struct Data([u8; 4]);
|
||||
/// #[repr(align(4))]
|
||||
@@ -199,7 +217,11 @@ pub trait ZerocopyMutSliceExt: ZerocopySliceExt + Sized + ByteSliceMut {
|
||||
/// assert_eq!(data_ref.0, [0,0,0,0]);
|
||||
/// assert_eq!(bytes.0, [0xFF, 0xFF, 0, 0, 0, 0]);
|
||||
/// ```
|
||||
fn zk_zeroized_from_suffix<T>(self) -> anyhow::Result<Ref<Self, T>> {
|
||||
fn zk_zeroized_from_suffix<T>(self) -> anyhow::Result<Ref<Self, T>>
|
||||
where
|
||||
Self: SplitByteSlice,
|
||||
T: KnownLayout + Immutable,
|
||||
{
|
||||
self.zk_ref_maker().from_suffix()?.make_zeroized()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,8 +170,8 @@ where
|
||||
let typ = msgs::MsgType::try_from(*typ)?;
|
||||
let msgs::MsgType::SetPsk = typ; // Assert type
|
||||
|
||||
let res = zerocopy::Ref::<&[u8], Envelope<SetPskResponse>>::new(res)
|
||||
.ok_or(invalid_msg_poller())?;
|
||||
let res = zerocopy::Ref::<&[u8], Envelope<SetPskResponse>>::from_bytes(res)
|
||||
.map_err(|_| invalid_msg_poller())?;
|
||||
let res: &msgs::SetPskResponse = &res.payload;
|
||||
let res: msgs::SetPskResponseReturnCode = res
|
||||
.return_code
|
||||
@@ -200,8 +200,9 @@ where
|
||||
let mut req = [0u8; BUF_SIZE];
|
||||
|
||||
// Construct message view
|
||||
let mut req = zerocopy::Ref::<&mut [u8], Envelope<msgs::SetPskRequest>>::new(&mut req)
|
||||
.ok_or(MsgError)?;
|
||||
let mut req =
|
||||
zerocopy::Ref::<&mut [u8], Envelope<msgs::SetPskRequest>>::from_bytes(&mut req[..])
|
||||
.map_err(|_| MsgError)?;
|
||||
|
||||
// Populate envelope
|
||||
req.msg_type = msgs::MsgType::SetPsk as u8;
|
||||
@@ -219,7 +220,7 @@ where
|
||||
// Send message
|
||||
self.io
|
||||
.borrow_mut()
|
||||
.send_msg(req.bytes())
|
||||
.send_msg(zerocopy::Ref::bytes(&req))
|
||||
.map_err(IoError)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use std::str::{Utf8Error, from_utf8};
|
||||
|
||||
use zerocopy::{AsBytes, FromBytes, FromZeroes};
|
||||
use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable};
|
||||
|
||||
/// The number of bytes reserved for overhead when packaging data.
|
||||
pub const ENVELOPE_OVERHEAD: usize = 1 + 3;
|
||||
@@ -15,8 +15,8 @@ pub const RESPONSE_MSG_BUFFER_SIZE: usize = ENVELOPE_OVERHEAD + 1;
|
||||
|
||||
/// Envelope for messages being passed around.
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct Envelope<M: IntoBytes + FromBytes + KnownLayout + Immutable> {
|
||||
/// [MsgType] of this message
|
||||
pub msg_type: u8,
|
||||
/// Reserved for future use
|
||||
@@ -29,7 +29,7 @@ pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
/// # Example
|
||||
///
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct SetPskRequest {
|
||||
/// The pre-shared key.
|
||||
pub psk: [u8; 32],
|
||||
@@ -85,7 +85,7 @@ impl SetPskRequest {
|
||||
|
||||
/// Message format for response to the set pre-shared key operation.
|
||||
#[repr(packed)]
|
||||
#[derive(AsBytes, FromBytes, FromZeroes)]
|
||||
#[derive(IntoBytes, FromBytes, KnownLayout, Immutable)]
|
||||
pub struct SetPskResponse {
|
||||
pub return_code: u8,
|
||||
}
|
||||
|
||||
@@ -78,14 +78,15 @@ where
|
||||
let typ = msgs::MsgType::try_from(*typ)?;
|
||||
let msgs::MsgType::SetPsk = typ; // Assert type
|
||||
|
||||
let req =
|
||||
zerocopy::Ref::<&[u8], Envelope<SetPskRequest>>::new(req).ok_or(InvalidMessage)?;
|
||||
let req = zerocopy::Ref::<&[u8], Envelope<SetPskRequest>>::from_bytes(req)
|
||||
.map_err(|_| InvalidMessage)?;
|
||||
let mut res =
|
||||
zerocopy::Ref::<&mut [u8], Envelope<SetPskResponse>>::new(res).ok_or(InvalidMessage)?;
|
||||
zerocopy::Ref::<&mut [u8], Envelope<SetPskResponse>>::from_bytes(&mut res[..])
|
||||
.map_err(|_| InvalidMessage)?;
|
||||
res.msg_type = msgs::MsgType::SetPsk as u8;
|
||||
self.handle_set_psk(&req.payload, &mut res.payload)?;
|
||||
|
||||
Ok(res.bytes().len())
|
||||
Ok(zerocopy::Ref::bytes(&res).len())
|
||||
}
|
||||
|
||||
/// Sets the pre-shared key for the interface identified in `req` to the pre-shared key
|
||||
@@ -138,7 +139,7 @@ mod tests {
|
||||
use crate::brokers::netlink::SetPskError;
|
||||
use crate::{SerializedBrokerConfig, WireGuardBroker};
|
||||
use rosenpass_secret_memory::{Secret, secret_policy_use_only_malloc_secrets};
|
||||
use zerocopy::AsBytes;
|
||||
use zerocopy::IntoBytes;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct MockWireGuardBroker {
|
||||
|
||||
Reference in New Issue
Block a user