Skip to content

Commit 17e5f99

Browse files
bors[bot]kedars
authored andcommitted
Merge #176
176: Add support for HKDF r=jethrogb a=kedars Co-authored-by: Kedar Sovani <[email protected]>
1 parent 161eb60 commit 17e5f99

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
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/src/pk/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extern "C" fn alloc_custom_pk_ctx() -> *mut c_void {
111111
}
112112

113113
unsafe extern "C" fn free_custom_pk_ctx(p: *mut c_void) {
114-
Box::from_raw(p as *mut CustomPkContext);
114+
let _ = Box::from_raw(p as *mut CustomPkContext);
115115
}
116116

117117
extern "C" fn custom_pk_can_do(_t: u32) -> i32 {

mbedtls/src/x509/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub use self::profile::Profile;
2828
use mbedtls_sys::*;
2929
use mbedtls_sys::types::raw_types::c_uint;
3030
bitflags! {
31-
#[doc(inline)]
3231
pub struct KeyUsage: c_uint {
3332
const DIGITAL_SIGNATURE = X509_KU_DIGITAL_SIGNATURE as c_uint;
3433
const NON_REPUDIATION = X509_KU_NON_REPUDIATION as c_uint;
@@ -43,7 +42,6 @@ bitflags! {
4342
}
4443

4544
bitflags! {
46-
#[doc(inline)]
4745
pub struct VerifyError: u32 {
4846
const CERT_BAD_KEY = X509_BADCERT_BAD_KEY as u32;
4947
const CERT_BAD_MD = X509_BADCERT_BAD_MD as u32;

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)