Skip to content

Commit

Permalink
fix(api)!: Use &[u8] instead of hex &str in constructors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chrysn committed Sep 29, 2023
1 parent e5cca6e commit f799b40
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/coap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
3 changes: 2 additions & 1 deletion examples/coap/src/bin/coapclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 2 additions & 2 deletions lib/src/c_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)),
Expand Down
21 changes: 12 additions & 9 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -273,7 +273,10 @@ mod hacspec {
message_2: &EdhocMessageBuffer,
) -> Result<u8, EDHOCError> {
// 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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -633,7 +636,7 @@ mod rust {
&cred_r,
self.cred_r.len() / 2,
&<BytesP256ElemLen>::from_hex(self.g_r).expect("Decoding failed"),
&<BytesP256ElemLen>::from_hex(self.i).expect("Decoding failed"),
&self.i,
) {
Ok((state, c_r, _kid)) => {
self.state = state;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f799b40

Please sign in to comment.