2019-06-21 09:32:07 +00:00
|
|
|
use std::convert::TryInto;
|
2021-09-06 09:48:08 +00:00
|
|
|
use std::io::Write;
|
2019-06-21 09:32:07 +00:00
|
|
|
|
2021-07-06 10:49:10 +00:00
|
|
|
use anyhow::{bail, Error};
|
2021-09-06 09:48:08 +00:00
|
|
|
use openssl::symm::{decrypt_aead, Mode};
|
2021-07-06 10:49:10 +00:00
|
|
|
|
2019-07-01 08:36:59 +00:00
|
|
|
use proxmox::tools::io::{ReadExt, WriteExt};
|
2019-06-22 11:24:29 +00:00
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
use super::file_formats::*;
|
2020-07-08 07:18:44 +00:00
|
|
|
use super::{CryptConfig, CryptMode};
|
|
|
|
|
|
|
|
const MAX_BLOB_SIZE: usize = 128*1024*1024;
|
2019-10-06 08:31:06 +00:00
|
|
|
|
|
|
|
/// Encoded data chunk with digest and positional information
|
|
|
|
pub struct ChunkInfo {
|
|
|
|
pub chunk: DataBlob,
|
|
|
|
pub digest: [u8; 32],
|
|
|
|
pub chunk_len: u64,
|
|
|
|
pub offset: u64,
|
|
|
|
}
|
2019-06-21 09:32:07 +00:00
|
|
|
|
|
|
|
/// Data blob binary storage format
|
|
|
|
///
|
2019-08-09 08:22:56 +00:00
|
|
|
/// Data blobs store arbitrary binary data (< 128MB), and can be
|
2019-10-06 08:31:06 +00:00
|
|
|
/// compressed and encrypted (or just signed). A simply binary format
|
|
|
|
/// is used to store them on disk or transfer them over the network.
|
2019-08-14 12:08:27 +00:00
|
|
|
///
|
|
|
|
/// Please use index files to store large data files (".fidx" of
|
|
|
|
/// ".didx").
|
2019-06-21 09:32:07 +00:00
|
|
|
///
|
|
|
|
pub struct DataBlob {
|
|
|
|
raw_data: Vec<u8>, // tagged, compressed, encryped data
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DataBlob {
|
|
|
|
|
|
|
|
/// accessor to raw_data field
|
|
|
|
pub fn raw_data(&self) -> &[u8] {
|
|
|
|
&self.raw_data
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
/// Returns raw_data size
|
|
|
|
pub fn raw_size(&self) -> u64 {
|
|
|
|
self.raw_data.len() as u64
|
|
|
|
}
|
|
|
|
|
2019-06-23 07:48:23 +00:00
|
|
|
/// Consume self and returns raw_data
|
|
|
|
pub fn into_inner(self) -> Vec<u8> {
|
|
|
|
self.raw_data
|
|
|
|
}
|
|
|
|
|
2019-06-21 09:32:07 +00:00
|
|
|
/// accessor to chunk type (magic number)
|
|
|
|
pub fn magic(&self) -> &[u8; 8] {
|
|
|
|
self.raw_data[0..8].try_into().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:24:21 +00:00
|
|
|
/// accessor to crc32 checksum
|
|
|
|
pub fn crc(&self) -> u32 {
|
2020-01-21 11:28:01 +00:00
|
|
|
let crc_o = proxmox::offsetof!(DataBlobHeader, crc);
|
2019-06-22 07:12:25 +00:00
|
|
|
u32::from_le_bytes(self.raw_data[crc_o..crc_o+4].try_into().unwrap())
|
2019-06-21 15:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the CRC checksum field
|
|
|
|
pub fn set_crc(&mut self, crc: u32) {
|
2020-01-21 11:28:01 +00:00
|
|
|
let crc_o = proxmox::offsetof!(DataBlobHeader, crc);
|
2019-06-22 07:12:25 +00:00
|
|
|
self.raw_data[crc_o..crc_o+4].copy_from_slice(&crc.to_le_bytes());
|
2019-06-21 15:24:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// compute the CRC32 checksum
|
2019-06-23 07:48:23 +00:00
|
|
|
pub fn compute_crc(&self) -> u32 {
|
2019-06-21 15:24:21 +00:00
|
|
|
let mut hasher = crc32fast::Hasher::new();
|
2019-08-14 10:35:53 +00:00
|
|
|
let start = header_size(self.magic()); // start after HEAD
|
2019-06-22 07:12:25 +00:00
|
|
|
hasher.update(&self.raw_data[start..]);
|
2019-06-21 15:24:21 +00:00
|
|
|
hasher.finalize()
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
// verify the CRC32 checksum
|
2020-09-16 08:43:42 +00:00
|
|
|
pub fn verify_crc(&self) -> Result<(), Error> {
|
2019-06-26 07:54:25 +00:00
|
|
|
let expected_crc = self.compute_crc();
|
|
|
|
if expected_crc != self.crc() {
|
|
|
|
bail!("Data blob has wrong CRC checksum.");
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-08-02 07:56:01 +00:00
|
|
|
/// Create a DataBlob, optionally compressed and/or encrypted
|
2019-06-21 09:32:07 +00:00
|
|
|
pub fn encode(
|
|
|
|
data: &[u8],
|
2019-10-07 09:36:39 +00:00
|
|
|
config: Option<&CryptConfig>,
|
2019-06-21 09:32:07 +00:00
|
|
|
compress: bool,
|
|
|
|
) -> Result<Self, Error> {
|
|
|
|
|
2019-08-09 09:49:06 +00:00
|
|
|
if data.len() > MAX_BLOB_SIZE {
|
2019-06-21 09:32:07 +00:00
|
|
|
bail!("data blob too large ({} bytes).", data.len());
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:42:14 +00:00
|
|
|
let mut blob = if let Some(config) = config {
|
2019-06-21 09:32:07 +00:00
|
|
|
|
2019-06-22 11:02:53 +00:00
|
|
|
let compr_data;
|
|
|
|
let (_compress, data, magic) = if compress {
|
|
|
|
compr_data = zstd::block::compress(data, 1)?;
|
|
|
|
// Note: We only use compression if result is shorter
|
|
|
|
if compr_data.len() < data.len() {
|
|
|
|
(true, &compr_data[..], ENCR_COMPR_BLOB_MAGIC_1_0)
|
|
|
|
} else {
|
|
|
|
(false, data, ENCRYPTED_BLOB_MAGIC_1_0)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(false, data, ENCRYPTED_BLOB_MAGIC_1_0)
|
|
|
|
};
|
|
|
|
|
|
|
|
let header_len = std::mem::size_of::<EncryptedDataBlobHeader>();
|
|
|
|
let mut raw_data = Vec::with_capacity(data.len() + header_len);
|
|
|
|
|
|
|
|
let dummy_head = EncryptedDataBlobHeader {
|
|
|
|
head: DataBlobHeader { magic: [0u8; 8], crc: [0; 4] },
|
|
|
|
iv: [0u8; 16],
|
|
|
|
tag: [0u8; 16],
|
|
|
|
};
|
2019-07-01 08:36:59 +00:00
|
|
|
unsafe {
|
|
|
|
raw_data.write_le_value(dummy_head)?;
|
|
|
|
}
|
2019-06-22 11:02:53 +00:00
|
|
|
|
2021-09-06 09:48:08 +00:00
|
|
|
let (iv, tag) = Self::encrypt_to(&config, data, &mut raw_data)?;
|
2019-06-22 11:02:53 +00:00
|
|
|
|
|
|
|
let head = EncryptedDataBlobHeader {
|
|
|
|
head: DataBlobHeader { magic, crc: [0; 4] }, iv, tag,
|
|
|
|
};
|
|
|
|
|
2019-07-01 08:36:59 +00:00
|
|
|
unsafe {
|
|
|
|
(&mut raw_data[0..header_len]).write_le_value(head)?;
|
|
|
|
}
|
2019-06-22 11:02:53 +00:00
|
|
|
|
2019-08-06 09:42:14 +00:00
|
|
|
DataBlob { raw_data }
|
2019-06-21 09:32:07 +00:00
|
|
|
} else {
|
|
|
|
|
2019-06-22 07:12:25 +00:00
|
|
|
let max_data_len = data.len() + std::mem::size_of::<DataBlobHeader>();
|
2019-06-21 09:32:07 +00:00
|
|
|
if compress {
|
2019-06-22 07:12:25 +00:00
|
|
|
let mut comp_data = Vec::with_capacity(max_data_len);
|
2019-06-21 09:32:07 +00:00
|
|
|
|
2019-06-22 07:12:25 +00:00
|
|
|
let head = DataBlobHeader {
|
|
|
|
magic: COMPRESSED_BLOB_MAGIC_1_0,
|
|
|
|
crc: [0; 4],
|
|
|
|
};
|
2019-07-01 08:36:59 +00:00
|
|
|
unsafe {
|
|
|
|
comp_data.write_le_value(head)?;
|
|
|
|
}
|
2019-06-21 15:24:21 +00:00
|
|
|
|
2019-06-21 09:32:07 +00:00
|
|
|
zstd::stream::copy_encode(data, &mut comp_data, 1)?;
|
|
|
|
|
2019-06-22 07:12:25 +00:00
|
|
|
if comp_data.len() < max_data_len {
|
2019-08-07 06:29:38 +00:00
|
|
|
let mut blob = DataBlob { raw_data: comp_data };
|
|
|
|
blob.set_crc(blob.compute_crc());
|
|
|
|
return Ok(blob);
|
2019-06-21 09:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 07:12:25 +00:00
|
|
|
let mut raw_data = Vec::with_capacity(max_data_len);
|
2019-06-21 09:32:07 +00:00
|
|
|
|
2019-06-22 07:12:25 +00:00
|
|
|
let head = DataBlobHeader {
|
|
|
|
magic: UNCOMPRESSED_BLOB_MAGIC_1_0,
|
|
|
|
crc: [0; 4],
|
|
|
|
};
|
2019-07-01 08:36:59 +00:00
|
|
|
unsafe {
|
|
|
|
raw_data.write_le_value(head)?;
|
|
|
|
}
|
2019-06-21 09:32:07 +00:00
|
|
|
raw_data.extend_from_slice(data);
|
|
|
|
|
2019-08-06 09:42:14 +00:00
|
|
|
DataBlob { raw_data }
|
|
|
|
};
|
|
|
|
|
|
|
|
blob.set_crc(blob.compute_crc());
|
|
|
|
|
|
|
|
Ok(blob)
|
2019-06-21 09:32:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 07:18:44 +00:00
|
|
|
/// Get the encryption mode for this blob.
|
|
|
|
pub fn crypt_mode(&self) -> Result<CryptMode, Error> {
|
|
|
|
let magic = self.magic();
|
|
|
|
|
|
|
|
Ok(if magic == &UNCOMPRESSED_BLOB_MAGIC_1_0 || magic == &COMPRESSED_BLOB_MAGIC_1_0 {
|
|
|
|
CryptMode::None
|
|
|
|
} else if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 || magic == &ENCRYPTED_BLOB_MAGIC_1_0 {
|
|
|
|
CryptMode::Encrypt
|
|
|
|
} else {
|
|
|
|
bail!("Invalid blob magic number.");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-21 09:32:07 +00:00
|
|
|
/// Decode blob data
|
2020-08-03 12:10:43 +00:00
|
|
|
pub fn decode(&self, config: Option<&CryptConfig>, digest: Option<&[u8; 32]>) -> Result<Vec<u8>, Error> {
|
2019-06-21 09:32:07 +00:00
|
|
|
|
|
|
|
let magic = self.magic();
|
|
|
|
|
|
|
|
if magic == &UNCOMPRESSED_BLOB_MAGIC_1_0 {
|
2019-06-22 07:12:25 +00:00
|
|
|
let data_start = std::mem::size_of::<DataBlobHeader>();
|
2020-08-03 12:10:43 +00:00
|
|
|
let data = self.raw_data[data_start..].to_vec();
|
|
|
|
if let Some(digest) = digest {
|
|
|
|
Self::verify_digest(&data, None, digest)?;
|
|
|
|
}
|
|
|
|
Ok(data)
|
2019-06-21 09:32:07 +00:00
|
|
|
} else if magic == &COMPRESSED_BLOB_MAGIC_1_0 {
|
2019-06-22 07:12:25 +00:00
|
|
|
let data_start = std::mem::size_of::<DataBlobHeader>();
|
2020-09-24 06:52:35 +00:00
|
|
|
let mut reader = &self.raw_data[data_start..];
|
|
|
|
let data = zstd::stream::decode_all(&mut reader)?;
|
|
|
|
// zstd::block::decompress is abou 10% slower
|
|
|
|
// let data = zstd::block::decompress(&self.raw_data[data_start..], MAX_BLOB_SIZE)?;
|
2020-08-03 12:10:43 +00:00
|
|
|
if let Some(digest) = digest {
|
|
|
|
Self::verify_digest(&data, None, digest)?;
|
|
|
|
}
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(data)
|
2019-06-21 09:32:07 +00:00
|
|
|
} else if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 || magic == &ENCRYPTED_BLOB_MAGIC_1_0 {
|
2019-06-22 11:24:29 +00:00
|
|
|
let header_len = std::mem::size_of::<EncryptedDataBlobHeader>();
|
2019-06-22 14:29:10 +00:00
|
|
|
let head = unsafe {
|
|
|
|
(&self.raw_data[..header_len]).read_le_value::<EncryptedDataBlobHeader>()?
|
|
|
|
};
|
2019-06-22 11:24:29 +00:00
|
|
|
|
2019-06-21 09:32:07 +00:00
|
|
|
if let Some(config) = config {
|
|
|
|
let data = if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 {
|
2021-09-06 09:48:08 +00:00
|
|
|
Self::decode_compressed_chunk(config, &self.raw_data[header_len..], &head.iv, &head.tag)?
|
2019-06-21 09:32:07 +00:00
|
|
|
} else {
|
2021-09-06 09:48:08 +00:00
|
|
|
Self::decode_uncompressed_chunk(config, &self.raw_data[header_len..], &head.iv, &head.tag)?
|
2019-06-21 09:32:07 +00:00
|
|
|
};
|
2020-08-03 12:10:43 +00:00
|
|
|
if let Some(digest) = digest {
|
|
|
|
Self::verify_digest(&data, Some(config), digest)?;
|
|
|
|
}
|
2019-10-26 09:36:01 +00:00
|
|
|
Ok(data)
|
2019-06-21 09:32:07 +00:00
|
|
|
} else {
|
|
|
|
bail!("unable to decrypt blob - missing CryptConfig");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bail!("Invalid blob magic number.");
|
|
|
|
}
|
|
|
|
}
|
2019-06-23 07:35:44 +00:00
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
/// Load blob from ``reader``, verify CRC
|
|
|
|
pub fn load_from_reader(reader: &mut dyn std::io::Read) -> Result<Self, Error> {
|
2019-10-06 08:31:06 +00:00
|
|
|
|
|
|
|
let mut data = Vec::with_capacity(1024*1024);
|
|
|
|
reader.read_to_end(&mut data)?;
|
|
|
|
|
2020-07-28 08:23:16 +00:00
|
|
|
let blob = Self::from_raw(data)?;
|
|
|
|
|
|
|
|
blob.verify_crc()?;
|
|
|
|
|
|
|
|
Ok(blob)
|
2019-10-06 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
2019-06-23 07:35:44 +00:00
|
|
|
/// Create Instance from raw data
|
|
|
|
pub fn from_raw(data: Vec<u8>) -> Result<Self, Error> {
|
|
|
|
|
|
|
|
if data.len() < std::mem::size_of::<DataBlobHeader>() {
|
|
|
|
bail!("blob too small ({} bytes).", data.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
let magic = &data[0..8];
|
|
|
|
|
|
|
|
if magic == ENCR_COMPR_BLOB_MAGIC_1_0 || magic == ENCRYPTED_BLOB_MAGIC_1_0 {
|
|
|
|
|
|
|
|
if data.len() < std::mem::size_of::<EncryptedDataBlobHeader>() {
|
|
|
|
bail!("encrypted blob too small ({} bytes).", data.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
let blob = DataBlob { raw_data: data };
|
|
|
|
|
|
|
|
Ok(blob)
|
|
|
|
} else if magic == COMPRESSED_BLOB_MAGIC_1_0 || magic == UNCOMPRESSED_BLOB_MAGIC_1_0 {
|
|
|
|
|
|
|
|
let blob = DataBlob { raw_data: data };
|
|
|
|
|
|
|
|
Ok(blob)
|
|
|
|
} else {
|
|
|
|
bail!("unable to parse raw blob - wrong magic");
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 08:31:06 +00:00
|
|
|
|
2020-09-24 10:36:04 +00:00
|
|
|
/// Returns if chunk is encrypted
|
|
|
|
pub fn is_encrypted(&self) -> bool {
|
|
|
|
let magic = self.magic();
|
|
|
|
magic == &ENCR_COMPR_BLOB_MAGIC_1_0 || magic == &ENCRYPTED_BLOB_MAGIC_1_0
|
|
|
|
}
|
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
/// Verify digest and data length for unencrypted chunks.
|
|
|
|
///
|
|
|
|
/// To do that, we need to decompress data first. Please note that
|
2020-06-24 04:56:48 +00:00
|
|
|
/// this is not possible for encrypted chunks. This function simply return Ok
|
|
|
|
/// for encrypted chunks.
|
2020-07-28 08:23:16 +00:00
|
|
|
/// Note: This does not call verify_crc, because this is usually done in load
|
2019-10-06 08:31:06 +00:00
|
|
|
pub fn verify_unencrypted(
|
|
|
|
&self,
|
|
|
|
expected_chunk_size: usize,
|
|
|
|
expected_digest: &[u8; 32],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let magic = self.magic();
|
|
|
|
|
2020-06-24 04:56:48 +00:00
|
|
|
if magic == &ENCR_COMPR_BLOB_MAGIC_1_0 || magic == &ENCRYPTED_BLOB_MAGIC_1_0 {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:10:43 +00:00
|
|
|
// verifies digest!
|
|
|
|
let data = self.decode(None, Some(expected_digest))?;
|
2019-10-06 08:31:06 +00:00
|
|
|
|
2020-06-24 04:56:48 +00:00
|
|
|
if expected_chunk_size != data.len() {
|
|
|
|
bail!("detected chunk with wrong length ({} != {})", expected_chunk_size, data.len());
|
|
|
|
}
|
2020-08-03 12:10:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn verify_digest(
|
|
|
|
data: &[u8],
|
|
|
|
config: Option<&CryptConfig>,
|
|
|
|
expected_digest: &[u8; 32],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let digest = match config {
|
|
|
|
Some(config) => config.compute_digest(data),
|
2020-09-01 10:47:17 +00:00
|
|
|
None => openssl::sha::sha256(data),
|
2020-08-03 12:10:43 +00:00
|
|
|
};
|
2020-06-24 04:56:48 +00:00
|
|
|
if &digest != expected_digest {
|
|
|
|
bail!("detected chunk with wrong digest.");
|
2019-10-06 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-06 09:48:08 +00:00
|
|
|
|
|
|
|
/// Benchmark encryption speed
|
|
|
|
pub fn encrypt_benchmark<W: Write>(
|
|
|
|
config: &CryptConfig,
|
|
|
|
data: &[u8],
|
|
|
|
output: W,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let _ = Self::encrypt_to(config, data, output)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt data using a random 16 byte IV.
|
|
|
|
//
|
|
|
|
// Writes encrypted data to ``output``, Return the used IV and computed MAC.
|
|
|
|
fn encrypt_to<W: Write>(
|
|
|
|
config: &CryptConfig,
|
|
|
|
data: &[u8],
|
|
|
|
mut output: W,
|
|
|
|
) -> Result<([u8;16], [u8;16]), Error> {
|
|
|
|
|
|
|
|
let mut iv = [0u8; 16];
|
|
|
|
proxmox::sys::linux::fill_with_random_data(&mut iv)?;
|
|
|
|
|
|
|
|
let mut tag = [0u8; 16];
|
|
|
|
|
|
|
|
let mut c = config.data_crypter(&iv, Mode::Encrypt)?;
|
|
|
|
|
|
|
|
const BUFFER_SIZE: usize = 32*1024;
|
|
|
|
|
|
|
|
let mut encr_buf = [0u8; BUFFER_SIZE];
|
|
|
|
let max_encoder_input = BUFFER_SIZE - config.cipher().block_size();
|
|
|
|
|
|
|
|
let mut start = 0;
|
|
|
|
loop {
|
|
|
|
let mut end = start + max_encoder_input;
|
|
|
|
if end > data.len() { end = data.len(); }
|
|
|
|
if end > start {
|
|
|
|
let count = c.update(&data[start..end], &mut encr_buf)?;
|
|
|
|
output.write_all(&encr_buf[..count])?;
|
|
|
|
start = end;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let rest = c.finalize(&mut encr_buf)?;
|
|
|
|
if rest > 0 { output.write_all(&encr_buf[..rest])?; }
|
|
|
|
|
|
|
|
output.flush()?;
|
|
|
|
|
|
|
|
c.get_tag(&mut tag)?;
|
|
|
|
|
|
|
|
Ok((iv, tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decompress and decrypt data, verify MAC.
|
|
|
|
fn decode_compressed_chunk(
|
|
|
|
config: &CryptConfig,
|
|
|
|
data: &[u8],
|
|
|
|
iv: &[u8; 16],
|
|
|
|
tag: &[u8; 16],
|
|
|
|
) -> Result<Vec<u8>, Error> {
|
|
|
|
|
|
|
|
let dec = Vec::with_capacity(1024*1024);
|
|
|
|
|
|
|
|
let mut decompressor = zstd::stream::write::Decoder::new(dec)?;
|
|
|
|
|
|
|
|
let mut c = config.data_crypter(iv, Mode::Decrypt)?;
|
|
|
|
|
|
|
|
const BUFFER_SIZE: usize = 32*1024;
|
|
|
|
|
|
|
|
let mut decr_buf = [0u8; BUFFER_SIZE];
|
|
|
|
let max_decoder_input = BUFFER_SIZE - config.cipher().block_size();
|
|
|
|
|
|
|
|
let mut start = 0;
|
|
|
|
loop {
|
|
|
|
let mut end = start + max_decoder_input;
|
|
|
|
if end > data.len() { end = data.len(); }
|
|
|
|
if end > start {
|
|
|
|
let count = c.update(&data[start..end], &mut decr_buf)?;
|
|
|
|
decompressor.write_all(&decr_buf[0..count])?;
|
|
|
|
start = end;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.set_tag(tag)?;
|
|
|
|
let rest = c.finalize(&mut decr_buf)?;
|
|
|
|
if rest > 0 { decompressor.write_all(&decr_buf[..rest])?; }
|
|
|
|
|
|
|
|
decompressor.flush()?;
|
|
|
|
|
|
|
|
Ok(decompressor.into_inner())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt data, verify tag.
|
|
|
|
fn decode_uncompressed_chunk(
|
|
|
|
config: &CryptConfig,
|
|
|
|
data: &[u8],
|
|
|
|
iv: &[u8; 16],
|
|
|
|
tag: &[u8; 16],
|
|
|
|
) -> Result<Vec<u8>, Error> {
|
|
|
|
|
|
|
|
let decr_data = decrypt_aead(
|
|
|
|
*config.cipher(),
|
|
|
|
config.enc_key(),
|
|
|
|
Some(iv),
|
|
|
|
b"", //??
|
|
|
|
data,
|
|
|
|
tag,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(decr_data)
|
|
|
|
}
|
|
|
|
|
2019-10-06 08:31:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builder for chunk DataBlobs
|
|
|
|
///
|
|
|
|
/// Main purpose is to centralize digest computation. Digest
|
|
|
|
/// computation differ for encryped chunk, and this interface ensures that
|
|
|
|
/// we always compute the correct one.
|
2019-10-07 09:36:39 +00:00
|
|
|
pub struct DataChunkBuilder<'a, 'b> {
|
|
|
|
config: Option<&'b CryptConfig>,
|
2019-10-06 08:31:06 +00:00
|
|
|
orig_data: &'a [u8],
|
|
|
|
digest_computed: bool,
|
|
|
|
digest: [u8; 32],
|
|
|
|
compress: bool,
|
|
|
|
}
|
|
|
|
|
2019-10-07 09:36:39 +00:00
|
|
|
impl <'a, 'b> DataChunkBuilder<'a, 'b> {
|
2019-10-06 08:31:06 +00:00
|
|
|
|
|
|
|
/// Create a new builder instance.
|
|
|
|
pub fn new(orig_data: &'a [u8]) -> Self {
|
|
|
|
Self {
|
|
|
|
orig_data,
|
|
|
|
config: None,
|
|
|
|
digest_computed: false,
|
|
|
|
digest: [0u8; 32],
|
|
|
|
compress: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set compression flag.
|
|
|
|
///
|
|
|
|
/// If true, chunk data is compressed using zstd (level 1).
|
|
|
|
pub fn compress(mut self, value: bool) -> Self {
|
|
|
|
self.compress = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set encryption Configuration
|
|
|
|
///
|
2020-07-08 12:06:50 +00:00
|
|
|
/// If set, chunks are encrypted
|
|
|
|
pub fn crypt_config(mut self, value: &'b CryptConfig) -> Self {
|
2019-10-06 08:31:06 +00:00
|
|
|
if self.digest_computed {
|
|
|
|
panic!("unable to set crypt_config after compute_digest().");
|
|
|
|
}
|
2020-07-08 12:06:50 +00:00
|
|
|
self.config = Some(value);
|
2019-10-06 08:31:06 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_digest(&mut self) {
|
|
|
|
if !self.digest_computed {
|
|
|
|
if let Some(ref config) = self.config {
|
|
|
|
self.digest = config.compute_digest(self.orig_data);
|
|
|
|
} else {
|
|
|
|
self.digest = openssl::sha::sha256(self.orig_data);
|
|
|
|
}
|
|
|
|
self.digest_computed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the chunk Digest
|
|
|
|
///
|
|
|
|
/// Note: For encrypted chunks, this needs to be called after
|
|
|
|
/// ``crypt_config``.
|
|
|
|
pub fn digest(&mut self) -> &[u8; 32] {
|
|
|
|
if !self.digest_computed {
|
|
|
|
self.compute_digest();
|
|
|
|
}
|
|
|
|
&self.digest
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume self and build the ``DataBlob``.
|
|
|
|
///
|
|
|
|
/// Returns the blob and the computet digest.
|
|
|
|
pub fn build(mut self) -> Result<(DataBlob, [u8; 32]), Error> {
|
|
|
|
if !self.digest_computed {
|
|
|
|
self.compute_digest();
|
|
|
|
}
|
|
|
|
|
2020-07-08 12:06:50 +00:00
|
|
|
let chunk = DataBlob::encode(self.orig_data, self.config, self.compress)?;
|
2019-10-06 08:31:06 +00:00
|
|
|
Ok((chunk, self.digest))
|
|
|
|
}
|
2019-10-14 08:44:46 +00:00
|
|
|
|
2019-10-14 08:58:26 +00:00
|
|
|
/// Create a chunk filled with zeroes
|
|
|
|
pub fn build_zero_chunk(
|
|
|
|
crypt_config: Option<&CryptConfig>,
|
|
|
|
chunk_size: usize,
|
|
|
|
compress: bool,
|
|
|
|
) -> Result<(DataBlob, [u8; 32]), Error> {
|
2021-01-19 10:37:49 +00:00
|
|
|
let zero_bytes = vec![0; chunk_size];
|
2019-10-14 08:58:26 +00:00
|
|
|
let mut chunk_builder = DataChunkBuilder::new(&zero_bytes).compress(compress);
|
|
|
|
if let Some(ref crypt_config) = crypt_config {
|
2020-07-08 12:06:50 +00:00
|
|
|
chunk_builder = chunk_builder.crypt_config(crypt_config);
|
2019-10-14 08:58:26 +00:00
|
|
|
}
|
2019-10-14 08:44:46 +00:00
|
|
|
|
2019-10-14 08:58:26 +00:00
|
|
|
chunk_builder.build()
|
2019-10-14 08:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|