libro 0.92.0

Cryptographic audit chain — tamper-proof event logging with hash-linked entries and verification
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
//! Merkle root anchoring to external witness systems.
//!
//! Periodically snapshot a chain's Merkle root and publish it to an external
//! witness system for third-party proof of chain state at a point in time.
//!
//! Libro provides the anchor types and verification logic; consumers implement
//! [`WitnessBackend`] for their specific witness system (transparency log,
//! blockchain, RFC 3161 TSA, file store, etc.).
//!
//! Requires the `anchoring` feature flag.
//!
//! # Usage
//!
//! ```rust,ignore
//! use libro::anchoring::{WitnessAnchor, AnchorVerification};
//! use libro::{AuditChain, MerkleTree, EventSeverity};
//!
//! let mut chain = AuditChain::new();
//! chain.append(EventSeverity::Info, "src", "act", serde_json::json!({}));
//!
//! let tree = MerkleTree::build(chain.entries()).unwrap();
//! let anchor = WitnessAnchor::new(&tree, &chain).unwrap();
//! assert!(anchor.verify_integrity());
//! assert!(anchor.verify_against(&tree, &chain).is_valid());
//! ```

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::chain::AuditChain;
use crate::entry::{constant_time_eq, hash_field};
use crate::hasher::{ChainHasher, HASH_ALGORITHM};
use crate::merkle::MerkleTree;

/// A snapshot of a chain's Merkle root, ready to be anchored to a witness.
///
/// Contains all metadata needed to verify the chain state at the time
/// of anchoring, independent of the witness system used. The anchor is
/// self-hashed for integrity checking.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WitnessAnchor {
    /// Unique identifier for this anchor.
    pub id: Uuid,
    /// The Merkle root hash being anchored (hex-encoded).
    pub merkle_root: String,
    /// Number of entries covered by this Merkle tree.
    pub entry_count: usize,
    /// The chain head hash at the time of anchoring (hex-encoded).
    pub chain_head: String,
    /// Hash algorithm used (e.g., "blake3" or "sha256").
    pub hash_algorithm: String,
    /// When the anchor was created.
    pub created_at: DateTime<Utc>,
    /// Hash of the previous anchor for anchor chaining (meta-chain).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prev_anchor_hash: Option<String>,
    /// This anchor's self-hash (covers all fields above).
    pub hash: String,
}

/// Receipt from a witness backend after publishing an anchor.
///
/// The receipt proves the anchor was accepted by the witness system.
/// Its contents are backend-specific (e.g., a transaction hash for
/// a blockchain, a signed receipt for a transparency log, or an
/// RFC 3161 token for a TSA).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WitnessReceipt {
    /// The anchor ID this receipt is for.
    pub anchor_id: Uuid,
    /// The witness backend identifier (e.g., "rfc3161", "transparency-log", "file").
    pub backend: String,
    /// When the receipt was obtained.
    pub received_at: DateTime<Utc>,
    /// Backend-specific receipt data (opaque to libro).
    ///
    /// Examples:
    /// - Blockchain: `{"tx_hash": "0x...", "block": 12345}`
    /// - TSA: `{"token_der": "3082..."}`
    /// - File: `{"path": "/var/anchors/2024-01-15.json"}`
    pub receipt_data: serde_json::Value,
    /// Optional integrity hash of the receipt data.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub receipt_hash: Option<String>,
}

/// Result of verifying an anchor against a chain or Merkle tree.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum AnchorVerification {
    /// The chain matches the anchor exactly.
    Valid,
    /// The Merkle root does not match.
    RootMismatch { expected: String, actual: String },
    /// The entry count does not match.
    CountMismatch { expected: usize, actual: usize },
    /// The chain head does not match.
    HeadMismatch { expected: String, actual: String },
}

/// Trait for witness backends that can store Merkle root anchors.
///
/// Consumers implement this for their witness system. Libro provides
/// the anchor creation and verification; the backend handles persistence
/// and third-party attestation.
pub trait WitnessBackend: Send + Sync {
    /// Publish an anchor to the witness system.
    ///
    /// Returns a receipt proving the anchor was accepted.
    fn publish(&self, anchor: &WitnessAnchor) -> crate::Result<WitnessReceipt>;

    /// Verify that a receipt is still valid for the given anchor.
    ///
    /// Checks backend-specific validity (e.g., blockchain confirmation,
    /// TSA token signature). Returns `true` if valid.
    fn verify_receipt(
        &self,
        anchor: &WitnessAnchor,
        receipt: &WitnessReceipt,
    ) -> crate::Result<bool>;

    /// A human-readable identifier for this backend (e.g., "rfc3161", "ethereum").
    fn backend_id(&self) -> &str;
}

impl WitnessAnchor {
    /// Create an anchor from a Merkle tree and chain.
    ///
    /// Captures the Merkle root, entry count, and chain head at the
    /// current point in time. Returns `None` if the chain is empty.
    #[must_use]
    pub fn new(tree: &MerkleTree, chain: &AuditChain) -> Option<Self> {
        let chain_head = chain.head_hash()?.to_owned();
        Some(Self::from_tree(tree, chain_head))
    }

    /// Create an anchor from a Merkle tree and explicit chain head.
    ///
    /// Useful when the chain is not directly available (e.g., working
    /// from a [`ChainArchive`](crate::ChainArchive) or store-loaded entries).
    #[must_use]
    pub fn from_tree(tree: &MerkleTree, chain_head: impl Into<String>) -> Self {
        let mut anchor = Self {
            id: Uuid::new_v4(),
            merkle_root: tree.root().to_owned(),
            entry_count: tree.leaf_count(),
            chain_head: chain_head.into(),
            hash_algorithm: HASH_ALGORITHM.to_owned(),
            created_at: Utc::now(),
            prev_anchor_hash: None,
            hash: String::new(),
        };
        anchor.hash = anchor.compute_hash();
        anchor
    }

    /// Chain this anchor to a previous anchor, creating a hash-linked
    /// sequence of anchors (meta-chain). Recomputes the self-hash.
    #[must_use]
    pub fn with_prev_anchor(mut self, prev: &WitnessAnchor) -> Self {
        self.prev_anchor_hash = Some(prev.hash.clone());
        self.hash = self.compute_hash();
        self
    }

    /// Verify that this anchor matches the given Merkle tree and chain.
    #[must_use]
    pub fn verify_against(&self, tree: &MerkleTree, chain: &AuditChain) -> AnchorVerification {
        let actual_head = chain.head_hash().unwrap_or("");
        if !constant_time_eq(&self.chain_head, actual_head) {
            return AnchorVerification::HeadMismatch {
                expected: self.chain_head.clone(),
                actual: actual_head.to_owned(),
            };
        }
        self.verify_against_tree(tree)
    }

    /// Verify that this anchor matches the given Merkle tree.
    ///
    /// Checks root hash and entry count only (no chain head check).
    #[must_use]
    pub fn verify_against_tree(&self, tree: &MerkleTree) -> AnchorVerification {
        if self.entry_count != tree.leaf_count() {
            return AnchorVerification::CountMismatch {
                expected: self.entry_count,
                actual: tree.leaf_count(),
            };
        }
        if !constant_time_eq(&self.merkle_root, tree.root()) {
            return AnchorVerification::RootMismatch {
                expected: self.merkle_root.clone(),
                actual: tree.root().to_owned(),
            };
        }
        AnchorVerification::Valid
    }

    /// Verify the anchor's self-hash integrity.
    #[must_use]
    pub fn verify_integrity(&self) -> bool {
        constant_time_eq(&self.hash, &self.compute_hash())
    }

    fn compute_hash(&self) -> String {
        let mut hasher = ChainHasher::new();
        // Fixed-length field (no prefix needed)
        hasher.update(self.id.as_bytes());
        // Variable-length fields: length-prefixed to prevent boundary ambiguity
        hash_field(&mut hasher, self.merkle_root.as_bytes());
        hasher.update(&self.entry_count.to_le_bytes());
        hash_field(&mut hasher, self.chain_head.as_bytes());
        hash_field(&mut hasher, self.hash_algorithm.as_bytes());
        hash_field(&mut hasher, self.created_at.to_rfc3339().as_bytes());
        hash_field(
            &mut hasher,
            self.prev_anchor_hash.as_deref().unwrap_or("").as_bytes(),
        );
        hasher.finalize_hex()
    }
}

impl WitnessReceipt {
    /// Create a receipt manually (for custom backends).
    pub fn new(
        anchor_id: Uuid,
        backend: impl Into<String>,
        receipt_data: serde_json::Value,
    ) -> Self {
        Self {
            anchor_id,
            backend: backend.into(),
            received_at: Utc::now(),
            receipt_data,
            receipt_hash: None,
        }
    }

    /// Create a receipt with an integrity hash over the receipt data.
    ///
    /// The hash uses length-prefixed fields and canonical JSON (sorted keys)
    /// for deterministic hashing regardless of key insertion order.
    pub fn with_hash(mut self) -> Self {
        let mut hasher = ChainHasher::new();
        hasher.update(self.anchor_id.as_bytes());
        hash_field(&mut hasher, self.backend.as_bytes());
        // Use serde_json::to_string for canonical representation
        // (serde_json preserves insertion order, which is deterministic
        // for receipts created from the same source)
        hash_field(
            &mut hasher,
            serde_json::to_string(&self.receipt_data)
                .unwrap_or_default()
                .as_bytes(),
        );
        self.receipt_hash = Some(hasher.finalize_hex());
        self
    }
}

impl AnchorVerification {
    /// Whether the verification passed.
    #[inline]
    #[must_use]
    pub fn is_valid(&self) -> bool {
        matches!(self, Self::Valid)
    }
}

impl std::fmt::Display for AnchorVerification {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Valid => write!(f, "valid"),
            Self::RootMismatch { expected, actual } => {
                write!(f, "root mismatch: expected {expected}, got {actual}")
            }
            Self::CountMismatch { expected, actual } => {
                write!(f, "count mismatch: expected {expected}, got {actual}")
            }
            Self::HeadMismatch { expected, actual } => {
                write!(f, "head mismatch: expected {expected}, got {actual}")
            }
        }
    }
}

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

    fn make_chain(n: usize) -> AuditChain {
        let mut chain = AuditChain::new();
        for i in 0..n {
            chain.append(
                EventSeverity::Info,
                "s",
                format!("e{i}"),
                serde_json::json!({}),
            );
        }
        chain
    }

    #[test]
    fn anchor_from_chain() {
        let chain = make_chain(5);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        assert_eq!(anchor.merkle_root, tree.root());
        assert_eq!(anchor.entry_count, 5);
        assert_eq!(anchor.chain_head, chain.head_hash().unwrap());
        assert!(!anchor.hash.is_empty());
        assert!(anchor.prev_anchor_hash.is_none());
    }

    #[test]
    fn anchor_none_for_empty_chain() {
        let chain = AuditChain::new();
        assert!(MerkleTree::build(chain.entries()).is_none());
    }

    #[test]
    fn anchor_from_tree() {
        let chain = make_chain(3);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::from_tree(&tree, "explicit_head");

        assert_eq!(anchor.chain_head, "explicit_head");
        assert_eq!(anchor.merkle_root, tree.root());
    }

    #[test]
    fn anchor_verify_against_valid() {
        let chain = make_chain(5);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        assert!(anchor.verify_against(&tree, &chain).is_valid());
    }

    #[test]
    fn anchor_verify_root_mismatch() {
        let chain = make_chain(5);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        // Build a different tree
        let chain2 = make_chain(5); // different UUIDs/timestamps
        let tree2 = MerkleTree::build(chain2.entries()).unwrap();

        let result = anchor.verify_against_tree(&tree2);
        assert!(!result.is_valid());
        assert!(matches!(result, AnchorVerification::RootMismatch { .. }));
    }

    #[test]
    fn anchor_verify_count_mismatch() {
        let chain = make_chain(5);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        // Build a tree with different count
        let chain2 = make_chain(3);
        let tree2 = MerkleTree::build(chain2.entries()).unwrap();

        let result = anchor.verify_against_tree(&tree2);
        assert!(matches!(result, AnchorVerification::CountMismatch { .. }));
    }

    #[test]
    fn anchor_verify_head_mismatch() {
        let chain1 = make_chain(5);
        let tree = MerkleTree::build(chain1.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain1).unwrap();

        // Different chain with different head
        let chain2 = make_chain(5);
        let result = anchor.verify_against(&tree, &chain2);
        assert!(matches!(result, AnchorVerification::HeadMismatch { .. }));
    }

    #[test]
    fn anchor_verify_empty_chain_head_mismatch() {
        let chain = make_chain(3);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        // Verify against an empty chain — should detect head mismatch
        let empty_chain = AuditChain::new();
        let result = anchor.verify_against(&tree, &empty_chain);
        assert!(matches!(result, AnchorVerification::HeadMismatch { .. }));
    }

    #[test]
    fn anchor_field_boundary_ambiguity() {
        // Anchors with shifted field boundaries should have different hashes
        let chain = make_chain(3);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let a1 = WitnessAnchor::from_tree(&tree, "ab");
        let a2 = WitnessAnchor::from_tree(&tree, "abc");
        // Same tree, different chain_head → different hashes
        assert_ne!(a1.hash, a2.hash);
    }

    #[test]
    fn anchor_integrity() {
        let chain = make_chain(3);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        assert!(anchor.verify_integrity());
    }

    #[test]
    fn anchor_integrity_tampered() {
        let chain = make_chain(3);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let mut anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        anchor.merkle_root = "tampered".to_owned();
        assert!(!anchor.verify_integrity());
    }

    #[test]
    fn anchor_chaining() {
        let chain1 = make_chain(3);
        let tree1 = MerkleTree::build(chain1.entries()).unwrap();
        let anchor1 = WitnessAnchor::new(&tree1, &chain1).unwrap();

        let chain2 = make_chain(5);
        let tree2 = MerkleTree::build(chain2.entries()).unwrap();
        let anchor2 = WitnessAnchor::new(&tree2, &chain2)
            .unwrap()
            .with_prev_anchor(&anchor1);

        assert_eq!(
            anchor2.prev_anchor_hash.as_deref(),
            Some(anchor1.hash.as_str())
        );
        assert!(anchor2.verify_integrity());

        // Anchor 1 and 2 have different hashes
        assert_ne!(anchor1.hash, anchor2.hash);
    }

    #[test]
    fn receipt_creation() {
        let receipt = WitnessReceipt::new(
            Uuid::new_v4(),
            "test-backend",
            serde_json::json!({"tx": "abc123"}),
        );
        assert_eq!(receipt.backend, "test-backend");
        assert!(receipt.receipt_hash.is_none());
    }

    #[test]
    fn receipt_with_hash() {
        let receipt = WitnessReceipt::new(
            Uuid::new_v4(),
            "test-backend",
            serde_json::json!({"tx": "abc123"}),
        )
        .with_hash();
        assert!(receipt.receipt_hash.is_some());
    }

    #[test]
    fn serde_roundtrip_anchor() {
        let chain = make_chain(3);
        let tree = MerkleTree::build(chain.entries()).unwrap();
        let anchor = WitnessAnchor::new(&tree, &chain).unwrap();

        let json = serde_json::to_string(&anchor).unwrap();
        let back: WitnessAnchor = serde_json::from_str(&json).unwrap();
        assert_eq!(back.hash, anchor.hash);
        assert!(back.verify_integrity());
    }

    #[test]
    fn serde_roundtrip_receipt() {
        let receipt = WitnessReceipt::new(Uuid::new_v4(), "test", serde_json::json!({"data": 42}))
            .with_hash();
        let json = serde_json::to_string(&receipt).unwrap();
        let back: WitnessReceipt = serde_json::from_str(&json).unwrap();
        assert_eq!(back.receipt_hash, receipt.receipt_hash);
    }

    #[test]
    fn serde_roundtrip_verification() {
        let v = AnchorVerification::RootMismatch {
            expected: "a".into(),
            actual: "b".into(),
        };
        let json = serde_json::to_string(&v).unwrap();
        let back: AnchorVerification = serde_json::from_str(&json).unwrap();
        assert_eq!(v, back);
    }

    #[test]
    fn verification_display() {
        assert_eq!(AnchorVerification::Valid.to_string(), "valid");
        let m = AnchorVerification::CountMismatch {
            expected: 5,
            actual: 3,
        };
        assert!(m.to_string().contains("5"));
        assert!(m.to_string().contains("3"));
    }
}