revault_lockbox_api 0.0.49

reVault lockbox API to create and manage lockboxes
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
//! Shared-control placement/authority component. No public writer uses this
//! profile. Inline metadata and public key slots have distinct permitted ranges;
//! they cannot alias publication or preparation. Overflow and allocation/retirement
//! integration remain separate requirements before activating a whole archive.
use super::*;

const PRIVATE_START: u64 = 16384;
const PRIVATE_BYTES: usize = 49152;
const KEYS_START: u64 = 12288;

pub(crate) fn commitment(anchor: &Anchor) -> Result<[u8; 32]> {
    Ok(strong_checksum(&message(
        &anchor.prefix_in(Layout::Shared)?,
    )))
}
pub(super) fn read_root(
    storage: &impl Storage,
    anchor: &Anchor,
    role: RootRole,
) -> Result<Vec<u8>> {
    anchor.validate_in(Layout::Shared)?;
    let root = match role {
        RootRole::Private => anchor.index,
        RootRole::Allocation => anchor.allocation,
        RootRole::PublicKeys => anchor.keys,
    };
    Layout::Shared.ranges(root, anchor.sealed_len, role)?;
    if root.absent() {
        return Err(Error::CorruptRecord);
    }
    for offset in [root.primary, root.mirror] {
        if offset.checked_add(root.len).ok_or(Error::CorruptRecord)? > storage.len()? {
            continue;
        }
        let bytes = storage.read_at(offset, root.len as usize)?;
        if strong_checksum(&bytes) == root.digest {
            return Ok(bytes);
        }
    }
    Err(Error::CorruptRecord)
}

#[cfg(test)]
pub(crate) mod tests;

mod catalogue;
pub(crate) mod ownership;
pub(crate) mod tree;

/// Publish a freshly staged comparison image. The file-image builder validates
/// all payload membership/bytes first and owns cleanup of the empty destination.
/// This function does not perform updates, install a path, or authorize retirement.
#[allow(clippy::too_many_arguments)]
pub(crate) fn initialize(
    storage: &mut impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    signer: Option<&OwnerSigningKeyPair>,
    key: Option<&[u8]>,
    body: &[u8],
    slots: &[crate::key_slot::KeySlot],
) -> Result<Anchor> {
    let public = if slots.is_empty() {
        None
    } else {
        Some(super::bootstrap::directory(archive, 1, slots)?)
    };
    initialize_at(
        storage,
        archive,
        mode,
        authority,
        signer,
        key,
        body,
        public.as_deref(),
        1,
        [0; 32],
        || Ok(()),
    )
}

/// Decode bounded wrappers from an already authenticated selected publication.
/// Fresh exports re-encode the directory generation while retaining every slot.
pub(crate) fn public_slots(
    storage: &impl Storage,
    anchor: &Anchor,
) -> Result<Vec<crate::key_slot::KeySlot>> {
    if anchor.keys.absent() {
        return Ok(Vec::new());
    }
    if anchor.mode.plaintext() {
        return Err(Error::CorruptHeader);
    }
    super::bootstrap::read_directory_in(storage, anchor, Layout::Shared)
}

/// Validate existing bounded bootstrap bytes before retaining them unchanged.
/// Caller must already have selected this anchor with the required authority.
/// The older directory generation remains valid under a newer publication.
pub(crate) fn retained_public_directory(
    storage: &impl Storage,
    anchor: &Anchor,
) -> Result<Option<Vec<u8>>> {
    if anchor.keys.absent() {
        return Ok(None);
    }
    if anchor.mode.plaintext() {
        return Err(Error::CorruptHeader);
    }
    super::bootstrap::read_directory_in(storage, anchor, Layout::Shared)?;
    read_root(storage, anchor, RootRole::PublicKeys).map(Some)
}

/// Relocate a stable authenticated selected image without resetting its lineage.
/// The typed caller validates full membership/content and owns destination cleanup.
#[allow(clippy::too_many_arguments)]
pub(crate) fn initialize_successor(
    source: &impl Storage,
    predecessor: &Anchor,
    storage: &mut impl Storage,
    authority: &Authority<'_>,
    signer: Option<&OwnerSigningKeyPair>,
    key: Option<&[u8]>,
    body: &[u8],
) -> Result<Anchor> {
    predecessor.validate_in(Layout::Shared)?;
    let public = retained_public_directory(source, predecessor)?;
    let generation = predecessor
        .generation
        .checked_add(1)
        .ok_or_else(|| Error::SecurityLimitExceeded("publication generation exhausted".into()))?;
    let previous = commitment(predecessor)?;
    let unchanged = || {
        let selected = select_in(
            source,
            predecessor.archive,
            predecessor.mode,
            authority,
            Layout::Shared,
        )?;
        if selected.anchor != *predecessor || selected.commitment != previous {
            return Err(Error::CorruptRecord);
        }
        Ok(())
    };
    unchanged()?;
    initialize_at(
        storage,
        predecessor.archive,
        predecessor.mode,
        authority,
        signer,
        key,
        body,
        public.as_deref(),
        generation,
        previous,
        unchanged,
    )
}

#[allow(clippy::too_many_arguments)]
fn initialize_at(
    storage: &mut impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    signer: Option<&OwnerSigningKeyPair>,
    key: Option<&[u8]>,
    body: &[u8],
    public: Option<&[u8]>,
    generation: u64,
    previous: [u8; 32],
    unchanged: impl Fn() -> Result<()>,
) -> Result<Anchor> {
    if storage.len()? < REGION_LEN as u64
        || storage
            .read_at(0, REGION_LEN)?
            .iter()
            .any(|byte| *byte != 0)
    {
        return Err(Error::InvalidInput(
            "shared image requires an empty control prefix".into(),
        ));
    }
    if let Authority::Symmetric(authority_key) = authority {
        if key != Some(*authority_key) {
            return Err(Error::InvalidKey);
        }
    }
    if mode.plaintext() && public.is_some() {
        return Err(Error::InvalidInput(
            "plaintext image cannot contain decryption slots".into(),
        ));
    }
    let private = catalogue::Codec::new(archive, mode, key)?.encode(body)?;
    let index = RootRef {
        primary: PRIVATE_START,
        mirror: FAILURE_REGION + PRIVATE_START,
        len: private.len() as u64,
        digest: strong_checksum(&private),
    };
    let keys = public.as_ref().map_or(RootRef::default(), |bytes| RootRef {
        primary: KEYS_START,
        mirror: FAILURE_REGION + KEYS_START,
        len: bytes.len() as u64,
        digest: strong_checksum(bytes),
    });
    let anchor = Anchor {
        archive,
        generation,
        mode,
        sealed_len: storage.len()?,
        object_root: index.digest,
        previous,
        index,
        allocation: RootRef::default(),
        keys,
    };
    anchor.validate_in(Layout::Shared)?;
    let base = commitment(&anchor)?;
    let stub =
        crate::file_format::preparation_journal::compact::initial_stub(archive, mode, key, base)?;
    let encoded = encode_in(&anchor, authority, signer, Layout::Shared)?;
    for bank in [0, FAILURE_REGION] {
        storage.write_at(bank + PRIVATE_START, &private)?;
        if let Some(public) = &public {
            storage.write_at(bank + KEYS_START, public)?;
        }
        storage.write_at(bank + 8192, &stub)?;
    }
    // Read back both dependencies before exposing either publication copy.
    for root in [index, keys] {
        if root.absent() {
            continue;
        }
        for offset in [root.primary, root.mirror] {
            if strong_checksum(&storage.read_at(offset, root.len as usize)?) != root.digest {
                return Err(Error::CorruptRecord);
            }
        }
    }
    crate::file_format::preparation_journal::compact::validate_initial_idle(
        storage, archive, mode, key, base,
    )?;
    storage.sync()?;
    unchanged()?;
    storage.write_at(0, &encoded)?;
    storage.sync()?;
    storage.write_at(FAILURE_REGION, &encoded)?;
    storage.sync()?;
    Ok(anchor)
}
pub(crate) fn open_private(
    storage: &impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    key: Option<&[u8]>,
) -> Result<(Anchor, crate::page_buffer::ZeroizingBytes)> {
    private_in(storage, archive, mode, authority, key, true)
}
/// Explicit read-only recovery trusts selected publication/private membership,
/// not unrelated preparation, public wrappers, payload availability or padding.
pub(crate) fn salvage_private(
    storage: &impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    key: Option<&[u8]>,
) -> Result<(Anchor, crate::page_buffer::ZeroizingBytes)> {
    private_in(storage, archive, mode, authority, key, false)
}
fn private_in(
    storage: &impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    key: Option<&[u8]>,
    strict: bool,
) -> Result<(Anchor, crate::page_buffer::ZeroizingBytes)> {
    let (anchor, body) = snapshot(storage, archive, mode, authority, key)?;
    if strict {
        if storage.len()? != anchor.sealed_len {
            return Err(Error::CorruptRecord);
        }
        if !anchor.keys.absent() {
            super::bootstrap::read_directory_in(storage, &anchor, Layout::Shared)?;
        }
        crate::file_format::preparation_journal::compact::session::InlineSession::open(
            storage, archive, mode, key,
        )?
        .require_idle(commitment(&anchor)?)?;
    }
    Ok((anchor, body))
}

pub(crate) fn credential_open(
    storage: &impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    owner: Option<&OwnerSigningPublicKey>,
    credential: super::bootstrap::Credential<'_>,
    slot: Option<u64>,
) -> Result<super::bootstrap::Opened> {
    super::bootstrap::open_in(
        storage,
        archive,
        mode,
        owner,
        credential,
        slot,
        Layout::Shared,
    )
}

/// The fresh file catalogue partitions every byte after the control prefix as
/// payload. External metadata/key roots would alias that graph; general shared
/// root placement alone is not sufficient ownership validation for this adapter.
fn validate_fresh_shape(anchor: &Anchor) -> Result<()> {
    anchor.validate_in(Layout::Shared)?;
    if anchor.generation != 1
        || anchor.index.len != PRIVATE_BYTES as u64
        || anchor.object_root != anchor.index.digest
        || !anchor.allocation.absent()
        || anchor.index.primary >= REGION_LEN as u64
        || anchor.index.mirror >= REGION_LEN as u64
        || (!anchor.keys.absent()
            && (anchor.keys.primary >= REGION_LEN as u64
                || anchor.keys.mirror >= REGION_LEN as u64))
    {
        return Err(Error::CorruptRecord);
    }
    Ok(())
}

/// Selected private bytes only. Caller must parse the typed catalogue, validate
/// its complete graph and independently check journal/padding for normal open.
pub(crate) fn snapshot(
    storage: &impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    key: Option<&[u8]>,
) -> Result<(Anchor, crate::page_buffer::ZeroizingBytes)> {
    let anchor = select_in(storage, archive, mode, authority, Layout::Shared)?.anchor;
    if anchor.index.len != PRIVATE_BYTES as u64
        || anchor.object_root != anchor.index.digest
        || !anchor.allocation.absent()
        || (!anchor.keys.absent() && anchor.keys.len != 4096)
    {
        return Err(Error::CorruptRecord);
    }
    let stored =
        crate::page_buffer::ZeroizingBytes::new(read_root(storage, &anchor, RootRole::Private)?);
    let body = catalogue::Codec::new(archive, mode, key)?.decode(&stored)?;
    Ok((anchor, body))
}
pub(crate) fn encode_private(
    archive: LockboxId,
    mode: FormatMode,
    key: Option<&[u8]>,
    body: &[u8],
) -> Result<crate::page_buffer::ZeroizingBytes> {
    catalogue::Codec::new(archive, mode, key)?.encode(body)
}
pub(crate) fn ensure_mirrored(
    storage: &mut impl Storage,
    archive: LockboxId,
    mode: FormatMode,
    authority: &Authority<'_>,
    expected: [u8; 32],
) -> Result<Anchor> {
    let selected = select_in(storage, archive, mode, authority, Layout::Shared)?;
    if selected.commitment != expected {
        return Err(Error::CorruptRecord);
    }
    for role in [RootRole::Private, RootRole::PublicKeys] {
        if matches!(role, RootRole::PublicKeys) && selected.anchor.keys.absent() {
            continue;
        }
        let bytes =
            crate::page_buffer::ZeroizingBytes::new(read_root(storage, &selected.anchor, role)?);
        let root = match role {
            RootRole::Private => selected.anchor.index,
            RootRole::PublicKeys => selected.anchor.keys,
            RootRole::Allocation => unreachable!(),
        };
        for offset in [root.primary, root.mirror] {
            let existing = crate::page_buffer::ZeroizingBytes::new(
                storage.read_at(offset, root.len as usize)?,
            );
            if existing.len() != root.len as usize || strong_checksum(&existing) != root.digest {
                storage.write_at(offset, &bytes)?;
            }
        }
    }
    storage.sync()?;
    if selected.copies != 3 {
        storage.write_at(
            (1 - selected.slot) as u64 * FAILURE_REGION,
            &selected.encoded,
        )?;
        storage.sync()?;
    }
    Ok(selected.anchor)
}
/// The typed transaction validates both complete graphs before calling this.
/// Dependencies are checked in BOTH copies before exposing the new publication.
pub(crate) struct Prepared {
    anchor: Anchor,
    encoded: Vec<u8>,
}
pub(crate) fn prepare(
    next: &Anchor,
    authority: &Authority<'_>,
    signer: Option<&OwnerSigningKeyPair>,
) -> Result<Prepared> {
    Ok(Prepared {
        anchor: next.clone(),
        encoded: encode_in(next, authority, signer, Layout::Shared)?,
    })
}
pub(crate) fn publish(
    storage: &mut impl Storage,
    prepared: &Prepared,
    authority: &Authority<'_>,
    expected: [u8; 32],
) -> Result<()> {
    publish_inner(storage, prepared, authority, expected, false)
}
/// Caller must supply a successful metadata-only graph transition before this
/// narrow publication. The old suffix stays intact until both shorter anchors
/// are durable; active preparation recovery wipes it before truncation.
pub(crate) fn publish_metadata_tail(
    storage: &mut impl Storage,
    prepared: &Prepared,
    authority: &Authority<'_>,
    expected: [u8; 32],
) -> Result<()> {
    publish_inner(storage, prepared, authority, expected, true)
}
fn publish_inner(
    storage: &mut impl Storage,
    prepared: &Prepared,
    authority: &Authority<'_>,
    expected: [u8; 32],
    shrinking: bool,
) -> Result<()> {
    let next = &prepared.anchor;
    next.validate_in(Layout::Shared)?;
    let current = select_in(storage, next.archive, next.mode, authority, Layout::Shared)?;
    let physical = if shrinking {
        if next.sealed_len >= current.anchor.sealed_len
            || next.index.primary != PRIVATE_START
            || next.index.mirror != FAILURE_REGION + PRIVATE_START
            || current.anchor.index.primary < next.sealed_len
            || current.anchor.index.mirror < next.sealed_len
            || next.keys != current.anchor.keys
        {
            return Err(Error::CorruptRecord);
        }
        current.anchor.sealed_len
    } else {
        next.sealed_len
    };
    if !next.allocation.absent() || storage.len()? != physical {
        return Err(Error::CorruptRecord);
    }
    if current.commitment != expected
        || next.previous != expected
        || current.anchor.generation.checked_add(1) != Some(next.generation)
    {
        return Err(Error::CorruptRecord);
    }
    for root in [next.index, next.keys] {
        if root.absent() {
            continue;
        }
        for offset in [root.primary, root.mirror] {
            let bytes = crate::page_buffer::ZeroizingBytes::new(
                storage.read_at(offset, root.len as usize)?,
            );
            if bytes.len() != root.len as usize || strong_checksum(&bytes) != root.digest {
                return Err(Error::CorruptRecord);
            }
        }
    }
    ensure_mirrored(storage, next.archive, next.mode, authority, expected)?;
    storage.sync()?;
    let first = 1 - current.slot;
    storage.write_at(first as u64 * FAILURE_REGION, &prepared.encoded)?;
    storage.sync()?;
    storage.write_at(current.slot as u64 * FAILURE_REGION, &prepared.encoded)?;
    storage.sync()?;
    Ok(())
}