• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)]
16 
17 use criterion::{criterion_group, criterion_main, Criterion};
18 
19 use crypto_provider::hmac::Hmac;
20 use crypto_provider::{CryptoProvider, CryptoRng};
21 use crypto_provider_default::CryptoProviderImpl;
22 use rand_ext::random_bytes;
23 
24 // simple benchmark, which creates a new hmac, updates once, then finalizes
hmac_sha256_operations<C: CryptoProvider>(c: &mut Criterion)25 fn hmac_sha256_operations<C: CryptoProvider>(c: &mut Criterion) {
26     let mut rng = C::CryptoRng::new();
27     let key: [u8; 32] = rand_ext::random_bytes::<32, C>(&mut rng);
28     let update_data: [u8; 16] = rand_ext::random_bytes::<16, C>(&mut rng);
29 
30     let _ = c.bench_function("bench for hmac sha256 single update", |b| {
31         b.iter(|| {
32             let mut hmac = C::HmacSha256::new_from_key(key);
33             hmac.update(&update_data);
34             let _result = hmac.finalize();
35         });
36     });
37 }
38 
hmac_sha512_operations<C: CryptoProvider>(c: &mut Criterion)39 fn hmac_sha512_operations<C: CryptoProvider>(c: &mut Criterion) {
40     let mut rng = C::CryptoRng::new();
41     let key: [u8; 64] = rand_ext::random_bytes::<64, C>(&mut rng);
42     let update_data: [u8; 16] = random_bytes::<16, C>(&mut rng);
43 
44     let _ = c.bench_function("bench for hmac sha512 single update", |b| {
45         b.iter(|| {
46             let mut hmac = C::HmacSha512::new_from_key(key);
47             hmac.update(&update_data);
48             let _result = hmac.finalize();
49         });
50     });
51 }
52 
53 criterion_group!(
54     benches,
55     hmac_sha256_operations::<CryptoProviderImpl>,
56     hmac_sha512_operations::<CryptoProviderImpl>,
57 );
58 
59 criterion_main!(benches);
60