oxabl_cache_crypto/
object_keys.rs1use subtle::ConstantTimeEq;
4use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
5
6use crate::domain::{CTX_COMMIT, CTX_ENC};
7use crate::keys::{RecordKey, SegmentKey};
8
9#[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 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 pub fn object_keys(&self, salt: &[u8; 32]) -> ObjectKeys {
50 ObjectKeys::derive(self.bytes(), salt)
51 }
52}
53
54impl RecordKey {
55 pub fn object_keys(&self, salt: &[u8; 32]) -> ObjectKeys {
57 ObjectKeys::derive(self.bytes(), salt)
58 }
59}