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 Oct 3, 2023
1 parent c8b13f2 commit f484f68
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 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 = [ "cb-psa" ] }
coap = { version = "0.13" }
coap-lite = { version = "0.11.3" }
3 changes: 2 additions & 1 deletion examples/coap/src/bin/coapclient.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use coap::CoAPClient;
use coap_lite::ResponseType;
use edhoc_rs::*;
use hexlit::hex;
use std::time::Duration;

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) -> EdhocInitiator {
EdhocInitiator::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 {
EdhocInitiator::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
10 changes: 5 additions & 5 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use hex::FromHex;
#[derive(Default, Copy, Clone, Debug)]
pub struct EdhocInitiatorState<'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 @@ -200,14 +200,14 @@ impl<'a> EdhocInitiatorState<'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,
) -> EdhocInitiatorState<'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 @@ -253,7 +253,7 @@ impl<'a> EdhocInitiatorState<'a> {
&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 @@ -349,7 +349,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 f484f68

Please sign in to comment.