Skip to content

Commit

Permalink
WIP: Fix visibility qualifiers, move worker method out of trait impl
Browse files Browse the repository at this point in the history
  • Loading branch information
chrysn committed Sep 29, 2023
1 parent a21bff0 commit ebd014b
Showing 1 changed file with 105 additions and 105 deletions.
210 changes: 105 additions & 105 deletions crypto/edhoc-crypto-psa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod hacspec {

pub struct Crypto;
impl CryptoTrait for Crypto {
pub fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen {
fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen {
let hash_alg = Hash::Sha256;
let mut hash: [u8; SHA256_DIGEST_LEN] = [0; SHA256_DIGEST_LEN];
let message = message.to_public_array();
Expand All @@ -45,7 +45,7 @@ mod hacspec {
output
}

pub fn hkdf_expand(
fn hkdf_expand(
prk: &BytesHashLen,
info: &BytesMaxInfoBuffer,
info_len: usize,
Expand Down Expand Up @@ -92,7 +92,7 @@ mod hacspec {
BytesMaxBuffer::from_public_slice(&output)
}

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

// TODO generalize if salt is not provided
Expand All @@ -101,7 +101,7 @@ mod hacspec {
output
}

pub fn aes_ccm_encrypt_tag_8(
fn aes_ccm_encrypt_tag_8(
key: &BytesCcmKeyLen,
iv: &BytesCcmIvLen,
ad: &BytesEncStructureLen,
Expand Down Expand Up @@ -143,7 +143,7 @@ mod hacspec {
output
}

pub fn aes_ccm_decrypt_tag_8(
fn aes_ccm_decrypt_tag_8(
key: &BytesCcmKeyLen,
iv: &BytesCcmIvLen,
ad: &BytesEncStructureLen,
Expand Down Expand Up @@ -185,7 +185,7 @@ mod hacspec {
Err(_) => Err(EDHOCError::MacVerificationFailed),
}
}
pub fn p256_ecdh(
fn p256_ecdh(
private_key: &BytesP256ElemLen,
public_key: &BytesP256ElemLen,
) -> BytesP256ElemLen {
Expand Down Expand Up @@ -221,60 +221,14 @@ mod hacspec {
output
}

pub fn hmac_sha256(message: &[u8], key: [u8; SHA256_DIGEST_LEN]) -> BytesHashLen {
// implementation of HMAC as per RFC2104

const IPAD: [u8; 64] = [0x36; 64];
const OPAD: [u8; 64] = [0x5C; 64];

// (1) append zeros to the end of K to create a B byte string
// (e.g., if K is of length 20 bytes and B=64, then K will be
// appended with 44 zero bytes 0x00)
let mut b: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
b[0..SHA256_DIGEST_LEN].copy_from_slice(&key);

// (2) XOR (bitwise exclusive-OR) the B byte string computed in step
// (1) with ipad
let mut s2: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s2[i] = b[i] ^ IPAD[i];
}

// (3) append the stream of data 'text' to the B byte string resulting
// from step (2)
s2[64..64 + message.len()].copy_from_slice(message);

// (4) apply H to the stream generated in step (3)
let ih = sha256_digest(&BytesMaxBuffer::from_public_slice(&s2), 64 + message.len());

// (5) XOR (bitwise exclusive-OR) the B byte string computed in
// step (1) with opad
let mut s5: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s5[i] = b[i] ^ OPAD[i];
}
// (6) append the H result from step (4) to the B byte string
// resulting from step (5)
s5[64..64 + SHA256_DIGEST_LEN].copy_from_slice(&ih.to_public_array());

// (7) apply H to the stream generated in step (6) and output
// the result
let oh = sha256_digest(
&BytesMaxBuffer::from_public_slice(&s5),
3 * SHA256_DIGEST_LEN,
);

oh
}

pub fn get_random_byte() -> U8 {
fn get_random_byte() -> U8 {
psa_crypto::init().unwrap();
let mut buffer = [0u8; 1];
generate_random(&mut buffer); // TODO: check return value
U8(buffer[0])
}

pub fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
let alg = RawKeyAgreement::Ecdh;
let mut usage_flags: UsageFlags = UsageFlags::default();
usage_flags.set_export();
Expand Down Expand Up @@ -305,6 +259,52 @@ mod hacspec {
(private_key, public_key)
}
}

fn hmac_sha256(message: &[u8], key: [u8; SHA256_DIGEST_LEN]) -> BytesHashLen {
// implementation of HMAC as per RFC2104

const IPAD: [u8; 64] = [0x36; 64];
const OPAD: [u8; 64] = [0x5C; 64];

// (1) append zeros to the end of K to create a B byte string
// (e.g., if K is of length 20 bytes and B=64, then K will be
// appended with 44 zero bytes 0x00)
let mut b: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
b[0..SHA256_DIGEST_LEN].copy_from_slice(&key);

// (2) XOR (bitwise exclusive-OR) the B byte string computed in step
// (1) with ipad
let mut s2: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s2[i] = b[i] ^ IPAD[i];
}

// (3) append the stream of data 'text' to the B byte string resulting
// from step (2)
s2[64..64 + message.len()].copy_from_slice(message);

// (4) apply H to the stream generated in step (3)
let ih = sha256_digest(&BytesMaxBuffer::from_public_slice(&s2), 64 + message.len());

// (5) XOR (bitwise exclusive-OR) the B byte string computed in
// step (1) with opad
let mut s5: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s5[i] = b[i] ^ OPAD[i];
}
// (6) append the H result from step (4) to the B byte string
// resulting from step (5)
s5[64..64 + SHA256_DIGEST_LEN].copy_from_slice(&ih.to_public_array());

// (7) apply H to the stream generated in step (6) and output
// the result
let oh = sha256_digest(
&BytesMaxBuffer::from_public_slice(&s5),
3 * SHA256_DIGEST_LEN,
);

oh
}
}

#[cfg(feature = "rust")]
Expand All @@ -313,7 +313,7 @@ mod rust {

pub struct Crypto;
impl CryptoTrait for Crypto {
pub fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen {
fn sha256_digest(message: &BytesMaxBuffer, message_len: usize) -> BytesHashLen {
let hash_alg = Hash::Sha256;
let mut hash: [u8; SHA256_DIGEST_LEN] = [0; SHA256_DIGEST_LEN];
psa_crypto::init().unwrap();
Expand All @@ -322,7 +322,7 @@ mod rust {
hash
}

pub fn hkdf_expand(
fn hkdf_expand(
prk: &BytesHashLen,
info: &BytesMaxInfoBuffer,
info_len: usize,
Expand Down Expand Up @@ -362,7 +362,7 @@ mod rust {
output
}

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

// TODO generalize if salt is not provided
Expand All @@ -371,7 +371,7 @@ mod rust {
output
}

pub fn aes_ccm_encrypt_tag_8(
fn aes_ccm_encrypt_tag_8(
key: &BytesCcmKeyLen,
iv: &BytesCcmIvLen,
ad: &BytesEncStructureLen,
Expand Down Expand Up @@ -412,7 +412,7 @@ mod rust {
output_buffer
}

pub fn aes_ccm_decrypt_tag_8(
fn aes_ccm_decrypt_tag_8(
key: &BytesCcmKeyLen,
iv: &BytesCcmIvLen,
ad: &BytesEncStructureLen,
Expand Down Expand Up @@ -455,7 +455,7 @@ mod rust {
}
}

pub fn p256_ecdh(
fn p256_ecdh(
private_key: &BytesP256ElemLen,
public_key: &BytesP256ElemLen,
) -> BytesP256ElemLen {
Expand Down Expand Up @@ -488,57 +488,14 @@ mod rust {
output_buffer
}

pub fn hmac_sha256(message: &[u8], key: &[u8; SHA256_DIGEST_LEN]) -> BytesHashLen {
// implementation of HMAC as per RFC2104

const IPAD: [u8; 64] = [0x36; 64];
const OPAD: [u8; 64] = [0x5C; 64];

// (1) append zeros to the end of K to create a B byte string
// (e.g., if K is of length 20 bytes and B=64, then K will be
// appended with 44 zero bytes 0x00)
let mut b: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
b[0..SHA256_DIGEST_LEN].copy_from_slice(&key[..]);

// (2) XOR (bitwise exclusive-OR) the B byte string computed in step
// (1) with ipad
let mut s2: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s2[i] = b[i] ^ IPAD[i];
}

// (3) append the stream of data 'text' to the B byte string resulting
// from step (2)
s2[64..64 + message.len()].copy_from_slice(message);

// (4) apply H to the stream generated in step (3)
let ih = sha256_digest(&s2, 64 + message.len());

// (5) XOR (bitwise exclusive-OR) the B byte string computed in
// step (1) with opad
let mut s5: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s5[i] = b[i] ^ OPAD[i];
}
// (6) append the H result from step (4) to the B byte string
// resulting from step (5)
s5[64..64 + SHA256_DIGEST_LEN].copy_from_slice(&ih);

// (7) apply H to the stream generated in step (6) and output
// the result
let oh = sha256_digest(&s5, 3 * SHA256_DIGEST_LEN);

oh
}

pub fn get_random_byte() -> u8 {
fn get_random_byte() -> u8 {
psa_crypto::init().unwrap();
let mut buffer = [0u8; 1];
generate_random(&mut buffer); // TODO: check return value
buffer[0]
}

pub fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
fn p256_generate_key_pair() -> (BytesP256ElemLen, BytesP256ElemLen) {
let alg = RawKeyAgreement::Ecdh;
let mut usage_flags: UsageFlags = UsageFlags::default();
usage_flags.set_export();
Expand Down Expand Up @@ -568,6 +525,49 @@ mod rust {
(private_key, public_key)
}
}

fn hmac_sha256(message: &[u8], key: &[u8; SHA256_DIGEST_LEN]) -> BytesHashLen {
// implementation of HMAC as per RFC2104

const IPAD: [u8; 64] = [0x36; 64];
const OPAD: [u8; 64] = [0x5C; 64];

// (1) append zeros to the end of K to create a B byte string
// (e.g., if K is of length 20 bytes and B=64, then K will be
// appended with 44 zero bytes 0x00)
let mut b: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
b[0..SHA256_DIGEST_LEN].copy_from_slice(&key[..]);

// (2) XOR (bitwise exclusive-OR) the B byte string computed in step
// (1) with ipad
let mut s2: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s2[i] = b[i] ^ IPAD[i];
}

// (3) append the stream of data 'text' to the B byte string resulting
// from step (2)
s2[64..64 + message.len()].copy_from_slice(message);

// (4) apply H to the stream generated in step (3)
let ih = sha256_digest(&s2, 64 + message.len());

// (5) XOR (bitwise exclusive-OR) the B byte string computed in
// step (1) with opad
let mut s5: [u8; MAX_BUFFER_LEN] = [0; MAX_BUFFER_LEN];
for i in 0..64 {
s5[i] = b[i] ^ OPAD[i];
}
// (6) append the H result from step (4) to the B byte string
// resulting from step (5)
s5[64..64 + SHA256_DIGEST_LEN].copy_from_slice(&ih);

// (7) apply H to the stream generated in step (6) and output
// the result
let oh = sha256_digest(&s5, 3 * SHA256_DIGEST_LEN);

oh
}
}

#[cfg(test)]
Expand Down

0 comments on commit ebd014b

Please sign in to comment.