revault_lockbox_api 0.0.48

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
//! Preparation transition executors. Callers must prove reservations and cleanup
//! against the selected authenticated ownership graph. OverflowSession stages a
//! tail arena and unlinks it before caller-authorized erasure; archive mutation
//! and committed-graph cleanup remain the integrating caller's responsibility.
use super::*;
pub(crate) struct InlineSession {
    context: Context,
    selected: InlineSelected,
}
struct InlineSelected {
    record: Record,
    encoded: Vec<u8>,
    slot: usize,
    copies: u8,
    overflow: Option<RootRef>,
}
impl InlineSession {
    pub(crate) fn open(
        storage: &impl Storage,
        archive: LockboxId,
        mode: FormatMode,
        key: Option<&[u8]>,
    ) -> Result<Self> {
        Self::open_inner(storage, archive, mode, key, false)
    }
    fn open_inner(
        storage: &impl Storage,
        archive: LockboxId,
        mode: FormatMode,
        key: Option<&[u8]>,
        allow_overflow: bool,
    ) -> Result<Self> {
        let context = Context::new(archive, mode, key)?;
        let mut candidates = Vec::with_capacity(2);
        for slot in 0..2 {
            let encoded =
                storage.read_at(slot as u64 * FAILURE_REGION + STUB_OFFSET, STUB_BYTES)?;
            if let Ok(stub) = decode(&context, &encoded) {
                candidates.push((slot, encoded, stub));
            }
        }
        candidates.sort_by_key(|(_, _, stub)| stub.sequence);
        let (slot, encoded, newest) = candidates.pop().ok_or(Error::CorruptRecord)?;
        let mut copies = 1 << slot;
        if let Some((old_slot, _, old)) = candidates.pop() {
            if old.sequence == newest.sequence {
                if old.digest != newest.digest {
                    return Err(Error::CorruptRecord);
                }
                copies |= 1 << old_slot;
            } else if old.sequence.checked_add(1) != Some(newest.sequence)
                || newest.previous != old.digest
            {
                return Err(Error::CorruptRecord);
            }
        }
        let overflow = match &newest.contents {
            Contents::Inline(_) => None,
            Contents::Overflow(reference) => Some(*reference),
        };
        if overflow.is_some() && !allow_overflow {
            return Err(Error::SecurityLimitExceeded(
                "shared update needs overflow-arena integration".into(),
            ));
        }
        let record = materialize(&context, storage, newest)?;
        if record.cleanup_commit != [0; 32] || record.cleanup_bytes != 0 {
            return Err(Error::CorruptRecord);
        }
        Ok(Self {
            context,
            selected: InlineSelected {
                record,
                encoded,
                slot,
                copies,
                overflow,
            },
        })
    }
    pub(crate) fn base(&self) -> [u8; 32] {
        self.selected.record.base
    }
    pub(crate) fn active(&self) -> bool {
        self.selected.record.active
    }
    pub(crate) fn reservations(&self) -> &[Reservation] {
        &self.selected.record.reservations
    }
    pub(crate) fn require_idle(&self, base: [u8; 32]) -> Result<()> {
        if self.active() || self.base() != base {
            return Err(Error::CorruptRecord);
        }
        Ok(())
    }
    pub(crate) fn mirror(&self, storage: &mut impl Storage) -> Result<()> {
        if let Some(reference) = self.selected.overflow {
            // A journal reference alone cannot authorize writing its arena.
            // Validate availability, but leave physical repair to a caller with
            // selected-graph ownership proof. Only fixed control stubs are mirrored.
            reference.read_verified(storage)?;
        }
        storage.sync()?;
        if self.selected.copies != 3 {
            storage.write_at(
                (1 - self.selected.slot) as u64 * FAILURE_REGION + STUB_OFFSET,
                &self.selected.encoded,
            )?;
            storage.sync()?;
        }
        Ok(())
    }
    pub(crate) fn begin(
        &mut self,
        storage: &mut impl Storage,
        base: [u8; 32],
        reservations: Vec<Reservation>,
    ) -> Result<()> {
        self.require_idle(base)?;
        self.replace(storage, base, true, reservations)
    }
    pub(crate) fn finish(&mut self, storage: &mut impl Storage, base: [u8; 32]) -> Result<()> {
        if !self.active() {
            self.require_idle(base)?;
            return self.mirror(storage);
        }
        self.replace(storage, base, false, Vec::new())
    }
    fn replace(
        &mut self,
        storage: &mut impl Storage,
        base: [u8; 32],
        active: bool,
        reservations: Vec<Reservation>,
    ) -> Result<()> {
        if self.selected.overflow.is_some() {
            return Err(Error::InvalidOperation(
                "unlink preparation overflow before finishing".into(),
            ));
        }
        let record = Record {
            sequence: self
                .selected
                .record
                .sequence
                .checked_add(1)
                .ok_or(Error::CorruptRecord)?,
            previous: strong_checksum(&self.selected.encoded),
            base,
            active,
            cleanup_commit: [0; 32],
            cleanup_bytes: 0,
            reservations,
        };
        let encoded = encode(&self.context, &record, None)?;
        if encoded.overflow.is_some() {
            return Err(Error::CorruptRecord);
        }
        self.publish_encoded(storage, record, encoded.stub, None)
    }
    fn publish_encoded(
        &mut self,
        storage: &mut impl Storage,
        record: Record,
        encoded: Vec<u8>,
        overflow: Option<RootRef>,
    ) -> Result<()> {
        self.mirror(storage)?;
        let first = 1 - self.selected.slot;
        storage.write_at(first as u64 * FAILURE_REGION + STUB_OFFSET, &encoded)?;
        storage.sync()?;
        storage.write_at(
            self.selected.slot as u64 * FAILURE_REGION + STUB_OFFSET,
            &encoded,
        )?;
        storage.sync()?;
        self.selected = InlineSelected {
            record,
            encoded,
            slot: first,
            copies: 3,
            overflow,
        };
        Ok(())
    }
}

/// Opt-in executor: inline-only archive recovery continues to reject overflow.
/// Callers hold exclusive ownership and authenticate the base graph themselves.
pub(crate) struct OverflowSession(InlineSession);
impl OverflowSession {
    pub(crate) fn open(
        storage: &impl Storage,
        archive: LockboxId,
        mode: FormatMode,
        key: Option<&[u8]>,
    ) -> Result<Self> {
        Ok(Self(InlineSession::open_inner(
            storage, archive, mode, key, true,
        )?))
    }
    pub(crate) fn active(&self) -> bool {
        self.0.active()
    }
    pub(crate) fn base(&self) -> [u8; 32] {
        self.0.base()
    }
    pub(crate) fn reservations(&self) -> &[Reservation] {
        self.0.reservations()
    }
    pub(crate) fn arena(&self) -> Option<RootRef> {
        self.0.selected.overflow
    }
    pub(crate) fn mirror(&self, storage: &mut impl Storage) -> Result<()> {
        self.0.mirror(storage)
    }

    /// The caller has authenticated this sealed base and validated every reusable
    /// reservation against its graph. An initial durable inline active stub owns
    /// the unpublished append tail before either overflow copy is written.
    pub(crate) fn begin(
        &mut self,
        storage: &mut impl Storage,
        base: [u8; 32],
        sealed: u64,
        reservations: Vec<Reservation>,
    ) -> Result<()> {
        self.0.require_idle(base)?;
        if storage.len()? != sealed || sealed < REGION_LEN as u64 {
            return Err(Error::CorruptRecord);
        }
        let primary = sealed
            .checked_add(FAILURE_REGION - 1)
            .ok_or(Error::CorruptRecord)?
            / FAILURE_REGION
            * FAILURE_REGION;
        let mirror = primary
            .checked_add(FAILURE_REGION)
            .ok_or(Error::CorruptRecord)?;
        mirror
            .checked_add(FAILURE_REGION)
            .ok_or(Error::CorruptRecord)?;
        // Validate capacity before touching storage. This explicit entry point
        // does not silently allocate arenas for records that fit inline.
        let mut record = Record {
            sequence: self
                .0
                .selected
                .record
                .sequence
                .checked_add(2)
                .ok_or(Error::CorruptRecord)?,
            previous: [1; 32],
            base,
            active: true,
            cleanup_commit: [0; 32],
            cleanup_bytes: 0,
            reservations,
        };
        encode(&self.0.context, &record, Some((primary, mirror)))?;
        self.0.begin(storage, base, Vec::new())?;
        record.previous = strong_checksum(&self.0.selected.encoded);
        let encoded = encode(&self.0.context, &record, Some((primary, mirror)))?;
        let bytes = encoded.overflow.ok_or(Error::CorruptRecord)?;
        if storage.len()? != sealed {
            return Err(Error::CorruptRecord);
        }
        if primary > sealed {
            storage.append(&vec![0; (primary - sealed) as usize])?;
        }
        if storage.append(&bytes)? != primary || storage.append(&bytes)? != mirror {
            return Err(Error::CorruptRecord);
        }
        storage.sync()?;
        let reference = RootRef {
            primary,
            mirror,
            len: bytes.len() as u64,
            digest: strong_checksum(&bytes),
        };
        for offset in [primary, mirror] {
            if strong_checksum(&storage.read_at(offset, bytes.len())?) != reference.digest {
                return Err(Error::CorruptRecord);
            }
        }
        self.0
            .publish_encoded(storage, record, encoded.stub, Some(reference))
    }

    /// The caller has completed all graph-authorized cleanup except the arena.
    /// For abort, reservations must already be erased; for committed changes,
    /// selected pending claims must be erased except these arena copies. The
    /// arena must remain owned by the unsealed tail or selected pending claims.
    /// Publish BOTH empty active stubs before the caller erases the old arena.
    pub(crate) fn unlink_after_cleanup(&mut self, storage: &mut impl Storage) -> Result<RootRef> {
        self.unlink_with_reservations(storage, Vec::new())
    }
    /// Reused arena allocations remain reserved in the small inline record after
    /// unlink. Interrupted erasure must not lose their in-sealed cleanup authority.
    pub(crate) fn unlink_reused_after_cleanup(
        &mut self,
        storage: &mut impl Storage,
        arena: Vec<Reservation>,
    ) -> Result<RootRef> {
        let reference = self.arena().ok_or(Error::CorruptRecord)?;
        if arena.len() != 2
            || ![reference.primary, reference.mirror]
                .into_iter()
                .all(|start| {
                    arena.iter().any(|range| {
                        range.start == start
                            && range.len == reference.len
                            && self.reservations().contains(range)
                    })
                })
        {
            return Err(Error::CorruptRecord);
        }
        self.unlink_with_reservations(storage, arena)
    }
    fn unlink_with_reservations(
        &mut self,
        storage: &mut impl Storage,
        reservations: Vec<Reservation>,
    ) -> Result<RootRef> {
        let reference = self
            .arena()
            .ok_or_else(|| Error::InvalidOperation("no preparation overflow arena".into()))?;
        if !self.active() {
            return Err(Error::CorruptRecord);
        }
        let record = Record {
            sequence: self
                .0
                .selected
                .record
                .sequence
                .checked_add(1)
                .ok_or(Error::CorruptRecord)?,
            previous: strong_checksum(&self.0.selected.encoded),
            base: self.base(),
            active: true,
            cleanup_commit: [0; 32],
            cleanup_bytes: 0,
            reservations,
        };
        let encoded = encode(&self.0.context, &record, None)?;
        self.0
            .publish_encoded(storage, record, encoded.stub, None)?;
        Ok(reference)
    }
    /// Caller proves both arena reservations and the full reservation set against
    /// the selected graph. Publish a SMALL inline preparation for the arena first;
    /// thus even a torn first arena write has durable cleanup ownership.
    pub(crate) fn begin_reused(
        &mut self,
        storage: &mut impl Storage,
        base: [u8; 32],
        sealed: u64,
        arena: [Reservation; 2],
        reservations: Vec<Reservation>,
    ) -> Result<()> {
        self.0.require_idle(base)?;
        if storage.len()? != sealed
            || arena[0].start == arena[1].start
            || arena.iter().any(|range| {
                range.len != FAILURE_REGION
                    || range
                        .start
                        .checked_add(range.len)
                        .is_none_or(|end| end > sealed)
                    || !reservations.contains(range)
            })
        {
            return Err(Error::CorruptRecord);
        }
        let mut record = Record {
            sequence: self
                .0
                .selected
                .record
                .sequence
                .checked_add(2)
                .ok_or(Error::CorruptRecord)?,
            previous: [1; 32],
            base,
            active: true,
            cleanup_commit: [0; 32],
            cleanup_bytes: 0,
            reservations,
        };
        let locations = (arena[0].start, arena[1].start);
        encode(&self.0.context, &record, Some(locations))?;
        self.0.begin(storage, base, arena.to_vec())?;
        record.previous = strong_checksum(&self.0.selected.encoded);
        let encoded = encode(&self.0.context, &record, Some(locations))?;
        let bytes = encoded.overflow.ok_or(Error::CorruptRecord)?;
        let reference = RootRef {
            primary: locations.0,
            mirror: locations.1,
            len: bytes.len() as u64,
            digest: strong_checksum(&bytes),
        };
        for start in [reference.primary, reference.mirror] {
            storage.write_at(start, &bytes)?;
        }
        storage.sync()?;
        for start in [reference.primary, reference.mirror] {
            if strong_checksum(&storage.read_at(start, bytes.len())?) != reference.digest {
                return Err(Error::CorruptRecord);
            }
        }
        self.0
            .publish_encoded(storage, record, encoded.stub, Some(reference))
    }
    /// Caller has completed erasure/truncation against the selected graph.
    pub(crate) fn finish(&mut self, storage: &mut impl Storage, base: [u8; 32]) -> Result<()> {
        self.0.finish(storage, base)
    }
}