From b985932d7bfdf99a3952274fee6aef640da226ec Mon Sep 17 00:00:00 2001 From: chrysn Date: Wed, 27 Nov 2024 17:11:24 +0100 Subject: [PATCH] BytesKeyEC2: Make into newtype Contributes-To: https://github.com/openwsn-berkeley/lakers/issues/93 --- shared/src/cred.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/shared/src/cred.rs b/shared/src/cred.rs index ac0ed777..17ce4c75 100644 --- a/shared/src/cred.rs +++ b/shared/src/cred.rs @@ -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 { + // 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 { + let slice: [u8; 32] = value.try_into().map_err(|_| EDHOCError::ParsingError)?; + slice.try_into() + } +} #[derive(Clone, Copy, Debug, PartialEq)] #[repr(C)]