Skip to content

Commit

Permalink
Merge branches 'ps/aes-tests' and 'master' of github.com:bitwarden/sd…
Browse files Browse the repository at this point in the history
…k into ps/aes-tests

# Conflicts:
#	crates/bitwarden/src/crypto/aes_ops.rs
  • Loading branch information
Hinton committed Nov 13, 2023
2 parents a702c27 + 39891e0 commit e051c19
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 50 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ codegen-units = 1

# Using master until 0.25.1 is released to fix https://github.com/mozilla/uniffi-rs/issues/1798
[patch.crates-io]
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "eb97592f8c48a7f5cf02a94662b8b7861a6544f3" }
uniffi_build = { git = "https://github.com/mozilla/uniffi-rs", rev = "eb97592f8c48a7f5cf02a94662b8b7861a6544f3" }
uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs", rev = "eb97592f8c48a7f5cf02a94662b8b7861a6544f3" }
uniffi_core = { git = "https://github.com/mozilla/uniffi-rs", rev = "eb97592f8c48a7f5cf02a94662b8b7861a6544f3" }
uniffi_macros = { git = "https://github.com/mozilla/uniffi-rs", rev = "eb97592f8c48a7f5cf02a94662b8b7861a6544f3" }
uniffi = { git = "https://github.com/mozilla/uniffi-rs", rev = "b369e7c15b1b7ebca34de9028209db11b7ff353d" }
uniffi_build = { git = "https://github.com/mozilla/uniffi-rs", rev = "b369e7c15b1b7ebca34de9028209db11b7ff353d" }
uniffi_bindgen = { git = "https://github.com/mozilla/uniffi-rs", rev = "b369e7c15b1b7ebca34de9028209db11b7ff353d" }
uniffi_core = { git = "https://github.com/mozilla/uniffi-rs", rev = "b369e7c15b1b7ebca34de9028209db11b7ff353d" }
uniffi_macros = { git = "https://github.com/mozilla/uniffi-rs", rev = "b369e7c15b1b7ebca34de9028209db11b7ff353d" }
6 changes: 3 additions & 3 deletions crates/bitwarden-napi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions crates/bitwarden-uniffi/src/vault/sends.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{path::Path, sync::Arc};

use bitwarden::vault::{self, SendListView, SendView};
use bitwarden::vault::{Send, SendListView, SendView};

use crate::{Client, Result};

Expand All @@ -10,12 +10,12 @@ pub struct ClientSends(pub Arc<Client>);
#[uniffi::export]
impl ClientSends {
/// Encrypt send
pub async fn encrypt(&self, send: SendView) -> Result<vault::Send> {
pub async fn encrypt(&self, send: SendView) -> Result<Send> {
Ok(self.0 .0.read().await.vault().sends().encrypt(send).await?)
}

/// Encrypt a send file in memory
pub async fn encrypt_buffer(&self, send: vault::Send, buffer: Vec<u8>) -> Result<Vec<u8>> {
pub async fn encrypt_buffer(&self, send: Send, buffer: Vec<u8>) -> Result<Vec<u8>> {
Ok(self
.0
.0
Expand All @@ -30,7 +30,7 @@ impl ClientSends {
/// Encrypt a send file located in the file system
pub async fn encrypt_file(
&self,
send: vault::Send,
send: Send,
decrypted_file_path: String,
encrypted_file_path: String,
) -> Result<()> {
Expand All @@ -50,12 +50,12 @@ impl ClientSends {
}

/// Decrypt send
pub async fn decrypt(&self, send: vault::Send) -> Result<SendView> {
pub async fn decrypt(&self, send: Send) -> Result<SendView> {
Ok(self.0 .0.read().await.vault().sends().decrypt(send).await?)
}

/// Decrypt send list
pub async fn decrypt_list(&self, sends: Vec<vault::Send>) -> Result<Vec<SendListView>> {
pub async fn decrypt_list(&self, sends: Vec<Send>) -> Result<Vec<SendListView>> {
Ok(self
.0
.0
Expand All @@ -68,7 +68,7 @@ impl ClientSends {
}

/// Decrypt a send file in memory
pub async fn decrypt_buffer(&self, send: vault::Send, buffer: Vec<u8>) -> Result<Vec<u8>> {
pub async fn decrypt_buffer(&self, send: Send, buffer: Vec<u8>) -> Result<Vec<u8>> {
Ok(self
.0
.0
Expand All @@ -83,7 +83,7 @@ impl ClientSends {
/// Decrypt a send file located in the file system
pub async fn decrypt_file(
&self,
send: vault::Send,
send: Send,
encrypted_file_path: String,
decrypted_file_path: String,
) -> Result<()> {
Expand Down
49 changes: 40 additions & 9 deletions crates/bitwarden/src/crypto/aes_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub fn decrypt_aes256_hmac(
/// ## Returns
///
/// A AesCbc256_B64 EncString
#[allow(unused)]
pub fn encrypt_aes256(data_dec: &[u8], key: GenericArray<u8, U32>) -> ([u8; 16], Vec<u8>) {
let rng = rand::thread_rng();
let (iv, data) = encrypt_aes256_internal(rng, data_dec, key);
Expand Down Expand Up @@ -120,17 +121,28 @@ fn validate_mac(mac_key: &[u8], iv: &[u8], data: &[u8]) -> Result<[u8; 32]> {
#[cfg(test)]
mod tests {
use aes::cipher::generic_array::sequence::GenericSequence;
use base64::Engine;
use rand::SeedableRng;

use crate::util::BASE64_ENGINE;

use super::*;

fn generate_array(offset: u8, increment: u8) -> GenericArray<u8, U32> {
/// Helper function for generating a `GenericArray` of size 32 with each element being
/// a multiple of a given increment, starting from a given offset.
fn generate_generic_array(offset: u8, increment: u8) -> GenericArray<u8, U32> {
GenericArray::generate(|i| offset + i as u8 * increment)
}

/// Helper function for generating a vector of a given size with each element being
/// a multiple of a given increment, starting from a given offset.
fn generate_vec(length: usize, offset: u8, increment: u8) -> Vec<u8> {
(0..length).map(|i| offset + i as u8 * increment).collect()
}

#[test]
fn test_encrypt_aes256_internal() {
let key = generate_array(0, 1);
let key = generate_generic_array(0, 1);

let rng = rand_chacha::ChaCha8Rng::from_seed([0u8; 32]);
let result = encrypt_aes256_internal(rng, "EncryptMe!".as_bytes(), key);
Expand All @@ -143,21 +155,40 @@ mod tests {
);
}

fn generate_array2(length: usize, offset: u8, increment: u8) -> Vec<u8> {
(0..length).map(|i| offset + i as u8 * increment).collect()
}

#[test]
fn test_validate_mac() {
let mac_key = generate_array2(16, 0, 16);
let mac_key = generate_vec(16, 0, 16);

let iv = generate_array2(16, 0, 16);
let data = generate_array2(16, 0, 16);
let iv = generate_vec(16, 0, 16);
let data = generate_vec(16, 0, 16);

let result = validate_mac(&mac_key, &iv, &data);

assert!(result.is_ok());
let mac = result.unwrap();
assert_eq!(mac.len(), 32);
}

#[test]
fn test_decrypt_aes256() {
let iv = generate_vec(16, 0, 1);
let iv: &[u8; 16] = iv.as_slice().try_into().unwrap();
let key = generate_generic_array(0, 1);
let data = BASE64_ENGINE.decode("ByUF8vhyX4ddU9gcooznwA==").unwrap();

let decrypted = decrypt_aes256(iv, data, key).unwrap();

assert_eq!(String::from_utf8(decrypted).unwrap(), "EncryptMe!");
}

#[test]
fn test_encrypt_decrypt_aes256() {
let key = generate_generic_array(0, 1);
let data = "EncryptMe!";

let (iv, encrypted) = encrypt_aes256(data.as_bytes(), key);
let decrypted = decrypt_aes256(&iv, encrypted, key).unwrap();

assert_eq!(String::from_utf8(decrypted).unwrap(), "EncryptMe!");
}
}
2 changes: 1 addition & 1 deletion crates/bitwarden/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub use encryptable::{Decryptable, Encryptable, LocateKey};
mod key_encryptable;
pub use key_encryptable::{KeyDecryptable, KeyEncryptable};
mod aes_ops;
pub use aes_ops::{decrypt_aes256, decrypt_aes256_hmac, encrypt_aes256, encrypt_aes256_hmac};
use aes_ops::{decrypt_aes256_hmac, encrypt_aes256_hmac};
mod symmetric_crypto_key;
pub use symmetric_crypto_key::SymmetricCryptoKey;
mod shareable_key;
Expand Down
7 changes: 5 additions & 2 deletions crates/bitwarden/src/mobile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ pub mod crypto;
pub mod kdf;
pub mod vault;

pub(crate) mod client_crypto;
pub(crate) mod client_kdf;
mod client_crypto;
mod client_kdf;

pub use client_crypto::ClientCrypto;
pub use client_kdf::ClientKdf;

// Usually we wouldn't want to expose EncStrings in the API or the schemas,
// but we need them in the mobile API, so define it here to limit the scope
Expand Down
7 changes: 7 additions & 0 deletions crates/bitwarden/src/mobile/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ mod client_password_history;
mod client_sends;
mod client_totp;
mod client_vault;

pub use client_ciphers::ClientCiphers;
pub use client_collection::ClientCollections;
pub use client_folders::ClientFolders;
pub use client_password_history::ClientPasswordHistory;
pub use client_sends::ClientSends;
pub use client_vault::ClientVault;
3 changes: 3 additions & 0 deletions crates/bitwarden/src/secrets_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub mod secrets;

mod client_projects;
mod client_secrets;

pub use client_projects::ClientProjects;
pub use client_secrets::ClientSecrets;
1 change: 1 addition & 0 deletions crates/bitwarden/src/tool/exporters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
};

mod client_exporter;
pub use client_exporter::ClientExporters;

#[derive(JsonSchema)]
#[cfg_attr(feature = "mobile", derive(uniffi::Enum))]
Expand Down
1 change: 1 addition & 0 deletions crates/bitwarden/src/tool/generators/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod client_generator;
mod password;

pub use client_generator::ClientGenerator;
pub use password::{PassphraseGeneratorRequest, PasswordGeneratorRequest};
4 changes: 2 additions & 2 deletions crates/bitwarden/src/tool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod exporters;
mod generators;

pub use exporters::ExportFormat;
pub use generators::{PassphraseGeneratorRequest, PasswordGeneratorRequest};
pub use exporters::{ClientExporters, ExportFormat};
pub use generators::{ClientGenerator, PassphraseGeneratorRequest, PasswordGeneratorRequest};
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"devDependencies": {
"@openapitools/openapi-generator-cli": "2.7.0",
"handlebars": "^4.7.8",
"prettier": "3.0.3",
"quicktype-core": "23.0.76",
"prettier": "3.1.0",
"quicktype-core": "23.0.77",
"rimraf": "5.0.5",
"ts-node": "10.9.1",
"typescript": "5.2.2"
Expand Down

0 comments on commit e051c19

Please sign in to comment.