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 / lib.rs
| 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<const N: usize>(pub(crate) [u8; N]); |
| 50 | |
| 51 | impl<const N: usize> core::ops::Deref for Secret<N> { |
| 52 | type Target = [u8; N]; |
| 53 | |
| 54 | fn deref(&self) -> &Self::Target { |
| 55 | &self.0 |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<const N: usize> core::ops::DerefMut for Secret<N> { |
| 60 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 61 | &mut self.0 |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl<const N: usize> core::fmt::Debug for Secret<N> { |
| 66 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 67 | f.write_str("Secret([REDACTED])") |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl<const N: usize> Secret<N> { |
| 72 | #[inline(always)] |
| 73 | pub(crate) fn zeroed() -> Self { |
| 74 | Self([0u8; N]) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl<const N: usize> Drop for Secret<N> { |
| 79 | fn drop(&mut self) { |
| 80 | wipe(&mut self.0); |
| 81 | } |
| 82 | } |