Skip to content

Commit

Permalink
edhoc: fixing mutable references. gstar.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElsaLopez133 committed Nov 2, 2024
1 parent 7615093 commit bb2d03b
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions lib/src/edhoc.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
use lakers_shared::{Crypto as CryptoTrait, *};

// Implementation of edhoc_exporter for 3 or 4 messages
pub trait Done {
fn get_prk_exporter_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN];
fn get_prk_exporter(&self) -> &[u8; SHA256_DIGEST_LEN];
fn get_prk_out_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN];
fn get_prk_out(&self) -> &[u8; SHA256_DIGEST_LEN];
fn update_keys(
&mut self,
new_prk_out: &[u8; SHA256_DIGEST_LEN],
new_prk_exporter: &[u8; SHA256_DIGEST_LEN],
);
}

impl Done for WaitM4 {
fn get_prk_exporter_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN] {
&mut self.prk_exporter
}
fn get_prk_exporter(&self) -> &[u8; SHA256_DIGEST_LEN] {
&self.prk_exporter
}
fn get_prk_out_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN] {
&mut self.prk_out
}
fn get_prk_out(&self) -> &[u8; SHA256_DIGEST_LEN] {
&self.prk_out
}
fn update_keys(
&mut self,
new_prk_out: &[u8; SHA256_DIGEST_LEN],
new_prk_exporter: &[u8; SHA256_DIGEST_LEN],
) {
self.prk_out.copy_from_slice(new_prk_out);
self.prk_exporter.copy_from_slice(new_prk_exporter);
}
}

impl Done for Completed {
fn get_prk_exporter_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN] {
&mut self.prk_exporter
}
fn get_prk_exporter(&self) -> &[u8; SHA256_DIGEST_LEN] {
&self.prk_exporter
}
fn get_prk_out_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN] {
&mut self.prk_out
}
fn get_prk_out(&self) -> &[u8; SHA256_DIGEST_LEN] {
&self.prk_out
}
fn update_keys(
&mut self,
new_prk_out: &[u8; SHA256_DIGEST_LEN],
new_prk_exporter: &[u8; SHA256_DIGEST_LEN],
) {
self.prk_out.copy_from_slice(new_prk_out);
self.prk_exporter.copy_from_slice(new_prk_exporter);
}
}

impl Done for ProcessedM3 {
fn get_prk_exporter_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN] {
&mut self.prk_exporter
}
fn get_prk_exporter(&self) -> &[u8; SHA256_DIGEST_LEN] {
&self.prk_exporter
}
fn get_prk_out_mut(&mut self) -> &mut [u8; SHA256_DIGEST_LEN] {
&mut self.prk_out
}
fn get_prk_out(&self) -> &[u8; SHA256_DIGEST_LEN] {
&self.prk_out
}
fn update_keys(
&mut self,
new_prk_out: &[u8; SHA256_DIGEST_LEN],
new_prk_exporter: &[u8; SHA256_DIGEST_LEN],
) {
self.prk_out.copy_from_slice(new_prk_out);
self.prk_exporter.copy_from_slice(new_prk_exporter);
}
}

pub fn edhoc_exporter(
Expand All @@ -77,12 +85,10 @@ pub fn edhoc_key_update(
context: &BytesMaxContextBuffer,
context_len: usize,
) -> BytesHashLen {
// new PRK_out
let mut prk_out = *state.get_prk_out();
// let mut prk_exporter = *state.get_prk_exporter();
// Calculate new PRK_out
let prk_new_buf = edhoc_kdf(
crypto,
&prk_out,
state.get_prk_out(),
11u8,
context,
context_len,
Expand All @@ -91,7 +97,7 @@ pub fn edhoc_key_update(
let mut new_prk_out = [0u8; SHA256_DIGEST_LEN];
new_prk_out.copy_from_slice(&prk_new_buf[..SHA256_DIGEST_LEN]);

// new PRK_exporter
// Calculate new PRK_exporter
let prk_exporter_buf = edhoc_kdf(
crypto,
&new_prk_out,
Expand All @@ -103,12 +109,8 @@ pub fn edhoc_key_update(
let mut new_prk_exporter = [0u8; SHA256_DIGEST_LEN];
new_prk_exporter.copy_from_slice(&prk_exporter_buf[..SHA256_DIGEST_LEN]);

// Update state
let prk_out_mut = state.get_prk_out_mut();
prk_out_mut.copy_from_slice(&new_prk_out);

let prk_exporter_mut = state.get_prk_exporter_mut();
prk_exporter_mut.copy_from_slice(&new_prk_exporter);
// Update state with new keys
state.update_keys(&new_prk_out, &new_prk_exporter);

new_prk_out
}
Expand Down

0 comments on commit bb2d03b

Please sign in to comment.