aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-security / src / secret.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 | //! Secret protection abstractions and provider contracts for AIEN. |
| 17 | |
| 18 | use serde::{Deserialize, Serialize}; |
| 19 | use std::collections::HashMap; |
| 20 | use std::sync::{Arc, RwLock}; |
| 21 | use thiserror::Error; |
| 22 | use zeroize::Zeroizing; |
| 23 | |
| 24 | /// Classification tiers for cryptographic secret protection at rest. |
| 25 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 26 | pub enum SecretProtectionLevel { |
| 27 | /// Private key material generated within hardware silicon that cannot be extracted. |
| 28 | HardwareBackedNonExportable, |
| 29 | /// Secrets encrypted at rest using keys bound to platform hardware state or TPM 2.0. |
| 30 | HardwareBackedSealed, |
| 31 | /// Secrets protected by operating system credential facilities. |
| 32 | OsProtected, |
| 33 | /// Ephemeral in-process memory store, wiped on process exit. |
| 34 | MemoryOnly, |
| 35 | } |
| 36 | |
| 37 | /// Operational capabilities exposed by a SecretProvider implementation. |
| 38 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 39 | pub struct SecretCapabilities { |
| 40 | pub protection_level: SecretProtectionLevel, |
| 41 | pub can_seal: bool, |
| 42 | pub can_unseal: bool, |
| 43 | pub hardware_bound: bool, |
| 44 | pub persistent_at_rest: bool, |
| 45 | } |
| 46 | |
| 47 | /// An encrypted or sealed secret container safe for persistent storage or transit. |
| 48 | #[derive(Debug, Clone, Serialize, Deserialize)] |
| 49 | pub struct SealedSecret { |
| 50 | pub name: String, |
| 51 | pub ciphertext: Vec<u8>, |
| 52 | pub protection_level: SecretProtectionLevel, |
| 53 | pub metadata: HashMap<String, String>, |
| 54 | } |
| 55 | |
| 56 | /// Errors occurring during secret resolution, sealing, or unsealing. |
| 57 | #[derive(Debug, Error)] |
| 58 | pub enum SecretError { |
| 59 | #[error("Secret '{0}' was not found in provider")] |
| 60 | NotFound(String), |
| 61 | #[error("Hardware security provider unavailable: {0}")] |
| 62 | HardwareUnavailable(String), |
| 63 | #[error("Cryptographic operation failed: {0}")] |
| 64 | CryptoError(String), |
| 65 | #[error("Plaintext secret persistence is forbidden by security policy: {0}")] |
| 66 | PolicyViolation(String), |
| 67 | #[error("I/O failure during secret operation: {0}")] |
| 68 | IoError(String), |
| 69 | } |
| 70 | |
| 71 | /// Uniform contract for platform secret providers. |
| 72 | pub trait SecretProvider: Send + Sync { |
| 73 | /// Return capabilities of this provider. |
| 74 | fn capabilities(&self) -> SecretCapabilities; |
| 75 | |
| 76 | /// Return protection level guaranteed by this provider. |
| 77 | fn protection_level(&self) -> SecretProtectionLevel; |
| 78 | |
| 79 | /// Seal plaintext bytes into a protected container. |
| 80 | fn seal(&self, name: &str, secret: &[u8]) -> Result<SealedSecret, SecretError>; |
| 81 | |
| 82 | /// Unseal a protected container into zeroized process memory. |
| 83 | fn unseal(&self, secret: &SealedSecret) -> Result<Zeroizing<Vec<u8>>, SecretError>; |
| 84 | |
| 85 | /// Delete a secret entry by name. |
| 86 | fn delete(&self, name: &str) -> Result<(), SecretError>; |
| 87 | |
| 88 | /// Resolve a secret directly by name into zeroized process memory. |
| 89 | fn resolve(&self, name: &str) -> Result<Zeroizing<Vec<u8>>, SecretError>; |
| 90 | } |
| 91 | |
| 92 | /// In-memory secret provider for tests and ephemeral environments. |
| 93 | #[derive(Default, Clone)] |
| 94 | pub struct MemorySecretProvider { |
| 95 | store: Arc<RwLock<HashMap<String, Vec<u8>>>>, |
| 96 | } |
| 97 | |
| 98 | impl MemorySecretProvider { |
| 99 | pub fn new() -> Self { |
| 100 | Self { |
| 101 | store: Arc::new(RwLock::new(HashMap::new())), |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | impl SecretProvider for MemorySecretProvider { |
| 107 | fn capabilities(&self) -> SecretCapabilities { |
| 108 | SecretCapabilities { |
| 109 | protection_level: SecretProtectionLevel::MemoryOnly, |
| 110 | can_seal: true, |
| 111 | can_unseal: true, |
| 112 | hardware_bound: false, |
| 113 | persistent_at_rest: false, |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn protection_level(&self) -> SecretProtectionLevel { |
| 118 | SecretProtectionLevel::MemoryOnly |
| 119 | } |
| 120 | |
| 121 | fn seal(&self, name: &str, secret: &[u8]) -> Result<SealedSecret, SecretError> { |
| 122 | let mut guard = self |
| 123 | .store |
| 124 | .write() |
| 125 | .map_err(|e| SecretError::CryptoError(e.to_string()))?; |
| 126 | guard.insert(name.to_string(), secret.to_vec()); |
| 127 | Ok(SealedSecret { |
| 128 | name: name.to_string(), |
| 129 | ciphertext: secret.to_vec(), |
| 130 | protection_level: SecretProtectionLevel::MemoryOnly, |
| 131 | metadata: HashMap::new(), |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | fn unseal(&self, secret: &SealedSecret) -> Result<Zeroizing<Vec<u8>>, SecretError> { |
| 136 | let guard = self |
| 137 | .store |
| 138 | .read() |
| 139 | .map_err(|e| SecretError::CryptoError(e.to_string()))?; |
| 140 | match guard.get(&secret.name) { |
| 141 | Some(bytes) => Ok(Zeroizing::new(bytes.clone())), |
| 142 | None => Err(SecretError::NotFound(secret.name.clone())), |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | fn delete(&self, name: &str) -> Result<(), SecretError> { |
| 147 | let mut guard = self |
| 148 | .store |
| 149 | .write() |
| 150 | .map_err(|e| SecretError::CryptoError(e.to_string()))?; |
| 151 | guard.remove(name); |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | fn resolve(&self, name: &str) -> Result<Zeroizing<Vec<u8>>, SecretError> { |
| 156 | let guard = self |
| 157 | .store |
| 158 | .read() |
| 159 | .map_err(|e| SecretError::CryptoError(e.to_string()))?; |
| 160 | match guard.get(name) { |
| 161 | Some(bytes) => Ok(Zeroizing::new(bytes.clone())), |
| 162 | None => Err(SecretError::NotFound(name.to_string())), |
| 163 | } |
| 164 | } |
| 165 | } |