Skip to content

Commit

Permalink
fixup! refactor!: Pass crypto implementation around as an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Oct 11, 2023
1 parent 82d60e2 commit c93f352
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 8 additions & 4 deletions crypto/edhoc-crypto-cryptocell310-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn convert_array(input: &[u32]) -> [u8; SHA256_DIGEST_LEN] {
pub struct Crypto;

impl CryptoTrait for Crypto {
fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen {
fn sha256_digest(&mut self, message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen {
let mut buffer: [u32; 64 / 4] = [0x00; 64 / 4];

unsafe {
Expand All @@ -39,6 +39,7 @@ impl CryptoTrait for Crypto {
}

fn hkdf_expand(
&mut self,
prk: &BytesHashLen,
info: &BytesMaxInfoBuffer,
info_len: usize,
Expand All @@ -63,7 +64,7 @@ impl CryptoTrait for Crypto {
buffer
}

fn hkdf_extract(salt: &BytesHashLen, ikm: &BytesP256ElemLen) -> BytesHashLen {
fn hkdf_extract(&mut self, salt: &BytesHashLen, ikm: &BytesP256ElemLen) -> BytesHashLen {
// Implementation of HKDF-Extract as per RFC 5869

// TODO generalize if salt is not provided
Expand All @@ -73,6 +74,7 @@ impl CryptoTrait for Crypto {
}

fn aes_ccm_encrypt_tag_8(
&mut self,
key: &BytesCcmKeyLen,
iv: &BytesCcmIvLen,
ad: &BytesEncStructureLen,
Expand Down Expand Up @@ -110,6 +112,7 @@ impl CryptoTrait for Crypto {
}

fn aes_ccm_decrypt_tag_8(
&mut self,
key: &BytesCcmKeyLen,
iv: &BytesCcmIvLen,
ad: &BytesEncStructureLen,
Expand Down Expand Up @@ -148,6 +151,7 @@ impl CryptoTrait for Crypto {
}

fn p256_ecdh(
&mut self,
private_key: &BytesP256ElemLen,
public_key: &BytesP256ElemLen,
) -> BytesP256ElemLen {
Expand Down Expand Up @@ -200,7 +204,7 @@ impl CryptoTrait for Crypto {
output
}

fn get_random_byte() -> u8 {
fn get_random_byte(&mut self) -> u8 {
let mut rnd_context = CRYS_RND_State_t::default();
let mut rnd_work_buffer = CRYS_RND_WorkBuff_t::default();
unsafe {
Expand All @@ -221,7 +225,7 @@ impl CryptoTrait for Crypto {
buffer[0]
}

fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
fn p256_generate_key_pair(&mut self) -> (BytesP256ElemLen, BytesP256ElemLen) {
let mut rnd_context = CRYS_RND_State_t::default();
let mut rnd_work_buffer = CRYS_RND_WorkBuff_t::default();
unsafe {
Expand Down
10 changes: 5 additions & 5 deletions examples/edhoc-rs-no_std/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use panic_semihosting as _;
#[cfg(feature = "rtt")]
use rtt_target::{rprintln as println, rtt_init_print};

use edhoc_crypto::{Crypto, CryptoTrait};
use edhoc_crypto::{default_crypto, CryptoTrait};
use edhoc_rs::*;

extern crate alloc;
Expand Down Expand Up @@ -81,11 +81,11 @@ fn main() -> ! {
println!("Test test_new_initiator passed.");

fn test_p256_keys() {
let (x, g_x) = Crypto::p256_generate_key_pair();
let (y, g_y) = Crypto::p256_generate_key_pair();
let (x, g_x) = default_crypto().p256_generate_key_pair();
let (y, g_y) = default_crypto().p256_generate_key_pair();

let g_xy = Crypto::p256_ecdh(&x, &g_y);
let g_yx = Crypto::p256_ecdh(&y, &g_x);
let g_xy = default_crypto().p256_ecdh(&x, &g_y);
let g_yx = default_crypto().p256_ecdh(&y, &g_x);

assert_eq!(g_xy, g_yx);
}
Expand Down

0 comments on commit c93f352

Please sign in to comment.