Skip to content

Commit 4bd96c3

Browse files
bors[bot]kedars
andauthored
Merge #176
176: Add support for HKDF r=jethrogb a=kedars Co-authored-by: Kedar Sovani <[email protected]>
2 parents f42d25c + b2bf356 commit 4bd96c3

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

mbedtls/src/hash/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ impl Md {
147147
Ok(olen)
148148
}
149149
}
150+
151+
pub fn hkdf(md: Type, salt: &[u8], ikm: &[u8], info: &[u8], key: &mut [u8]) -> Result<()> {
152+
let md: MdInfo = match md.into() {
153+
Some(md) => md,
154+
None => return Err(Error::MdBadInputData),
155+
};
156+
157+
unsafe {
158+
hkdf(
159+
md.inner,
160+
salt.as_ptr(),
161+
salt.len(),
162+
ikm.as_ptr(),
163+
ikm.len(),
164+
info.as_ptr(),
165+
info.len(),
166+
key.as_mut_ptr(),
167+
key.len(),
168+
)
169+
.into_result()?;
170+
Ok(())
171+
}
172+
}
150173
}
151174

152175
pub fn pbkdf2_hmac(

mbedtls/tests/hkdf.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright (c) Fortanix, Inc.
2+
*
3+
* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or
4+
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version
5+
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your
6+
* option. This file may not be copied, modified, or distributed except
7+
* according to those terms. */
8+
9+
use mbedtls::hash::Md;
10+
use mbedtls::hash::Type as MdType;
11+
12+
#[test]
13+
fn test_hkdf_sha256() {
14+
let ikm = [
15+
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
16+
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
17+
];
18+
19+
let salt = [
20+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
21+
];
22+
let info = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9];
23+
let mut output = [0u8; 42];
24+
Md::hkdf(MdType::Sha256, &salt, &ikm, &info, &mut output).unwrap();
25+
26+
assert_eq!(
27+
output,
28+
[
29+
0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36,
30+
0x2f, 0x2a, 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, 0x5d, 0xb0, 0x2d, 0x56,
31+
0xec, 0xc4, 0xc5, 0xbf, 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, 0x58, 0x65
32+
]
33+
);
34+
}

0 commit comments

Comments
 (0)