Skip to main content

oxabl_cache_crypto/
record_open.rs

1//! Opening a record: commitment, AEAD, strict decoding and the signature over the transcript.
2
3use oxabl_cache_format::{EntryPayload, Limits, Namespace, PublicKey, SealedRecord};
4use zeroize::Zeroizing;
5
6use crate::aead::Cipher;
7use crate::error::{Error, Result};
8use crate::keys::RecordKey;
9use crate::nonce::RECORD_NONCE;
10use crate::record_context::RecordContext;
11use crate::record_entry::{Entry, EntryFields};
12use crate::signing;
13
14/// A record whose encryption and signature verified, but whose signer is not yet vetted
15/// against a roster. Call [`OpenedRecord::accept`] to use it.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct OpenedRecord {
18    pub(crate) entry: Entry,
19    pub(crate) context: RecordContext,
20}
21
22impl OpenedRecord {
23    /// The key that signed the record.
24    pub fn signer(&self) -> PublicKey {
25        self.entry.signer
26    }
27
28    pub fn namespace(&self) -> Namespace {
29        self.context.namespace
30    }
31}
32
33/// Opens `sealed` for the slot `ctx`. Fails for any other cache, epoch, namespace kind or
34/// lookup id, for any altered byte, and for a signature that does not verify.
35pub fn open_record(
36    key: &RecordKey,
37    ctx: &RecordContext,
38    sealed: &[u8],
39    limits: &Limits,
40) -> Result<OpenedRecord> {
41    let record = SealedRecord::decode(sealed)?;
42    let keys = key.object_keys(&record.salt);
43    if !keys.commits_to(&record.k_commit) {
44        return Err(Error::KeyCommitment);
45    }
46    let cipher = Cipher::new(keys.enc())?;
47    let mut buf = Zeroizing::new(record.ciphertext.to_vec());
48    let plaintext = cipher.open(RECORD_NONCE, &ctx.associated_data(), &mut buf)?;
49    let payload = EntryPayload::decode(plaintext, limits)?;
50    let transcript = ctx.entry_transcript(&payload.signed_bytes(limits)?);
51    signing::verify(&payload.signer, &transcript, &payload.signature)?;
52    Ok(OpenedRecord {
53        entry: Entry {
54            fields: EntryFields::from_payload(&payload),
55            signer: payload.signer,
56        },
57        context: *ctx,
58    })
59}