Skip to main content

qualia_core_db/bundle/stream/protection/
metadata.rs

1//! Private fixed-width M serializer, never an exported key-bearing byte object.
2use super::{geometry::PreparedGeometry, publisher::PublisherError as E, ObjectKind};
3use crate::crypto::network::object_stream::Context;
4use crate::net::qdnf::types::StrongDigest;
5use crate::specialized_libs::cryptographic_library::ManagedObjectPublication;
6use zeroize::Zeroize;
7/// Exact references awaiting the separate recognized closure/policy composition.
8/// Construction alone neither authenticates these digests nor grants disclosure.
9#[derive(Clone, Copy)]
10pub struct PrivateReferences {
11    pub closure: StrongDigest,
12    pub policy: StrongDigest,
13    pub consumer: StrongDigest,
14}
15pub(super) struct Erase<'a> {
16    pub(super) bytes: &'a mut [u8],
17}
18impl Drop for Erase<'_> {
19    fn drop(&mut self) {
20        self.bytes.zeroize();
21    }
22}
23struct Writer<'a> {
24    bytes: &'a mut [u8],
25    pos: usize,
26}
27impl Writer<'_> {
28    fn put(&mut self, b: &[u8]) -> Result<(), E> {
29        let end = self.pos.checked_add(b.len()).ok_or(E::Capacity)?;
30        self.bytes
31            .get_mut(self.pos..end)
32            .ok_or(E::Capacity)?
33            .copy_from_slice(b);
34        self.pos = end;
35        Ok(())
36    }
37    fn u16(&mut self, n: u16) -> Result<(), E> {
38        self.put(&n.to_be_bytes())
39    }
40    fn u32(&mut self, n: u32) -> Result<(), E> {
41        self.put(&n.to_be_bytes())
42    }
43    fn u64(&mut self, n: u64) -> Result<(), E> {
44        self.put(&n.to_be_bytes())
45    }
46}
47pub(super) fn write(
48    g: &PreparedGeometry<'_, '_>,
49    p: &ManagedObjectPublication<'_>,
50    c: Context<'_>,
51    refs: PrivateReferences,
52    out: &mut [u8],
53) -> Result<(), E> {
54    if out.len() != g.metadata_length()
55        || refs.closure.is_zero()
56        || refs.policy.is_zero()
57        || refs.consumer.is_zero()
58    {
59        return Err(E::Capacity);
60    }
61    g.check_context(c)?;
62    let h = g.source.checked_header();
63    let mut w = Writer { bytes: out, pos: 0 };
64    w.put(b"QBDL-PMD-V1\0")?;
65    w.u16(1)?;
66    w.u16(0)?;
67    w.put(&c.id().0)?;
68    w.u16(h.probe().version)?;
69    w.u16(h.probe().flags)?;
70    w.u64(h.total_length())?;
71    w.put(&g.source_digest.0)?;
72    w.u64(h.index_offset())?;
73    w.u32(64)?;
74    w.u32(u32::try_from(h.index_length()).map_err(|_| E::Capacity)?)?;
75    w.put(&refs.closure.0)?;
76    w.put(&refs.policy.0)?;
77    w.put(&refs.consumer.0)?;
78    w.u32(g.object_count() as u32)?;
79    if w.pos != 288 {
80        return Err(E::Codec);
81    }
82    for (sorted, m) in g.mappings.iter().enumerate() {
83        let row = &g.source.objects()[m.source_slot];
84        w.u32(m.id)?;
85        w.u64(row.offset())?;
86        w.u64(row.length())?;
87        let (kind, ordinal) = match row.kind() {
88            ObjectKind::Entry(n) => (1, n),
89            ObjectKind::Gap => (2, u32::MAX),
90        };
91        w.put(&[kind])?;
92        w.u32(ordinal)?;
93        w.put(&[u8::from(row.length() > 0)])?;
94        w.u16(0)?;
95        w.put(&m.digest.0)?;
96        let end = w.pos.checked_add(32).ok_or(E::Capacity)?;
97        let master: &mut [u8; 32] = w
98            .bytes
99            .get_mut(w.pos..end)
100            .ok_or(E::Capacity)?
101            .try_into()
102            .map_err(|_| E::Capacity)?;
103        p.metadata_master_row(sorted, master)
104            .map_err(|_| E::Crypto)?;
105        if (*master == [0; 32]) != (row.length() == 0) {
106            return Err(E::Crypto);
107        }
108        w.pos = end;
109    }
110    w.put(g.source.header_bytes())?;
111    w.put(g.source.index_bytes())?;
112    if w.pos != w.bytes.len() {
113        return Err(E::Codec);
114    }
115    Ok(())
116}