1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
45// This file contains code that was copied from the ring crate which is under
6// the ISC license, reproduced below:
78// Copyright 2015-2017 Brian Smith.
910// Permission to use, copy, modify, and/or distribute this software for any
11// purpose with or without fee is hereby granted, provided that the above
12// copyright notice and this permission notice appear in all copies.
1314// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
15// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
17// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2122use crate::{error::*, hmac};
2324pub fn extract_and_expand(
25 salt: &hmac::SigningKey,
26 secret: &[u8],
27 info: &[u8],
28 out: &mut [u8],
29) -> Result<()> {
30let prk = extract(salt, secret)?;
31 expand(&prk, info, out)?;
32Ok(())
33}
3435pub fn extract(salt: &hmac::SigningKey, secret: &[u8]) -> Result<hmac::SigningKey> {
36let prk = hmac::sign(salt, secret)?;
37Ok(hmac::SigningKey::new(salt.digest_algorithm(), prk.as_ref()))
38}
3940pub fn expand(prk: &hmac::SigningKey, info: &[u8], out: &mut [u8]) -> Result<()> {
41let mut derived =
42 nss::pk11::sym_key::hkdf_expand(prk.digest_alg, &prk.key_value, info, out.len())?;
43 out.swap_with_slice(&mut derived[0..out.len()]);
44Ok(())
45}
4647#[cfg(test)]
48mod tests {
49use super::*;
50use crate::digest;
51use nss::ensure_initialized;
5253#[test]
54fn hkdf_produces_correct_result() {
55 ensure_initialized();
56let secret = hex::decode("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").unwrap();
57let salt = hex::decode("000102030405060708090a0b0c").unwrap();
58let info = hex::decode("f0f1f2f3f4f5f6f7f8f9").unwrap();
59let expected_out = hex::decode(
60"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
61 )
62 .unwrap();
63let salt = hmac::SigningKey::new(&digest::SHA256, &salt);
64let mut out = vec![0u8; expected_out.len()];
65 extract_and_expand(&salt, &secret, &info, &mut out).unwrap();
66assert_eq!(out, expected_out);
67 }
6869#[test]
70fn hkdf_rejects_gigantic_salt() {
71 ensure_initialized();
72if (u32::MAX as usize) < usize::MAX {
73let salt_bytes = vec![0; (u32::MAX as usize) + 1];
74let salt = hmac::SigningKey {
75 digest_alg: &digest::SHA256,
76 key_value: salt_bytes,
77 };
78let mut out = vec![0u8; 8];
79assert!(extract_and_expand(&salt, b"secret", b"info", &mut out).is_err());
80 }
81 }
8283#[test]
84fn hkdf_rejects_gigantic_secret() {
85 ensure_initialized();
86if (u32::MAX as usize) < usize::MAX {
87let salt = hmac::SigningKey::new(&digest::SHA256, b"salt");
88let secret = vec![0; (u32::MAX as usize) + 1];
89let mut out = vec![0u8; 8];
90assert!(extract_and_expand(&salt, secret.as_slice(), b"info", &mut out).is_err());
91 }
92 }
9394// N.B. the `info `parameter is a `c_ulong`, and I can't figure out how to check whether
95 // `c_ulong` is smaller than `usize` in order to write a `hkdf_rejects_gigantic_info` test.
9697#[test]
98fn hkdf_rejects_gigantic_output_buffers() {
99 ensure_initialized();
100let salt = hmac::SigningKey::new(&digest::SHA256, b"salt");
101let mut out = vec![0u8; 8160 + 1]; // RFC maximum (hashlen * 255) + 1
102assert!(extract_and_expand(&salt, b"secret", b"info", &mut out).is_err());
103 }
104105#[test]
106fn hkdf_rejects_zero_length_output_buffer() {
107 ensure_initialized();
108let salt = hmac::SigningKey::new(&digest::SHA256, b"salt");
109let mut out = vec![0u8; 0];
110assert!(extract_and_expand(&salt, b"secret", b"info", &mut out).is_err());
111 }
112113#[test]
114fn hkdf_can_produce_small_output() {
115 ensure_initialized();
116let salt = hmac::SigningKey::new(&digest::SHA256, b"salt");
117let mut out = vec![0u8; 1];
118assert!(extract_and_expand(&salt, b"secret", b"info", &mut out).is_ok());
119 }
120121#[test]
122fn hkdf_accepts_zero_length_info() {
123 ensure_initialized();
124let salt = hmac::SigningKey::new(&digest::SHA256, b"salt");
125let mut out = vec![0u8; 32];
126assert!(extract_and_expand(&salt, b"secret", b"", &mut out).is_ok());
127 }
128129#[test]
130fn hkdf_expand_rejects_short_prk() {
131 ensure_initialized();
132let prk = hmac::SigningKey::new(&digest::SHA256, b"too short"); // must be >= HashLen
133let mut out = vec![0u8; 8];
134assert!(expand(&prk, b"info", &mut out).is_ok());
135 }
136}