blob: 665229890345d5f4664924ca2f666546af1192fe [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>
David Benjaminbcb65b92016-08-14 22:06:49 -040021#include <stdlib.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080022#include <string.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070023
Adam Langley92b6b022015-04-16 14:01:33 -070024#include <openssl/aead.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070025#include <openssl/bn.h>
Adam Langley4fb0dc42015-11-13 13:09:47 -080026#include <openssl/curve25519.h>
Adam Langley92b6b022015-04-16 14:01:33 -070027#include <openssl/digest.h>
28#include <openssl/err.h>
Matt Braithwaited17d74d2016-08-17 20:10:28 -070029#include <openssl/ec.h>
30#include <openssl/ecdsa.h>
31#include <openssl/ec_key.h>
David Benjaminb5292532017-06-09 19:27:37 -040032#include <openssl/evp.h>
David Benjamin98193672016-03-25 18:07:11 -040033#include <openssl/nid.h>
Adam Langley92b6b022015-04-16 14:01:33 -070034#include <openssl/rand.h>
35#include <openssl/rsa.h>
36
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070037#if defined(OPENSSL_WINDOWS)
David Benjamina353cdb2016-06-09 16:48:33 -040038OPENSSL_MSVC_PRAGMA(warning(push, 3))
Adam Langley3e719312015-03-20 16:32:23 -070039#include <windows.h>
David Benjamina353cdb2016-06-09 16:48:33 -040040OPENSSL_MSVC_PRAGMA(warning(pop))
Adam Langley30eda1d2014-06-24 11:15:12 -070041#elif defined(OPENSSL_APPLE)
42#include <sys/time.h>
David Benjamin2f401ec2016-09-09 19:49:35 -040043#else
44#include <time.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070045#endif
46
David Benjamin17cf2cb2016-12-13 01:07:13 -050047#include "../crypto/internal.h"
David Benjamin58084af2015-06-07 00:25:15 -040048#include "internal.h"
Adam Langleyad6b28e2015-04-14 12:07:44 -070049
Adam Langley2b2d66d2015-01-30 17:08:37 -080050
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070051// TimeResults represents the results of benchmarking a function.
52struct TimeResults {
53 // num_calls is the number of function calls done in the time period.
54 unsigned num_calls;
55 // us is the number of microseconds that elapsed in the time period.
56 unsigned us;
57
58 void Print(const std::string &description) {
59 printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls,
60 description.c_str(), us,
61 (static_cast<double>(num_calls) / us) * 1000000);
62 }
63
64 void PrintWithBytes(const std::string &description, size_t bytes_per_call) {
65 printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n",
66 num_calls, description.c_str(), us,
67 (static_cast<double>(num_calls) / us) * 1000000,
68 static_cast<double>(bytes_per_call * num_calls) / us);
69 }
70};
71
72#if defined(OPENSSL_WINDOWS)
73static uint64_t time_now() { return GetTickCount64() * 1000; }
Adam Langley30eda1d2014-06-24 11:15:12 -070074#elif defined(OPENSSL_APPLE)
75static uint64_t time_now() {
76 struct timeval tv;
77 uint64_t ret;
78
79 gettimeofday(&tv, NULL);
80 ret = tv.tv_sec;
81 ret *= 1000000;
82 ret += tv.tv_usec;
83 return ret;
84}
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070085#else
86static uint64_t time_now() {
87 struct timespec ts;
88 clock_gettime(CLOCK_MONOTONIC, &ts);
89
90 uint64_t ret = ts.tv_sec;
91 ret *= 1000000;
92 ret += ts.tv_nsec / 1000;
93 return ret;
94}
95#endif
96
David Benjaminbcb65b92016-08-14 22:06:49 -040097static uint64_t g_timeout_seconds = 1;
98
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070099static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
David Benjaminbcb65b92016-08-14 22:06:49 -0400100 // total_us is the total amount of time that we'll aim to measure a function
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700101 // for.
David Benjaminbcb65b92016-08-14 22:06:49 -0400102 const uint64_t total_us = g_timeout_seconds * 1000000;
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700103 uint64_t start = time_now(), now, delta;
104 unsigned done = 0, iterations_between_time_checks;
105
106 if (!func()) {
107 return false;
108 }
109 now = time_now();
110 delta = now - start;
111 if (delta == 0) {
112 iterations_between_time_checks = 250;
113 } else {
114 // Aim for about 100ms between time checks.
115 iterations_between_time_checks =
116 static_cast<double>(100000) / static_cast<double>(delta);
117 if (iterations_between_time_checks > 1000) {
118 iterations_between_time_checks = 1000;
119 } else if (iterations_between_time_checks < 1) {
120 iterations_between_time_checks = 1;
121 }
122 }
123
124 for (;;) {
125 for (unsigned i = 0; i < iterations_between_time_checks; i++) {
126 if (!func()) {
127 return false;
128 }
129 done++;
130 }
131
132 now = time_now();
David Benjaminbcb65b92016-08-14 22:06:49 -0400133 if (now - start > total_us) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700134 break;
135 }
136 }
137
138 results->us = now - start;
139 results->num_calls = done;
140 return true;
141}
142
Adam Langley90b58402015-04-13 11:04:18 -0700143static bool SpeedRSA(const std::string &key_name, RSA *key,
144 const std::string &selected) {
145 if (!selected.empty() && key_name.find(selected) == std::string::npos) {
146 return true;
147 }
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700148
149 std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]);
150 const uint8_t fake_sha256_hash[32] = {0};
151 unsigned sig_len;
152
Adam Langley90b58402015-04-13 11:04:18 -0700153 TimeResults results;
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700154 if (!TimeFunction(&results,
155 [key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
Brian Smith7bee8532016-08-19 15:11:20 -1000156 /* Usually during RSA signing we're using a long-lived |RSA| that has
157 * already had all of its |BN_MONT_CTX|s constructed, so it makes
158 * sense to use |key| directly here. */
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700159 return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
160 sig.get(), &sig_len, key);
161 })) {
162 fprintf(stderr, "RSA_sign failed.\n");
Brian Smith83a82982015-04-09 16:21:10 -1000163 ERR_print_errors_fp(stderr);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700164 return false;
165 }
166 results.Print(key_name + " signing");
167
168 if (!TimeFunction(&results,
169 [key, &fake_sha256_hash, &sig, sig_len]() -> bool {
Brian Smith7bee8532016-08-19 15:11:20 -1000170 /* Usually during RSA verification we have to parse an RSA key from a
171 * certificate or similar, in which case we'd need to construct a new
172 * RSA key, with a new |BN_MONT_CTX| for the public modulus. If we were
173 * to use |key| directly instead, then these costs wouldn't be
174 * accounted for. */
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700175 bssl::UniquePtr<RSA> verify_key(RSA_new());
Brian Smith7bee8532016-08-19 15:11:20 -1000176 if (!verify_key) {
177 return false;
178 }
179 verify_key->n = BN_dup(key->n);
180 verify_key->e = BN_dup(key->e);
181 if (!verify_key->n ||
182 !verify_key->e) {
183 return false;
184 }
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700185 return RSA_verify(NID_sha256, fake_sha256_hash,
186 sizeof(fake_sha256_hash), sig.get(), sig_len, key);
187 })) {
188 fprintf(stderr, "RSA_verify failed.\n");
Brian Smith83a82982015-04-09 16:21:10 -1000189 ERR_print_errors_fp(stderr);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700190 return false;
191 }
192 results.Print(key_name + " verify");
193
194 return true;
195}
196
Adam Langley26725342015-01-28 15:52:57 -0800197static uint8_t *align(uint8_t *in, unsigned alignment) {
198 return reinterpret_cast<uint8_t *>(
Brian Smithd53b2c32015-03-17 00:37:06 -1000199 (reinterpret_cast<uintptr_t>(in) + alignment) &
200 ~static_cast<size_t>(alignment - 1));
David Benjamin384673c2015-01-21 15:56:14 -0500201}
Adam Langley543d0062015-01-15 16:05:41 -0800202
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700203static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name,
Adam Langleyba9557d2017-02-27 13:53:23 -0800204 size_t chunk_len, size_t ad_len,
205 evp_aead_direction_t direction) {
Adam Langley26725342015-01-28 15:52:57 -0800206 static const unsigned kAlignment = 16;
207
David Benjamin0cce8632016-10-20 15:13:26 -0400208 bssl::ScopedEVP_AEAD_CTX ctx;
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700209 const size_t key_len = EVP_AEAD_key_length(aead);
210 const size_t nonce_len = EVP_AEAD_nonce_length(aead);
211 const size_t overhead_len = EVP_AEAD_max_overhead(aead);
212
213 std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500214 OPENSSL_memset(key.get(), 0, key_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700215 std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500216 OPENSSL_memset(nonce.get(), 0, nonce_len);
Brian Smith1d1562d2015-03-17 00:32:20 -1000217 std::unique_ptr<uint8_t[]> in_storage(new uint8_t[chunk_len + kAlignment]);
218 std::unique_ptr<uint8_t[]> out_storage(new uint8_t[chunk_len + overhead_len + kAlignment]);
Adam Langleyba9557d2017-02-27 13:53:23 -0800219 std::unique_ptr<uint8_t[]> in2_storage(new uint8_t[chunk_len + kAlignment]);
Adam Langleye7624342015-01-15 17:33:48 -0800220 std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500221 OPENSSL_memset(ad.get(), 0, ad_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700222
Adam Langley26725342015-01-28 15:52:57 -0800223 uint8_t *const in = align(in_storage.get(), kAlignment);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500224 OPENSSL_memset(in, 0, chunk_len);
Adam Langley26725342015-01-28 15:52:57 -0800225 uint8_t *const out = align(out_storage.get(), kAlignment);
David Benjamin17cf2cb2016-12-13 01:07:13 -0500226 OPENSSL_memset(out, 0, chunk_len + overhead_len);
Adam Langleyba9557d2017-02-27 13:53:23 -0800227 uint8_t *const in2 = align(in2_storage.get(), kAlignment);
Adam Langley26725342015-01-28 15:52:57 -0800228
David Benjamin0cce8632016-10-20 15:13:26 -0400229 if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len,
David Benjamind434f282015-03-17 18:28:37 -0400230 EVP_AEAD_DEFAULT_TAG_LENGTH,
231 evp_aead_seal)) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700232 fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
Brian Smith83a82982015-04-09 16:21:10 -1000233 ERR_print_errors_fp(stderr);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700234 return false;
235 }
236
237 TimeResults results;
Adam Langleyba9557d2017-02-27 13:53:23 -0800238 if (direction == evp_aead_seal) {
239 if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, in,
240 out, &ctx, &nonce, &ad]() -> bool {
241 size_t out_len;
242 return EVP_AEAD_CTX_seal(ctx.get(), out, &out_len,
243 chunk_len + overhead_len, nonce.get(),
244 nonce_len, in, chunk_len, ad.get(), ad_len);
245 })) {
246 fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
247 ERR_print_errors_fp(stderr);
248 return false;
249 }
250 } else {
251 size_t out_len;
252 EVP_AEAD_CTX_seal(ctx.get(), out, &out_len, chunk_len + overhead_len,
253 nonce.get(), nonce_len, in, chunk_len, ad.get(), ad_len);
254
Adam Langley84cd4932017-03-02 10:30:03 -0800255 if (!TimeFunction(&results, [chunk_len, nonce_len, ad_len, in2, out, &ctx,
256 &nonce, &ad, out_len]() -> bool {
Adam Langleyba9557d2017-02-27 13:53:23 -0800257 size_t in2_len;
258 return EVP_AEAD_CTX_open(ctx.get(), in2, &in2_len, chunk_len,
259 nonce.get(), nonce_len, out, out_len,
260 ad.get(), ad_len);
261 })) {
262 fprintf(stderr, "EVP_AEAD_CTX_open failed.\n");
263 ERR_print_errors_fp(stderr);
264 return false;
265 }
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700266 }
267
Adam Langleyba9557d2017-02-27 13:53:23 -0800268 results.PrintWithBytes(
269 name + (direction == evp_aead_seal ? " seal" : " open"), chunk_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700270 return true;
271}
272
Adam Langleye7624342015-01-15 17:33:48 -0800273static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name,
Adam Langley90b58402015-04-13 11:04:18 -0700274 size_t ad_len, const std::string &selected) {
275 if (!selected.empty() && name.find(selected) == std::string::npos) {
276 return true;
277 }
278
Adam Langleyba9557d2017-02-27 13:53:23 -0800279 return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len,
280 evp_aead_seal) &&
281 SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len,
282 evp_aead_seal) &&
283 SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len,
284 evp_aead_seal);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700285}
286
Adam Langleyba9557d2017-02-27 13:53:23 -0800287static bool SpeedAEADOpen(const EVP_AEAD *aead, const std::string &name,
288 size_t ad_len, const std::string &selected) {
289 if (!selected.empty() && name.find(selected) == std::string::npos) {
290 return true;
291 }
292
293 return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len,
294 evp_aead_open) &&
295 SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len,
296 evp_aead_open) &&
297 SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len,
298 evp_aead_open);
299}
Adam Langleyba9557d2017-02-27 13:53:23 -0800300
Adam Langley006779a2014-06-20 12:00:00 -0700301static bool SpeedHashChunk(const EVP_MD *md, const std::string &name,
302 size_t chunk_len) {
303 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
304 uint8_t scratch[8192];
305
306 if (chunk_len > sizeof(scratch)) {
307 return false;
308 }
309
310 TimeResults results;
311 if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool {
312 uint8_t digest[EVP_MAX_MD_SIZE];
313 unsigned int md_len;
314
315 return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) &&
316 EVP_DigestUpdate(ctx, scratch, chunk_len) &&
317 EVP_DigestFinal_ex(ctx, digest, &md_len);
318 })) {
319 fprintf(stderr, "EVP_DigestInit_ex failed.\n");
Brian Smith83a82982015-04-09 16:21:10 -1000320 ERR_print_errors_fp(stderr);
Adam Langley006779a2014-06-20 12:00:00 -0700321 return false;
322 }
323
324 results.PrintWithBytes(name, chunk_len);
325
326 EVP_MD_CTX_destroy(ctx);
327
328 return true;
329}
Adam Langley90b58402015-04-13 11:04:18 -0700330static bool SpeedHash(const EVP_MD *md, const std::string &name,
331 const std::string &selected) {
332 if (!selected.empty() && name.find(selected) == std::string::npos) {
333 return true;
334 }
335
Adam Langley006779a2014-06-20 12:00:00 -0700336 return SpeedHashChunk(md, name + " (16 bytes)", 16) &&
337 SpeedHashChunk(md, name + " (256 bytes)", 256) &&
338 SpeedHashChunk(md, name + " (8192 bytes)", 8192);
339}
340
David Benjamin1e5ac5d2016-11-01 16:37:09 -0400341static bool SpeedRandomChunk(const std::string &name, size_t chunk_len) {
Adam Langley90b58402015-04-13 11:04:18 -0700342 uint8_t scratch[8192];
343
344 if (chunk_len > sizeof(scratch)) {
345 return false;
346 }
347
348 TimeResults results;
349 if (!TimeFunction(&results, [chunk_len, &scratch]() -> bool {
350 RAND_bytes(scratch, chunk_len);
351 return true;
352 })) {
353 return false;
354 }
355
356 results.PrintWithBytes(name, chunk_len);
357 return true;
358}
359
360static bool SpeedRandom(const std::string &selected) {
361 if (!selected.empty() && selected != "RNG") {
362 return true;
363 }
364
365 return SpeedRandomChunk("RNG (16 bytes)", 16) &&
366 SpeedRandomChunk("RNG (256 bytes)", 256) &&
367 SpeedRandomChunk("RNG (8192 bytes)", 8192);
368}
369
Adam Langleyad6b28e2015-04-14 12:07:44 -0700370static bool SpeedECDHCurve(const std::string &name, int nid,
371 const std::string &selected) {
372 if (!selected.empty() && name.find(selected) == std::string::npos) {
373 return true;
374 }
375
376 TimeResults results;
377 if (!TimeFunction(&results, [nid]() -> bool {
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700378 bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
Adam Langleyad6b28e2015-04-14 12:07:44 -0700379 if (!key ||
380 !EC_KEY_generate_key(key.get())) {
381 return false;
382 }
383 const EC_GROUP *const group = EC_KEY_get0_group(key.get());
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700384 bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group));
385 bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
Adam Langleyad6b28e2015-04-14 12:07:44 -0700386
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700387 bssl::UniquePtr<BIGNUM> x(BN_new());
388 bssl::UniquePtr<BIGNUM> y(BN_new());
Adam Langleyad6b28e2015-04-14 12:07:44 -0700389
390 if (!point || !ctx || !x || !y ||
391 !EC_POINT_mul(group, point.get(), NULL,
392 EC_KEY_get0_public_key(key.get()),
393 EC_KEY_get0_private_key(key.get()), ctx.get()) ||
394 !EC_POINT_get_affine_coordinates_GFp(group, point.get(), x.get(),
395 y.get(), ctx.get())) {
396 return false;
397 }
398
399 return true;
400 })) {
401 return false;
402 }
403
404 results.Print(name);
405 return true;
406}
407
408static bool SpeedECDSACurve(const std::string &name, int nid,
409 const std::string &selected) {
410 if (!selected.empty() && name.find(selected) == std::string::npos) {
411 return true;
412 }
413
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700414 bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
Adam Langleyad6b28e2015-04-14 12:07:44 -0700415 if (!key ||
416 !EC_KEY_generate_key(key.get())) {
417 return false;
418 }
419
420 uint8_t signature[256];
421 if (ECDSA_size(key.get()) > sizeof(signature)) {
422 return false;
423 }
424 uint8_t digest[20];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500425 OPENSSL_memset(digest, 42, sizeof(digest));
Adam Langleyad6b28e2015-04-14 12:07:44 -0700426 unsigned sig_len;
427
428 TimeResults results;
429 if (!TimeFunction(&results, [&key, &signature, &digest, &sig_len]() -> bool {
430 return ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len,
431 key.get()) == 1;
432 })) {
433 return false;
434 }
435
436 results.Print(name + " signing");
437
438 if (!TimeFunction(&results, [&key, &signature, &digest, sig_len]() -> bool {
439 return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len,
440 key.get()) == 1;
441 })) {
442 return false;
443 }
444
445 results.Print(name + " verify");
446
447 return true;
448}
449
450static bool SpeedECDH(const std::string &selected) {
451 return SpeedECDHCurve("ECDH P-224", NID_secp224r1, selected) &&
452 SpeedECDHCurve("ECDH P-256", NID_X9_62_prime256v1, selected) &&
453 SpeedECDHCurve("ECDH P-384", NID_secp384r1, selected) &&
454 SpeedECDHCurve("ECDH P-521", NID_secp521r1, selected);
455}
456
457static bool SpeedECDSA(const std::string &selected) {
458 return SpeedECDSACurve("ECDSA P-224", NID_secp224r1, selected) &&
459 SpeedECDSACurve("ECDSA P-256", NID_X9_62_prime256v1, selected) &&
460 SpeedECDSACurve("ECDSA P-384", NID_secp384r1, selected) &&
461 SpeedECDSACurve("ECDSA P-521", NID_secp521r1, selected);
462}
463
Adam Langley4fb0dc42015-11-13 13:09:47 -0800464static bool Speed25519(const std::string &selected) {
465 if (!selected.empty() && selected.find("25519") == std::string::npos) {
466 return true;
467 }
468
469 TimeResults results;
470
Adam Langley4fb0dc42015-11-13 13:09:47 -0800471 uint8_t public_key[32], private_key[64];
472
473 if (!TimeFunction(&results, [&public_key, &private_key]() -> bool {
474 ED25519_keypair(public_key, private_key);
475 return true;
476 })) {
477 return false;
478 }
479
480 results.Print("Ed25519 key generation");
481
482 static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5};
483 uint8_t signature[64];
484
485 if (!TimeFunction(&results, [&private_key, &signature]() -> bool {
486 return ED25519_sign(signature, kMessage, sizeof(kMessage),
487 private_key) == 1;
488 })) {
489 return false;
490 }
491
492 results.Print("Ed25519 signing");
493
494 if (!TimeFunction(&results, [&public_key, &signature]() -> bool {
495 return ED25519_verify(kMessage, sizeof(kMessage), signature,
496 public_key) == 1;
497 })) {
498 fprintf(stderr, "Ed25519 verify failed.\n");
499 return false;
500 }
501
502 results.Print("Ed25519 verify");
Adam Langley4fb0dc42015-11-13 13:09:47 -0800503
504 if (!TimeFunction(&results, []() -> bool {
505 uint8_t out[32], in[32];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500506 OPENSSL_memset(in, 0, sizeof(in));
Adam Langley4fb0dc42015-11-13 13:09:47 -0800507 X25519_public_from_private(out, in);
508 return true;
509 })) {
510 fprintf(stderr, "Curve25519 base-point multiplication failed.\n");
511 return false;
512 }
513
514 results.Print("Curve25519 base-point multiplication");
515
516 if (!TimeFunction(&results, []() -> bool {
517 uint8_t out[32], in1[32], in2[32];
David Benjamin17cf2cb2016-12-13 01:07:13 -0500518 OPENSSL_memset(in1, 0, sizeof(in1));
519 OPENSSL_memset(in2, 0, sizeof(in2));
Adam Langley3ac32b12015-11-17 15:15:05 -0800520 in1[0] = 1;
521 in2[0] = 9;
Adam Langley4fb0dc42015-11-13 13:09:47 -0800522 return X25519(out, in1, in2) == 1;
523 })) {
524 fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n");
525 return false;
526 }
527
528 results.Print("Curve25519 arbitrary point multiplication");
529
530 return true;
531}
532
Arnar Birgissonf27459e2016-02-09 18:09:00 -0800533static bool SpeedSPAKE2(const std::string &selected) {
534 if (!selected.empty() && selected.find("SPAKE2") == std::string::npos) {
535 return true;
536 }
537
538 TimeResults results;
539
540 static const uint8_t kAliceName[] = {'A'};
541 static const uint8_t kBobName[] = {'B'};
542 static const uint8_t kPassword[] = "password";
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700543 bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(spake2_role_alice,
544 kAliceName, sizeof(kAliceName), kBobName,
545 sizeof(kBobName)));
Arnar Birgissonf27459e2016-02-09 18:09:00 -0800546 uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE];
547 size_t alice_msg_len;
548
549 if (!SPAKE2_generate_msg(alice.get(), alice_msg, &alice_msg_len,
550 sizeof(alice_msg),
551 kPassword, sizeof(kPassword))) {
552 fprintf(stderr, "SPAKE2_generate_msg failed.\n");
553 return false;
554 }
555
Adam Langley708db162016-03-01 11:48:00 -0800556 if (!TimeFunction(&results, [&alice_msg, alice_msg_len]() -> bool {
Matt Braithwaited17d74d2016-08-17 20:10:28 -0700557 bssl::UniquePtr<SPAKE2_CTX> bob(SPAKE2_CTX_new(spake2_role_bob,
558 kBobName, sizeof(kBobName), kAliceName,
559 sizeof(kAliceName)));
Arnar Birgissonf27459e2016-02-09 18:09:00 -0800560 uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE], bob_key[64];
561 size_t bob_msg_len, bob_key_len;
562 if (!SPAKE2_generate_msg(bob.get(), bob_msg, &bob_msg_len,
563 sizeof(bob_msg), kPassword,
564 sizeof(kPassword)) ||
565 !SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len,
566 sizeof(bob_key), alice_msg, alice_msg_len)) {
567 return false;
568 }
569
570 return true;
571 })) {
572 fprintf(stderr, "SPAKE2 failed.\n");
573 }
574
575 results.Print("SPAKE2 over Ed25519");
576
577 return true;
578}
579
David Benjaminb5292532017-06-09 19:27:37 -0400580static bool SpeedScrypt(const std::string &selected) {
581 if (!selected.empty() && selected.find("scrypt") == std::string::npos) {
582 return true;
583 }
584
585 TimeResults results;
586
587 static const char kPassword[] = "password";
588 static const uint8_t kSalt[] = "NaCl";
589
590 if (!TimeFunction(&results, [&]() -> bool {
591 uint8_t out[64];
592 return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt,
593 sizeof(kSalt) - 1, 1024, 8, 16, 0 /* max_mem */,
594 out, sizeof(out));
595 })) {
596 fprintf(stderr, "scrypt failed.\n");
597 return false;
598 }
599 results.Print("scrypt (N = 1024, r = 8, p = 16)");
600
601 if (!TimeFunction(&results, [&]() -> bool {
602 uint8_t out[64];
603 return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt,
604 sizeof(kSalt) - 1, 16384, 8, 1, 0 /* max_mem */,
605 out, sizeof(out));
606 })) {
607 fprintf(stderr, "scrypt failed.\n");
608 return false;
609 }
610 results.Print("scrypt (N = 16384, r = 8, p = 1)");
611
612 return true;
613}
614
David Benjaminbcb65b92016-08-14 22:06:49 -0400615static const struct argument kArguments[] = {
616 {
617 "-filter", kOptionalArgument,
618 "A filter on the speed tests to run",
619 },
620 {
621 "-timeout", kOptionalArgument,
622 "The number of seconds to run each test for (default is 1)",
623 },
624 {
625 "", kOptionalArgument, "",
626 },
627};
628
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700629bool Speed(const std::vector<std::string> &args) {
David Benjaminbcb65b92016-08-14 22:06:49 -0400630 std::map<std::string, std::string> args_map;
631 if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
632 PrintUsage(kArguments);
Adam Langley90b58402015-04-13 11:04:18 -0700633 return false;
634 }
David Benjaminbcb65b92016-08-14 22:06:49 -0400635
636 std::string selected;
637 if (args_map.count("-filter") != 0) {
638 selected = args_map["-filter"];
639 }
640
641 if (args_map.count("-timeout") != 0) {
642 g_timeout_seconds = atoi(args_map["-timeout"].c_str());
Adam Langley90b58402015-04-13 11:04:18 -0700643 }
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700644
David Benjamin0cce8632016-10-20 15:13:26 -0400645 bssl::UniquePtr<RSA> key(
646 RSA_private_key_from_bytes(kDERRSAPrivate2048, kDERRSAPrivate2048Len));
647 if (key == nullptr) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700648 fprintf(stderr, "Failed to parse RSA key.\n");
Brian Smith83a82982015-04-09 16:21:10 -1000649 ERR_print_errors_fp(stderr);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700650 return false;
651 }
652
David Benjamin0cce8632016-10-20 15:13:26 -0400653 if (!SpeedRSA("RSA 2048", key.get(), selected)) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700654 return false;
655 }
656
David Benjamin0cce8632016-10-20 15:13:26 -0400657 key.reset(
658 RSA_private_key_from_bytes(kDERRSAPrivate4096, kDERRSAPrivate4096Len));
659 if (key == nullptr) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700660 fprintf(stderr, "Failed to parse 4096-bit RSA key.\n");
Brian Smith83a82982015-04-09 16:21:10 -1000661 ERR_print_errors_fp(stderr);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700662 return 1;
663 }
664
David Benjamin0cce8632016-10-20 15:13:26 -0400665 if (!SpeedRSA("RSA 4096", key.get(), selected)) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700666 return false;
667 }
668
David Benjamin0cce8632016-10-20 15:13:26 -0400669 key.reset();
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700670
Adam Langleye7624342015-01-15 17:33:48 -0800671 // kTLSADLen is the number of bytes of additional data that TLS passes to
672 // AEADs.
673 static const size_t kTLSADLen = 13;
674 // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs.
675 // These are AEADs that weren't originally defined as AEADs, but which we use
676 // via the AEAD interface. In order for that to work, they have some TLS
677 // knowledge in them and construct a couple of the AD bytes internally.
678 static const size_t kLegacyADLen = kTLSADLen - 2;
679
Adam Langley90b58402015-04-13 11:04:18 -0700680 if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) ||
681 !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) ||
David Benjamin8ffab722015-11-30 18:48:18 -0500682 !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen,
683 selected) ||
David Benjamindf571632015-12-07 19:48:16 -0500684 !SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1",
685 kLegacyADLen, selected) ||
Adam Langley90b58402015-04-13 11:04:18 -0700686 !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1",
687 kLegacyADLen, selected) ||
688 !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1",
689 kLegacyADLen, selected) ||
Adam Langleydf447ba2016-12-01 08:24:24 -0800690 !SpeedAEAD(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
691 selected) ||
692 !SpeedAEAD(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
693 selected) ||
Adam Langleyba9557d2017-02-27 13:53:23 -0800694 !SpeedAEADOpen(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
695 selected) ||
696 !SpeedAEADOpen(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
697 selected) ||
Adam Langley90b58402015-04-13 11:04:18 -0700698 !SpeedHash(EVP_sha1(), "SHA-1", selected) ||
699 !SpeedHash(EVP_sha256(), "SHA-256", selected) ||
700 !SpeedHash(EVP_sha512(), "SHA-512", selected) ||
Adam Langleyad6b28e2015-04-14 12:07:44 -0700701 !SpeedRandom(selected) ||
702 !SpeedECDH(selected) ||
Adam Langley4fb0dc42015-11-13 13:09:47 -0800703 !SpeedECDSA(selected) ||
Arnar Birgissonf27459e2016-02-09 18:09:00 -0800704 !Speed25519(selected) ||
David Benjaminb5292532017-06-09 19:27:37 -0400705 !SpeedSPAKE2(selected) ||
706 !SpeedScrypt(selected)) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700707 return false;
708 }
709
Adam Langley90b58402015-04-13 11:04:18 -0700710 return true;
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700711}