From 9399c98f82f5b83ef1383281ccfb9f5d2141f830 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 13 Jun 2019 17:16:43 +0200 Subject: [PATCH] src/bin/cipherbench.rs: cipher speed test with large blocks --- src/bin/cipherbench.rs | 70 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/bin/cipherbench.rs diff --git a/src/bin/cipherbench.rs b/src/bin/cipherbench.rs new file mode 100644 index 00000000..22f0f3dc --- /dev/null +++ b/src/bin/cipherbench.rs @@ -0,0 +1,70 @@ +use failure::*; + +// chacha20-poly1305 + +fn rate_test(name: &str, bench: & Fn() -> usize) { + + let start = std::time::SystemTime::now(); + let duration = std::time::Duration::new(1, 0); + + let mut bytes = 0; + + loop { + bytes += bench(); + let elapsed = start.elapsed().unwrap(); + if elapsed > duration { break; } + } + + let elapsed = start.elapsed().unwrap(); + let elapsed = (elapsed.as_secs() as f64) + + (elapsed.subsec_millis() as f64)/1000.0; + + println!("{} {} MB/s", name, (bytes as f64)/(elapsed*1024.0*1024.0)); +} + + +fn main() -> Result<(), Error> { + + let input = proxmox::sys::linux::random_data(1024*1024)?; + let key = proxmox::sys::linux::random_data(32)?; + + let iv = proxmox::sys::linux::random_data(16)?; + + let start = std::time::SystemTime::now(); + let duration = std::time::Duration::new(1, 0); + + rate_test("sha256", &|| { + openssl::sha::sha256(&input); + input.len() + }); + + let cipher = openssl::symm::Cipher::aes_256_gcm(); + + rate_test("aes-256-gcm", &|| { + let mut tag = [0u8;16]; + openssl::symm::encrypt_aead( + cipher, + &key, + Some(&iv), + b"", + &input, + &mut tag).unwrap(); + input.len() + }); + + let cipher = openssl::symm::Cipher::chacha20_poly1305(); + + rate_test("chacha20-poly1305", &|| { + let mut tag = [0u8;16]; + openssl::symm::encrypt_aead( + cipher, + &key, + Some(&iv), + b"", + &input, + &mut tag).unwrap(); + input.len() + }); + + Ok(()) +}