Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove Enclave NONCE_SIZE generic #13

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion benches/src/enclave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
});
}
Expand Down
26 changes: 14 additions & 12 deletions enclave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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<T, const NONCE_SIZE: usize> {
pub struct Enclave<T> {
/// Metadata associated with the encrypted data.
pub metadata: T,

Expand All @@ -25,7 +28,7 @@ pub struct Enclave<T, const NONCE_SIZE: usize> {
nonce: [u8; NONCE_SIZE],
}

impl<T, const NONCE_SIZE: usize> Enclave<T, NONCE_SIZE> {
impl<T> Enclave<T> {
/// Creates a new `Enclave` instance from unencrypted data.
///
/// # Arguments
Expand Down Expand Up @@ -66,7 +69,7 @@ impl<T, const NONCE_SIZE: usize> Enclave<T, NONCE_SIZE> {
}
}

impl<T, const NONCE_SIZE: usize> From<Enclave<T, NONCE_SIZE>> for Vec<u8>
impl<T> From<Enclave<T>> for Vec<u8>
where
T: TryFrom<Vec<u8>> + Into<Vec<u8>>,
{
Expand All @@ -77,7 +80,7 @@ where
///
/// # Returns
/// A `Vec<u8>` representing the serialized enclave.
fn from(enclave: Enclave<T, NONCE_SIZE>) -> Vec<u8> {
fn from(enclave: Enclave<T>) -> Vec<u8> {
let mut bytes: Vec<u8> = vec![];
let metadata_bytes = enclave.metadata.into();

Expand All @@ -90,7 +93,7 @@ where
}
}

impl<T, const NONCE_SIZE: usize> TryFrom<Vec<u8>> for Enclave<T, NONCE_SIZE>
impl<T> TryFrom<Vec<u8>> for Enclave<T>
where
T: TryFrom<Vec<u8>> + Into<Vec<u8>>,
{
Expand Down Expand Up @@ -121,7 +124,7 @@ where
}
}

impl<T, const NONCE_SIZE: usize> PartialEq for Enclave<T, NONCE_SIZE>
impl<T> PartialEq for Enclave<T>
where
T: PartialEq + TryFrom<Vec<u8>> + Into<Vec<u8>>,
{
Expand Down Expand Up @@ -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");
Expand All @@ -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);

Expand All @@ -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();
Expand All @@ -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<u8> = enclave.clone().into();
let deserialized = Enclave::try_from(serialized).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand All @@ -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)
Expand Down