Skip to content

Commit

Permalink
crypto: replace custom Blake2s with RustCrypto crate implementation (c…
Browse files Browse the repository at this point in the history
…loudflare#277)

* Add benchmarks for blake2 crate

* Replace custom Blake2s with RustCrypto crate implementation
  • Loading branch information
jeff-hiner authored Jun 30, 2022
1 parent ceb0a05 commit 16cb513
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 1,841 deletions.
42 changes: 41 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions boringtun/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ x25519-dalek = { version = "2.0.0-pre.1", features = ["reusable_secrets"] }
rand_core = { version = "0.6.3", features = ["getrandom"]}
chacha20poly1305 = "0.10.0-pre"
aead = "0.4.3"
blake2 = "0.10"
hmac = "0.12"

[dev-dependencies]
tracing-subscriber = "0.3"
Expand Down
81 changes: 74 additions & 7 deletions boringtun/benches/crypto_benches/blake2s_benching.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,90 @@
use boringtun::crypto::Blake2s;
use blake2::digest::{FixedOutput, KeyInit};
use blake2::{Blake2s256, Blake2sMac, Digest};
use criterion::{BenchmarkId, Criterion, Throughput};
use ring::rand::{SecureRandom, SystemRandom};

pub fn bench_blake2s(c: &mut Criterion) {
let mut group = c.benchmark_group("blake2s");
pub fn bench_blake2s_hash(c: &mut Criterion) {
let mut group = c.benchmark_group("blake2s_hash");

group.sample_size(1000);

for size in [128, 1024] {
for size in [32, 64, 128] {
group.throughput(Throughput::Bytes(size as u64));

group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, _| {
group.bench_with_input(BenchmarkId::new("blake2s_crate", size), &size, |b, _| {
let buf_in = vec![0u8; size];

b.iter(|| {
Blake2s::new_hash().hash(&buf_in).finalize();
buf_in.len()
let mut hasher = Blake2s256::new();
hasher.update(&buf_in);
hasher.finalize();
});
});
}

group.finish();
}

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

group.sample_size(1000);

for size in [16, 32] {
group.throughput(Throughput::Bytes(size as u64));

group.bench_with_input(BenchmarkId::new("blake2s_crate", size), &size, |b, _| {
let buf_in = vec![0u8; size];
let rng = SystemRandom::new();

b.iter_batched(
|| {
let mut key = [0u8; 32];
rng.fill(&mut key).unwrap();
key
},
|key| {
use blake2::digest::Update;
type HmacBlake2s = hmac::SimpleHmac<blake2::Blake2s256>;
let mut hmac = HmacBlake2s::new_from_slice(&key).unwrap();
hmac.update(&buf_in);
hmac.finalize_fixed();
},
criterion::BatchSize::SmallInput,
);
});
}

group.finish();
}

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

group.sample_size(1000);

for size in [128, 1024] {
group.throughput(Throughput::Bytes(size as u64));

group.bench_with_input(BenchmarkId::new("blake2s_crate", size), &size, |b, _| {
let buf_in = vec![0u8; size];
let rng = SystemRandom::new();

b.iter_batched(
|| {
let mut key = [0u8; 16];
rng.fill(&mut key).unwrap();
key
},
|key| -> [u8; 16] {
let mut hmac = Blake2sMac::new_from_slice(&key).unwrap();
blake2::digest::Update::update(&mut hmac, &buf_in);
hmac.finalize_fixed().into()
},
criterion::BatchSize::SmallInput,
);
});
}

group.finish();
}
6 changes: 4 additions & 2 deletions boringtun/benches/crypto_benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blake2s_benching::bench_blake2s;
use blake2s_benching::{bench_blake2s_hash, bench_blake2s_hmac, bench_blake2s_keyed};
use chacha20poly1305_benching::bench_chacha20poly1305;
use x25519_public_key_benching::bench_x25519_public_key;
use x25519_shared_key_benching::bench_x25519_shared_key;
Expand All @@ -11,7 +11,9 @@ mod x25519_shared_key_benching;
criterion::criterion_group!(
crypto_benches,
bench_chacha20poly1305,
bench_blake2s,
bench_blake2s_hash,
bench_blake2s_hmac,
bench_blake2s_keyed,
bench_x25519_shared_key,
bench_x25519_public_key
);
Expand Down
Loading

0 comments on commit 16cb513

Please sign in to comment.