Bitcoin ABC  0.22.13
P2P Digital Currency
crypto_aes.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 The Bitcoin 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 #include <bench/bench.h>
6 #include <crypto/aes.h>
7 #include <util/time.h>
8 #include <validation.h>
9 
10 #define BENCH_AES128_ITERATION 800000
11 #define BENCH_AES128CBC_ITERATION 200000
12 #define BENCH_AES256_ITERATION 640000
13 #define BENCH_AES256CBC_ITERATION 160000
14 
15 static void AES128_Encrypt(benchmark::State &state) {
16  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
17  const std::vector<uint8_t> plaintext(16, 0);
18  std::vector<uint8_t> cyphertext(16, 0);
19 
20  while (state.KeepRunning()) {
21  AES128Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data());
22  }
23 }
24 
25 static void AES128_Decrypt(benchmark::State &state) {
26  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
27  const std::vector<uint8_t> cyphertext(16, 0);
28  std::vector<uint8_t> plaintext(16, 0);
29 
30  while (state.KeepRunning()) {
31  AES128Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data());
32  }
33 }
34 
35 static void AES256_Encrypt(benchmark::State &state) {
36  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
37  const std::vector<uint8_t> plaintext(16, 0);
38  std::vector<uint8_t> cyphertext(16, 0);
39 
40  while (state.KeepRunning()) {
41  AES256Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data());
42  }
43 }
44 
45 static void AES256_Decrypt(benchmark::State &state) {
46  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
47  const std::vector<uint8_t> cyphertext(16, 0);
48  std::vector<uint8_t> plaintext(16, 0);
49 
50  while (state.KeepRunning()) {
51  AES256Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data());
52  }
53 }
54 
56  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
57  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
58  const std::vector<uint8_t> plaintext(128, 0);
59  std::vector<uint8_t> cyphertext(128, 0);
60 
61  while (state.KeepRunning()) {
62  AES128CBCEncrypt(key.data(), iv.data(), false)
63  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
64  }
65 }
66 
68  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
69  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
70  const std::vector<uint8_t> cyphertext(128, 0);
71  std::vector<uint8_t> plaintext(128, 0);
72 
73  while (state.KeepRunning()) {
74  AES128CBCDecrypt(key.data(), iv.data(), false)
75  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
76  }
77 }
78 
80  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
81  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
82  const std::vector<uint8_t> plaintext(128, 0);
83  std::vector<uint8_t> cyphertext(128 + AES_BLOCKSIZE, 0);
84 
85  while (state.KeepRunning()) {
86  AES128CBCEncrypt(key.data(), iv.data(), true)
87  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
88  }
89 }
90 
92  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
93  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
94  const std::vector<uint8_t> cyphertext(128, 0);
95  std::vector<uint8_t> plaintext(128 + AES_BLOCKSIZE, 0);
96 
97  while (state.KeepRunning()) {
98  AES128CBCDecrypt(key.data(), iv.data(), true)
99  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
100  }
101 }
102 
104  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
105  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
106  const std::vector<uint8_t> plaintext(128, 0);
107  std::vector<uint8_t> cyphertext(128, 0);
108 
109  while (state.KeepRunning()) {
110  AES256CBCEncrypt(key.data(), iv.data(), false)
111  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
112  }
113 }
114 
116  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
117  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
118  const std::vector<uint8_t> cyphertext(128, 0);
119  std::vector<uint8_t> plaintext(128, 0);
120 
121  while (state.KeepRunning()) {
122  AES256CBCDecrypt(key.data(), iv.data(), false)
123  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
124  }
125 }
126 
128  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
129  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
130  const std::vector<uint8_t> plaintext(128, 0);
131  std::vector<uint8_t> cyphertext(128 + AES_BLOCKSIZE, 0);
132 
133  while (state.KeepRunning()) {
134  AES256CBCEncrypt(key.data(), iv.data(), true)
135  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
136  }
137 }
138 
140  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
141  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
142  const std::vector<uint8_t> cyphertext(128, 0);
143  std::vector<uint8_t> plaintext(128 + AES_BLOCKSIZE, 0);
144 
145  while (state.KeepRunning()) {
146  AES256CBCDecrypt(key.data(), iv.data(), true)
147  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
148  }
149 }
150 
static void AES128_Encrypt(benchmark::State &state)
Definition: crypto_aes.cpp:15
BENCHMARK(AES128_Encrypt, BENCH_AES128_ITERATION)
static void AES128CBC_DecryptNoPad(benchmark::State &state)
Definition: crypto_aes.cpp:67
static void AES256CBC_EncryptWithPad(benchmark::State &state)
Definition: crypto_aes.cpp:127
static const int AES256_KEYSIZE
Definition: aes.h:16
A decryption class for AES-256.
Definition: aes.h:52
static void AES256CBC_DecryptWithPad(benchmark::State &state)
Definition: crypto_aes.cpp:139
#define BENCH_AES128_ITERATION
Definition: crypto_aes.cpp:10
bool KeepRunning()
Definition: bench.h:71
static void AES256CBC_EncryptNoPad(benchmark::State &state)
Definition: crypto_aes.cpp:103
An encryption class for AES-256.
Definition: aes.h:41
static void AES128CBC_EncryptNoPad(benchmark::State &state)
Definition: crypto_aes.cpp:55
#define BENCH_AES256_ITERATION
Definition: crypto_aes.cpp:12
static void AES256CBC_DecryptNoPad(benchmark::State &state)
Definition: crypto_aes.cpp:115
A decryption class for AES-128.
Definition: aes.h:30
static void AES128CBC_DecryptWithPad(benchmark::State &state)
Definition: crypto_aes.cpp:91
#define BENCH_AES256CBC_ITERATION
Definition: crypto_aes.cpp:13
static void AES256_Encrypt(benchmark::State &state)
Definition: crypto_aes.cpp:35
static const int AES128_KEYSIZE
Definition: aes.h:15
static void AES128_Decrypt(benchmark::State &state)
Definition: crypto_aes.cpp:25
An encryption class for AES-128.
Definition: aes.h:19
static void AES256_Decrypt(benchmark::State &state)
Definition: crypto_aes.cpp:45
#define BENCH_AES128CBC_ITERATION
Definition: crypto_aes.cpp:11
static const int AES_BLOCKSIZE
Definition: aes.h:14
static void AES128CBC_EncryptWithPad(benchmark::State &state)
Definition: crypto_aes.cpp:79