blob: 41a61419b72f9fd38c3b742fe6c592d68405ac02 [file] [log] [blame]
Adam Langleyc5c0c7e2014-06-20 12:00:00 -07001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <string>
16#include <functional>
17#include <memory>
18#include <vector>
19
20#include <stdint.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080021#include <string.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070022#include <time.h>
23
24#include <openssl/aead.h>
25#include <openssl/bio.h>
Adam Langley006779a2014-06-20 12:00:00 -070026#include <openssl/digest.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070027#include <openssl/obj.h>
28#include <openssl/rsa.h>
29
30#if defined(OPENSSL_WINDOWS)
Brian Smithefed2212015-01-28 16:20:02 -080031#pragma warning(push, 3)
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070032#include <Windows.h>
Brian Smithefed2212015-01-28 16:20:02 -080033#pragma warning(pop)
Adam Langley30eda1d2014-06-24 11:15:12 -070034#elif defined(OPENSSL_APPLE)
35#include <sys/time.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070036#endif
37
Adam Langley2b2d66d2015-01-30 17:08:37 -080038
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070039extern "C" {
40// These values are DER encoded, RSA private keys.
41extern const uint8_t kDERRSAPrivate2048[];
42extern size_t kDERRSAPrivate2048Len;
43extern const uint8_t kDERRSAPrivate4096[];
44extern size_t kDERRSAPrivate4096Len;
45}
46
47// TimeResults represents the results of benchmarking a function.
48struct TimeResults {
49 // num_calls is the number of function calls done in the time period.
50 unsigned num_calls;
51 // us is the number of microseconds that elapsed in the time period.
52 unsigned us;
53
54 void Print(const std::string &description) {
55 printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls,
56 description.c_str(), us,
57 (static_cast<double>(num_calls) / us) * 1000000);
58 }
59
60 void PrintWithBytes(const std::string &description, size_t bytes_per_call) {
61 printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n",
62 num_calls, description.c_str(), us,
63 (static_cast<double>(num_calls) / us) * 1000000,
64 static_cast<double>(bytes_per_call * num_calls) / us);
65 }
66};
67
68#if defined(OPENSSL_WINDOWS)
69static uint64_t time_now() { return GetTickCount64() * 1000; }
Adam Langley30eda1d2014-06-24 11:15:12 -070070#elif defined(OPENSSL_APPLE)
71static uint64_t time_now() {
72 struct timeval tv;
73 uint64_t ret;
74
75 gettimeofday(&tv, NULL);
76 ret = tv.tv_sec;
77 ret *= 1000000;
78 ret += tv.tv_usec;
79 return ret;
80}
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070081#else
82static uint64_t time_now() {
83 struct timespec ts;
84 clock_gettime(CLOCK_MONOTONIC, &ts);
85
86 uint64_t ret = ts.tv_sec;
87 ret *= 1000000;
88 ret += ts.tv_nsec / 1000;
89 return ret;
90}
91#endif
92
93static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
94 // kTotalMS is the total amount of time that we'll aim to measure a function
95 // for.
96 static const uint64_t kTotalUS = 3000000;
97 uint64_t start = time_now(), now, delta;
98 unsigned done = 0, iterations_between_time_checks;
99
100 if (!func()) {
101 return false;
102 }
103 now = time_now();
104 delta = now - start;
105 if (delta == 0) {
106 iterations_between_time_checks = 250;
107 } else {
108 // Aim for about 100ms between time checks.
109 iterations_between_time_checks =
110 static_cast<double>(100000) / static_cast<double>(delta);
111 if (iterations_between_time_checks > 1000) {
112 iterations_between_time_checks = 1000;
113 } else if (iterations_between_time_checks < 1) {
114 iterations_between_time_checks = 1;
115 }
116 }
117
118 for (;;) {
119 for (unsigned i = 0; i < iterations_between_time_checks; i++) {
120 if (!func()) {
121 return false;
122 }
123 done++;
124 }
125
126 now = time_now();
127 if (now - start > kTotalUS) {
128 break;
129 }
130 }
131
132 results->us = now - start;
133 results->num_calls = done;
134 return true;
135}
136
137static bool SpeedRSA(const std::string& key_name, RSA *key) {
138 TimeResults results;
139
140 std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]);
141 const uint8_t fake_sha256_hash[32] = {0};
142 unsigned sig_len;
143
144 if (!TimeFunction(&results,
145 [key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
146 return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
147 sig.get(), &sig_len, key);
148 })) {
149 fprintf(stderr, "RSA_sign failed.\n");
150 BIO_print_errors_fp(stderr);
151 return false;
152 }
153 results.Print(key_name + " signing");
154
155 if (!TimeFunction(&results,
156 [key, &fake_sha256_hash, &sig, sig_len]() -> bool {
157 return RSA_verify(NID_sha256, fake_sha256_hash,
158 sizeof(fake_sha256_hash), sig.get(), sig_len, key);
159 })) {
160 fprintf(stderr, "RSA_verify failed.\n");
161 BIO_print_errors_fp(stderr);
162 return false;
163 }
164 results.Print(key_name + " verify");
165
166 return true;
167}
168
Adam Langley26725342015-01-28 15:52:57 -0800169static uint8_t *align(uint8_t *in, unsigned alignment) {
170 return reinterpret_cast<uint8_t *>(
Brian Smithd53b2c32015-03-17 00:37:06 -1000171 (reinterpret_cast<uintptr_t>(in) + alignment) &
172 ~static_cast<size_t>(alignment - 1));
David Benjamin384673c2015-01-21 15:56:14 -0500173}
Adam Langley543d0062015-01-15 16:05:41 -0800174
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700175static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name,
Adam Langleye7624342015-01-15 17:33:48 -0800176 size_t chunk_len, size_t ad_len) {
Adam Langley26725342015-01-28 15:52:57 -0800177 static const unsigned kAlignment = 16;
178
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700179 EVP_AEAD_CTX ctx;
180 const size_t key_len = EVP_AEAD_key_length(aead);
181 const size_t nonce_len = EVP_AEAD_nonce_length(aead);
182 const size_t overhead_len = EVP_AEAD_max_overhead(aead);
183
184 std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
185 memset(key.get(), 0, key_len);
186 std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]);
187 memset(nonce.get(), 0, nonce_len);
Brian Smith1d1562d2015-03-17 00:32:20 -1000188 std::unique_ptr<uint8_t[]> in_storage(new uint8_t[chunk_len + kAlignment]);
189 std::unique_ptr<uint8_t[]> out_storage(new uint8_t[chunk_len + overhead_len + kAlignment]);
Adam Langleye7624342015-01-15 17:33:48 -0800190 std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]);
191 memset(ad.get(), 0, ad_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700192
Adam Langley26725342015-01-28 15:52:57 -0800193 uint8_t *const in = align(in_storage.get(), kAlignment);
194 memset(in, 0, chunk_len);
195 uint8_t *const out = align(out_storage.get(), kAlignment);
196 memset(out, 0, chunk_len + overhead_len);
197
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700198 if (!EVP_AEAD_CTX_init(&ctx, aead, key.get(), key_len,
199 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) {
200 fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
201 BIO_print_errors_fp(stderr);
202 return false;
203 }
204
205 TimeResults results;
Adam Langley26725342015-01-28 15:52:57 -0800206 if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, in,
207 out, &ctx, &nonce, &ad]() -> bool {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700208 size_t out_len;
209
Adam Langleye7624342015-01-15 17:33:48 -0800210 return EVP_AEAD_CTX_seal(
Adam Langley26725342015-01-28 15:52:57 -0800211 &ctx, out, &out_len, chunk_len + overhead_len, nonce.get(),
212 nonce_len, in, chunk_len, ad.get(), ad_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700213 })) {
214 fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
215 BIO_print_errors_fp(stderr);
216 return false;
217 }
218
219 results.PrintWithBytes(name + " seal", chunk_len);
220
221 EVP_AEAD_CTX_cleanup(&ctx);
222
223 return true;
224}
225
Adam Langleye7624342015-01-15 17:33:48 -0800226static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name,
227 size_t ad_len) {
228 return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len) &&
229 SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len) &&
230 SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700231}
232
Adam Langley006779a2014-06-20 12:00:00 -0700233static bool SpeedHashChunk(const EVP_MD *md, const std::string &name,
234 size_t chunk_len) {
235 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
236 uint8_t scratch[8192];
237
238 if (chunk_len > sizeof(scratch)) {
239 return false;
240 }
241
242 TimeResults results;
243 if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool {
244 uint8_t digest[EVP_MAX_MD_SIZE];
245 unsigned int md_len;
246
247 return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) &&
248 EVP_DigestUpdate(ctx, scratch, chunk_len) &&
249 EVP_DigestFinal_ex(ctx, digest, &md_len);
250 })) {
251 fprintf(stderr, "EVP_DigestInit_ex failed.\n");
252 BIO_print_errors_fp(stderr);
253 return false;
254 }
255
256 results.PrintWithBytes(name, chunk_len);
257
258 EVP_MD_CTX_destroy(ctx);
259
260 return true;
261}
262static bool SpeedHash(const EVP_MD *md, const std::string &name) {
263 return SpeedHashChunk(md, name + " (16 bytes)", 16) &&
264 SpeedHashChunk(md, name + " (256 bytes)", 256) &&
265 SpeedHashChunk(md, name + " (8192 bytes)", 8192);
266}
267
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700268bool Speed(const std::vector<std::string> &args) {
269 const uint8_t *inp;
270
271 RSA *key = NULL;
272 inp = kDERRSAPrivate2048;
273 if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate2048Len)) {
274 fprintf(stderr, "Failed to parse RSA key.\n");
275 BIO_print_errors_fp(stderr);
276 return false;
277 }
278
279 if (!SpeedRSA("RSA 2048", key)) {
280 return false;
281 }
282
283 RSA_free(key);
284 key = NULL;
285
286 inp = kDERRSAPrivate4096;
287 if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate4096Len)) {
288 fprintf(stderr, "Failed to parse 4096-bit RSA key.\n");
289 BIO_print_errors_fp(stderr);
290 return 1;
291 }
292
293 if (!SpeedRSA("RSA 4096", key)) {
294 return false;
295 }
296
297 RSA_free(key);
298
Adam Langleye7624342015-01-15 17:33:48 -0800299 // kTLSADLen is the number of bytes of additional data that TLS passes to
300 // AEADs.
301 static const size_t kTLSADLen = 13;
302 // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs.
303 // These are AEADs that weren't originally defined as AEADs, but which we use
304 // via the AEAD interface. In order for that to work, they have some TLS
305 // knowledge in them and construct a couple of the AD bytes internally.
306 static const size_t kLegacyADLen = kTLSADLen - 2;
307
308 if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen) ||
309 !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen) ||
310 !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen) ||
311 !SpeedAEAD(EVP_aead_rc4_md5_tls(), "RC4-MD5", kLegacyADLen) ||
312 !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", kLegacyADLen) ||
313 !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", kLegacyADLen) ||
Adam Langley006779a2014-06-20 12:00:00 -0700314 !SpeedHash(EVP_sha1(), "SHA-1") ||
315 !SpeedHash(EVP_sha256(), "SHA-256") ||
316 !SpeedHash(EVP_sha512(), "SHA-512")) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700317 return false;
318 }
319
320 return 0;
321}