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-evidence/src/canon.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-evidence / src / canon.rs` 108 lines ยท 3565 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 //! Canonical JSON and the bundle checksum. 17 //! 18 //! The checksum covers a canonical encoding (object keys sorted, no 19 //! insignificant whitespace) of the bundle without its checksum field, so it 20 //! does not depend on how the file is pretty-printed or on serde_json's map 21 //! ordering features. 22 23 use aienos_kernel::crypto::sha256; 24 use serde_json::Value; 25 26 pub const CHECKSUM_FIELD: &str = "bundle_checksum_sha256"; 27 28 pub fn canonical(value: &Value) -> String { 29 let mut out = String::new(); 30 write(value, &mut out); 31 out 32 } 33 34 fn write(value: &Value, out: &mut String) { 35 match value { 36 Value::Object(map) => { 37 let mut keys: Vec<&String> = map.keys().collect(); 38 keys.sort(); 39 out.push('{'); 40 for (i, key) in keys.iter().enumerate() { 41 if i > 0 { 42 out.push(','); 43 } 44 out.push_str(&Value::String((*key).clone()).to_string()); 45 out.push(':'); 46 write(&map[*key], out); 47 } 48 out.push('}'); 49 } 50 Value::Array(items) => { 51 out.push('['); 52 for (i, item) in items.iter().enumerate() { 53 if i > 0 { 54 out.push(','); 55 } 56 write(item, out); 57 } 58 out.push(']'); 59 } 60 scalar => out.push_str(&scalar.to_string()), 61 } 62 } 63 64 pub fn hex(bytes: &[u8]) -> String { 65 bytes.iter().map(|b| format!("{b:02x}")).collect() 66 } 67 68 /// SHA-256 of the canonical bundle with the checksum field removed. 69 pub fn checksum(bundle: &Value) -> String { 70 let mut body = bundle.clone(); 71 if let Value::Object(map) = &mut body { 72 map.remove(CHECKSUM_FIELD); 73 } 74 hex(&sha256::hash(canonical(&body).as_bytes())) 75 } 76 77 /// Returns the bundle with a fresh checksum field. 78 pub fn seal(mut bundle: Value) -> Value { 79 let sum = checksum(&bundle); 80 if let Value::Object(map) = &mut bundle { 81 map.insert(CHECKSUM_FIELD.into(), Value::String(sum)); 82 } 83 bundle 84 } 85 86 #[cfg(test)] 87 mod tests { 88 use super::*; 89 use serde_json::json; 90 91 #[test] 92 fn key_order_and_whitespace_do_not_change_the_checksum() { 93 let a: Value = serde_json::from_str(r#"{"b": 1, "a": {"y": [1, 2], "x": "s"}}"#).unwrap(); 94 let b: Value = serde_json::from_str(r#"{"a":{"x":"s","y":[1,2]},"b":1}"#).unwrap(); 95 assert_eq!(canonical(&a), r#"{"a":{"x":"s","y":[1,2]},"b":1}"#); 96 assert_eq!(checksum(&a), checksum(&b)); 97 } 98 99 #[test] 100 fn sealed_bundle_verifies_and_any_edit_breaks_it() { 101 let sealed = seal(json!({"schema_version": "3.0.0", "value": 7})); 102 let stored = sealed[CHECKSUM_FIELD].as_str().unwrap().to_string(); 103 assert_eq!(stored, checksum(&sealed)); 104 let mut edited = sealed.clone(); 105 edited["value"] = json!(8); 106 assert_ne!(stored, checksum(&edited)); 107 } 108 }