Bitcoin ABC  0.29.2
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 static void AES128_Encrypt(benchmark::Bench &bench) {
11  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
12  const std::vector<uint8_t> plaintext(16, 0);
13  std::vector<uint8_t> cyphertext(16, 0);
14 
15  bench.batch(plaintext.size()).unit("byte").run([&] {
16  AES128Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data());
17  });
18 }
19 
20 static void AES128_Decrypt(benchmark::Bench &bench) {
21  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
22  const std::vector<uint8_t> cyphertext(16, 0);
23  std::vector<uint8_t> plaintext(16, 0);
24 
25  bench.batch(cyphertext.size()).unit("byte").run([&] {
26  AES128Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data());
27  });
28 }
29 
30 static void AES256_Encrypt(benchmark::Bench &bench) {
31  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
32  const std::vector<uint8_t> plaintext(16, 0);
33  std::vector<uint8_t> cyphertext(16, 0);
34 
35  bench.batch(plaintext.size()).unit("byte").run([&] {
36  AES256Encrypt(key.data()).Encrypt(cyphertext.data(), plaintext.data());
37  });
38 }
39 
40 static void AES256_Decrypt(benchmark::Bench &bench) {
41  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
42  const std::vector<uint8_t> cyphertext(16, 0);
43  std::vector<uint8_t> plaintext(16, 0);
44 
45  bench.batch(cyphertext.size()).unit("byte").run([&] {
46  AES256Decrypt(key.data()).Decrypt(plaintext.data(), cyphertext.data());
47  });
48 }
49 
51  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
52  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
53  const std::vector<uint8_t> plaintext(128, 0);
54  std::vector<uint8_t> cyphertext(128, 0);
55 
56  bench.batch(plaintext.size()).unit("byte").run([&] {
57  AES128CBCEncrypt(key.data(), iv.data(), false)
58  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
59  });
60 }
61 
63  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
64  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
65  const std::vector<uint8_t> cyphertext(128, 0);
66  std::vector<uint8_t> plaintext(128, 0);
67 
68  bench.batch(cyphertext.size()).unit("byte").run([&] {
69  AES128CBCDecrypt(key.data(), iv.data(), false)
70  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
71  });
72 }
73 
75  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
76  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
77  const std::vector<uint8_t> plaintext(128, 0);
78  std::vector<uint8_t> cyphertext(128 + AES_BLOCKSIZE, 0);
79 
80  bench.batch(plaintext.size()).unit("byte").run([&] {
81  AES128CBCEncrypt(key.data(), iv.data(), true)
82  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
83  });
84 }
85 
87  const std::vector<uint8_t> key(AES128_KEYSIZE, 0);
88  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
89  const std::vector<uint8_t> cyphertext(128, 0);
90  std::vector<uint8_t> plaintext(128 + AES_BLOCKSIZE, 0);
91 
92  bench.batch(cyphertext.size()).unit("byte").run([&] {
93  AES128CBCDecrypt(key.data(), iv.data(), true)
94  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
95  });
96 }
97 
99  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
100  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
101  const std::vector<uint8_t> plaintext(128, 0);
102  std::vector<uint8_t> cyphertext(128, 0);
103 
104  bench.batch(plaintext.size()).unit("byte").run([&] {
105  AES256CBCEncrypt(key.data(), iv.data(), false)
106  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
107  });
108 }
109 
111  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
112  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
113  const std::vector<uint8_t> cyphertext(128, 0);
114  std::vector<uint8_t> plaintext(128, 0);
115 
116  bench.batch(cyphertext.size()).unit("byte").run([&] {
117  AES256CBCDecrypt(key.data(), iv.data(), false)
118  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
119  });
120 }
121 
123  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
124  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
125  const std::vector<uint8_t> plaintext(128, 0);
126  std::vector<uint8_t> cyphertext(128 + AES_BLOCKSIZE, 0);
127 
128  bench.batch(plaintext.size()).unit("byte").run([&] {
129  AES256CBCEncrypt(key.data(), iv.data(), true)
130  .Encrypt(plaintext.data(), plaintext.size(), cyphertext.data());
131  });
132 }
133 
135  const std::vector<uint8_t> key(AES256_KEYSIZE, 0);
136  const std::vector<uint8_t> iv(AES_BLOCKSIZE, 0);
137  const std::vector<uint8_t> cyphertext(128, 0);
138  std::vector<uint8_t> plaintext(128 + AES_BLOCKSIZE, 0);
139 
140  bench.batch(cyphertext.size()).unit("byte").run([&] {
141  AES256CBCDecrypt(key.data(), iv.data(), true)
142  .Decrypt(cyphertext.data(), cyphertext.size(), plaintext.data());
143  });
144 }
145 
static const int AES128_KEYSIZE
Definition: aes.h:15
static const int AES256_KEYSIZE
Definition: aes.h:16
static const int AES_BLOCKSIZE
Definition: aes.h:14
int Decrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:210
int Encrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:194
A decryption class for AES-128.
Definition: aes.h:30
void Decrypt(uint8_t plaintext[16], const uint8_t ciphertext[16]) const
Definition: aes.cpp:34
An encryption class for AES-128.
Definition: aes.h:19
void Encrypt(uint8_t ciphertext[16], const uint8_t plaintext[16]) const
Definition: aes.cpp:21
int Decrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:174
int Encrypt(const uint8_t *data, int size, uint8_t *out) const
Definition: aes.cpp:158
A decryption class for AES-256.
Definition: aes.h:52
void Decrypt(uint8_t plaintext[16], const uint8_t ciphertext[16]) const
Definition: aes.cpp:60
An encryption class for AES-256.
Definition: aes.h:41
void Encrypt(uint8_t ciphertext[16], const uint8_t plaintext[16]) const
Definition: aes.cpp:47
Main entry point to nanobench's benchmarking facility.
Definition: nanobench.h:616
Bench & run(char const *benchmarkName, Op &&op)
Repeatedly calls op() based on the configuration, and performs measurements.
Definition: nanobench.h:1183
ANKERL_NANOBENCH(NODISCARD) std Bench & batch(T b) noexcept
Sets the batch size.
Bench & unit(char const *unit)
Sets the operation unit.
static void AES128CBC_EncryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:74
static void AES256_Encrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:30
static void AES128CBC_DecryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:86
static void AES128CBC_DecryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:62
static void AES256CBC_DecryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:110
static void AES256CBC_EncryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:98
static void AES128_Decrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:20
static void AES256CBC_EncryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:122
static void AES256_Decrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:40
static void AES128_Encrypt(benchmark::Bench &bench)
Definition: crypto_aes.cpp:10
static void AES256CBC_DecryptWithPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:134
BENCHMARK(AES128_Encrypt)
static void AES128CBC_EncryptNoPad(benchmark::Bench &bench)
Definition: crypto_aes.cpp:50