async_psec/
crypto.rs

1use std::convert::TryInto;
2use hkdf::Hkdf;
3use hmac::{Hmac, NewMac, Mac};
4use sha2::Sha384;
5
6pub const HASH_OUTPUT_LEN: usize = 48; //SHA384
7const KEY_LEN: usize = 16;
8pub const IV_LEN: usize = 12;
9
10pub fn iv_to_nonce(iv: &[u8], counter: &mut usize) -> Vec<u8> {
11    let mut counter_bytes = vec![0; 4];
12    counter_bytes.extend_from_slice(&counter.to_be_bytes());
13    let r: Vec<u8> = iv.iter().zip(counter_bytes.iter()).map(|(a, b)| a^b).collect();
14    *counter += 1;
15    r
16}
17
18fn hkdf_expand_label(key: &[u8], label: &str, context: Option<&[u8]>, okm: &mut [u8]) {
19    let mut info: Vec<u8> = [&(label.len() as u32).to_be_bytes(), label.as_bytes()].concat();
20    if let Some(context) = context {
21        info.extend([&(context.len() as u32).to_be_bytes(), context].concat());
22    }
23    let hkdf = Hkdf::<Sha384>::from_prk(key).unwrap();
24    hkdf.expand(&info, okm).unwrap();
25}
26
27fn get_labels(handshake: bool, i_am_bob: bool) -> (String, String) {
28    let mut label = if handshake {
29        "handshake"
30    } else {
31        "application"
32    }.to_owned();
33    label += "_i_am_";
34    let local_label = label.clone() + if i_am_bob {
35        "bob"
36    } else {
37        "alice"
38    };
39    let peer_label = label + if i_am_bob {
40        "alice"
41    } else {
42        "bob"
43    };
44    (local_label, peer_label)
45}
46
47pub struct HandshakeKeys {
48    pub local_key: [u8; KEY_LEN],
49    pub local_iv: [u8; IV_LEN],
50    pub local_handshake_traffic_secret: [u8; HASH_OUTPUT_LEN],
51    pub peer_key: [u8; KEY_LEN],
52    pub peer_iv: [u8; IV_LEN],
53    pub peer_handshake_traffic_secret: [u8; HASH_OUTPUT_LEN],
54    pub handshake_secret: [u8; HASH_OUTPUT_LEN],
55}
56
57impl HandshakeKeys {
58    pub fn derive_keys(shared_secret: [u8; 32], handshake_hash: [u8; HASH_OUTPUT_LEN], i_am_bob: bool) -> HandshakeKeys {
59        let (handshake_secret, _) = Hkdf::<Sha384>::extract(None, &shared_secret);
60
61        let (local_label, peer_label) = get_labels(true, i_am_bob);
62
63        let mut local_handshake_traffic_secret = [0; HASH_OUTPUT_LEN];
64        hkdf_expand_label(handshake_secret.as_slice(), &local_label, Some(&handshake_hash), &mut local_handshake_traffic_secret);
65
66        let mut peer_handshake_traffic_secret = [0; HASH_OUTPUT_LEN];
67        hkdf_expand_label(handshake_secret.as_slice(), &peer_label, Some(&handshake_hash), &mut peer_handshake_traffic_secret);
68
69        let mut local_handshake_key = [0; KEY_LEN];
70        hkdf_expand_label(&local_handshake_traffic_secret, "key", None, &mut local_handshake_key);
71        let mut local_handshake_iv = [0; IV_LEN];
72        hkdf_expand_label(&local_handshake_traffic_secret, "iv", None, &mut local_handshake_iv);
73    
74        let mut peer_handshake_key = [0; KEY_LEN];
75        hkdf_expand_label(&peer_handshake_traffic_secret, "key", None, &mut peer_handshake_key);
76        let mut peer_handshake_iv = [0; IV_LEN];
77        hkdf_expand_label(&peer_handshake_traffic_secret,"iv", None, &mut peer_handshake_iv);
78
79        HandshakeKeys {
80            local_key: local_handshake_key,
81            local_iv: local_handshake_iv,
82            local_handshake_traffic_secret: local_handshake_traffic_secret,
83            peer_key: peer_handshake_key,
84            peer_iv: peer_handshake_iv,
85            peer_handshake_traffic_secret: peer_handshake_traffic_secret,
86            handshake_secret: handshake_secret.as_slice().try_into().unwrap(),
87        }
88    }
89}
90
91pub struct ApplicationKeys {
92    pub local_key: [u8; KEY_LEN],
93    pub local_iv: [u8; IV_LEN],
94    pub peer_key: [u8; KEY_LEN],
95    pub peer_iv: [u8; IV_LEN],
96}
97
98impl ApplicationKeys {
99    pub fn derive_keys(handshake_secret: [u8; HASH_OUTPUT_LEN], handshake_hash: [u8; HASH_OUTPUT_LEN], i_am_bob: bool) -> ApplicationKeys {
100        let mut derived_secret = [0; HASH_OUTPUT_LEN];
101        hkdf_expand_label(&handshake_secret, "derived", None, &mut derived_secret);
102        let (master_secret, _) = Hkdf::<Sha384>::extract(Some(&derived_secret), b"");
103
104        let (local_label, peer_label) = get_labels(false, i_am_bob);
105        
106        let mut local_application_traffic_secret = [0; HASH_OUTPUT_LEN];
107        hkdf_expand_label(&master_secret, &local_label, Some(&handshake_hash), &mut local_application_traffic_secret);
108    
109        let mut peer_application_traffic_secret = [0; HASH_OUTPUT_LEN];
110        hkdf_expand_label(&master_secret, &peer_label, Some(&handshake_hash), &mut peer_application_traffic_secret);
111
112        let mut local_application_key = [0; KEY_LEN];
113        hkdf_expand_label(&local_application_traffic_secret, "key", None, &mut local_application_key);
114        let mut local_application_iv = [0; IV_LEN];
115        hkdf_expand_label(&local_application_traffic_secret, "iv", None, &mut local_application_iv);
116    
117        let mut peer_application_key = [0; KEY_LEN];
118        hkdf_expand_label(&peer_application_traffic_secret, "key", None, &mut peer_application_key);
119        let mut peer_application_iv = [0; IV_LEN];
120        hkdf_expand_label(&peer_application_traffic_secret,"iv", None, &mut peer_application_iv);
121
122        ApplicationKeys {
123            local_key: local_application_key,
124            local_iv: local_application_iv,
125            peer_key: peer_application_key,
126            peer_iv: peer_application_iv,
127        }
128    }
129}
130
131pub fn compute_handshake_finished(local_handshake_traffic_secret: [u8; HASH_OUTPUT_LEN], handshake_hash: [u8; HASH_OUTPUT_LEN]) -> [u8; HASH_OUTPUT_LEN] {
132    let mut finished_key = [0; HASH_OUTPUT_LEN];
133    hkdf_expand_label(&local_handshake_traffic_secret, "finished", None, &mut finished_key);
134    let mut hmac = Hmac::<Sha384>::new_from_slice(&finished_key).unwrap();
135    hmac.update(&handshake_hash);
136    hmac.finalize().into_bytes().as_slice().try_into().unwrap()
137}
138
139pub fn verify_handshake_finished(peer_handshake_finished: [u8; HASH_OUTPUT_LEN], peer_handshake_traffic_secret: [u8; HASH_OUTPUT_LEN], handshake_hash: [u8; HASH_OUTPUT_LEN]) -> bool {
140    let mut peer_finished_key = [0; HASH_OUTPUT_LEN];
141    hkdf_expand_label(&peer_handshake_traffic_secret, "finished", None, &mut peer_finished_key);
142    let mut hmac = Hmac::<Sha384>::new_from_slice(&peer_finished_key).unwrap();
143    hmac.update(&handshake_hash);
144    hmac.verify(&peer_handshake_finished).is_ok()
145}
146
147#[cfg(test)]
148mod tests {
149    use super::{IV_LEN, HASH_OUTPUT_LEN};
150    use rand::{Rng, RngCore, rngs::OsRng};
151
152    #[test]
153    fn iv_to_nonce() {
154        let mut iv = [0; IV_LEN];
155        OsRng.fill_bytes(&mut iv);
156        let mut counter = OsRng.gen();
157
158        let mut counters = Vec::with_capacity(1000);
159        let mut nonces = Vec::with_capacity(1000);
160        for _ in 0..1000 {
161            counters.push(counter);
162            let nonce = super::iv_to_nonce(&iv, &mut counter);
163
164            assert!(!counters.contains(&counter));
165            assert!(!nonces.contains(&nonce));
166
167            nonces.push(nonce);
168        }
169    }
170
171    #[test]
172    fn get_labels() {
173        let (hl, hp) = super::get_labels(true, true);
174        assert_eq!(hl, "handshake_i_am_bob");
175        assert_eq!(hp, "handshake_i_am_alice");
176
177        let (al, ap) = super::get_labels(false, false);
178        assert_eq!(al, "application_i_am_alice");
179        assert_eq!(ap, "application_i_am_bob");
180    }
181
182    #[test]
183    fn hkdf_expand_label() {
184        let key = "Hardcore Music is the best music. You can't deny";
185        let mut okm = [0; HASH_OUTPUT_LEN];
186        super::hkdf_expand_label(key.as_bytes(), "the_label", Some(b"the_context"), &mut okm);
187        assert_eq!(hex::encode(okm), "108b05132cfdb9416be7a63763eda8e834b2235556b36aab5ced2cac15d7d2c24fb1d579a8c5de5c9cd5d2a357545bbf");
188    }
189}