diff --git a/Cargo.toml b/Cargo.toml index d4f0372..bd154cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ hkdf = "0.11" hmac = "0.11" [dev-dependencies] +hex = "0.4" tokio = {version = "1.6", features = ["rt-multi-thread", "macros"]} [features] diff --git a/src/crypto.rs b/src/crypto.rs index e8bc83b..3a3ed7e 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -17,18 +17,12 @@ pub fn iv_to_nonce(iv: &[u8], counter: &mut usize) -> Vec { } fn hkdf_expand_label(key: &[u8], label: &str, context: Option<&[u8]>, okm: &mut [u8]) { + let mut info: Vec = [&(label.len() as u32).to_be_bytes(), label.as_bytes()].concat(); + if let Some(context) = context { + info.extend([&(context.len() as u32).to_be_bytes(), context].concat()); + } let hkdf = Hkdf::::from_prk(key).unwrap(); - //can't set info conditionnally because of different array size - match context { - Some(context) => { - let info = [&(label.len() as u32).to_be_bytes(), label.as_bytes(), &(context.len() as u32).to_be_bytes(), context]; - hkdf.expand_multi_info(&info, okm).unwrap(); - } - None => { - let info = [&(label.len() as u32).to_be_bytes(), label.as_bytes()]; - hkdf.expand_multi_info(&info, okm).unwrap(); - } - }; + hkdf.expand(&info, okm).unwrap(); } fn get_labels(handshake: bool, i_am_bob: bool) -> (String, String) { @@ -153,7 +147,7 @@ pub fn verify_handshake_finished(peer_handshake_finished: [u8; HASH_OUTPUT_LEN], #[cfg(test)] mod tests { - use super::IV_LEN; + use super::{IV_LEN, HASH_OUTPUT_LEN}; use rand::{Rng, RngCore, rngs::OsRng}; #[test] @@ -185,4 +179,12 @@ mod tests { assert_eq!(al, "application_i_am_alice"); assert_eq!(ap, "application_i_am_bob"); } -} + + #[test] + fn hkdf_expand_label() { + let key = "Hardcore Music is the best music. You can't deny"; + let mut okm = [0; HASH_OUTPUT_LEN]; + super::hkdf_expand_label(key.as_bytes(), "the_label", Some(b"the_context"), &mut okm); + assert_eq!(hex::encode(okm), "108b05132cfdb9416be7a63763eda8e834b2235556b36aab5ced2cac15d7d2c24fb1d579a8c5de5c9cd5d2a357545bbf"); + } +} \ No newline at end of file