diff --git a/crates/bitwarden-crypto/benches/new_encryptable.rs b/crates/bitwarden-crypto/benches/new_encryptable.rs index b719484f1..3eff9e98d 100644 --- a/crates/bitwarden-crypto/benches/new_encryptable.rs +++ b/crates/bitwarden-crypto/benches/new_encryptable.rs @@ -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()); @@ -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 { diff --git a/crates/bitwarden-crypto/src/service/encryptable.rs b/crates/bitwarden-crypto/src/keys/encryptable.rs similarity index 96% rename from crates/bitwarden-crypto/src/service/encryptable.rs rename to crates/bitwarden-crypto/src/keys/encryptable.rs index 4fa23618c..8b74d6009 100644 --- a/crates/bitwarden-crypto/src/service/encryptable.rs +++ b/crates/bitwarden-crypto/src/keys/encryptable.rs @@ -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 @@ -63,8 +58,6 @@ impl< } } -///////////////////// - pub trait Encryptable< SymmKeyRef: SymmetricKeyRef, AsymmKeyRef: AsymmetricKeyRef, diff --git a/crates/bitwarden-crypto/src/service/key_ref.rs b/crates/bitwarden-crypto/src/keys/key_ref.rs similarity index 90% rename from crates/bitwarden-crypto/src/service/key_ref.rs rename to crates/bitwarden-crypto/src/keys/key_ref.rs index a26b6d895..39ac2cddc 100644 --- a/crates/bitwarden-crypto/src/service/key_ref.rs +++ b/crates/bitwarden-crypto/src/keys/key_ref.rs @@ -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 { @@ -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 }; diff --git a/crates/bitwarden-crypto/src/keys/mod.rs b/crates/bitwarden-crypto/src/keys/mod.rs index ac1732966..1267fd4fb 100644 --- a/crates/bitwarden-crypto/src/keys/mod.rs +++ b/crates/bitwarden-crypto/src/keys/mod.rs @@ -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, diff --git a/crates/bitwarden-crypto/src/service/context.rs b/crates/bitwarden-crypto/src/service/context.rs index 81a902164..c4ca497a3 100644 --- a/crates/bitwarden-crypto/src/service/context.rs +++ b/crates/bitwarden-crypto/src/service/context.rs @@ -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, @@ -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], @@ -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, @@ -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], diff --git a/crates/bitwarden-crypto/src/service/key_store/slice.rs b/crates/bitwarden-crypto/src/service/key_store/slice.rs index 1bcc688a5..c2143c858 100644 --- a/crates/bitwarden-crypto/src/service/key_store/slice.rs +++ b/crates/bitwarden-crypto/src/service/key_store/slice.rs @@ -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 @@ -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 { diff --git a/crates/bitwarden-crypto/src/service/mod.rs b/crates/bitwarden-crypto/src/service/mod.rs index b54f879d1..907069c2f 100644 --- a/crates/bitwarden-crypto/src/service/mod.rs +++ b/crates/bitwarden-crypto/src/service/mod.rs @@ -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;