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-artifact / src / signature.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 | //! Binary Artifact v0 signature-block, trust, and verification interfaces. |
| 17 | |
| 18 | use crate::error::ArtifactError; |
| 19 | use crate::format::SIGNATURE_ALGORITHM_ED25519; |
| 20 | use crate::id::{compute_artifact_id, ArtifactId}; |
| 21 | use crate::verify::{parse_and_identify, IdentifiedArtifact}; |
| 22 | use aienos_crypto::sha256::{hash, Digest}; |
| 23 | use ed25519_dalek::{Signature, VerifyingKey}; |
| 24 | |
| 25 | pub const SIGNATURE_DOMAIN: &[u8] = b"AIENOS-ARTIFACT-SIGNATURE-V1\0"; |
| 26 | pub const SIGNATURE_BLOCK_SIZE: usize = 100; |
| 27 | pub const SIGNATURE_SIZE: usize = 64; |
| 28 | pub const PUBLIC_KEY_SIZE: usize = 32; |
| 29 | pub const ARTIFACT_SIGNATURE_MESSAGE_SIZE: usize = SIGNATURE_DOMAIN.len() + 32; |
| 30 | |
| 31 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 32 | pub enum TrustTier { |
| 33 | Production, |
| 34 | Seed0bQualification, |
| 35 | } |
| 36 | |
| 37 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 38 | pub struct TrustedSigner { |
| 39 | pub public_key: [u8; PUBLIC_KEY_SIZE], |
| 40 | pub tier: TrustTier, |
| 41 | } |
| 42 | |
| 43 | pub trait TrustAnchorSet { |
| 44 | fn find(&self, fingerprint: &Digest) -> Option<TrustedSigner>; |
| 45 | } |
| 46 | |
| 47 | /// Production trust remains fail-closed until the owner identity ceremony. |
| 48 | #[derive(Clone, Copy, Debug, Default)] |
| 49 | pub struct EmptyTrustAnchorSet; |
| 50 | |
| 51 | /// The ordinary production set intentionally contains no anchors before M5. |
| 52 | pub type ProductionTrustAnchorSet = EmptyTrustAnchorSet; |
| 53 | |
| 54 | impl TrustAnchorSet for EmptyTrustAnchorSet { |
| 55 | fn find(&self, _fingerprint: &Digest) -> Option<TrustedSigner> { |
| 56 | None |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | pub trait SignatureVerifier { |
| 61 | fn verify( |
| 62 | &self, |
| 63 | public_key: &[u8; PUBLIC_KEY_SIZE], |
| 64 | message: &[u8], |
| 65 | signature: &[u8; SIGNATURE_SIZE], |
| 66 | ) -> bool; |
| 67 | } |
| 68 | |
| 69 | /// Signs a domain-separated admission-receipt digest. Implementations own |
| 70 | /// their key custody; the kernel-facing artifact crate stores no private key. |
| 71 | pub trait ReceiptSigner { |
| 72 | fn signer_fingerprint(&self) -> Digest; |
| 73 | fn sign_receipt_digest(&self, digest: &Digest) -> Result<[u8; SIGNATURE_SIZE], ArtifactError>; |
| 74 | } |
| 75 | |
| 76 | #[derive(Clone, Copy, Debug, Default)] |
| 77 | pub struct Ed25519Verifier; |
| 78 | |
| 79 | impl SignatureVerifier for Ed25519Verifier { |
| 80 | fn verify( |
| 81 | &self, |
| 82 | public_key: &[u8; PUBLIC_KEY_SIZE], |
| 83 | message: &[u8], |
| 84 | signature: &[u8; SIGNATURE_SIZE], |
| 85 | ) -> bool { |
| 86 | let Ok(key) = VerifyingKey::from_bytes(public_key) else { |
| 87 | return false; |
| 88 | }; |
| 89 | key.verify_strict(message, &Signature::from_bytes(signature)) |
| 90 | .is_ok() |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 95 | pub struct VerifiedArtifact<'a> { |
| 96 | identified: IdentifiedArtifact<'a>, |
| 97 | signer_fingerprint: Digest, |
| 98 | trust_tier: TrustTier, |
| 99 | } |
| 100 | |
| 101 | impl<'a> VerifiedArtifact<'a> { |
| 102 | pub const fn identified(&self) -> &IdentifiedArtifact<'a> { |
| 103 | &self.identified |
| 104 | } |
| 105 | |
| 106 | pub const fn signer_fingerprint(&self) -> &Digest { |
| 107 | &self.signer_fingerprint |
| 108 | } |
| 109 | |
| 110 | pub const fn trust_tier(&self) -> TrustTier { |
| 111 | self.trust_tier |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub trait ArtifactVerifier { |
| 116 | fn verify<'a>(&self, bytes: &'a [u8]) -> Result<VerifiedArtifact<'a>, ArtifactError>; |
| 117 | } |
| 118 | |
| 119 | pub struct ConfiguredArtifactVerifier<'a, A, S> { |
| 120 | anchors: &'a A, |
| 121 | signatures: S, |
| 122 | } |
| 123 | |
| 124 | impl<'a, A, S> ConfiguredArtifactVerifier<'a, A, S> { |
| 125 | pub const fn new(anchors: &'a A, signatures: S) -> Self { |
| 126 | Self { |
| 127 | anchors, |
| 128 | signatures, |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | impl<A: TrustAnchorSet, S: SignatureVerifier> ArtifactVerifier |
| 134 | for ConfiguredArtifactVerifier<'_, A, S> |
| 135 | { |
| 136 | fn verify<'b>(&self, bytes: &'b [u8]) -> Result<VerifiedArtifact<'b>, ArtifactError> { |
| 137 | let identified = parse_and_identify(bytes)?; |
| 138 | let fingerprint = *identified.artifact.signer_fingerprint; |
| 139 | let signer = self |
| 140 | .anchors |
| 141 | .find(&fingerprint) |
| 142 | .ok_or(ArtifactError::UntrustedSigner)?; |
| 143 | if hash(&signer.public_key) != fingerprint { |
| 144 | return Err(ArtifactError::UntrustedSigner); |
| 145 | } |
| 146 | |
| 147 | let mut message = [0u8; ARTIFACT_SIGNATURE_MESSAGE_SIZE]; |
| 148 | message[..SIGNATURE_DOMAIN.len()].copy_from_slice(SIGNATURE_DOMAIN); |
| 149 | message[SIGNATURE_DOMAIN.len()..].copy_from_slice(identified.artifact_id.as_bytes()); |
| 150 | if !self |
| 151 | .signatures |
| 152 | .verify(&signer.public_key, &message, identified.artifact.signature) |
| 153 | { |
| 154 | return Err(ArtifactError::BadSignature); |
| 155 | } |
| 156 | Ok(VerifiedArtifact { |
| 157 | identified, |
| 158 | signer_fingerprint: fingerprint, |
| 159 | trust_tier: signer.tier, |
| 160 | }) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #[cfg(feature = "seed0b-test-anchor")] |
| 165 | pub mod seed0b_test_anchor { |
| 166 | use super::{TrustAnchorSet, TrustTier, TrustedSigner}; |
| 167 | use aienos_crypto::sha256::{hash, Digest}; |
| 168 | |
| 169 | pub const LABEL: &str = "SEED-0B QUALIFICATION BUILD — TEST ONLY TRUST ANCHOR"; |
| 170 | |
| 171 | // RFC 8032 test-vector public key. Its matching seed is for tests and |
| 172 | // qualification fixtures only; never use it for production identity. |
| 173 | pub const PUBLIC_KEY: [u8; 32] = [ |
| 174 | 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, |
| 175 | 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, |
| 176 | 0x51, 0x1a, |
| 177 | ]; |
| 178 | |
| 179 | #[derive(Clone, Copy, Debug, Default)] |
| 180 | pub struct Seed0bTestAnchorSet; |
| 181 | |
| 182 | impl TrustAnchorSet for Seed0bTestAnchorSet { |
| 183 | fn find(&self, fingerprint: &Digest) -> Option<TrustedSigner> { |
| 184 | let expected = hash(&PUBLIC_KEY); |
| 185 | (*fingerprint == expected).then_some(TrustedSigner { |
| 186 | public_key: PUBLIC_KEY, |
| 187 | tier: TrustTier::Seed0bQualification, |
| 188 | }) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 194 | pub struct SignatureBlock<'a> { |
| 195 | pub algorithm: u16, |
| 196 | pub signer_fingerprint: &'a [u8; 32], |
| 197 | pub signature: &'a [u8; SIGNATURE_SIZE], |
| 198 | } |
| 199 | |
| 200 | impl<'a> SignatureBlock<'a> { |
| 201 | pub fn decode(bytes: &'a [u8]) -> Result<Self, ArtifactError> { |
| 202 | if bytes.len() != SIGNATURE_BLOCK_SIZE { |
| 203 | return Err(if bytes.len() < SIGNATURE_BLOCK_SIZE { |
| 204 | ArtifactError::Truncated |
| 205 | } else { |
| 206 | ArtifactError::WrongLength |
| 207 | }); |
| 208 | } |
| 209 | let algorithm = u16::from_le_bytes([bytes[0], bytes[1]]); |
| 210 | if algorithm != SIGNATURE_ALGORITHM_ED25519 { |
| 211 | return Err(ArtifactError::BadSignatureFormat); |
| 212 | } |
| 213 | if bytes[2] != 0 || bytes[3] != 0 { |
| 214 | return Err(ArtifactError::ReservedNonZero); |
| 215 | } |
| 216 | let signer_fingerprint = bytes[4..36] |
| 217 | .try_into() |
| 218 | .map_err(|_| ArtifactError::Truncated)?; |
| 219 | let signature = bytes[36..100] |
| 220 | .try_into() |
| 221 | .map_err(|_| ArtifactError::Truncated)?; |
| 222 | Ok(Self { |
| 223 | algorithm, |
| 224 | signer_fingerprint, |
| 225 | signature, |
| 226 | }) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | pub fn artifact_signature_message( |
| 231 | artifact_id: &ArtifactId, |
| 232 | ) -> [u8; ARTIFACT_SIGNATURE_MESSAGE_SIZE] { |
| 233 | let mut message = [0u8; ARTIFACT_SIGNATURE_MESSAGE_SIZE]; |
| 234 | message[..SIGNATURE_DOMAIN.len()].copy_from_slice(SIGNATURE_DOMAIN); |
| 235 | message[SIGNATURE_DOMAIN.len()..].copy_from_slice(artifact_id.as_bytes()); |
| 236 | message |
| 237 | } |
| 238 | |
| 239 | pub fn signer_fingerprint(public_key: &[u8; PUBLIC_KEY_SIZE]) -> Digest { |
| 240 | hash(public_key) |
| 241 | } |
| 242 | |
| 243 | pub fn identify_unsigned_artifact(bytes: &[u8]) -> Result<ArtifactId, ArtifactError> { |
| 244 | let artifact = crate::format::Artifact::parse(bytes)?; |
| 245 | Ok(compute_artifact_id(&artifact)) |
| 246 | } |