use std::io::Write;
use oxabl_cache_format::segment::{
CHUNK_PLAINTEXT_LEN, CHUNK_TAG_LEN, SEGMENT_HEADER_LEN, SealedSegment, plaintext_len,
};
use oxabl_cache_format::{Codec, Limits, SegmentId, SegmentPlaintext};
use zeroize::Zeroizing;
use crate::aead::Cipher;
use crate::compress::decompress;
use crate::dictionary::Dictionary;
use crate::error::{Error, Result};
use crate::keys::SegmentKey;
use crate::nonce::chunk_nonce;
pub struct SegmentExpectation<'a> {
pub id: SegmentId,
pub sealed_len: u64,
pub plain_len: u64,
pub dictionary: Option<&'a Dictionary>,
}
pub(crate) fn decrypt_segment(key: &SegmentKey, sealed: &[u8]) -> Result<Zeroizing<Vec<u8>>> {
let segment = SealedSegment::decode(sealed)?;
let keys = key.object_keys(&segment.header.salt);
if !keys.commits_to(&segment.header.k_commit) {
return Err(Error::KeyCommitment);
}
let aad = blake3::hash(&sealed[..SEGMENT_HEADER_LEN]);
let cipher = Cipher::new(keys.enc())?;
let total = plaintext_len(segment.body.len())?;
let mut plaintext = Zeroizing::new(Vec::with_capacity(total as usize));
let mut scratch = Zeroizing::new(Vec::with_capacity(CHUNK_PLAINTEXT_LEN + CHUNK_TAG_LEN));
let chunks = segment.body.chunks(CHUNK_PLAINTEXT_LEN + CHUNK_TAG_LEN);
let last = chunks.len() - 1;
for (i, chunk) in chunks.enumerate() {
scratch.clear();
scratch.extend_from_slice(chunk);
let opened = cipher.open(
chunk_nonce(i as u64, i == last),
aad.as_bytes(),
&mut scratch,
)?;
plaintext.extend_from_slice(opened);
}
Ok(plaintext)
}
pub(crate) fn verify_id(sealed: &[u8], id: &SegmentId) -> Result<()> {
if blake3::hash(sealed).as_bytes() != &id.0 {
return Err(Error::SegmentId);
}
Ok(())
}
pub fn open_segment(
key: &SegmentKey,
sealed: &[u8],
expect: &SegmentExpectation<'_>,
limits: &Limits,
out: impl Write,
) -> Result<()> {
if expect.plain_len > limits.plaintext_bytes() {
return Err(Error::Limit("plaintext per artifact set"));
}
if sealed.len() as u64 != expect.sealed_len {
return Err(Error::Mismatch("sealed length"));
}
verify_id(sealed, &expect.id)?;
let plaintext = decrypt_segment(key, sealed)?;
let body = SegmentPlaintext::decode(&plaintext)?;
match (body.codec, expect.dictionary) {
(Codec::Zstd, None) | (Codec::ZstdDictionary, Some(_)) => {}
_ => return Err(Error::Mismatch("codec and dictionary")),
}
decompress(body.compressed, expect.dictionary, expect.plain_len, out)
}