1 /* 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 <algorithm>
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <inttypes.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <openssl/aead.h>
29 #include <openssl/aes.h>
30 #include <openssl/base64.h>
31 #include <openssl/bn.h>
32 #include <openssl/bytestring.h>
33 #include <openssl/crypto.h>
34 #include <openssl/curve25519.h>
35 #include <openssl/digest.h>
36 #include <openssl/ec.h>
37 #include <openssl/ec_key.h>
38 #include <openssl/ecdsa.h>
39 #include <openssl/err.h>
40 #include <openssl/evp.h>
41 #include <openssl/hrss.h>
42 #include <openssl/kyber.h>
43 #include <openssl/mem.h>
44 #include <openssl/nid.h>
45 #include <openssl/rand.h>
46 #include <openssl/rsa.h>
47 #include <openssl/siphash.h>
48 #include <openssl/trust_token.h>
49
50 #if defined(OPENSSL_WINDOWS)
51 OPENSSL_MSVC_PRAGMA(warning(push, 3))
52 #include <windows.h>
53 OPENSSL_MSVC_PRAGMA(warning(pop))
54 #elif defined(OPENSSL_APPLE)
55 #include <sys/time.h>
56 #else
57 #include <time.h>
58 #endif
59
60 #if defined(OPENSSL_THREADS)
61 #include <condition_variable>
62 #include <mutex>
63 #include <thread>
64 #endif
65
66 #include "../crypto/ec_extra/internal.h"
67 #include "../crypto/fipsmodule/ec/internal.h"
68 #include "../crypto/internal.h"
69 #include "../crypto/trust_token/internal.h"
70 #include "../crypto/spx/internal.h"
71 #include "internal.h"
72
73 // g_print_json is true if printed output is JSON formatted.
74 static bool g_print_json = false;
75
76 // TimeResults represents the results of benchmarking a function.
77 struct TimeResults {
78 // num_calls is the number of function calls done in the time period.
79 uint64_t num_calls;
80 // us is the number of microseconds that elapsed in the time period.
81 uint64_t us;
82
PrintTimeResults83 void Print(const std::string &description) const {
84 if (g_print_json) {
85 PrintJSON(description);
86 } else {
87 printf(
88 "Did %" PRIu64 " %s operations in %" PRIu64 "us (%.1f ops/sec)\n",
89 num_calls, description.c_str(), us,
90 (static_cast<double>(num_calls) / static_cast<double>(us)) * 1000000);
91 }
92 }
93
PrintWithBytesTimeResults94 void PrintWithBytes(const std::string &description,
95 size_t bytes_per_call) const {
96 if (g_print_json) {
97 PrintJSON(description, bytes_per_call);
98 } else {
99 printf(
100 "Did %" PRIu64 " %s operations in %" PRIu64
101 "us (%.1f ops/sec): %.1f MB/s\n",
102 num_calls, description.c_str(), us,
103 (static_cast<double>(num_calls) / static_cast<double>(us)) * 1000000,
104 static_cast<double>(bytes_per_call * num_calls) /
105 static_cast<double>(us));
106 }
107 }
108
109 private:
PrintJSONTimeResults110 void PrintJSON(const std::string &description,
111 size_t bytes_per_call = 0) const {
112 if (first_json_printed) {
113 puts(",");
114 }
115
116 printf("{\"description\": \"%s\", \"numCalls\": %" PRIu64
117 ", \"microseconds\": %" PRIu64,
118 description.c_str(), num_calls, us);
119
120 if (bytes_per_call > 0) {
121 printf(", \"bytesPerCall\": %zu", bytes_per_call);
122 }
123
124 printf("}");
125 first_json_printed = true;
126 }
127
128 // first_json_printed is true if |g_print_json| is true and the first item in
129 // the JSON results has been printed already. This is used to handle the
130 // commas between each item in the result list.
131 static bool first_json_printed;
132 };
133
134 bool TimeResults::first_json_printed = false;
135
136 #if defined(OPENSSL_WINDOWS)
time_now()137 static uint64_t time_now() { return GetTickCount64() * 1000; }
138 #elif defined(OPENSSL_APPLE)
time_now()139 static uint64_t time_now() {
140 struct timeval tv;
141 uint64_t ret;
142
143 gettimeofday(&tv, NULL);
144 ret = tv.tv_sec;
145 ret *= 1000000;
146 ret += tv.tv_usec;
147 return ret;
148 }
149 #else
time_now()150 static uint64_t time_now() {
151 struct timespec ts;
152 clock_gettime(CLOCK_MONOTONIC, &ts);
153
154 uint64_t ret = ts.tv_sec;
155 ret *= 1000000;
156 ret += ts.tv_nsec / 1000;
157 return ret;
158 }
159 #endif
160
161 static uint64_t g_timeout_seconds = 1;
162 static std::vector<size_t> g_chunk_lengths = {16, 256, 1350, 8192, 16384};
163
164 // IterationsBetweenTimeChecks returns the number of iterations of |func| to run
165 // in between checking the time, or zero on error.
IterationsBetweenTimeChecks(std::function<bool ()> func)166 static uint32_t IterationsBetweenTimeChecks(std::function<bool()> func) {
167 uint64_t start = time_now();
168 if (!func()) {
169 return 0;
170 }
171 uint64_t delta = time_now() - start;
172 if (delta == 0) {
173 return 250;
174 }
175
176 // Aim for about 100ms between time checks.
177 uint32_t ret = static_cast<double>(100000) / static_cast<double>(delta);
178 if (ret > 1000) {
179 ret = 1000;
180 } else if (ret < 1) {
181 ret = 1;
182 }
183 return ret;
184 }
185
TimeFunctionImpl(TimeResults * results,std::function<bool ()> func,uint32_t iterations_between_time_checks)186 static bool TimeFunctionImpl(TimeResults *results, std::function<bool()> func,
187 uint32_t iterations_between_time_checks) {
188 // total_us is the total amount of time that we'll aim to measure a function
189 // for.
190 const uint64_t total_us = g_timeout_seconds * 1000000;
191 uint64_t start = time_now(), now;
192 uint64_t done = 0;
193 for (;;) {
194 for (uint32_t i = 0; i < iterations_between_time_checks; i++) {
195 if (!func()) {
196 return false;
197 }
198 done++;
199 }
200
201 now = time_now();
202 if (now - start > total_us) {
203 break;
204 }
205 }
206
207 results->us = now - start;
208 results->num_calls = done;
209 return true;
210 }
211
TimeFunction(TimeResults * results,std::function<bool ()> func)212 static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
213 uint32_t iterations_between_time_checks = IterationsBetweenTimeChecks(func);
214 if (iterations_between_time_checks == 0) {
215 return false;
216 }
217
218 return TimeFunctionImpl(results, std::move(func),
219 iterations_between_time_checks);
220 }
221
222 #if defined(OPENSSL_THREADS)
223 // g_threads is the number of threads to run in parallel benchmarks.
224 static int g_threads = 1;
225
226 // Latch behaves like C++20 std::latch.
227 class Latch {
228 public:
Latch(int expected)229 explicit Latch(int expected) : expected_(expected) {}
230 Latch(const Latch &) = delete;
231 Latch &operator=(const Latch &) = delete;
232
ArriveAndWait()233 void ArriveAndWait() {
234 std::unique_lock<std::mutex> lock(lock_);
235 expected_--;
236 if (expected_ > 0) {
237 cond_.wait(lock, [&] { return expected_ == 0; });
238 } else {
239 cond_.notify_all();
240 }
241 }
242
243 private:
244 int expected_;
245 std::mutex lock_;
246 std::condition_variable cond_;
247 };
248
TimeFunctionParallel(TimeResults * results,std::function<bool ()> func)249 static bool TimeFunctionParallel(TimeResults *results,
250 std::function<bool()> func) {
251 if (g_threads <= 1) {
252 return TimeFunction(results, std::move(func));
253 }
254
255 uint32_t iterations_between_time_checks = IterationsBetweenTimeChecks(func);
256 if (iterations_between_time_checks == 0) {
257 return false;
258 }
259
260 struct ThreadResult {
261 TimeResults time_result;
262 bool ok = false;
263 };
264 std::vector<ThreadResult> thread_results(g_threads);
265 Latch latch(g_threads);
266 std::vector<std::thread> threads;
267 for (int i = 0; i < g_threads; i++) {
268 threads.emplace_back([&, i] {
269 // Wait for all the threads to be ready before running the benchmark.
270 latch.ArriveAndWait();
271 thread_results[i].ok = TimeFunctionImpl(
272 &thread_results[i].time_result, func, iterations_between_time_checks);
273 });
274 }
275
276 for (auto &thread : threads) {
277 thread.join();
278 }
279
280 results->num_calls = 0;
281 results->us = 0;
282 for (const auto &pair : thread_results) {
283 if (!pair.ok) {
284 return false;
285 }
286 results->num_calls += pair.time_result.num_calls;
287 results->us += pair.time_result.us;
288 }
289 return true;
290 }
291
292 #else
TimeFunctionParallel(TimeResults * results,std::function<bool ()> func)293 static bool TimeFunctionParallel(TimeResults *results,
294 std::function<bool()> func) {
295 return TimeFunction(results, std::move(func));
296 }
297 #endif
298
SpeedRSA(const std::string & selected)299 static bool SpeedRSA(const std::string &selected) {
300 if (!selected.empty() && selected.find("RSA") == std::string::npos) {
301 return true;
302 }
303
304 static const struct {
305 const char *name;
306 const uint8_t *key;
307 const size_t key_len;
308 } kRSAKeys[] = {
309 {"RSA 2048", kDERRSAPrivate2048, kDERRSAPrivate2048Len},
310 {"RSA 4096", kDERRSAPrivate4096, kDERRSAPrivate4096Len},
311 };
312
313 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRSAKeys); i++) {
314 const std::string name = kRSAKeys[i].name;
315
316 bssl::UniquePtr<RSA> key(
317 RSA_private_key_from_bytes(kRSAKeys[i].key, kRSAKeys[i].key_len));
318 if (key == nullptr) {
319 fprintf(stderr, "Failed to parse %s key.\n", name.c_str());
320 ERR_print_errors_fp(stderr);
321 return false;
322 }
323
324 static constexpr size_t kMaxSignature = 512;
325 if (RSA_size(key.get()) > kMaxSignature) {
326 abort();
327 }
328 const uint8_t fake_sha256_hash[32] = {0};
329
330 TimeResults results;
331 if (!TimeFunctionParallel(&results, [&key, &fake_sha256_hash]() -> bool {
332 // Usually during RSA signing we're using a long-lived |RSA| that
333 // has already had all of its |BN_MONT_CTX|s constructed, so it
334 // makes sense to use |key| directly here.
335 uint8_t out[kMaxSignature];
336 unsigned out_len;
337 return RSA_sign(NID_sha256, fake_sha256_hash,
338 sizeof(fake_sha256_hash), out, &out_len, key.get());
339 })) {
340 fprintf(stderr, "RSA_sign failed.\n");
341 ERR_print_errors_fp(stderr);
342 return false;
343 }
344 results.Print(name + " signing");
345
346 uint8_t sig[kMaxSignature];
347 unsigned sig_len;
348 if (!RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash), sig,
349 &sig_len, key.get())) {
350 return false;
351 }
352 if (!TimeFunctionParallel(
353 &results, [&key, &fake_sha256_hash, &sig, sig_len]() -> bool {
354 return RSA_verify(NID_sha256, fake_sha256_hash,
355 sizeof(fake_sha256_hash), sig, sig_len,
356 key.get());
357 })) {
358 fprintf(stderr, "RSA_verify failed.\n");
359 ERR_print_errors_fp(stderr);
360 return false;
361 }
362 results.Print(name + " verify (same key)");
363
364 if (!TimeFunctionParallel(
365 &results, [&key, &fake_sha256_hash, &sig, sig_len]() -> bool {
366 // Usually during RSA verification we have to parse an RSA key
367 // from a certificate or similar, in which case we'd need to
368 // construct a new RSA key, with a new |BN_MONT_CTX| for the
369 // public modulus. If we were to use |key| directly instead, then
370 // these costs wouldn't be accounted for.
371 bssl::UniquePtr<RSA> verify_key(RSA_new_public_key(
372 RSA_get0_n(key.get()), RSA_get0_e(key.get())));
373 if (!verify_key) {
374 return false;
375 }
376 return RSA_verify(NID_sha256, fake_sha256_hash,
377 sizeof(fake_sha256_hash), sig, sig_len,
378 verify_key.get());
379 })) {
380 fprintf(stderr, "RSA_verify failed.\n");
381 ERR_print_errors_fp(stderr);
382 return false;
383 }
384 results.Print(name + " verify (fresh key)");
385
386 if (!TimeFunctionParallel(&results, [&]() -> bool {
387 return bssl::UniquePtr<RSA>(RSA_private_key_from_bytes(
388 kRSAKeys[i].key, kRSAKeys[i].key_len)) != nullptr;
389 })) {
390 fprintf(stderr, "Failed to parse %s key.\n", name.c_str());
391 ERR_print_errors_fp(stderr);
392 return false;
393 }
394 results.Print(name + " private key parse");
395 }
396
397 return true;
398 }
399
SpeedRSAKeyGen(const std::string & selected)400 static bool SpeedRSAKeyGen(const std::string &selected) {
401 // Don't run this by default because it's so slow.
402 if (selected != "RSAKeyGen") {
403 return true;
404 }
405
406 bssl::UniquePtr<BIGNUM> e(BN_new());
407 if (!BN_set_word(e.get(), 65537)) {
408 return false;
409 }
410
411 const std::vector<int> kSizes = {2048, 3072, 4096};
412 for (int size : kSizes) {
413 const uint64_t start = time_now();
414 uint64_t num_calls = 0;
415 uint64_t us;
416 std::vector<uint64_t> durations;
417
418 for (;;) {
419 bssl::UniquePtr<RSA> rsa(RSA_new());
420
421 const uint64_t iteration_start = time_now();
422 if (!RSA_generate_key_ex(rsa.get(), size, e.get(), nullptr)) {
423 fprintf(stderr, "RSA_generate_key_ex failed.\n");
424 ERR_print_errors_fp(stderr);
425 return false;
426 }
427 const uint64_t iteration_end = time_now();
428
429 num_calls++;
430 durations.push_back(iteration_end - iteration_start);
431
432 us = iteration_end - start;
433 if (us > 30 * 1000000 /* 30 secs */) {
434 break;
435 }
436 }
437
438 std::sort(durations.begin(), durations.end());
439 const std::string description =
440 std::string("RSA ") + std::to_string(size) + std::string(" key-gen");
441 const TimeResults results = {num_calls, us};
442 results.Print(description);
443 const size_t n = durations.size();
444 assert(n > 0);
445
446 // Distribution information is useful, but doesn't fit into the standard
447 // format used by |g_print_json|.
448 if (!g_print_json) {
449 uint64_t min = durations[0];
450 uint64_t median = n & 1 ? durations[n / 2]
451 : (durations[n / 2 - 1] + durations[n / 2]) / 2;
452 uint64_t max = durations[n - 1];
453 printf(" min: %" PRIu64 "us, median: %" PRIu64 "us, max: %" PRIu64
454 "us\n",
455 min, median, max);
456 }
457 }
458
459 return true;
460 }
461
ChunkLenSuffix(size_t chunk_len)462 static std::string ChunkLenSuffix(size_t chunk_len) {
463 char buf[32];
464 snprintf(buf, sizeof(buf), " (%zu byte%s)", chunk_len,
465 chunk_len != 1 ? "s" : "");
466 return buf;
467 }
468
SpeedAEADChunk(const EVP_AEAD * aead,std::string name,size_t chunk_len,size_t ad_len,evp_aead_direction_t direction)469 static bool SpeedAEADChunk(const EVP_AEAD *aead, std::string name,
470 size_t chunk_len, size_t ad_len,
471 evp_aead_direction_t direction) {
472 static const unsigned kAlignment = 16;
473
474 name += ChunkLenSuffix(chunk_len);
475 bssl::ScopedEVP_AEAD_CTX ctx;
476 const size_t key_len = EVP_AEAD_key_length(aead);
477 const size_t nonce_len = EVP_AEAD_nonce_length(aead);
478 const size_t overhead_len = EVP_AEAD_max_overhead(aead);
479
480 auto key = std::make_unique<uint8_t[]>(key_len);
481 OPENSSL_memset(key.get(), 0, key_len);
482 auto nonce = std::make_unique<uint8_t[]>(nonce_len);
483 OPENSSL_memset(nonce.get(), 0, nonce_len);
484 auto in_storage = std::make_unique<uint8_t[]>(chunk_len + kAlignment);
485 // N.B. for EVP_AEAD_CTX_seal_scatter the input and output buffers may be the
486 // same size. However, in the direction == evp_aead_open case we still use
487 // non-scattering seal, hence we add overhead_len to the size of this buffer.
488 auto out_storage =
489 std::make_unique<uint8_t[]>(chunk_len + overhead_len + kAlignment);
490 auto in2_storage =
491 std::make_unique<uint8_t[]>(chunk_len + overhead_len + kAlignment);
492 auto ad = std::make_unique<uint8_t[]>(ad_len);
493 OPENSSL_memset(ad.get(), 0, ad_len);
494 auto tag_storage = std::make_unique<uint8_t[]>(overhead_len + kAlignment);
495
496 uint8_t *const in =
497 static_cast<uint8_t *>(align_pointer(in_storage.get(), kAlignment));
498 OPENSSL_memset(in, 0, chunk_len);
499 uint8_t *const out =
500 static_cast<uint8_t *>(align_pointer(out_storage.get(), kAlignment));
501 OPENSSL_memset(out, 0, chunk_len + overhead_len);
502 uint8_t *const tag =
503 static_cast<uint8_t *>(align_pointer(tag_storage.get(), kAlignment));
504 OPENSSL_memset(tag, 0, overhead_len);
505 uint8_t *const in2 =
506 static_cast<uint8_t *>(align_pointer(in2_storage.get(), kAlignment));
507
508 if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len,
509 EVP_AEAD_DEFAULT_TAG_LENGTH,
510 evp_aead_seal)) {
511 fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
512 ERR_print_errors_fp(stderr);
513 return false;
514 }
515
516 // TODO(davidben): In most cases, this can be |TimeFunctionParallel|, but a
517 // few stateful AEADs must be run serially.
518 TimeResults results;
519 if (direction == evp_aead_seal) {
520 if (!TimeFunction(&results,
521 [chunk_len, nonce_len, ad_len, overhead_len, in, out, tag,
522 &ctx, &nonce, &ad]() -> bool {
523 size_t tag_len;
524 return EVP_AEAD_CTX_seal_scatter(
525 ctx.get(), out, tag, &tag_len, overhead_len,
526 nonce.get(), nonce_len, in, chunk_len, nullptr, 0,
527 ad.get(), ad_len);
528 })) {
529 fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
530 ERR_print_errors_fp(stderr);
531 return false;
532 }
533 } else {
534 size_t out_len;
535 EVP_AEAD_CTX_seal(ctx.get(), out, &out_len, chunk_len + overhead_len,
536 nonce.get(), nonce_len, in, chunk_len, ad.get(), ad_len);
537
538 ctx.Reset();
539 if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len,
540 EVP_AEAD_DEFAULT_TAG_LENGTH,
541 evp_aead_open)) {
542 fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
543 ERR_print_errors_fp(stderr);
544 return false;
545 }
546
547 if (!TimeFunction(&results,
548 [chunk_len, overhead_len, nonce_len, ad_len, in2, out,
549 out_len, &ctx, &nonce, &ad]() -> bool {
550 size_t in2_len;
551 // N.B. EVP_AEAD_CTX_open_gather is not implemented for
552 // all AEADs.
553 return EVP_AEAD_CTX_open(ctx.get(), in2, &in2_len,
554 chunk_len + overhead_len,
555 nonce.get(), nonce_len, out,
556 out_len, ad.get(), ad_len);
557 })) {
558 fprintf(stderr, "EVP_AEAD_CTX_open failed.\n");
559 ERR_print_errors_fp(stderr);
560 return false;
561 }
562 }
563
564 results.PrintWithBytes(
565 name + (direction == evp_aead_seal ? " seal" : " open"), chunk_len);
566 return true;
567 }
568
SpeedAEAD(const EVP_AEAD * aead,const std::string & name,size_t ad_len,const std::string & selected)569 static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name,
570 size_t ad_len, const std::string &selected) {
571 if (!selected.empty() && name.find(selected) == std::string::npos) {
572 return true;
573 }
574
575 for (size_t chunk_len : g_chunk_lengths) {
576 if (!SpeedAEADChunk(aead, name, chunk_len, ad_len, evp_aead_seal)) {
577 return false;
578 }
579 }
580 return true;
581 }
582
SpeedAEADOpen(const EVP_AEAD * aead,const std::string & name,size_t ad_len,const std::string & selected)583 static bool SpeedAEADOpen(const EVP_AEAD *aead, const std::string &name,
584 size_t ad_len, const std::string &selected) {
585 if (!selected.empty() && name.find(selected) == std::string::npos) {
586 return true;
587 }
588
589 for (size_t chunk_len : g_chunk_lengths) {
590 if (!SpeedAEADChunk(aead, name, chunk_len, ad_len, evp_aead_open)) {
591 return false;
592 }
593 }
594
595 return true;
596 }
597
SpeedAESBlock(const std::string & name,unsigned bits,const std::string & selected)598 static bool SpeedAESBlock(const std::string &name, unsigned bits,
599 const std::string &selected) {
600 if (!selected.empty() && name.find(selected) == std::string::npos) {
601 return true;
602 }
603
604 static const uint8_t kZero[32] = {0};
605
606 {
607 TimeResults results;
608 if (!TimeFunctionParallel(&results, [&]() -> bool {
609 AES_KEY key;
610 return AES_set_encrypt_key(kZero, bits, &key) == 0;
611 })) {
612 fprintf(stderr, "AES_set_encrypt_key failed.\n");
613 return false;
614 }
615 results.Print(name + " encrypt setup");
616 }
617
618 {
619 AES_KEY key;
620 if (AES_set_encrypt_key(kZero, bits, &key) != 0) {
621 return false;
622 }
623 uint8_t block[16] = {0};
624 TimeResults results;
625 if (!TimeFunctionParallel(&results, [&]() -> bool {
626 AES_encrypt(block, block, &key);
627 return true;
628 })) {
629 fprintf(stderr, "AES_encrypt failed.\n");
630 return false;
631 }
632 results.Print(name + " encrypt");
633 }
634
635 {
636 TimeResults results;
637 if (!TimeFunctionParallel(&results, [&]() -> bool {
638 AES_KEY key;
639 return AES_set_decrypt_key(kZero, bits, &key) == 0;
640 })) {
641 fprintf(stderr, "AES_set_decrypt_key failed.\n");
642 return false;
643 }
644 results.Print(name + " decrypt setup");
645 }
646
647 {
648 AES_KEY key;
649 if (AES_set_decrypt_key(kZero, bits, &key) != 0) {
650 return false;
651 }
652 uint8_t block[16] = {0};
653 TimeResults results;
654 if (!TimeFunctionParallel(&results, [&]() -> bool {
655 AES_decrypt(block, block, &key);
656 return true;
657 })) {
658 fprintf(stderr, "AES_decrypt failed.\n");
659 return false;
660 }
661 results.Print(name + " decrypt");
662 }
663
664 return true;
665 }
666
SpeedHashChunk(const EVP_MD * md,std::string name,size_t chunk_len)667 static bool SpeedHashChunk(const EVP_MD *md, std::string name,
668 size_t chunk_len) {
669 uint8_t input[16384] = {0};
670
671 if (chunk_len > sizeof(input)) {
672 return false;
673 }
674
675 name += ChunkLenSuffix(chunk_len);
676 TimeResults results;
677 if (!TimeFunctionParallel(&results, [md, chunk_len, &input]() -> bool {
678 uint8_t digest[EVP_MAX_MD_SIZE];
679 unsigned int md_len;
680
681 bssl::ScopedEVP_MD_CTX ctx;
682 return EVP_DigestInit_ex(ctx.get(), md, NULL /* ENGINE */) &&
683 EVP_DigestUpdate(ctx.get(), input, chunk_len) &&
684 EVP_DigestFinal_ex(ctx.get(), digest, &md_len);
685 })) {
686 fprintf(stderr, "EVP_DigestInit_ex failed.\n");
687 ERR_print_errors_fp(stderr);
688 return false;
689 }
690
691 results.PrintWithBytes(name, chunk_len);
692 return true;
693 }
694
SpeedHash(const EVP_MD * md,const std::string & name,const std::string & selected)695 static bool SpeedHash(const EVP_MD *md, const std::string &name,
696 const std::string &selected) {
697 if (!selected.empty() && name.find(selected) == std::string::npos) {
698 return true;
699 }
700
701 for (size_t chunk_len : g_chunk_lengths) {
702 if (!SpeedHashChunk(md, name, chunk_len)) {
703 return false;
704 }
705 }
706
707 return true;
708 }
709
SpeedRandomChunk(std::string name,size_t chunk_len)710 static bool SpeedRandomChunk(std::string name, size_t chunk_len) {
711 static constexpr size_t kMaxChunk = 16384;
712 if (chunk_len > kMaxChunk) {
713 return false;
714 }
715
716 name += ChunkLenSuffix(chunk_len);
717 TimeResults results;
718 if (!TimeFunctionParallel(&results, [chunk_len]() -> bool {
719 uint8_t scratch[kMaxChunk];
720 RAND_bytes(scratch, chunk_len);
721 return true;
722 })) {
723 return false;
724 }
725
726 results.PrintWithBytes(name, chunk_len);
727 return true;
728 }
729
SpeedRandom(const std::string & selected)730 static bool SpeedRandom(const std::string &selected) {
731 if (!selected.empty() && selected != "RNG") {
732 return true;
733 }
734
735 for (size_t chunk_len : g_chunk_lengths) {
736 if (!SpeedRandomChunk("RNG", chunk_len)) {
737 return false;
738 }
739 }
740
741 return true;
742 }
743
SpeedECDHCurve(const std::string & name,const EC_GROUP * group,const std::string & selected)744 static bool SpeedECDHCurve(const std::string &name, const EC_GROUP *group,
745 const std::string &selected) {
746 if (!selected.empty() && name.find(selected) == std::string::npos) {
747 return true;
748 }
749
750 bssl::UniquePtr<EC_KEY> peer_key(EC_KEY_new());
751 if (!peer_key ||
752 !EC_KEY_set_group(peer_key.get(), group) ||
753 !EC_KEY_generate_key(peer_key.get())) {
754 return false;
755 }
756
757 size_t peer_value_len = EC_POINT_point2oct(
758 EC_KEY_get0_group(peer_key.get()), EC_KEY_get0_public_key(peer_key.get()),
759 POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr);
760 if (peer_value_len == 0) {
761 return false;
762 }
763 auto peer_value = std::make_unique<uint8_t[]>(peer_value_len);
764 peer_value_len = EC_POINT_point2oct(
765 EC_KEY_get0_group(peer_key.get()), EC_KEY_get0_public_key(peer_key.get()),
766 POINT_CONVERSION_UNCOMPRESSED, peer_value.get(), peer_value_len, nullptr);
767 if (peer_value_len == 0) {
768 return false;
769 }
770
771 TimeResults results;
772 if (!TimeFunctionParallel(
773 &results, [group, peer_value_len, &peer_value]() -> bool {
774 bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
775 if (!key || !EC_KEY_set_group(key.get(), group) ||
776 !EC_KEY_generate_key(key.get())) {
777 return false;
778 }
779 bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group));
780 bssl::UniquePtr<EC_POINT> peer_point(EC_POINT_new(group));
781 bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
782 bssl::UniquePtr<BIGNUM> x(BN_new());
783 if (!point || !peer_point || !ctx || !x ||
784 !EC_POINT_oct2point(group, peer_point.get(), peer_value.get(),
785 peer_value_len, ctx.get()) ||
786 !EC_POINT_mul(group, point.get(), nullptr, peer_point.get(),
787 EC_KEY_get0_private_key(key.get()), ctx.get()) ||
788 !EC_POINT_get_affine_coordinates_GFp(
789 group, point.get(), x.get(), nullptr, ctx.get())) {
790 return false;
791 }
792
793 return true;
794 })) {
795 return false;
796 }
797
798 results.Print(name);
799 return true;
800 }
801
SpeedECDSACurve(const std::string & name,const EC_GROUP * group,const std::string & selected)802 static bool SpeedECDSACurve(const std::string &name, const EC_GROUP *group,
803 const std::string &selected) {
804 if (!selected.empty() && name.find(selected) == std::string::npos) {
805 return true;
806 }
807
808 bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
809 if (!key ||
810 !EC_KEY_set_group(key.get(), group) ||
811 !EC_KEY_generate_key(key.get())) {
812 return false;
813 }
814
815 static constexpr size_t kMaxSignature = 256;
816 if (ECDSA_size(key.get()) > kMaxSignature) {
817 abort();
818 }
819 uint8_t digest[20];
820 OPENSSL_memset(digest, 42, sizeof(digest));
821
822 TimeResults results;
823 if (!TimeFunctionParallel(&results, [&key, &digest]() -> bool {
824 uint8_t out[kMaxSignature];
825 unsigned out_len;
826 return ECDSA_sign(0, digest, sizeof(digest), out, &out_len,
827 key.get()) == 1;
828 })) {
829 return false;
830 }
831
832 results.Print(name + " signing");
833
834 uint8_t signature[kMaxSignature];
835 unsigned sig_len;
836 if (!ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len, key.get())) {
837 return false;
838 }
839
840 if (!TimeFunctionParallel(
841 &results, [&key, &signature, &digest, sig_len]() -> bool {
842 return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len,
843 key.get()) == 1;
844 })) {
845 return false;
846 }
847
848 results.Print(name + " verify");
849
850 return true;
851 }
852
SpeedECDH(const std::string & selected)853 static bool SpeedECDH(const std::string &selected) {
854 return SpeedECDHCurve("ECDH P-224", EC_group_p224(), selected) &&
855 SpeedECDHCurve("ECDH P-256", EC_group_p256(), selected) &&
856 SpeedECDHCurve("ECDH P-384", EC_group_p384(), selected) &&
857 SpeedECDHCurve("ECDH P-521", EC_group_p521(), selected);
858 }
859
SpeedECDSA(const std::string & selected)860 static bool SpeedECDSA(const std::string &selected) {
861 return SpeedECDSACurve("ECDSA P-224", EC_group_p224(), selected) &&
862 SpeedECDSACurve("ECDSA P-256", EC_group_p256(), selected) &&
863 SpeedECDSACurve("ECDSA P-384", EC_group_p384(), selected) &&
864 SpeedECDSACurve("ECDSA P-521", EC_group_p521(), selected);
865 }
866
Speed25519(const std::string & selected)867 static bool Speed25519(const std::string &selected) {
868 if (!selected.empty() && selected.find("25519") == std::string::npos) {
869 return true;
870 }
871
872 TimeResults results;
873 if (!TimeFunctionParallel(&results, []() -> bool {
874 uint8_t public_key[32], private_key[64];
875 ED25519_keypair(public_key, private_key);
876 return true;
877 })) {
878 return false;
879 }
880
881 results.Print("Ed25519 key generation");
882
883 uint8_t public_key[32], private_key[64];
884 ED25519_keypair(public_key, private_key);
885 static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5};
886
887 if (!TimeFunctionParallel(&results, [&private_key]() -> bool {
888 uint8_t out[64];
889 return ED25519_sign(out, kMessage, sizeof(kMessage), private_key) == 1;
890 })) {
891 return false;
892 }
893
894 results.Print("Ed25519 signing");
895
896 uint8_t signature[64];
897 if (!ED25519_sign(signature, kMessage, sizeof(kMessage), private_key)) {
898 return false;
899 }
900
901 if (!TimeFunctionParallel(&results, [&public_key, &signature]() -> bool {
902 return ED25519_verify(kMessage, sizeof(kMessage), signature,
903 public_key) == 1;
904 })) {
905 fprintf(stderr, "Ed25519 verify failed.\n");
906 return false;
907 }
908
909 results.Print("Ed25519 verify");
910
911 if (!TimeFunctionParallel(&results, []() -> bool {
912 uint8_t out[32], in[32];
913 OPENSSL_memset(in, 0, sizeof(in));
914 X25519_public_from_private(out, in);
915 return true;
916 })) {
917 fprintf(stderr, "Curve25519 base-point multiplication failed.\n");
918 return false;
919 }
920
921 results.Print("Curve25519 base-point multiplication");
922
923 if (!TimeFunctionParallel(&results, []() -> bool {
924 uint8_t out[32], in1[32], in2[32];
925 OPENSSL_memset(in1, 0, sizeof(in1));
926 OPENSSL_memset(in2, 0, sizeof(in2));
927 in1[0] = 1;
928 in2[0] = 9;
929 return X25519(out, in1, in2) == 1;
930 })) {
931 fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n");
932 return false;
933 }
934
935 results.Print("Curve25519 arbitrary point multiplication");
936
937 return true;
938 }
939
SpeedSPAKE2(const std::string & selected)940 static bool SpeedSPAKE2(const std::string &selected) {
941 if (!selected.empty() && selected.find("SPAKE2") == std::string::npos) {
942 return true;
943 }
944
945 TimeResults results;
946
947 static const uint8_t kAliceName[] = {'A'};
948 static const uint8_t kBobName[] = {'B'};
949 static const uint8_t kPassword[] = "password";
950 bssl::UniquePtr<SPAKE2_CTX> alice(
951 SPAKE2_CTX_new(spake2_role_alice, kAliceName, sizeof(kAliceName),
952 kBobName, sizeof(kBobName)));
953 uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE];
954 size_t alice_msg_len;
955
956 if (!SPAKE2_generate_msg(alice.get(), alice_msg, &alice_msg_len,
957 sizeof(alice_msg), kPassword, sizeof(kPassword))) {
958 fprintf(stderr, "SPAKE2_generate_msg failed.\n");
959 return false;
960 }
961
962 if (!TimeFunctionParallel(&results, [&alice_msg, alice_msg_len]() -> bool {
963 bssl::UniquePtr<SPAKE2_CTX> bob(
964 SPAKE2_CTX_new(spake2_role_bob, kBobName, sizeof(kBobName),
965 kAliceName, sizeof(kAliceName)));
966 uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE], bob_key[64];
967 size_t bob_msg_len, bob_key_len;
968 if (!SPAKE2_generate_msg(bob.get(), bob_msg, &bob_msg_len,
969 sizeof(bob_msg), kPassword,
970 sizeof(kPassword)) ||
971 !SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len,
972 sizeof(bob_key), alice_msg, alice_msg_len)) {
973 return false;
974 }
975
976 return true;
977 })) {
978 fprintf(stderr, "SPAKE2 failed.\n");
979 }
980
981 results.Print("SPAKE2 over Ed25519");
982
983 return true;
984 }
985
SpeedScrypt(const std::string & selected)986 static bool SpeedScrypt(const std::string &selected) {
987 if (!selected.empty() && selected.find("scrypt") == std::string::npos) {
988 return true;
989 }
990
991 TimeResults results;
992
993 static const char kPassword[] = "password";
994 static const uint8_t kSalt[] = "NaCl";
995
996 if (!TimeFunctionParallel(&results, [&]() -> bool {
997 uint8_t out[64];
998 return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt,
999 sizeof(kSalt) - 1, 1024, 8, 16, 0 /* max_mem */,
1000 out, sizeof(out));
1001 })) {
1002 fprintf(stderr, "scrypt failed.\n");
1003 return false;
1004 }
1005 results.Print("scrypt (N = 1024, r = 8, p = 16)");
1006
1007 if (!TimeFunctionParallel(&results, [&]() -> bool {
1008 uint8_t out[64];
1009 return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt,
1010 sizeof(kSalt) - 1, 16384, 8, 1, 0 /* max_mem */,
1011 out, sizeof(out));
1012 })) {
1013 fprintf(stderr, "scrypt failed.\n");
1014 return false;
1015 }
1016 results.Print("scrypt (N = 16384, r = 8, p = 1)");
1017
1018 return true;
1019 }
1020
SpeedHRSS(const std::string & selected)1021 static bool SpeedHRSS(const std::string &selected) {
1022 if (!selected.empty() && selected != "HRSS") {
1023 return true;
1024 }
1025
1026 TimeResults results;
1027
1028 if (!TimeFunctionParallel(&results, []() -> bool {
1029 struct HRSS_public_key pub;
1030 struct HRSS_private_key priv;
1031 uint8_t entropy[HRSS_GENERATE_KEY_BYTES];
1032 RAND_bytes(entropy, sizeof(entropy));
1033 return HRSS_generate_key(&pub, &priv, entropy);
1034 })) {
1035 fprintf(stderr, "Failed to time HRSS_generate_key.\n");
1036 return false;
1037 }
1038
1039 results.Print("HRSS generate");
1040
1041 struct HRSS_public_key pub;
1042 struct HRSS_private_key priv;
1043 uint8_t key_entropy[HRSS_GENERATE_KEY_BYTES];
1044 RAND_bytes(key_entropy, sizeof(key_entropy));
1045 if (!HRSS_generate_key(&pub, &priv, key_entropy)) {
1046 return false;
1047 }
1048
1049 if (!TimeFunctionParallel(&results, [&pub]() -> bool {
1050 uint8_t entropy[HRSS_ENCAP_BYTES];
1051 uint8_t shared_key[HRSS_KEY_BYTES];
1052 uint8_t ciphertext[HRSS_CIPHERTEXT_BYTES];
1053 RAND_bytes(entropy, sizeof(entropy));
1054 return HRSS_encap(ciphertext, shared_key, &pub, entropy);
1055 })) {
1056 fprintf(stderr, "Failed to time HRSS_encap.\n");
1057 return false;
1058 }
1059 results.Print("HRSS encap");
1060
1061 uint8_t entropy[HRSS_ENCAP_BYTES];
1062 uint8_t shared_key[HRSS_KEY_BYTES];
1063 uint8_t ciphertext[HRSS_CIPHERTEXT_BYTES];
1064 RAND_bytes(entropy, sizeof(entropy));
1065 if (!HRSS_encap(ciphertext, shared_key, &pub, entropy)) {
1066 return false;
1067 }
1068
1069 if (!TimeFunctionParallel(&results, [&priv, &ciphertext]() -> bool {
1070 uint8_t shared_key2[HRSS_KEY_BYTES];
1071 return HRSS_decap(shared_key2, &priv, ciphertext, sizeof(ciphertext));
1072 })) {
1073 fprintf(stderr, "Failed to time HRSS_encap.\n");
1074 return false;
1075 }
1076
1077 results.Print("HRSS decap");
1078
1079 return true;
1080 }
1081
SpeedKyber(const std::string & selected)1082 static bool SpeedKyber(const std::string &selected) {
1083 if (!selected.empty() && selected != "Kyber") {
1084 return true;
1085 }
1086
1087 TimeResults results;
1088
1089 uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES];
1090 // This ciphertext is nonsense, but Kyber decap is constant-time so, for the
1091 // purposes of timing, it's fine.
1092 memset(ciphertext, 42, sizeof(ciphertext));
1093 if (!TimeFunctionParallel(&results, [&]() -> bool {
1094 KYBER_private_key priv;
1095 uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES];
1096 KYBER_generate_key(encoded_public_key, &priv);
1097 uint8_t shared_secret[32];
1098 KYBER_decap(shared_secret, sizeof(shared_secret), ciphertext, &priv);
1099 return true;
1100 })) {
1101 fprintf(stderr, "Failed to time KYBER_generate_key + KYBER_decap.\n");
1102 return false;
1103 }
1104
1105 results.Print("Kyber generate + decap");
1106
1107 KYBER_private_key priv;
1108 uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES];
1109 KYBER_generate_key(encoded_public_key, &priv);
1110 KYBER_public_key pub;
1111 if (!TimeFunctionParallel(&results, [&]() -> bool {
1112 CBS encoded_public_key_cbs;
1113 CBS_init(&encoded_public_key_cbs, encoded_public_key,
1114 sizeof(encoded_public_key));
1115 if (!KYBER_parse_public_key(&pub, &encoded_public_key_cbs)) {
1116 return false;
1117 }
1118 uint8_t shared_secret[32];
1119 KYBER_encap(ciphertext, shared_secret, sizeof(shared_secret), &pub);
1120 return true;
1121 })) {
1122 fprintf(stderr, "Failed to time KYBER_encap.\n");
1123 return false;
1124 }
1125
1126 results.Print("Kyber parse + encap");
1127
1128 return true;
1129 }
1130
SpeedSpx(const std::string & selected)1131 static bool SpeedSpx(const std::string &selected) {
1132 if (!selected.empty() && selected.find("spx") == std::string::npos) {
1133 return true;
1134 }
1135
1136 TimeResults results;
1137 if (!TimeFunctionParallel(&results, []() -> bool {
1138 uint8_t public_key[32], private_key[64];
1139 spx_generate_key(public_key, private_key);
1140 return true;
1141 })) {
1142 return false;
1143 }
1144
1145 results.Print("SPHINCS+-SHA2-128s key generation");
1146
1147 uint8_t public_key[32], private_key[64];
1148 spx_generate_key(public_key, private_key);
1149 static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5};
1150
1151 if (!TimeFunctionParallel(&results, [&private_key]() -> bool {
1152 uint8_t out[SPX_SIGNATURE_BYTES];
1153 spx_sign(out, private_key, kMessage, sizeof(kMessage), true);
1154 return true;
1155 })) {
1156 return false;
1157 }
1158
1159 results.Print("SPHINCS+-SHA2-128s signing");
1160
1161 uint8_t signature[SPX_SIGNATURE_BYTES];
1162 spx_sign(signature, private_key, kMessage, sizeof(kMessage), true);
1163
1164 if (!TimeFunctionParallel(&results, [&public_key, &signature]() -> bool {
1165 return spx_verify(signature, public_key, kMessage, sizeof(kMessage)) ==
1166 1;
1167 })) {
1168 fprintf(stderr, "SPHINCS+-SHA2-128s verify failed.\n");
1169 return false;
1170 }
1171
1172 results.Print("SPHINCS+-SHA2-128s verify");
1173
1174 return true;
1175 }
1176
SpeedHashToCurve(const std::string & selected)1177 static bool SpeedHashToCurve(const std::string &selected) {
1178 if (!selected.empty() && selected.find("hashtocurve") == std::string::npos) {
1179 return true;
1180 }
1181
1182 uint8_t input[64];
1183 RAND_bytes(input, sizeof(input));
1184
1185 static const uint8_t kLabel[] = "label";
1186
1187 TimeResults results;
1188 {
1189 if (!TimeFunctionParallel(&results, [&]() -> bool {
1190 EC_JACOBIAN out;
1191 return ec_hash_to_curve_p256_xmd_sha256_sswu(EC_group_p256(), &out,
1192 kLabel, sizeof(kLabel),
1193 input, sizeof(input));
1194 })) {
1195 fprintf(stderr, "hash-to-curve failed.\n");
1196 return false;
1197 }
1198 results.Print("hash-to-curve P256_XMD:SHA-256_SSWU_RO_");
1199
1200 if (!TimeFunctionParallel(&results, [&]() -> bool {
1201 EC_JACOBIAN out;
1202 return ec_hash_to_curve_p384_xmd_sha384_sswu(EC_group_p384(), &out,
1203 kLabel, sizeof(kLabel),
1204 input, sizeof(input));
1205 })) {
1206 fprintf(stderr, "hash-to-curve failed.\n");
1207 return false;
1208 }
1209 results.Print("hash-to-curve P384_XMD:SHA-384_SSWU_RO_");
1210
1211 if (!TimeFunctionParallel(&results, [&]() -> bool {
1212 EC_SCALAR out;
1213 return ec_hash_to_scalar_p384_xmd_sha512_draft07(
1214 EC_group_p384(), &out, kLabel, sizeof(kLabel), input,
1215 sizeof(input));
1216 })) {
1217 fprintf(stderr, "hash-to-scalar failed.\n");
1218 return false;
1219 }
1220 results.Print("hash-to-scalar P384_XMD:SHA-512");
1221 }
1222
1223 return true;
1224 }
1225
SpeedBase64(const std::string & selected)1226 static bool SpeedBase64(const std::string &selected) {
1227 if (!selected.empty() && selected.find("base64") == std::string::npos) {
1228 return true;
1229 }
1230
1231 static const char kInput[] =
1232 "MIIDtTCCAp2gAwIBAgIJALW2IrlaBKUhMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV"
1233 "BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX"
1234 "aWRnaXRzIFB0eSBMdGQwHhcNMTYwNzA5MDQzODA5WhcNMTYwODA4MDQzODA5WjBF"
1235 "MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50"
1236 "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB"
1237 "CgKCAQEAugvahBkSAUF1fC49vb1bvlPrcl80kop1iLpiuYoz4Qptwy57+EWssZBc"
1238 "HprZ5BkWf6PeGZ7F5AX1PyJbGHZLqvMCvViP6pd4MFox/igESISEHEixoiXCzepB"
1239 "rhtp5UQSjHD4D4hKtgdMgVxX+LRtwgW3mnu/vBu7rzpr/DS8io99p3lqZ1Aky+aN"
1240 "lcMj6MYy8U+YFEevb/V0lRY9oqwmW7BHnXikm/vi6sjIS350U8zb/mRzYeIs2R65"
1241 "LUduTL50+UMgat9ocewI2dv8aO9Dph+8NdGtg8LFYyTTHcUxJoMr1PTOgnmET19W"
1242 "JH4PrFwk7ZE1QJQQ1L4iKmPeQistuQIDAQABo4GnMIGkMB0GA1UdDgQWBBT5m6Vv"
1243 "zYjVYHG30iBE+j2XDhUE8jB1BgNVHSMEbjBsgBT5m6VvzYjVYHG30iBE+j2XDhUE"
1244 "8qFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNV"
1245 "BAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJALW2IrlaBKUhMAwGA1UdEwQF"
1246 "MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAD7Jg68SArYWlcoHfZAB90Pmyrt5H6D8"
1247 "LRi+W2Ri1fBNxREELnezWJ2scjl4UMcsKYp4Pi950gVN+62IgrImcCNvtb5I1Cfy"
1248 "/MNNur9ffas6X334D0hYVIQTePyFk3umI+2mJQrtZZyMPIKSY/sYGQHhGGX6wGK+"
1249 "GO/og0PQk/Vu6D+GU2XRnDV0YZg1lsAsHd21XryK6fDmNkEMwbIWrts4xc7scRrG"
1250 "HWy+iMf6/7p/Ak/SIicM4XSwmlQ8pPxAZPr+E2LoVd9pMpWUwpW2UbtO5wsGTrY5"
1251 "sO45tFNN/y+jtUheB1C2ijObG/tXELaiyCdM+S/waeuv0MXtI4xnn1A=";
1252
1253 TimeResults results;
1254 if (!TimeFunctionParallel(&results, [&]() -> bool {
1255 uint8_t out[sizeof(kInput)];
1256 size_t len;
1257 return EVP_DecodeBase64(out, &len, sizeof(out),
1258 reinterpret_cast<const uint8_t *>(kInput),
1259 strlen(kInput));
1260 })) {
1261 fprintf(stderr, "base64 decode failed.\n");
1262 return false;
1263 }
1264 results.PrintWithBytes("base64 decode", strlen(kInput));
1265 return true;
1266 }
1267
SpeedSipHash(const std::string & selected)1268 static bool SpeedSipHash(const std::string &selected) {
1269 if (!selected.empty() && selected.find("siphash") == std::string::npos) {
1270 return true;
1271 }
1272
1273 uint64_t key[2] = {0};
1274 for (size_t len : g_chunk_lengths) {
1275 std::vector<uint8_t> input(len);
1276 TimeResults results;
1277 if (!TimeFunctionParallel(&results, [&]() -> bool {
1278 SIPHASH_24(key, input.data(), input.size());
1279 return true;
1280 })) {
1281 fprintf(stderr, "SIPHASH_24 failed.\n");
1282 ERR_print_errors_fp(stderr);
1283 return false;
1284 }
1285 results.PrintWithBytes("SipHash-2-4" + ChunkLenSuffix(len), len);
1286 }
1287
1288 return true;
1289 }
1290
trust_token_pretoken_dup(const TRUST_TOKEN_PRETOKEN * in)1291 static TRUST_TOKEN_PRETOKEN *trust_token_pretoken_dup(
1292 const TRUST_TOKEN_PRETOKEN *in) {
1293 return static_cast<TRUST_TOKEN_PRETOKEN *>(
1294 OPENSSL_memdup(in, sizeof(TRUST_TOKEN_PRETOKEN)));
1295 }
1296
SpeedTrustToken(std::string name,const TRUST_TOKEN_METHOD * method,size_t batchsize,const std::string & selected)1297 static bool SpeedTrustToken(std::string name, const TRUST_TOKEN_METHOD *method,
1298 size_t batchsize, const std::string &selected) {
1299 if (!selected.empty() && selected.find("trusttoken") == std::string::npos) {
1300 return true;
1301 }
1302
1303 TimeResults results;
1304 if (!TimeFunction(&results, [&]() -> bool {
1305 uint8_t priv_key[TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE];
1306 uint8_t pub_key[TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE];
1307 size_t priv_key_len, pub_key_len;
1308 return TRUST_TOKEN_generate_key(
1309 method, priv_key, &priv_key_len, TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE,
1310 pub_key, &pub_key_len, TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE, 0);
1311 })) {
1312 fprintf(stderr, "TRUST_TOKEN_generate_key failed.\n");
1313 return false;
1314 }
1315 results.Print(name + " generate_key");
1316
1317 bssl::UniquePtr<TRUST_TOKEN_CLIENT> client(
1318 TRUST_TOKEN_CLIENT_new(method, batchsize));
1319 bssl::UniquePtr<TRUST_TOKEN_ISSUER> issuer(
1320 TRUST_TOKEN_ISSUER_new(method, batchsize));
1321 uint8_t priv_key[TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE];
1322 uint8_t pub_key[TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE];
1323 size_t priv_key_len, pub_key_len, key_index;
1324 if (!client || !issuer ||
1325 !TRUST_TOKEN_generate_key(
1326 method, priv_key, &priv_key_len, TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE,
1327 pub_key, &pub_key_len, TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE, 0) ||
1328 !TRUST_TOKEN_CLIENT_add_key(client.get(), &key_index, pub_key,
1329 pub_key_len) ||
1330 !TRUST_TOKEN_ISSUER_add_key(issuer.get(), priv_key, priv_key_len)) {
1331 fprintf(stderr, "failed to generate trust token key.\n");
1332 return false;
1333 }
1334
1335 uint8_t public_key[32], private_key[64];
1336 ED25519_keypair(public_key, private_key);
1337 bssl::UniquePtr<EVP_PKEY> priv(
1338 EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, nullptr, private_key, 32));
1339 bssl::UniquePtr<EVP_PKEY> pub(
1340 EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, public_key, 32));
1341 if (!priv || !pub) {
1342 fprintf(stderr, "failed to generate trust token SRR key.\n");
1343 return false;
1344 }
1345
1346 TRUST_TOKEN_CLIENT_set_srr_key(client.get(), pub.get());
1347 TRUST_TOKEN_ISSUER_set_srr_key(issuer.get(), priv.get());
1348 uint8_t metadata_key[32];
1349 RAND_bytes(metadata_key, sizeof(metadata_key));
1350 if (!TRUST_TOKEN_ISSUER_set_metadata_key(issuer.get(), metadata_key,
1351 sizeof(metadata_key))) {
1352 fprintf(stderr, "failed to generate trust token metadata key.\n");
1353 return false;
1354 }
1355
1356 if (!TimeFunction(&results, [&]() -> bool {
1357 uint8_t *issue_msg = NULL;
1358 size_t msg_len;
1359 int ok = TRUST_TOKEN_CLIENT_begin_issuance(client.get(), &issue_msg,
1360 &msg_len, batchsize);
1361 OPENSSL_free(issue_msg);
1362 // Clear pretokens.
1363 sk_TRUST_TOKEN_PRETOKEN_pop_free(client->pretokens,
1364 TRUST_TOKEN_PRETOKEN_free);
1365 client->pretokens = sk_TRUST_TOKEN_PRETOKEN_new_null();
1366 return ok;
1367 })) {
1368 fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_issuance failed.\n");
1369 return false;
1370 }
1371 results.Print(name + " begin_issuance");
1372
1373 uint8_t *issue_msg = NULL;
1374 size_t msg_len;
1375 if (!TRUST_TOKEN_CLIENT_begin_issuance(client.get(), &issue_msg, &msg_len,
1376 batchsize)) {
1377 fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_issuance failed.\n");
1378 return false;
1379 }
1380 bssl::UniquePtr<uint8_t> free_issue_msg(issue_msg);
1381
1382 bssl::UniquePtr<STACK_OF(TRUST_TOKEN_PRETOKEN)> pretokens(
1383 sk_TRUST_TOKEN_PRETOKEN_deep_copy(client->pretokens,
1384 trust_token_pretoken_dup,
1385 TRUST_TOKEN_PRETOKEN_free));
1386
1387 if (!TimeFunction(&results, [&]() -> bool {
1388 uint8_t *issue_resp = NULL;
1389 size_t resp_len, tokens_issued;
1390 int ok = TRUST_TOKEN_ISSUER_issue(issuer.get(), &issue_resp, &resp_len,
1391 &tokens_issued, issue_msg, msg_len,
1392 /*public_metadata=*/0,
1393 /*private_metadata=*/0,
1394 /*max_issuance=*/batchsize);
1395 OPENSSL_free(issue_resp);
1396 return ok;
1397 })) {
1398 fprintf(stderr, "TRUST_TOKEN_ISSUER_issue failed.\n");
1399 return false;
1400 }
1401 results.Print(name + " issue");
1402
1403 uint8_t *issue_resp = NULL;
1404 size_t resp_len, tokens_issued;
1405 if (!TRUST_TOKEN_ISSUER_issue(issuer.get(), &issue_resp, &resp_len,
1406 &tokens_issued, issue_msg, msg_len,
1407 /*public_metadata=*/0, /*private_metadata=*/0,
1408 /*max_issuance=*/batchsize)) {
1409 fprintf(stderr, "TRUST_TOKEN_ISSUER_issue failed.\n");
1410 return false;
1411 }
1412 bssl::UniquePtr<uint8_t> free_issue_resp(issue_resp);
1413
1414 if (!TimeFunction(&results, [&]() -> bool {
1415 size_t key_index2;
1416 bssl::UniquePtr<STACK_OF(TRUST_TOKEN)> tokens(
1417 TRUST_TOKEN_CLIENT_finish_issuance(client.get(), &key_index2,
1418 issue_resp, resp_len));
1419
1420 // Reset pretokens.
1421 client->pretokens = sk_TRUST_TOKEN_PRETOKEN_deep_copy(
1422 pretokens.get(), trust_token_pretoken_dup,
1423 TRUST_TOKEN_PRETOKEN_free);
1424 return !!tokens;
1425 })) {
1426 fprintf(stderr, "TRUST_TOKEN_CLIENT_finish_issuance failed.\n");
1427 return false;
1428 }
1429 results.Print(name + " finish_issuance");
1430
1431 bssl::UniquePtr<STACK_OF(TRUST_TOKEN)> tokens(
1432 TRUST_TOKEN_CLIENT_finish_issuance(client.get(), &key_index, issue_resp,
1433 resp_len));
1434 if (!tokens || sk_TRUST_TOKEN_num(tokens.get()) < 1) {
1435 fprintf(stderr, "TRUST_TOKEN_CLIENT_finish_issuance failed.\n");
1436 return false;
1437 }
1438
1439 const TRUST_TOKEN *token = sk_TRUST_TOKEN_value(tokens.get(), 0);
1440
1441 const uint8_t kClientData[] = "\x70TEST CLIENT DATA";
1442 uint64_t kRedemptionTime = 13374242;
1443
1444 if (!TimeFunction(&results, [&]() -> bool {
1445 uint8_t *redeem_msg = NULL;
1446 size_t redeem_msg_len;
1447 int ok = TRUST_TOKEN_CLIENT_begin_redemption(
1448 client.get(), &redeem_msg, &redeem_msg_len, token, kClientData,
1449 sizeof(kClientData) - 1, kRedemptionTime);
1450 OPENSSL_free(redeem_msg);
1451 return ok;
1452 })) {
1453 fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_redemption failed.\n");
1454 return false;
1455 }
1456 results.Print(name + " begin_redemption");
1457
1458 uint8_t *redeem_msg = NULL;
1459 size_t redeem_msg_len;
1460 if (!TRUST_TOKEN_CLIENT_begin_redemption(
1461 client.get(), &redeem_msg, &redeem_msg_len, token, kClientData,
1462 sizeof(kClientData) - 1, kRedemptionTime)) {
1463 fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_redemption failed.\n");
1464 return false;
1465 }
1466 bssl::UniquePtr<uint8_t> free_redeem_msg(redeem_msg);
1467
1468 if (!TimeFunction(&results, [&]() -> bool {
1469 uint32_t public_value;
1470 uint8_t private_value;
1471 TRUST_TOKEN *rtoken;
1472 uint8_t *client_data = NULL;
1473 size_t client_data_len;
1474 int ok = TRUST_TOKEN_ISSUER_redeem(
1475 issuer.get(), &public_value, &private_value, &rtoken, &client_data,
1476 &client_data_len, redeem_msg, redeem_msg_len);
1477 OPENSSL_free(client_data);
1478 TRUST_TOKEN_free(rtoken);
1479 return ok;
1480 })) {
1481 fprintf(stderr, "TRUST_TOKEN_ISSUER_redeem failed.\n");
1482 return false;
1483 }
1484 results.Print(name + " redeem");
1485
1486 uint32_t public_value;
1487 uint8_t private_value;
1488 TRUST_TOKEN *rtoken;
1489 uint8_t *client_data = NULL;
1490 size_t client_data_len;
1491 if (!TRUST_TOKEN_ISSUER_redeem(issuer.get(), &public_value, &private_value,
1492 &rtoken, &client_data, &client_data_len,
1493 redeem_msg, redeem_msg_len)) {
1494 fprintf(stderr, "TRUST_TOKEN_ISSUER_redeem failed.\n");
1495 return false;
1496 }
1497 bssl::UniquePtr<uint8_t> free_client_data(client_data);
1498 bssl::UniquePtr<TRUST_TOKEN> free_rtoken(rtoken);
1499
1500 return true;
1501 }
1502
1503 #if defined(BORINGSSL_FIPS)
SpeedSelfTest(const std::string & selected)1504 static bool SpeedSelfTest(const std::string &selected) {
1505 if (!selected.empty() && selected.find("self-test") == std::string::npos) {
1506 return true;
1507 }
1508
1509 TimeResults results;
1510 if (!TimeFunction(&results, []() -> bool { return BORINGSSL_self_test(); })) {
1511 fprintf(stderr, "BORINGSSL_self_test faileid.\n");
1512 ERR_print_errors_fp(stderr);
1513 return false;
1514 }
1515
1516 results.Print("self-test");
1517 return true;
1518 }
1519 #endif
1520
1521 static const struct argument kArguments[] = {
1522 {
1523 "-filter",
1524 kOptionalArgument,
1525 "A filter on the speed tests to run",
1526 },
1527 {
1528 "-timeout",
1529 kOptionalArgument,
1530 "The number of seconds to run each test for (default is 1)",
1531 },
1532 {
1533 "-chunks",
1534 kOptionalArgument,
1535 "A comma-separated list of input sizes to run tests at (default is "
1536 "16,256,1350,8192,16384)",
1537 },
1538 {
1539 "-json",
1540 kBooleanArgument,
1541 "If this flag is set, speed will print the output of each benchmark in "
1542 "JSON format as follows: \"{\"description\": "
1543 "\"descriptionOfOperation\", \"numCalls\": 1234, "
1544 "\"timeInMicroseconds\": 1234567, \"bytesPerCall\": 1234}\". When "
1545 "there is no information about the bytes per call for an operation, "
1546 "the JSON field for bytesPerCall will be omitted.",
1547 },
1548 #if defined(OPENSSL_THREADS)
1549 {
1550 "-threads",
1551 kOptionalArgument,
1552 "The number of threads to benchmark in parallel (default is 1)",
1553 },
1554 #endif
1555 {
1556 "",
1557 kOptionalArgument,
1558 "",
1559 },
1560 };
1561
Speed(const std::vector<std::string> & args)1562 bool Speed(const std::vector<std::string> &args) {
1563 std::map<std::string, std::string> args_map;
1564 if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
1565 PrintUsage(kArguments);
1566 return false;
1567 }
1568
1569 std::string selected;
1570 if (args_map.count("-filter") != 0) {
1571 selected = args_map["-filter"];
1572 }
1573
1574 if (args_map.count("-json") != 0) {
1575 g_print_json = true;
1576 }
1577
1578 if (args_map.count("-timeout") != 0) {
1579 g_timeout_seconds = atoi(args_map["-timeout"].c_str());
1580 }
1581
1582 #if defined(OPENSSL_THREADS)
1583 if (args_map.count("-threads") != 0) {
1584 g_threads = atoi(args_map["-threads"].c_str());
1585 }
1586 #endif
1587
1588 if (args_map.count("-chunks") != 0) {
1589 g_chunk_lengths.clear();
1590 const char *start = args_map["-chunks"].data();
1591 const char *end = start + args_map["-chunks"].size();
1592 while (start != end) {
1593 errno = 0;
1594 char *ptr;
1595 unsigned long long val = strtoull(start, &ptr, 10);
1596 if (ptr == start /* no numeric characters found */ ||
1597 errno == ERANGE /* overflow */ || static_cast<size_t>(val) != val) {
1598 fprintf(stderr, "Error parsing -chunks argument\n");
1599 return false;
1600 }
1601 g_chunk_lengths.push_back(static_cast<size_t>(val));
1602 start = ptr;
1603 if (start != end) {
1604 if (*start != ',') {
1605 fprintf(stderr, "Error parsing -chunks argument\n");
1606 return false;
1607 }
1608 start++;
1609 }
1610 }
1611 }
1612
1613 // kTLSADLen is the number of bytes of additional data that TLS passes to
1614 // AEADs.
1615 static const size_t kTLSADLen = 13;
1616 // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs.
1617 // These are AEADs that weren't originally defined as AEADs, but which we use
1618 // via the AEAD interface. In order for that to work, they have some TLS
1619 // knowledge in them and construct a couple of the AD bytes internally.
1620 static const size_t kLegacyADLen = kTLSADLen - 2;
1621
1622 if (g_print_json) {
1623 puts("[");
1624 }
1625 if (!SpeedRSA(selected) ||
1626 !SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) ||
1627 !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) ||
1628 !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen,
1629 selected) ||
1630 !SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1",
1631 kLegacyADLen, selected) ||
1632 !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1",
1633 kLegacyADLen, selected) ||
1634 !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1",
1635 kLegacyADLen, selected) ||
1636 !SpeedAEADOpen(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1",
1637 kLegacyADLen, selected) ||
1638 !SpeedAEADOpen(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1",
1639 kLegacyADLen, selected) ||
1640 !SpeedAEAD(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
1641 selected) ||
1642 !SpeedAEAD(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
1643 selected) ||
1644 !SpeedAEADOpen(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
1645 selected) ||
1646 !SpeedAEADOpen(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
1647 selected) ||
1648 !SpeedAEAD(EVP_aead_aes_128_ccm_bluetooth(), "AES-128-CCM-Bluetooth",
1649 kTLSADLen, selected) ||
1650 !SpeedAESBlock("AES-128", 128, selected) ||
1651 !SpeedAESBlock("AES-256", 256, selected) ||
1652 !SpeedHash(EVP_sha1(), "SHA-1", selected) ||
1653 !SpeedHash(EVP_sha256(), "SHA-256", selected) ||
1654 !SpeedHash(EVP_sha512(), "SHA-512", selected) ||
1655 !SpeedHash(EVP_blake2b256(), "BLAKE2b-256", selected) ||
1656 !SpeedRandom(selected) || //
1657 !SpeedECDH(selected) || //
1658 !SpeedECDSA(selected) || //
1659 !Speed25519(selected) || //
1660 !SpeedSPAKE2(selected) || //
1661 !SpeedScrypt(selected) || //
1662 !SpeedRSAKeyGen(selected) || //
1663 !SpeedHRSS(selected) || //
1664 !SpeedKyber(selected) || //
1665 !SpeedSpx(selected) || //
1666 !SpeedHashToCurve(selected) || //
1667 !SpeedTrustToken("TrustToken-Exp1-Batch1", TRUST_TOKEN_experiment_v1(), 1,
1668 selected) ||
1669 !SpeedTrustToken("TrustToken-Exp1-Batch10", TRUST_TOKEN_experiment_v1(),
1670 10, selected) ||
1671 !SpeedTrustToken("TrustToken-Exp2VOPRF-Batch1",
1672 TRUST_TOKEN_experiment_v2_voprf(), 1, selected) ||
1673 !SpeedTrustToken("TrustToken-Exp2VOPRF-Batch10",
1674 TRUST_TOKEN_experiment_v2_voprf(), 10, selected) ||
1675 !SpeedTrustToken("TrustToken-Exp2PMB-Batch1",
1676 TRUST_TOKEN_experiment_v2_pmb(), 1, selected) ||
1677 !SpeedTrustToken("TrustToken-Exp2PMB-Batch10",
1678 TRUST_TOKEN_experiment_v2_pmb(), 10, selected) ||
1679 !SpeedBase64(selected) || //
1680 !SpeedSipHash(selected)) {
1681 return false;
1682 }
1683 #if defined(BORINGSSL_FIPS)
1684 if (!SpeedSelfTest(selected)) {
1685 return false;
1686 }
1687 #endif
1688 if (g_print_json) {
1689 puts("\n]");
1690 }
1691
1692 return true;
1693 }
1694