Skip to main content

oxabl_cache_crypto/
object_keys.rs

1//! Single-use keys of one sealed object, derived from a sub-key and the object's salt.
2
3use subtle::ConstantTimeEq;
4use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
5
6use crate::domain::{CTX_COMMIT, CTX_ENC};
7use crate::keys::{RecordKey, SegmentKey};
8
9/// `K_enc` and `K_commit` of one object.
10#[derive(Zeroize, ZeroizeOnDrop)]
11pub struct ObjectKeys {
12    enc: [u8; 32],
13    commit: [u8; 32],
14}
15
16impl ObjectKeys {
17    fn derive(sub_key: &[u8; 32], salt: &[u8; 32]) -> Self {
18        let mut material = Zeroizing::new([0u8; 64]);
19        material[..32].copy_from_slice(sub_key);
20        material[32..].copy_from_slice(salt);
21        Self {
22            enc: blake3::derive_key(CTX_ENC, material.as_slice()),
23            commit: blake3::derive_key(CTX_COMMIT, material.as_slice()),
24        }
25    }
26
27    pub(crate) fn enc(&self) -> &[u8; 32] {
28        &self.enc
29    }
30
31    pub(crate) fn commit(&self) -> [u8; 32] {
32        self.commit
33    }
34
35    /// Compares a header's commitment with this object's, in constant time.
36    pub(crate) fn commits_to(&self, header_commit: &[u8; 32]) -> bool {
37        bool::from(self.commit.ct_eq(header_commit))
38    }
39}
40
41impl std::fmt::Debug for ObjectKeys {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.write_str("ObjectKeys(<redacted>)")
44    }
45}
46
47impl SegmentKey {
48    /// Keys of the segment sealed under `salt`.
49    pub fn object_keys(&self, salt: &[u8; 32]) -> ObjectKeys {
50        ObjectKeys::derive(self.bytes(), salt)
51    }
52}
53
54impl RecordKey {
55    /// Keys of the record sealed under `salt`.
56    pub fn object_keys(&self, salt: &[u8; 32]) -> ObjectKeys {
57        ObjectKeys::derive(self.bytes(), salt)
58    }
59}