diff --git a/consts/src/lib.rs b/consts/src/lib.rs index 8d49086d..ffab8c32 100644 --- a/consts/src/lib.rs +++ b/consts/src/lib.rs @@ -88,8 +88,7 @@ mod common { #[derive(Debug)] pub struct EADItem { - pub(crate) label: u8, - pub(crate) is_critical: bool, + pub(crate) label: i16, // TODO[ead]: have adjustable (smaller) length for this buffer pub(crate) value: Option, } @@ -122,11 +121,15 @@ mod common { impl EADTrait for EADItem { #[inline(always)] // Assist const propagation that removes error states fn new(label: u16, is_critical: bool, value: Option<&[u8]>) -> Result { + let mut label: i16 = label + .try_into() + .map_err(|_| EADNewError::InexpressibleLabel)?; + if is_critical { + // As it has been positive before, this can not underflow. + label = -label; + } Ok(EADItem { - label: label - .try_into() - .map_err(|_| EADNewError::InexpressibleLabel)?, - is_critical, + label, value: value .map(|v| v.try_into().map_err(|_| EADNewError::SizeExceeded)) .transpose()?, @@ -134,11 +137,11 @@ mod common { } fn label(&self) -> u16 { - self.label.into() + self.label.unsigned_abs() } fn is_critical(&self) -> bool { - self.is_critical + self.label < 0 } fn value(&self) -> Option<&[u8]> { @@ -149,7 +152,7 @@ mod common { pub const MAX_MESSAGE_SIZE_LEN: usize = 64; pub const MAX_EAD_SIZE_LEN: usize = 64; pub type EADMessageBuffer = EdhocMessageBuffer; // TODO: make it of size MAX_EAD_SIZE_LEN - pub const EAD_ZEROCONF_LABEL: u8 = 0x1; // NOTE: in lake-authz-draft-02 it is still TBD1 + pub const EAD_ZEROCONF_LABEL: u16 = 0x1; // NOTE: in lake-authz-draft-02 it is still TBD1 pub const ID_CRED_LEN: usize = 4; pub const SUITES_LEN: usize = 9; @@ -327,8 +330,8 @@ mod hacspec { } fn from_public_item(item: &EADItem) -> Self { EADItemHacspec { - label: U8(item.label), - is_critical: item.is_critical, + label: U8(item.label().try_into().unwrap()), + is_critical: item.is_critical(), value: match &item.value { Some(value) => Some(EdhocMessageBufferHacspec::from_public_buffer(value)), None => None, @@ -336,14 +339,15 @@ mod hacspec { } } fn to_public_item(&self) -> EADItem { - EADItem { - label: self.label.declassify(), - is_critical: self.is_critical, - value: match &self.value { - Some(value) => Some(value.to_public_buffer()), - None => None, - }, - } + let value_full = self + .value + .as_ref() + .map(|v| (v.content.to_public_array(), v.len)); + let value = value_full + .as_ref() + .map(|(value, len)| &value[..(*len as usize)]); + + EADItem::new(self.label.declassify().into(), self.is_critical, value).unwrap() } } diff --git a/ead/edhoc-ead-zeroconf/src/lib.rs b/ead/edhoc-ead-zeroconf/src/lib.rs index c10c146c..bfe56f5f 100644 --- a/ead/edhoc-ead-zeroconf/src/lib.rs +++ b/ead/edhoc-ead-zeroconf/src/lib.rs @@ -39,7 +39,7 @@ pub fn ead_initiator_set_global_state(new_state: EADInitiatorState) { pub fn i_prepare_ead_1() -> Option { // TODO: build Voucher_Info (LOC_W, ENC_ID), and append it to the buffer - let mut ead_1 = EADItem::new(EAD_ZEROCONF_LABEL.into(), true, None) + let mut ead_1 = EADItem::new(EAD_ZEROCONF_LABEL, true, None) // Const propagation will remove this. .unwrap(); @@ -114,7 +114,7 @@ pub fn r_process_ead_1(ead_1: EADItem) -> Result<(), ()> { pub fn r_prepare_ead_2() -> Option { // TODO: append Voucher (H(message_1), CRED_V) to the buffer - let ead_2 = EADItem::new(EAD_ZEROCONF_LABEL.into(), true, None).unwrap(); + let ead_2 = EADItem::new(EAD_ZEROCONF_LABEL, true, None).unwrap(); // NOTE: see the note in lib.rs::test_ead // state.protocol_state = EADResponderProtocolState::WaitMessage3;