Skip to content

Commit

Permalink
BytesKeyEC2: Make into newtype
Browse files Browse the repository at this point in the history
Contributes-To: #93
  • Loading branch information
chrysn committed Nov 27, 2024
1 parent e1561d6 commit b985932
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion shared/src/cred.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@ pub type BufferCred = EdhocBuffer<192>; // arbitrary size
pub type BufferKid = EdhocBuffer<16>; // variable size, up to 16 bytes
pub type BufferIdCred = EdhocBuffer<192>; // variable size, can contain either the contents of a BufferCred or a BufferKid
pub type BytesKeyAES128 = [u8; 16];
pub type BytesKeyEC2 = [u8; 32];

#[derive(Debug, Copy, Clone, PartialEq)]
pub struct BytesKeyEC2([u8; 32]);

impl TryFrom<[u8; 32]> for BytesKeyEC2 {
type Error = EDHOCError;

fn try_from(value: [u8; 32]) -> Result<Self, Self::Error> {
// Not performing any validation yet
Ok(Self(value))
}
}

// This is convenient in particular while transitioning away from `pub type BytesKeyEC2 = [u8;
// 32]`, because try_into was a common way to get it.
impl TryFrom<&[u8]> for BytesKeyEC2 {
type Error = EDHOCError;

fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
let slice: [u8; 32] = value.try_into().map_err(|_| EDHOCError::ParsingError)?;
slice.try_into()
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
Expand Down

0 comments on commit b985932

Please sign in to comment.