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-tool / src / main.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 | use std::env; |
| 17 | use std::error::Error; |
| 18 | use std::fs; |
| 19 | use std::path::Path; |
| 20 | |
| 21 | use aienos_artifact::canonical::payload_digest; |
| 22 | use aienos_artifact::capability::CAPABILITY_RECORD_SIZE; |
| 23 | use aienos_artifact::format::{ |
| 24 | Artifact, ABI_V1, CAPABILITY_TABLE_OFFSET, HEADER_SIZE, MAX_ARTIFACT_SIZE, MAX_PAYLOAD_SIZE, |
| 25 | SECTION_RECORD_SIZE, SECTION_TABLE_OFFSET, SIGNATURE_ALGORITHM_ED25519, SIGNATURE_BLOCK_SIZE, |
| 26 | TARGET_AARCH64_LE, |
| 27 | }; |
| 28 | use aienos_artifact::resource::RESOURCE_ENVELOPE_SIZE; |
| 29 | #[cfg(not(feature = "seed0b-test-signing"))] |
| 30 | use aienos_artifact::signature::ProductionTrustAnchorSet; |
| 31 | #[cfg(feature = "seed0b-test-signing")] |
| 32 | use aienos_artifact::signature::{artifact_signature_message, signer_fingerprint}; |
| 33 | use aienos_artifact::signature::{ |
| 34 | ArtifactVerifier, ConfiguredArtifactVerifier, Ed25519Verifier, |
| 35 | SIGNATURE_BLOCK_SIZE as SIG_BLOCK, |
| 36 | }; |
| 37 | use aienos_artifact::verify::parse_and_identify; |
| 38 | #[cfg(feature = "seed0b-test-signing")] |
| 39 | use ed25519_dalek::{Signer, SigningKey}; |
| 40 | use serde::Deserialize; |
| 41 | |
| 42 | #[cfg(all(feature = "seed0b-test-signing", not(debug_assertions)))] |
| 43 | compile_error!("the SEED-0B test signing identity is unavailable to release builds"); |
| 44 | |
| 45 | #[cfg(feature = "seed0b-test-signing")] |
| 46 | mod corpus; |
| 47 | mod receipt_cmd; |
| 48 | |
| 49 | const PAGE_SIZE: u32 = 4096; |
| 50 | const SIGNATURE_OFFSET_HEADER: usize = 64; |
| 51 | const SIGNATURE_LENGTH_HEADER: usize = 68; |
| 52 | const SIGNATURE_ALGORITHM_HEADER: usize = 70; |
| 53 | |
| 54 | #[derive(Debug, Deserialize)] |
| 55 | #[serde(deny_unknown_fields)] |
| 56 | struct Manifest { |
| 57 | entry_offset: u32, |
| 58 | capabilities: Vec<CapabilityManifest>, |
| 59 | resources: ResourceManifest, |
| 60 | } |
| 61 | |
| 62 | #[derive(Debug, Deserialize)] |
| 63 | #[serde(deny_unknown_fields)] |
| 64 | struct CapabilityManifest { |
| 65 | resource_kind: u16, |
| 66 | resource_id: u32, |
| 67 | rights: u32, |
| 68 | bounds_kind: u32, |
| 69 | max_operations: u32, |
| 70 | max_bytes: u64, |
| 71 | byte_offset: u64, |
| 72 | byte_length: u64, |
| 73 | } |
| 74 | |
| 75 | #[derive(Debug, Deserialize)] |
| 76 | #[serde(deny_unknown_fields)] |
| 77 | struct ResourceManifest { |
| 78 | code_pages: u32, |
| 79 | data_pages: u32, |
| 80 | stack_pages: u32, |
| 81 | max_capabilities: u16, |
| 82 | ipc_messages: u32, |
| 83 | ipc_bytes: u32, |
| 84 | cpu_ticks: u64, |
| 85 | elapsed_ticks: u64, |
| 86 | syscall_count: u32, |
| 87 | } |
| 88 | |
| 89 | fn main() { |
| 90 | if let Err(error) = run() { |
| 91 | eprintln!("aienos-artifact-tool: {error}"); |
| 92 | std::process::exit(2); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | fn run() -> Result<(), Box<dyn Error>> { |
| 97 | let args: Vec<String> = env::args().collect(); |
| 98 | match args.get(1).map(String::as_str) { |
| 99 | Some("pack") if args.len() == 6 => { |
| 100 | let manifest: Manifest = serde_json::from_slice(&fs::read(&args[2])?)?; |
| 101 | let code = fs::read(&args[3])?; |
| 102 | let data = fs::read(&args[4])?; |
| 103 | let bytes = pack_bytes(&manifest, &code, &data)?; |
| 104 | fs::write(&args[5], bytes)?; |
| 105 | println!("PACK: PASS"); |
| 106 | } |
| 107 | Some("inspect") if args.len() == 3 => inspect(&fs::read(&args[2])?)?, |
| 108 | Some("id") if args.len() == 3 => { |
| 109 | let bytes = fs::read(&args[2])?; |
| 110 | let identified = tool_artifact(parse_and_identify(&bytes))?; |
| 111 | println!("{}", hex(identified.artifact_id.as_bytes())); |
| 112 | } |
| 113 | Some("sign") if args.len() == 4 => sign_file(Path::new(&args[2]), Path::new(&args[3]))?, |
| 114 | Some("verify") if args.len() == 3 => verify_file(&fs::read(&args[2])?)?, |
| 115 | Some("receipt") => receipt_cmd::run(&args[2..])?, |
| 116 | #[cfg(feature = "seed0b-test-signing")] |
| 117 | Some("negative-corpus") if args.len() == 3 => corpus::write_corpus(Path::new(&args[2]))?, |
| 118 | _ => return Err(usage().into()), |
| 119 | } |
| 120 | Ok(()) |
| 121 | } |
| 122 | |
| 123 | fn usage() -> String { |
| 124 | format!( |
| 125 | "usage: aienos-artifact-tool pack MANIFEST.json CODE.bin DATA.bin OUTPUT.aien | inspect FILE.aien | id FILE.aien | sign INPUT.aien OUTPUT.aien | verify FILE.aien | {}", |
| 126 | receipt_cmd::USAGE |
| 127 | ) |
| 128 | } |
| 129 | |
| 130 | fn pack_bytes(manifest: &Manifest, code: &[u8], data: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> { |
| 131 | if code.is_empty() || data.is_empty() || manifest.capabilities.len() > 16 { |
| 132 | return Err("code, data, and capability count are outside v0 bounds".into()); |
| 133 | } |
| 134 | let capability_bytes = manifest |
| 135 | .capabilities |
| 136 | .len() |
| 137 | .checked_mul(CAPABILITY_RECORD_SIZE) |
| 138 | .ok_or("capability length overflow")?; |
| 139 | let resource_offset = CAPABILITY_TABLE_OFFSET |
| 140 | .checked_add(capability_bytes) |
| 141 | .ok_or("resource offset overflow")?; |
| 142 | let payload_offset = align_up_16( |
| 143 | resource_offset |
| 144 | .checked_add(RESOURCE_ENVELOPE_SIZE) |
| 145 | .ok_or("payload offset overflow")?, |
| 146 | )?; |
| 147 | let payload_length = code |
| 148 | .len() |
| 149 | .checked_add(data.len()) |
| 150 | .ok_or("payload overflow")?; |
| 151 | let signature_offset = payload_offset |
| 152 | .checked_add(payload_length) |
| 153 | .ok_or("signature offset overflow")?; |
| 154 | let total_length = signature_offset |
| 155 | .checked_add(SIG_BLOCK) |
| 156 | .ok_or("artifact length overflow")?; |
| 157 | if total_length > MAX_ARTIFACT_SIZE || payload_length > MAX_PAYLOAD_SIZE { |
| 158 | return Err("artifact exceeds v0 maximum size".into()); |
| 159 | } |
| 160 | |
| 161 | let code_length = u32::try_from(code.len())?; |
| 162 | let data_length = u32::try_from(data.len())?; |
| 163 | let payload_length_u32 = u32::try_from(payload_length)?; |
| 164 | let resource_offset_u32 = u32::try_from(resource_offset)?; |
| 165 | let payload_offset_u32 = u32::try_from(payload_offset)?; |
| 166 | let signature_offset_u32 = u32::try_from(signature_offset)?; |
| 167 | let total_length_u32 = u32::try_from(total_length)?; |
| 168 | |
| 169 | let mut bytes = vec![0u8; total_length]; |
| 170 | bytes[0..8].copy_from_slice(b"AIENART\0"); |
| 171 | put_u16(&mut bytes, 8, 0); |
| 172 | put_u16(&mut bytes, 10, HEADER_SIZE as u16); |
| 173 | put_u16(&mut bytes, 12, TARGET_AARCH64_LE); |
| 174 | put_u16(&mut bytes, 14, ABI_V1); |
| 175 | put_u32(&mut bytes, 20, total_length_u32); |
| 176 | put_u32(&mut bytes, 24, SECTION_TABLE_OFFSET as u32); |
| 177 | put_u16(&mut bytes, 28, 2); |
| 178 | put_u16(&mut bytes, 30, SECTION_RECORD_SIZE as u16); |
| 179 | put_u16(&mut bytes, 32, 0); |
| 180 | put_u32(&mut bytes, 36, manifest.entry_offset); |
| 181 | put_u32(&mut bytes, 40, CAPABILITY_TABLE_OFFSET as u32); |
| 182 | put_u16(&mut bytes, 44, u16::try_from(manifest.capabilities.len())?); |
| 183 | put_u16(&mut bytes, 46, CAPABILITY_RECORD_SIZE as u16); |
| 184 | put_u32(&mut bytes, 48, resource_offset_u32); |
| 185 | put_u16(&mut bytes, 52, RESOURCE_ENVELOPE_SIZE as u16); |
| 186 | put_u32(&mut bytes, 56, payload_offset_u32); |
| 187 | put_u32(&mut bytes, 60, payload_length_u32); |
| 188 | put_u32(&mut bytes, SIGNATURE_OFFSET_HEADER, signature_offset_u32); |
| 189 | put_u16( |
| 190 | &mut bytes, |
| 191 | SIGNATURE_LENGTH_HEADER, |
| 192 | SIGNATURE_BLOCK_SIZE as u16, |
| 193 | ); |
| 194 | put_u16( |
| 195 | &mut bytes, |
| 196 | SIGNATURE_ALGORITHM_HEADER, |
| 197 | SIGNATURE_ALGORITHM_ED25519, |
| 198 | ); |
| 199 | |
| 200 | encode_section( |
| 201 | &mut bytes, |
| 202 | SECTION_TABLE_OFFSET, |
| 203 | 1, |
| 204 | 5, |
| 205 | 0, |
| 206 | code_length, |
| 207 | code_length, |
| 208 | ); |
| 209 | encode_section( |
| 210 | &mut bytes, |
| 211 | SECTION_TABLE_OFFSET + SECTION_RECORD_SIZE, |
| 212 | 2, |
| 213 | 3, |
| 214 | code_length, |
| 215 | data_length, |
| 216 | manifest |
| 217 | .resources |
| 218 | .data_pages |
| 219 | .checked_mul(PAGE_SIZE) |
| 220 | .ok_or("data page overflow")?, |
| 221 | ); |
| 222 | |
| 223 | for (index, cap) in manifest.capabilities.iter().enumerate() { |
| 224 | let offset = CAPABILITY_TABLE_OFFSET + index * CAPABILITY_RECORD_SIZE; |
| 225 | put_u16(&mut bytes, offset, cap.resource_kind); |
| 226 | put_u32(&mut bytes, offset + 4, cap.resource_id); |
| 227 | put_u32(&mut bytes, offset + 8, cap.rights); |
| 228 | put_u32(&mut bytes, offset + 12, cap.bounds_kind); |
| 229 | put_u32(&mut bytes, offset + 16, cap.max_operations); |
| 230 | put_u64(&mut bytes, offset + 24, cap.max_bytes); |
| 231 | put_u64(&mut bytes, offset + 32, cap.byte_offset); |
| 232 | put_u64(&mut bytes, offset + 40, cap.byte_length); |
| 233 | } |
| 234 | encode_resources(&mut bytes, resource_offset, &manifest.resources); |
| 235 | bytes[payload_offset..payload_offset + code.len()].copy_from_slice(code); |
| 236 | bytes[payload_offset + code.len()..signature_offset].copy_from_slice(data); |
| 237 | put_u16(&mut bytes, signature_offset, SIGNATURE_ALGORITHM_ED25519); |
| 238 | |
| 239 | let parsed = tool_artifact(Artifact::parse(&bytes))?; |
| 240 | for request in parsed.capabilities.as_slice() { |
| 241 | tool_artifact(request.validate())?; |
| 242 | } |
| 243 | tool_artifact(parsed.resources.validate(parsed.capabilities.len()))?; |
| 244 | Ok(bytes) |
| 245 | } |
| 246 | |
| 247 | fn encode_section( |
| 248 | bytes: &mut [u8], |
| 249 | offset: usize, |
| 250 | kind: u16, |
| 251 | permissions: u16, |
| 252 | payload_offset: u32, |
| 253 | file_length: u32, |
| 254 | memory_length: u32, |
| 255 | ) { |
| 256 | put_u16(bytes, offset, kind); |
| 257 | put_u16(bytes, offset + 2, permissions); |
| 258 | put_u32(bytes, offset + 8, payload_offset); |
| 259 | put_u32(bytes, offset + 12, file_length); |
| 260 | put_u32(bytes, offset + 16, memory_length); |
| 261 | put_u32(bytes, offset + 20, PAGE_SIZE); |
| 262 | } |
| 263 | |
| 264 | fn encode_resources(bytes: &mut [u8], offset: usize, value: &ResourceManifest) { |
| 265 | put_u32(bytes, offset, value.code_pages); |
| 266 | put_u32(bytes, offset + 4, value.data_pages); |
| 267 | put_u32(bytes, offset + 8, value.stack_pages); |
| 268 | put_u16(bytes, offset + 12, value.max_capabilities); |
| 269 | put_u32(bytes, offset + 16, value.ipc_messages); |
| 270 | put_u32(bytes, offset + 20, value.ipc_bytes); |
| 271 | put_u64(bytes, offset + 24, value.cpu_ticks); |
| 272 | put_u64(bytes, offset + 32, value.elapsed_ticks); |
| 273 | put_u32(bytes, offset + 40, value.syscall_count); |
| 274 | } |
| 275 | |
| 276 | fn sign_file(input: &Path, output: &Path) -> Result<(), Box<dyn Error>> { |
| 277 | #[cfg(feature = "seed0b-test-signing")] |
| 278 | { |
| 279 | let mut bytes = fs::read(input)?; |
| 280 | let artifact = tool_artifact(Artifact::parse(&bytes))?; |
| 281 | let id = tool_artifact(parse_and_identify(&bytes))?.artifact_id; |
| 282 | let signing_key = SigningKey::from_bytes(&test_only_seed()); |
| 283 | let fingerprint = signer_fingerprint(&signing_key.verifying_key().to_bytes()); |
| 284 | let signature = signing_key.sign(&artifact_signature_message(&id)); |
| 285 | let start = artifact.unsigned_bytes.len(); |
| 286 | bytes[start + 4..start + 36].copy_from_slice(&fingerprint); |
| 287 | bytes[start + 36..start + 100].copy_from_slice(&signature.to_bytes()); |
| 288 | tool_artifact(Artifact::parse(&bytes))?; |
| 289 | fs::write(output, bytes)?; |
| 290 | println!("SIGN: TEST ONLY SEED-0B QUALIFICATION IDENTITY"); |
| 291 | println!("SIGNER_FINGERPRINT: {}", hex(&fingerprint)); |
| 292 | Ok(()) |
| 293 | } |
| 294 | #[cfg(not(feature = "seed0b-test-signing"))] |
| 295 | { |
| 296 | let _ = (input, output); |
| 297 | Err("signing requires explicit feature seed0b-test-signing; production signing is not available".into()) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | fn verify_file(bytes: &[u8]) -> Result<(), Box<dyn Error>> { |
| 302 | #[cfg(feature = "seed0b-test-signing")] |
| 303 | { |
| 304 | use aienos_artifact::signature::seed0b_test_anchor::Seed0bTestAnchorSet; |
| 305 | let anchors = Seed0bTestAnchorSet; |
| 306 | let verifier = ConfiguredArtifactVerifier::new(&anchors, Ed25519Verifier); |
| 307 | let verified = tool_artifact(verifier.verify(bytes))?; |
| 308 | println!("VERIFY: PASS"); |
| 309 | println!( |
| 310 | "ARTIFACT_ID: {}", |
| 311 | hex(verified.identified().artifact_id.as_bytes()) |
| 312 | ); |
| 313 | println!( |
| 314 | "PAYLOAD_SHA256: {}", |
| 315 | hex(&verified.identified().payload_digest) |
| 316 | ); |
| 317 | println!("SIGNER_FINGERPRINT: {}", hex(verified.signer_fingerprint())); |
| 318 | println!("TRUST_TIER: SEED-0B-QUALIFICATION-TEST-ONLY"); |
| 319 | Ok(()) |
| 320 | } |
| 321 | #[cfg(not(feature = "seed0b-test-signing"))] |
| 322 | { |
| 323 | let anchors = ProductionTrustAnchorSet::default(); |
| 324 | let verifier = ConfiguredArtifactVerifier::new(&anchors, Ed25519Verifier); |
| 325 | let _ = tool_artifact(verifier.verify(bytes))?; |
| 326 | Ok(()) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | fn inspect(bytes: &[u8]) -> Result<(), Box<dyn Error>> { |
| 331 | let identified = tool_artifact(parse_and_identify(bytes))?; |
| 332 | let artifact = identified.artifact; |
| 333 | println!("magic: AIENART\\0"); |
| 334 | println!("format_version: {}", read_u16(bytes, 8)); |
| 335 | println!("header_size: {}", read_u16(bytes, 10)); |
| 336 | println!( |
| 337 | "target_arch: {} (AArch64 little-endian)", |
| 338 | read_u16(bytes, 12) |
| 339 | ); |
| 340 | println!("abi_version: {}", read_u16(bytes, 14)); |
| 341 | println!("flags: {:#x}", read_u32(bytes, 16)); |
| 342 | println!("total_length: {}", read_u32(bytes, 20)); |
| 343 | println!("section_table_offset: {}", read_u32(bytes, 24)); |
| 344 | println!("section_count: {}", read_u16(bytes, 28)); |
| 345 | println!("section_entry_size: {}", read_u16(bytes, 30)); |
| 346 | println!("entry_section_index: {}", read_u16(bytes, 32)); |
| 347 | println!("header_reserved_34: {}", read_u16(bytes, 34)); |
| 348 | println!("entry_offset: {}", read_u32(bytes, 36)); |
| 349 | println!("capability_table_offset: {}", read_u32(bytes, 40)); |
| 350 | println!("capability_count: {}", read_u16(bytes, 44)); |
| 351 | println!("capability_entry_size: {}", read_u16(bytes, 46)); |
| 352 | println!("resource_envelope_offset: {}", read_u32(bytes, 48)); |
| 353 | println!("resource_envelope_size: {}", read_u16(bytes, 52)); |
| 354 | println!("header_reserved_54: {}", read_u16(bytes, 54)); |
| 355 | println!("payload_offset: {}", read_u32(bytes, 56)); |
| 356 | println!("payload_length: {}", read_u32(bytes, 60)); |
| 357 | println!("signature_offset: {}", read_u32(bytes, 64)); |
| 358 | println!("signature_length: {}", read_u16(bytes, 68)); |
| 359 | println!("signature_algorithm: {}", read_u16(bytes, 70)); |
| 360 | println!("header_reserved_72_127: {}", hex(&bytes[72..128])); |
| 361 | println!("artifact_id: {}", hex(identified.artifact_id.as_bytes())); |
| 362 | println!("payload_sha256: {}", hex(&payload_digest(&artifact))); |
| 363 | for index in 0..2 { |
| 364 | let offset = SECTION_TABLE_OFFSET + index * SECTION_RECORD_SIZE; |
| 365 | println!("section[{index}]: kind={} permissions={} reserved_4={} payload_relative_offset={} file_length={} memory_length={} alignment={} reserved_24={}", read_u16(bytes, offset), read_u16(bytes, offset + 2), read_u32(bytes, offset + 4), read_u32(bytes, offset + 8), read_u32(bytes, offset + 12), read_u32(bytes, offset + 16), read_u32(bytes, offset + 20), read_u64(bytes, offset + 24)); |
| 366 | } |
| 367 | for (index, cap) in artifact.capabilities.as_slice().iter().enumerate() { |
| 368 | let offset = CAPABILITY_TABLE_OFFSET + index * CAPABILITY_RECORD_SIZE; |
| 369 | println!("capability[{index}]: kind={} flags={} resource={} rights={:#x} bounds={} ops={} reserved_20={} bytes={} offset={} length={}", cap.resource_kind, read_u16(bytes, offset + 2), cap.resource_id, cap.rights, cap.bounds_kind, cap.max_operations, read_u32(bytes, offset + 20), cap.max_bytes, cap.byte_offset, cap.byte_length); |
| 370 | } |
| 371 | let resources = artifact.resources; |
| 372 | println!("resource_envelope: code_pages={} data_pages={} stack_pages={} max_capabilities={} reserved_14={} ipc_messages={} ipc_bytes={} cpu_ticks={} elapsed_ticks={} syscall_count={} reserved_44={}", resources.code_pages, resources.data_pages, resources.stack_pages, resources.max_capabilities, read_u16(bytes, read_u32(bytes, 48) as usize + 14), resources.ipc_messages, resources.ipc_bytes, resources.cpu_ticks, resources.elapsed_ticks, resources.syscall_count, read_u32(bytes, read_u32(bytes, 48) as usize + 44)); |
| 373 | println!( |
| 374 | "signature_block_algorithm: {}", |
| 375 | u16::from_le_bytes([ |
| 376 | bytes[artifact.unsigned_bytes.len()], |
| 377 | bytes[artifact.unsigned_bytes.len() + 1] |
| 378 | ]) |
| 379 | ); |
| 380 | println!( |
| 381 | "signature_block_reserved: {}", |
| 382 | u16::from_le_bytes([ |
| 383 | bytes[artifact.unsigned_bytes.len() + 2], |
| 384 | bytes[artifact.unsigned_bytes.len() + 3] |
| 385 | ]) |
| 386 | ); |
| 387 | println!("signer_fingerprint: {}", hex(artifact.signer_fingerprint)); |
| 388 | println!("signature: {}", hex(artifact.signature)); |
| 389 | Ok(()) |
| 390 | } |
| 391 | |
| 392 | fn read_u16(bytes: &[u8], offset: usize) -> u16 { |
| 393 | u16::from_le_bytes([bytes[offset], bytes[offset + 1]]) |
| 394 | } |
| 395 | |
| 396 | fn read_u32(bytes: &[u8], offset: usize) -> u32 { |
| 397 | u32::from_le_bytes( |
| 398 | bytes[offset..offset + 4] |
| 399 | .try_into() |
| 400 | .expect("parsed field bounds"), |
| 401 | ) |
| 402 | } |
| 403 | |
| 404 | fn read_u64(bytes: &[u8], offset: usize) -> u64 { |
| 405 | u64::from_le_bytes( |
| 406 | bytes[offset..offset + 8] |
| 407 | .try_into() |
| 408 | .expect("parsed field bounds"), |
| 409 | ) |
| 410 | } |
| 411 | |
| 412 | fn align_up_16(value: usize) -> Result<usize, Box<dyn Error>> { |
| 413 | Ok(value.checked_add(15).ok_or("alignment overflow")? & !15) |
| 414 | } |
| 415 | |
| 416 | fn tool_artifact<T>( |
| 417 | result: Result<T, aienos_artifact::ArtifactError>, |
| 418 | ) -> Result<T, Box<dyn Error>> { |
| 419 | result.map_err(|error| format!("artifact rejected: {error:?}").into()) |
| 420 | } |
| 421 | |
| 422 | fn put_u16(bytes: &mut [u8], offset: usize, value: u16) { |
| 423 | bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes()); |
| 424 | } |
| 425 | |
| 426 | fn put_u32(bytes: &mut [u8], offset: usize, value: u32) { |
| 427 | bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes()); |
| 428 | } |
| 429 | |
| 430 | fn put_u64(bytes: &mut [u8], offset: usize, value: u64) { |
| 431 | bytes[offset..offset + 8].copy_from_slice(&value.to_le_bytes()); |
| 432 | } |
| 433 | |
| 434 | fn hex(bytes: &[u8]) -> String { |
| 435 | use std::fmt::Write; |
| 436 | let mut value = String::with_capacity(bytes.len() * 2); |
| 437 | for byte in bytes { |
| 438 | let _ = write!(&mut value, "{byte:02x}"); |
| 439 | } |
| 440 | value |
| 441 | } |
| 442 | |
| 443 | #[cfg(feature = "seed0b-test-signing")] |
| 444 | fn test_only_seed() -> [u8; 32] { |
| 445 | // RFC 8032 test-vector seed. Test fixture only; never use as a secret. |
| 446 | [ |
| 447 | 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, |
| 448 | 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, |
| 449 | 0x7f, 0x60, |
| 450 | ] |
| 451 | } |
| 452 | |
| 453 | #[cfg(test)] |
| 454 | mod tests { |
| 455 | use super::*; |
| 456 | |
| 457 | #[test] |
| 458 | fn deterministic_pack_uses_the_shared_parser_and_id() { |
| 459 | let manifest: Manifest = serde_json::from_str( |
| 460 | r#"{ |
| 461 | "entry_offset": 0, |
| 462 | "capabilities": [{ |
| 463 | "resource_kind": 3, "resource_id": 7, "rights": 1, |
| 464 | "bounds_kind": 1, "max_operations": 4, "max_bytes": 4, |
| 465 | "byte_offset": 0, "byte_length": 4 |
| 466 | }], |
| 467 | "resources": { |
| 468 | "code_pages": 1, "data_pages": 1, "stack_pages": 1, |
| 469 | "max_capabilities": 1, "ipc_messages": 2, "ipc_bytes": 16, |
| 470 | "cpu_ticks": 1000, "elapsed_ticks": 2000, "syscall_count": 16 |
| 471 | } |
| 472 | }"#, |
| 473 | ) |
| 474 | .unwrap(); |
| 475 | let code = [0xc0, 0x03, 0x5f, 0xd6]; |
| 476 | let data = *b"DATA"; |
| 477 | let first = pack_bytes(&manifest, &code, &data).unwrap(); |
| 478 | let second = pack_bytes(&manifest, &code, &data).unwrap(); |
| 479 | assert_eq!(first, second); |
| 480 | let id = parse_and_identify(&first).unwrap().artifact_id; |
| 481 | assert_eq!( |
| 482 | hex(id.as_bytes()), |
| 483 | "71976b5c38ec15f2307565ed3eea51fde4a5375970cd49338d6e16166238cf4b" |
| 484 | ); |
| 485 | } |
| 486 | |
| 487 | #[cfg(feature = "seed0b-test-signing")] |
| 488 | #[test] |
| 489 | fn test_signer_roundtrips_through_shared_verifier() { |
| 490 | use aienos_artifact::signature::seed0b_test_anchor::Seed0bTestAnchorSet; |
| 491 | |
| 492 | let manifest: Manifest = serde_json::from_str( |
| 493 | r#"{"entry_offset":0,"capabilities":[],"resources":{"code_pages":1,"data_pages":1,"stack_pages":1,"max_capabilities":0,"ipc_messages":0,"ipc_bytes":0,"cpu_ticks":1,"elapsed_ticks":1,"syscall_count":1}}"#, |
| 494 | ) |
| 495 | .unwrap(); |
| 496 | let bytes = pack_bytes(&manifest, &[0xc0, 0x03, 0x5f, 0xd6], b"DATA").unwrap(); |
| 497 | let id = parse_and_identify(&bytes).unwrap().artifact_id; |
| 498 | let key = SigningKey::from_bytes(&test_only_seed()); |
| 499 | let signature = key.sign(&artifact_signature_message(&id)); |
| 500 | let mut signed = bytes; |
| 501 | let start = signed.len() - SIGNATURE_BLOCK_SIZE; |
| 502 | let fingerprint = signer_fingerprint(&key.verifying_key().to_bytes()); |
| 503 | signed[start + 4..start + 36].copy_from_slice(&fingerprint); |
| 504 | signed[start + 36..].copy_from_slice(&signature.to_bytes()); |
| 505 | |
| 506 | let anchors = Seed0bTestAnchorSet; |
| 507 | let verifier = ConfiguredArtifactVerifier::new(&anchors, Ed25519Verifier); |
| 508 | assert!(verifier.verify(&signed).is_ok()); |
| 509 | } |
| 510 | } |