Skip to main content

bitwarden_crypto_primitives/
hkdf.rs

1//! HKDF-SHA256 (RFC 5869).
2
3use zeroize::Zeroizing;
4
5use crate::{Error, backend};
6
7/// Pseudorandom key length in bytes (SHA-256 output).
8pub const PRK_LEN: usize = 32;
9
10/// HKDF-Extract. Returns the pseudorandom key.
11pub fn extract(salt: Option<&[u8]>, ikm: &[u8]) -> Zeroizing<[u8; PRK_LEN]> {
12    backend::hkdf::extract(salt, ikm)
13}
14
15/// HKDF-Expand of `prk` into `out`. Fails if `prk` is shorter than 32 bytes or `out` is longer
16/// than 255 * 32 bytes.
17pub fn expand(prk: &[u8], info: &[u8], out: &mut [u8]) -> Result<(), Error> {
18    backend::hkdf::expand(prk, info, out)
19}
20
21/// HKDF-Extract followed by HKDF-Expand.
22pub fn derive(salt: Option<&[u8]>, ikm: &[u8], info: &[u8], out: &mut [u8]) -> Result<(), Error> {
23    let prk = extract(salt, ikm);
24    expand(prk.as_slice(), info, out)
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::test_vectors::{array, bytes};
31
32    // RFC 5869 test case 1.
33    const IKM: &str = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";
34    const SALT: &str = "000102030405060708090a0b0c";
35    const INFO: &str = "f0f1f2f3f4f5f6f7f8f9";
36    const PRK: &str = "077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5";
37    const OKM: &str =
38        "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865";
39
40    #[test]
41    fn known_answer() {
42        let salt = bytes(SALT);
43        let info = bytes(INFO);
44
45        assert_eq!(
46            hex::encode(extract(Some(&salt), &bytes(IKM)).as_slice()),
47            PRK
48        );
49
50        let mut okm = [0u8; 42];
51        derive(Some(&salt), &bytes(IKM), &info, &mut okm).expect("valid lengths");
52        assert_eq!(hex::encode(okm), OKM);
53    }
54
55    #[test]
56    fn rejects_short_prk_and_long_output() {
57        const SHORT_PRK: &str = "00000000000000000000000000000000";
58        const ZERO_PRK: &str = "0000000000000000000000000000000000000000000000000000000000000000";
59
60        let mut out = [0u8; 32];
61        assert_eq!(
62            expand(&bytes(SHORT_PRK), b"", &mut out),
63            Err(Error::InvalidParameters)
64        );
65
66        let mut too_long = vec![0u8; 255 * 32 + 1];
67        assert_eq!(
68            expand(&bytes(ZERO_PRK), b"", &mut too_long),
69            Err(Error::InvalidParameters)
70        );
71    }
72
73    /// RFC 5869 test case 3: no salt, no info.
74    #[test]
75    fn known_answer_no_salt_no_info() {
76        let mut okm = [0u8; 42];
77        derive(None, &bytes(IKM), b"", &mut okm).expect("valid lengths");
78        assert_eq!(
79            hex::encode(okm),
80            "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d\
81            9d201395faa4b61a96c8"
82        );
83    }
84
85    /// Shortest and longest (255 blocks) expansions; the long one is compared by its SHA-256.
86    #[test]
87    fn known_answer_output_length_bounds() {
88        let prk: [u8; PRK_LEN] =
89            array("4242424242424242424242424242424242424242424242424242424242424242");
90
91        let mut one = [0u8; 1];
92        expand(&prk, b"info", &mut one).expect("valid length");
93        assert_eq!(hex::encode(one), "a2");
94
95        let mut max = vec![0u8; 255 * PRK_LEN];
96        expand(&prk, b"info", &mut max).expect("valid length");
97        assert_eq!(
98            hex::encode(crate::sha256::digest(&max)),
99            "1a40dcf431b2fc7b6404941c3e4fcd3b1b2f9d0efb269a910fb52236ff39df59"
100        );
101    }
102}