Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BEEEP] Send chunked decryption #203

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 180 additions & 1 deletion Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,10 @@ bitwarden-api-identity = { path = "../bitwarden-api-identity", version = "=0.2.1
bitwarden-api-api = { path = "../bitwarden-api-api", version = "=0.2.1" }

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
tokio = { version = "1.28.2", features = ["rt", "macros"] }
wiremock = "0.5.18"

[[bench]]
name = "chunked_decryption"
harness = false
67 changes: 67 additions & 0 deletions crates/bitwarden/benches/chunked_decryption.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::io::Write;

use bitwarden::crypto::{encrypt_aes256_hmac, ChunkedDecryptor, SymmetricCryptoKey};
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use rand::RngCore;

struct SizeFmt(usize);
impl std::fmt::Display for SizeFmt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
const SUFFIXES: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
let mut size = self.0 as f64;
for suffix in SUFFIXES {
if size < 1024.0 {
return write!(f, "{:.1}{}", size, suffix);
}
size /= 1024.0;
}
write!(f, "{}", self.0)
}
}

pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("decryption");

for size in [100 * 1024, 15 * 1024 * 1024, 200 * 1024 * 1024] {
group.throughput(criterion::Throughput::Bytes(size as u64));
if size > 1024 * 1024 {
group.sample_size(20);
}

let mut initial_buf = Vec::with_capacity(size);
initial_buf.resize(size, 0);
rand::thread_rng().fill_bytes(&mut initial_buf[..size]);
let key: SymmetricCryptoKey = SymmetricCryptoKey::generate("test");
let enc_str = encrypt_aes256_hmac(&initial_buf, key.mac_key.unwrap(), key.key).unwrap();
let enc_buf = enc_str.to_buffer().unwrap();

group.bench_with_input(
BenchmarkId::new("decrypt_with_key", SizeFmt(size)),
&size,
|b, _size| b.iter(|| black_box(enc_str.decrypt_with_key(&key).unwrap())),
);

for chunk_size in [64, 2048, 8192] {
group.bench_with_input(
BenchmarkId::new(format!("ChunkedDecryptor[{chunk_size}]"), SizeFmt(size)),
&size,
|b, _size| {
b.iter(|| {
let mut decrypted_buf = Vec::with_capacity(size);
let mut cd = ChunkedDecryptor::new(&key, &mut decrypted_buf);

for chunk in enc_buf.chunks(chunk_size) {
cd.write_all(chunk).unwrap();
}
cd.finalize().unwrap();

//
})
},
);
}
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading
Loading