qualia_core_db/bundle/stream/storage/
identity.rs1use super::{StorageError as E, wire::*};
2use crate::bundle::stream::proof::{Representation, TreeSpec};
3use crate::net::qdnf::types::StrongDigest;
4pub const IDENTITY_BYTES: usize = 256;
5const MAGIC: &[u8; 8] = b"QBSID01\0";
6#[repr(u8)]
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum StorageClass {
9 PublicPlaintextCandidate = 0,
10 SharedCiphertextCandidate = 1,
11 PrivatePlaintext = 2,
12}
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct IdentityFields {
15 pub descriptor: StrongDigest,
16 pub context: StrongDigest,
17 pub entry: u32,
18 pub generation: u64,
19 pub representation: Representation,
20 pub storage_class: StorageClass,
21 pub key_epoch: u32,
22 pub logical_length: u64,
23 pub root: StrongDigest,
24 pub privacy_partition: StrongDigest,
25 pub policy_generation: u64,
26 pub storage_generation: u64,
27}
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub struct StorageIdentity {
31 fields: IdentityFields,
32 tree: TreeSpec,
33}
34impl StorageIdentity {
35 pub fn new(fields: IdentityFields) -> Result<Self, E> {
36 match (fields.storage_class, fields.representation) {
37 (StorageClass::PublicPlaintextCandidate, Representation::Plaintext)
38 | (StorageClass::SharedCiphertextCandidate, Representation::Ciphertext) => {}
39 _ => return Err(E::Unsupported),
40 }
41 let tree = TreeSpec::new(
42 fields.context,
43 fields.entry,
44 fields.generation,
45 fields.representation,
46 fields.key_epoch,
47 fields.logical_length,
48 )
49 .map_err(E::Proof)?;
50 Ok(Self { fields, tree })
51 }
52 pub const fn fields(&self) -> &IdentityFields {
53 &self.fields
54 }
55 pub const fn tree_spec(&self) -> TreeSpec {
56 self.tree
57 }
58 pub const fn leaf_count(&self) -> u64 {
59 self.tree.leaf_count()
60 }
61}
62pub fn encode_identity(identity: &StorageIdentity, out: &mut [u8]) -> Result<usize, E> {
63 let out = out.get_mut(..IDENTITY_BYTES).ok_or(E::Capacity)?;
64 out.fill(0);
65 let f = identity.fields();
66 out[..8].copy_from_slice(MAGIC);
67 out[8..10].copy_from_slice(&1u16.to_be_bytes());
68 out[16..64].copy_from_slice(f.descriptor.as_bytes());
69 out[64..112].copy_from_slice(f.context.as_bytes());
70 out[112..116].copy_from_slice(&f.entry.to_be_bytes());
71 out[116..124].copy_from_slice(&f.generation.to_be_bytes());
72 out[124] = f.representation as u8;
73 out[125] = f.storage_class as u8;
74 out[128..132].copy_from_slice(&f.key_epoch.to_be_bytes());
75 out[132..140].copy_from_slice(&f.logical_length.to_be_bytes());
76 out[140..188].copy_from_slice(f.root.as_bytes());
77 out[188..236].copy_from_slice(f.privacy_partition.as_bytes());
78 out[236..244].copy_from_slice(&f.policy_generation.to_be_bytes());
79 out[244..252].copy_from_slice(&f.storage_generation.to_be_bytes());
80 Ok(IDENTITY_BYTES)
81}
82pub fn decode_identity(bytes: &[u8]) -> Result<StorageIdentity, E> {
83 if bytes.len() != IDENTITY_BYTES {
84 return Err(E::Length);
85 }
86 if &bytes[..8] != MAGIC {
87 return Err(E::Magic);
88 }
89 if u16_at(bytes, 8)? != 1 {
90 return Err(E::Version);
91 }
92 if u16_at(bytes, 10)? != 0 {
93 return Err(E::Flags);
94 }
95 zero(&bytes[12..16])?;
96 zero(&bytes[126..128])?;
97 zero(&bytes[252..256])?;
98 let representation = match bytes[124] {
99 0 => Representation::Plaintext,
100 1 => Representation::Ciphertext,
101 _ => return Err(E::Unsupported),
102 };
103 let storage_class = match bytes[125] {
104 0 => StorageClass::PublicPlaintextCandidate,
105 1 => StorageClass::SharedCiphertextCandidate,
106 _ => return Err(E::Unsupported),
107 };
108 StorageIdentity::new(IdentityFields {
109 descriptor: digest_at(bytes, 16)?,
110 context: digest_at(bytes, 64)?,
111 entry: u32_at(bytes, 112)?,
112 generation: u64_at(bytes, 116)?,
113 representation,
114 storage_class,
115 key_epoch: u32_at(bytes, 128)?,
116 logical_length: u64_at(bytes, 132)?,
117 root: digest_at(bytes, 140)?,
118 privacy_partition: digest_at(bytes, 188)?,
119 policy_generation: u64_at(bytes, 236)?,
120 storage_generation: u64_at(bytes, 244)?,
121 })
122}