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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Witness log implementation for boot attestation.
//!
//! The witness log is an append-only region that records:
//! - Boot attestations (first entry)
//! - Proof attestations during runtime
//! - Capability mutations
//! - Other security-relevant events
//!
//! # Integrity
//!
//! Entries are hash-chained for integrity verification.
//! Each entry contains:
//! - Previous entry hash (32 bytes)
//! - Entry type (1 byte)
//! - Timestamp (8 bytes)
//! - Payload (variable)

use crate::attestation::BootAttestation;
use crate::manifest::WitnessLogPolicy;
use ruvix_types::{KernelError, ProofAttestation};
use sha2::{Sha256, Digest};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// Witness log entry type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum WitnessLogEntryType {
    /// Boot attestation (first entry).
    BootAttestation = 0,

    /// Proof attestation during runtime.
    ProofAttestation = 1,

    /// Capability grant.
    CapabilityGrant = 2,

    /// Capability revoke.
    CapabilityRevoke = 3,

    /// Component mount.
    ComponentMount = 4,

    /// Component unmount.
    ComponentUnmount = 5,

    /// Region creation.
    RegionCreate = 6,

    /// Region destruction.
    RegionDestroy = 7,

    /// Checkpoint marker.
    Checkpoint = 8,

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

/// Witness log entry header.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub struct WitnessLogEntryHeader {
    /// Hash of the previous entry (chain link).
    pub prev_hash: [u8; 32],

    /// Entry type.
    pub entry_type: WitnessLogEntryType,

    /// Entry sequence number.
    pub sequence: u64,

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

    /// Payload size in bytes.
    pub payload_size: u32,
}

impl WitnessLogEntryHeader {
    /// Size of the header in bytes.
    pub const SIZE: usize = 32 + 1 + 8 + 8 + 4; // 53 bytes

    /// Creates a new entry header.
    #[must_use]
    pub fn new(
        prev_hash: [u8; 32],
        entry_type: WitnessLogEntryType,
        sequence: u64,
        timestamp_ns: u64,
        payload_size: u32,
    ) -> Self {
        Self {
            prev_hash,
            entry_type,
            sequence,
            timestamp_ns,
            payload_size,
        }
    }

    /// Computes the hash of this header.
    #[must_use]
    pub fn hash(&self) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(&self.prev_hash);
        hasher.update(&[self.entry_type as u8]);
        hasher.update(&self.sequence.to_le_bytes());
        hasher.update(&self.timestamp_ns.to_le_bytes());
        hasher.update(&self.payload_size.to_le_bytes());

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

/// Complete witness log entry (header + payload).
#[derive(Debug, Clone)]
pub struct WitnessLogEntry {
    /// Entry header.
    pub header: WitnessLogEntryHeader,

    /// Entry payload.
    #[cfg(feature = "alloc")]
    pub payload: Vec<u8>,
    /// Entry payload (fixed-size no_std variant).
    #[cfg(not(feature = "alloc"))]
    pub payload: [u8; 256],
    /// Payload length in the fixed-size array.
    #[cfg(not(feature = "alloc"))]
    pub payload_len: usize,
}

impl WitnessLogEntry {
    /// Creates a new witness log entry.
    #[cfg(feature = "alloc")]
    pub fn new(header: WitnessLogEntryHeader, payload: Vec<u8>) -> Self {
        Self { header, payload }
    }

    /// Creates a new witness log entry (no_std).
    #[cfg(not(feature = "alloc"))]
    pub fn new(header: WitnessLogEntryHeader, payload_data: &[u8]) -> Self {
        let mut payload = [0u8; 256];
        let len = payload_data.len().min(256);
        payload[..len].copy_from_slice(&payload_data[..len]);

        Self {
            header,
            payload,
            payload_len: len,
        }
    }

    /// Computes the hash of the entire entry (header + payload).
    #[must_use]
    pub fn hash(&self) -> [u8; 32] {
        let mut hasher = Sha256::new();
        hasher.update(&self.header.prev_hash);
        hasher.update(&[self.header.entry_type as u8]);
        hasher.update(&self.header.sequence.to_le_bytes());
        hasher.update(&self.header.timestamp_ns.to_le_bytes());
        hasher.update(&self.header.payload_size.to_le_bytes());

        #[cfg(feature = "alloc")]
        hasher.update(&self.payload);
        #[cfg(not(feature = "alloc"))]
        hasher.update(&self.payload[..self.payload_len]);

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

    /// Returns the payload as a slice.
    #[must_use]
    pub fn payload(&self) -> &[u8] {
        #[cfg(feature = "alloc")]
        {
            &self.payload
        }
        #[cfg(not(feature = "alloc"))]
        {
            &self.payload[..self.payload_len]
        }
    }
}

/// Witness log configuration.
#[derive(Debug, Clone, Copy)]
pub struct WitnessLogConfig {
    /// Maximum entries before rotation.
    pub max_entries: u64,

    /// Maximum size in bytes.
    pub max_size_bytes: u64,

    /// Whether to hash-chain entries.
    pub hash_chain: bool,

    /// Retention period in seconds (0 = forever).
    pub retention_seconds: u64,
}

impl WitnessLogConfig {
    /// Default witness log configuration.
    pub const DEFAULT: Self = Self {
        max_entries: 1_000_000,
        max_size_bytes: 100 * 1024 * 1024, // 100 MiB
        hash_chain: true,
        retention_seconds: 0,
    };

    /// Creates a configuration from a witness log policy.
    #[must_use]
    pub fn from_policy(policy: &WitnessLogPolicy) -> Self {
        Self {
            max_entries: policy.max_entries,
            max_size_bytes: policy.max_size_bytes,
            hash_chain: policy.hash_chain,
            retention_seconds: policy.retention_seconds,
        }
    }
}

impl Default for WitnessLogConfig {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// Witness log for boot and runtime attestation.
///
/// The witness log is append-only and hash-chained for integrity.
#[derive(Debug)]
pub struct WitnessLog {
    /// Configuration.
    config: WitnessLogConfig,

    /// Current entry count.
    entry_count: u64,

    /// Current size in bytes.
    size_bytes: u64,

    /// Hash of the last entry (for chaining).
    last_hash: [u8; 32],

    /// Entries (in-memory for Phase A).
    #[cfg(feature = "alloc")]
    entries: Vec<WitnessLogEntry>,
}

impl WitnessLog {
    /// Creates a new witness log.
    #[must_use]
    pub fn new(config: WitnessLogConfig) -> Self {
        Self {
            config,
            entry_count: 0,
            size_bytes: 0,
            last_hash: [0u8; 32], // Genesis hash is all zeros
            #[cfg(feature = "alloc")]
            entries: Vec::new(),
        }
    }

    /// Returns the current entry count.
    #[inline]
    #[must_use]
    pub fn entry_count(&self) -> u64 {
        self.entry_count
    }

    /// Returns the current size in bytes.
    #[inline]
    #[must_use]
    pub fn size_bytes(&self) -> u64 {
        self.size_bytes
    }

    /// Returns the hash of the last entry.
    #[inline]
    #[must_use]
    pub fn last_hash(&self) -> [u8; 32] {
        self.last_hash
    }

    /// Appends a boot attestation entry.
    ///
    /// This should be the first entry in the witness log.
    pub fn append_boot_attestation(&mut self, attestation: &BootAttestation) -> Result<(), KernelError> {
        if self.entry_count != 0 {
            // Boot attestation must be first
            return Err(KernelError::NotPermitted);
        }

        let payload = attestation.to_bytes();
        self.append(WitnessLogEntryType::BootAttestation, &payload)
    }

    /// Appends a proof attestation entry.
    pub fn append_proof_attestation(&mut self, attestation: &ProofAttestation) -> Result<(), KernelError> {
        let payload = Self::serialize_proof_attestation(attestation);
        self.append(WitnessLogEntryType::ProofAttestation, &payload)
    }

    /// Appends a generic entry.
    pub fn append(&mut self, entry_type: WitnessLogEntryType, payload: &[u8]) -> Result<(), KernelError> {
        // Check limits
        if self.entry_count >= self.config.max_entries {
            return Err(KernelError::RegionFull);
        }

        let entry_size = WitnessLogEntryHeader::SIZE + payload.len();
        if self.size_bytes + entry_size as u64 > self.config.max_size_bytes {
            return Err(KernelError::RegionFull);
        }

        // Create header
        let header = WitnessLogEntryHeader::new(
            self.last_hash,
            entry_type,
            self.entry_count,
            Self::get_timestamp(),
            payload.len() as u32,
        );

        // Create entry
        #[cfg(feature = "alloc")]
        let entry = WitnessLogEntry::new(header, payload.to_vec());
        #[cfg(not(feature = "alloc"))]
        let entry = WitnessLogEntry::new(header, payload);

        // Update hash chain
        if self.config.hash_chain {
            self.last_hash = entry.hash();
        }

        // Store entry
        #[cfg(feature = "alloc")]
        self.entries.push(entry);

        self.entry_count += 1;
        self.size_bytes += entry_size as u64;

        Ok(())
    }

    /// Gets an entry by sequence number.
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn get_entry(&self, sequence: u64) -> Option<&WitnessLogEntry> {
        self.entries.get(sequence as usize)
    }

    /// Verifies the hash chain integrity.
    #[cfg(feature = "alloc")]
    #[must_use]
    pub fn verify_chain(&self) -> bool {
        if !self.config.hash_chain {
            return true;
        }

        let mut expected_prev = [0u8; 32]; // Genesis

        for entry in &self.entries {
            if entry.header.prev_hash != expected_prev {
                return false;
            }
            expected_prev = entry.hash();
        }

        expected_prev == self.last_hash
    }

    fn serialize_proof_attestation(attestation: &ProofAttestation) -> [u8; 82] {
        let mut bytes = [0u8; 82];

        bytes[0..32].copy_from_slice(&attestation.proof_term_hash);
        bytes[32..64].copy_from_slice(&attestation.environment_hash);
        bytes[64..72].copy_from_slice(&attestation.verification_timestamp_ns.to_le_bytes());
        bytes[72..76].copy_from_slice(&attestation.verifier_version.to_le_bytes());
        bytes[76..80].copy_from_slice(&attestation.reduction_steps.to_le_bytes());
        bytes[80..82].copy_from_slice(&attestation.cache_hit_rate_bps.to_le_bytes());

        bytes
    }

    fn get_timestamp() -> u64 {
        #[cfg(feature = "std")]
        {
            use std::time::{SystemTime, UNIX_EPOCH};
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map(|d| d.as_nanos() as u64)
                .unwrap_or(0)
        }
        #[cfg(not(feature = "std"))]
        {
            0
        }
    }
}

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

    #[test]
    fn test_witness_log_creation() {
        let config = WitnessLogConfig::default();
        let log = WitnessLog::new(config);

        assert_eq!(log.entry_count(), 0);
        assert_eq!(log.size_bytes(), 0);
        assert_eq!(log.last_hash(), [0u8; 32]);
    }

    #[test]
    fn test_boot_attestation_append() {
        let config = WitnessLogConfig::default();
        let mut log = WitnessLog::new(config);

        let attestation = BootAttestation::new(
            [1u8; 32],
            [2u8; 32],
            [3u8; 32],
            1234567890,
        );

        log.append_boot_attestation(&attestation).unwrap();

        assert_eq!(log.entry_count(), 1);
        assert!(log.size_bytes() > 0);
        assert_ne!(log.last_hash(), [0u8; 32]); // Hash should have changed
    }

    #[test]
    fn test_boot_attestation_must_be_first() {
        let config = WitnessLogConfig::default();
        let mut log = WitnessLog::new(config);

        // Add a regular entry first
        log.append(WitnessLogEntryType::Custom, b"test").unwrap();

        // Boot attestation should fail
        let attestation = BootAttestation::new([0u8; 32], [0u8; 32], [0u8; 32], 0);
        let result = log.append_boot_attestation(&attestation);

        assert_eq!(result, Err(KernelError::NotPermitted));
    }

    #[test]
    fn test_witness_log_limit() {
        let config = WitnessLogConfig {
            max_entries: 2,
            max_size_bytes: 1024 * 1024,
            hash_chain: true,
            retention_seconds: 0,
        };
        let mut log = WitnessLog::new(config);

        log.append(WitnessLogEntryType::Custom, b"entry1").unwrap();
        log.append(WitnessLogEntryType::Custom, b"entry2").unwrap();

        // Third entry should fail
        let result = log.append(WitnessLogEntryType::Custom, b"entry3");
        assert_eq!(result, Err(KernelError::RegionFull));
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_hash_chain_verification() {
        let config = WitnessLogConfig::default();
        let mut log = WitnessLog::new(config);

        log.append(WitnessLogEntryType::Custom, b"entry1").unwrap();
        log.append(WitnessLogEntryType::Custom, b"entry2").unwrap();
        log.append(WitnessLogEntryType::Custom, b"entry3").unwrap();

        assert!(log.verify_chain());
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_get_entry() {
        let config = WitnessLogConfig::default();
        let mut log = WitnessLog::new(config);

        log.append(WitnessLogEntryType::Custom, b"hello").unwrap();

        let entry = log.get_entry(0).unwrap();
        assert_eq!(entry.header.entry_type, WitnessLogEntryType::Custom);
        assert_eq!(entry.header.sequence, 0);
        assert_eq!(entry.payload(), b"hello");
    }

    #[test]
    fn test_entry_header_hash() {
        let header = WitnessLogEntryHeader::new(
            [1u8; 32],
            WitnessLogEntryType::Custom,
            42,
            1234567890,
            100,
        );

        let hash1 = header.hash();
        let hash2 = header.hash();

        // Hash should be deterministic
        assert_eq!(hash1, hash2);
        assert_ne!(hash1, [0u8; 32]); // Non-zero
    }
}