Skip to content

Commit

Permalink
refactor: Store signed label in EAD
Browse files Browse the repository at this point in the history
This increases the usable size of EAD labels to +-32767.
  • Loading branch information
chrysn committed Sep 29, 2023
1 parent c5651bd commit 57a284a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
42 changes: 23 additions & 19 deletions consts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EdhocMessageBuffer>,
}
Expand Down Expand Up @@ -122,23 +121,27 @@ 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<Self, EADNewError> {
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()?,
})
}

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]> {
Expand All @@ -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;
Expand Down Expand Up @@ -327,23 +330,24 @@ 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,
},
}
}
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()
}
}

Expand Down
4 changes: 2 additions & 2 deletions ead/edhoc-ead-zeroconf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn ead_initiator_set_global_state(new_state: EADInitiatorState) {

pub fn i_prepare_ead_1() -> Option<EADItem> {
// 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();

Expand Down Expand Up @@ -114,7 +114,7 @@ pub fn r_process_ead_1(ead_1: EADItem) -> Result<(), ()> {

pub fn r_prepare_ead_2() -> Option<EADItem> {
// 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;
Expand Down

0 comments on commit 57a284a

Please sign in to comment.