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-crypto/src/lib.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-crypto / src / lib.rs` 82 lines ยท 2602 bytes Raw Blame History 1 #![no_std] 2 // SPDX-License-Identifier: Apache-2.0 3 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 //! Shared, allocation-free cryptographic primitives used by the kernel and 18 //! host/bare-metal parsers. 19 //! 20 //! # Side-channel stance 21 //! 22 //! AES-256, POLYVAL and AES-256-GCM-SIV are written to be constant-time by 23 //! construction: no table lookup is indexed by secret data and no branch 24 //! depends on secret data. Secret key material and scratch buffers are wiped 25 //! with volatile writes when they go out of scope. This has been checked by 26 //! reading the source and spot-checking AArch64 disassembly; it has not been 27 //! measured on hardware and has not had an independent side-channel review. 28 //! See `docs/CRYPTO_CONSTANT_TIME.md`. 29 30 #[cfg(test)] 31 extern crate std; 32 33 pub mod aes; 34 pub mod aes_gcm_siv; 35 pub mod polyval; 36 pub mod sha256; 37 38 /// Overwrite `buf` with zeros using volatile writes the optimizer cannot drop. 39 #[inline(never)] 40 pub(crate) fn wipe(buf: &mut [u8]) { 41 for b in buf.iter_mut() { 42 // SAFETY: `b` is a valid, aligned, exclusive reference into `buf`. 43 unsafe { core::ptr::write_volatile(b, 0) }; 44 } 45 core::sync::atomic::compiler_fence(core::sync::atomic::Ordering::SeqCst); 46 } 47 48 /// Fixed-size secret byte buffer that is wiped on drop, on every exit path. 49 pub struct Secret(pub(crate) [u8; N]); 50 51 impl core::ops::Deref for Secret { 52 type Target = [u8; N]; 53 54 fn deref(&self) -> &Self::Target { 55 &self.0 56 } 57 } 58 59 impl core::ops::DerefMut for Secret { 60 fn deref_mut(&mut self) -> &mut Self::Target { 61 &mut self.0 62 } 63 } 64 65 impl core::fmt::Debug for Secret { 66 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 67 f.write_str("Secret([REDACTED])") 68 } 69 } 70 71 impl Secret { 72 #[inline(always)] 73 pub(crate) fn zeroed() -> Self { 74 Self([0u8; N]) 75 } 76 } 77 78 impl Drop for Secret { 79 fn drop(&mut self) { 80 wipe(&mut self.0); 81 } 82 }