Skip to content

Commit

Permalink
Defined and implemented an Encryption-trait (code cleanup/quality)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph0llux committed Dec 14, 2024
1 parent 59d0c3c commit d6bfab1
Show file tree
Hide file tree
Showing 18 changed files with 376 additions and 680 deletions.
902 changes: 260 additions & 642 deletions src/lib/encryption.rs

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions src/lib/footer/file_footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl FileFooter {
let mut data_to_encrypt = Vec::new();
data_to_encrypt.append(&mut self.encode_content());

let encrypted_data = Encryption::encrypt_file_footer(
let encrypted_data = FileFooter::encrypt(
key, data_to_encrypt,
self.file_number,
algorithm
Expand All @@ -148,7 +148,7 @@ impl FileFooter {
let file_number = u64::decode_directly(&mut cursor)?;
let encrypted_data = Vec::<u8>::decode_directly(&mut cursor)?;
let algorithm = &encryption_information.borrow().algorithm;
let decrypted_data = Encryption::decrypt_file_footer(
let decrypted_data = FileFooter::decrypt(
&encryption_information.borrow().encryption_key,
encrypted_data,
file_number,
Expand Down Expand Up @@ -220,4 +220,10 @@ impl FileFooter {
fn struct_name(&self) -> &'static str {
"FileFooter"
}
}

impl Encryption for FileFooter {
fn crypto_nonce_padding() -> u8 {
0b00001000 //TODO: move all crypto paddings to constants (#codeCleanup)
}
}
6 changes: 6 additions & 0 deletions src/lib/footer/object_footer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ impl ObjectFooter {
}
}

impl Encryption for ObjectFooter {
fn crypto_nonce_padding() -> u8 {
0b00100000
}
}


/// Each object contains its own object footer (and this is the encrypted variant).
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions src/lib/footer/object_footer/object_footer_logical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl ObjectFooterLogical {
{
let mut vec = Vec::new();

let encrypted_content = Encryption::encrypt_object_footer(
let encrypted_content = ObjectFooter::encrypt(
&encryption_information.borrow().encryption_key,
self.encode_content(),
self.object_number,
Expand Down Expand Up @@ -301,7 +301,7 @@ impl EncryptedObjectFooterLogical {
A: Borrow<EncryptionAlgorithm>,
K: AsRef<[u8]>,
{
let content = Encryption::decrypt_object_footer(key, &self.encrypted_data, self.object_number, algorithm.borrow())?;
let content = ObjectFooter::decrypt(key, &self.encrypted_data, self.object_number, algorithm.borrow())?;
let mut cursor = Cursor::new(content);
let (acquisition_start,
acquisition_end,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/footer/object_footer/object_footer_physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ObjectFooterPhysical {
E: Borrow<EncryptionInformation>
{
let mut vec = Vec::new();
let encrypted_content = Encryption::encrypt_object_footer(
let encrypted_content = ObjectFooter::encrypt(
&encryption_information.borrow().encryption_key,
self.encode_content(),
self.object_number,
Expand Down Expand Up @@ -195,7 +195,7 @@ impl EncryptedObjectFooterPhysical {
A: Borrow<EncryptionAlgorithm>,
K: AsRef<[u8]>,
{
let content = Encryption::decrypt_object_footer(key, &self.encrypted_data, self.object_number, algorithm.borrow())?;
let content = ObjectFooter::decrypt(key, &self.encrypted_data, self.object_number, algorithm.borrow())?;
let mut cursor = Cursor::new(content);
let (acquisition_start,
acquisition_end,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/footer/object_footer/object_footer_virtual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl ObjectFooterVirtual {
E: Borrow<EncryptionInformation>
{
let mut vec = Vec::new();
let encrypted_content = Encryption::encrypt_object_footer(
let encrypted_content = ObjectFooter::encrypt(
&encryption_information.borrow().encryption_key,
self.encode_content(),
self.object_number,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl EncryptedObjectFooterVirtual {
A: Borrow<EncryptionAlgorithm>,
K: AsRef<[u8]>,
{
let content = Encryption::decrypt_object_footer(
let content = ObjectFooter::decrypt(
key, &self.encrypted_data, self.object_number, algorithm.borrow())?;
let mut cursor = Cursor::new(content);
let (creation_timestamp,
Expand Down
10 changes: 8 additions & 2 deletions src/lib/header/chunk_map/chunk_deduplication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl ChunkMap for ChunkDeduplicationMap {
D: Read,
Self: Sized {
let structure_data = Self::inner_structure_data(data)?;
let enc_buffer = Encryption::decrypt_chunk_deduplication_map(key, structure_data, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = ChunkDeduplicationMap::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 @@ -105,7 +105,7 @@ impl ChunkMap for ChunkDeduplicationMap {
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Encryption::encrypt_chunk_deduplication_map(key, vec, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = ChunkDeduplicationMap::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
}
}
Expand Down Expand Up @@ -151,6 +151,12 @@ impl ChunkDeduplicationMap {
}
}

impl Encryption for ChunkDeduplicationMap {
fn crypto_nonce_padding() -> u8 {
0b00111111
}
}

#[cfg(feature = "serde")]
impl Serialize for ChunkDeduplicationMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
10 changes: 8 additions & 2 deletions src/lib/header/chunk_map/chunk_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl ChunkMap for ChunkFlagsMap {
D: Read,
Self: Sized {
let structure_data = Self::inner_structure_data(data)?;
let enc_buffer = Encryption::decrypt_chunk_flags_map(key, structure_data, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = ChunkFlagsMap::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 @@ -203,7 +203,7 @@ impl ChunkMap for ChunkFlagsMap {
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Encryption::encrypt_chunk_flags_map(key, vec, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = ChunkFlagsMap::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
}
}
Expand Down Expand Up @@ -249,6 +249,12 @@ impl ChunkFlagsMap {
}
}

impl Encryption for ChunkFlagsMap {
fn crypto_nonce_padding() -> u8 {
0b00000111
}
}

#[cfg(feature = "serde")]
impl Serialize for ChunkFlagsMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
10 changes: 8 additions & 2 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 = Encryption::decrypt_chunk_offset_map(key, structure_data, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = ChunkOffsetMap::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 @@ -100,7 +100,7 @@ impl ChunkMap for ChunkOffsetMap {
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Encryption::encrypt_chunk_offset_map(key, vec, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = ChunkOffsetMap::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
}
}
Expand Down Expand Up @@ -146,6 +146,12 @@ impl ChunkOffsetMap {
}
}

impl Encryption for ChunkOffsetMap {
fn crypto_nonce_padding() -> u8 {
0b00000001
}
}

#[cfg(feature = "serde")]
impl Serialize for ChunkOffsetMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
10 changes: 8 additions & 2 deletions src/lib/header/chunk_map/chunk_same_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ChunkMap for ChunkSamebytesMap {
D: Read,
Self: Sized {
let structure_data = Self::inner_structure_data(data)?;
let enc_buffer = Encryption::decrypt_chunk_samebytes_map(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 @@ -100,7 +100,7 @@ impl ChunkMap for ChunkSamebytesMap {
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Encryption::encrypt_chunk_samebytes_map(key, vec, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = Self::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
}
}
Expand Down Expand Up @@ -146,6 +146,12 @@ impl ChunkSamebytesMap {
}
}

impl Encryption for ChunkSamebytesMap {
fn crypto_nonce_padding() -> u8 {
0b00011111
}
}

#[cfg(feature = "serde")]
impl Serialize for ChunkSamebytesMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
10 changes: 8 additions & 2 deletions src/lib/header/chunk_map/chunk_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ChunkMap for ChunkSizeMap {
D: Read,
Self: Sized {
let structure_data = Self::inner_structure_data(data)?;
let enc_buffer = Encryption::decrypt_chunk_size_map(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 @@ -100,7 +100,7 @@ impl ChunkMap for ChunkSizeMap {
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Encryption::encrypt_chunk_size_map(key, vec, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = Self::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
}
}
Expand Down Expand Up @@ -146,6 +146,12 @@ impl ChunkSizeMap {
}
}

impl Encryption for ChunkSizeMap {
fn crypto_nonce_padding() -> u8 {
0b00000011
}
}

#[cfg(feature = "serde")]
impl Serialize for ChunkSizeMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
10 changes: 8 additions & 2 deletions src/lib/header/chunk_map/chunk_xxhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ChunkMap for ChunkXxHashMap {
D: Read,
Self: Sized {
let structure_data = Self::inner_structure_data(data)?;
let enc_buffer = Encryption::decrypt_chunk_xxhash_map(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 @@ -100,7 +100,7 @@ impl ChunkMap for ChunkXxHashMap {
Self: HeaderCoding, {
let mut vec = Vec::new();
vec.append(&mut Self::encode_map(self));
let enc_buffer = Encryption::encrypt_chunk_xxhash_map(key, vec, chunk_no, encryption_algorithm.borrow())?;
let enc_buffer = Self::encrypt(key, vec, chunk_no, encryption_algorithm.borrow())?;
Ok(enc_buffer)
}
}
Expand Down Expand Up @@ -145,6 +145,12 @@ impl ChunkXxHashMap {
}
}

impl Encryption for ChunkXxHashMap {
fn crypto_nonce_padding() -> u8 {
0b00001111
}
}

#[cfg(feature = "serde")]
impl Serialize for ChunkXxHashMap {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
Expand Down
14 changes: 7 additions & 7 deletions src/lib/header/encryption_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
ZffErrorKind,
KDFScheme,
PBEScheme,
Encryption,
encryption::*,
};

use crate::{
Expand Down Expand Up @@ -161,14 +161,14 @@ impl EncryptionHeader {
let iterations = parameters.iterations;
let salt = parameters.salt;
match self.pbe_header.encryption_scheme {
PBEScheme::AES128CBC => Encryption::decrypt_pbkdf2sha256_aes128cbc(
PBEScheme::AES128CBC => decrypt_pbkdf2sha256_aes128cbc(
iterations,
&salt,
&self.pbe_header.pbencryption_nonce,
&password,
&self.encrypted_encryption_key
),
PBEScheme::AES256CBC => Encryption::decrypt_pbkdf2sha256_aes256cbc(
PBEScheme::AES256CBC => decrypt_pbkdf2sha256_aes256cbc(
iterations,
&salt,
&self.pbe_header.pbencryption_nonce,
Expand All @@ -186,7 +186,7 @@ impl EncryptionHeader {
let r = parameters.r;
let salt = parameters.salt;
match self.pbe_header.encryption_scheme {
PBEScheme::AES128CBC => Encryption::decrypt_scrypt_aes128cbc(
PBEScheme::AES128CBC => decrypt_scrypt_aes128cbc(
logn,
p,
r,
Expand All @@ -195,7 +195,7 @@ impl EncryptionHeader {
&password,
&self.encrypted_encryption_key
),
PBEScheme::AES256CBC => Encryption::decrypt_scrypt_aes256cbc(
PBEScheme::AES256CBC => decrypt_scrypt_aes256cbc(
logn,
p,
r,
Expand All @@ -215,7 +215,7 @@ impl EncryptionHeader {
let iterations = parameters.iterations;
let salt = parameters.salt;
match self.pbe_header.encryption_scheme {
PBEScheme::AES128CBC => Encryption::decrypt_argon2_aes128cbc(
PBEScheme::AES128CBC => decrypt_argon2_aes128cbc(
mem_cost,
lanes,
iterations,
Expand All @@ -224,7 +224,7 @@ impl EncryptionHeader {
&password,
&self.encrypted_encryption_key
),
PBEScheme::AES256CBC => Encryption::decrypt_argon2_aes256cbc(
PBEScheme::AES256CBC => decrypt_argon2_aes256cbc(
mem_cost,
lanes,
iterations,
Expand Down
10 changes: 8 additions & 2 deletions src/lib/header/file_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl FileHeader {
let mut data_to_encrypt = Vec::new();
data_to_encrypt.append(&mut self.encode_content());

let encrypted_data = Encryption::encrypt_file_header(
let encrypted_data = FileHeader::encrypt(
key, data_to_encrypt,
self.file_number,
algorithm
Expand Down Expand Up @@ -249,7 +249,7 @@ impl FileHeader {

let encrypted_data = Vec::<u8>::decode_directly(&mut cursor)?;
let algorithm = &encryption_information.borrow().algorithm;
let decrypted_data = Encryption::decrypt_file_header(
let decrypted_data = FileHeader::decrypt(
&encryption_information.borrow().encryption_key,
encrypted_data,
file_number,
Expand Down Expand Up @@ -337,6 +337,12 @@ impl FileHeader {
}
}

impl Encryption for FileHeader {
fn crypto_nonce_padding() -> u8 {
0b00000100
}
}

/// This is a wrapper enum for all possible values of the metadata extended values.
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
Expand Down
Loading

0 comments on commit d6bfab1

Please sign in to comment.