Skip to content
This repository has been archived by the owner on May 30, 2020. It is now read-only.

Commit

Permalink
cargo fmt -p crypto_bench_ring
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jul 14, 2019
1 parent 679925d commit 26665b6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 66 deletions.
73 changes: 41 additions & 32 deletions ring/aead.rs
@@ -1,11 +1,16 @@
// TODO: The BoringSSL benchmarks align the input/output buffers to 16-bytes
// boundaries. Should we?

use ring::{aead::{self, BoundKey}, error, rand};
use ring::{
aead::{self, BoundKey},
error, rand,
};

struct NonceSequence(u64);
impl NonceSequence {
fn new() -> Self { Self(0) }
fn new() -> Self {
Self(0)
}
}

impl aead::NonceSequence for NonceSequence {
Expand All @@ -17,18 +22,23 @@ impl aead::NonceSequence for NonceSequence {
}
}

fn generate_sealing_key(algorithm: &'static aead::Algorithm, rng: &dyn rand::SecureRandom)
-> Result<aead::SealingKey<NonceSequence>, error::Unspecified> {
fn generate_sealing_key(
algorithm: &'static aead::Algorithm,
rng: &dyn rand::SecureRandom,
) -> Result<aead::SealingKey<NonceSequence>, error::Unspecified> {
let mut key_bytes = vec![0u8; algorithm.key_len()];
rng.fill(&mut key_bytes)?;
let key = aead::UnboundKey::new(algorithm, &key_bytes)?;
Ok(aead::SealingKey::new(key, NonceSequence::new()))
}

fn seal_in_place_bench(algorithm: &'static aead::Algorithm,
rng: &dyn rand::SecureRandom,
chunk_len: usize, aad: &[u8],
b: &mut test::Bencher) {
fn seal_in_place_bench(
algorithm: &'static aead::Algorithm,
rng: &dyn rand::SecureRandom,
chunk_len: usize,
aad: &[u8],
b: &mut test::Bencher,
) {
let out_suffix_capacity = algorithm.tag_len();
let mut in_out = vec![0u8; chunk_len + out_suffix_capacity];

Expand All @@ -38,23 +48,22 @@ fn seal_in_place_bench(algorithm: &'static aead::Algorithm,
let mut key = generate_sealing_key(algorithm, rng).unwrap();
b.iter(|| {
let aad = aead::Aad::from(aad);
key.seal_in_place(aad, &mut in_out,out_suffix_capacity).unwrap();


key.seal_in_place(aad, &mut in_out, out_suffix_capacity)
.unwrap();
});
}

macro_rules! ring_seal_in_place_bench {
( $benchmark_name:ident, $algorithm:expr, $chunk_len:expr, $ad:expr ) => {
#[bench]
fn $benchmark_name(b: &mut test::Bencher) {
use super::super::seal_in_place_bench;
use ring::aead;
use ring::rand::SystemRandom;
use super::super::seal_in_place_bench;
let rng = SystemRandom::new();
seal_in_place_bench($algorithm, &rng, $chunk_len, $ad, b);
}
}
};
}

macro_rules! ring_seal_in_place_benches {
Expand All @@ -63,35 +72,35 @@ macro_rules! ring_seal_in_place_benches {
use crypto_bench;

// A TLS 1.2 finished message.
ring_seal_in_place_bench!(tls12_finished, $algorithm,
crypto_bench::aead::TLS12_FINISHED_LEN,
&crypto_bench::aead::TLS12_AD);
ring_seal_in_place_bench!(tls13_finished, $algorithm,
crypto_bench::aead::TLS13_FINISHED_LEN,
&crypto_bench::aead::TLS13_AD);
ring_seal_in_place_bench!(
tls12_finished,
$algorithm,
crypto_bench::aead::TLS12_FINISHED_LEN,
&crypto_bench::aead::TLS12_AD
);
ring_seal_in_place_bench!(
tls13_finished,
$algorithm,
crypto_bench::aead::TLS13_FINISHED_LEN,
&crypto_bench::aead::TLS13_AD
);

// For comparison with BoringSSL.
ring_seal_in_place_bench!(tls12_16, $algorithm, 16,
&crypto_bench::aead::TLS12_AD);
ring_seal_in_place_bench!(tls12_16, $algorithm, 16, &crypto_bench::aead::TLS12_AD);

// ~1 packet of data in TLS.
ring_seal_in_place_bench!(tls12_1350, $algorithm, 1350,
&crypto_bench::aead::TLS12_AD);
ring_seal_in_place_bench!(tls13_1350, $algorithm, 1350,
&crypto_bench::aead::TLS13_AD);
ring_seal_in_place_bench!(tls12_1350, $algorithm, 1350, &crypto_bench::aead::TLS12_AD);
ring_seal_in_place_bench!(tls13_1350, $algorithm, 1350, &crypto_bench::aead::TLS13_AD);

// For comparison with BoringSSL.
ring_seal_in_place_bench!(tls12_8192, $algorithm, 8192,
&crypto_bench::aead::TLS12_AD);
ring_seal_in_place_bench!(tls13_8192, $algorithm, 8192,
&crypto_bench::aead::TLS13_AD);
ring_seal_in_place_bench!(tls12_8192, $algorithm, 8192, &crypto_bench::aead::TLS12_AD);
ring_seal_in_place_bench!(tls13_8192, $algorithm, 8192, &crypto_bench::aead::TLS13_AD);
}
}
};
}

mod seal_in_place {
ring_seal_in_place_benches!(aes_128_gcm, &aead::AES_128_GCM);
ring_seal_in_place_benches!(aes_256_gcm, &aead::AES_256_GCM);
ring_seal_in_place_benches!(chacha20_poly1305,
&aead::CHACHA20_POLY1305);
ring_seal_in_place_benches!(chacha20_poly1305, &aead::CHACHA20_POLY1305);
}
81 changes: 47 additions & 34 deletions ring/ring.rs
Expand Up @@ -20,8 +20,8 @@ mod agreement {
fn generate_key_pair(b: &mut test::Bencher) {
let rng = rand::SystemRandom::new();
b.iter(|| {
let private_key = agreement::EphemeralPrivateKey::
generate($alg, &rng).unwrap();
let private_key =
agreement::EphemeralPrivateKey::generate($alg, &rng).unwrap();
let _ = test::black_box(private_key.compute_public_key().unwrap());
});
}
Expand All @@ -30,8 +30,7 @@ mod agreement {
fn generate_private_key(b: &mut test::Bencher) {
let rng = rand::SystemRandom::new();
b.iter(|| {
let _ = agreement::EphemeralPrivateKey::
generate($alg, &rng).unwrap();
let _ = agreement::EphemeralPrivateKey::generate($alg, &rng).unwrap();
});
}

Expand All @@ -46,34 +45,28 @@ mod agreement {
let rng = rand::SystemRandom::new();

// These operations are done by the peer.
let b_private =
agreement::EphemeralPrivateKey::generate($alg, &rng)
.unwrap();
let b_private = agreement::EphemeralPrivateKey::generate($alg, &rng).unwrap();
let b_public = b_private.compute_public_key().unwrap();
let b_public = agreement::UnparsedPublicKey::new($alg, b_public.as_ref());

b.iter(|| {
// These operations are all done in the
// `generate_key_pair` step.
let a_private =
agreement::EphemeralPrivateKey::generate($alg, &rng)
.unwrap();
agreement::EphemeralPrivateKey::generate($alg, &rng).unwrap();
let _a_public = a_private.compute_public_key().unwrap();
agreement::agree_ephemeral(a_private, &b_public, (), |_| {
Ok(())
}).unwrap();
agreement::agree_ephemeral(a_private, &b_public, (), |_| Ok(())).unwrap();
});
}
}
}
};
}

ring_agreement_benches!(p256, &agreement::ECDH_P256);
ring_agreement_benches!(p384, &agreement::ECDH_P384);
ring_agreement_benches!(x25519, &agreement::X25519);
}


mod digest {
macro_rules! ring_digest_benches {
( $name:ident, $algorithm:expr) => {
Expand All @@ -83,7 +76,7 @@ mod digest {
let _ = digest::digest($algorithm, &input);
});
}
}
};
}

ring_digest_benches!(sha1, &digest::SHA1_FOR_LEGACY_USE_ONLY);
Expand All @@ -96,23 +89,44 @@ mod pbkdf2 {
use crypto_bench;
use ring::pbkdf2;

pbkdf2_bench!(hmac_sha256, crypto_bench::SHA256_OUTPUT_LEN, out,
pbkdf2::derive(pbkdf2::PBKDF2_HMAC_SHA256,
crypto_bench::pbkdf2::ITERATIONS,
&crypto_bench::pbkdf2::SALT,
crypto_bench::pbkdf2::PASSWORD, &mut out));

pbkdf2_bench!(hmac_sha384, crypto_bench::SHA384_OUTPUT_LEN, out,
pbkdf2::derive(pbkdf2::PBKDF2_HMAC_SHA384,
crypto_bench::pbkdf2::ITERATIONS,
crypto_bench::pbkdf2::SALT,
crypto_bench::pbkdf2::PASSWORD, &mut out));

pbkdf2_bench!(hmac_sha512, crypto_bench::SHA512_OUTPUT_LEN, out,
pbkdf2::derive(pbkdf2::PBKDF2_HMAC_SHA512,
crypto_bench::pbkdf2::ITERATIONS,
crypto_bench::pbkdf2::SALT,
crypto_bench::pbkdf2::PASSWORD, &mut out));
pbkdf2_bench!(
hmac_sha256,
crypto_bench::SHA256_OUTPUT_LEN,
out,
pbkdf2::derive(
pbkdf2::PBKDF2_HMAC_SHA256,
crypto_bench::pbkdf2::ITERATIONS,
&crypto_bench::pbkdf2::SALT,
crypto_bench::pbkdf2::PASSWORD,
&mut out
)
);

pbkdf2_bench!(
hmac_sha384,
crypto_bench::SHA384_OUTPUT_LEN,
out,
pbkdf2::derive(
pbkdf2::PBKDF2_HMAC_SHA384,
crypto_bench::pbkdf2::ITERATIONS,
crypto_bench::pbkdf2::SALT,
crypto_bench::pbkdf2::PASSWORD,
&mut out
)
);

pbkdf2_bench!(
hmac_sha512,
crypto_bench::SHA512_OUTPUT_LEN,
out,
pbkdf2::derive(
pbkdf2::PBKDF2_HMAC_SHA512,
crypto_bench::pbkdf2::ITERATIONS,
crypto_bench::pbkdf2::SALT,
crypto_bench::pbkdf2::PASSWORD,
&mut out
)
);
}

mod signature {
Expand All @@ -134,8 +148,7 @@ mod signature {
fn sign_empty(b: &mut test::Bencher) {
let rng = rand::SystemRandom::new();
let key_pair = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
let key_pair = signature::Ed25519KeyPair::from_pkcs8(
key_pair.as_ref()).unwrap();
let key_pair = signature::Ed25519KeyPair::from_pkcs8(key_pair.as_ref()).unwrap();
b.iter(|| {
let signature = key_pair.sign(b"");
let _ = signature.as_ref();
Expand Down

0 comments on commit 26665b6

Please sign in to comment.