Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 <string> |
| 16 | #include <functional> |
| 17 | #include <memory> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include <stdint.h> |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 21 | #include <stdlib.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 22 | #include <string.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 23 | |
Adam Langley | 92b6b02 | 2015-04-16 14:01:33 -0700 | [diff] [blame] | 24 | #include <openssl/aead.h> |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 25 | #include <openssl/bn.h> |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 26 | #include <openssl/curve25519.h> |
Adam Langley | 92b6b02 | 2015-04-16 14:01:33 -0700 | [diff] [blame] | 27 | #include <openssl/digest.h> |
| 28 | #include <openssl/err.h> |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 29 | #include <openssl/ec.h> |
| 30 | #include <openssl/ecdsa.h> |
| 31 | #include <openssl/ec_key.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 32 | #include <openssl/nid.h> |
Adam Langley | 92b6b02 | 2015-04-16 14:01:33 -0700 | [diff] [blame] | 33 | #include <openssl/rand.h> |
| 34 | #include <openssl/rsa.h> |
| 35 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 36 | #if defined(OPENSSL_WINDOWS) |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 37 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 38 | #include <windows.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 39 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 40 | #elif defined(OPENSSL_APPLE) |
| 41 | #include <sys/time.h> |
David Benjamin | 2f401ec | 2016-09-09 19:49:35 -0400 | [diff] [blame] | 42 | #else |
| 43 | #include <time.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 46 | #include "../crypto/internal.h" |
David Benjamin | 58084af | 2015-06-07 00:25:15 -0400 | [diff] [blame] | 47 | #include "internal.h" |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 48 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 49 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 50 | // TimeResults represents the results of benchmarking a function. |
| 51 | struct TimeResults { |
| 52 | // num_calls is the number of function calls done in the time period. |
| 53 | unsigned num_calls; |
| 54 | // us is the number of microseconds that elapsed in the time period. |
| 55 | unsigned us; |
| 56 | |
| 57 | void Print(const std::string &description) { |
| 58 | printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls, |
| 59 | description.c_str(), us, |
| 60 | (static_cast<double>(num_calls) / us) * 1000000); |
| 61 | } |
| 62 | |
| 63 | void PrintWithBytes(const std::string &description, size_t bytes_per_call) { |
| 64 | printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n", |
| 65 | num_calls, description.c_str(), us, |
| 66 | (static_cast<double>(num_calls) / us) * 1000000, |
| 67 | static_cast<double>(bytes_per_call * num_calls) / us); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | #if defined(OPENSSL_WINDOWS) |
| 72 | static uint64_t time_now() { return GetTickCount64() * 1000; } |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 73 | #elif defined(OPENSSL_APPLE) |
| 74 | static uint64_t time_now() { |
| 75 | struct timeval tv; |
| 76 | uint64_t ret; |
| 77 | |
| 78 | gettimeofday(&tv, NULL); |
| 79 | ret = tv.tv_sec; |
| 80 | ret *= 1000000; |
| 81 | ret += tv.tv_usec; |
| 82 | return ret; |
| 83 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 84 | #else |
| 85 | static uint64_t time_now() { |
| 86 | struct timespec ts; |
| 87 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 88 | |
| 89 | uint64_t ret = ts.tv_sec; |
| 90 | ret *= 1000000; |
| 91 | ret += ts.tv_nsec / 1000; |
| 92 | return ret; |
| 93 | } |
| 94 | #endif |
| 95 | |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 96 | static uint64_t g_timeout_seconds = 1; |
| 97 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | static bool TimeFunction(TimeResults *results, std::function<bool()> func) { |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 99 | // total_us is the total amount of time that we'll aim to measure a function |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | // for. |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 101 | const uint64_t total_us = g_timeout_seconds * 1000000; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 102 | uint64_t start = time_now(), now, delta; |
| 103 | unsigned done = 0, iterations_between_time_checks; |
| 104 | |
| 105 | if (!func()) { |
| 106 | return false; |
| 107 | } |
| 108 | now = time_now(); |
| 109 | delta = now - start; |
| 110 | if (delta == 0) { |
| 111 | iterations_between_time_checks = 250; |
| 112 | } else { |
| 113 | // Aim for about 100ms between time checks. |
| 114 | iterations_between_time_checks = |
| 115 | static_cast<double>(100000) / static_cast<double>(delta); |
| 116 | if (iterations_between_time_checks > 1000) { |
| 117 | iterations_between_time_checks = 1000; |
| 118 | } else if (iterations_between_time_checks < 1) { |
| 119 | iterations_between_time_checks = 1; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | for (;;) { |
| 124 | for (unsigned i = 0; i < iterations_between_time_checks; i++) { |
| 125 | if (!func()) { |
| 126 | return false; |
| 127 | } |
| 128 | done++; |
| 129 | } |
| 130 | |
| 131 | now = time_now(); |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 132 | if (now - start > total_us) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | results->us = now - start; |
| 138 | results->num_calls = done; |
| 139 | return true; |
| 140 | } |
| 141 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 142 | static bool SpeedRSA(const std::string &key_name, RSA *key, |
| 143 | const std::string &selected) { |
| 144 | if (!selected.empty() && key_name.find(selected) == std::string::npos) { |
| 145 | return true; |
| 146 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 147 | |
| 148 | std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]); |
| 149 | const uint8_t fake_sha256_hash[32] = {0}; |
| 150 | unsigned sig_len; |
| 151 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 152 | TimeResults results; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | if (!TimeFunction(&results, |
| 154 | [key, &sig, &fake_sha256_hash, &sig_len]() -> bool { |
Brian Smith | 7bee853 | 2016-08-19 15:11:20 -1000 | [diff] [blame] | 155 | /* Usually during RSA signing we're using a long-lived |RSA| that has |
| 156 | * already had all of its |BN_MONT_CTX|s constructed, so it makes |
| 157 | * sense to use |key| directly here. */ |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash), |
| 159 | sig.get(), &sig_len, key); |
| 160 | })) { |
| 161 | fprintf(stderr, "RSA_sign failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 162 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | return false; |
| 164 | } |
| 165 | results.Print(key_name + " signing"); |
| 166 | |
| 167 | if (!TimeFunction(&results, |
| 168 | [key, &fake_sha256_hash, &sig, sig_len]() -> bool { |
Brian Smith | 7bee853 | 2016-08-19 15:11:20 -1000 | [diff] [blame] | 169 | /* Usually during RSA verification we have to parse an RSA key from a |
| 170 | * certificate or similar, in which case we'd need to construct a new |
| 171 | * RSA key, with a new |BN_MONT_CTX| for the public modulus. If we were |
| 172 | * to use |key| directly instead, then these costs wouldn't be |
| 173 | * accounted for. */ |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 174 | bssl::UniquePtr<RSA> verify_key(RSA_new()); |
Brian Smith | 7bee853 | 2016-08-19 15:11:20 -1000 | [diff] [blame] | 175 | if (!verify_key) { |
| 176 | return false; |
| 177 | } |
| 178 | verify_key->n = BN_dup(key->n); |
| 179 | verify_key->e = BN_dup(key->e); |
| 180 | if (!verify_key->n || |
| 181 | !verify_key->e) { |
| 182 | return false; |
| 183 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | return RSA_verify(NID_sha256, fake_sha256_hash, |
| 185 | sizeof(fake_sha256_hash), sig.get(), sig_len, key); |
| 186 | })) { |
| 187 | fprintf(stderr, "RSA_verify failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 188 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | results.Print(key_name + " verify"); |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 196 | static uint8_t *align(uint8_t *in, unsigned alignment) { |
| 197 | return reinterpret_cast<uint8_t *>( |
Brian Smith | d53b2c3 | 2015-03-17 00:37:06 -1000 | [diff] [blame] | 198 | (reinterpret_cast<uintptr_t>(in) + alignment) & |
| 199 | ~static_cast<size_t>(alignment - 1)); |
David Benjamin | 384673c | 2015-01-21 15:56:14 -0500 | [diff] [blame] | 200 | } |
Adam Langley | 543d006 | 2015-01-15 16:05:41 -0800 | [diff] [blame] | 201 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 202 | static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name, |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 203 | size_t chunk_len, size_t ad_len, |
| 204 | evp_aead_direction_t direction) { |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 205 | static const unsigned kAlignment = 16; |
| 206 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 207 | bssl::ScopedEVP_AEAD_CTX ctx; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | const size_t key_len = EVP_AEAD_key_length(aead); |
| 209 | const size_t nonce_len = EVP_AEAD_nonce_length(aead); |
| 210 | const size_t overhead_len = EVP_AEAD_max_overhead(aead); |
| 211 | |
| 212 | std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 213 | OPENSSL_memset(key.get(), 0, key_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 215 | OPENSSL_memset(nonce.get(), 0, nonce_len); |
Brian Smith | 1d1562d | 2015-03-17 00:32:20 -1000 | [diff] [blame] | 216 | std::unique_ptr<uint8_t[]> in_storage(new uint8_t[chunk_len + kAlignment]); |
| 217 | std::unique_ptr<uint8_t[]> out_storage(new uint8_t[chunk_len + overhead_len + kAlignment]); |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 218 | std::unique_ptr<uint8_t[]> in2_storage(new uint8_t[chunk_len + kAlignment]); |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 219 | std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 220 | OPENSSL_memset(ad.get(), 0, ad_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 222 | uint8_t *const in = align(in_storage.get(), kAlignment); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 223 | OPENSSL_memset(in, 0, chunk_len); |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 224 | uint8_t *const out = align(out_storage.get(), kAlignment); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 225 | OPENSSL_memset(out, 0, chunk_len + overhead_len); |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 226 | uint8_t *const in2 = align(in2_storage.get(), kAlignment); |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 227 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 228 | if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len, |
David Benjamin | d434f28 | 2015-03-17 18:28:37 -0400 | [diff] [blame] | 229 | EVP_AEAD_DEFAULT_TAG_LENGTH, |
| 230 | evp_aead_seal)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 232 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | TimeResults results; |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 237 | if (direction == evp_aead_seal) { |
| 238 | if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, in, |
| 239 | out, &ctx, &nonce, &ad]() -> bool { |
| 240 | size_t out_len; |
| 241 | return EVP_AEAD_CTX_seal(ctx.get(), out, &out_len, |
| 242 | chunk_len + overhead_len, nonce.get(), |
| 243 | nonce_len, in, chunk_len, ad.get(), ad_len); |
| 244 | })) { |
| 245 | fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n"); |
| 246 | ERR_print_errors_fp(stderr); |
| 247 | return false; |
| 248 | } |
| 249 | } else { |
| 250 | size_t out_len; |
| 251 | EVP_AEAD_CTX_seal(ctx.get(), out, &out_len, chunk_len + overhead_len, |
| 252 | nonce.get(), nonce_len, in, chunk_len, ad.get(), ad_len); |
| 253 | |
Adam Langley | 84cd493 | 2017-03-02 10:30:03 -0800 | [diff] [blame] | 254 | if (!TimeFunction(&results, [chunk_len, nonce_len, ad_len, in2, out, &ctx, |
| 255 | &nonce, &ad, out_len]() -> bool { |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 256 | size_t in2_len; |
| 257 | return EVP_AEAD_CTX_open(ctx.get(), in2, &in2_len, chunk_len, |
| 258 | nonce.get(), nonce_len, out, out_len, |
| 259 | ad.get(), ad_len); |
| 260 | })) { |
| 261 | fprintf(stderr, "EVP_AEAD_CTX_open failed.\n"); |
| 262 | ERR_print_errors_fp(stderr); |
| 263 | return false; |
| 264 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 267 | results.PrintWithBytes( |
| 268 | name + (direction == evp_aead_seal ? " seal" : " open"), chunk_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | return true; |
| 270 | } |
| 271 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 272 | static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name, |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 273 | size_t ad_len, const std::string &selected) { |
| 274 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 275 | return true; |
| 276 | } |
| 277 | |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 278 | return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len, |
| 279 | evp_aead_seal) && |
| 280 | SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len, |
| 281 | evp_aead_seal) && |
| 282 | SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len, |
| 283 | evp_aead_seal); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 286 | #if !defined(OPENSSL_SMALL) |
| 287 | static 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 | } |
| 300 | #endif /* !SMALL */ |
| 301 | |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 302 | static bool SpeedHashChunk(const EVP_MD *md, const std::string &name, |
| 303 | size_t chunk_len) { |
| 304 | EVP_MD_CTX *ctx = EVP_MD_CTX_create(); |
| 305 | uint8_t scratch[8192]; |
| 306 | |
| 307 | if (chunk_len > sizeof(scratch)) { |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | TimeResults results; |
| 312 | if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool { |
| 313 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 314 | unsigned int md_len; |
| 315 | |
| 316 | return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) && |
| 317 | EVP_DigestUpdate(ctx, scratch, chunk_len) && |
| 318 | EVP_DigestFinal_ex(ctx, digest, &md_len); |
| 319 | })) { |
| 320 | fprintf(stderr, "EVP_DigestInit_ex failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 321 | ERR_print_errors_fp(stderr); |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | |
| 325 | results.PrintWithBytes(name, chunk_len); |
| 326 | |
| 327 | EVP_MD_CTX_destroy(ctx); |
| 328 | |
| 329 | return true; |
| 330 | } |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 331 | static bool SpeedHash(const EVP_MD *md, const std::string &name, |
| 332 | const std::string &selected) { |
| 333 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 334 | return true; |
| 335 | } |
| 336 | |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | return SpeedHashChunk(md, name + " (16 bytes)", 16) && |
| 338 | SpeedHashChunk(md, name + " (256 bytes)", 256) && |
| 339 | SpeedHashChunk(md, name + " (8192 bytes)", 8192); |
| 340 | } |
| 341 | |
David Benjamin | 1e5ac5d | 2016-11-01 16:37:09 -0400 | [diff] [blame] | 342 | static bool SpeedRandomChunk(const std::string &name, size_t chunk_len) { |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 343 | uint8_t scratch[8192]; |
| 344 | |
| 345 | if (chunk_len > sizeof(scratch)) { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | TimeResults results; |
| 350 | if (!TimeFunction(&results, [chunk_len, &scratch]() -> bool { |
| 351 | RAND_bytes(scratch, chunk_len); |
| 352 | return true; |
| 353 | })) { |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | results.PrintWithBytes(name, chunk_len); |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | static bool SpeedRandom(const std::string &selected) { |
| 362 | if (!selected.empty() && selected != "RNG") { |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | return SpeedRandomChunk("RNG (16 bytes)", 16) && |
| 367 | SpeedRandomChunk("RNG (256 bytes)", 256) && |
| 368 | SpeedRandomChunk("RNG (8192 bytes)", 8192); |
| 369 | } |
| 370 | |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 371 | static bool SpeedECDHCurve(const std::string &name, int nid, |
| 372 | const std::string &selected) { |
| 373 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | TimeResults results; |
| 378 | if (!TimeFunction(&results, [nid]() -> bool { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 379 | bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid)); |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 380 | if (!key || |
| 381 | !EC_KEY_generate_key(key.get())) { |
| 382 | return false; |
| 383 | } |
| 384 | const EC_GROUP *const group = EC_KEY_get0_group(key.get()); |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 385 | bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group)); |
| 386 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 387 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 388 | bssl::UniquePtr<BIGNUM> x(BN_new()); |
| 389 | bssl::UniquePtr<BIGNUM> y(BN_new()); |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 390 | |
| 391 | if (!point || !ctx || !x || !y || |
| 392 | !EC_POINT_mul(group, point.get(), NULL, |
| 393 | EC_KEY_get0_public_key(key.get()), |
| 394 | EC_KEY_get0_private_key(key.get()), ctx.get()) || |
| 395 | !EC_POINT_get_affine_coordinates_GFp(group, point.get(), x.get(), |
| 396 | y.get(), ctx.get())) { |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | return true; |
| 401 | })) { |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | results.Print(name); |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | static bool SpeedECDSACurve(const std::string &name, int nid, |
| 410 | const std::string &selected) { |
| 411 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 412 | return true; |
| 413 | } |
| 414 | |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 415 | bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid)); |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 416 | if (!key || |
| 417 | !EC_KEY_generate_key(key.get())) { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | uint8_t signature[256]; |
| 422 | if (ECDSA_size(key.get()) > sizeof(signature)) { |
| 423 | return false; |
| 424 | } |
| 425 | uint8_t digest[20]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 426 | OPENSSL_memset(digest, 42, sizeof(digest)); |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 427 | unsigned sig_len; |
| 428 | |
| 429 | TimeResults results; |
| 430 | if (!TimeFunction(&results, [&key, &signature, &digest, &sig_len]() -> bool { |
| 431 | return ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len, |
| 432 | key.get()) == 1; |
| 433 | })) { |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | results.Print(name + " signing"); |
| 438 | |
| 439 | if (!TimeFunction(&results, [&key, &signature, &digest, sig_len]() -> bool { |
| 440 | return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len, |
| 441 | key.get()) == 1; |
| 442 | })) { |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | results.Print(name + " verify"); |
| 447 | |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | static bool SpeedECDH(const std::string &selected) { |
| 452 | return SpeedECDHCurve("ECDH P-224", NID_secp224r1, selected) && |
| 453 | SpeedECDHCurve("ECDH P-256", NID_X9_62_prime256v1, selected) && |
| 454 | SpeedECDHCurve("ECDH P-384", NID_secp384r1, selected) && |
| 455 | SpeedECDHCurve("ECDH P-521", NID_secp521r1, selected); |
| 456 | } |
| 457 | |
| 458 | static bool SpeedECDSA(const std::string &selected) { |
| 459 | return SpeedECDSACurve("ECDSA P-224", NID_secp224r1, selected) && |
| 460 | SpeedECDSACurve("ECDSA P-256", NID_X9_62_prime256v1, selected) && |
| 461 | SpeedECDSACurve("ECDSA P-384", NID_secp384r1, selected) && |
| 462 | SpeedECDSACurve("ECDSA P-521", NID_secp521r1, selected); |
| 463 | } |
| 464 | |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 465 | static bool Speed25519(const std::string &selected) { |
| 466 | if (!selected.empty() && selected.find("25519") == std::string::npos) { |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | TimeResults results; |
| 471 | |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 472 | uint8_t public_key[32], private_key[64]; |
| 473 | |
| 474 | if (!TimeFunction(&results, [&public_key, &private_key]() -> bool { |
| 475 | ED25519_keypair(public_key, private_key); |
| 476 | return true; |
| 477 | })) { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | results.Print("Ed25519 key generation"); |
| 482 | |
| 483 | static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5}; |
| 484 | uint8_t signature[64]; |
| 485 | |
| 486 | if (!TimeFunction(&results, [&private_key, &signature]() -> bool { |
| 487 | return ED25519_sign(signature, kMessage, sizeof(kMessage), |
| 488 | private_key) == 1; |
| 489 | })) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | results.Print("Ed25519 signing"); |
| 494 | |
| 495 | if (!TimeFunction(&results, [&public_key, &signature]() -> bool { |
| 496 | return ED25519_verify(kMessage, sizeof(kMessage), signature, |
| 497 | public_key) == 1; |
| 498 | })) { |
| 499 | fprintf(stderr, "Ed25519 verify failed.\n"); |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | results.Print("Ed25519 verify"); |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 504 | |
| 505 | if (!TimeFunction(&results, []() -> bool { |
| 506 | uint8_t out[32], in[32]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 507 | OPENSSL_memset(in, 0, sizeof(in)); |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 508 | X25519_public_from_private(out, in); |
| 509 | return true; |
| 510 | })) { |
| 511 | fprintf(stderr, "Curve25519 base-point multiplication failed.\n"); |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | results.Print("Curve25519 base-point multiplication"); |
| 516 | |
| 517 | if (!TimeFunction(&results, []() -> bool { |
| 518 | uint8_t out[32], in1[32], in2[32]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 519 | OPENSSL_memset(in1, 0, sizeof(in1)); |
| 520 | OPENSSL_memset(in2, 0, sizeof(in2)); |
Adam Langley | 3ac32b1 | 2015-11-17 15:15:05 -0800 | [diff] [blame] | 521 | in1[0] = 1; |
| 522 | in2[0] = 9; |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 523 | return X25519(out, in1, in2) == 1; |
| 524 | })) { |
| 525 | fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n"); |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | results.Print("Curve25519 arbitrary point multiplication"); |
| 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
Arnar Birgisson | f27459e | 2016-02-09 18:09:00 -0800 | [diff] [blame] | 534 | static bool SpeedSPAKE2(const std::string &selected) { |
| 535 | if (!selected.empty() && selected.find("SPAKE2") == std::string::npos) { |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | TimeResults results; |
| 540 | |
| 541 | static const uint8_t kAliceName[] = {'A'}; |
| 542 | static const uint8_t kBobName[] = {'B'}; |
| 543 | static const uint8_t kPassword[] = "password"; |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 544 | bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(spake2_role_alice, |
| 545 | kAliceName, sizeof(kAliceName), kBobName, |
| 546 | sizeof(kBobName))); |
Arnar Birgisson | f27459e | 2016-02-09 18:09:00 -0800 | [diff] [blame] | 547 | uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE]; |
| 548 | size_t alice_msg_len; |
| 549 | |
| 550 | if (!SPAKE2_generate_msg(alice.get(), alice_msg, &alice_msg_len, |
| 551 | sizeof(alice_msg), |
| 552 | kPassword, sizeof(kPassword))) { |
| 553 | fprintf(stderr, "SPAKE2_generate_msg failed.\n"); |
| 554 | return false; |
| 555 | } |
| 556 | |
Adam Langley | 708db16 | 2016-03-01 11:48:00 -0800 | [diff] [blame] | 557 | if (!TimeFunction(&results, [&alice_msg, alice_msg_len]() -> bool { |
Matt Braithwaite | d17d74d | 2016-08-17 20:10:28 -0700 | [diff] [blame] | 558 | bssl::UniquePtr<SPAKE2_CTX> bob(SPAKE2_CTX_new(spake2_role_bob, |
| 559 | kBobName, sizeof(kBobName), kAliceName, |
| 560 | sizeof(kAliceName))); |
Arnar Birgisson | f27459e | 2016-02-09 18:09:00 -0800 | [diff] [blame] | 561 | uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE], bob_key[64]; |
| 562 | size_t bob_msg_len, bob_key_len; |
| 563 | if (!SPAKE2_generate_msg(bob.get(), bob_msg, &bob_msg_len, |
| 564 | sizeof(bob_msg), kPassword, |
| 565 | sizeof(kPassword)) || |
| 566 | !SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len, |
| 567 | sizeof(bob_key), alice_msg, alice_msg_len)) { |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | return true; |
| 572 | })) { |
| 573 | fprintf(stderr, "SPAKE2 failed.\n"); |
| 574 | } |
| 575 | |
| 576 | results.Print("SPAKE2 over Ed25519"); |
| 577 | |
| 578 | return true; |
| 579 | } |
| 580 | |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 581 | static const struct argument kArguments[] = { |
| 582 | { |
| 583 | "-filter", kOptionalArgument, |
| 584 | "A filter on the speed tests to run", |
| 585 | }, |
| 586 | { |
| 587 | "-timeout", kOptionalArgument, |
| 588 | "The number of seconds to run each test for (default is 1)", |
| 589 | }, |
| 590 | { |
| 591 | "", kOptionalArgument, "", |
| 592 | }, |
| 593 | }; |
| 594 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 595 | bool Speed(const std::vector<std::string> &args) { |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 596 | std::map<std::string, std::string> args_map; |
| 597 | if (!ParseKeyValueArguments(&args_map, args, kArguments)) { |
| 598 | PrintUsage(kArguments); |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 599 | return false; |
| 600 | } |
David Benjamin | bcb65b9 | 2016-08-14 22:06:49 -0400 | [diff] [blame] | 601 | |
| 602 | std::string selected; |
| 603 | if (args_map.count("-filter") != 0) { |
| 604 | selected = args_map["-filter"]; |
| 605 | } |
| 606 | |
| 607 | if (args_map.count("-timeout") != 0) { |
| 608 | g_timeout_seconds = atoi(args_map["-timeout"].c_str()); |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 609 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 610 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 611 | bssl::UniquePtr<RSA> key( |
| 612 | RSA_private_key_from_bytes(kDERRSAPrivate2048, kDERRSAPrivate2048Len)); |
| 613 | if (key == nullptr) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 614 | fprintf(stderr, "Failed to parse RSA key.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 615 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 616 | return false; |
| 617 | } |
| 618 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 619 | if (!SpeedRSA("RSA 2048", key.get(), selected)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 623 | key.reset( |
| 624 | RSA_private_key_from_bytes(kDERRSAPrivate4096, kDERRSAPrivate4096Len)); |
| 625 | if (key == nullptr) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 626 | fprintf(stderr, "Failed to parse 4096-bit RSA key.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 627 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | return 1; |
| 629 | } |
| 630 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 631 | if (!SpeedRSA("RSA 4096", key.get(), selected)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 632 | return false; |
| 633 | } |
| 634 | |
David Benjamin | 0cce863 | 2016-10-20 15:13:26 -0400 | [diff] [blame] | 635 | key.reset(); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 636 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 637 | // kTLSADLen is the number of bytes of additional data that TLS passes to |
| 638 | // AEADs. |
| 639 | static const size_t kTLSADLen = 13; |
| 640 | // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs. |
| 641 | // These are AEADs that weren't originally defined as AEADs, but which we use |
| 642 | // via the AEAD interface. In order for that to work, they have some TLS |
| 643 | // knowledge in them and construct a couple of the AD bytes internally. |
| 644 | static const size_t kLegacyADLen = kTLSADLen - 2; |
| 645 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 646 | if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) || |
| 647 | !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) || |
David Benjamin | 8ffab72 | 2015-11-30 18:48:18 -0500 | [diff] [blame] | 648 | !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen, |
| 649 | selected) || |
David Benjamin | df57163 | 2015-12-07 19:48:16 -0500 | [diff] [blame] | 650 | !SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1", |
| 651 | kLegacyADLen, selected) || |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 652 | !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", |
| 653 | kLegacyADLen, selected) || |
| 654 | !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", |
| 655 | kLegacyADLen, selected) || |
Adam Langley | df447ba | 2016-12-01 08:24:24 -0800 | [diff] [blame] | 656 | #if !defined(OPENSSL_SMALL) |
| 657 | !SpeedAEAD(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen, |
| 658 | selected) || |
| 659 | !SpeedAEAD(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen, |
| 660 | selected) || |
Adam Langley | ba9557d | 2017-02-27 13:53:23 -0800 | [diff] [blame] | 661 | !SpeedAEADOpen(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen, |
| 662 | selected) || |
| 663 | !SpeedAEADOpen(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen, |
| 664 | selected) || |
Adam Langley | df447ba | 2016-12-01 08:24:24 -0800 | [diff] [blame] | 665 | #endif |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 666 | !SpeedHash(EVP_sha1(), "SHA-1", selected) || |
| 667 | !SpeedHash(EVP_sha256(), "SHA-256", selected) || |
| 668 | !SpeedHash(EVP_sha512(), "SHA-512", selected) || |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 669 | !SpeedRandom(selected) || |
| 670 | !SpeedECDH(selected) || |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 671 | !SpeedECDSA(selected) || |
Arnar Birgisson | f27459e | 2016-02-09 18:09:00 -0800 | [diff] [blame] | 672 | !Speed25519(selected) || |
Matthew Braithwaite | f440e82 | 2016-12-09 16:41:07 -0800 | [diff] [blame] | 673 | !SpeedSPAKE2(selected)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 677 | return true; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 678 | } |