oxabl_cache_crypto/
telemetry.rs1use oxabl_cache_format::{CacheId, SealedTelemetry, TelemetryReport};
5
6use crate::aead::Cipher;
7use crate::domain::{AD_TELEMETRY, CTX_TELEMETRY, prefixed};
8use crate::error::{Error, Result};
9use crate::keys::RootKey;
10use crate::random;
11
12fn cipher(root: &RootKey) -> Result<Cipher> {
13 Cipher::new(&blake3::derive_key(CTX_TELEMETRY, root.bytes()))
14}
15
16fn associated(cache: &CacheId, header: &[u8]) -> Vec<u8> {
17 let mut bound = prefixed(AD_TELEMETRY, &cache.0);
18 bound.extend_from_slice(header);
19 bound
20}
21
22pub fn seal_telemetry(
24 root: &RootKey,
25 cache: &CacheId,
26 epoch: u32,
27 report: &TelemetryReport,
28) -> Result<Vec<u8>> {
29 let nonce = random::array()?;
30 let header = SealedTelemetry::header(epoch, &nonce);
31 let mut body = report.encode();
32 cipher(root)?.seal(nonce, &associated(cache, &header), &mut body)?;
33 let mut frame = header;
34 frame.extend_from_slice(&body);
35 Ok(frame)
36}
37
38pub fn telemetry_epoch(frame: &[u8]) -> Result<u32> {
40 Ok(SealedTelemetry::decode(frame)?.epoch)
41}
42
43pub fn open_telemetry(
45 root: &RootKey,
46 cache: &CacheId,
47 epoch: u32,
48 frame: &[u8],
49) -> Result<TelemetryReport> {
50 let sealed = SealedTelemetry::decode(frame)?;
51 if sealed.epoch != epoch {
52 return Err(Error::Mismatch("telemetry of another epoch"));
53 }
54 let header = SealedTelemetry::header(sealed.epoch, &sealed.nonce);
55 let mut body = sealed.sealed.to_vec();
56 let plain = cipher(root)?.open(sealed.nonce, &associated(cache, &header), &mut body)?;
57 Ok(TelemetryReport::decode(plain)?)
58}