Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/aienos-kernel/src/crypto/hkdf.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / aienos-kernel / src / crypto / hkdf.rs` 132 lines ยท 5030 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! HKDF-SHA256 (RFC 5869). 17 use super::hmac::HmacSha256; 18 use super::sha256::Digest; 19 20 const HASH_LEN: usize = 32; 21 22 fn zeroize(bytes: &mut [u8]) { 23 for byte in bytes { 24 // Volatile writes prevent the compiler from eliding secret cleanup. 25 unsafe { core::ptr::write_volatile(byte, 0) }; 26 } 27 } 28 29 /// HKDF expand error for outputs longer than RFC 5869 permits. 30 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 31 pub enum HkdfError { 32 /// Output length exceeds 255 SHA-256 blocks (8160 bytes). 33 OutputTooLong, 34 } 35 36 /// HKDF-Extract(salt, IKM), returning the 32-byte pseudorandom key. 37 pub fn hkdf_extract(salt: &[u8], ikm: &[u8]) -> Digest { 38 let mut hmac = HmacSha256::new(salt); 39 hmac.update(ikm); 40 hmac.finalize() 41 } 42 43 /// HKDF-Expand(PRK, info), writing output in place. 44 pub fn hkdf_expand(prk: &[u8], info: &[u8], out: &mut [u8]) -> Result<(), HkdfError> { 45 if out.len() > 255 * HASH_LEN { 46 return Err(HkdfError::OutputTooLong); 47 } 48 let mut scratch = ExpandScratch { 49 block: [0; HASH_LEN], 50 }; 51 let previous = &mut scratch.block; 52 let mut previous_len = 0; 53 let mut written = 0; 54 let blocks = out.len().div_ceil(HASH_LEN); 55 for counter in 1..=blocks { 56 let mut hmac = HmacSha256::new(prk); 57 hmac.update(&previous[..previous_len]); 58 hmac.update(info); 59 hmac.update(&[counter as u8]); 60 *previous = hmac.finalize(); 61 previous_len = HASH_LEN; 62 let n = core::cmp::min(HASH_LEN, out.len() - written); 63 out[written..written + n].copy_from_slice(&previous[..n]); 64 written += n; 65 } 66 Ok(()) 67 } 68 69 impl Drop for ExpandScratch { 70 fn drop(&mut self) { 71 zeroize(&mut self.block); 72 } 73 } 74 75 // Kept as a drop guard so scratch is wiped on every exit path, including panic. 76 struct ExpandScratch { 77 block: Digest, 78 } 79 80 #[cfg(test)] 81 mod tests { 82 use super::*; 83 fn bytes(hex: &str) -> std::vec::Vec { 84 (0..hex.len()) 85 .step_by(2) 86 .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap()) 87 .collect() 88 } 89 fn case(ikm: &[u8], salt: &[u8], info: &[u8], length: usize, prk: &str, okm: &str) { 90 let actual_prk = hkdf_extract(salt, ikm); 91 assert_eq!(actual_prk.as_slice(), bytes(prk)); 92 let mut actual = std::vec![0; length]; 93 hkdf_expand(&actual_prk, info, &mut actual).unwrap(); 94 assert_eq!(actual, bytes(okm)); 95 } 96 #[test] 97 fn rfc5869_cases_1_to_3() { 98 case( 99 &[0x0b; 22], 100 &bytes("`000102030405060708090a0b0c`"), 101 &bytes("`f0f1f2f3f4f5f6f7f8f9`"), 102 42, 103 "`077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5`", 104 "`3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865`", 105 ); 106 case( 107 &[0x0b; 22], 108 &[], 109 &[], 110 42, 111 "`19ef24a32c717b167f33a91d6f648bdf96596776afdb6377ac434c1c293ccb04`", 112 "`8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8`", 113 ); 114 let ikm = bytes("`000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f`"); 115 let salt = bytes("`606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf`"); 116 let info = bytes("`b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff`"); 117 case(&ikm, &salt, &info, 82, 118 "`06a6b88c5853361a06104c9ceb35b45cef760014904671014a193f40c15fc244`", 119 "`b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87`"); 120 } 121 #[test] 122 fn length_limit_and_compare() { 123 let mut too_long = std::vec![0; 255 * HASH_LEN + 1]; 124 assert_eq!( 125 hkdf_expand(&[0; 32], b"", &mut too_long), 126 Err(HkdfError::OutputTooLong) 127 ); 128 let mut max = std::vec![0; 255 * HASH_LEN]; 129 hkdf_expand(&[0; 32], b"", &mut max).unwrap(); 130 assert!(super::super::hmac::constant_time_eq(&max, &max)); 131 } 132 }