//! Service library for implementing hwbcc servers in Rust
usecrate::{
hwbcc_req_hdr, hwbcc_req_sign_data, BccCmd, BccMsg, HwBccError, HwBccMode, HwBccResponse,
SigningAlgorithm, HWBCC_MAX_AAD_SIZE, HWBCC_MAX_DATA_TO_SIGN_SIZE,
}; use alloc::rc::Rc; use core::mem::size_of; use num_traits::FromPrimitive; use tipc::{ConnectResult, Deserialize, Handle, MessageResult, PortCfg, Service, TipcError, Uuid}; use trusty_std::alloc::TryAllocFrom; use trusty_sys::Error; use zerocopy::FromBytes;
const HWBCC_MAX_MESSAGE_SIZE: usize = size_of::<hwbcc_req_hdr>()
+ size_of::<hwbcc_req_sign_data>()
+ HWBCC_MAX_AAD_SIZE as usize
+ HWBCC_MAX_DATA_TO_SIGN_SIZE as usize;
pubstruct RequestContext { pub peer: Uuid,
}
/// A raw buffer that holds a serialized `BccMsg` and allows /// for extraction of a `BccMsg` after deserialization. /// /// Deserialization itself performs no validation and only /// copies the input buffer into the `RawBccMsgBuffer`. All /// validation occurs when calling `to_req_msg`. /// /// This approach is needed because `BccMsg` is implemented /// to support serializing from borrowed data in the client. /// As such, it specifies an explicit lifetime, which is not /// supported by the `Message` type of the `Service` trait. pubstruct RawBccMsgBuffer(Vec<u8>);
if sign_header.aad_size > HWBCC_MAX_AAD_SIZE {
log::error!( "Failed to deserialize. aad size {} is greater than max of {}",
sign_header.aad_size,
HWBCC_MAX_AAD_SIZE
); return Err(HwBccError::BadLen);
}
if sign_header.data_size as u32 > HWBCC_MAX_DATA_TO_SIGN_SIZE {
log::error!( "Failed to deserialize. data size {} is greater than max of {}",
sign_header.aad_size,
HWBCC_MAX_AAD_SIZE
); return Err(HwBccError::BadLen);
}
// hwbcc defines sizes with different data types, and at the end of the // day we need these as usize so we can use them to index into the remaining // data. `data_size` is u16 so it infallibly converts. `aad_size` is unfortunately // u32, but hwbcc defines `MAX_AAD_SIZE` as 512 bytes, which should always fit in // rust's usize. let data_size: usize = sign_header.data_size.into(); let aad_size: usize = sign_header.aad_size.try_into()?;
let required_rest_len = data_size + aad_size;
if rest.len() < required_rest_len {
log::error!("hwbcc_req_sign_data header sizes are larger than the provided buffer"); return Err(HwBccError::BadLen);
}
// We've checked above to ensure that these indices are not out of range. // In the event that a larger buffer was sent than the configured sizes, this // truncates to the sizes provided in the header. let _ = msg.add_signing_req(
algorithm,
&rest[..data_size],
&rest[data_size..required_rest_len],
);
#[test] fn deserialize_fail_if_sign_header_too_small() { letmut serializer = TestSerializer::default(); let msg = BccMsg::new(BccCmd::GetBcc, HwBccMode::Test, 5); let _ = msg.serialize(&mut serializer).expect("serialization of BccMsg failed");
// The signing header is encoded directly after the main header so this results in // a signing header of length 1.
serializer.buffers.push(0xAA); let deserialized =
&RawBccMsgBuffer::deserialize(&mut serializer.buffers, &mut serializer.handles)
.expect("deserialization failed");
// The end of a message is the AAD data. We're adding erroneous data on the end here // and ensuring that when deserializing, it's ignored.
serializer.buffers.push(0xAA); let deserialized =
&RawBccMsgBuffer::deserialize(&mut serializer.buffers, &mut serializer.handles)
.expect("deserialization failed");
expect_eq!(deserialized.to_req_msg().expect("failed to get deserialized BccMsg"), msg);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.