use std::convert::Infallible;
use std::fmt;
use std::io::{self, Read, Seek, SeekFrom, Write};
use chacha20poly1305::aead::AeadInOut;
use chacha20poly1305::{ChaCha20Poly1305, KeyInit, Nonce, Tag};
use nibkeep_base::{Hash, Hasher};
use rand_core::CryptoRng;
use zeroize::Zeroizing;
use crate::{CryptoError, Key};
const SUITE: u8 = 1;
const CHUNKS_CONTEXT: &str = "nibkeep 2026-10 blob chunks";
pub const BLOB_CHUNK_LEN: usize = 65_536;
const CHUNK_LEN: u64 = BLOB_CHUNK_LEN as u64;
const NONCE_LEN: usize = 16;
const HEADER_LEN: u64 = 1 + NONCE_LEN as u64;
const TAG_BYTES: usize = 16;
const TAG_LEN: u64 = TAG_BYTES as u64;
const SEALED_CHUNK_LEN: u64 = CHUNK_LEN + TAG_LEN;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SealedBlob {
pub content: Hash,
pub sealed: Hash,
pub len: u64,
}
pub fn seal_blob(
key: &Key,
mut content: impl Read,
mut sealed: impl Write,
rng: &mut impl CryptoRng,
) -> io::Result<SealedBlob> {
let mut nonce = [0; NONCE_LEN];
rng.fill_bytes(&mut nonce);
let cipher = cipher(key, &nonce);
let mut content_hash = Hasher::new();
let mut sealed_hash = Hasher::new();
let mut write = |bytes: &[u8]| {
sealed_hash.update(bytes);
sealed.write_all(bytes)
};
write(&[SUITE])?;
write(&nonce)?;
let mut current = Zeroizing::new(vec![0; BLOB_CHUNK_LEN]);
let mut next = Zeroizing::new(vec![0; BLOB_CHUNK_LEN]);
let mut current_len = fill(&mut content, &mut current)?;
let mut len = 0;
for index in 0.. {
let next_len = if current_len == BLOB_CHUNK_LEN {
fill(&mut content, &mut next)?
} else {
0
};
let last = next_len == 0;
let chunk = &mut current[..current_len];
content_hash.update(chunk);
let tag = cipher
.encrypt_inout_detached(&chunk_nonce(index, last), &[], (&mut *chunk).into())
.map_err(|_| io::Error::other("a chunk is too long to encrypt"))?;
write(chunk)?;
write(&tag)?;
len += current_len as u64;
if last {
break;
}
std::mem::swap(&mut current, &mut next);
current_len = next_len;
}
Ok(SealedBlob {
content: content_hash.finalize(),
sealed: sealed_hash.finalize(),
len,
})
}
pub struct BlobReader<R> {
sealed: R,
cipher: ChaCha20Poly1305,
content: Hash,
len: u64,
sealed_len: u64,
chunks: u64,
position: u64,
buffer: Zeroizing<Vec<u8>>,
chunk: Option<(u64, usize)>,
hasher: Hasher,
hashed: u64,
checked: bool,
}
impl<R: Read + Seek> BlobReader<R> {
pub fn new(key: &Key, content: Hash, mut sealed: R) -> io::Result<Self> {
let sealed_len = sealed.seek(SeekFrom::End(0))?;
let body = sealed_len.checked_sub(HEADER_LEN).ok_or_else(damaged)?;
let chunks = body.div_ceil(SEALED_CHUNK_LEN);
let last = body - chunks.saturating_sub(1) * SEALED_CHUNK_LEN;
if chunks == 0 || last < TAG_LEN || (chunks > 1 && last == TAG_LEN) {
return Err(damaged());
}
let mut suite = [0];
let mut nonce = [0; NONCE_LEN];
sealed.seek(SeekFrom::Start(0))?;
sealed.read_exact(&mut suite)?;
sealed.read_exact(&mut nonce)?;
if suite != [SUITE] {
return Err(damaged());
}
let room = usize::try_from(body.min(SEALED_CHUNK_LEN)).map_err(|_| damaged())?;
Ok(Self {
sealed,
cipher: cipher(key, &nonce),
content,
len: body - chunks * TAG_LEN,
sealed_len,
chunks,
position: 0,
buffer: Zeroizing::new(vec![0; room]),
chunk: None,
hasher: Hasher::new(),
hashed: 0,
checked: false,
})
}
#[must_use]
pub const fn len(&self) -> u64 {
self.len
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len == 0
}
fn decrypt(&mut self, index: u64) -> io::Result<()> {
if self.chunk.is_some_and(|(decrypted, _)| decrypted == index) {
return Ok(());
}
self.chunk = None;
let start = HEADER_LEN + index * SEALED_CHUNK_LEN;
let last = index + 1 == self.chunks;
let len = if last {
self.sealed_len - start
} else {
SEALED_CHUNK_LEN
};
let encrypted = &mut self.buffer[..usize::try_from(len).map_err(|_| damaged())?];
self.sealed.seek(SeekFrom::Start(start))?;
self.sealed.read_exact(encrypted).map_err(|error| {
if error.kind() == io::ErrorKind::UnexpectedEof {
damaged()
} else {
error
}
})?;
let (chunk, tag) = encrypted.split_at_mut(encrypted.len() - TAG_BYTES);
let tag = Tag::try_from(&*tag).map_err(|_| damaged())?;
self.cipher
.decrypt_inout_detached(&chunk_nonce(index, last), &[], (&mut *chunk).into(), &tag)
.map_err(|_| damaged())?;
self.chunk = Some((index, chunk.len()));
Ok(())
}
fn check(&mut self) -> io::Result<()> {
if self.checked {
return Ok(());
}
if self.len == 0 {
self.decrypt(0)?;
}
if self.hasher.finalize() != self.content {
return Err(damaged());
}
self.checked = true;
Ok(())
}
}
impl<R: Read + Seek> Read for BlobReader<R> {
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
if self.position >= self.len {
if self.hashed == self.len {
self.check()?;
}
return Ok(0);
}
let start = self.position;
self.decrypt(start / CHUNK_LEN)?;
let Some((_, chunk_len)) = self.chunk else {
unreachable!("the chunk was just decrypted");
};
let offset = usize::try_from(start % CHUNK_LEN).expect("below the chunk length");
let read = buffer.len().min(chunk_len - offset);
buffer[..read].copy_from_slice(&self.buffer[offset..offset + read]);
self.position += read as u64;
if start <= self.hashed && self.hashed < self.position {
let new = usize::try_from(self.hashed - start).expect("below the chunk length");
self.hasher.update(&buffer[new..read]);
self.hashed = self.position;
if self.hashed == self.len {
self.check()?;
}
}
Ok(read)
}
}
impl<R: Read + Seek> Seek for BlobReader<R> {
fn seek(&mut self, position: SeekFrom) -> io::Result<u64> {
let target = match position {
SeekFrom::Start(offset) => Some(offset),
SeekFrom::End(offset) => self.len.checked_add_signed(offset),
SeekFrom::Current(offset) => self.position.checked_add_signed(offset),
};
self.position = target.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"cannot seek before the start of a blob",
)
})?;
Ok(self.position)
}
}
impl<R> fmt::Debug for BlobReader<R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BlobReader")
.field("content", &self.content)
.field("len", &self.len)
.field("position", &self.position)
.finish_non_exhaustive()
}
}
fn cipher(key: &Key, nonce: &[u8; NONCE_LEN]) -> ChaCha20Poly1305 {
let Ok(chunks_key) = Key::fill_with(|bytes| {
blake3::Hasher::new_derive_key(CHUNKS_CONTEXT)
.update(key.expose_bytes())
.update(nonce)
.finalize_xof()
.fill(bytes);
Ok::<(), Infallible>(())
});
ChaCha20Poly1305::new(chunks_key.expose_bytes().into())
}
fn chunk_nonce(index: u64, last: bool) -> Nonce {
let mut nonce = [0; 12];
nonce[3..11].copy_from_slice(&index.to_be_bytes());
nonce[11] = u8::from(last);
Nonce::from(nonce)
}
fn fill(content: &mut impl Read, buffer: &mut [u8]) -> io::Result<usize> {
let mut filled = 0;
while filled < buffer.len() {
match content.read(&mut buffer[filled..]) {
Ok(0) => break,
Ok(read) => filled += read,
Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
Err(error) => return Err(error),
}
}
Ok(filled)
}
fn damaged() -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, CryptoError::Decryption)
}