From f799b4095db73489835ed8f3edb76942c9d31bd0 Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 29 Sep 2023 17:20:37 +0200 Subject: [PATCH] fix(api)!: Use &[u8] instead of hex &str in constructors This only changes the `i` part of the EdhocInitiator; follow-ups will extend this once the style is final. BREAKING CHANGE: This alters EdhocInitiator's argument style. --- examples/coap/Cargo.toml | 1 + examples/coap/src/bin/coapclient.rs | 3 ++- lib/src/c_wrapper.rs | 4 ++-- lib/src/lib.rs | 21 ++++++++++++--------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/examples/coap/Cargo.toml b/examples/coap/Cargo.toml index bee4d96d..30b5875f 100644 --- a/examples/coap/Cargo.toml +++ b/examples/coap/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +hexlit = "0.5.3" edhoc-rs = { path = "../../lib", features = [ "hacspec-hacspec" ] } coap = { version = "0.12" } coap-lite = { version = "0.9.1" } diff --git a/examples/coap/src/bin/coapclient.rs b/examples/coap/src/bin/coapclient.rs index 275074d8..d734144c 100644 --- a/examples/coap/src/bin/coapclient.rs +++ b/examples/coap/src/bin/coapclient.rs @@ -2,11 +2,12 @@ use coap::CoAPClient; use coap_lite::ResponseType; use edhoc_rs::*; use std::time::Duration; +use hexlit::hex; const ID_CRED_I: &str = "a104412b"; const ID_CRED_R: &str = "a104410a"; const CRED_I: &str = "A2027734322D35302D33312D46462D45462D33372D33322D333908A101A5010202412B2001215820AC75E9ECE3E50BFC8ED60399889522405C47BF16DF96660A41298CB4307F7EB62258206E5DE611388A4B8A8211334AC7D37ECB52A387D257E6DB3C2A93DF21FF3AFFC8"; -const I: &str = "fb13adeb6518cee5f88417660841142e830a81fe334380a953406a1305e8706b"; +const I: &[u8] = &hex!("fb13adeb6518cee5f88417660841142e830a81fe334380a953406a1305e8706b"); const _G_I_X_COORD: &str = "ac75e9ece3e50bfc8ed60399889522405c47bf16df96660a41298cb4307f7eb6"; // not used const _G_I_Y_COORD: &str = "6e5de611388a4b8a8211334ac7d37ecb52a387d257e6db3c2a93df21ff3affc8"; // not used const CRED_R: &str = "A2026008A101A5010202410A2001215820BBC34960526EA4D32E940CAD2A234148DDC21791A12AFBCBAC93622046DD44F02258204519E257236B2A0CE2023F0931F1F386CA7AFDA64FCDE0108C224C51EABF6072"; diff --git a/lib/src/c_wrapper.rs b/lib/src/c_wrapper.rs index 45ca9117..ab1b3033 100644 --- a/lib/src/c_wrapper.rs +++ b/lib/src/c_wrapper.rs @@ -40,7 +40,7 @@ impl EdhocInitiatorC { pub fn to_rust(&self) -> RustEdhocInitiator { RustEdhocInitiator::new( self.state, - unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.i, self.i_len)) }, + unsafe { slice::from_raw_parts(self.i, self.i_len) }, unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.g_r, self.g_r_len)) }, unsafe { str::from_utf8_unchecked(slice::from_raw_parts(self.id_cred_i, self.id_cred_i_len)) @@ -141,7 +141,7 @@ pub unsafe extern "C" fn initiator_new( ) -> EdhocInitiatorC { RustEdhocInitiator::new( State::default(), - str::from_utf8_unchecked(slice::from_raw_parts(i, i_len)), + slice::from_raw_parts(i, i_len), str::from_utf8_unchecked(slice::from_raw_parts(g_r, g_r_len)), str::from_utf8_unchecked(slice::from_raw_parts(id_cred_i, id_cred_i_len)), str::from_utf8_unchecked(slice::from_raw_parts(cred_i, cred_i_len)), diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 95f1b5f8..97cbd815 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -69,7 +69,7 @@ mod hacspec { #[derive(Default, Copy, Clone, Debug)] pub struct HacspecEdhocInitiator<'a> { state: State, // opaque state - i: &'a str, // private authentication key of I + i: &'a [u8], // private authentication key of I g_r: &'a str, // public authentication key of R id_cred_i: &'a str, // identifier of I's credential cred_i: &'a str, // I's full credential @@ -229,14 +229,14 @@ mod hacspec { impl<'a> HacspecEdhocInitiator<'a> { pub fn new( state: State, - i: &'a str, + i: &'a [u8], g_r: &'a str, id_cred_i: &'a str, cred_i: &'a str, id_cred_r: &'a str, cred_r: &'a str, ) -> HacspecEdhocInitiator<'a> { - assert!(i.len() == P256_ELEM_LEN * 2); + assert!(i.len() == P256_ELEM_LEN); assert!(g_r.len() == P256_ELEM_LEN * 2); assert!(id_cred_i.len() == ID_CRED_LEN * 2); assert!(id_cred_r.len() == ID_CRED_LEN * 2); @@ -273,7 +273,10 @@ mod hacspec { message_2: &EdhocMessageBuffer, ) -> Result { // init hacspec struct for I, I's private static DH key - let i = BytesP256ElemLen::from_hex(self.i); + let mut i = BytesP256ElemLen::new(); + for (src, dst) in self.i.iter().zip(i.0.iter_mut()) { + *dst = U8::classify(*src); + } // init hacspec structs for id_cred_r and cred_r let id_cred_r = BytesIdCred::from_hex(self.id_cred_r); @@ -400,7 +403,7 @@ mod rust { #[derive(Default, Copy, Clone, Debug)] pub struct RustEdhocInitiator<'a> { state: State, // opaque state - i: &'a str, // private authentication key of I + i: &[u8], // private authentication key of I g_r: &'a str, // public authentication key of R id_cred_i: &'a str, // identifier of I's credential cred_i: &'a str, // I's full credential @@ -580,14 +583,14 @@ mod rust { pub fn new( state: State, - i: &'a str, + i: &[u8], g_r: &'a str, id_cred_i: &'a str, cred_i: &'a str, id_cred_r: &'a str, cred_r: &'a str, ) -> RustEdhocInitiator<'a> { - assert!(i.len() == P256_ELEM_LEN * 2); + assert!(i.len() == P256_ELEM_LEN); assert!(g_r.len() == P256_ELEM_LEN * 2); assert!(id_cred_i.len() == ID_CRED_LEN * 2); assert!(id_cred_r.len() == ID_CRED_LEN * 2); @@ -633,7 +636,7 @@ mod rust { &cred_r, self.cred_r.len() / 2, &::from_hex(self.g_r).expect("Decoding failed"), - &::from_hex(self.i).expect("Decoding failed"), + &self.i, ) { Ok((state, c_r, _kid)) => { self.state = state; @@ -730,7 +733,7 @@ mod test { const ID_CRED_I: &str = "a104412b"; const ID_CRED_R: &str = "a104410a"; const CRED_I: &str = "A2027734322D35302D33312D46462D45462D33372D33322D333908A101A5010202412B2001215820AC75E9ECE3E50BFC8ED60399889522405C47BF16DF96660A41298CB4307F7EB62258206E5DE611388A4B8A8211334AC7D37ECB52A387D257E6DB3C2A93DF21FF3AFFC8"; - const I: &str = "fb13adeb6518cee5f88417660841142e830a81fe334380a953406a1305e8706b"; + const I: &[u8] = &hex!("fb13adeb6518cee5f88417660841142e830a81fe334380a953406a1305e8706b"); const R: &str = "72cc4761dbd4c78f758931aa589d348d1ef874a7e303ede2f140dcf3e6aa4aac"; const G_I: &str = "ac75e9ece3e50bfc8ed60399889522405c47bf16df96660a41298cb4307f7eb6"; // used const _G_I_Y_COORD: &str = "6e5de611388a4b8a8211334ac7d37ecb52a387d257e6db3c2a93df21ff3affc8"; // not used