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 / receipt_tests.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 | //! Admission Receipt v0 tests: layout, golden bytes, strict decoding, |
| 17 | //! mutation and signature behaviour. |
| 18 | |
| 19 | use std::vec::Vec; |
| 20 | |
| 21 | use ed25519_dalek::{Signer, SigningKey}; |
| 22 | |
| 23 | use crate::error::ArtifactError; |
| 24 | use crate::receipt::*; |
| 25 | use crate::signature::{Ed25519Verifier, ReceiptSigner}; |
| 26 | use aienos_crypto::sha256::{hash, Digest}; |
| 27 | |
| 28 | /// RFC 8032 TEST 2 seed: TEST-ONLY receipt key for SEED-0B qualification. |
| 29 | const TEST2_SEED: [u8; 32] = [ |
| 30 | 0x4c, 0xcd, 0x08, 0x9b, 0x28, 0xff, 0x96, 0xda, 0x9d, 0xb6, 0xc3, 0x46, 0xec, 0x11, 0x4e, 0x0f, |
| 31 | 0x5b, 0x8a, 0x31, 0x9f, 0x35, 0xab, 0xa6, 0x24, 0xda, 0x8c, 0xf6, 0xed, 0x4f, 0xb8, 0xa6, 0xfb, |
| 32 | ]; |
| 33 | /// RFC 8032 TEST 3 seed: a key no receipt anchor trusts. |
| 34 | const TEST3_SEED: [u8; 32] = [ |
| 35 | 0xc5, 0xaa, 0x8d, 0xf4, 0x3f, 0x9f, 0x83, 0x7b, 0xed, 0xb7, 0x44, 0x2f, 0x31, 0xdc, 0xb7, 0xb1, |
| 36 | 0x66, 0xd3, 0x85, 0x35, 0x07, 0x6f, 0x09, 0x4b, 0x85, 0xce, 0x3a, 0x2e, 0x0b, 0x44, 0x58, 0xf7, |
| 37 | ]; |
| 38 | |
| 39 | struct KeySigner(SigningKey); |
| 40 | |
| 41 | impl ReceiptSigner for KeySigner { |
| 42 | fn signer_fingerprint(&self) -> Digest { |
| 43 | hash(&self.0.verifying_key().to_bytes()) |
| 44 | } |
| 45 | fn sign_receipt_digest(&self, digest: &Digest) -> Result<[u8; 64], ArtifactError> { |
| 46 | Ok(self.0.sign(&signature_message(digest)).to_bytes()) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | struct OneKey([u8; 32]); |
| 51 | |
| 52 | impl ReceiptAnchorSet for OneKey { |
| 53 | fn find(&self, fingerprint: &Digest) -> Option<[u8; 32]> { |
| 54 | (hash(&self.0) == *fingerprint).then_some(self.0) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn pattern(seed: u8) -> Digest { |
| 59 | let mut d = [0u8; 32]; |
| 60 | for (i, b) in d.iter_mut().enumerate() { |
| 61 | *b = seed.wrapping_add(i as u8); |
| 62 | } |
| 63 | d |
| 64 | } |
| 65 | |
| 66 | fn admitted() -> Receipt { |
| 67 | let verifier_identity = pattern(0x80); |
| 68 | Receipt { |
| 69 | flags: 0, |
| 70 | decision: ReceiptDecision::Admitted, |
| 71 | tier: QualificationTier::Seed0bQemu, |
| 72 | sequence: 1, |
| 73 | nonce: receipt_nonce(&verifier_identity, 1), |
| 74 | observed_time_ns: 0, |
| 75 | execution_status: ExecutionStatusCode::Exited, |
| 76 | exit_status: 0, |
| 77 | result_flags: RESULT_READ_OK |
| 78 | | RESULT_WRITE_DENIED |
| 79 | | RESULT_RECLAIMED |
| 80 | | RESULT_CANARY_PASSED |
| 81 | | RESULT_FORGED_DENIED |
| 82 | | RESULT_MAPPED_BYTES_MATCH |
| 83 | | RESULT_EXECUTED_BYTES_MATCH |
| 84 | | RESULT_WX_SEALED, |
| 85 | rejection_stage: 0, |
| 86 | rejection_reason: 0, |
| 87 | syscalls: 8, |
| 88 | object_reads_ok: 2, |
| 89 | denials: 5, |
| 90 | frames_reserved: 10, |
| 91 | artifact_id: pattern(0x10), |
| 92 | payload_digest: pattern(0x20), |
| 93 | artifact_signer_fingerprint: pattern(0x30), |
| 94 | policy_digest: pattern(0x40), |
| 95 | requested_capability_digest: pattern(0x50), |
| 96 | granted_capability_digest: pattern(0x60), |
| 97 | resource_envelope_digest: pattern(0x70), |
| 98 | verifier_identity, |
| 99 | machine_id_digest: [0; 32], |
| 100 | generation: 0, |
| 101 | context_id: 0, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn rejected() -> Receipt { |
| 106 | let verifier_identity = pattern(0x80); |
| 107 | Receipt { |
| 108 | decision: ReceiptDecision::Rejected, |
| 109 | sequence: 2, |
| 110 | nonce: receipt_nonce(&verifier_identity, 2), |
| 111 | execution_status: ExecutionStatusCode::NotRun, |
| 112 | exit_status: 0, |
| 113 | result_flags: RESULT_RECLAIMED, |
| 114 | rejection_stage: 3, |
| 115 | rejection_reason: ArtifactError::BadSignature as u16, |
| 116 | syscalls: 0, |
| 117 | object_reads_ok: 0, |
| 118 | denials: 0, |
| 119 | frames_reserved: 0, |
| 120 | artifact_signer_fingerprint: [0; 32], |
| 121 | granted_capability_digest: [0; 32], |
| 122 | ..admitted() |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | fn signed(r: &Receipt, seed: &[u8; 32]) -> [u8; RECEIPT_SIZE] { |
| 127 | let mut bytes = encode(r); |
| 128 | sign(&mut bytes, &KeySigner(SigningKey::from_bytes(seed))).unwrap(); |
| 129 | bytes |
| 130 | } |
| 131 | |
| 132 | fn hex(bytes: &[u8]) -> std::string::String { |
| 133 | use core::fmt::Write; |
| 134 | let mut s = std::string::String::new(); |
| 135 | for b in bytes { |
| 136 | let _ = write!(s, "{b:02x}"); |
| 137 | } |
| 138 | s |
| 139 | } |
| 140 | |
| 141 | fn le16(b: &[u8], at: usize) -> u16 { |
| 142 | u16::from_le_bytes([b[at], b[at + 1]]) |
| 143 | } |
| 144 | fn le32(b: &[u8], at: usize) -> u32 { |
| 145 | u32::from_le_bytes([b[at], b[at + 1], b[at + 2], b[at + 3]]) |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn layout_matches_adr_table_offsets() { |
| 150 | let r = admitted(); |
| 151 | let b = encode(&r); |
| 152 | assert_eq!(&b[0..8], b"AIENRCP\0"); |
| 153 | assert_eq!(le16(&b, 8), 0); |
| 154 | assert_eq!(le16(&b, 10), 96); |
| 155 | assert_eq!(le32(&b, 12), 512); |
| 156 | assert_eq!(le32(&b, 16), 0); |
| 157 | assert_eq!(le16(&b, 20), 1); |
| 158 | assert_eq!(le16(&b, 22), 1); |
| 159 | assert_eq!(&b[24..32], &1u64.to_le_bytes()); |
| 160 | assert_eq!(&b[32..48], &r.nonce); |
| 161 | assert!(b[48..64].iter().all(|x| *x == 0)); |
| 162 | assert_eq!(le32(&b, 64), 1); |
| 163 | assert_eq!(le32(&b, 68), 0); |
| 164 | assert_eq!(le32(&b, 72), 0xff); |
| 165 | assert_eq!((le16(&b, 76), le16(&b, 78)), (0, 0)); |
| 166 | assert_eq!( |
| 167 | (le32(&b, 80), le32(&b, 84), le32(&b, 88), le32(&b, 92)), |
| 168 | (8, 2, 5, 10) |
| 169 | ); |
| 170 | for (at, seed) in [ |
| 171 | (96, 0x10), |
| 172 | (128, 0x20), |
| 173 | (160, 0x30), |
| 174 | (192, 0x40), |
| 175 | (224, 0x50), |
| 176 | (256, 0x60), |
| 177 | (288, 0x70), |
| 178 | (320, 0x80), |
| 179 | ] { |
| 180 | assert_eq!(&b[at..at + 32], &pattern(seed), "digest at {at}"); |
| 181 | } |
| 182 | assert!( |
| 183 | b[352..].iter().all(|x| *x == 0), |
| 184 | "absent fields and signature block" |
| 185 | ); |
| 186 | let exit = Receipt { |
| 187 | execution_status: ExecutionStatusCode::Exited, |
| 188 | exit_status: -2, |
| 189 | result_flags: 0, |
| 190 | ..r |
| 191 | }; |
| 192 | assert_eq!(&encode(&exit)[68..72], &(-2i32).to_le_bytes()); |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn golden_bytes_are_stable() { |
| 197 | let a = encode(&admitted()); |
| 198 | let r = encode(&rejected()); |
| 199 | assert_eq!( |
| 200 | hex(&receipt_digest(&a)), |
| 201 | GOLDEN_ADMITTED_DIGEST, |
| 202 | "admitted record: {}", |
| 203 | hex(&a) |
| 204 | ); |
| 205 | assert_eq!(hex(&a), GOLDEN_ADMITTED_RECORD); |
| 206 | assert_eq!( |
| 207 | hex(&receipt_digest(&r)), |
| 208 | GOLDEN_REJECTED_DIGEST, |
| 209 | "rejected record: {}", |
| 210 | hex(&r) |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn encoding_is_deterministic_and_round_trips() { |
| 216 | for r in [admitted(), rejected()] { |
| 217 | let first = encode(&r); |
| 218 | assert_eq!(first, encode(&r)); |
| 219 | assert_eq!(decode(&first), Ok(r)); |
| 220 | let s = signed(&r, &TEST2_SEED); |
| 221 | assert_eq!(decode(&s), Ok(r)); |
| 222 | assert_eq!(receipt_digest(&first), receipt_digest(&s)); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #[test] |
| 227 | fn nonce_and_verifier_identity_vectors() { |
| 228 | let id = pattern(0x80); |
| 229 | assert_ne!(receipt_nonce(&id, 1), receipt_nonce(&id, 2)); |
| 230 | assert_ne!(receipt_nonce(&id, 1), receipt_nonce(&pattern(0x81), 1)); |
| 231 | let commit = b"71cb1703484cbc9d56eabe43402a6f9bf62b1028"; |
| 232 | let bytes = verifier_identity_bytes(commit, 1, 1, 1).unwrap(); |
| 233 | assert_eq!(&bytes[..40], commit); |
| 234 | assert_eq!(&bytes[40..], &[1, 0, 1, 0, 1]); |
| 235 | assert!( |
| 236 | verifier_identity_bytes(b"71CB1703484CBC9D56EABE43402A6F9BF62B1028", 1, 1, 1).is_none() |
| 237 | ); |
| 238 | assert!(verifier_identity_bytes(&commit[..39], 1, 1, 1).is_none()); |
| 239 | assert!( |
| 240 | verifier_identity_bytes(b"71cb1703484cbc9d56eabe43402a6f9bf62b102g", 1, 1, 1).is_none() |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn rejects_length_magic_version_and_sizes() { |
| 246 | let b = encode(&admitted()); |
| 247 | assert_eq!(decode(&b[..511]), Err(ArtifactError::Truncated)); |
| 248 | assert_eq!(decode(&[]), Err(ArtifactError::Truncated)); |
| 249 | let mut long = b.to_vec(); |
| 250 | long.push(0); |
| 251 | assert_eq!(decode(&long), Err(ArtifactError::WrongLength)); |
| 252 | let set = |at: usize, v: u8| { |
| 253 | let mut m = b; |
| 254 | m[at] = v; |
| 255 | m |
| 256 | }; |
| 257 | assert_eq!(decode(&set(0, b'X')), Err(ArtifactError::BadMagic)); |
| 258 | assert_eq!(decode(&set(8, 1)), Err(ArtifactError::UnsupportedVersion)); |
| 259 | assert_eq!(decode(&set(10, 95)), Err(ArtifactError::UnsupportedVersion)); |
| 260 | assert_eq!(decode(&set(13, 0)), Err(ArtifactError::UnsupportedVersion)); |
| 261 | } |
| 262 | |
| 263 | #[test] |
| 264 | fn rejects_unknown_flags_and_nonzero_reserved_bytes() { |
| 265 | let b = encode(&admitted()); |
| 266 | for bit in 3..32 { |
| 267 | let mut m = b; |
| 268 | m[16..20].copy_from_slice(&(1u32 << bit).to_le_bytes()); |
| 269 | assert_eq!( |
| 270 | decode(&m), |
| 271 | Err(ArtifactError::UnsupportedFlags), |
| 272 | "flag bit {bit}" |
| 273 | ); |
| 274 | } |
| 275 | for bit in 8..32 { |
| 276 | let mut m = b; |
| 277 | m[72..76].copy_from_slice(&(0xffu32 | (1 << bit)).to_le_bytes()); |
| 278 | assert_eq!( |
| 279 | decode(&m), |
| 280 | Err(ArtifactError::UnsupportedFlags), |
| 281 | "result bit {bit}" |
| 282 | ); |
| 283 | } |
| 284 | for at in (56..64).chain(500..512) { |
| 285 | let mut m = b; |
| 286 | m[at] = 1; |
| 287 | assert_eq!( |
| 288 | decode(&m), |
| 289 | Err(ArtifactError::ReservedNonZero), |
| 290 | "reserved {at}" |
| 291 | ); |
| 292 | } |
| 293 | let mut s = signed(&admitted(), &TEST2_SEED); |
| 294 | s[402] = 1; |
| 295 | assert_eq!(decode(&s), Err(ArtifactError::ReservedNonZero)); |
| 296 | let mut s = signed(&admitted(), &TEST2_SEED); |
| 297 | s[400] = 2; |
| 298 | assert_eq!(decode(&s), Err(ArtifactError::BadSignatureFormat)); |
| 299 | } |
| 300 | |
| 301 | #[test] |
| 302 | fn pi5_tier_round_trips_with_its_label() { |
| 303 | let r = Receipt { |
| 304 | tier: QualificationTier::Seed0bPi5, |
| 305 | ..admitted() |
| 306 | }; |
| 307 | let back = decode(&encode(&r)).unwrap(); |
| 308 | assert_eq!(back.tier, QualificationTier::Seed0bPi5); |
| 309 | assert_eq!(back.tier.label(), "SEED-0B-PI5"); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn rejects_unknown_enum_and_code_values() { |
| 314 | let b = encode(&admitted()); |
| 315 | let m16 = |at: usize, v: u16| { |
| 316 | let mut m = b; |
| 317 | m[at..at + 2].copy_from_slice(&v.to_le_bytes()); |
| 318 | m |
| 319 | }; |
| 320 | for v in [0u16, 5, 0xffff] { |
| 321 | assert_eq!( |
| 322 | decode(&m16(20, v)), |
| 323 | Err(ArtifactError::MalformedReceipt), |
| 324 | "decision {v}" |
| 325 | ); |
| 326 | } |
| 327 | for v in [0u16, 4, 0xffff] { |
| 328 | assert_eq!( |
| 329 | decode(&m16(22, v)), |
| 330 | Err(ArtifactError::MalformedReceipt), |
| 331 | "tier {v}" |
| 332 | ); |
| 333 | } |
| 334 | for v in [7u32, 0xffff_ffff] { |
| 335 | let mut m = b; |
| 336 | m[64..68].copy_from_slice(&v.to_le_bytes()); |
| 337 | assert_eq!( |
| 338 | decode(&m), |
| 339 | Err(ArtifactError::MalformedReceipt), |
| 340 | "status {v}" |
| 341 | ); |
| 342 | } |
| 343 | let base = rejected(); |
| 344 | for stage in [0u16, 10, 0xffff] { |
| 345 | let r = Receipt { |
| 346 | rejection_stage: stage, |
| 347 | ..base |
| 348 | }; |
| 349 | assert_eq!( |
| 350 | decode(&encode(&r)), |
| 351 | Err(ArtifactError::MalformedReceipt), |
| 352 | "stage {stage}" |
| 353 | ); |
| 354 | } |
| 355 | for reason in [0u16, 20, 0x100, 0x10b, 0xffff] { |
| 356 | let r = Receipt { |
| 357 | rejection_reason: reason, |
| 358 | ..base |
| 359 | }; |
| 360 | assert_eq!( |
| 361 | decode(&encode(&r)), |
| 362 | Err(ArtifactError::MalformedReceipt), |
| 363 | "reason {reason}" |
| 364 | ); |
| 365 | } |
| 366 | for reason in (1u16..=19).chain(0x101..=0x10a) { |
| 367 | let r = Receipt { |
| 368 | rejection_reason: reason, |
| 369 | ..base |
| 370 | }; |
| 371 | assert_eq!(decode(&encode(&r)), Ok(r), "valid reason {reason}"); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | #[test] |
| 376 | fn optional_fields_require_their_presence_flags() { |
| 377 | let base = admitted(); |
| 378 | let cases = [ |
| 379 | Receipt { |
| 380 | machine_id_digest: pattern(1), |
| 381 | ..base |
| 382 | }, |
| 383 | Receipt { |
| 384 | generation: 1, |
| 385 | ..base |
| 386 | }, |
| 387 | Receipt { |
| 388 | context_id: 1, |
| 389 | ..base |
| 390 | }, |
| 391 | Receipt { |
| 392 | observed_time_ns: 1, |
| 393 | ..base |
| 394 | }, |
| 395 | ]; |
| 396 | for r in cases { |
| 397 | assert_eq!(decode(&encode(&r)), Err(ArtifactError::MalformedReceipt)); |
| 398 | } |
| 399 | let present = Receipt { |
| 400 | flags: VALID_FLAGS, |
| 401 | machine_id_digest: pattern(1), |
| 402 | generation: 3, |
| 403 | context_id: 4, |
| 404 | observed_time_ns: 5, |
| 405 | ..base |
| 406 | }; |
| 407 | assert_eq!(decode(&encode(&present)), Ok(present)); |
| 408 | } |
| 409 | |
| 410 | #[test] |
| 411 | fn decision_consistency_rules_are_enforced() { |
| 412 | let a = admitted(); |
| 413 | let r = rejected(); |
| 414 | let bad = [ |
| 415 | Receipt { |
| 416 | rejection_stage: 3, |
| 417 | rejection_reason: 18, |
| 418 | ..a |
| 419 | }, |
| 420 | Receipt { |
| 421 | rejection_reason: 18, |
| 422 | ..a |
| 423 | }, |
| 424 | Receipt { |
| 425 | execution_status: ExecutionStatusCode::Exited, |
| 426 | ..r |
| 427 | }, |
| 428 | Receipt { |
| 429 | exit_status: 1, |
| 430 | ..r |
| 431 | }, |
| 432 | Receipt { syscalls: 1, ..r }, |
| 433 | Receipt { |
| 434 | object_reads_ok: 1, |
| 435 | ..r |
| 436 | }, |
| 437 | Receipt { denials: 1, ..r }, |
| 438 | Receipt { |
| 439 | result_flags: RESULT_RECLAIMED | RESULT_READ_OK, |
| 440 | ..r |
| 441 | }, |
| 442 | Receipt { |
| 443 | exit_status: 3, |
| 444 | ..a |
| 445 | }, |
| 446 | Receipt { |
| 447 | execution_status: ExecutionStatusCode::Fault, |
| 448 | ..a |
| 449 | }, |
| 450 | Receipt { sequence: 9, ..a }, |
| 451 | Receipt { |
| 452 | nonce: [0; 16], |
| 453 | ..a |
| 454 | }, |
| 455 | ]; |
| 456 | for (i, case) in bad.iter().enumerate() { |
| 457 | assert_eq!( |
| 458 | decode(&encode(case)), |
| 459 | Err(ArtifactError::MalformedReceipt), |
| 460 | "case {i}" |
| 461 | ); |
| 462 | } |
| 463 | let faulted = Receipt { |
| 464 | execution_status: ExecutionStatusCode::Fault, |
| 465 | result_flags: RESULT_RECLAIMED | RESULT_WX_SEALED, |
| 466 | ..a |
| 467 | }; |
| 468 | assert_eq!(decode(&encode(&faulted)), Ok(faulted)); |
| 469 | } |
| 470 | |
| 471 | #[test] |
| 472 | fn every_single_bit_change_in_the_signed_range_changes_meaning_or_is_rejected() { |
| 473 | let b = encode(&admitted()); |
| 474 | let digest = receipt_digest(&b); |
| 475 | let original = decode(&b).unwrap(); |
| 476 | for at in 0..RECEIPT_SIGNATURE_OFFSET { |
| 477 | for mask in [0x01u8, 0x80] { |
| 478 | let mut m = b; |
| 479 | m[at] ^= mask; |
| 480 | assert_ne!(receipt_digest(&m), digest, "digest ignores byte {at}"); |
| 481 | if let Ok(r) = decode(&m) { |
| 482 | assert_ne!(r, original, "byte {at} mask {mask:#x} decoded identically"); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | #[test] |
| 489 | fn signature_block_is_outside_the_digest_and_fully_authenticated() { |
| 490 | let anchors = OneKey( |
| 491 | SigningKey::from_bytes(&TEST2_SEED) |
| 492 | .verifying_key() |
| 493 | .to_bytes(), |
| 494 | ); |
| 495 | let s = signed(&admitted(), &TEST2_SEED); |
| 496 | assert_eq!(verify(&s, &anchors, &Ed25519Verifier), Ok(admitted())); |
| 497 | assert_eq!(receipt_digest(&s), receipt_digest(&encode(&admitted()))); |
| 498 | for at in RECEIPT_SIGNATURE_OFFSET..RECEIPT_SIZE { |
| 499 | let mut m = s; |
| 500 | m[at] ^= 0x01; |
| 501 | assert!(verify(&m, &anchors, &Ed25519Verifier).is_err(), "byte {at}"); |
| 502 | } |
| 503 | for at in 0..RECEIPT_SIGNATURE_OFFSET { |
| 504 | let mut m = s; |
| 505 | m[at] ^= 0x01; |
| 506 | assert!( |
| 507 | verify(&m, &anchors, &Ed25519Verifier).is_err(), |
| 508 | "signed byte {at}" |
| 509 | ); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn unsigned_untrusted_and_production_verification_fail_closed() { |
| 515 | let anchors = OneKey( |
| 516 | SigningKey::from_bytes(&TEST2_SEED) |
| 517 | .verifying_key() |
| 518 | .to_bytes(), |
| 519 | ); |
| 520 | let unsigned = encode(&admitted()); |
| 521 | assert_eq!( |
| 522 | verify(&unsigned, &anchors, &Ed25519Verifier), |
| 523 | Err(ArtifactError::BadSignatureFormat) |
| 524 | ); |
| 525 | let other = signed(&admitted(), &TEST3_SEED); |
| 526 | assert_eq!( |
| 527 | verify(&other, &anchors, &Ed25519Verifier), |
| 528 | Err(ArtifactError::UntrustedSigner) |
| 529 | ); |
| 530 | let good = signed(&admitted(), &TEST2_SEED); |
| 531 | assert_eq!( |
| 532 | verify(&good, &EmptyReceiptAnchorSet, &Ed25519Verifier), |
| 533 | Err(ArtifactError::UntrustedSigner) |
| 534 | ); |
| 535 | let mut forged = good; |
| 536 | forged[436] ^= 0xff; |
| 537 | assert_eq!( |
| 538 | verify(&forged, &anchors, &Ed25519Verifier), |
| 539 | Err(ArtifactError::BadSignature) |
| 540 | ); |
| 541 | let mut resigned = good; |
| 542 | assert_eq!( |
| 543 | sign( |
| 544 | &mut resigned, |
| 545 | &KeySigner(SigningKey::from_bytes(&TEST2_SEED)) |
| 546 | ), |
| 547 | Err(ArtifactError::BadSignatureFormat) |
| 548 | ); |
| 549 | let mut invalid = encode(&Receipt { |
| 550 | denials: 1, |
| 551 | ..rejected() |
| 552 | }); |
| 553 | assert_eq!( |
| 554 | sign( |
| 555 | &mut invalid, |
| 556 | &KeySigner(SigningKey::from_bytes(&TEST2_SEED)) |
| 557 | ), |
| 558 | Err(ArtifactError::MalformedReceipt) |
| 559 | ); |
| 560 | } |
| 561 | |
| 562 | #[cfg(feature = "seed0b-test-anchor")] |
| 563 | #[test] |
| 564 | fn test_receipt_anchor_is_rfc8032_test2_and_distinct_from_artifact_key() { |
| 565 | use crate::receipt::seed0b_test_receipt_anchor::{Seed0bTestReceiptAnchorSet, PUBLIC_KEY}; |
| 566 | use crate::signature::seed0b_test_anchor::PUBLIC_KEY as ARTIFACT_KEY; |
| 567 | assert_eq!( |
| 568 | SigningKey::from_bytes(&TEST2_SEED) |
| 569 | .verifying_key() |
| 570 | .to_bytes(), |
| 571 | PUBLIC_KEY |
| 572 | ); |
| 573 | assert_ne!(PUBLIC_KEY, ARTIFACT_KEY); |
| 574 | let s = signed(&admitted(), &TEST2_SEED); |
| 575 | assert_eq!( |
| 576 | verify(&s, &Seed0bTestReceiptAnchorSet, &Ed25519Verifier), |
| 577 | Ok(admitted()) |
| 578 | ); |
| 579 | let other = signed(&admitted(), &TEST3_SEED); |
| 580 | assert_eq!( |
| 581 | verify(&other, &Seed0bTestReceiptAnchorSet, &Ed25519Verifier), |
| 582 | Err(ArtifactError::UntrustedSigner) |
| 583 | ); |
| 584 | } |
| 585 | |
| 586 | #[test] |
| 587 | fn arbitrary_records_never_panic_the_decoder() { |
| 588 | let base = encode(&admitted()); |
| 589 | let mut state = 0x9e37_79b9_7f4a_7c15u64; |
| 590 | let mut buffers: Vec<[u8; RECEIPT_SIZE]> = Vec::new(); |
| 591 | for _ in 0..512 { |
| 592 | let mut m = base; |
| 593 | for _ in 0..8 { |
| 594 | state ^= state << 13; |
| 595 | state ^= state >> 7; |
| 596 | state ^= state << 17; |
| 597 | m[(state as usize) % RECEIPT_SIZE] = (state >> 32) as u8; |
| 598 | } |
| 599 | buffers.push(m); |
| 600 | } |
| 601 | for m in &buffers { |
| 602 | let _ = decode(m); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | const GOLDEN_ADMITTED_DIGEST: &str = |
| 607 | "16bcbe523a62d9f4c907729fb2fb09d630ce36f80d7169e202730173a59bdfdc"; |
| 608 | const GOLDEN_ADMITTED_RECORD: &str = "4149454e52435000000060000002000000000000010001000100000000000000e28b2fcc80dd4e973fbc3d13dc62801b000000000000000000000000000000000100000000000000ff000000000000000800000002000000050000000a000000101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; |
| 609 | const GOLDEN_REJECTED_DIGEST: &str = |
| 610 | "f12ebace5ec0f2a8f06c415d58d936389dcc2fed64dbed4bc91efd01a734a6a3"; |
| 611 | |
| 612 | #[test] |
| 613 | fn canary_failed_decision_and_status_must_agree() { |
| 614 | let base = admitted(); |
| 615 | // Decision CanaryFailed with a non-CanaryFailed status. |
| 616 | for status in [ |
| 617 | ExecutionStatusCode::NotRun, |
| 618 | ExecutionStatusCode::Exited, |
| 619 | ExecutionStatusCode::Timeout, |
| 620 | ExecutionStatusCode::Fault, |
| 621 | ] { |
| 622 | let r = Receipt { |
| 623 | decision: ReceiptDecision::CanaryFailed, |
| 624 | execution_status: status, |
| 625 | ..base |
| 626 | }; |
| 627 | assert_eq!(validate(&r), Err(ArtifactError::MalformedReceipt)); |
| 628 | assert_eq!(decode(&encode(&r)), Err(ArtifactError::MalformedReceipt)); |
| 629 | } |
| 630 | // Status CanaryFailed under every other decision. |
| 631 | for decision in [ReceiptDecision::Admitted, ReceiptDecision::Destroyed] { |
| 632 | let r = Receipt { |
| 633 | decision, |
| 634 | execution_status: ExecutionStatusCode::CanaryFailed, |
| 635 | ..base |
| 636 | }; |
| 637 | assert_eq!(validate(&r), Err(ArtifactError::MalformedReceipt)); |
| 638 | assert_eq!(decode(&encode(&r)), Err(ArtifactError::MalformedReceipt)); |
| 639 | } |
| 640 | let rej = Receipt { |
| 641 | execution_status: ExecutionStatusCode::CanaryFailed, |
| 642 | ..rejected() |
| 643 | }; |
| 644 | assert_eq!(decode(&encode(&rej)), Err(ArtifactError::MalformedReceipt)); |
| 645 | // The matching pair is accepted. |
| 646 | let ok = Receipt { |
| 647 | decision: ReceiptDecision::CanaryFailed, |
| 648 | execution_status: ExecutionStatusCode::CanaryFailed, |
| 649 | result_flags: base.result_flags & !RESULT_CANARY_PASSED, |
| 650 | ..base |
| 651 | }; |
| 652 | assert_eq!(validate(&ok), Ok(())); |
| 653 | assert_eq!(decode(&encode(&ok)), Ok(ok)); |
| 654 | } |