diff --git a/crates/bitwarden/src/crypto/mod.rs b/crates/bitwarden/src/crypto/mod.rs index 45c13ce6f..7994fa2f8 100644 --- a/crates/bitwarden/src/crypto/mod.rs +++ b/crates/bitwarden/src/crypto/mod.rs @@ -23,7 +23,10 @@ use aes::cipher::{generic_array::GenericArray, ArrayLength, Unsigned}; use hmac::digest::OutputSizeUser; -use rand::Rng; +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; use crate::error::Result; @@ -76,6 +79,9 @@ fn hkdf_expand>(prk: &[u8], info: Option<&str>) -> Result [u8; 16] { +pub(crate) fn generate_random_bytes() -> T +where + Standard: Distribution, +{ rand::thread_rng().gen() } diff --git a/crates/bitwarden/src/crypto/symmetric_crypto_key.rs b/crates/bitwarden/src/crypto/symmetric_crypto_key.rs index 2fbb2dc4e..741fe0559 100644 --- a/crates/bitwarden/src/crypto/symmetric_crypto_key.rs +++ b/crates/bitwarden/src/crypto/symmetric_crypto_key.rs @@ -4,7 +4,7 @@ use aes::cipher::{generic_array::GenericArray, typenum::U32}; use base64::Engine; use crate::{ - crypto::{derive_shareable_key, generate_16_bytes}, + crypto::{derive_shareable_key, generate_random_bytes}, error::{CryptoError, Error}, util::BASE64_ENGINE, }; @@ -20,7 +20,7 @@ impl SymmetricCryptoKey { const MAC_LEN: usize = 32; pub fn generate(name: &str) -> Self { - let secret = generate_16_bytes(); + let secret: [u8; 16] = generate_random_bytes(); derive_shareable_key(secret, name, None) } diff --git a/crates/bitwarden/src/vault/send.rs b/crates/bitwarden/src/vault/send.rs index c20f4ec6b..eada79ca4 100644 --- a/crates/bitwarden/src/vault/send.rs +++ b/crates/bitwarden/src/vault/send.rs @@ -8,7 +8,7 @@ use uuid::Uuid; use crate::{ crypto::{ - derive_shareable_key, generate_16_bytes, EncString, KeyDecryptable, KeyEncryptable, + derive_shareable_key, generate_random_bytes, EncString, KeyDecryptable, KeyEncryptable, LocateKey, SymmetricCryptoKey, }, error::{CryptoError, Error, Result}, @@ -249,7 +249,10 @@ impl KeyEncryptable for SendView { Some(k) => URL_SAFE_NO_PAD .decode(k) .map_err(|_| CryptoError::InvalidKey)?, - None => generate_16_bytes().to_vec(), + None => { + let key: [u8; 16] = generate_random_bytes(); + key.to_vec() + } }; let send_key = Send::derive_shareable_key(&k)?;