aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / crypto / hmac.rs
| 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 | //! HMAC-SHA256 (RFC 2104). |
| 17 | use super::sha256::{hash, Digest, Sha256}; |
| 18 | |
| 19 | const BLOCK_LEN: usize = 64; |
| 20 | |
| 21 | fn zeroize(bytes: &mut [u8]) { |
| 22 | for byte in bytes { |
| 23 | // Volatile writes prevent the compiler from eliding secret cleanup. |
| 24 | unsafe { core::ptr::write_volatile(byte, 0) }; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /// Streaming HMAC-SHA256 state. The keyed pads are erased when it is dropped. |
| 29 | pub struct HmacSha256 { |
| 30 | inner: Option<Sha256>, |
| 31 | outer_pad: [u8; BLOCK_LEN], |
| 32 | } |
| 33 | |
| 34 | impl HmacSha256 { |
| 35 | /// Start an HMAC operation with any key length. |
| 36 | pub fn new(key: &[u8]) -> Self { |
| 37 | let mut key_block = [0u8; BLOCK_LEN]; |
| 38 | if key.len() > BLOCK_LEN { |
| 39 | let mut key_digest = hash(key); |
| 40 | key_block[..32].copy_from_slice(&key_digest); |
| 41 | zeroize(&mut key_digest); |
| 42 | } else { |
| 43 | key_block[..key.len()].copy_from_slice(key); |
| 44 | } |
| 45 | let mut inner_pad = [0x36; BLOCK_LEN]; |
| 46 | let mut outer_pad = [0x5c; BLOCK_LEN]; |
| 47 | for i in 0..BLOCK_LEN { |
| 48 | inner_pad[i] ^= key_block[i]; |
| 49 | outer_pad[i] ^= key_block[i]; |
| 50 | } |
| 51 | zeroize(&mut key_block); |
| 52 | let mut inner = Sha256::new(); |
| 53 | inner.update(&inner_pad); |
| 54 | zeroize(&mut inner_pad); |
| 55 | Self { |
| 56 | inner: Some(inner), |
| 57 | outer_pad, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// Add message bytes to the MAC. |
| 62 | pub fn update(&mut self, data: &[u8]) { |
| 63 | self.inner |
| 64 | .as_mut() |
| 65 | .expect("HMAC state finalized") |
| 66 | .update(data); |
| 67 | } |
| 68 | |
| 69 | /// Finish the MAC and erase its retained outer pad. |
| 70 | pub fn finalize(mut self) -> Digest { |
| 71 | let mut inner_digest = self.inner.take().expect("HMAC state finalized").finalize(); |
| 72 | let mut outer = Sha256::new(); |
| 73 | outer.update(&self.outer_pad); |
| 74 | outer.update(&inner_digest); |
| 75 | let result = outer.finalize(); |
| 76 | zeroize(&mut inner_digest); |
| 77 | zeroize(&mut self.outer_pad); |
| 78 | result |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl Drop for HmacSha256 { |
| 83 | fn drop(&mut self) { |
| 84 | zeroize(&mut self.outer_pad); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /// Compute HMAC-SHA256 over a single message. |
| 89 | pub fn hmac_sha256(key: &[u8], message: &[u8]) -> Digest { |
| 90 | let mut hmac = HmacSha256::new(key); |
| 91 | hmac.update(message); |
| 92 | hmac.finalize() |
| 93 | } |
| 94 | |
| 95 | /// Compare tags without exiting early based on their contents. |
| 96 | pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool { |
| 97 | let mut diff = a.len() ^ b.len(); |
| 98 | for i in 0..core::cmp::max(a.len(), b.len()) { |
| 99 | let x = a.get(i).copied().unwrap_or(0); |
| 100 | let y = b.get(i).copied().unwrap_or(0); |
| 101 | diff |= usize::from(x ^ y); |
| 102 | } |
| 103 | diff == 0 |
| 104 | } |
| 105 | |
| 106 | #[cfg(test)] |
| 107 | mod tests { |
| 108 | use super::*; |
| 109 | fn bytes(hex: &str) -> std::vec::Vec<u8> { |
| 110 | (0..hex.len()) |
| 111 | .step_by(2) |
| 112 | .map(|i| u8::from_str_radix(&hex[i..i + 2], 16).unwrap()) |
| 113 | .collect() |
| 114 | } |
| 115 | fn check(key: &[u8], message: &[u8], expected: &str) { |
| 116 | let mut h = HmacSha256::new(key); |
| 117 | for part in message.chunks(7) { |
| 118 | h.update(part); |
| 119 | } |
| 120 | let tag = h.finalize(); |
| 121 | let expected = bytes(expected); |
| 122 | assert_eq!(&tag[..expected.len()], expected.as_slice()); |
| 123 | } |
| 124 | #[test] |
| 125 | fn rfc4231_cases_1_to_7() { |
| 126 | check( |
| 127 | &[0x0b; 20], |
| 128 | b"Hi There", |
| 129 | "b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7", |
| 130 | ); |
| 131 | check( |
| 132 | b"Jefe", |
| 133 | b"what do ya want for nothing?", |
| 134 | "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", |
| 135 | ); |
| 136 | check( |
| 137 | &[0xaa; 20], |
| 138 | &[0xdd; 50], |
| 139 | "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe", |
| 140 | ); |
| 141 | let key4: std::vec::Vec<u8> = (1..=25).collect(); |
| 142 | check(&key4, &[0xcd; 50], "82558a389a443c0ea4cc819899f2083a"); |
| 143 | check( |
| 144 | &[0x0c; 20], |
| 145 | b"Test With Truncation", |
| 146 | "a3b6167473100ee06e0c796c2955552b", |
| 147 | ); |
| 148 | check( |
| 149 | &[0xaa; 131], |
| 150 | b"Test Using Larger Than Block-Size Key - Hash Key First", |
| 151 | "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54", |
| 152 | ); |
| 153 | check(&[0xaa; 131], b"This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.", "9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"); |
| 154 | } |
| 155 | #[test] |
| 156 | fn compare_tags() { |
| 157 | assert!(constant_time_eq(b"same", b"same")); |
| 158 | assert!(!constant_time_eq(b"same", b"samf")); |
| 159 | assert!(!constant_time_eq(b"same", b"same!")); |
| 160 | } |
| 161 | } |