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-artifact/src/receipt.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-artifact / src / receipt.rs` 512 lines · 17585 bytes Raw Blame History 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 //! Admission Receipt v0 (ADR 0014 §7): a 512-byte canonical record of one 17 //! candidate's verification, admission and execution outcome. 18 //! 19 //! The in-memory [`Receipt`] is a view only; the contract is the byte layout 20 //! produced by [`encode`]. The kernel emits unsigned records (signature block 21 //! all zero); only host qualification tooling signs them (§7.3). 22 23 use aienos_crypto::sha256::{hash, Digest, Sha256}; 24 25 use crate::error::ArtifactError; 26 use crate::format::SIGNATURE_ALGORITHM_ED25519; 27 use crate::signature::{ReceiptSigner, SignatureVerifier, PUBLIC_KEY_SIZE, SIGNATURE_SIZE}; 28 29 pub const RECEIPT_MAGIC: &[u8; 8] = b"AIENRCP\0"; 30 pub const RECEIPT_VERSION: u16 = 0; 31 pub const RECEIPT_HEADER_SIZE: usize = 96; 32 pub const RECEIPT_SIZE: usize = 512; 33 pub const RECEIPT_SIGNATURE_OFFSET: usize = 400; 34 pub const RECEIPT_SIGNATURE_DOMAIN: &[u8] = b"AIENOS-ADMISSION-RECEIPT-SIGNATURE-V1\0"; 35 pub const RECEIPT_DIGEST_DOMAIN: &[u8] = b"AIENOS-ADMISSION-RECEIPT-V1\0"; 36 pub const RECEIPT_NONCE_DOMAIN: &[u8] = b"AIENOS-ADMISSION-RECEIPT-NONCE-V1\0"; 37 38 /// Presence flags (offset 16). 39 pub const FLAG_MACHINE_ID: u32 = 1 << 0; 40 pub const FLAG_CONTEXT_ID: u32 = 1 << 1; 41 pub const FLAG_TIMESTAMP: u32 = 1 << 2; 42 pub const VALID_FLAGS: u32 = FLAG_MACHINE_ID | FLAG_CONTEXT_ID | FLAG_TIMESTAMP; 43 44 /// Result flags (offset 72). 45 pub const RESULT_READ_OK: u32 = 1 << 0; 46 pub const RESULT_WRITE_DENIED: u32 = 1 << 1; 47 pub const RESULT_RECLAIMED: u32 = 1 << 2; 48 pub const RESULT_CANARY_PASSED: u32 = 1 << 3; 49 pub const RESULT_FORGED_DENIED: u32 = 1 << 4; 50 pub const RESULT_MAPPED_BYTES_MATCH: u32 = 1 << 5; 51 pub const RESULT_EXECUTED_BYTES_MATCH: u32 = 1 << 6; 52 pub const RESULT_WX_SEALED: u32 = 1 << 7; 53 pub const VALID_RESULT_FLAGS: u32 = 0xff; 54 55 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 56 #[repr(u16)] 57 pub enum ReceiptDecision { 58 Admitted = 1, 59 Rejected = 2, 60 CanaryFailed = 3, 61 Destroyed = 4, 62 } 63 64 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 65 #[repr(u16)] 66 pub enum QualificationTier { 67 Seed0bQemu = 1, 68 Seed0bMachine1 = 2, 69 Seed0bPi5 = 3, 70 } 71 72 /// Offset 64; §7.3 mapping of the task runtime outcome. 73 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 74 #[repr(u32)] 75 pub enum ExecutionStatusCode { 76 NotRun = 0, 77 Exited = 1, 78 Timeout = 2, 79 Fault = 3, 80 BadSyscall = 4, 81 ResourceOverrun = 5, 82 CanaryFailed = 6, 83 } 84 85 /// In-memory view of one receipt. Encoding is explicit and field by field. 86 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 87 pub struct Receipt { 88 pub flags: u32, 89 pub decision: ReceiptDecision, 90 pub tier: QualificationTier, 91 pub sequence: u64, 92 pub nonce: [u8; 16], 93 pub observed_time_ns: u64, 94 pub execution_status: ExecutionStatusCode, 95 pub exit_status: i32, 96 pub result_flags: u32, 97 pub rejection_stage: u16, 98 pub rejection_reason: u16, 99 pub syscalls: u32, 100 pub object_reads_ok: u32, 101 pub denials: u32, 102 pub frames_reserved: u32, 103 pub artifact_id: Digest, 104 pub payload_digest: Digest, 105 pub artifact_signer_fingerprint: Digest, 106 pub policy_digest: Digest, 107 pub requested_capability_digest: Digest, 108 pub granted_capability_digest: Digest, 109 pub resource_envelope_digest: Digest, 110 pub verifier_identity: Digest, 111 pub machine_id_digest: Digest, 112 pub generation: u64, 113 pub context_id: u64, 114 } 115 116 /// Canonical 512-byte record with an all-zero signature block. 117 pub fn encode(r: &Receipt) -> [u8; RECEIPT_SIZE] { 118 let mut b = [0u8; RECEIPT_SIZE]; 119 b[0..8].copy_from_slice(RECEIPT_MAGIC); 120 put16(&mut b, 8, RECEIPT_VERSION); 121 put16(&mut b, 10, RECEIPT_HEADER_SIZE as u16); 122 put32(&mut b, 12, RECEIPT_SIZE as u32); 123 put32(&mut b, 16, r.flags); 124 put16(&mut b, 20, r.decision as u16); 125 put16(&mut b, 22, r.tier as u16); 126 put64(&mut b, 24, r.sequence); 127 b[32..48].copy_from_slice(&r.nonce); 128 put64(&mut b, 48, r.observed_time_ns); 129 put32(&mut b, 64, r.execution_status as u32); 130 b[68..72].copy_from_slice(&r.exit_status.to_le_bytes()); 131 put32(&mut b, 72, r.result_flags); 132 put16(&mut b, 76, r.rejection_stage); 133 put16(&mut b, 78, r.rejection_reason); 134 put32(&mut b, 80, r.syscalls); 135 put32(&mut b, 84, r.object_reads_ok); 136 put32(&mut b, 88, r.denials); 137 put32(&mut b, 92, r.frames_reserved); 138 for (offset, digest) in [ 139 (96, &r.artifact_id), 140 (128, &r.payload_digest), 141 (160, &r.artifact_signer_fingerprint), 142 (192, &r.policy_digest), 143 (224, &r.requested_capability_digest), 144 (256, &r.granted_capability_digest), 145 (288, &r.resource_envelope_digest), 146 (320, &r.verifier_identity), 147 (352, &r.machine_id_digest), 148 ] { 149 b[offset..offset + 32].copy_from_slice(digest); 150 } 151 put64(&mut b, 384, r.generation); 152 put64(&mut b, 392, r.context_id); 153 b 154 } 155 156 /// `SHA256("AIENOS-ADMISSION-RECEIPT-V1\0" || bytes[0..400])`. 157 pub fn receipt_digest(bytes: &[u8; RECEIPT_SIZE]) -> Digest { 158 let mut h = Sha256::new(); 159 h.update(RECEIPT_DIGEST_DOMAIN); 160 h.update(&bytes[..RECEIPT_SIGNATURE_OFFSET]); 161 h.finalize() 162 } 163 164 /// Deterministic per-context nonce (§7.3). 165 pub fn receipt_nonce(verifier_identity: &Digest, sequence: u64) -> [u8; 16] { 166 let mut h = Sha256::new(); 167 h.update(RECEIPT_NONCE_DOMAIN); 168 h.update(verifier_identity); 169 h.update(&sequence.to_le_bytes()); 170 let d = h.finalize(); 171 let mut nonce = [0u8; 16]; 172 nonce.copy_from_slice(&d[..16]); 173 nonce 174 } 175 176 /// Canonical verifier build identity bytes (§7.1): 40 lowercase hex commit 177 /// characters, target u16 LE, ABI u16 LE, qualification feature id. 178 /// Returns `None` unless `commit` is exactly 40 lowercase hex digits. 179 pub fn verifier_identity_bytes( 180 commit: &[u8], 181 target: u16, 182 abi: u16, 183 feature: u8, 184 ) -> Option<[u8; 45]> { 185 if commit.len() != 40 186 || !commit 187 .iter() 188 .all(|c| matches!(c, b'0'..=b'9' | b'a'..=b'f')) 189 { 190 return None; 191 } 192 let mut out = [0u8; 45]; 193 out[..40].copy_from_slice(commit); 194 out[40..42].copy_from_slice(&target.to_le_bytes()); 195 out[42..44].copy_from_slice(&abi.to_le_bytes()); 196 out[44] = feature; 197 Some(out) 198 } 199 200 /// Stage codes (§7.3). 201 pub const MAX_REJECTION_STAGE: u16 = 9; 202 /// Loader reason codes (§7.3), after the `ArtifactError` range 1..=19. 203 pub const LOADER_REASON_FIRST: u16 = 0x101; 204 pub const LOADER_REASON_LAST: u16 = 0x10a; 205 206 impl ReceiptDecision { 207 pub const fn from_u16(v: u16) -> Option { 208 match v { 209 1 => Some(Self::Admitted), 210 2 => Some(Self::Rejected), 211 3 => Some(Self::CanaryFailed), 212 4 => Some(Self::Destroyed), 213 _ => None, 214 } 215 } 216 } 217 218 impl QualificationTier { 219 pub const fn from_u16(v: u16) -> Option { 220 match v { 221 1 => Some(Self::Seed0bQemu), 222 2 => Some(Self::Seed0bMachine1), 223 3 => Some(Self::Seed0bPi5), 224 _ => None, 225 } 226 } 227 228 pub const fn label(self) -> &'static str { 229 match self { 230 Self::Seed0bQemu => "SEED-0B-QEMU", 231 Self::Seed0bMachine1 => "SEED-0B-MACHINE1", 232 Self::Seed0bPi5 => "SEED-0B-PI5", 233 } 234 } 235 } 236 237 impl ExecutionStatusCode { 238 pub const fn from_u32(v: u32) -> Option { 239 match v { 240 0 => Some(Self::NotRun), 241 1 => Some(Self::Exited), 242 2 => Some(Self::Timeout), 243 3 => Some(Self::Fault), 244 4 => Some(Self::BadSyscall), 245 5 => Some(Self::ResourceOverrun), 246 6 => Some(Self::CanaryFailed), 247 _ => None, 248 } 249 } 250 } 251 252 const fn valid_reason(code: u16) -> bool { 253 matches!(code, 1..=19) || (code >= LOADER_REASON_FIRST && code <= LOADER_REASON_LAST) 254 } 255 256 /// Strictly decode one record (signed or unsigned). Error mapping: 257 /// short → `Truncated`, long → `WrongLength`, magic → `BadMagic`, 258 /// version/header/record size → `UnsupportedVersion`, unknown presence or 259 /// result bits → `UnsupportedFlags`, reserved bytes → `ReservedNonZero`, 260 /// signature algorithm → `BadSignatureFormat`, and every enum, code, 261 /// presence, consistency or nonce violation → `MalformedReceipt`. 262 pub fn decode(bytes: &[u8]) -> Result { 263 if bytes.len() < RECEIPT_SIZE { 264 return Err(ArtifactError::Truncated); 265 } 266 if bytes.len() > RECEIPT_SIZE { 267 return Err(ArtifactError::WrongLength); 268 } 269 if &bytes[0..8] != RECEIPT_MAGIC { 270 return Err(ArtifactError::BadMagic); 271 } 272 if get16(bytes, 8) != RECEIPT_VERSION 273 || usize::from(get16(bytes, 10)) != RECEIPT_HEADER_SIZE 274 || get32(bytes, 12) as usize != RECEIPT_SIZE 275 { 276 return Err(ArtifactError::UnsupportedVersion); 277 } 278 let flags = get32(bytes, 16); 279 let result_flags = get32(bytes, 72); 280 if flags & !VALID_FLAGS != 0 || result_flags & !VALID_RESULT_FLAGS != 0 { 281 return Err(ArtifactError::UnsupportedFlags); 282 } 283 if bytes[56..64].iter().any(|b| *b != 0) || bytes[500..512].iter().any(|b| *b != 0) { 284 return Err(ArtifactError::ReservedNonZero); 285 } 286 if bytes[400..500].iter().any(|b| *b != 0) { 287 if get16(bytes, 400) != SIGNATURE_ALGORITHM_ED25519 { 288 return Err(ArtifactError::BadSignatureFormat); 289 } 290 if bytes[402..404].iter().any(|b| *b != 0) { 291 return Err(ArtifactError::ReservedNonZero); 292 } 293 } 294 let malformed = ArtifactError::MalformedReceipt; 295 let decision = ReceiptDecision::from_u16(get16(bytes, 20)).ok_or(malformed)?; 296 let tier = QualificationTier::from_u16(get16(bytes, 22)).ok_or(malformed)?; 297 let execution_status = ExecutionStatusCode::from_u32(get32(bytes, 64)).ok_or(malformed)?; 298 let mut nonce = [0u8; 16]; 299 nonce.copy_from_slice(&bytes[32..48]); 300 let digest_at = |at: usize| { 301 let mut d = [0u8; 32]; 302 d.copy_from_slice(&bytes[at..at + 32]); 303 d 304 }; 305 let r = Receipt { 306 flags, 307 decision, 308 tier, 309 sequence: get64(bytes, 24), 310 nonce, 311 observed_time_ns: get64(bytes, 48), 312 execution_status, 313 exit_status: i32::from_le_bytes([bytes[68], bytes[69], bytes[70], bytes[71]]), 314 result_flags, 315 rejection_stage: get16(bytes, 76), 316 rejection_reason: get16(bytes, 78), 317 syscalls: get32(bytes, 80), 318 object_reads_ok: get32(bytes, 84), 319 denials: get32(bytes, 88), 320 frames_reserved: get32(bytes, 92), 321 artifact_id: digest_at(96), 322 payload_digest: digest_at(128), 323 artifact_signer_fingerprint: digest_at(160), 324 policy_digest: digest_at(192), 325 requested_capability_digest: digest_at(224), 326 granted_capability_digest: digest_at(256), 327 resource_envelope_digest: digest_at(288), 328 verifier_identity: digest_at(320), 329 machine_id_digest: digest_at(352), 330 generation: get64(bytes, 384), 331 context_id: get64(bytes, 392), 332 }; 333 validate(&r)?; 334 Ok(r) 335 } 336 337 /// §7.1/§7.3 semantic rules shared by `decode` and producers. 338 pub fn validate(r: &Receipt) -> Result<(), ArtifactError> { 339 let bad = Err(ArtifactError::MalformedReceipt); 340 if r.flags & FLAG_MACHINE_ID == 0 && r.machine_id_digest != [0; 32] { 341 return bad; 342 } 343 if r.flags & FLAG_CONTEXT_ID == 0 && (r.generation != 0 || r.context_id != 0) { 344 return bad; 345 } 346 if r.flags & FLAG_TIMESTAMP == 0 && r.observed_time_ns != 0 { 347 return bad; 348 } 349 if r.rejection_stage > MAX_REJECTION_STAGE 350 || (r.rejection_reason != 0 && !valid_reason(r.rejection_reason)) 351 { 352 return bad; 353 } 354 match r.decision { 355 ReceiptDecision::Rejected => { 356 if r.rejection_stage == 0 357 || r.rejection_reason == 0 358 || r.execution_status != ExecutionStatusCode::NotRun 359 || r.exit_status != 0 360 || r.syscalls != 0 361 || r.object_reads_ok != 0 362 || r.denials != 0 363 || r.result_flags & !RESULT_RECLAIMED != 0 364 { 365 return bad; 366 } 367 } 368 _ => { 369 if r.rejection_stage != 0 || r.rejection_reason != 0 { 370 return bad; 371 } 372 } 373 } 374 // A CanaryFailed decision and a CanaryFailed execution status imply each 375 // other (FORMAL-4 discrepancy): neither may appear without the other. 376 if (r.decision == ReceiptDecision::CanaryFailed) 377 != (r.execution_status == ExecutionStatusCode::CanaryFailed) 378 { 379 return bad; 380 } 381 if r.result_flags & RESULT_CANARY_PASSED != 0 382 && (r.execution_status != ExecutionStatusCode::Exited || r.exit_status != 0) 383 { 384 return bad; 385 } 386 if r.nonce != receipt_nonce(&r.verifier_identity, r.sequence) { 387 return bad; 388 } 389 Ok(()) 390 } 391 392 /// True when bytes 400..512 are all zero (kernel-emitted record). 393 pub fn is_unsigned(bytes: &[u8; RECEIPT_SIZE]) -> bool { 394 bytes[RECEIPT_SIGNATURE_OFFSET..].iter().all(|b| *b == 0) 395 } 396 397 /// `RECEIPT_SIGNATURE_DOMAIN || ReceiptDigest`. 398 pub fn signature_message(digest: &Digest) -> [u8; RECEIPT_SIGNATURE_DOMAIN.len() + 32] { 399 let mut m = [0u8; RECEIPT_SIGNATURE_DOMAIN.len() + 32]; 400 m[..RECEIPT_SIGNATURE_DOMAIN.len()].copy_from_slice(RECEIPT_SIGNATURE_DOMAIN); 401 m[RECEIPT_SIGNATURE_DOMAIN.len()..].copy_from_slice(digest); 402 m 403 } 404 405 /// Sign a valid unsigned record in place. 406 pub fn sign( 407 bytes: &mut [u8; RECEIPT_SIZE], 408 signer: &impl ReceiptSigner, 409 ) -> Result<(), ArtifactError> { 410 if !is_unsigned(bytes) { 411 return Err(ArtifactError::BadSignatureFormat); 412 } 413 decode(bytes)?; 414 let digest = receipt_digest(bytes); 415 let signature = signer.sign_receipt_digest(&digest)?; 416 put16(bytes, 400, SIGNATURE_ALGORITHM_ED25519); 417 bytes[404..436].copy_from_slice(&signer.signer_fingerprint()); 418 bytes[436..500].copy_from_slice(&signature); 419 Ok(()) 420 } 421 422 /// Receipt signing keys the verifier trusts. Distinct from artifact anchors. 423 pub trait ReceiptAnchorSet { 424 fn find(&self, fingerprint: &Digest) -> Option<[u8; PUBLIC_KEY_SIZE]>; 425 } 426 427 /// Production receipt anchors are empty until M5 and fail closed. 428 #[derive(Clone, Copy, Debug, Default)] 429 pub struct EmptyReceiptAnchorSet; 430 431 impl ReceiptAnchorSet for EmptyReceiptAnchorSet { 432 fn find(&self, _fingerprint: &Digest) -> Option<[u8; PUBLIC_KEY_SIZE]> { 433 None 434 } 435 } 436 437 /// Decode and authenticate a signed record. 438 pub fn verify( 439 bytes: &[u8], 440 anchors: &impl ReceiptAnchorSet, 441 signatures: &impl SignatureVerifier, 442 ) -> Result { 443 let receipt = decode(bytes)?; 444 let mut record = [0u8; RECEIPT_SIZE]; 445 record.copy_from_slice(bytes); 446 if is_unsigned(&record) { 447 return Err(ArtifactError::BadSignatureFormat); 448 } 449 let mut fingerprint = [0u8; 32]; 450 fingerprint.copy_from_slice(&record[404..436]); 451 let key = anchors 452 .find(&fingerprint) 453 .ok_or(ArtifactError::UntrustedSigner)?; 454 if hash(&key) != fingerprint { 455 return Err(ArtifactError::UntrustedSigner); 456 } 457 let mut signature = [0u8; SIGNATURE_SIZE]; 458 signature.copy_from_slice(&record[436..500]); 459 let message = signature_message(&receipt_digest(&record)); 460 if !signatures.verify(&key, &message, &signature) { 461 return Err(ArtifactError::BadSignature); 462 } 463 Ok(receipt) 464 } 465 466 /// TEST-ONLY SEED-0B receipt signing identity: RFC 8032 TEST 2 public key, 467 /// distinct from the artifact test key (TEST 1). Its seed exists only in 468 /// debug host qualification tooling and tests. 469 #[cfg(feature = "seed0b-test-anchor")] 470 pub mod seed0b_test_receipt_anchor { 471 use super::ReceiptAnchorSet; 472 use aienos_crypto::sha256::{hash, Digest}; 473 474 pub const LABEL: &str = "SEED-0B QUALIFICATION RECEIPT KEY — TEST ONLY"; 475 476 pub const PUBLIC_KEY: [u8; 32] = [ 477 0x3d, 0x40, 0x17, 0xc3, 0xe8, 0x43, 0x89, 0x5a, 0x92, 0xb7, 0x0a, 0xa7, 0x4d, 0x1b, 0x7e, 478 0xbc, 0x9c, 0x98, 0x2c, 0xcf, 0x2e, 0xc4, 0x96, 0x8c, 0xc0, 0xcd, 0x55, 0xf1, 0x2a, 0xf4, 479 0x66, 0x0c, 480 ]; 481 482 #[derive(Clone, Copy, Debug, Default)] 483 pub struct Seed0bTestReceiptAnchorSet; 484 485 impl ReceiptAnchorSet for Seed0bTestReceiptAnchorSet { 486 fn find(&self, fingerprint: &Digest) -> Option<[u8; 32]> { 487 (*fingerprint == hash(&PUBLIC_KEY)).then_some(PUBLIC_KEY) 488 } 489 } 490 } 491 492 fn get16(b: &[u8], at: usize) -> u16 { 493 u16::from_le_bytes([b[at], b[at + 1]]) 494 } 495 fn get32(b: &[u8], at: usize) -> u32 { 496 u32::from_le_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]) 497 } 498 fn get64(b: &[u8], at: usize) -> u64 { 499 let mut v = [0u8; 8]; 500 v.copy_from_slice(&b[at..at + 8]); 501 u64::from_le_bytes(v) 502 } 503 504 fn put16(b: &mut [u8], at: usize, v: u16) { 505 b[at..at + 2].copy_from_slice(&v.to_le_bytes()); 506 } 507 fn put32(b: &mut [u8], at: usize, v: u32) { 508 b[at..at + 4].copy_from_slice(&v.to_le_bytes()); 509 } 510 fn put64(b: &mut [u8], at: usize, v: u64) { 511 b[at..at + 8].copy_from_slice(&v.to_le_bytes()); 512 }