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> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 21 | #include <string.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 22 | |
Adam Langley | 92b6b02 | 2015-04-16 14:01:33 -0700 | [diff] [blame] | 23 | #include <openssl/aead.h> |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 24 | #include <openssl/curve25519.h> |
Adam Langley | 92b6b02 | 2015-04-16 14:01:33 -0700 | [diff] [blame] | 25 | #include <openssl/digest.h> |
| 26 | #include <openssl/err.h> |
| 27 | #include <openssl/obj.h> |
| 28 | #include <openssl/rand.h> |
| 29 | #include <openssl/rsa.h> |
| 30 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 31 | #if defined(OPENSSL_WINDOWS) |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 32 | #pragma warning(push, 3) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 33 | #include <windows.h> |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 34 | #pragma warning(pop) |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 35 | #elif defined(OPENSSL_APPLE) |
| 36 | #include <sys/time.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 39 | #include "../crypto/test/scoped_types.h" |
David Benjamin | 58084af | 2015-06-07 00:25:15 -0400 | [diff] [blame] | 40 | #include "internal.h" |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 41 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 42 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 43 | // TimeResults represents the results of benchmarking a function. |
| 44 | struct TimeResults { |
| 45 | // num_calls is the number of function calls done in the time period. |
| 46 | unsigned num_calls; |
| 47 | // us is the number of microseconds that elapsed in the time period. |
| 48 | unsigned us; |
| 49 | |
| 50 | void Print(const std::string &description) { |
| 51 | printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls, |
| 52 | description.c_str(), us, |
| 53 | (static_cast<double>(num_calls) / us) * 1000000); |
| 54 | } |
| 55 | |
| 56 | void PrintWithBytes(const std::string &description, size_t bytes_per_call) { |
| 57 | printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n", |
| 58 | num_calls, description.c_str(), us, |
| 59 | (static_cast<double>(num_calls) / us) * 1000000, |
| 60 | static_cast<double>(bytes_per_call * num_calls) / us); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | #if defined(OPENSSL_WINDOWS) |
| 65 | static uint64_t time_now() { return GetTickCount64() * 1000; } |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 66 | #elif defined(OPENSSL_APPLE) |
| 67 | static uint64_t time_now() { |
| 68 | struct timeval tv; |
| 69 | uint64_t ret; |
| 70 | |
| 71 | gettimeofday(&tv, NULL); |
| 72 | ret = tv.tv_sec; |
| 73 | ret *= 1000000; |
| 74 | ret += tv.tv_usec; |
| 75 | return ret; |
| 76 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | #else |
| 78 | static uint64_t time_now() { |
| 79 | struct timespec ts; |
| 80 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 81 | |
| 82 | uint64_t ret = ts.tv_sec; |
| 83 | ret *= 1000000; |
| 84 | ret += ts.tv_nsec / 1000; |
| 85 | return ret; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | static bool TimeFunction(TimeResults *results, std::function<bool()> func) { |
| 90 | // kTotalMS is the total amount of time that we'll aim to measure a function |
| 91 | // for. |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 92 | static const uint64_t kTotalUS = 1000000; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 93 | uint64_t start = time_now(), now, delta; |
| 94 | unsigned done = 0, iterations_between_time_checks; |
| 95 | |
| 96 | if (!func()) { |
| 97 | return false; |
| 98 | } |
| 99 | now = time_now(); |
| 100 | delta = now - start; |
| 101 | if (delta == 0) { |
| 102 | iterations_between_time_checks = 250; |
| 103 | } else { |
| 104 | // Aim for about 100ms between time checks. |
| 105 | iterations_between_time_checks = |
| 106 | static_cast<double>(100000) / static_cast<double>(delta); |
| 107 | if (iterations_between_time_checks > 1000) { |
| 108 | iterations_between_time_checks = 1000; |
| 109 | } else if (iterations_between_time_checks < 1) { |
| 110 | iterations_between_time_checks = 1; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | for (;;) { |
| 115 | for (unsigned i = 0; i < iterations_between_time_checks; i++) { |
| 116 | if (!func()) { |
| 117 | return false; |
| 118 | } |
| 119 | done++; |
| 120 | } |
| 121 | |
| 122 | now = time_now(); |
| 123 | if (now - start > kTotalUS) { |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | results->us = now - start; |
| 129 | results->num_calls = done; |
| 130 | return true; |
| 131 | } |
| 132 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 133 | static bool SpeedRSA(const std::string &key_name, RSA *key, |
| 134 | const std::string &selected) { |
| 135 | if (!selected.empty() && key_name.find(selected) == std::string::npos) { |
| 136 | return true; |
| 137 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | |
| 139 | std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]); |
| 140 | const uint8_t fake_sha256_hash[32] = {0}; |
| 141 | unsigned sig_len; |
| 142 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 143 | TimeResults results; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | if (!TimeFunction(&results, |
| 145 | [key, &sig, &fake_sha256_hash, &sig_len]() -> bool { |
| 146 | return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash), |
| 147 | sig.get(), &sig_len, key); |
| 148 | })) { |
| 149 | fprintf(stderr, "RSA_sign failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 150 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 151 | return false; |
| 152 | } |
| 153 | results.Print(key_name + " signing"); |
| 154 | |
| 155 | if (!TimeFunction(&results, |
| 156 | [key, &fake_sha256_hash, &sig, sig_len]() -> bool { |
| 157 | return RSA_verify(NID_sha256, fake_sha256_hash, |
| 158 | sizeof(fake_sha256_hash), sig.get(), sig_len, key); |
| 159 | })) { |
| 160 | fprintf(stderr, "RSA_verify failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 161 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | return false; |
| 163 | } |
| 164 | results.Print(key_name + " verify"); |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 169 | static uint8_t *align(uint8_t *in, unsigned alignment) { |
| 170 | return reinterpret_cast<uint8_t *>( |
Brian Smith | d53b2c3 | 2015-03-17 00:37:06 -1000 | [diff] [blame] | 171 | (reinterpret_cast<uintptr_t>(in) + alignment) & |
| 172 | ~static_cast<size_t>(alignment - 1)); |
David Benjamin | 384673c | 2015-01-21 15:56:14 -0500 | [diff] [blame] | 173 | } |
Adam Langley | 543d006 | 2015-01-15 16:05:41 -0800 | [diff] [blame] | 174 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name, |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 176 | size_t chunk_len, size_t ad_len) { |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 177 | static const unsigned kAlignment = 16; |
| 178 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | EVP_AEAD_CTX ctx; |
| 180 | const size_t key_len = EVP_AEAD_key_length(aead); |
| 181 | const size_t nonce_len = EVP_AEAD_nonce_length(aead); |
| 182 | const size_t overhead_len = EVP_AEAD_max_overhead(aead); |
| 183 | |
| 184 | std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]); |
| 185 | memset(key.get(), 0, key_len); |
| 186 | std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]); |
| 187 | memset(nonce.get(), 0, nonce_len); |
Brian Smith | 1d1562d | 2015-03-17 00:32:20 -1000 | [diff] [blame] | 188 | std::unique_ptr<uint8_t[]> in_storage(new uint8_t[chunk_len + kAlignment]); |
| 189 | std::unique_ptr<uint8_t[]> out_storage(new uint8_t[chunk_len + overhead_len + kAlignment]); |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 190 | std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]); |
| 191 | memset(ad.get(), 0, ad_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 193 | uint8_t *const in = align(in_storage.get(), kAlignment); |
| 194 | memset(in, 0, chunk_len); |
| 195 | uint8_t *const out = align(out_storage.get(), kAlignment); |
| 196 | memset(out, 0, chunk_len + overhead_len); |
| 197 | |
David Benjamin | d434f28 | 2015-03-17 18:28:37 -0400 | [diff] [blame] | 198 | if (!EVP_AEAD_CTX_init_with_direction(&ctx, aead, key.get(), key_len, |
| 199 | EVP_AEAD_DEFAULT_TAG_LENGTH, |
| 200 | evp_aead_seal)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 202 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
| 206 | TimeResults results; |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 207 | if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, in, |
| 208 | out, &ctx, &nonce, &ad]() -> bool { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | size_t out_len; |
| 210 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 211 | return EVP_AEAD_CTX_seal( |
Adam Langley | 2672534 | 2015-01-28 15:52:57 -0800 | [diff] [blame] | 212 | &ctx, out, &out_len, chunk_len + overhead_len, nonce.get(), |
| 213 | nonce_len, in, chunk_len, ad.get(), ad_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | })) { |
| 215 | fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 216 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | results.PrintWithBytes(name + " seal", chunk_len); |
| 221 | |
| 222 | EVP_AEAD_CTX_cleanup(&ctx); |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 227 | static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name, |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 228 | size_t ad_len, const std::string &selected) { |
| 229 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 230 | return true; |
| 231 | } |
| 232 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 233 | return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len) && |
| 234 | SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len) && |
| 235 | SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | static bool SpeedHashChunk(const EVP_MD *md, const std::string &name, |
| 239 | size_t chunk_len) { |
| 240 | EVP_MD_CTX *ctx = EVP_MD_CTX_create(); |
| 241 | uint8_t scratch[8192]; |
| 242 | |
| 243 | if (chunk_len > sizeof(scratch)) { |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | TimeResults results; |
| 248 | if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool { |
| 249 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 250 | unsigned int md_len; |
| 251 | |
| 252 | return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) && |
| 253 | EVP_DigestUpdate(ctx, scratch, chunk_len) && |
| 254 | EVP_DigestFinal_ex(ctx, digest, &md_len); |
| 255 | })) { |
| 256 | fprintf(stderr, "EVP_DigestInit_ex failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 257 | ERR_print_errors_fp(stderr); |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
| 261 | results.PrintWithBytes(name, chunk_len); |
| 262 | |
| 263 | EVP_MD_CTX_destroy(ctx); |
| 264 | |
| 265 | return true; |
| 266 | } |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 267 | static bool SpeedHash(const EVP_MD *md, const std::string &name, |
| 268 | const std::string &selected) { |
| 269 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 270 | return true; |
| 271 | } |
| 272 | |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | return SpeedHashChunk(md, name + " (16 bytes)", 16) && |
| 274 | SpeedHashChunk(md, name + " (256 bytes)", 256) && |
| 275 | SpeedHashChunk(md, name + " (8192 bytes)", 8192); |
| 276 | } |
| 277 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 278 | static bool SpeedRandomChunk(const std::string name, size_t chunk_len) { |
| 279 | uint8_t scratch[8192]; |
| 280 | |
| 281 | if (chunk_len > sizeof(scratch)) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | TimeResults results; |
| 286 | if (!TimeFunction(&results, [chunk_len, &scratch]() -> bool { |
| 287 | RAND_bytes(scratch, chunk_len); |
| 288 | return true; |
| 289 | })) { |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | results.PrintWithBytes(name, chunk_len); |
| 294 | return true; |
| 295 | } |
| 296 | |
| 297 | static bool SpeedRandom(const std::string &selected) { |
| 298 | if (!selected.empty() && selected != "RNG") { |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | return SpeedRandomChunk("RNG (16 bytes)", 16) && |
| 303 | SpeedRandomChunk("RNG (256 bytes)", 256) && |
| 304 | SpeedRandomChunk("RNG (8192 bytes)", 8192); |
| 305 | } |
| 306 | |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 307 | static bool SpeedECDHCurve(const std::string &name, int nid, |
| 308 | const std::string &selected) { |
| 309 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | TimeResults results; |
| 314 | if (!TimeFunction(&results, [nid]() -> bool { |
| 315 | ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid)); |
| 316 | if (!key || |
| 317 | !EC_KEY_generate_key(key.get())) { |
| 318 | return false; |
| 319 | } |
| 320 | const EC_GROUP *const group = EC_KEY_get0_group(key.get()); |
| 321 | ScopedEC_POINT point(EC_POINT_new(group)); |
| 322 | ScopedBN_CTX ctx(BN_CTX_new()); |
| 323 | |
| 324 | ScopedBIGNUM x(BN_new()); |
| 325 | ScopedBIGNUM y(BN_new()); |
| 326 | |
| 327 | if (!point || !ctx || !x || !y || |
| 328 | !EC_POINT_mul(group, point.get(), NULL, |
| 329 | EC_KEY_get0_public_key(key.get()), |
| 330 | EC_KEY_get0_private_key(key.get()), ctx.get()) || |
| 331 | !EC_POINT_get_affine_coordinates_GFp(group, point.get(), x.get(), |
| 332 | y.get(), ctx.get())) { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | return true; |
| 337 | })) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | results.Print(name); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | static bool SpeedECDSACurve(const std::string &name, int nid, |
| 346 | const std::string &selected) { |
| 347 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid)); |
| 352 | if (!key || |
| 353 | !EC_KEY_generate_key(key.get())) { |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | uint8_t signature[256]; |
| 358 | if (ECDSA_size(key.get()) > sizeof(signature)) { |
| 359 | return false; |
| 360 | } |
| 361 | uint8_t digest[20]; |
| 362 | memset(digest, 42, sizeof(digest)); |
| 363 | unsigned sig_len; |
| 364 | |
| 365 | TimeResults results; |
| 366 | if (!TimeFunction(&results, [&key, &signature, &digest, &sig_len]() -> bool { |
| 367 | return ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len, |
| 368 | key.get()) == 1; |
| 369 | })) { |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | results.Print(name + " signing"); |
| 374 | |
| 375 | if (!TimeFunction(&results, [&key, &signature, &digest, sig_len]() -> bool { |
| 376 | return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len, |
| 377 | key.get()) == 1; |
| 378 | })) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | results.Print(name + " verify"); |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | static bool SpeedECDH(const std::string &selected) { |
| 388 | return SpeedECDHCurve("ECDH P-224", NID_secp224r1, selected) && |
| 389 | SpeedECDHCurve("ECDH P-256", NID_X9_62_prime256v1, selected) && |
| 390 | SpeedECDHCurve("ECDH P-384", NID_secp384r1, selected) && |
| 391 | SpeedECDHCurve("ECDH P-521", NID_secp521r1, selected); |
| 392 | } |
| 393 | |
| 394 | static bool SpeedECDSA(const std::string &selected) { |
| 395 | return SpeedECDSACurve("ECDSA P-224", NID_secp224r1, selected) && |
| 396 | SpeedECDSACurve("ECDSA P-256", NID_X9_62_prime256v1, selected) && |
| 397 | SpeedECDSACurve("ECDSA P-384", NID_secp384r1, selected) && |
| 398 | SpeedECDSACurve("ECDSA P-521", NID_secp521r1, selected); |
| 399 | } |
| 400 | |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 401 | static bool Speed25519(const std::string &selected) { |
| 402 | if (!selected.empty() && selected.find("25519") == std::string::npos) { |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | TimeResults results; |
| 407 | |
| 408 | #if !defined(OPENSSL_SMALL) |
| 409 | uint8_t public_key[32], private_key[64]; |
| 410 | |
| 411 | if (!TimeFunction(&results, [&public_key, &private_key]() -> bool { |
| 412 | ED25519_keypair(public_key, private_key); |
| 413 | return true; |
| 414 | })) { |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | results.Print("Ed25519 key generation"); |
| 419 | |
| 420 | static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5}; |
| 421 | uint8_t signature[64]; |
| 422 | |
| 423 | if (!TimeFunction(&results, [&private_key, &signature]() -> bool { |
| 424 | return ED25519_sign(signature, kMessage, sizeof(kMessage), |
| 425 | private_key) == 1; |
| 426 | })) { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | results.Print("Ed25519 signing"); |
| 431 | |
| 432 | if (!TimeFunction(&results, [&public_key, &signature]() -> bool { |
| 433 | return ED25519_verify(kMessage, sizeof(kMessage), signature, |
| 434 | public_key) == 1; |
| 435 | })) { |
| 436 | fprintf(stderr, "Ed25519 verify failed.\n"); |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | results.Print("Ed25519 verify"); |
| 441 | #endif |
| 442 | |
| 443 | if (!TimeFunction(&results, []() -> bool { |
| 444 | uint8_t out[32], in[32]; |
| 445 | memset(in, 0, sizeof(in)); |
| 446 | X25519_public_from_private(out, in); |
| 447 | return true; |
| 448 | })) { |
| 449 | fprintf(stderr, "Curve25519 base-point multiplication failed.\n"); |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | results.Print("Curve25519 base-point multiplication"); |
| 454 | |
| 455 | if (!TimeFunction(&results, []() -> bool { |
| 456 | uint8_t out[32], in1[32], in2[32]; |
| 457 | memset(in1, 0, sizeof(in1)); |
| 458 | memset(in2, 0, sizeof(in2)); |
Adam Langley | 3ac32b1 | 2015-11-17 15:15:05 -0800 | [diff] [blame] | 459 | in1[0] = 1; |
| 460 | in2[0] = 9; |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 461 | return X25519(out, in1, in2) == 1; |
| 462 | })) { |
| 463 | fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n"); |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | results.Print("Curve25519 arbitrary point multiplication"); |
| 468 | |
| 469 | return true; |
| 470 | } |
| 471 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 472 | bool Speed(const std::vector<std::string> &args) { |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 473 | std::string selected; |
| 474 | if (args.size() > 1) { |
| 475 | fprintf(stderr, "Usage: bssl speed [speed test selector, i.e. 'RNG']\n"); |
| 476 | return false; |
| 477 | } |
| 478 | if (args.size() > 0) { |
| 479 | selected = args[0]; |
| 480 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
David Benjamin | 74f7110 | 2015-06-27 14:56:25 -0400 | [diff] [blame] | 482 | RSA *key = RSA_private_key_from_bytes(kDERRSAPrivate2048, |
| 483 | kDERRSAPrivate2048Len); |
| 484 | if (key == NULL) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 485 | fprintf(stderr, "Failed to parse RSA key.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 486 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 490 | if (!SpeedRSA("RSA 2048", key, selected)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
| 494 | RSA_free(key); |
David Benjamin | 74f7110 | 2015-06-27 14:56:25 -0400 | [diff] [blame] | 495 | key = RSA_private_key_from_bytes(kDERRSAPrivate3Prime2048, |
| 496 | kDERRSAPrivate3Prime2048Len); |
| 497 | if (key == NULL) { |
Adam Langley | 839b881 | 2015-05-26 11:36:46 -0700 | [diff] [blame] | 498 | fprintf(stderr, "Failed to parse RSA key.\n"); |
| 499 | ERR_print_errors_fp(stderr); |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | if (!SpeedRSA("RSA 2048 (3 prime, e=3)", key, selected)) { |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | RSA_free(key); |
David Benjamin | 74f7110 | 2015-06-27 14:56:25 -0400 | [diff] [blame] | 508 | key = RSA_private_key_from_bytes(kDERRSAPrivate4096, |
| 509 | kDERRSAPrivate4096Len); |
| 510 | if (key == NULL) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 511 | fprintf(stderr, "Failed to parse 4096-bit RSA key.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame] | 512 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 513 | return 1; |
| 514 | } |
| 515 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 516 | if (!SpeedRSA("RSA 4096", key, selected)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | |
| 520 | RSA_free(key); |
| 521 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 522 | // kTLSADLen is the number of bytes of additional data that TLS passes to |
| 523 | // AEADs. |
| 524 | static const size_t kTLSADLen = 13; |
| 525 | // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs. |
| 526 | // These are AEADs that weren't originally defined as AEADs, but which we use |
| 527 | // via the AEAD interface. In order for that to work, they have some TLS |
| 528 | // knowledge in them and construct a couple of the AD bytes internally. |
| 529 | static const size_t kLegacyADLen = kTLSADLen - 2; |
| 530 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 531 | if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) || |
| 532 | !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) || |
David Benjamin | 8ffab72 | 2015-11-30 18:48:18 -0500 | [diff] [blame] | 533 | !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen, |
| 534 | selected) || |
Brian Smith | 3e23e4c | 2015-10-03 11:38:58 -1000 | [diff] [blame] | 535 | !SpeedAEAD(EVP_aead_chacha20_poly1305_old(), "ChaCha20-Poly1305-Old", |
| 536 | kTLSADLen, selected) || |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 537 | !SpeedAEAD(EVP_aead_rc4_md5_tls(), "RC4-MD5", kLegacyADLen, selected) || |
David Benjamin | df57163 | 2015-12-07 19:48:16 -0500 | [diff] [blame^] | 538 | !SpeedAEAD(EVP_aead_rc4_sha1_tls(), "RC4-SHA1", kLegacyADLen, selected) || |
| 539 | !SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1", |
| 540 | kLegacyADLen, selected) || |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 541 | !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", |
| 542 | kLegacyADLen, selected) || |
| 543 | !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", |
| 544 | kLegacyADLen, selected) || |
| 545 | !SpeedHash(EVP_sha1(), "SHA-1", selected) || |
| 546 | !SpeedHash(EVP_sha256(), "SHA-256", selected) || |
| 547 | !SpeedHash(EVP_sha512(), "SHA-512", selected) || |
Adam Langley | ad6b28e | 2015-04-14 12:07:44 -0700 | [diff] [blame] | 548 | !SpeedRandom(selected) || |
| 549 | !SpeedECDH(selected) || |
Adam Langley | 4fb0dc4 | 2015-11-13 13:09:47 -0800 | [diff] [blame] | 550 | !SpeedECDSA(selected) || |
| 551 | !Speed25519(selected)) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 552 | return false; |
| 553 | } |
| 554 | |
Adam Langley | 90b5840 | 2015-04-13 11:04:18 -0700 | [diff] [blame] | 555 | return true; |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 556 | } |