1 // Copyright (c) 2019-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 
6 #include <bench/bench.h>
7 #include <crypto/chacha_poly_aead.h>
8 #include <crypto/poly1305.h> // for the POLY1305_TAGLEN constant
9 #include <hash.h>
10 
11 #include <assert.h>
12 #include <limits>
13 
14 /* Number of bytes to process per iteration */
15 static constexpr uint64_t BUFFER_SIZE_TINY = 64;
16 static constexpr uint64_t BUFFER_SIZE_SMALL = 256;
17 static constexpr uint64_t BUFFER_SIZE_LARGE = 1024 * 1024;
18 
19 static const unsigned char k1[32] = {0};
20 static const unsigned char k2[32] = {0};
21 
22 static ChaCha20Poly1305AEAD aead(k1, 32, k2, 32);
23 
CHACHA20_POLY1305_AEAD(benchmark::Bench & bench,size_t buffersize,bool include_decryption)24 static void CHACHA20_POLY1305_AEAD(benchmark::Bench& bench, size_t buffersize, bool include_decryption)
25 {
26     std::vector<unsigned char> in(buffersize + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
27     std::vector<unsigned char> out(buffersize + CHACHA20_POLY1305_AEAD_AAD_LEN + POLY1305_TAGLEN, 0);
28     uint64_t seqnr_payload = 0;
29     uint64_t seqnr_aad = 0;
30     int aad_pos = 0;
31     uint32_t len = 0;
32     bench.batch(buffersize).unit("byte").run([&] {
33         // encrypt or decrypt the buffer with a static key
34         const bool crypt_ok_1 = aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(), out.size(), in.data(), buffersize, true);
35         assert(crypt_ok_1);
36 
37         if (include_decryption) {
38             // if we decrypt, include the GetLength
39             const bool get_length_ok = aead.GetLength(&len, seqnr_aad, aad_pos, in.data());
40             assert(get_length_ok);
41             const bool crypt_ok_2 = aead.Crypt(seqnr_payload, seqnr_aad, aad_pos, out.data(), out.size(), in.data(), buffersize, true);
42             assert(crypt_ok_2);
43         }
44 
45         // increase main sequence number
46         seqnr_payload++;
47         // increase aad position (position in AAD keystream)
48         aad_pos += CHACHA20_POLY1305_AEAD_AAD_LEN;
49         if (aad_pos + CHACHA20_POLY1305_AEAD_AAD_LEN > CHACHA20_ROUND_OUTPUT) {
50             aad_pos = 0;
51             seqnr_aad++;
52         }
53         if (seqnr_payload + 1 == std::numeric_limits<uint64_t>::max()) {
54             // reuse of nonce+key is okay while benchmarking.
55             seqnr_payload = 0;
56             seqnr_aad = 0;
57             aad_pos = 0;
58         }
59     });
60 }
61 
CHACHA20_POLY1305_AEAD_64BYTES_ONLY_ENCRYPT(benchmark::Bench & bench)62 static void CHACHA20_POLY1305_AEAD_64BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
63 {
64     CHACHA20_POLY1305_AEAD(bench, BUFFER_SIZE_TINY, false);
65 }
66 
CHACHA20_POLY1305_AEAD_256BYTES_ONLY_ENCRYPT(benchmark::Bench & bench)67 static void CHACHA20_POLY1305_AEAD_256BYTES_ONLY_ENCRYPT(benchmark::Bench& bench)
68 {
69     CHACHA20_POLY1305_AEAD(bench, BUFFER_SIZE_SMALL, false);
70 }
71 
CHACHA20_POLY1305_AEAD_1MB_ONLY_ENCRYPT(benchmark::Bench & bench)72 static void CHACHA20_POLY1305_AEAD_1MB_ONLY_ENCRYPT(benchmark::Bench& bench)
73 {
74     CHACHA20_POLY1305_AEAD(bench, BUFFER_SIZE_LARGE, false);
75 }
76 
CHACHA20_POLY1305_AEAD_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench & bench)77 static void CHACHA20_POLY1305_AEAD_64BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
78 {
79     CHACHA20_POLY1305_AEAD(bench, BUFFER_SIZE_TINY, true);
80 }
81 
CHACHA20_POLY1305_AEAD_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench & bench)82 static void CHACHA20_POLY1305_AEAD_256BYTES_ENCRYPT_DECRYPT(benchmark::Bench& bench)
83 {
84     CHACHA20_POLY1305_AEAD(bench, BUFFER_SIZE_SMALL, true);
85 }
86 
CHACHA20_POLY1305_AEAD_1MB_ENCRYPT_DECRYPT(benchmark::Bench & bench)87 static void CHACHA20_POLY1305_AEAD_1MB_ENCRYPT_DECRYPT(benchmark::Bench& bench)
88 {
89     CHACHA20_POLY1305_AEAD(bench, BUFFER_SIZE_LARGE, true);
90 }
91 
92 // Add Hash() (dbl-sha256) bench for comparison
93 
HASH(benchmark::Bench & bench,size_t buffersize)94 static void HASH(benchmark::Bench& bench, size_t buffersize)
95 {
96     uint8_t hash[CHash256::OUTPUT_SIZE];
97     std::vector<uint8_t> in(buffersize,0);
98     bench.batch(in.size()).unit("byte").run([&] {
99         CHash256().Write(in).Finalize(hash);
100     });
101 }
102 
HASH_64BYTES(benchmark::Bench & bench)103 static void HASH_64BYTES(benchmark::Bench& bench)
104 {
105     HASH(bench, BUFFER_SIZE_TINY);
106 }
107 
HASH_256BYTES(benchmark::Bench & bench)108 static void HASH_256BYTES(benchmark::Bench& bench)
109 {
110     HASH(bench, BUFFER_SIZE_SMALL);
111 }
112 
HASH_1MB(benchmark::Bench & bench)113 static void HASH_1MB(benchmark::Bench& bench)
114 {
115     HASH(bench, BUFFER_SIZE_LARGE);
116 }
117 
118 BENCHMARK(CHACHA20_POLY1305_AEAD_64BYTES_ONLY_ENCRYPT);
119 BENCHMARK(CHACHA20_POLY1305_AEAD_256BYTES_ONLY_ENCRYPT);
120 BENCHMARK(CHACHA20_POLY1305_AEAD_1MB_ONLY_ENCRYPT);
121 BENCHMARK(CHACHA20_POLY1305_AEAD_64BYTES_ENCRYPT_DECRYPT);
122 BENCHMARK(CHACHA20_POLY1305_AEAD_256BYTES_ENCRYPT_DECRYPT);
123 BENCHMARK(CHACHA20_POLY1305_AEAD_1MB_ENCRYPT_DECRYPT);
124 BENCHMARK(HASH_64BYTES);
125 BENCHMARK(HASH_256BYTES);
126 BENCHMARK(HASH_1MB);
127