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/tests.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 / tests.rs` 425 lines ยท 15801 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 use std::vec; 17 use std::vec::Vec; 18 19 use crate::canonical::{ 20 granted_capability_digest, payload_digest, requested_capability_digest, 21 resource_envelope_digest, 22 }; 23 use crate::capability::{CapabilityRequest, RIGHT_READ, RIGHT_WRITE}; 24 use crate::error::ArtifactError; 25 use crate::format::{Artifact, CAPABILITY_TABLE_OFFSET, MAX_ARTIFACT_SIZE}; 26 use crate::id::compute_artifact_id; 27 use crate::signature::{Ed25519Verifier, SignatureVerifier}; 28 use crate::verify::parse_and_identify; 29 30 #[cfg(feature = "seed0b-test-anchor")] 31 use crate::signature::{ 32 artifact_signature_message, signer_fingerprint, ArtifactVerifier, ConfiguredArtifactVerifier, 33 EmptyTrustAnchorSet, TrustAnchorSet, TrustTier, 34 }; 35 36 const RESOURCE_OFFSET: usize = 240; 37 const PAYLOAD_OFFSET: usize = 288; 38 const SIGNATURE_OFFSET: usize = 296; 39 const TOTAL_LENGTH: usize = 396; 40 41 fn put_u16(bytes: &mut [u8], offset: usize, value: u16) { 42 bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes()); 43 } 44 45 fn put_u32(bytes: &mut [u8], offset: usize, value: u32) { 46 bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); 47 } 48 49 fn put_u64(bytes: &mut [u8], offset: usize, value: u64) { 50 bytes[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); 51 } 52 53 fn fixture() -> Vec { 54 let mut bytes = vec![0u8; TOTAL_LENGTH]; 55 bytes[0..8].copy_from_slice(b"AIENART\0"); 56 put_u16(&mut bytes, 8, 0); 57 put_u16(&mut bytes, 10, 128); 58 put_u16(&mut bytes, 12, 1); 59 put_u16(&mut bytes, 14, 1); 60 put_u32(&mut bytes, 20, TOTAL_LENGTH as u32); 61 put_u32(&mut bytes, 24, 128); 62 put_u16(&mut bytes, 28, 2); 63 put_u16(&mut bytes, 30, 32); 64 put_u16(&mut bytes, 32, 0); 65 put_u32(&mut bytes, 36, 0); 66 put_u32(&mut bytes, 40, CAPABILITY_TABLE_OFFSET as u32); 67 put_u16(&mut bytes, 44, 1); 68 put_u16(&mut bytes, 46, 48); 69 put_u32(&mut bytes, 48, RESOURCE_OFFSET as u32); 70 put_u16(&mut bytes, 52, 48); 71 put_u32(&mut bytes, 56, PAYLOAD_OFFSET as u32); 72 put_u32(&mut bytes, 60, 8); 73 put_u32(&mut bytes, 64, SIGNATURE_OFFSET as u32); 74 put_u16(&mut bytes, 68, 100); 75 put_u16(&mut bytes, 70, 1); 76 77 // Code section: one AArch64 RET instruction, RX. 78 put_u16(&mut bytes, 128, 1); 79 put_u16(&mut bytes, 130, 5); 80 put_u32(&mut bytes, 136, 0); 81 put_u32(&mut bytes, 140, 4); 82 put_u32(&mut bytes, 144, 4); 83 put_u32(&mut bytes, 148, 4096); 84 85 // Data section: four initialized bytes, one page reserved, RW and NX. 86 put_u16(&mut bytes, 160, 2); 87 put_u16(&mut bytes, 162, 3); 88 put_u32(&mut bytes, 168, 4); 89 put_u32(&mut bytes, 172, 4); 90 put_u32(&mut bytes, 176, 4096); 91 put_u32(&mut bytes, 180, 4096); 92 93 // Scoped READ request for object 7, restricted to bytes [0, 4). 94 put_u16(&mut bytes, 192, 3); 95 put_u32(&mut bytes, 196, 7); 96 put_u32(&mut bytes, 200, RIGHT_READ); 97 put_u32(&mut bytes, 204, 1); 98 put_u32(&mut bytes, 208, 4); 99 put_u64(&mut bytes, 216, 4); 100 put_u64(&mut bytes, 224, 0); 101 put_u64(&mut bytes, 232, 4); 102 103 put_u32(&mut bytes, RESOURCE_OFFSET, 1); // code pages 104 put_u32(&mut bytes, RESOURCE_OFFSET + 4, 1); // data pages 105 put_u32(&mut bytes, RESOURCE_OFFSET + 8, 1); // stack pages 106 put_u16(&mut bytes, RESOURCE_OFFSET + 12, 1); // capability slots 107 put_u32(&mut bytes, RESOURCE_OFFSET + 16, 2); // IPC messages 108 put_u32(&mut bytes, RESOURCE_OFFSET + 20, 16); // IPC bytes 109 put_u64(&mut bytes, RESOURCE_OFFSET + 24, 1000); // CPU ticks 110 put_u64(&mut bytes, RESOURCE_OFFSET + 32, 2000); // elapsed ticks 111 put_u32(&mut bytes, RESOURCE_OFFSET + 40, 16); // syscalls 112 113 bytes[PAYLOAD_OFFSET..PAYLOAD_OFFSET + 4].copy_from_slice(&[0xc0, 0x03, 0x5f, 0xd6]); 114 bytes[PAYLOAD_OFFSET + 4..PAYLOAD_OFFSET + 8].copy_from_slice(b"DATA"); 115 put_u16(&mut bytes, SIGNATURE_OFFSET, 1); 116 bytes[SIGNATURE_OFFSET + 4..SIGNATURE_OFFSET + 36].fill(0xa5); 117 bytes[SIGNATURE_OFFSET + 36..SIGNATURE_OFFSET + 100].fill(0x5a); 118 bytes 119 } 120 121 fn parse_error(bytes: &[u8]) -> ArtifactError { 122 Artifact::parse(bytes).expect_err("mutation must fail closed") 123 } 124 125 #[test] 126 fn accepts_canonical_artifact_and_exposes_exact_slices() { 127 let bytes = fixture(); 128 let parsed = parse_and_identify(&bytes).unwrap(); 129 assert_eq!(parsed.artifact.target_arch, 1); 130 assert_eq!(parsed.artifact.abi_version, 1); 131 assert_eq!(parsed.artifact.entry_offset, 0); 132 assert_eq!(parsed.artifact.code_bytes(), &[0xc0, 0x03, 0x5f, 0xd6]); 133 assert_eq!(parsed.artifact.data_bytes(), b"DATA"); 134 assert_eq!(parsed.artifact.capabilities.len(), 1); 135 assert_eq!(parsed.artifact.capabilities.as_slice()[0].resource_id, 7); 136 assert_eq!( 137 parsed.artifact.capabilities.as_slice()[0].rights, 138 RIGHT_READ 139 ); 140 assert_eq!(parsed.artifact.resources.code_pages, 1); 141 assert_eq!(parsed.artifact.unsigned_bytes.len(), SIGNATURE_OFFSET); 142 assert_eq!( 143 parsed.payload_digest, 144 payload_digest(&parsed.artifact), 145 "payload digest is the exact serialized code then data bytes" 146 ); 147 assert_eq!( 148 parsed.artifact_id, 149 compute_artifact_id(&parsed.artifact), 150 "host and kernel entry points share the same identity function" 151 ); 152 let requested = requested_capability_digest(parsed.artifact.capability_request_bytes); 153 let granted = granted_capability_digest(parsed.artifact.capability_request_bytes); 154 let resources = resource_envelope_digest(parsed.artifact.resource_envelope_bytes); 155 assert_ne!(requested, granted); 156 assert_eq!( 157 hex(&requested), 158 "`adf96aa3e792b1d4a48fe925532efe34ff2a82fe7c07ce013d6daa8bfecb28db`" 159 ); 160 assert_eq!( 161 hex(&resources), 162 "`02f22c42bc7fff8143e11d282f2cfc7773fc483356a5ad9861c937cc6484a3e5`" 163 ); 164 assert_eq!( 165 hex(&parsed.payload_digest), 166 "`f9f5b6ad6a7b931afdce57c59c806df4971c31d72ef560cd7a140d34aba19758`" 167 ); 168 assert_eq!( 169 hex(parsed.artifact_id.as_bytes()), 170 "`71976b5c38ec15f2307565ed3eea51fde4a5375970cd49338d6e16166238cf4b`" 171 ); 172 } 173 174 #[test] 175 fn every_truncation_of_a_valid_artifact_is_rejected() { 176 let bytes = fixture(); 177 for end in 0..bytes.len() { 178 assert!( 179 Artifact::parse(&bytes[..end]).is_err(), 180 "truncation at {end}" 181 ); 182 } 183 } 184 185 #[test] 186 fn rejects_header_and_length_mutations() { 187 let mut bytes = fixture(); 188 bytes[0] ^= 1; 189 assert_eq!(parse_error(&bytes), ArtifactError::BadMagic); 190 191 let mut bytes = fixture(); 192 put_u16(&mut bytes, 8, 1); 193 assert_eq!(parse_error(&bytes), ArtifactError::UnsupportedVersion); 194 195 let mut bytes = fixture(); 196 put_u16(&mut bytes, 12, 2); 197 assert_eq!(parse_error(&bytes), ArtifactError::WrongTarget); 198 199 let mut bytes = fixture(); 200 put_u16(&mut bytes, 14, 2); 201 assert_eq!(parse_error(&bytes), ArtifactError::WrongAbi); 202 203 let mut bytes = fixture(); 204 put_u32(&mut bytes, 20, (TOTAL_LENGTH - 1) as u32); 205 assert_eq!(parse_error(&bytes), ArtifactError::WrongLength); 206 207 let mut bytes = fixture(); 208 put_u32(&mut bytes, 64, u32::MAX - 15); 209 assert!( 210 parse_error(&bytes) == ArtifactError::SectionOverlap 211 || parse_error(&bytes) == ArtifactError::LengthOverflow 212 ); 213 214 let mut bytes = fixture(); 215 put_u32(&mut bytes, 16, 1); 216 assert_eq!(parse_error(&bytes), ArtifactError::UnsupportedFlags); 217 } 218 219 #[test] 220 fn rejects_section_entry_capability_resource_and_reserved_mutations() { 221 let mut bytes = fixture(); 222 put_u32(&mut bytes, 168, 0); // data overlaps code 223 assert_eq!(parse_error(&bytes), ArtifactError::BadSection); 224 225 let mut bytes = fixture(); 226 put_u32(&mut bytes, 36, 4); // entry outside code file bytes 227 assert_eq!(parse_error(&bytes), ArtifactError::BadEntryPoint); 228 229 let mut bytes = fixture(); 230 put_u32(&mut bytes, 200, RIGHT_READ | 0x40); // unknown rights bit 231 assert_eq!(parse_error(&bytes), ArtifactError::MalformedCapability); 232 233 let mut bytes = fixture(); 234 put_u64(&mut bytes, 224, u64::MAX); // range-end overflow 235 assert_eq!(parse_error(&bytes), ArtifactError::LengthOverflow); 236 237 let mut bytes = fixture(); 238 put_u32(&mut bytes, RESOURCE_OFFSET, 65); // code pages exceed format max 239 assert_eq!(parse_error(&bytes), ArtifactError::ResourceLimit); 240 241 let mut bytes = fixture(); 242 put_u64(&mut bytes, RESOURCE_OFFSET + 24, 1_000_000_001); 243 assert_eq!(parse_error(&bytes), ArtifactError::ResourceLimit); 244 245 let mut bytes = fixture(); 246 bytes[34] = 1; 247 assert_eq!(parse_error(&bytes), ArtifactError::UnsupportedVersion); 248 249 let mut bytes = fixture(); 250 bytes[192 + 2] = 1; 251 assert_eq!(parse_error(&bytes), ArtifactError::ReservedNonZero); 252 253 let mut bytes = fixture(); 254 bytes[RESOURCE_OFFSET + 14] = 1; 255 assert_eq!(parse_error(&bytes), ArtifactError::ReservedNonZero); 256 257 let mut bytes = fixture(); 258 put_u16(&mut bytes, 44, 17); 259 assert_eq!(parse_error(&bytes), ArtifactError::ResourceLimit); 260 } 261 262 #[test] 263 fn payload_manifest_and_resource_changes_change_artifact_identity() { 264 let original_bytes = fixture(); 265 let original = parse_and_identify(&original_bytes).unwrap(); 266 267 let mut payload_changed = fixture(); 268 payload_changed[PAYLOAD_OFFSET] ^= 1; 269 let payload_changed = parse_and_identify(&payload_changed).unwrap(); 270 assert_ne!(original.artifact_id, payload_changed.artifact_id); 271 assert_ne!(original.payload_digest, payload_changed.payload_digest); 272 273 let mut request_changed = fixture(); 274 put_u32(&mut request_changed, 200, RIGHT_READ | RIGHT_WRITE); 275 let request_changed = parse_and_identify(&request_changed).unwrap(); 276 assert_ne!(original.artifact_id, request_changed.artifact_id); 277 278 let mut resources_changed = fixture(); 279 put_u64(&mut resources_changed, RESOURCE_OFFSET + 24, 999); 280 let resources_changed = parse_and_identify(&resources_changed).unwrap(); 281 assert_ne!(original.artifact_id, resources_changed.artifact_id); 282 } 283 284 #[test] 285 fn signature_bytes_are_not_recursively_included_in_artifact_id() { 286 let original_bytes = fixture(); 287 let original = parse_and_identify(&original_bytes).unwrap(); 288 let mut changed_signature = fixture(); 289 changed_signature[SIGNATURE_OFFSET + 36] ^= 1; 290 let changed_signature = parse_and_identify(&changed_signature).unwrap(); 291 assert_eq!(original.artifact_id, changed_signature.artifact_id); 292 293 let mut changed_signer = fixture(); 294 changed_signer[SIGNATURE_OFFSET + 4] ^= 1; 295 let changed_signer = parse_and_identify(&changed_signer).unwrap(); 296 assert_eq!(original.artifact_id, changed_signer.artifact_id); 297 } 298 299 #[test] 300 fn arbitrary_bounded_bytes_never_panic_the_parser() { 301 let mut state = 0x9e37_79b9_u32; 302 for length in 0..=4096 { 303 let mut bytes = vec![0u8; length]; 304 for byte in &mut bytes { 305 state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); 306 *byte = (state >> 24) as u8; 307 } 308 let _ = Artifact::parse(&bytes); 309 } 310 311 let over_limit = vec![0u8; MAX_ARTIFACT_SIZE + 1]; 312 assert_eq!(parse_error(&over_limit), ArtifactError::ResourceLimit); 313 } 314 315 #[test] 316 fn authority_attenuation_is_directly_checkable() { 317 let read = CapabilityRequest { 318 resource_kind: 3, 319 resource_id: 7, 320 rights: RIGHT_READ, 321 bounds_kind: 1, 322 max_operations: 4, 323 max_bytes: 4, 324 byte_offset: 0, 325 byte_length: 4, 326 }; 327 let read_write = CapabilityRequest { 328 rights: RIGHT_READ | RIGHT_WRITE, 329 ..read 330 }; 331 assert!(read.is_attenuation_of(&read)); 332 assert!(!read_write.is_attenuation_of(&read)); 333 assert!(read.is_attenuation_of(&read_write)); 334 } 335 336 #[test] 337 fn `ed25519`_verifier_accepts_rfc8032_pure_signature_vector() { 338 const PUBLIC_KEY: [u8; 32] = [ 339 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe, 0xd3, 0xc9, 0x64, 0x07, 340 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 341 0x51, 0x1a, 342 ]; 343 const SIGNATURE: [u8; 64] = [ 344 0xe5, 0x56, 0x43, 0x00, 0xc3, 0x60, 0xac, 0x72, 0x90, 0x86, 0xe2, 0xcc, 0x80, 0x6e, 0x82, 345 0x8a, 0x84, 0x87, 0x7f, 0x1e, 0xb8, 0xe5, 0xd9, 0x74, 0xd8, 0x73, 0xe0, 0x65, 0x22, 0x49, 346 0x01, 0x55, 0x5f, 0xb8, 0x82, 0x15, 0x90, 0xa3, 0x3b, 0xac, 0xc6, 0x1e, 0x39, 0x70, 0x1c, 347 0xf9, 0xb4, 0x6b, 0xd2, 0x5b, 0xf5, 0xf0, 0x59, 0x5b, 0xbe, 0x24, 0x65, 0x51, 0x41, 0x43, 348 0x8e, 0x7a, 0x10, 0x0b, 349 ]; 350 assert!(Ed25519Verifier.verify(&PUBLIC_KEY, b"", &SIGNATURE)); 351 let mut changed = SIGNATURE; 352 changed[0] ^= 1; 353 assert!(!Ed25519Verifier.verify(&PUBLIC_KEY, b"", &changed)); 354 } 355 356 #[cfg(feature = "seed0b-test-anchor")] 357 #[test] 358 fn qualification_anchor_authenticates_exact_artifact_identity() { 359 use `ed25519`_dalek::{Signer, SigningKey}; 360 361 use crate::signature::seed0b_test_anchor::{Seed0bTestAnchorSet, PUBLIC_KEY}; 362 363 const TEST_ONLY_SEED: [u8; 32] = [ 364 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 365 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 366 0x7f, 0x60, 367 ]; 368 369 let signing_key = SigningKey::from_bytes(&TEST_ONLY_SEED); 370 assert_eq!(signing_key.verifying_key().to_bytes(), PUBLIC_KEY); 371 let mut bytes = fixture(); 372 let unsigned = Artifact::parse(&bytes).unwrap(); 373 let id = compute_artifact_id(&unsigned); 374 let signature = signing_key.sign(&artifact_signature_message(&id)); 375 bytes[SIGNATURE_OFFSET + 4..SIGNATURE_OFFSET + 36] 376 .copy_from_slice(&signer_fingerprint(&PUBLIC_KEY)); 377 bytes[SIGNATURE_OFFSET + 36..SIGNATURE_OFFSET + 100].copy_from_slice(&signature.to_bytes()); 378 379 let anchors = Seed0bTestAnchorSet; 380 let verifier = ConfiguredArtifactVerifier::new(&anchors, Ed25519Verifier); 381 let verified = verifier.verify(&bytes).unwrap(); 382 assert_eq!(verified.identified().artifact_id, id); 383 assert_eq!(verified.trust_tier(), TrustTier::Seed0bQualification); 384 385 let no_anchors = EmptyTrustAnchorSet; 386 let production_verifier = ConfiguredArtifactVerifier::new(&no_anchors, Ed25519Verifier); 387 assert_eq!( 388 production_verifier.verify(&bytes).unwrap_err(), 389 ArtifactError::UntrustedSigner 390 ); 391 392 let mut bad_signature = bytes.clone(); 393 bad_signature[SIGNATURE_OFFSET + 36] ^= 1; 394 assert_eq!( 395 verifier.verify(&bad_signature).unwrap_err(), 396 ArtifactError::BadSignature 397 ); 398 399 let mut bad_fingerprint = bytes.clone(); 400 bad_fingerprint[SIGNATURE_OFFSET + 4] ^= 1; 401 assert_eq!( 402 verifier.verify(&bad_fingerprint).unwrap_err(), 403 ArtifactError::UntrustedSigner 404 ); 405 406 let mut changed_payload = bytes; 407 changed_payload[PAYLOAD_OFFSET] ^= 1; 408 assert_eq!( 409 verifier.verify(&changed_payload).unwrap_err(), 410 ArtifactError::BadSignature 411 ); 412 413 let expected = signer_fingerprint(&PUBLIC_KEY); 414 assert!(anchors.find(&expected).is_some()); 415 assert!(anchors.find(&[0; 32]).is_none()); 416 } 417 418 fn hex(bytes: &[u8]) -> std::string::String { 419 use std::fmt::Write; 420 let mut output = std::string::String::new(); 421 for byte in bytes { 422 write!(&mut output, "{byte:02x}").unwrap(); 423 } 424 output 425 }