From 6862384f1f4625083e9f8a25c6f876d291e1a6d6 Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 29 Sep 2023 22:06:24 +0200 Subject: [PATCH] WIP: cargo fmt kept as a separate commit for now to minimize visible diff from functions changed to associated functions --- crypto/edhoc-crypto-hacspec/src/lib.rs | 200 ++++++++++++------------- crypto/src/lib.rs | 1 - hacspec/src/lib.rs | 2 +- lib/src/lib.rs | 9 +- 4 files changed, 106 insertions(+), 106 deletions(-) diff --git a/crypto/edhoc-crypto-hacspec/src/lib.rs b/crypto/edhoc-crypto-hacspec/src/lib.rs index 944bdfe0..acb64467 100644 --- a/crypto/edhoc-crypto-hacspec/src/lib.rs +++ b/crypto/edhoc-crypto-hacspec/src/lib.rs @@ -14,118 +14,116 @@ use edhoc_crypto_trait::Crypto as CryptoTrait; pub struct Crypto; impl CryptoTrait for Crypto { + fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen { + let output = BytesHashLen::from_seq(&hash(&ByteSeq::from_slice(message, 0, message_len))); + output + } -fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen { - let output = BytesHashLen::from_seq(&hash(&ByteSeq::from_slice(message, 0, message_len))); - output -} - -fn hkdf_expand( - prk: &BytesHashLen, - info: &BytesMaxInfoBuffer, - info_len: usize, - length: usize, -) -> BytesMaxBuffer { - let mut output = BytesMaxBuffer::new(); - output = output.update( - 0, - &expand( - &ByteSeq::from_slice(prk, 0, prk.len()), - &ByteSeq::from_slice(info, 0, info_len), - length, - ) - .unwrap(), - ); - output -} - -fn hkdf_extract(salt: &BytesHashLen, ikm: &BytesP256ElemLen) -> BytesHashLen { - let output = BytesHashLen::from_seq(&extract( - &ByteSeq::from_slice(salt, 0, salt.len()), - &ByteSeq::from_slice(ikm, 0, ikm.len()), - )); - output -} + fn hkdf_expand( + prk: &BytesHashLen, + info: &BytesMaxInfoBuffer, + info_len: usize, + length: usize, + ) -> BytesMaxBuffer { + let mut output = BytesMaxBuffer::new(); + output = output.update( + 0, + &expand( + &ByteSeq::from_slice(prk, 0, prk.len()), + &ByteSeq::from_slice(info, 0, info_len), + length, + ) + .unwrap(), + ); + output + } -fn aes_ccm_encrypt_tag_8( - key: &BytesCcmKeyLen, - iv: &BytesCcmIvLen, - ad: &BytesEncStructureLen, - plaintext: &BufferPlaintext3, -) -> BufferCiphertext3 { - let output = BufferCiphertext3::from_seq(&encrypt_ccm( - ByteSeq::from_slice(ad, 0, ad.len()), - ByteSeq::from_slice(iv, 0, iv.len()), - ByteSeq::from_slice(&plaintext.content, 0, plaintext.len), - Key128::from_slice(key, 0, key.len()), - AES_CCM_TAG_LEN, - )); - - output -} + fn hkdf_extract(salt: &BytesHashLen, ikm: &BytesP256ElemLen) -> BytesHashLen { + let output = BytesHashLen::from_seq(&extract( + &ByteSeq::from_slice(salt, 0, salt.len()), + &ByteSeq::from_slice(ikm, 0, ikm.len()), + )); + output + } -fn aes_ccm_decrypt_tag_8( - key: &BytesCcmKeyLen, - iv: &BytesCcmIvLen, - ad: &BytesEncStructureLen, - ciphertext: &BufferCiphertext3, -) -> Result { - match decrypt_ccm( - ByteSeq::from_slice(ad, 0, ad.len()), - ByteSeq::from_slice(iv, 0, iv.len()), - Key128::from_slice(key, 0, key.len()), - ByteSeq::from_slice(&ciphertext.content, 0, ciphertext.len), - ciphertext.len, - AES_CCM_TAG_LEN, - ) { - Ok(p) => Ok(BufferPlaintext3::from_seq(&p)), - Err(_) => Err(EDHOCError::MacVerificationFailed), + fn aes_ccm_encrypt_tag_8( + key: &BytesCcmKeyLen, + iv: &BytesCcmIvLen, + ad: &BytesEncStructureLen, + plaintext: &BufferPlaintext3, + ) -> BufferCiphertext3 { + let output = BufferCiphertext3::from_seq(&encrypt_ccm( + ByteSeq::from_slice(ad, 0, ad.len()), + ByteSeq::from_slice(iv, 0, iv.len()), + ByteSeq::from_slice(&plaintext.content, 0, plaintext.len), + Key128::from_slice(key, 0, key.len()), + AES_CCM_TAG_LEN, + )); + + output } -} -fn p256_ecdh( - private_key: &BytesP256ElemLen, - public_key: &BytesP256ElemLen, -) -> BytesP256ElemLen { - let scalar = P256Scalar::from_byte_seq_be(private_key); - let point = ( - P256FieldElement::from_byte_seq_be(public_key), - p256_calculate_w(P256FieldElement::from_byte_seq_be(public_key)), - ); - - // we only care about the x coordinate - let (x, _y) = p256_point_mul(scalar, point).unwrap(); - - let secret = BytesP256ElemLen::from_seq(&x.to_byte_seq_be()); - secret -} + fn aes_ccm_decrypt_tag_8( + key: &BytesCcmKeyLen, + iv: &BytesCcmIvLen, + ad: &BytesEncStructureLen, + ciphertext: &BufferCiphertext3, + ) -> Result { + match decrypt_ccm( + ByteSeq::from_slice(ad, 0, ad.len()), + ByteSeq::from_slice(iv, 0, iv.len()), + Key128::from_slice(key, 0, key.len()), + ByteSeq::from_slice(&ciphertext.content, 0, ciphertext.len), + ciphertext.len, + AES_CCM_TAG_LEN, + ) { + Ok(p) => Ok(BufferPlaintext3::from_seq(&p)), + Err(_) => Err(EDHOCError::MacVerificationFailed), + } + } -#[cfg(not(feature = "hacspec-pure"))] -fn get_random_byte() -> U8 { - U8(rand::thread_rng().gen::()) -} + fn p256_ecdh( + private_key: &BytesP256ElemLen, + public_key: &BytesP256ElemLen, + ) -> BytesP256ElemLen { + let scalar = P256Scalar::from_byte_seq_be(private_key); + let point = ( + P256FieldElement::from_byte_seq_be(public_key), + p256_calculate_w(P256FieldElement::from_byte_seq_be(public_key)), + ); + + // we only care about the x coordinate + let (x, _y) = p256_point_mul(scalar, point).unwrap(); + + let secret = BytesP256ElemLen::from_seq(&x.to_byte_seq_be()); + secret + } -#[cfg(not(feature = "hacspec-pure"))] -fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) { - // generate a private key - let mut private_key = BytesP256ElemLen::new(); - loop { - for i in 0..private_key.len() { - private_key[i] = U8(rand::thread_rng().gen::()); - } - if p256_validate_private_key(&ByteSeq::from_slice(&private_key, 0, private_key.len())) { - break; - } + #[cfg(not(feature = "hacspec-pure"))] + fn get_random_byte() -> U8 { + U8(rand::thread_rng().gen::()) } - // obtain the corresponding public key - let scalar = P256Scalar::from_byte_seq_be(&private_key); - let public_key_point = p256_point_mul_base(scalar).unwrap(); - let public_key = BytesP256ElemLen::from_seq(&public_key_point.0.to_byte_seq_be()); + #[cfg(not(feature = "hacspec-pure"))] + fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) { + // generate a private key + let mut private_key = BytesP256ElemLen::new(); + loop { + for i in 0..private_key.len() { + private_key[i] = U8(rand::thread_rng().gen::()); + } + if p256_validate_private_key(&ByteSeq::from_slice(&private_key, 0, private_key.len())) { + break; + } + } - (private_key, public_key) -} + // obtain the corresponding public key + let scalar = P256Scalar::from_byte_seq_be(&private_key); + let public_key_point = p256_point_mul_base(scalar).unwrap(); + let public_key = BytesP256ElemLen::from_seq(&public_key_point.0.to_byte_seq_be()); + (private_key, public_key) + } } #[cfg(test)] diff --git a/crypto/src/lib.rs b/crypto/src/lib.rs index 53a40c5e..948e8d91 100644 --- a/crypto/src/lib.rs +++ b/crypto/src/lib.rs @@ -21,7 +21,6 @@ pub use edhoc_crypto_psa::*; #[cfg(any(feature = "cryptocell310", feature = "cryptocell310-rust"))] pub use edhoc_crypto_cryptocell310::*; - /// See test_implements_crypto #[allow(dead_code)] fn test_helper() {} diff --git a/hacspec/src/lib.rs b/hacspec/src/lib.rs index 3c659c48..d4fb0ef6 100644 --- a/hacspec/src/lib.rs +++ b/hacspec/src/lib.rs @@ -1,9 +1,9 @@ #![no_std] use edhoc_consts::*; +use edhoc_crypto::{Crypto, CryptoTrait}; use edhoc_ead::*; use hacspec_lib::*; -use edhoc_crypto::{Crypto, CryptoTrait}; pub fn edhoc_exporter( state: State, diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 5b507d24..9fffd957 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -7,8 +7,11 @@ feature = "hacspec-cryptocell310" ))] pub use { - edhoc_consts::State as EdhocState, edhoc_consts::*, edhoc_crypto::{Crypto, CryptoTrait}, - hacspec::generate_connection_identifier, hacspec::HacspecEdhocInitiator as EdhocInitiator, + edhoc_consts::State as EdhocState, + edhoc_consts::*, + edhoc_crypto::{Crypto, CryptoTrait}, + hacspec::generate_connection_identifier, + hacspec::HacspecEdhocInitiator as EdhocInitiator, hacspec::HacspecEdhocResponder as EdhocResponder, }; @@ -62,9 +65,9 @@ use c_wrapper::*; ))] mod hacspec { use edhoc_consts::*; + use edhoc_crypto::{Crypto, CryptoTrait}; use edhoc_hacspec::*; use hacspec_lib::*; - use edhoc_crypto::{Crypto, CryptoTrait}; #[repr(C)] #[derive(Default, Copy, Clone, Debug)]