2014-06-20 20:00:00 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
|
|
|
|
Add RSA key generation to speed.cc
On a Skylake machine, the improvements to make RSA key generation
constant-time did slow things down a bit:
Before:
Did 217 RSA 2048 key-gen operations in 30231344us (7.2 ops/sec)
min: 17154us, median: 117284us, max: 518336us
Did 70 RSA 3072 key-gen operations in 30188611us (2.3 ops/sec)
min: 57759us, median: 348873us, max: 1760351us
Did 27 RSA 4096 key-gen operations in 30264235us (0.9 ops/sec)
min: 202096us, median: 980160us, max: 4282915us
After:
Did 186 RSA 2048 key-gen operations in 30021173us (6.2 ops/sec)
min: 74850us, median: 147650us, max: 407031us
Did 54 RSA 3072 key-gen operations in 30111667us (1.8 ops/sec)
min: 292050us, median: 483786us, max: 1294105us
Did 18 RSA 4096 key-gen operations in 30662495us (0.6 ops/sec)
min: 902547us, median: 1446689us, max: 3660302us
Change-Id: I52a96bb41bab759aa7ef6239bdfa533707a9eb3c
Reviewed-on: https://boringssl-review.googlesource.com/26904
Commit-Queue: Adam Langley <alangley@gmail.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-03-30 21:43:59 +01:00
|
|
|
#include <algorithm>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <string>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
Add RSA key generation to speed.cc
On a Skylake machine, the improvements to make RSA key generation
constant-time did slow things down a bit:
Before:
Did 217 RSA 2048 key-gen operations in 30231344us (7.2 ops/sec)
min: 17154us, median: 117284us, max: 518336us
Did 70 RSA 3072 key-gen operations in 30188611us (2.3 ops/sec)
min: 57759us, median: 348873us, max: 1760351us
Did 27 RSA 4096 key-gen operations in 30264235us (0.9 ops/sec)
min: 202096us, median: 980160us, max: 4282915us
After:
Did 186 RSA 2048 key-gen operations in 30021173us (6.2 ops/sec)
min: 74850us, median: 147650us, max: 407031us
Did 54 RSA 3072 key-gen operations in 30111667us (1.8 ops/sec)
min: 292050us, median: 483786us, max: 1294105us
Did 18 RSA 4096 key-gen operations in 30662495us (0.6 ops/sec)
min: 902547us, median: 1446689us, max: 3660302us
Change-Id: I52a96bb41bab759aa7ef6239bdfa533707a9eb3c
Reviewed-on: https://boringssl-review.googlesource.com/26904
Commit-Queue: Adam Langley <alangley@gmail.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-03-30 21:43:59 +01:00
|
|
|
#include <assert.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <stdint.h>
|
2016-08-15 03:06:49 +01:00
|
|
|
#include <stdlib.h>
|
2015-01-31 01:08:37 +00:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-04-16 22:01:33 +01:00
|
|
|
#include <openssl/aead.h>
|
2016-08-18 04:10:28 +01:00
|
|
|
#include <openssl/bn.h>
|
2015-11-13 21:09:47 +00:00
|
|
|
#include <openssl/curve25519.h>
|
2015-04-16 22:01:33 +01:00
|
|
|
#include <openssl/digest.h>
|
|
|
|
#include <openssl/err.h>
|
2016-08-18 04:10:28 +01:00
|
|
|
#include <openssl/ec.h>
|
|
|
|
#include <openssl/ecdsa.h>
|
|
|
|
#include <openssl/ec_key.h>
|
2017-06-10 00:27:37 +01:00
|
|
|
#include <openssl/evp.h>
|
2018-11-12 21:53:42 +00:00
|
|
|
#include <openssl/hrss.h>
|
2016-03-25 22:07:11 +00:00
|
|
|
#include <openssl/nid.h>
|
2015-04-16 22:01:33 +01:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
#if defined(OPENSSL_WINDOWS)
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
2015-03-20 23:32:23 +00:00
|
|
|
#include <windows.h>
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(pop))
|
2014-06-24 19:15:12 +01:00
|
|
|
#elif defined(OPENSSL_APPLE)
|
|
|
|
#include <sys/time.h>
|
2016-09-10 00:49:35 +01:00
|
|
|
#else
|
|
|
|
#include <time.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#endif
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../crypto/internal.h"
|
2015-06-07 05:25:15 +01:00
|
|
|
#include "internal.h"
|
2015-04-14 20:07:44 +01:00
|
|
|
|
2015-01-31 01:08:37 +00:00
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
// TimeResults represents the results of benchmarking a function.
|
|
|
|
struct TimeResults {
|
|
|
|
// num_calls is the number of function calls done in the time period.
|
|
|
|
unsigned num_calls;
|
|
|
|
// us is the number of microseconds that elapsed in the time period.
|
|
|
|
unsigned us;
|
|
|
|
|
|
|
|
void Print(const std::string &description) {
|
|
|
|
printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls,
|
|
|
|
description.c_str(), us,
|
|
|
|
(static_cast<double>(num_calls) / us) * 1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintWithBytes(const std::string &description, size_t bytes_per_call) {
|
|
|
|
printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n",
|
|
|
|
num_calls, description.c_str(), us,
|
|
|
|
(static_cast<double>(num_calls) / us) * 1000000,
|
|
|
|
static_cast<double>(bytes_per_call * num_calls) / us);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
|
|
static uint64_t time_now() { return GetTickCount64() * 1000; }
|
2014-06-24 19:15:12 +01:00
|
|
|
#elif defined(OPENSSL_APPLE)
|
|
|
|
static uint64_t time_now() {
|
|
|
|
struct timeval tv;
|
|
|
|
uint64_t ret;
|
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
ret = tv.tv_sec;
|
|
|
|
ret *= 1000000;
|
|
|
|
ret += tv.tv_usec;
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
#else
|
|
|
|
static uint64_t time_now() {
|
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
|
|
|
|
uint64_t ret = ts.tv_sec;
|
|
|
|
ret *= 1000000;
|
|
|
|
ret += ts.tv_nsec / 1000;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-08-15 03:06:49 +01:00
|
|
|
static uint64_t g_timeout_seconds = 1;
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
|
2016-08-15 03:06:49 +01:00
|
|
|
// total_us is the total amount of time that we'll aim to measure a function
|
2014-06-20 20:00:00 +01:00
|
|
|
// for.
|
2016-08-15 03:06:49 +01:00
|
|
|
const uint64_t total_us = g_timeout_seconds * 1000000;
|
2014-06-20 20:00:00 +01:00
|
|
|
uint64_t start = time_now(), now, delta;
|
|
|
|
unsigned done = 0, iterations_between_time_checks;
|
|
|
|
|
|
|
|
if (!func()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
now = time_now();
|
|
|
|
delta = now - start;
|
|
|
|
if (delta == 0) {
|
|
|
|
iterations_between_time_checks = 250;
|
|
|
|
} else {
|
|
|
|
// Aim for about 100ms between time checks.
|
|
|
|
iterations_between_time_checks =
|
|
|
|
static_cast<double>(100000) / static_cast<double>(delta);
|
|
|
|
if (iterations_between_time_checks > 1000) {
|
|
|
|
iterations_between_time_checks = 1000;
|
|
|
|
} else if (iterations_between_time_checks < 1) {
|
|
|
|
iterations_between_time_checks = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
for (unsigned i = 0; i < iterations_between_time_checks; i++) {
|
|
|
|
if (!func()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
done++;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = time_now();
|
2016-08-15 03:06:49 +01:00
|
|
|
if (now - start > total_us) {
|
2014-06-20 20:00:00 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results->us = now - start;
|
|
|
|
results->num_calls = done;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:09:25 +01:00
|
|
|
static bool SpeedRSA(const std::string &selected) {
|
|
|
|
if (!selected.empty() && selected.find("RSA") == std::string::npos) {
|
2015-04-13 19:04:18 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-04-13 18:09:25 +01:00
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
const uint8_t *key;
|
|
|
|
const size_t key_len;
|
|
|
|
} kRSAKeys[] = {
|
|
|
|
{"RSA 2048", kDERRSAPrivate2048, kDERRSAPrivate2048Len},
|
|
|
|
{"RSA 4096", kDERRSAPrivate4096, kDERRSAPrivate4096Len},
|
|
|
|
};
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-04-13 18:09:25 +01:00
|
|
|
for (unsigned i = 0; i < OPENSSL_ARRAY_SIZE(kRSAKeys); i++) {
|
|
|
|
const std::string name = kRSAKeys[i].name;
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2018-04-13 18:09:25 +01:00
|
|
|
bssl::UniquePtr<RSA> key(
|
|
|
|
RSA_private_key_from_bytes(kRSAKeys[i].key, kRSAKeys[i].key_len));
|
|
|
|
if (key == nullptr) {
|
|
|
|
fprintf(stderr, "Failed to parse %s key.\n", name.c_str());
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key.get())]);
|
|
|
|
const uint8_t fake_sha256_hash[32] = {0};
|
|
|
|
unsigned sig_len;
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
if (!TimeFunction(&results,
|
|
|
|
[&key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
|
|
|
|
// Usually during RSA signing we're using a long-lived |RSA| that has
|
|
|
|
// already had all of its |BN_MONT_CTX|s constructed, so it makes
|
|
|
|
// sense to use |key| directly here.
|
|
|
|
return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
|
|
|
|
sig.get(), &sig_len, key.get());
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "RSA_sign failed.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
results.Print(name + " signing");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results,
|
|
|
|
[&key, &fake_sha256_hash, &sig, sig_len]() -> bool {
|
|
|
|
return RSA_verify(
|
|
|
|
NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
|
|
|
|
sig.get(), sig_len, key.get());
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "RSA_verify failed.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
results.Print(name + " verify (same key)");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results,
|
|
|
|
[&key, &fake_sha256_hash, &sig, sig_len]() -> bool {
|
|
|
|
// Usually during RSA verification we have to parse an RSA key from a
|
|
|
|
// certificate or similar, in which case we'd need to construct a new
|
|
|
|
// RSA key, with a new |BN_MONT_CTX| for the public modulus. If we
|
|
|
|
// were to use |key| directly instead, then these costs wouldn't be
|
|
|
|
// accounted for.
|
|
|
|
bssl::UniquePtr<RSA> verify_key(RSA_new());
|
|
|
|
if (!verify_key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
verify_key->n = BN_dup(key->n);
|
|
|
|
verify_key->e = BN_dup(key->e);
|
|
|
|
if (!verify_key->n ||
|
|
|
|
!verify_key->e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return RSA_verify(NID_sha256, fake_sha256_hash,
|
|
|
|
sizeof(fake_sha256_hash), sig.get(), sig_len,
|
|
|
|
verify_key.get());
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "RSA_verify failed.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
results.Print(name + " verify (fresh key)");
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Add RSA key generation to speed.cc
On a Skylake machine, the improvements to make RSA key generation
constant-time did slow things down a bit:
Before:
Did 217 RSA 2048 key-gen operations in 30231344us (7.2 ops/sec)
min: 17154us, median: 117284us, max: 518336us
Did 70 RSA 3072 key-gen operations in 30188611us (2.3 ops/sec)
min: 57759us, median: 348873us, max: 1760351us
Did 27 RSA 4096 key-gen operations in 30264235us (0.9 ops/sec)
min: 202096us, median: 980160us, max: 4282915us
After:
Did 186 RSA 2048 key-gen operations in 30021173us (6.2 ops/sec)
min: 74850us, median: 147650us, max: 407031us
Did 54 RSA 3072 key-gen operations in 30111667us (1.8 ops/sec)
min: 292050us, median: 483786us, max: 1294105us
Did 18 RSA 4096 key-gen operations in 30662495us (0.6 ops/sec)
min: 902547us, median: 1446689us, max: 3660302us
Change-Id: I52a96bb41bab759aa7ef6239bdfa533707a9eb3c
Reviewed-on: https://boringssl-review.googlesource.com/26904
Commit-Queue: Adam Langley <alangley@gmail.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-03-30 21:43:59 +01:00
|
|
|
static bool SpeedRSAKeyGen(const std::string &selected) {
|
|
|
|
// Don't run this by default because it's so slow.
|
|
|
|
if (selected != "RSAKeyGen") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bssl::UniquePtr<BIGNUM> e(BN_new());
|
|
|
|
if (!BN_set_word(e.get(), 65537)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<int> kSizes = {2048, 3072, 4096};
|
|
|
|
for (int size : kSizes) {
|
|
|
|
const uint64_t start = time_now();
|
|
|
|
unsigned num_calls = 0;
|
|
|
|
unsigned us;
|
|
|
|
std::vector<unsigned> durations;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
bssl::UniquePtr<RSA> rsa(RSA_new());
|
|
|
|
|
|
|
|
const uint64_t iteration_start = time_now();
|
|
|
|
if (!RSA_generate_key_ex(rsa.get(), size, e.get(), nullptr)) {
|
|
|
|
fprintf(stderr, "RSA_generate_key_ex failed.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const uint64_t iteration_end = time_now();
|
|
|
|
|
|
|
|
num_calls++;
|
|
|
|
durations.push_back(iteration_end - iteration_start);
|
|
|
|
|
|
|
|
us = iteration_end - start;
|
|
|
|
if (us > 30 * 1000000 /* 30 secs */) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(durations.begin(), durations.end());
|
|
|
|
printf("Did %u RSA %d key-gen operations in %uus (%.1f ops/sec)\n",
|
|
|
|
num_calls, size, us,
|
|
|
|
(static_cast<double>(num_calls) / us) * 1000000);
|
|
|
|
const size_t n = durations.size();
|
|
|
|
assert(n > 0);
|
|
|
|
unsigned median = n & 1 ? durations[n / 2]
|
|
|
|
: (durations[n / 2 - 1] + durations[n / 2]) / 2;
|
|
|
|
printf(" min: %uus, median: %uus, max: %uus\n", durations[0], median,
|
|
|
|
durations[n - 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-28 23:52:57 +00:00
|
|
|
static uint8_t *align(uint8_t *in, unsigned alignment) {
|
|
|
|
return reinterpret_cast<uint8_t *>(
|
2015-03-17 10:37:06 +00:00
|
|
|
(reinterpret_cast<uintptr_t>(in) + alignment) &
|
|
|
|
~static_cast<size_t>(alignment - 1));
|
2015-01-16 00:05:41 +00:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name,
|
2017-02-27 21:53:23 +00:00
|
|
|
size_t chunk_len, size_t ad_len,
|
|
|
|
evp_aead_direction_t direction) {
|
2015-01-28 23:52:57 +00:00
|
|
|
static const unsigned kAlignment = 16;
|
|
|
|
|
2016-10-20 20:13:26 +01:00
|
|
|
bssl::ScopedEVP_AEAD_CTX ctx;
|
2014-06-20 20:00:00 +01:00
|
|
|
const size_t key_len = EVP_AEAD_key_length(aead);
|
|
|
|
const size_t nonce_len = EVP_AEAD_nonce_length(aead);
|
|
|
|
const size_t overhead_len = EVP_AEAD_max_overhead(aead);
|
|
|
|
|
|
|
|
std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(key.get(), 0, key_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(nonce.get(), 0, nonce_len);
|
2015-03-17 10:32:20 +00:00
|
|
|
std::unique_ptr<uint8_t[]> in_storage(new uint8_t[chunk_len + kAlignment]);
|
2017-08-02 23:40:09 +01:00
|
|
|
// N.B. for EVP_AEAD_CTX_seal_scatter the input and output buffers may be the
|
|
|
|
// same size. However, in the direction == evp_aead_open case we still use
|
|
|
|
// non-scattering seal, hence we add overhead_len to the size of this buffer.
|
|
|
|
std::unique_ptr<uint8_t[]> out_storage(
|
|
|
|
new uint8_t[chunk_len + overhead_len + kAlignment]);
|
2018-05-21 22:14:05 +01:00
|
|
|
std::unique_ptr<uint8_t[]> in2_storage(
|
|
|
|
new uint8_t[chunk_len + overhead_len + kAlignment]);
|
2015-01-16 01:33:48 +00:00
|
|
|
std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(ad.get(), 0, ad_len);
|
2017-08-02 23:40:09 +01:00
|
|
|
std::unique_ptr<uint8_t[]> tag_storage(
|
|
|
|
new uint8_t[overhead_len + kAlignment]);
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-28 23:52:57 +00:00
|
|
|
uint8_t *const in = align(in_storage.get(), kAlignment);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(in, 0, chunk_len);
|
2015-01-28 23:52:57 +00:00
|
|
|
uint8_t *const out = align(out_storage.get(), kAlignment);
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(out, 0, chunk_len + overhead_len);
|
2017-08-02 23:40:09 +01:00
|
|
|
uint8_t *const tag = align(tag_storage.get(), kAlignment);
|
|
|
|
OPENSSL_memset(tag, 0, overhead_len);
|
2017-02-27 21:53:23 +00:00
|
|
|
uint8_t *const in2 = align(in2_storage.get(), kAlignment);
|
2015-01-28 23:52:57 +00:00
|
|
|
|
2016-10-20 20:13:26 +01:00
|
|
|
if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len,
|
2015-03-17 22:28:37 +00:00
|
|
|
EVP_AEAD_DEFAULT_TAG_LENGTH,
|
|
|
|
evp_aead_seal)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
|
2015-04-10 03:21:10 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2014-06-20 20:00:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
2017-02-27 21:53:23 +00:00
|
|
|
if (direction == evp_aead_seal) {
|
2017-08-02 23:40:09 +01:00
|
|
|
if (!TimeFunction(&results,
|
|
|
|
[chunk_len, nonce_len, ad_len, overhead_len, in, out, tag,
|
|
|
|
&ctx, &nonce, &ad]() -> bool {
|
|
|
|
size_t tag_len;
|
|
|
|
return EVP_AEAD_CTX_seal_scatter(
|
|
|
|
ctx.get(), out, tag, &tag_len, overhead_len,
|
|
|
|
nonce.get(), nonce_len, in, chunk_len, nullptr, 0,
|
|
|
|
ad.get(), ad_len);
|
|
|
|
})) {
|
2017-02-27 21:53:23 +00:00
|
|
|
fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t out_len;
|
|
|
|
EVP_AEAD_CTX_seal(ctx.get(), out, &out_len, chunk_len + overhead_len,
|
|
|
|
nonce.get(), nonce_len, in, chunk_len, ad.get(), ad_len);
|
|
|
|
|
2018-05-21 22:14:05 +01:00
|
|
|
ctx.Reset();
|
|
|
|
if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len,
|
|
|
|
EVP_AEAD_DEFAULT_TAG_LENGTH,
|
|
|
|
evp_aead_open)) {
|
|
|
|
fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-02 23:40:09 +01:00
|
|
|
if (!TimeFunction(&results,
|
2018-05-21 22:14:05 +01:00
|
|
|
[chunk_len, overhead_len, nonce_len, ad_len, in2, out,
|
|
|
|
out_len, &ctx, &nonce, &ad]() -> bool {
|
2017-08-02 23:40:09 +01:00
|
|
|
size_t in2_len;
|
|
|
|
// N.B. EVP_AEAD_CTX_open_gather is not implemented for
|
|
|
|
// all AEADs.
|
2018-05-21 22:14:05 +01:00
|
|
|
return EVP_AEAD_CTX_open(ctx.get(), in2, &in2_len,
|
|
|
|
chunk_len + overhead_len,
|
|
|
|
nonce.get(), nonce_len, out,
|
|
|
|
out_len, ad.get(), ad_len);
|
2017-08-02 23:40:09 +01:00
|
|
|
})) {
|
2017-02-27 21:53:23 +00:00
|
|
|
fprintf(stderr, "EVP_AEAD_CTX_open failed.\n");
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2017-02-27 21:53:23 +00:00
|
|
|
results.PrintWithBytes(
|
|
|
|
name + (direction == evp_aead_seal ? " seal" : " open"), chunk_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-16 01:33:48 +00:00
|
|
|
static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name,
|
2015-04-13 19:04:18 +01:00
|
|
|
size_t ad_len, const std::string &selected) {
|
|
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-27 21:53:23 +00:00
|
|
|
return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len,
|
|
|
|
evp_aead_seal) &&
|
|
|
|
SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len,
|
|
|
|
evp_aead_seal) &&
|
|
|
|
SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len,
|
|
|
|
evp_aead_seal);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SpeedAEADOpen(const EVP_AEAD *aead, const std::string &name,
|
|
|
|
size_t ad_len, const std::string &selected) {
|
|
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len,
|
|
|
|
evp_aead_open) &&
|
|
|
|
SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len,
|
|
|
|
evp_aead_open) &&
|
|
|
|
SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len,
|
|
|
|
evp_aead_open);
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
static bool SpeedHashChunk(const EVP_MD *md, const std::string &name,
|
|
|
|
size_t chunk_len) {
|
2018-04-13 18:09:25 +01:00
|
|
|
bssl::ScopedEVP_MD_CTX ctx;
|
2014-06-20 20:00:00 +01:00
|
|
|
uint8_t scratch[8192];
|
|
|
|
|
|
|
|
if (chunk_len > sizeof(scratch)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
2018-04-13 18:09:25 +01:00
|
|
|
if (!TimeFunction(&results, [&ctx, md, chunk_len, &scratch]() -> bool {
|
2014-06-20 20:00:00 +01:00
|
|
|
uint8_t digest[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int md_len;
|
|
|
|
|
2018-04-13 18:09:25 +01:00
|
|
|
return EVP_DigestInit_ex(ctx.get(), md, NULL /* ENGINE */) &&
|
|
|
|
EVP_DigestUpdate(ctx.get(), scratch, chunk_len) &&
|
|
|
|
EVP_DigestFinal_ex(ctx.get(), digest, &md_len);
|
2014-06-20 20:00:00 +01:00
|
|
|
})) {
|
|
|
|
fprintf(stderr, "EVP_DigestInit_ex failed.\n");
|
2015-04-10 03:21:10 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2014-06-20 20:00:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.PrintWithBytes(name, chunk_len);
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-13 19:04:18 +01:00
|
|
|
static bool SpeedHash(const EVP_MD *md, const std::string &name,
|
|
|
|
const std::string &selected) {
|
|
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
return SpeedHashChunk(md, name + " (16 bytes)", 16) &&
|
|
|
|
SpeedHashChunk(md, name + " (256 bytes)", 256) &&
|
|
|
|
SpeedHashChunk(md, name + " (8192 bytes)", 8192);
|
|
|
|
}
|
|
|
|
|
2016-11-01 20:37:09 +00:00
|
|
|
static bool SpeedRandomChunk(const std::string &name, size_t chunk_len) {
|
2015-04-13 19:04:18 +01:00
|
|
|
uint8_t scratch[8192];
|
|
|
|
|
|
|
|
if (chunk_len > sizeof(scratch)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
if (!TimeFunction(&results, [chunk_len, &scratch]() -> bool {
|
|
|
|
RAND_bytes(scratch, chunk_len);
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.PrintWithBytes(name, chunk_len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SpeedRandom(const std::string &selected) {
|
|
|
|
if (!selected.empty() && selected != "RNG") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SpeedRandomChunk("RNG (16 bytes)", 16) &&
|
|
|
|
SpeedRandomChunk("RNG (256 bytes)", 256) &&
|
|
|
|
SpeedRandomChunk("RNG (8192 bytes)", 8192);
|
|
|
|
}
|
|
|
|
|
2015-04-14 20:07:44 +01:00
|
|
|
static bool SpeedECDHCurve(const std::string &name, int nid,
|
|
|
|
const std::string &selected) {
|
|
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-01 04:56:13 +01:00
|
|
|
bssl::UniquePtr<EC_KEY> peer_key(EC_KEY_new_by_curve_name(nid));
|
|
|
|
if (!peer_key ||
|
|
|
|
!EC_KEY_generate_key(peer_key.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t peer_value_len = EC_POINT_point2oct(
|
|
|
|
EC_KEY_get0_group(peer_key.get()), EC_KEY_get0_public_key(peer_key.get()),
|
|
|
|
POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr);
|
|
|
|
if (peer_value_len == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::unique_ptr<uint8_t[]> peer_value(new uint8_t[peer_value_len]);
|
|
|
|
peer_value_len = EC_POINT_point2oct(
|
|
|
|
EC_KEY_get0_group(peer_key.get()), EC_KEY_get0_public_key(peer_key.get()),
|
|
|
|
POINT_CONVERSION_UNCOMPRESSED, peer_value.get(), peer_value_len, nullptr);
|
|
|
|
if (peer_value_len == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-14 20:07:44 +01:00
|
|
|
TimeResults results;
|
2018-04-01 04:56:13 +01:00
|
|
|
if (!TimeFunction(&results, [nid, peer_value_len, &peer_value]() -> bool {
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
|
2015-04-14 20:07:44 +01:00
|
|
|
if (!key ||
|
|
|
|
!EC_KEY_generate_key(key.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const EC_GROUP *const group = EC_KEY_get0_group(key.get());
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group));
|
2018-04-01 04:56:13 +01:00
|
|
|
bssl::UniquePtr<EC_POINT> peer_point(EC_POINT_new(group));
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
|
2015-04-14 20:07:44 +01:00
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<BIGNUM> x(BN_new());
|
|
|
|
bssl::UniquePtr<BIGNUM> y(BN_new());
|
2015-04-14 20:07:44 +01:00
|
|
|
|
2018-04-01 04:56:13 +01:00
|
|
|
if (!point || !peer_point || !ctx || !x || !y ||
|
|
|
|
!EC_POINT_oct2point(group, peer_point.get(), peer_value.get(),
|
|
|
|
peer_value_len, ctx.get()) ||
|
|
|
|
!EC_POINT_mul(group, point.get(), NULL, peer_point.get(),
|
2015-04-14 20:07:44 +01:00
|
|
|
EC_KEY_get0_private_key(key.get()), ctx.get()) ||
|
|
|
|
!EC_POINT_get_affine_coordinates_GFp(group, point.get(), x.get(),
|
|
|
|
y.get(), ctx.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print(name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SpeedECDSACurve(const std::string &name, int nid,
|
|
|
|
const std::string &selected) {
|
|
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
|
2015-04-14 20:07:44 +01:00
|
|
|
if (!key ||
|
|
|
|
!EC_KEY_generate_key(key.get())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t signature[256];
|
|
|
|
if (ECDSA_size(key.get()) > sizeof(signature)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint8_t digest[20];
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(digest, 42, sizeof(digest));
|
2015-04-14 20:07:44 +01:00
|
|
|
unsigned sig_len;
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
if (!TimeFunction(&results, [&key, &signature, &digest, &sig_len]() -> bool {
|
|
|
|
return ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len,
|
|
|
|
key.get()) == 1;
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print(name + " signing");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, [&key, &signature, &digest, sig_len]() -> bool {
|
|
|
|
return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len,
|
|
|
|
key.get()) == 1;
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print(name + " verify");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SpeedECDH(const std::string &selected) {
|
|
|
|
return SpeedECDHCurve("ECDH P-224", NID_secp224r1, selected) &&
|
|
|
|
SpeedECDHCurve("ECDH P-256", NID_X9_62_prime256v1, selected) &&
|
|
|
|
SpeedECDHCurve("ECDH P-384", NID_secp384r1, selected) &&
|
|
|
|
SpeedECDHCurve("ECDH P-521", NID_secp521r1, selected);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SpeedECDSA(const std::string &selected) {
|
|
|
|
return SpeedECDSACurve("ECDSA P-224", NID_secp224r1, selected) &&
|
|
|
|
SpeedECDSACurve("ECDSA P-256", NID_X9_62_prime256v1, selected) &&
|
|
|
|
SpeedECDSACurve("ECDSA P-384", NID_secp384r1, selected) &&
|
|
|
|
SpeedECDSACurve("ECDSA P-521", NID_secp521r1, selected);
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:09:47 +00:00
|
|
|
static bool Speed25519(const std::string &selected) {
|
|
|
|
if (!selected.empty() && selected.find("25519") == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
|
|
|
|
uint8_t public_key[32], private_key[64];
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, [&public_key, &private_key]() -> bool {
|
|
|
|
ED25519_keypair(public_key, private_key);
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("Ed25519 key generation");
|
|
|
|
|
|
|
|
static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5};
|
|
|
|
uint8_t signature[64];
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, [&private_key, &signature]() -> bool {
|
|
|
|
return ED25519_sign(signature, kMessage, sizeof(kMessage),
|
|
|
|
private_key) == 1;
|
|
|
|
})) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("Ed25519 signing");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, [&public_key, &signature]() -> bool {
|
|
|
|
return ED25519_verify(kMessage, sizeof(kMessage), signature,
|
|
|
|
public_key) == 1;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "Ed25519 verify failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("Ed25519 verify");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, []() -> bool {
|
|
|
|
uint8_t out[32], in[32];
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(in, 0, sizeof(in));
|
2015-11-13 21:09:47 +00:00
|
|
|
X25519_public_from_private(out, in);
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "Curve25519 base-point multiplication failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("Curve25519 base-point multiplication");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, []() -> bool {
|
|
|
|
uint8_t out[32], in1[32], in2[32];
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memset(in1, 0, sizeof(in1));
|
|
|
|
OPENSSL_memset(in2, 0, sizeof(in2));
|
2015-11-17 23:15:05 +00:00
|
|
|
in1[0] = 1;
|
|
|
|
in2[0] = 9;
|
2015-11-13 21:09:47 +00:00
|
|
|
return X25519(out, in1, in2) == 1;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("Curve25519 arbitrary point multiplication");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-10 02:09:00 +00:00
|
|
|
static bool SpeedSPAKE2(const std::string &selected) {
|
|
|
|
if (!selected.empty() && selected.find("SPAKE2") == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
|
|
|
|
static const uint8_t kAliceName[] = {'A'};
|
|
|
|
static const uint8_t kBobName[] = {'B'};
|
|
|
|
static const uint8_t kPassword[] = "password";
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(spake2_role_alice,
|
|
|
|
kAliceName, sizeof(kAliceName), kBobName,
|
|
|
|
sizeof(kBobName)));
|
2016-02-10 02:09:00 +00:00
|
|
|
uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE];
|
|
|
|
size_t alice_msg_len;
|
|
|
|
|
|
|
|
if (!SPAKE2_generate_msg(alice.get(), alice_msg, &alice_msg_len,
|
|
|
|
sizeof(alice_msg),
|
|
|
|
kPassword, sizeof(kPassword))) {
|
|
|
|
fprintf(stderr, "SPAKE2_generate_msg failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:48:00 +00:00
|
|
|
if (!TimeFunction(&results, [&alice_msg, alice_msg_len]() -> bool {
|
2016-08-18 04:10:28 +01:00
|
|
|
bssl::UniquePtr<SPAKE2_CTX> bob(SPAKE2_CTX_new(spake2_role_bob,
|
|
|
|
kBobName, sizeof(kBobName), kAliceName,
|
|
|
|
sizeof(kAliceName)));
|
2016-02-10 02:09:00 +00:00
|
|
|
uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE], bob_key[64];
|
|
|
|
size_t bob_msg_len, bob_key_len;
|
|
|
|
if (!SPAKE2_generate_msg(bob.get(), bob_msg, &bob_msg_len,
|
|
|
|
sizeof(bob_msg), kPassword,
|
|
|
|
sizeof(kPassword)) ||
|
|
|
|
!SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len,
|
|
|
|
sizeof(bob_key), alice_msg, alice_msg_len)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "SPAKE2 failed.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("SPAKE2 over Ed25519");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-10 00:27:37 +01:00
|
|
|
static bool SpeedScrypt(const std::string &selected) {
|
|
|
|
if (!selected.empty() && selected.find("scrypt") == std::string::npos) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
|
|
|
|
static const char kPassword[] = "password";
|
|
|
|
static const uint8_t kSalt[] = "NaCl";
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, [&]() -> bool {
|
|
|
|
uint8_t out[64];
|
|
|
|
return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt,
|
|
|
|
sizeof(kSalt) - 1, 1024, 8, 16, 0 /* max_mem */,
|
|
|
|
out, sizeof(out));
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "scrypt failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
results.Print("scrypt (N = 1024, r = 8, p = 16)");
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, [&]() -> bool {
|
|
|
|
uint8_t out[64];
|
|
|
|
return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt,
|
|
|
|
sizeof(kSalt) - 1, 16384, 8, 1, 0 /* max_mem */,
|
|
|
|
out, sizeof(out));
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "scrypt failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
results.Print("scrypt (N = 16384, r = 8, p = 1)");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:53:42 +00:00
|
|
|
static bool SpeedHRSS(const std::string &selected) {
|
|
|
|
if (!selected.empty() && selected != "HRSS") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeResults results;
|
|
|
|
|
|
|
|
if (!TimeFunction(&results, []() -> bool {
|
|
|
|
struct HRSS_public_key pub;
|
|
|
|
struct HRSS_private_key priv;
|
|
|
|
uint8_t entropy[HRSS_GENERATE_KEY_BYTES];
|
|
|
|
RAND_bytes(entropy, sizeof(entropy));
|
|
|
|
HRSS_generate_key(&pub, &priv, entropy);
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "Failed to time HRSS_generate_key.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("HRSS generate");
|
|
|
|
|
|
|
|
struct HRSS_public_key pub;
|
|
|
|
struct HRSS_private_key priv;
|
|
|
|
uint8_t key_entropy[HRSS_GENERATE_KEY_BYTES];
|
|
|
|
RAND_bytes(key_entropy, sizeof(key_entropy));
|
|
|
|
HRSS_generate_key(&pub, &priv, key_entropy);
|
|
|
|
|
|
|
|
uint8_t ciphertext[HRSS_CIPHERTEXT_BYTES];
|
|
|
|
if (!TimeFunction(&results, [&pub, &ciphertext]() -> bool {
|
|
|
|
uint8_t entropy[HRSS_ENCAP_BYTES];
|
|
|
|
uint8_t shared_key[HRSS_KEY_BYTES];
|
|
|
|
RAND_bytes(entropy, sizeof(entropy));
|
|
|
|
HRSS_encap(ciphertext, shared_key, &pub, entropy);
|
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "Failed to time HRSS_encap.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("HRSS encap");
|
|
|
|
|
2018-12-17 19:17:26 +00:00
|
|
|
if (!TimeFunction(&results, [&priv, &ciphertext]() -> bool {
|
2018-11-12 21:53:42 +00:00
|
|
|
uint8_t shared_key[HRSS_KEY_BYTES];
|
2018-12-17 19:17:26 +00:00
|
|
|
HRSS_decap(shared_key, &priv, ciphertext, sizeof(ciphertext));
|
2018-11-12 21:53:42 +00:00
|
|
|
return true;
|
|
|
|
})) {
|
|
|
|
fprintf(stderr, "Failed to time HRSS_encap.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.Print("HRSS decap");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-15 03:06:49 +01:00
|
|
|
static const struct argument kArguments[] = {
|
|
|
|
{
|
|
|
|
"-filter", kOptionalArgument,
|
|
|
|
"A filter on the speed tests to run",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"-timeout", kOptionalArgument,
|
|
|
|
"The number of seconds to run each test for (default is 1)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"", kOptionalArgument, "",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
bool Speed(const std::vector<std::string> &args) {
|
2016-08-15 03:06:49 +01:00
|
|
|
std::map<std::string, std::string> args_map;
|
|
|
|
if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
|
|
|
|
PrintUsage(kArguments);
|
2015-04-13 19:04:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-08-15 03:06:49 +01:00
|
|
|
|
|
|
|
std::string selected;
|
|
|
|
if (args_map.count("-filter") != 0) {
|
|
|
|
selected = args_map["-filter"];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args_map.count("-timeout") != 0) {
|
|
|
|
g_timeout_seconds = atoi(args_map["-timeout"].c_str());
|
2015-04-13 19:04:18 +01:00
|
|
|
}
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-01-16 01:33:48 +00:00
|
|
|
// kTLSADLen is the number of bytes of additional data that TLS passes to
|
|
|
|
// AEADs.
|
|
|
|
static const size_t kTLSADLen = 13;
|
|
|
|
// kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs.
|
|
|
|
// These are AEADs that weren't originally defined as AEADs, but which we use
|
|
|
|
// via the AEAD interface. In order for that to work, they have some TLS
|
|
|
|
// knowledge in them and construct a couple of the AD bytes internally.
|
|
|
|
static const size_t kLegacyADLen = kTLSADLen - 2;
|
|
|
|
|
2018-04-13 18:09:25 +01:00
|
|
|
if (!SpeedRSA(selected) ||
|
|
|
|
!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) ||
|
2015-04-13 19:04:18 +01:00
|
|
|
!SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) ||
|
2015-11-30 23:48:18 +00:00
|
|
|
!SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen,
|
|
|
|
selected) ||
|
2015-12-08 00:48:16 +00:00
|
|
|
!SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1",
|
|
|
|
kLegacyADLen, selected) ||
|
2015-04-13 19:04:18 +01:00
|
|
|
!SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1",
|
|
|
|
kLegacyADLen, selected) ||
|
|
|
|
!SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1",
|
|
|
|
kLegacyADLen, selected) ||
|
2018-05-21 22:14:05 +01:00
|
|
|
!SpeedAEADOpen(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1",
|
|
|
|
kLegacyADLen, selected) ||
|
|
|
|
!SpeedAEADOpen(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1",
|
|
|
|
kLegacyADLen, selected) ||
|
2016-12-01 16:24:24 +00:00
|
|
|
!SpeedAEAD(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
|
|
|
|
selected) ||
|
|
|
|
!SpeedAEAD(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
|
|
|
|
selected) ||
|
2017-02-27 21:53:23 +00:00
|
|
|
!SpeedAEADOpen(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
|
|
|
|
selected) ||
|
|
|
|
!SpeedAEADOpen(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
|
|
|
|
selected) ||
|
2018-02-13 18:39:42 +00:00
|
|
|
!SpeedAEAD(EVP_aead_aes_128_ccm_bluetooth(), "AES-128-CCM-Bluetooth",
|
|
|
|
kTLSADLen, selected) ||
|
2015-04-13 19:04:18 +01:00
|
|
|
!SpeedHash(EVP_sha1(), "SHA-1", selected) ||
|
|
|
|
!SpeedHash(EVP_sha256(), "SHA-256", selected) ||
|
|
|
|
!SpeedHash(EVP_sha512(), "SHA-512", selected) ||
|
2015-04-14 20:07:44 +01:00
|
|
|
!SpeedRandom(selected) ||
|
|
|
|
!SpeedECDH(selected) ||
|
2015-11-13 21:09:47 +00:00
|
|
|
!SpeedECDSA(selected) ||
|
2016-02-10 02:09:00 +00:00
|
|
|
!Speed25519(selected) ||
|
2017-06-10 00:27:37 +01:00
|
|
|
!SpeedSPAKE2(selected) ||
|
Add RSA key generation to speed.cc
On a Skylake machine, the improvements to make RSA key generation
constant-time did slow things down a bit:
Before:
Did 217 RSA 2048 key-gen operations in 30231344us (7.2 ops/sec)
min: 17154us, median: 117284us, max: 518336us
Did 70 RSA 3072 key-gen operations in 30188611us (2.3 ops/sec)
min: 57759us, median: 348873us, max: 1760351us
Did 27 RSA 4096 key-gen operations in 30264235us (0.9 ops/sec)
min: 202096us, median: 980160us, max: 4282915us
After:
Did 186 RSA 2048 key-gen operations in 30021173us (6.2 ops/sec)
min: 74850us, median: 147650us, max: 407031us
Did 54 RSA 3072 key-gen operations in 30111667us (1.8 ops/sec)
min: 292050us, median: 483786us, max: 1294105us
Did 18 RSA 4096 key-gen operations in 30662495us (0.6 ops/sec)
min: 902547us, median: 1446689us, max: 3660302us
Change-Id: I52a96bb41bab759aa7ef6239bdfa533707a9eb3c
Reviewed-on: https://boringssl-review.googlesource.com/26904
Commit-Queue: Adam Langley <alangley@gmail.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
2018-03-30 21:43:59 +01:00
|
|
|
!SpeedScrypt(selected) ||
|
2018-11-12 21:53:42 +00:00
|
|
|
!SpeedRSAKeyGen(selected) ||
|
|
|
|
!SpeedHRSS(selected)) {
|
2014-06-20 20:00:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-13 19:04:18 +01:00
|
|
|
return true;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|