Skip to main content

oxabl_cache_crypto/
keys.rs

1//! The cache root key of an epoch and the sub-keys derived from it.
2
3use crate::domain::{CTX_LOOKUP, CTX_RECORD, CTX_SEGMENT};
4use crate::error::Result;
5use crate::random;
6use crate::secret::secret_key;
7
8secret_key!(
9    /// Cache root key `CRK_e`: 32 random bytes, one per epoch, held only by members.
10    RootKey
11);
12secret_key!(
13    /// Key of keyed lookup ids, derived from the root key.
14    LookupKey
15);
16secret_key!(
17    /// Parent of the per-segment keys, derived from the root key.
18    SegmentKey
19);
20secret_key!(
21    /// Parent of the per-record keys, derived from the root key.
22    RecordKey
23);
24
25impl RootKey {
26    /// A fresh key from the operating system's random generator.
27    pub fn generate() -> Result<Self> {
28        random::array().map(Self)
29    }
30
31    pub fn lookup_key(&self) -> LookupKey {
32        LookupKey(blake3::derive_key(CTX_LOOKUP, &self.0))
33    }
34
35    pub fn segment_key(&self) -> SegmentKey {
36        SegmentKey(blake3::derive_key(CTX_SEGMENT, &self.0))
37    }
38
39    pub fn record_key(&self) -> RecordKey {
40        RecordKey(blake3::derive_key(CTX_RECORD, &self.0))
41    }
42}