Skip to content

Commit

Permalink
Refactor keyref/encryptable locations
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Oct 4, 2024
1 parent 8652d79 commit fdb0263
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 31 deletions.
10 changes: 5 additions & 5 deletions crates/bitwarden-crypto/benches/new_encryptable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};

use bitwarden_crypto::{
key_refs, service::*, CryptoError, EncString, Encryptable, KeyDecryptable, KeyEncryptable,
SymmetricCryptoKey, UsesKey,
};

pub fn criterion_benchmark(c: &mut Criterion) {
let user_key = SymmetricCryptoKey::generate(rand::thread_rng());

Expand Down Expand Up @@ -90,11 +95,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

use bitwarden_crypto::{
key_refs, service::*, CryptoError, EncString, KeyDecryptable, KeyEncryptable,
SymmetricCryptoKey,
};

key_refs! {
#[symmetric]
pub enum MySymmKeyRef {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use super::{
key_ref::{AsymmetricKeyRef, KeyRef, SymmetricKeyRef},
CryptoServiceContext,
};
use crate::{AsymmetricEncString, CryptoError, EncString};

///////////////////////
use super::key_ref::{AsymmetricKeyRef, KeyRef, SymmetricKeyRef};
use crate::{service::CryptoServiceContext, AsymmetricEncString, CryptoError, EncString};

// Just like LocateKey but this time we're not locating anything, just returning a ref

Expand Down Expand Up @@ -63,8 +58,6 @@ impl<
}
}

/////////////////////

pub trait Encryptable<
SymmKeyRef: SymmetricKeyRef,
AsymmKeyRef: AsymmetricKeyRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ macro_rules! key_refs {
$variant $( ($inner) )?
,)+ }

impl $crate::service::key_ref::__internal::KeyRef for $name {
impl $crate::key_ref::__internal::KeyRef for $name {
type KeyValue = key_refs!(@key_type $meta_type);

fn is_local(&self) -> bool {
Expand All @@ -78,8 +78,8 @@ macro_rules! key_refs {
( @key_type symmetric ) => { $crate::SymmetricCryptoKey };
( @key_type asymmetric ) => { $crate::AsymmetricCryptoKey };

( @key_trait symmetric $name:ident ) => { impl $crate::service::key_ref::SymmetricKeyRef for $name {} };
( @key_trait asymmetric $name:ident ) => { impl $crate::service::key_ref::AsymmetricKeyRef for $name {} };
( @key_trait symmetric $name:ident ) => { impl $crate::key_ref::SymmetricKeyRef for $name {} };
( @key_trait asymmetric $name:ident ) => { impl $crate::key_ref::AsymmetricKeyRef for $name {} };

( @variant_match $variant:ident ( $inner:ty ) ) => { $variant (_) };
( @variant_match $variant:ident ) => { $variant };
Expand Down
5 changes: 5 additions & 0 deletions crates/bitwarden-crypto/src/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
mod key_encryptable;
pub use key_encryptable::{CryptoKey, KeyContainer, KeyDecryptable, KeyEncryptable, LocateKey};
mod encryptable;
pub use encryptable::{Decryptable, Encryptable, UsesKey, UsingKey, UsingKeyExt};
pub mod key_ref;
pub(crate) use key_ref::KeyRef;
pub use key_ref::{AsymmetricKeyRef, SymmetricKeyRef};
mod master_key;
pub use master_key::{
default_argon2_iterations, default_argon2_memory, default_argon2_parallelism,
Expand Down
8 changes: 4 additions & 4 deletions crates/bitwarden-crypto/src/service/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ impl<
Ok(())
}

pub(super) fn decrypt_data_with_symmetric_key(
pub(crate) fn decrypt_data_with_symmetric_key(
&self,
key: SymmKeyRef,
data: &EncString,
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<
}
}

pub(super) fn encrypt_data_with_symmetric_key(
pub(crate) fn encrypt_data_with_symmetric_key(
&self,
key: SymmKeyRef,
data: &[u8],
Expand All @@ -274,7 +274,7 @@ impl<
)
}

pub(super) fn decrypt_data_with_asymmetric_key(
pub(crate) fn decrypt_data_with_asymmetric_key(
&self,
key: AsymmKeyRef,
data: &AsymmetricEncString,
Expand All @@ -297,7 +297,7 @@ impl<
.map_err(|_| CryptoError::KeyDecrypt)
}

pub(super) fn encrypt_data_with_asymmetric_key(
pub(crate) fn encrypt_data_with_asymmetric_key(
&self,
key: AsymmKeyRef,
data: &[u8],
Expand Down
8 changes: 3 additions & 5 deletions crates/bitwarden-crypto/src/service/key_store/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use std::marker::PhantomData;

use zeroize::ZeroizeOnDrop;

use crate::KeyRef;

use super::KeyStore;
use crate::service::key_ref::KeyRef;

/// This trait represents some data stored sequentially in memory, with a fixed size.
/// We use this to abstract the implementation over Vec/Box<[u8]/NonNull<[u8]>, which
Expand Down Expand Up @@ -271,10 +272,7 @@ pub(crate) mod tests {
use zeroize::Zeroize;

use super::*;
use crate::{
service::{key_ref::KeyRef, key_store::implementation::rust_slice::RustKeyStore},
CryptoKey,
};
use crate::{service::key_store::implementation::rust_slice::RustKeyStore, CryptoKey, KeyRef};

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TestKey {
Expand Down
11 changes: 6 additions & 5 deletions crates/bitwarden-crypto/src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::sync::{Arc, RwLock};

use crate::{AsymmetricCryptoKey, SymmetricCryptoKey};
use crate::{
AsymmetricCryptoKey, AsymmetricKeyRef, Decryptable, Encryptable, KeyRef, SymmetricCryptoKey,
SymmetricKeyRef, UsesKey,
};

mod context;
mod encryptable;
pub mod key_ref;

mod key_store;

use context::ReadWriteGlobal;
pub use context::{CryptoServiceContext, ReadOnlyGlobal};
pub use encryptable::{Decryptable, Encryptable, UsesKey, UsingKey, UsingKeyExt};
use key_ref::{AsymmetricKeyRef, KeyRef, SymmetricKeyRef};

pub use key_store::create_key_store;
use key_store::KeyStore;

Expand Down

0 comments on commit fdb0263

Please sign in to comment.