From e8d22f866275dacdeee7cbd2741552fedf3f987c Mon Sep 17 00:00:00 2001 From: Michele Esposito <34438276+mikesposito@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:13:06 +0100 Subject: [PATCH] refactor: remove `Enclave` `NONCE_SIZE` generic (#13) * refactor: remove `Enclave` `NONCE_SIZE` generic * chore: run rustfmt --- README.md | 2 +- benches/src/enclave.rs | 2 +- enclave/src/lib.rs | 26 ++++++++++++++------------ src/lib.rs | 2 +- src/main.rs | 7 +++---- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b518476..92b5521 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ fn main() { // Using Enclave for data encapsulation (&str metadata, 8-byte nonce) let enclave = - Enclave::<&str, 8>::from_plain_bytes("Some metadata", key.pubk, b"Some bytes to encrypt".to_vec()) + Enclave::from_plain_bytes("Some metadata", key.pubk, b"Some bytes to encrypt".to_vec()) .unwrap(); // Get encrypted bytes (ciphertext) diff --git a/benches/src/enclave.rs b/benches/src/enclave.rs index 7c8fa9d..dff6ba6 100644 --- a/benches/src/enclave.rs +++ b/benches/src/enclave.rs @@ -14,7 +14,7 @@ fn bench(c: &mut Criterion) { group.bench_with_input(BenchmarkId::new("encrypt", size), size, |b, &_size| { b.iter(|| { let buf = vec![0u8; *size]; - Enclave::<&str, NONCE_SIZE>::from_plain_bytes("Metadata", key, buf) + Enclave::from_plain_bytes("Metadata", key, buf) }); }); } diff --git a/enclave/src/lib.rs b/enclave/src/lib.rs index 2861399..14429e6 100644 --- a/enclave/src/lib.rs +++ b/enclave/src/lib.rs @@ -2,7 +2,10 @@ pub mod errors; pub use errors::EnclaveError; use secured_cipher::{ - chacha20::{core::KEY_SIZE, ChaCha20}, + chacha20::{ + core::{KEY_SIZE, NONCE_SIZE}, + ChaCha20, + }, random_bytes, Cipher, }; @@ -14,7 +17,7 @@ use secured_cipher::{ /// # Type Parameters /// * `T`: The type of metadata associated with the encrypted data. #[derive(Debug, Clone)] -pub struct Enclave { +pub struct Enclave { /// Metadata associated with the encrypted data. pub metadata: T, @@ -25,7 +28,7 @@ pub struct Enclave { nonce: [u8; NONCE_SIZE], } -impl Enclave { +impl Enclave { /// Creates a new `Enclave` instance from unencrypted data. /// /// # Arguments @@ -66,7 +69,7 @@ impl Enclave { } } -impl From> for Vec +impl From> for Vec where T: TryFrom> + Into>, { @@ -77,7 +80,7 @@ where /// /// # Returns /// A `Vec` representing the serialized enclave. - fn from(enclave: Enclave) -> Vec { + fn from(enclave: Enclave) -> Vec { let mut bytes: Vec = vec![]; let metadata_bytes = enclave.metadata.into(); @@ -90,7 +93,7 @@ where } } -impl TryFrom> for Enclave +impl TryFrom> for Enclave where T: TryFrom> + Into>, { @@ -121,7 +124,7 @@ where } } -impl PartialEq for Enclave +impl PartialEq for Enclave where T: PartialEq + TryFrom> + Into>, { @@ -152,7 +155,7 @@ mod tests { let key: Key<32, 16> = Key::new(b"my password", 10_000); let bytes = [0u8, 1u8, 2u8, 3u8, 4u8].to_vec(); - let safe = Enclave::<&str, 8>::from_plain_bytes("metadata", key.pubk, bytes); + let safe = Enclave::from_plain_bytes("metadata", key.pubk, bytes); assert!(safe.is_ok()); assert_eq!(safe.unwrap().metadata, "metadata"); @@ -166,7 +169,7 @@ mod tests { fn it_should_decrypt_enclave() { let key: Key<32, 16> = Key::new(b"my password", 10_000); let bytes = [0u8, 1u8, 2u8, 3u8, 4u8].to_vec(); - let safe = Enclave::<&str, 8>::from_plain_bytes("metadata", key.pubk, bytes.clone()).unwrap(); + let safe = Enclave::from_plain_bytes("metadata", key.pubk, bytes.clone()).unwrap(); let decrypted_bytes = safe.decrypt(key.pubk); @@ -178,7 +181,7 @@ mod tests { fn it_should_fail_with_wrong_key() { let key: Key<32, 16> = Key::new(b"my password", 10_000); let bytes = [0u8, 1u8, 2u8, 3u8, 4u8].to_vec(); - let safe = Enclave::<&str, 8>::from_plain_bytes("metadata", key.pubk, bytes.clone()).unwrap(); + let safe = Enclave::from_plain_bytes("metadata", key.pubk, bytes.clone()).unwrap(); let wrong_key: Key<32, 16> = Key::new(b"my wrong password", 10_000); let decrypted_bytes = safe.decrypt(wrong_key.pubk).unwrap(); @@ -190,8 +193,7 @@ mod tests { fn it_should_serialize_and_deserialize_to_bytes() { let key: Key<32, 16> = Key::new(b"my password", 10_000); let bytes = [0u8, 1u8, 2u8, 3u8, 4u8].to_vec(); - let enclave = - Enclave::<[u8; 2], 8>::from_plain_bytes([0_u8, 1_u8], key.pubk, bytes.clone()).unwrap(); + let enclave = Enclave::from_plain_bytes([0_u8, 1_u8], key.pubk, bytes.clone()).unwrap(); let serialized: Vec = enclave.clone().into(); let deserialized = Enclave::try_from(serialized).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 022cda7..68c32bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ pub use cipher; /// let key: Key<32, 16> = Key::new(b"my password", 1_000); /// /// // Encrypt data: Utilize the Enclave to securely encrypt data along with metadata. -/// let enclave = Enclave::<&str, 8>::from_plain_bytes("some metadata", key.pubk, b"some bytes to encrypt".to_vec()).unwrap(); +/// let enclave = Enclave::from_plain_bytes("some metadata", key.pubk, b"some bytes to encrypt".to_vec()).unwrap(); /// /// // Decrypt data: Recover the original bytes from the encrypted enclave. /// let recovered_bytes = enclave.decrypt(key.pubk); diff --git a/src/main.rs b/src/main.rs index 877d7ec..94c99bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use rpassword::prompt_password; use std::fs::{metadata, File}; use std::io::{Read, Write}; -use cipher::{chacha20::NONCE_SIZE, Key}; +use cipher::Key; use enclave::Enclave; /// Defines command line subcommands for the application. @@ -57,7 +57,7 @@ fn main() { /// * `filename` - The name of the file to be encrypted. fn encrypt_file(password: &String, filename: &String) { let encryption_key: Key<32, 16> = Key::new(password.as_bytes(), 900_000); - let enclave = Enclave::<[u8; 16], NONCE_SIZE>::from_plain_bytes( + let enclave = Enclave::from_plain_bytes( encryption_key.salt, encryption_key.pubk, get_file_as_byte_vec(filename), @@ -80,8 +80,7 @@ fn encrypt_file(password: &String, filename: &String) { /// * `filename` - The name of the file to be decrypted. fn decrypt_file(password: &String, filename: &String) { let encrypted_bytes = get_file_as_byte_vec(filename); - let enclave = Enclave::<[u8; 16], NONCE_SIZE>::try_from(encrypted_bytes) - .expect("Unable to deserialize enclave"); + let enclave = Enclave::try_from(encrypted_bytes).expect("Unable to deserialize enclave"); let encryption_key: Key<32, 16> = Key::with_salt(password.as_bytes(), enclave.metadata, 900_000); let recovered_bytes = enclave .decrypt(encryption_key.pubk)