blob: 6bc74bef5cff9c6e09757a8f457b04aff6c39ea2 [file] [log] [blame]
Adam Langleyc5c0c7e2014-06-20 12:00:00 -07001/* 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 Langley006779a2014-06-20 12:00:00 -070025#include <openssl/digest.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070026#include <openssl/obj.h>
27#include <openssl/rsa.h>
28
29#if defined(OPENSSL_WINDOWS)
30#include <Windows.h>
Adam Langley30eda1d2014-06-24 11:15:12 -070031#elif defined(OPENSSL_APPLE)
32#include <sys/time.h>
Adam Langleyc5c0c7e2014-06-20 12:00:00 -070033#endif
34
35extern "C" {
36// These values are DER encoded, RSA private keys.
37extern const uint8_t kDERRSAPrivate2048[];
38extern size_t kDERRSAPrivate2048Len;
39extern const uint8_t kDERRSAPrivate4096[];
40extern size_t kDERRSAPrivate4096Len;
41}
42
43// TimeResults represents the results of benchmarking a function.
44struct 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)
65static uint64_t time_now() { return GetTickCount64() * 1000; }
Adam Langley30eda1d2014-06-24 11:15:12 -070066#elif defined(OPENSSL_APPLE)
67static 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 Langleyc5c0c7e2014-06-20 12:00:00 -070077#else
78static 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
89static 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.
92 static const uint64_t kTotalUS = 3000000;
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
133static bool SpeedRSA(const std::string& key_name, RSA *key) {
134 TimeResults results;
135
136 std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]);
137 const uint8_t fake_sha256_hash[32] = {0};
138 unsigned sig_len;
139
140 if (!TimeFunction(&results,
141 [key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
142 return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
143 sig.get(), &sig_len, key);
144 })) {
145 fprintf(stderr, "RSA_sign failed.\n");
146 BIO_print_errors_fp(stderr);
147 return false;
148 }
149 results.Print(key_name + " signing");
150
151 if (!TimeFunction(&results,
152 [key, &fake_sha256_hash, &sig, sig_len]() -> bool {
153 return RSA_verify(NID_sha256, fake_sha256_hash,
154 sizeof(fake_sha256_hash), sig.get(), sig_len, key);
155 })) {
156 fprintf(stderr, "RSA_verify failed.\n");
157 BIO_print_errors_fp(stderr);
158 return false;
159 }
160 results.Print(key_name + " verify");
161
162 return true;
163}
164
Adam Langley543d0062015-01-15 16:05:41 -0800165template<typename T>
166struct free_functor {
167 void operator()(T* ptr) {
168 free(ptr);
169 }
170};
171
172#if defined(OPENSSL_WINDOWS)
173#define AllocAligned malloc
174#else
175uint8_t *AllocAligned(size_t size) {
176 void *ptr;
177 if (posix_memalign(&ptr, 64, size)) {
178 abort();
179 }
180 return static_cast<uint8_t*>(ptr);
181}
182#endif
183
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700184static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name,
Adam Langleye7624342015-01-15 17:33:48 -0800185 size_t chunk_len, size_t ad_len) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700186 EVP_AEAD_CTX ctx;
187 const size_t key_len = EVP_AEAD_key_length(aead);
188 const size_t nonce_len = EVP_AEAD_nonce_length(aead);
189 const size_t overhead_len = EVP_AEAD_max_overhead(aead);
190
191 std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
192 memset(key.get(), 0, key_len);
193 std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]);
194 memset(nonce.get(), 0, nonce_len);
Adam Langley543d0062015-01-15 16:05:41 -0800195 std::unique_ptr<uint8_t, free_functor<uint8_t>> in(AllocAligned(chunk_len));
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700196 memset(in.get(), 0, chunk_len);
Adam Langley543d0062015-01-15 16:05:41 -0800197 std::unique_ptr<uint8_t, free_functor<uint8_t>> out(
198 AllocAligned(chunk_len + overhead_len));
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700199 memset(out.get(), 0, chunk_len + overhead_len);
Adam Langleye7624342015-01-15 17:33:48 -0800200 std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]);
201 memset(ad.get(), 0, ad_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700202
203 if (!EVP_AEAD_CTX_init(&ctx, aead, key.get(), key_len,
204 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) {
205 fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
206 BIO_print_errors_fp(stderr);
207 return false;
208 }
209
210 TimeResults results;
Adam Langleye7624342015-01-15 17:33:48 -0800211 if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, &in,
212 &out, &ctx, &nonce, &ad]() -> bool {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700213 size_t out_len;
214
Adam Langleye7624342015-01-15 17:33:48 -0800215 return EVP_AEAD_CTX_seal(
216 &ctx, out.get(), &out_len, chunk_len + overhead_len, nonce.get(),
217 nonce_len, in.get(), chunk_len, ad.get(), ad_len);
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700218 })) {
219 fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
220 BIO_print_errors_fp(stderr);
221 return false;
222 }
223
224 results.PrintWithBytes(name + " seal", chunk_len);
225
226 EVP_AEAD_CTX_cleanup(&ctx);
227
228 return true;
229}
230
Adam Langleye7624342015-01-15 17:33:48 -0800231static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name,
232 size_t ad_len) {
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 Langleyc5c0c7e2014-06-20 12:00:00 -0700236}
237
Adam Langley006779a2014-06-20 12:00:00 -0700238static 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");
257 BIO_print_errors_fp(stderr);
258 return false;
259 }
260
261 results.PrintWithBytes(name, chunk_len);
262
263 EVP_MD_CTX_destroy(ctx);
264
265 return true;
266}
267static bool SpeedHash(const EVP_MD *md, const std::string &name) {
268 return SpeedHashChunk(md, name + " (16 bytes)", 16) &&
269 SpeedHashChunk(md, name + " (256 bytes)", 256) &&
270 SpeedHashChunk(md, name + " (8192 bytes)", 8192);
271}
272
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700273bool Speed(const std::vector<std::string> &args) {
274 const uint8_t *inp;
275
276 RSA *key = NULL;
277 inp = kDERRSAPrivate2048;
278 if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate2048Len)) {
279 fprintf(stderr, "Failed to parse RSA key.\n");
280 BIO_print_errors_fp(stderr);
281 return false;
282 }
283
284 if (!SpeedRSA("RSA 2048", key)) {
285 return false;
286 }
287
288 RSA_free(key);
289 key = NULL;
290
291 inp = kDERRSAPrivate4096;
292 if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate4096Len)) {
293 fprintf(stderr, "Failed to parse 4096-bit RSA key.\n");
294 BIO_print_errors_fp(stderr);
295 return 1;
296 }
297
298 if (!SpeedRSA("RSA 4096", key)) {
299 return false;
300 }
301
302 RSA_free(key);
303
Adam Langleye7624342015-01-15 17:33:48 -0800304 // kTLSADLen is the number of bytes of additional data that TLS passes to
305 // AEADs.
306 static const size_t kTLSADLen = 13;
307 // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs.
308 // These are AEADs that weren't originally defined as AEADs, but which we use
309 // via the AEAD interface. In order for that to work, they have some TLS
310 // knowledge in them and construct a couple of the AD bytes internally.
311 static const size_t kLegacyADLen = kTLSADLen - 2;
312
313 if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen) ||
314 !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen) ||
315 !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen) ||
316 !SpeedAEAD(EVP_aead_rc4_md5_tls(), "RC4-MD5", kLegacyADLen) ||
317 !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", kLegacyADLen) ||
318 !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", kLegacyADLen) ||
Adam Langley006779a2014-06-20 12:00:00 -0700319 !SpeedHash(EVP_sha1(), "SHA-1") ||
320 !SpeedHash(EVP_sha256(), "SHA-256") ||
321 !SpeedHash(EVP_sha512(), "SHA-512")) {
Adam Langleyc5c0c7e2014-06-20 12:00:00 -0700322 return false;
323 }
324
325 return 0;
326}