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> |
| 21 | #include <time.h> |
| 22 | |
| 23 | #include <openssl/aead.h> |
| 24 | #include <openssl/bio.h> |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 25 | #include <openssl/digest.h> |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 26 | #include <openssl/obj.h> |
| 27 | #include <openssl/rsa.h> |
| 28 | |
| 29 | #if defined(OPENSSL_WINDOWS) |
| 30 | #include <Windows.h> |
| 31 | #endif |
| 32 | |
| 33 | extern "C" { |
| 34 | // These values are DER encoded, RSA private keys. |
| 35 | extern const uint8_t kDERRSAPrivate2048[]; |
| 36 | extern size_t kDERRSAPrivate2048Len; |
| 37 | extern const uint8_t kDERRSAPrivate4096[]; |
| 38 | extern size_t kDERRSAPrivate4096Len; |
| 39 | } |
| 40 | |
| 41 | // TimeResults represents the results of benchmarking a function. |
| 42 | struct TimeResults { |
| 43 | // num_calls is the number of function calls done in the time period. |
| 44 | unsigned num_calls; |
| 45 | // us is the number of microseconds that elapsed in the time period. |
| 46 | unsigned us; |
| 47 | |
| 48 | void Print(const std::string &description) { |
| 49 | printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls, |
| 50 | description.c_str(), us, |
| 51 | (static_cast<double>(num_calls) / us) * 1000000); |
| 52 | } |
| 53 | |
| 54 | void PrintWithBytes(const std::string &description, size_t bytes_per_call) { |
| 55 | printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n", |
| 56 | num_calls, description.c_str(), us, |
| 57 | (static_cast<double>(num_calls) / us) * 1000000, |
| 58 | static_cast<double>(bytes_per_call * num_calls) / us); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | #if defined(OPENSSL_WINDOWS) |
| 63 | static uint64_t time_now() { return GetTickCount64() * 1000; } |
| 64 | #else |
| 65 | static uint64_t time_now() { |
| 66 | struct timespec ts; |
| 67 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 68 | |
| 69 | uint64_t ret = ts.tv_sec; |
| 70 | ret *= 1000000; |
| 71 | ret += ts.tv_nsec / 1000; |
| 72 | return ret; |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | static bool TimeFunction(TimeResults *results, std::function<bool()> func) { |
| 77 | // kTotalMS is the total amount of time that we'll aim to measure a function |
| 78 | // for. |
| 79 | static const uint64_t kTotalUS = 3000000; |
| 80 | uint64_t start = time_now(), now, delta; |
| 81 | unsigned done = 0, iterations_between_time_checks; |
| 82 | |
| 83 | if (!func()) { |
| 84 | return false; |
| 85 | } |
| 86 | now = time_now(); |
| 87 | delta = now - start; |
| 88 | if (delta == 0) { |
| 89 | iterations_between_time_checks = 250; |
| 90 | } else { |
| 91 | // Aim for about 100ms between time checks. |
| 92 | iterations_between_time_checks = |
| 93 | static_cast<double>(100000) / static_cast<double>(delta); |
| 94 | if (iterations_between_time_checks > 1000) { |
| 95 | iterations_between_time_checks = 1000; |
| 96 | } else if (iterations_between_time_checks < 1) { |
| 97 | iterations_between_time_checks = 1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | for (;;) { |
| 102 | for (unsigned i = 0; i < iterations_between_time_checks; i++) { |
| 103 | if (!func()) { |
| 104 | return false; |
| 105 | } |
| 106 | done++; |
| 107 | } |
| 108 | |
| 109 | now = time_now(); |
| 110 | if (now - start > kTotalUS) { |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | results->us = now - start; |
| 116 | results->num_calls = done; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | static bool SpeedRSA(const std::string& key_name, RSA *key) { |
| 121 | TimeResults results; |
| 122 | |
| 123 | std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]); |
| 124 | const uint8_t fake_sha256_hash[32] = {0}; |
| 125 | unsigned sig_len; |
| 126 | |
| 127 | if (!TimeFunction(&results, |
| 128 | [key, &sig, &fake_sha256_hash, &sig_len]() -> bool { |
| 129 | return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash), |
| 130 | sig.get(), &sig_len, key); |
| 131 | })) { |
| 132 | fprintf(stderr, "RSA_sign failed.\n"); |
| 133 | BIO_print_errors_fp(stderr); |
| 134 | return false; |
| 135 | } |
| 136 | results.Print(key_name + " signing"); |
| 137 | |
| 138 | if (!TimeFunction(&results, |
| 139 | [key, &fake_sha256_hash, &sig, sig_len]() -> bool { |
| 140 | return RSA_verify(NID_sha256, fake_sha256_hash, |
| 141 | sizeof(fake_sha256_hash), sig.get(), sig_len, key); |
| 142 | })) { |
| 143 | fprintf(stderr, "RSA_verify failed.\n"); |
| 144 | BIO_print_errors_fp(stderr); |
| 145 | return false; |
| 146 | } |
| 147 | results.Print(key_name + " verify"); |
| 148 | |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name, |
| 153 | size_t chunk_len) { |
| 154 | EVP_AEAD_CTX ctx; |
| 155 | const size_t key_len = EVP_AEAD_key_length(aead); |
| 156 | const size_t nonce_len = EVP_AEAD_nonce_length(aead); |
| 157 | const size_t overhead_len = EVP_AEAD_max_overhead(aead); |
| 158 | |
| 159 | std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]); |
| 160 | memset(key.get(), 0, key_len); |
| 161 | std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]); |
| 162 | memset(nonce.get(), 0, nonce_len); |
| 163 | std::unique_ptr<uint8_t[]> in(new uint8_t[chunk_len]); |
| 164 | memset(in.get(), 0, chunk_len); |
| 165 | std::unique_ptr<uint8_t[]> out(new uint8_t[chunk_len + overhead_len]); |
| 166 | memset(out.get(), 0, chunk_len + overhead_len); |
| 167 | |
| 168 | if (!EVP_AEAD_CTX_init(&ctx, aead, key.get(), key_len, |
| 169 | EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) { |
| 170 | fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n"); |
| 171 | BIO_print_errors_fp(stderr); |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | TimeResults results; |
| 176 | if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, &in, &out, |
| 177 | &ctx, &nonce]() -> bool { |
| 178 | size_t out_len; |
| 179 | |
| 180 | return EVP_AEAD_CTX_seal(&ctx, out.get(), &out_len, |
| 181 | chunk_len + overhead_len, nonce.get(), |
| 182 | nonce_len, in.get(), chunk_len, NULL, 0); |
| 183 | })) { |
| 184 | fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n"); |
| 185 | BIO_print_errors_fp(stderr); |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | results.PrintWithBytes(name + " seal", chunk_len); |
| 190 | |
| 191 | EVP_AEAD_CTX_cleanup(&ctx); |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name) { |
| 197 | return SpeedAEADChunk(aead, name + " (16 bytes)", 16) && |
| 198 | SpeedAEADChunk(aead, name + " (1350 bytes)", 1350) && |
| 199 | SpeedAEADChunk(aead, name + " (8192 bytes)", 8192); |
| 200 | } |
| 201 | |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 202 | static bool SpeedHashChunk(const EVP_MD *md, const std::string &name, |
| 203 | size_t chunk_len) { |
| 204 | EVP_MD_CTX *ctx = EVP_MD_CTX_create(); |
| 205 | uint8_t scratch[8192]; |
| 206 | |
| 207 | if (chunk_len > sizeof(scratch)) { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | TimeResults results; |
| 212 | if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool { |
| 213 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 214 | unsigned int md_len; |
| 215 | |
| 216 | return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) && |
| 217 | EVP_DigestUpdate(ctx, scratch, chunk_len) && |
| 218 | EVP_DigestFinal_ex(ctx, digest, &md_len); |
| 219 | })) { |
| 220 | fprintf(stderr, "EVP_DigestInit_ex failed.\n"); |
| 221 | BIO_print_errors_fp(stderr); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | results.PrintWithBytes(name, chunk_len); |
| 226 | |
| 227 | EVP_MD_CTX_destroy(ctx); |
| 228 | |
| 229 | return true; |
| 230 | } |
| 231 | static bool SpeedHash(const EVP_MD *md, const std::string &name) { |
| 232 | return SpeedHashChunk(md, name + " (16 bytes)", 16) && |
| 233 | SpeedHashChunk(md, name + " (256 bytes)", 256) && |
| 234 | SpeedHashChunk(md, name + " (8192 bytes)", 8192); |
| 235 | } |
| 236 | |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 237 | bool Speed(const std::vector<std::string> &args) { |
| 238 | const uint8_t *inp; |
| 239 | |
| 240 | RSA *key = NULL; |
| 241 | inp = kDERRSAPrivate2048; |
| 242 | if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate2048Len)) { |
| 243 | fprintf(stderr, "Failed to parse RSA key.\n"); |
| 244 | BIO_print_errors_fp(stderr); |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | if (!SpeedRSA("RSA 2048", key)) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | RSA_free(key); |
| 253 | key = NULL; |
| 254 | |
| 255 | inp = kDERRSAPrivate4096; |
| 256 | if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate4096Len)) { |
| 257 | fprintf(stderr, "Failed to parse 4096-bit RSA key.\n"); |
| 258 | BIO_print_errors_fp(stderr); |
| 259 | return 1; |
| 260 | } |
| 261 | |
| 262 | if (!SpeedRSA("RSA 4096", key)) { |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | RSA_free(key); |
| 267 | |
| 268 | if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM") || |
| 269 | !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM") || |
Adam Langley | 006779a | 2014-06-20 12:00:00 -0700 | [diff] [blame^] | 270 | !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305") || |
| 271 | !SpeedHash(EVP_sha1(), "SHA-1") || |
| 272 | !SpeedHash(EVP_sha256(), "SHA-256") || |
| 273 | !SpeedHash(EVP_sha512(), "SHA-512")) { |
Adam Langley | c5c0c7e | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |