]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/cipherbench.rs
cipherbench: be conform with rfc7539, use 12 byte nonce
[proxmox-backup.git] / src / bin / cipherbench.rs
CommitLineData
9399c98f
DM
1use failure::*;
2
3// chacha20-poly1305
4
0f375770 5fn rate_test(name: &str, bench: &dyn Fn() -> usize) {
9399c98f
DM
6
7 let start = std::time::SystemTime::now();
8 let duration = std::time::Duration::new(1, 0);
9
10 let mut bytes = 0;
11
12 loop {
13 bytes += bench();
14 let elapsed = start.elapsed().unwrap();
15 if elapsed > duration { break; }
16 }
17
18 let elapsed = start.elapsed().unwrap();
19 let elapsed = (elapsed.as_secs() as f64) +
20 (elapsed.subsec_millis() as f64)/1000.0;
21
22 println!("{} {} MB/s", name, (bytes as f64)/(elapsed*1024.0*1024.0));
23}
24
25
26fn main() -> Result<(), Error> {
27
28 let input = proxmox::sys::linux::random_data(1024*1024)?;
9399c98f 29
a7f67a9a
DM
30 rate_test("crc32", &|| {
31 let mut crchasher = crc32fast::Hasher::new();
32 crchasher.update(&input);
33 let _checksum = crchasher.finalize();
34 input.len()
35 });
36
0f375770
DM
37 rate_test("zstd", &|| {
38 zstd::block::compress(&input, 1).unwrap();
39 input.len()
40 });
9399c98f
DM
41
42 rate_test("sha256", &|| {
43 openssl::sha::sha256(&input);
44 input.len()
45 });
46
0f375770
DM
47 let key = proxmox::sys::linux::random_data(32)?;
48
49 let iv = proxmox::sys::linux::random_data(16)?;
50
9399c98f
DM
51 let cipher = openssl::symm::Cipher::aes_256_gcm();
52
53 rate_test("aes-256-gcm", &|| {
54 let mut tag = [0u8;16];
55 openssl::symm::encrypt_aead(
56 cipher,
57 &key,
58 Some(&iv),
59 b"",
60 &input,
61 &mut tag).unwrap();
62 input.len()
63 });
64
65 let cipher = openssl::symm::Cipher::chacha20_poly1305();
66
67 rate_test("chacha20-poly1305", &|| {
68 let mut tag = [0u8;16];
69 openssl::symm::encrypt_aead(
70 cipher,
71 &key,
4832f53a 72 Some(&iv[..12]),
9399c98f
DM
73 b"",
74 &input,
75 &mut tag).unwrap();
76 input.len()
77 });
78
79 Ok(())
80}