1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #![allow(missing_docs, clippy::expect_used)]
16
17 use criterion::{criterion_group, criterion_main, Criterion};
18 use hex_literal::hex;
19
20 use crypto_provider::hkdf::Hkdf;
21 use crypto_provider::CryptoProvider;
22 use crypto_provider_default::CryptoProviderImpl;
23
24 // simple benchmark, which creates a new hmac, updates once, then finalizes
hkdf_sha256_operations<C: CryptoProvider>(c: &mut Criterion)25 fn hkdf_sha256_operations<C: CryptoProvider>(c: &mut Criterion) {
26 let ikm = hex!("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
27 let salt = hex!("000102030405060708090a0b0c");
28 let info = hex!("f0f1f2f3f4f5f6f7f8f9");
29
30 let _ =
31 c.bench_function(&format!("bench hkdf with salt {}", std::any::type_name::<C>()), |b| {
32 b.iter(|| {
33 let hk = C::HkdfSha256::new(Some(&salt[..]), &ikm);
34 let mut okm = [0u8; 42];
35 hk.expand(&info, &mut okm).expect("42 is a valid length for Sha256 to output");
36 });
37 });
38
39 let _ = c.bench_function(&format!("bench hkdf no salt {}", std::any::type_name::<C>()), |b| {
40 b.iter(|| {
41 let hk = C::HkdfSha256::new(None, &ikm);
42 let mut okm = [0u8; 42];
43 hk.expand(&info, &mut okm).expect("42 is a valid length for Sha256 to output");
44 });
45 });
46 }
47
48 criterion_group!(benches, hkdf_sha256_operations::<CryptoProviderImpl>);
49
50 criterion_main!(benches);
51