forked from cloudflare/boringtun
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
crypto: replace custom Blake2s with RustCrypto crate implementation (c…
…loudflare#277) * Add benchmarks for blake2 crate * Replace custom Blake2s with RustCrypto crate implementation
- Loading branch information
1 parent
ceb0a05
commit 16cb513
Showing
11 changed files
with
177 additions
and
1,841 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.