Skip to content

Commit

Permalink
Remove using
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Oct 7, 2024
1 parent f882fb2 commit bec4786
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 54 deletions.
55 changes: 2 additions & 53 deletions crates/bitwarden-crypto/src/keys/encryptable.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,12 @@
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

/// This trait should be implemented by any struct capable of knowing which key it needs
/// to encrypt or decrypt itself.
pub trait UsesKey<Key: KeyRef> {
fn uses_key(&self) -> Key;
}

// This extension trait allows any type to be wrapped with `UsingKey`
// to make it easy to encrypt/decrypt it with the desired key,
// this way we don't need to have a separate `encrypt_with_key` function
pub trait UsingKeyExt<Key: KeyRef>: Sized {
fn using_key(self, key: Key) -> UsingKey<Key, Self> {
UsingKey { key, value: self }
}
}
impl<Key: KeyRef, T> UsingKeyExt<Key> for T {}
pub struct UsingKey<Key: KeyRef, T: ?Sized> {
key: Key,
value: T,
}
impl<Key: KeyRef, T> UsesKey<Key> for UsingKey<Key, T> {
fn uses_key(&self) -> Key {
self.key
}
}
impl<
SymmKeyRef: SymmetricKeyRef,
AsymmKeyRef: AsymmetricKeyRef,
Key: KeyRef,
T: Encryptable<SymmKeyRef, AsymmKeyRef, Key, Output>,
Output,
> Encryptable<SymmKeyRef, AsymmKeyRef, Key, Output> for UsingKey<Key, T>
{
fn encrypt(
&self,
ctx: &mut CryptoServiceContext<SymmKeyRef, AsymmKeyRef>,
_key: Key,
) -> Result<Output, crate::CryptoError> {
self.value.encrypt(ctx, self.key)
}
}
impl<
SymmKeyRef: SymmetricKeyRef,
AsymmKeyRef: AsymmetricKeyRef,
Key: KeyRef,
T: Decryptable<SymmKeyRef, AsymmKeyRef, Key, Output>,
Output,
> Decryptable<SymmKeyRef, AsymmKeyRef, Key, Output> for UsingKey<Key, T>
{
fn decrypt(
&self,
ctx: &mut CryptoServiceContext<SymmKeyRef, AsymmKeyRef>,
_key: Key,
) -> Result<Output, crate::CryptoError> {
self.value.decrypt(ctx, self.key)
}
}

pub trait Encryptable<
SymmKeyRef: SymmetricKeyRef,
AsymmKeyRef: AsymmetricKeyRef,
Expand Down
2 changes: 1 addition & 1 deletion crates/bitwarden-crypto/src/keys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod key_encryptable;
pub use key_encryptable::{CryptoKey, KeyContainer, KeyDecryptable, KeyEncryptable, LocateKey};
mod encryptable;
pub use encryptable::{Decryptable, Encryptable, UsesKey, UsingKey, UsingKeyExt};
pub use encryptable::{Decryptable, Encryptable, UsesKey};
pub mod key_ref;
pub(crate) use key_ref::KeyRef;
pub use key_ref::{AsymmetricKeyRef, SymmetricKeyRef};
Expand Down

0 comments on commit bec4786

Please sign in to comment.