ruvix-boot 0.1.0

RVF boot loading for RuVix Cognition Kernel (ADR-087)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Boot attestation types and utilities.
//!
//! Boot attestation records the initial system state:
//! - RVF package hash
//! - Capability table hash
//! - Region layout hash
//! - Boot timestamp

use sha2::{Sha256, Digest};

/// Boot attestation entry recorded as the first witness log entry.
///
/// Contains cryptographic hashes of the initial system state
/// for later verification and audit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct BootAttestation {
    /// SHA-256 hash of the RVF package that was booted.
    pub rvf_hash: [u8; 32],

    /// SHA-256 hash of the initial capability table.
    pub capability_table_hash: [u8; 32],

    /// SHA-256 hash of the region layout.
    pub region_layout_hash: [u8; 32],

    /// Boot timestamp in nanoseconds since UNIX epoch.
    pub boot_timestamp_ns: u64,

    /// Boot sequence number (for multi-boot detection).
    pub boot_sequence: u64,

    /// Platform identifier.
    pub platform_id: u64,

    /// Reserved for future use.
    pub reserved: [u8; 16],
}

impl BootAttestation {
    /// Size of boot attestation in bytes.
    pub const SIZE: usize = 32 + 32 + 32 + 8 + 8 + 8 + 16; // 136 bytes

    /// Creates a new boot attestation.
    #[must_use]
    pub fn new(
        rvf_hash: [u8; 32],
        capability_table_hash: [u8; 32],
        region_layout_hash: [u8; 32],
        boot_timestamp_ns: u64,
    ) -> Self {
        Self {
            rvf_hash,
            capability_table_hash,
            region_layout_hash,
            boot_timestamp_ns,
            boot_sequence: 0,
            platform_id: 0,
            reserved: [0u8; 16],
        }
    }

    /// Creates a boot attestation with full metadata.
    #[must_use]
    pub fn with_metadata(
        rvf_hash: [u8; 32],
        capability_table_hash: [u8; 32],
        region_layout_hash: [u8; 32],
        boot_timestamp_ns: u64,
        boot_sequence: u64,
        platform_id: u64,
    ) -> Self {
        Self {
            rvf_hash,
            capability_table_hash,
            region_layout_hash,
            boot_timestamp_ns,
            boot_sequence,
            platform_id,
            reserved: [0u8; 16],
        }
    }

    /// Computes the hash of this attestation.
    #[must_use]
    pub fn hash(&self) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(&self.rvf_hash);
        hasher.update(&self.capability_table_hash);
        hasher.update(&self.region_layout_hash);
        hasher.update(&self.boot_timestamp_ns.to_le_bytes());
        hasher.update(&self.boot_sequence.to_le_bytes());
        hasher.update(&self.platform_id.to_le_bytes());
        hasher.update(&self.reserved);

        let result = hasher.finalize();
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&result);
        hash
    }

    /// Serializes the attestation to bytes.
    #[must_use]
    pub fn to_bytes(&self) -> [u8; Self::SIZE] {
        let mut bytes = [0u8; Self::SIZE];

        bytes[0..32].copy_from_slice(&self.rvf_hash);
        bytes[32..64].copy_from_slice(&self.capability_table_hash);
        bytes[64..96].copy_from_slice(&self.region_layout_hash);
        bytes[96..104].copy_from_slice(&self.boot_timestamp_ns.to_le_bytes());
        bytes[104..112].copy_from_slice(&self.boot_sequence.to_le_bytes());
        bytes[112..120].copy_from_slice(&self.platform_id.to_le_bytes());
        bytes[120..136].copy_from_slice(&self.reserved);

        bytes
    }

    /// Deserializes an attestation from bytes.
    pub fn from_bytes(bytes: &[u8]) -> Option<Self> {
        if bytes.len() < Self::SIZE {
            return None;
        }

        let mut rvf_hash = [0u8; 32];
        rvf_hash.copy_from_slice(&bytes[0..32]);

        let mut capability_table_hash = [0u8; 32];
        capability_table_hash.copy_from_slice(&bytes[32..64]);

        let mut region_layout_hash = [0u8; 32];
        region_layout_hash.copy_from_slice(&bytes[64..96]);

        let boot_timestamp_ns = u64::from_le_bytes([
            bytes[96], bytes[97], bytes[98], bytes[99],
            bytes[100], bytes[101], bytes[102], bytes[103],
        ]);

        let boot_sequence = u64::from_le_bytes([
            bytes[104], bytes[105], bytes[106], bytes[107],
            bytes[108], bytes[109], bytes[110], bytes[111],
        ]);

        let platform_id = u64::from_le_bytes([
            bytes[112], bytes[113], bytes[114], bytes[115],
            bytes[116], bytes[117], bytes[118], bytes[119],
        ]);

        let mut reserved = [0u8; 16];
        reserved.copy_from_slice(&bytes[120..136]);

        Some(Self {
            rvf_hash,
            capability_table_hash,
            region_layout_hash,
            boot_timestamp_ns,
            boot_sequence,
            platform_id,
            reserved,
        })
    }

    /// Verifies that this attestation matches expected values.
    #[must_use]
    pub fn verify(&self, expected_rvf_hash: &[u8; 32]) -> bool {
        self.rvf_hash == *expected_rvf_hash
    }
}

impl Default for BootAttestation {
    fn default() -> Self {
        Self {
            rvf_hash: [0u8; 32],
            capability_table_hash: [0u8; 32],
            region_layout_hash: [0u8; 32],
            boot_timestamp_ns: 0,
            boot_sequence: 0,
            platform_id: 0,
            reserved: [0u8; 16],
        }
    }
}

/// Witness log entry for attestation.
///
/// Used for entries other than boot attestation that record
/// proof-gated mutations and other security events.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct AttestationEntry {
    /// Entry type identifier.
    pub entry_type: AttestationEntryType,

    /// Hash of the attested data.
    pub data_hash: [u8; 32],

    /// Timestamp in nanoseconds since UNIX epoch.
    pub timestamp_ns: u64,

    /// Task that generated this attestation.
    pub task_id: u32,

    /// Component that generated this attestation.
    pub component_id: u32,

    /// Additional flags.
    pub flags: AttestationFlags,

    /// Reserved for future use.
    pub reserved: [u8; 8],
}

/// Attestation entry type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum AttestationEntryType {
    /// Proof-gated vector mutation.
    VectorMutation = 0,

    /// Proof-gated graph mutation.
    GraphMutation = 1,

    /// Capability delegation.
    CapabilityDelegate = 2,

    /// Capability revocation.
    CapabilityRevoke = 3,

    /// Component state checkpoint.
    Checkpoint = 4,

    /// Component state rollback.
    Rollback = 5,

    /// Custom application-defined.
    Custom = 255,
}

/// Attestation entry flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(transparent)]
pub struct AttestationFlags(pub u32);

impl AttestationFlags {
    /// No flags.
    pub const NONE: Self = Self(0);

    /// Entry contains high-priority mutation.
    pub const HIGH_PRIORITY: Self = Self(1 << 0);

    /// Entry was generated by a deadline-driven task.
    pub const DEADLINE_DRIVEN: Self = Self(1 << 1);

    /// Entry required Deep proof tier.
    pub const DEEP_PROOF: Self = Self(1 << 2);

    /// Entry was generated during rollback.
    pub const DURING_ROLLBACK: Self = Self(1 << 3);

    /// Checks if a flag is set.
    #[inline]
    #[must_use]
    pub const fn contains(&self, flag: Self) -> bool {
        (self.0 & flag.0) != 0
    }

    /// Returns the union of two flag sets.
    #[inline]
    #[must_use]
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

impl AttestationEntry {
    /// Size of attestation entry in bytes.
    pub const SIZE: usize = 1 + 32 + 8 + 4 + 4 + 4 + 8; // 61 bytes

    /// Creates a new attestation entry.
    #[must_use]
    pub fn new(
        entry_type: AttestationEntryType,
        data_hash: [u8; 32],
        timestamp_ns: u64,
        task_id: u32,
        component_id: u32,
    ) -> Self {
        Self {
            entry_type,
            data_hash,
            timestamp_ns,
            task_id,
            component_id,
            flags: AttestationFlags::NONE,
            reserved: [0u8; 8],
        }
    }

    /// Creates an attestation entry with flags.
    #[must_use]
    pub fn with_flags(
        entry_type: AttestationEntryType,
        data_hash: [u8; 32],
        timestamp_ns: u64,
        task_id: u32,
        component_id: u32,
        flags: AttestationFlags,
    ) -> Self {
        Self {
            entry_type,
            data_hash,
            timestamp_ns,
            task_id,
            component_id,
            flags,
            reserved: [0u8; 8],
        }
    }

    /// Computes the hash of this entry.
    #[must_use]
    pub fn hash(&self) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(&[self.entry_type as u8]);
        hasher.update(&self.data_hash);
        hasher.update(&self.timestamp_ns.to_le_bytes());
        hasher.update(&self.task_id.to_le_bytes());
        hasher.update(&self.component_id.to_le_bytes());
        hasher.update(&self.flags.0.to_le_bytes());
        hasher.update(&self.reserved);

        let result = hasher.finalize();
        let mut hash = [0u8; 32];
        hash.copy_from_slice(&result);
        hash
    }
}

impl Default for AttestationEntry {
    fn default() -> Self {
        Self {
            entry_type: AttestationEntryType::Custom,
            data_hash: [0u8; 32],
            timestamp_ns: 0,
            task_id: 0,
            component_id: 0,
            flags: AttestationFlags::NONE,
            reserved: [0u8; 8],
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_boot_attestation_creation() {
        let att = BootAttestation::new(
            [1u8; 32],
            [2u8; 32],
            [3u8; 32],
            1234567890,
        );

        assert_eq!(att.rvf_hash, [1u8; 32]);
        assert_eq!(att.capability_table_hash, [2u8; 32]);
        assert_eq!(att.region_layout_hash, [3u8; 32]);
        assert_eq!(att.boot_timestamp_ns, 1234567890);
    }

    #[test]
    fn test_boot_attestation_serialization() {
        let att = BootAttestation::new(
            [0xAA; 32],
            [0xBB; 32],
            [0xCC; 32],
            999999999,
        );

        let bytes = att.to_bytes();
        let recovered = BootAttestation::from_bytes(&bytes).unwrap();

        assert_eq!(att, recovered);
    }

    #[test]
    fn test_boot_attestation_hash() {
        let att1 = BootAttestation::new([1u8; 32], [2u8; 32], [3u8; 32], 100);
        let att2 = BootAttestation::new([1u8; 32], [2u8; 32], [3u8; 32], 100);
        let att3 = BootAttestation::new([1u8; 32], [2u8; 32], [4u8; 32], 100);

        // Same inputs = same hash
        assert_eq!(att1.hash(), att2.hash());

        // Different inputs = different hash
        assert_ne!(att1.hash(), att3.hash());
    }

    #[test]
    fn test_boot_attestation_verify() {
        let expected_hash = [0xDEu8; 32];
        let att = BootAttestation::new(expected_hash, [0u8; 32], [0u8; 32], 0);

        assert!(att.verify(&expected_hash));
        assert!(!att.verify(&[0u8; 32]));
    }

    #[test]
    fn test_attestation_entry_creation() {
        let entry = AttestationEntry::new(
            AttestationEntryType::VectorMutation,
            [0xAB; 32],
            1234567890,
            1,
            2,
        );

        assert_eq!(entry.entry_type, AttestationEntryType::VectorMutation);
        assert_eq!(entry.task_id, 1);
        assert_eq!(entry.component_id, 2);
    }

    #[test]
    fn test_attestation_flags() {
        let flags = AttestationFlags::HIGH_PRIORITY.union(AttestationFlags::DEEP_PROOF);

        assert!(flags.contains(AttestationFlags::HIGH_PRIORITY));
        assert!(flags.contains(AttestationFlags::DEEP_PROOF));
        assert!(!flags.contains(AttestationFlags::DEADLINE_DRIVEN));
    }

    #[test]
    fn test_boot_attestation_size() {
        assert_eq!(BootAttestation::SIZE, 136);
    }
}