Skip to content

Commit

Permalink
feat!: Allow initiators to pick their C_I
Browse files Browse the repository at this point in the history
BREAKING CHANGE: APIs for creating messager 1 change

As before, this does *not* affect the C API (this time, for consistency)
  • Loading branch information
chrysn committed Sep 30, 2023
1 parent cc9f566 commit be0ab60
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
3 changes: 2 additions & 1 deletion examples/coap/src/bin/coapclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ fn main() {

// Send Message 1 over CoAP and convert the response to byte
let mut msg_1_buf = Vec::from([0xf5u8]); // EDHOC message_1 when transported over CoAP is prepended with CBOR true
let message_1 = initiator.prepare_message_1().unwrap();
let c_i: u8 = generate_connection_identifier_cbor().into();
let message_1 = initiator.prepare_message_1(c_i).unwrap();
msg_1_buf.extend_from_slice(&message_1.content[..message_1.len]);
println!("message_1 len = {}", msg_1_buf.len());

Expand Down
6 changes: 4 additions & 2 deletions examples/edhoc-rs-no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ fn main() -> ! {
let mut initiator =
EdhocInitiator::new(state, I, G_R, ID_CRED_I, CRED_I, ID_CRED_R, CRED_R);

let message_1 = initiator.prepare_message_1();
let c_i: u8 = generate_connection_identifier_cbor().into();
let message_1 = initiator.prepare_message_1(c_i);
assert!(message_1.is_ok());
}

Expand Down Expand Up @@ -128,7 +129,8 @@ fn main() -> ! {
CRED_R,
);

let ret = initiator.prepare_message_1(); // to update the state
let c_i: u8 = generate_connection_identifier_cbor().into();
let ret = initiator.prepare_message_1(c_i); // to update the state
assert!(ret.is_ok());
let message_1 = ret.unwrap();

Expand Down
3 changes: 2 additions & 1 deletion lib/src/c_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ pub unsafe extern "C" fn initiator_prepare_message_1(
) -> i8 {
let mut initiator = (*initiator_c).to_rust();

let result = match initiator.prepare_message_1() {
let c_i: u8 = generate_connection_identifier_cbor().into();
let result = match initiator.prepare_message_1(c_i) {
Ok(msg_1) => {
*message_1 = msg_1;
0
Expand Down
15 changes: 9 additions & 6 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ mod hacspec {

pub fn prepare_message_1(
self: &mut HacspecEdhocInitiator<'a>,
c_i: u8,
) -> Result<EdhocMessageBuffer, EDHOCError> {
// Generate ephemeral key pair
let (x, g_x) = edhoc_crypto::p256_generate_key_pair();
let c_i = generate_connection_identifier_cbor();

match edhoc_hacspec::i_prepare_message_1(self.state, x, g_x, c_i) {
match edhoc_hacspec::i_prepare_message_1(self.state, x, g_x, U8(c_i)) {
Ok((state, message_1)) => {
self.state = state;
Ok(message_1.to_public_buffer())
Expand Down Expand Up @@ -614,9 +614,9 @@ mod rust {

pub fn prepare_message_1(
self: &mut RustEdhocInitiator<'a>,
c_i: u8,
) -> Result<BufferMessage1, EDHOCError> {
let (x, g_x) = edhoc_crypto::p256_generate_key_pair();
let c_i = generate_connection_identifier_cbor();

match i_prepare_message_1(self.state, x, g_x, c_i) {
Ok((state, message_1)) => {
Expand Down Expand Up @@ -770,7 +770,8 @@ mod test {
let mut initiator =
EdhocInitiator::new(state, I, G_R, ID_CRED_I, CRED_I, ID_CRED_R, CRED_R);

let message_1 = initiator.prepare_message_1();
let c_i: u8 = generate_connection_identifier_cbor().into();
let message_1 = initiator.prepare_message_1(c_i);
assert!(message_1.is_ok());
}

Expand Down Expand Up @@ -821,7 +822,8 @@ mod test {
CRED_R,
);

let result = initiator.prepare_message_1(); // to update the state
let c_i: u8 = generate_connection_identifier_cbor().into();
let result = initiator.prepare_message_1(c_i); // to update the state
assert!(result.is_ok());

let error = responder.process_message_1(&result.unwrap());
Expand Down Expand Up @@ -915,7 +917,8 @@ mod test {
EADResponderProtocolState::Start
);

let message_1 = initiator.prepare_message_1().unwrap();
let c_i: u8 = generate_connection_identifier_cbor().into();
let message_1 = initiator.prepare_message_1(c_i).unwrap();
assert_eq!(
ead_initiator_state.protocol_state,
EADInitiatorProtocolState::WaitEAD2
Expand Down

0 comments on commit be0ab60

Please sign in to comment.