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-crypto / src / sha256.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 | //! Pure-Rust, sovereign SHA-256 implementation conforming to FIPS 180-4. |
| 17 | //! |
| 18 | //! Fully deterministic and zero-dependency, safe for bare-metal `#![no_std]`. |
| 19 | |
| 20 | /// 32-byte digest returned by SHA-256. |
| 21 | pub type Digest = [u8; 32]; |
| 22 | |
| 23 | const K: [u32; 64] = [ |
| 24 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 25 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 26 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 27 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 28 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 29 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 30 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 31 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, |
| 32 | ]; |
| 33 | |
| 34 | /// SHA-256 state machine. |
| 35 | #[derive(Clone, Debug)] |
| 36 | pub struct Sha256 { |
| 37 | state: [u32; 8], |
| 38 | buffer: [u8; 64], |
| 39 | buf_len: usize, |
| 40 | total_len_bytes: u64, |
| 41 | } |
| 42 | |
| 43 | impl Drop for Sha256 { |
| 44 | fn drop(&mut self) { |
| 45 | for word in &mut self.state { |
| 46 | // Volatile writes prevent intermediate hash state from being elided. |
| 47 | unsafe { core::ptr::write_volatile(word, 0) }; |
| 48 | } |
| 49 | for byte in &mut self.buffer { |
| 50 | unsafe { core::ptr::write_volatile(byte, 0) }; |
| 51 | } |
| 52 | unsafe { |
| 53 | core::ptr::write_volatile(&mut self.buf_len, 0); |
| 54 | core::ptr::write_volatile(&mut self.total_len_bytes, 0); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl Default for Sha256 { |
| 60 | fn default() -> Self { |
| 61 | Self::new() |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl Sha256 { |
| 66 | /// Initialize a new SHA-256 state with FIPS 180-4 standard IV. |
| 67 | pub const fn new() -> Self { |
| 68 | Self { |
| 69 | state: [ |
| 70 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, |
| 71 | 0x5be0cd19, |
| 72 | ], |
| 73 | buffer: [0u8; 64], |
| 74 | buf_len: 0, |
| 75 | total_len_bytes: 0, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Ingest a slice of bytes into the hasher. |
| 80 | pub fn update(&mut self, data: &[u8]) { |
| 81 | self.total_len_bytes = self.total_len_bytes.wrapping_add(data.len() as u64); |
| 82 | let mut offset = 0; |
| 83 | |
| 84 | // Fill remaining buffer if not empty |
| 85 | if self.buf_len > 0 { |
| 86 | let to_copy = core::cmp::min(64 - self.buf_len, data.len()); |
| 87 | self.buffer[self.buf_len..self.buf_len + to_copy].copy_from_slice(&data[..to_copy]); |
| 88 | self.buf_len += to_copy; |
| 89 | offset += to_copy; |
| 90 | |
| 91 | if self.buf_len == 64 { |
| 92 | let block = self.buffer; |
| 93 | Self::process_block(&mut self.state, &block); |
| 94 | self.buf_len = 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Process full 64-byte chunks |
| 99 | while offset + 64 <= data.len() { |
| 100 | let mut block = [0u8; 64]; |
| 101 | block.copy_from_slice(&data[offset..offset + 64]); |
| 102 | Self::process_block(&mut self.state, &block); |
| 103 | offset += 64; |
| 104 | } |
| 105 | |
| 106 | // Buffer remaining bytes |
| 107 | if offset < data.len() { |
| 108 | let rem = data.len() - offset; |
| 109 | self.buffer[..rem].copy_from_slice(&data[offset..]); |
| 110 | self.buf_len = rem; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /// Finalize hash calculation and return the 32-byte digest. |
| 115 | pub fn finalize(mut self) -> Digest { |
| 116 | let bit_len = self.total_len_bytes.wrapping_mul(8); |
| 117 | |
| 118 | // Append 0x80 byte |
| 119 | self.buffer[self.buf_len] = 0x80; |
| 120 | self.buf_len += 1; |
| 121 | |
| 122 | // If buffer len > 56, pad with zeros, process block, then pad next block |
| 123 | if self.buf_len > 56 { |
| 124 | for b in &mut self.buffer[self.buf_len..64] { |
| 125 | *b = 0; |
| 126 | } |
| 127 | let block = self.buffer; |
| 128 | Self::process_block(&mut self.state, &block); |
| 129 | self.buffer = [0u8; 64]; |
| 130 | self.buf_len = 0; |
| 131 | } |
| 132 | |
| 133 | // Pad with zeros up to byte 56 |
| 134 | for b in &mut self.buffer[self.buf_len..56] { |
| 135 | *b = 0; |
| 136 | } |
| 137 | |
| 138 | // Append bit length as 64-bit big endian integer |
| 139 | self.buffer[56..64].copy_from_slice(&bit_len.to_be_bytes()); |
| 140 | let block = self.buffer; |
| 141 | Self::process_block(&mut self.state, &block); |
| 142 | |
| 143 | let mut out = [0u8; 32]; |
| 144 | for (i, word) in self.state.iter().enumerate() { |
| 145 | out[i * 4..(i + 1) * 4].copy_from_slice(&word.to_be_bytes()); |
| 146 | } |
| 147 | out |
| 148 | } |
| 149 | |
| 150 | fn process_block(state: &mut [u32; 8], block: &[u8; 64]) { |
| 151 | let mut w = [0u32; 64]; |
| 152 | for i in 0..16 { |
| 153 | w[i] = u32::from_be_bytes([ |
| 154 | block[i * 4], |
| 155 | block[i * 4 + 1], |
| 156 | block[i * 4 + 2], |
| 157 | block[i * 4 + 3], |
| 158 | ]); |
| 159 | } |
| 160 | |
| 161 | for i in 16..64 { |
| 162 | let s0 = w[i - 15].rotate_right(7) ^ w[i - 15].rotate_right(18) ^ (w[i - 15] >> 3); |
| 163 | let s1 = w[i - 2].rotate_right(17) ^ w[i - 2].rotate_right(19) ^ (w[i - 2] >> 10); |
| 164 | w[i] = w[i - 16] |
| 165 | .wrapping_add(s0) |
| 166 | .wrapping_add(w[i - 7]) |
| 167 | .wrapping_add(s1); |
| 168 | } |
| 169 | |
| 170 | let mut a = state[0]; |
| 171 | let mut b = state[1]; |
| 172 | let mut c = state[2]; |
| 173 | let mut d = state[3]; |
| 174 | let mut e = state[4]; |
| 175 | let mut f = state[5]; |
| 176 | let mut g = state[6]; |
| 177 | let mut h = state[7]; |
| 178 | |
| 179 | for i in 0..64 { |
| 180 | let s1 = e.rotate_right(6) ^ e.rotate_right(11) ^ e.rotate_right(25); |
| 181 | let ch = (e & f) ^ ((!e) & g); |
| 182 | let temp1 = h |
| 183 | .wrapping_add(s1) |
| 184 | .wrapping_add(ch) |
| 185 | .wrapping_add(K[i]) |
| 186 | .wrapping_add(w[i]); |
| 187 | let s0 = a.rotate_right(2) ^ a.rotate_right(13) ^ a.rotate_right(22); |
| 188 | let maj = (a & b) ^ (a & c) ^ (b & c); |
| 189 | let temp2 = s0.wrapping_add(maj); |
| 190 | |
| 191 | h = g; |
| 192 | g = f; |
| 193 | f = e; |
| 194 | e = d.wrapping_add(temp1); |
| 195 | d = c; |
| 196 | c = b; |
| 197 | b = a; |
| 198 | a = temp1.wrapping_add(temp2); |
| 199 | } |
| 200 | |
| 201 | state[0] = state[0].wrapping_add(a); |
| 202 | state[1] = state[1].wrapping_add(b); |
| 203 | state[2] = state[2].wrapping_add(c); |
| 204 | state[3] = state[3].wrapping_add(d); |
| 205 | state[4] = state[4].wrapping_add(e); |
| 206 | state[5] = state[5].wrapping_add(f); |
| 207 | state[6] = state[6].wrapping_add(g); |
| 208 | state[7] = state[7].wrapping_add(h); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// Compute the SHA-256 digest of arbitrary byte data. |
| 213 | pub fn hash(data: &[u8]) -> Digest { |
| 214 | let mut hasher = Sha256::new(); |
| 215 | hasher.update(data); |
| 216 | hasher.finalize() |
| 217 | } |
| 218 | |
| 219 | /// HMAC-SHA256 for a 32-byte secret and a sequence of message slices. |
| 220 | pub fn hmac_sha256(key: &[u8; 32], parts: &[&[u8]]) -> Digest { |
| 221 | let mut inner_pad = [0x36u8; 64]; |
| 222 | let mut outer_pad = [0x5cu8; 64]; |
| 223 | for (index, byte) in key.iter().enumerate() { |
| 224 | inner_pad[index] ^= byte; |
| 225 | outer_pad[index] ^= byte; |
| 226 | } |
| 227 | |
| 228 | let mut inner = Sha256::new(); |
| 229 | inner.update(&inner_pad); |
| 230 | for part in parts { |
| 231 | inner.update(part); |
| 232 | } |
| 233 | let inner_digest = inner.finalize(); |
| 234 | |
| 235 | let mut outer = Sha256::new(); |
| 236 | outer.update(&outer_pad); |
| 237 | outer.update(&inner_digest); |
| 238 | outer.finalize() |
| 239 | } |
| 240 | |
| 241 | #[cfg(test)] |
| 242 | mod tests { |
| 243 | use super::*; |
| 244 | |
| 245 | #[test] |
| 246 | fn test_hmac_sha256_rfc4231_case_1() { |
| 247 | let mut key = [0u8; 32]; |
| 248 | key[..20].fill(0x0b); |
| 249 | assert_eq!( |
| 250 | hmac_sha256(&key, &[b"Hi ", b"There"]), |
| 251 | [ |
| 252 | 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, |
| 253 | 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, 0x26, 0xe9, 0x37, 0x6c, |
| 254 | 0x2e, 0x32, 0xcf, 0xf7, |
| 255 | ] |
| 256 | ); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn test_sha256_empty() { |
| 261 | // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 |
| 262 | let res = hash(b""); |
| 263 | assert_eq!( |
| 264 | res, |
| 265 | [ |
| 266 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, |
| 267 | 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, |
| 268 | 0x78, 0x52, 0xb8, 0x55, |
| 269 | ] |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn test_sha256_abc() { |
| 275 | // ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad |
| 276 | let res = hash(b"abc"); |
| 277 | assert_eq!( |
| 278 | res, |
| 279 | [ |
| 280 | 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, |
| 281 | 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, |
| 282 | 0xf2, 0x00, 0x15, 0xad, |
| 283 | ] |
| 284 | ); |
| 285 | } |
| 286 | } |