Skip to content

Commit

Permalink
Fixed some issues when using encryption.
Browse files Browse the repository at this point in the history
  • Loading branch information
ph0llux committed Dec 25, 2024
1 parent 84d57a3 commit f53e675
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 44 deletions.
17 changes: 14 additions & 3 deletions src/lib/header/chunk_map/chunk_deduplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,20 @@ impl ChunkMap for ChunkDeduplicationMap {
A: Borrow<EncryptionAlgorithm>,
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = ChunkDeduplicationMap::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
let encoded_map = Self::encode_map(self);
let mut encrypted_map = Self::encrypt(key, encoded_map, chunk_no, encryption_algorithm.borrow())?;
let mut encoded_version = Self::version().encode_directly();
let identifier = Self::identifier();
let encoded_header_length = (
DEFAULT_LENGTH_HEADER_IDENTIFIER +
DEFAULT_LENGTH_VALUE_HEADER_LENGTH +
encrypted_map.len() +
encoded_version.len()) as u64;
vec.append(&mut identifier.to_be_bytes().to_vec());
vec.append(&mut encoded_header_length.to_le_bytes().to_vec());
vec.append(&mut encoded_version);
vec.append(&mut encrypted_map);
Ok(vec)
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/lib/header/chunk_map/chunk_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,20 @@ impl ChunkMap for ChunkFlagsMap {
A: Borrow<EncryptionAlgorithm>,
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = ChunkFlagsMap::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
let encoded_map = Self::encode_map(self);
let mut encrypted_map = Self::encrypt(key, encoded_map, chunk_no, encryption_algorithm.borrow())?;
let mut encoded_version = Self::version().encode_directly();
let identifier = Self::identifier();
let encoded_header_length = (
DEFAULT_LENGTH_HEADER_IDENTIFIER +
DEFAULT_LENGTH_VALUE_HEADER_LENGTH +
encrypted_map.len() +
encoded_version.len()) as u64;
vec.append(&mut identifier.to_be_bytes().to_vec());
vec.append(&mut encoded_header_length.to_le_bytes().to_vec());
vec.append(&mut encoded_version);
vec.append(&mut encrypted_map);
Ok(vec)
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/lib/header/chunk_map/chunk_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ChunkMap for ChunkOffsetMap {
D: Read,
Self: Sized {
let structure_data = Self::inner_structure_data(data)?;
let enc_buffer = ChunkOffsetMap::decrypt(key, structure_data, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = Self::decrypt(key, structure_data, chunk_no, encryption_algorithm.borrow())?;
let mut reader = Cursor::new(enc_buffer);
let map = BTreeMap::decode_directly(&mut reader)?;
Ok(Self::with_data(map))
Expand All @@ -99,9 +99,20 @@ impl ChunkMap for ChunkOffsetMap {
A: Borrow<EncryptionAlgorithm>,
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = ChunkOffsetMap::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
let encoded_map = Self::encode_map(self);
let mut encrypted_map = Self::encrypt(key, encoded_map, chunk_no, encryption_algorithm.borrow())?;
let mut encoded_version = Self::version().encode_directly();
let identifier = Self::identifier();
let encoded_header_length = (
DEFAULT_LENGTH_HEADER_IDENTIFIER +
DEFAULT_LENGTH_VALUE_HEADER_LENGTH +
encrypted_map.len() +
encoded_version.len()) as u64;
vec.append(&mut identifier.to_be_bytes().to_vec());
vec.append(&mut encoded_header_length.to_le_bytes().to_vec());
vec.append(&mut encoded_version);
vec.append(&mut encrypted_map);
Ok(vec)
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/lib/header/chunk_map/chunk_same_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,20 @@ impl ChunkMap for ChunkSamebytesMap {
A: Borrow<EncryptionAlgorithm>,
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Self::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
let encoded_map = Self::encode_map(self);
let mut encrypted_map = Self::encrypt(key, encoded_map, chunk_no, encryption_algorithm.borrow())?;
let mut encoded_version = Self::version().encode_directly();
let identifier = Self::identifier();
let encoded_header_length = (
DEFAULT_LENGTH_HEADER_IDENTIFIER +
DEFAULT_LENGTH_VALUE_HEADER_LENGTH +
encrypted_map.len() +
encoded_version.len()) as u64;
vec.append(&mut identifier.to_be_bytes().to_vec());
vec.append(&mut encoded_header_length.to_le_bytes().to_vec());
vec.append(&mut encoded_version);
vec.append(&mut encrypted_map);
Ok(vec)
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/lib/header/chunk_map/chunk_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,20 @@ impl ChunkMap for ChunkSizeMap {
A: Borrow<EncryptionAlgorithm>,
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Self::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
let encoded_map = Self::encode_map(self);
let mut encrypted_map = Self::encrypt(key, encoded_map, chunk_no, encryption_algorithm.borrow())?;
let mut encoded_version = Self::version().encode_directly();
let identifier = Self::identifier();
let encoded_header_length = (
DEFAULT_LENGTH_HEADER_IDENTIFIER +
DEFAULT_LENGTH_VALUE_HEADER_LENGTH +
encrypted_map.len() +
encoded_version.len()) as u64;
vec.append(&mut identifier.to_be_bytes().to_vec());
vec.append(&mut encoded_header_length.to_le_bytes().to_vec());
vec.append(&mut encoded_version);
vec.append(&mut encrypted_map);
Ok(vec)
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/lib/header/chunk_map/chunk_xxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,20 @@ impl ChunkMap for ChunkXxHashMap {
A: Borrow<EncryptionAlgorithm>,
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Self::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
let encoded_map = Self::encode_map(self);
let mut encrypted_map = Self::encrypt(key, encoded_map, chunk_no, encryption_algorithm.borrow())?;
let mut encoded_version = Self::version().encode_directly();
let identifier = Self::identifier();
let encoded_header_length = (
DEFAULT_LENGTH_HEADER_IDENTIFIER +
DEFAULT_LENGTH_VALUE_HEADER_LENGTH +
encrypted_map.len() +
encoded_version.len()) as u64;
vec.append(&mut identifier.to_be_bytes().to_vec());
vec.append(&mut encoded_header_length.to_le_bytes().to_vec());
vec.append(&mut encoded_version);
vec.append(&mut encrypted_map);
Ok(vec)
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/lib/header/chunk_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ pub trait ChunkMap {
return Err(ZffError::new(ZffErrorKind::HeaderDecodeMismatchIdentifier, ERROR_HEADER_DECODER_MISMATCH_IDENTIFIER));
}
let header_length = Self::decode_header_length(data)? as usize;
let mut structure_content = vec![0u8; header_length-DEFAULT_LENGTH_HEADER_IDENTIFIER-DEFAULT_LENGTH_VALUE_HEADER_LENGTH];
let version = u8::decode_directly(data)?;
if version != Self::version() {
return Err(ZffError::new(ZffErrorKind::UnsupportedVersion, version.to_string()));
}
let mut structure_content = vec![0u8; header_length-DEFAULT_LENGTH_HEADER_IDENTIFIER-DEFAULT_LENGTH_VALUE_HEADER_LENGTH-1];
data.read_exact(&mut structure_content)?;
Ok(structure_content)
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib/io/zffreader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ impl<R: Read + Seek> ZffReader<R> {
Some(reader) => reader,
None => return Err(ZffError::new(ZffErrorKind::MissingObjectNumber, object_number.to_string())),
};

let chunk_numbers = get_chunks_of_unencrypted_object(&self.object_reader, object_number)?;
let enc_info = get_enc_info_from_obj_reader(obj_reader)?;

for chunk_no in chunk_numbers {
self.preload_chunk_offset_map(chunk_no, &enc_info)?;
}
Expand Down
41 changes: 19 additions & 22 deletions src/lib/io/zffstreamer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::ops::{Add, AddAssign};
// Parent
use super::*;

use crate::chunk;
// - internal
use crate::{
footer::SegmentFooter, header::{ChunkMapType, ChunkMaps, SegmentHeader, ChunkMap},
Expand Down Expand Up @@ -435,20 +436,16 @@ impl<R: Read> ZffStreamer<R> {
}

/// checks if the appropriate object has an encryption header and encrypts the chunkmap if necessary.
fn encode_chunkmap(&self, chunkmap_type: ChunkMapType, encoded_chunkmap: Vec<u8>, last_chunk_no: u64) -> Result<Vec<u8>> {
fn encode_chunkmap<C>(&self, chunkmap: &C, last_chunk_no: u64) -> Result<Vec<u8>>
where
C: ChunkMap + HeaderCoding,
{
if let Some(encryption_header) = &self.current_object_encoder.get_obj_header().encryption_header {
let key = encryption_header.get_encryption_key_ref().unwrap(); //unwrap should be safe here - I don't know how we would encrypt all the other stuff, without knowing the key. :D
let algorithm = &encryption_header.algorithm;
match chunkmap_type {
ChunkMapType::OffsetMap => ChunkOffsetMap::encrypt(key, encoded_chunkmap, last_chunk_no, algorithm),
ChunkMapType::SizeMap => ChunkSizeMap::encrypt(key, encoded_chunkmap, last_chunk_no, algorithm),
ChunkMapType::FlagsMap => ChunkFlagsMap::encrypt(key, encoded_chunkmap, last_chunk_no, algorithm),
ChunkMapType::XxHashMap => ChunkXxHashMap::encrypt(key, encoded_chunkmap, last_chunk_no, algorithm),
ChunkMapType::SamebytesMap => ChunkSamebytesMap::encrypt(key, encoded_chunkmap, last_chunk_no, algorithm),
ChunkMapType::DeduplicationMap => ChunkDeduplicationMap::encrypt(key, encoded_chunkmap, last_chunk_no, algorithm),
}
Ok(chunkmap.encrypt_encoded_map(key, algorithm, last_chunk_no)?)
} else {
Ok(encoded_chunkmap)
Ok(chunkmap.encode_directly())
}
}

Expand All @@ -462,8 +459,8 @@ impl<R: Read> ZffStreamer<R> {
if let Some(chunk_no) = self.in_progress_data.chunkmaps.offset_map.chunkmap().keys().max() {
self.in_progress_data.main_footer.chunk_offset_maps.insert(*chunk_no, segment_number);
self.in_progress_data.segment_footer.chunk_offset_map_table.insert(*chunk_no, self.in_progress_data.bytes_read.current_segment);
let encoded_map = self.in_progress_data.chunkmaps.offset_map.encode_directly();
self.in_progress_data.current_encoded_chunk_offset_map = self.encode_chunkmap(chunk_map_type, encoded_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_offset_map = self.encode_chunkmap(
&self.in_progress_data.chunkmaps.offset_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_offset_map_read_bytes = ReadBytes::NotRead;
self.in_progress_data.chunkmaps.offset_map.flush();
}
Expand All @@ -472,8 +469,8 @@ impl<R: Read> ZffStreamer<R> {
if let Some(chunk_no) = self.in_progress_data.chunkmaps.size_map.chunkmap().keys().max() {
self.in_progress_data.main_footer.chunk_size_maps.insert(*chunk_no, segment_number);
self.in_progress_data.segment_footer.chunk_size_map_table.insert(*chunk_no, self.in_progress_data.bytes_read.current_segment);
let encoded_map = self.in_progress_data.chunkmaps.size_map.encode_directly();
self.in_progress_data.current_encoded_chunk_size_map = self.encode_chunkmap(chunk_map_type, encoded_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_size_map = self.encode_chunkmap(
&self.in_progress_data.chunkmaps.size_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_size_map_read_bytes = ReadBytes::NotRead;
self.in_progress_data.chunkmaps.size_map.flush();
}
Expand All @@ -482,8 +479,8 @@ impl<R: Read> ZffStreamer<R> {
if let Some(chunk_no) = self.in_progress_data.chunkmaps.flags_map.chunkmap().keys().max() {
self.in_progress_data.main_footer.chunk_flags_maps.insert(*chunk_no, segment_number);
self.in_progress_data.segment_footer.chunk_flags_map_table.insert(*chunk_no, self.in_progress_data.bytes_read.current_segment);
let encoded_map = self.in_progress_data.chunkmaps.flags_map.encode_directly();
self.in_progress_data.current_encoded_chunk_flags_map = self.encode_chunkmap(chunk_map_type, encoded_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_flags_map = self.encode_chunkmap(
&self.in_progress_data.chunkmaps.flags_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_flags_map_read_bytes = ReadBytes::NotRead;
self.in_progress_data.chunkmaps.flags_map.flush();
}
Expand All @@ -492,8 +489,8 @@ impl<R: Read> ZffStreamer<R> {
if let Some(chunk_no) = self.in_progress_data.chunkmaps.xxhash_map.chunkmap().keys().max() {
self.in_progress_data.main_footer.chunk_xxhash_maps.insert(*chunk_no, segment_number);
self.in_progress_data.segment_footer.chunk_xxhash_map_table.insert(*chunk_no, self.in_progress_data.bytes_read.current_segment);
let encoded_map = self.in_progress_data.chunkmaps.xxhash_map.encode_directly();
self.in_progress_data.current_encoded_chunk_xxhash_map = self.encode_chunkmap(chunk_map_type, encoded_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_xxhash_map = self.encode_chunkmap(
&self.in_progress_data.chunkmaps.xxhash_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_xxhash_map_read_bytes = ReadBytes::NotRead;
self.in_progress_data.chunkmaps.xxhash_map.flush();
}
Expand All @@ -502,8 +499,8 @@ impl<R: Read> ZffStreamer<R> {
if let Some(chunk_no) = self.in_progress_data.chunkmaps.same_bytes_map.chunkmap().keys().max() {
self.in_progress_data.main_footer.chunk_samebytes_maps.insert(*chunk_no, segment_number);
self.in_progress_data.segment_footer.chunk_samebytes_map_table.insert(*chunk_no, self.in_progress_data.bytes_read.current_segment);
let encoded_map = self.in_progress_data.chunkmaps.same_bytes_map.encode_directly();
self.in_progress_data.current_encoded_chunk_samebytes_map = self.encode_chunkmap(chunk_map_type, encoded_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_samebytes_map = self.encode_chunkmap(
&self.in_progress_data.chunkmaps.same_bytes_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_samebytes_map_read_bytes = ReadBytes::NotRead;
self.in_progress_data.chunkmaps.same_bytes_map.flush();
}
Expand All @@ -512,8 +509,8 @@ impl<R: Read> ZffStreamer<R> {
if let Some(chunk_no) = self.in_progress_data.chunkmaps.duplicate_chunks.chunkmap().keys().max() {
self.in_progress_data.main_footer.chunk_samebytes_maps.insert(*chunk_no, segment_number);
self.in_progress_data.segment_footer.chunk_samebytes_map_table.insert(*chunk_no, self.in_progress_data.bytes_read.current_segment);
let encoded_map = self.in_progress_data.chunkmaps.duplicate_chunks.encode_directly();
self.in_progress_data.current_encoded_chunk_deduplication_map = self.encode_chunkmap(chunk_map_type, encoded_map, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_deduplication_map = self.encode_chunkmap(
&self.in_progress_data.chunkmaps.duplicate_chunks, *chunk_no)?;
self.in_progress_data.current_encoded_chunk_deduplication_map_read_bytes = ReadBytes::NotRead;
self.in_progress_data.chunkmaps.duplicate_chunks.flush();
}
Expand Down

0 comments on commit f53e675

Please sign in to comment.