Skip to content

Commit

Permalink
#87: Implement EDHOC_KeyUpdate for hacspec and add to the handshake test
Browse files Browse the repository at this point in the history
  • Loading branch information
malishav committed Jul 19, 2023
1 parent e530f5a commit 42dc7c8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
61 changes: 61 additions & 0 deletions hacspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,67 @@ pub fn edhoc_exporter(
}
}

pub fn edhoc_key_update(
mut state: State,
context: &BytesMaxContextBuffer,
context_len: usize,
) -> Result<(State, BytesHashLen), EDHOCError> {
let State(
current_state,
_x_or_y,
_c_i,
_gy_or_gx,
_prk_3e2m,
_prk_4e3m,
mut prk_out,
mut prk_exporter,
_h_message_1,
_th_3,
) = state;

let mut prk_new_buf = BytesMaxBuffer::new();
let mut error = EDHOCError::UnknownError;

if current_state == EDHOCState::Completed {
// new PRK_out
prk_new_buf = edhoc_kdf(
&prk_out,
U8(11 as u8),
context,
context_len,
SHA256_DIGEST_LEN,
);
prk_out = prk_out.update_slice(0, &prk_new_buf, 0, SHA256_DIGEST_LEN);

// new PRK_exporter
prk_new_buf = edhoc_kdf(
&prk_out,
U8(10 as u8),
&BytesMaxContextBuffer::new(),
0,
SHA256_DIGEST_LEN,
);
prk_exporter = prk_exporter.update_slice(0, &prk_new_buf, 0, SHA256_DIGEST_LEN);

state = construct_state(
current_state,
_x_or_y,
_c_i,
_gy_or_gx,
_prk_3e2m,
_prk_4e3m,
prk_out,
prk_exporter,
_h_message_1,
_th_3,
);

Ok((state, prk_out))
} else {
Err(EDHOCError::WrongState)
}
}

/// process message_1: parse, check method, check cipher suite
pub fn r_process_message_1(
mut state: State,
Expand Down
48 changes: 48 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,23 @@ mod hacspec {
Err(error) => Err(error),
}
}

pub fn edhoc_key_update(
self: &mut HacspecEdhocResponder<'a>,
context: &[u8],
) -> Result<[u8; SHA256_DIGEST_LEN], EDHOCError> {
// init hacspec struct for context
let mut context_hacspec = BytesMaxContextBuffer::new();
context_hacspec = context_hacspec.update(0, &ByteSeq::from_public_slice(context));

match edhoc_key_update(self.state, &context_hacspec, context.len()) {
Ok((state, prk_out_new)) => {
self.state = state;
Ok(prk_out_new.to_public_array())
}
Err(error) => Err(error),
}
}
}

impl<'a> HacspecEdhocInitiator<'a> {
Expand Down Expand Up @@ -325,6 +342,23 @@ mod hacspec {
Err(error) => Err(error),
}
}

pub fn edhoc_key_update(
self: &mut HacspecEdhocInitiator<'a>,
context: &[u8],
) -> Result<[u8; SHA256_DIGEST_LEN], EDHOCError> {
// init hacspec struct for context
let mut context_hacspec = BytesMaxContextBuffer::new();
context_hacspec = context_hacspec.update(0, &ByteSeq::from_public_slice(context));

match edhoc_key_update(self.state, &context_hacspec, context.len()) {
Ok((state, prk_out_new)) => {
self.state = state;
Ok(prk_out_new.to_public_array())
}
Err(error) => Err(error),
}
}
}
}

Expand Down Expand Up @@ -730,6 +764,20 @@ mod test {

assert_eq!(i_oscore_secret.unwrap(), r_oscore_secret.unwrap());
assert_eq!(i_oscore_salt.unwrap(), r_oscore_salt.unwrap());

// test key update with context from draft-ietf-lake-traces
let i_prk_out_new = initiator.edhoc_key_update(&[
0xa0, 0x11, 0x58, 0xfd, 0xb8, 0x20, 0x89, 0x0c, 0xd6, 0xbe, 0x16, 0x96, 0x02, 0xb8,
0xbc, 0xea,
]);
assert!(i_prk_out_new.is_ok());
let r_prk_out_new = responder.edhoc_key_update(&[
0xa0, 0x11, 0x58, 0xfd, 0xb8, 0x20, 0x89, 0x0c, 0xd6, 0xbe, 0x16, 0x96, 0x02, 0xb8,
0xbc, 0xea,
]);
assert!(r_prk_out_new.is_ok());

assert_eq!(i_prk_out_new.unwrap(), r_prk_out_new.unwrap());
}

#[cfg(feature = "ead-zeroconf")]
Expand Down

0 comments on commit 42dc7c8

Please sign in to comment.