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 | #include <time.h> |
| 23 | |
| 24 | #include <openssl/aead.h> |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 25 | #include <openssl/digest.h> |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame^] | 26 | #include <openssl/err.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 27 | #include <openssl/obj.h> |
| 28 | #include <openssl/rsa.h> |
| 29 | |
| 30 | #if defined(OPENSSL_WINDOWS) |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 31 | #pragma warning(push, 3) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 32 | #include <windows.h> |
Brian Smith | efed221 | 2015-01-28 16:20:02 -0800 | [diff] [blame] | 33 | #pragma warning(pop) |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 34 | #elif defined(OPENSSL_APPLE) |
| 35 | #include <sys/time.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 36 | #endif |
| 37 | |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 38 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 39 | extern "C" { |
| 40 | // These values are DER encoded, RSA private keys. |
| 41 | extern const uint8_t kDERRSAPrivate2048[]; |
| 42 | extern size_t kDERRSAPrivate2048Len; |
| 43 | extern const uint8_t kDERRSAPrivate4096[]; |
| 44 | extern size_t kDERRSAPrivate4096Len; |
| 45 | } |
| 46 | |
| 47 | // TimeResults represents the results of benchmarking a function. |
| 48 | struct TimeResults { |
| 49 | // num_calls is the number of function calls done in the time period. |
| 50 | unsigned num_calls; |
| 51 | // us is the number of microseconds that elapsed in the time period. |
| 52 | unsigned us; |
| 53 | |
| 54 | void Print(const std::string &description) { |
| 55 | printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls, |
| 56 | description.c_str(), us, |
| 57 | (static_cast<double>(num_calls) / us) * 1000000); |
| 58 | } |
| 59 | |
| 60 | void PrintWithBytes(const std::string &description, size_t bytes_per_call) { |
| 61 | printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n", |
| 62 | num_calls, description.c_str(), us, |
| 63 | (static_cast<double>(num_calls) / us) * 1000000, |
| 64 | static_cast<double>(bytes_per_call * num_calls) / us); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | #if defined(OPENSSL_WINDOWS) |
| 69 | static uint64_t time_now() { return GetTickCount64() * 1000; } |
Adam Langley | 30eda1d | 2014-06-24 11:15:12 -0700 | [diff] [blame] | 70 | #elif defined(OPENSSL_APPLE) |
| 71 | static uint64_t time_now() { |
| 72 | struct timeval tv; |
| 73 | uint64_t ret; |
| 74 | |
| 75 | gettimeofday(&tv, NULL); |
| 76 | ret = tv.tv_sec; |
| 77 | ret *= 1000000; |
| 78 | ret += tv.tv_usec; |
| 79 | return ret; |
| 80 | } |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 81 | #else |
| 82 | static uint64_t time_now() { |
| 83 | struct timespec ts; |
| 84 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 85 | |
| 86 | uint64_t ret = ts.tv_sec; |
| 87 | ret *= 1000000; |
| 88 | ret += ts.tv_nsec / 1000; |
| 89 | return ret; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | static bool TimeFunction(TimeResults *results, std::function<bool()> func) { |
| 94 | // kTotalMS is the total amount of time that we'll aim to measure a function |
| 95 | // for. |
| 96 | static const uint64_t kTotalUS = 3000000; |
| 97 | uint64_t start = time_now(), now, delta; |
| 98 | unsigned done = 0, iterations_between_time_checks; |
| 99 | |
| 100 | if (!func()) { |
| 101 | return false; |
| 102 | } |
| 103 | now = time_now(); |
| 104 | delta = now - start; |
| 105 | if (delta == 0) { |
| 106 | iterations_between_time_checks = 250; |
| 107 | } else { |
| 108 | // Aim for about 100ms between time checks. |
| 109 | iterations_between_time_checks = |
| 110 | static_cast<double>(100000) / static_cast<double>(delta); |
| 111 | if (iterations_between_time_checks > 1000) { |
| 112 | iterations_between_time_checks = 1000; |
| 113 | } else if (iterations_between_time_checks < 1) { |
| 114 | iterations_between_time_checks = 1; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (;;) { |
| 119 | for (unsigned i = 0; i < iterations_between_time_checks; i++) { |
| 120 | if (!func()) { |
| 121 | return false; |
| 122 | } |
| 123 | done++; |
| 124 | } |
| 125 | |
| 126 | now = time_now(); |
| 127 | if (now - start > kTotalUS) { |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | results->us = now - start; |
| 133 | results->num_calls = done; |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | static bool SpeedRSA(const std::string& key_name, RSA *key) { |
| 138 | TimeResults results; |
| 139 | |
| 140 | std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]); |
| 141 | const uint8_t fake_sha256_hash[32] = {0}; |
| 142 | unsigned sig_len; |
| 143 | |
| 144 | if (!TimeFunction(&results, |
| 145 | [key, &sig, &fake_sha256_hash, &sig_len]() -> bool { |
| 146 | return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash), |
| 147 | sig.get(), &sig_len, key); |
| 148 | })) { |
| 149 | fprintf(stderr, "RSA_sign failed.\n"); |
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, |
| 228 | size_t ad_len) { |
| 229 | return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len) && |
| 230 | SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len) && |
| 231 | SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | static bool SpeedHashChunk(const EVP_MD *md, const std::string &name, |
| 235 | size_t chunk_len) { |
| 236 | EVP_MD_CTX *ctx = EVP_MD_CTX_create(); |
| 237 | uint8_t scratch[8192]; |
| 238 | |
| 239 | if (chunk_len > sizeof(scratch)) { |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | TimeResults results; |
| 244 | if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool { |
| 245 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 246 | unsigned int md_len; |
| 247 | |
| 248 | return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) && |
| 249 | EVP_DigestUpdate(ctx, scratch, chunk_len) && |
| 250 | EVP_DigestFinal_ex(ctx, digest, &md_len); |
| 251 | })) { |
| 252 | fprintf(stderr, "EVP_DigestInit_ex failed.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame^] | 253 | ERR_print_errors_fp(stderr); |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | |
| 257 | results.PrintWithBytes(name, chunk_len); |
| 258 | |
| 259 | EVP_MD_CTX_destroy(ctx); |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | static bool SpeedHash(const EVP_MD *md, const std::string &name) { |
| 264 | return SpeedHashChunk(md, name + " (16 bytes)", 16) && |
| 265 | SpeedHashChunk(md, name + " (256 bytes)", 256) && |
| 266 | SpeedHashChunk(md, name + " (8192 bytes)", 8192); |
| 267 | } |
| 268 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | bool Speed(const std::vector<std::string> &args) { |
| 270 | const uint8_t *inp; |
| 271 | |
| 272 | RSA *key = NULL; |
| 273 | inp = kDERRSAPrivate2048; |
| 274 | if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate2048Len)) { |
| 275 | fprintf(stderr, "Failed to parse RSA key.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame^] | 276 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | if (!SpeedRSA("RSA 2048", key)) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | RSA_free(key); |
| 285 | key = NULL; |
| 286 | |
| 287 | inp = kDERRSAPrivate4096; |
| 288 | if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate4096Len)) { |
| 289 | fprintf(stderr, "Failed to parse 4096-bit RSA key.\n"); |
Brian Smith | 83a8298 | 2015-04-09 16:21:10 -1000 | [diff] [blame^] | 290 | ERR_print_errors_fp(stderr); |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | if (!SpeedRSA("RSA 4096", key)) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | RSA_free(key); |
| 299 | |
Adam Langley | e762434 | 2015-01-15 17:33:48 -0800 | [diff] [blame] | 300 | // kTLSADLen is the number of bytes of additional data that TLS passes to |
| 301 | // AEADs. |
| 302 | static const size_t kTLSADLen = 13; |
| 303 | // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs. |
| 304 | // These are AEADs that weren't originally defined as AEADs, but which we use |
| 305 | // via the AEAD interface. In order for that to work, they have some TLS |
| 306 | // knowledge in them and construct a couple of the AD bytes internally. |
| 307 | static const size_t kLegacyADLen = kTLSADLen - 2; |
| 308 | |
| 309 | if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen) || |
| 310 | !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen) || |
| 311 | !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen) || |
| 312 | !SpeedAEAD(EVP_aead_rc4_md5_tls(), "RC4-MD5", kLegacyADLen) || |
| 313 | !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", kLegacyADLen) || |
| 314 | !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", kLegacyADLen) || |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | !SpeedHash(EVP_sha1(), "SHA-1") || |
| 316 | !SpeedHash(EVP_sha256(), "SHA-256") || |
| 317 | !SpeedHash(EVP_sha512(), "SHA-512")) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |