Skip to content

Commit

Permalink
WIP: cargo fmt
Browse files Browse the repository at this point in the history
kept as a separate commit for now to minimize visible diff from functions changed to associated functions
  • Loading branch information
chrysn committed Sep 29, 2023
1 parent 169cf5e commit 6862384
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 106 deletions.
200 changes: 99 additions & 101 deletions crypto/edhoc-crypto-hacspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BufferPlaintext3, EDHOCError> {
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<BufferPlaintext3, EDHOCError> {
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::<u8>())
}
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::<u8>());
}
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::<u8>())
}

// 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::<u8>());
}
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)]
Expand Down
1 change: 0 additions & 1 deletion crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: CryptoTrait>() {}
Expand Down
2 changes: 1 addition & 1 deletion hacspec/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
9 changes: 6 additions & 3 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 6862384

Please sign in to comment.