355 lines
10 KiB
C++
355 lines
10 KiB
C++
/* 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. */
|
|
|
|
// Work around warning C4265 "class has virtual functions, but destructor is
|
|
// not virtual" caused by code in <functional>.
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1900
|
|
#pragma warning(disable: 4265)
|
|
#endif
|
|
|
|
#include <string>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <openssl/digest.h>
|
|
#include <openssl/ecdh.h>
|
|
#include <openssl/ec_key.h>
|
|
#include <openssl/err.h>
|
|
#include <openssl/obj.h>
|
|
#include <openssl/rand.h>
|
|
#include <openssl/rsa.h>
|
|
|
|
#if defined(OPENSSL_WINDOWS)
|
|
#pragma warning(push, 3)
|
|
#include <windows.h>
|
|
#pragma warning(pop)
|
|
#elif defined(OPENSSL_APPLE)
|
|
#include <sys/time.h>
|
|
#endif
|
|
|
|
#include "../crypto/test/scoped_types.h"
|
|
#include "internal.h"
|
|
|
|
|
|
// 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; }
|
|
#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;
|
|
}
|
|
#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
|
|
|
|
static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
|
|
// kTotalMS is the total amount of time that we'll aim to measure a function
|
|
// for.
|
|
static const uint64_t kTotalUS = 1000000;
|
|
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();
|
|
if (now - start > kTotalUS) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
results->us = now - start;
|
|
results->num_calls = done;
|
|
return true;
|
|
}
|
|
|
|
static bool SpeedRSA(const std::string &key_name, RSA *key,
|
|
const std::string &selected) {
|
|
if (!selected.empty() && key_name.find(selected) == std::string::npos) {
|
|
return true;
|
|
}
|
|
|
|
std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]);
|
|
const uint8_t fake_sha256_hash[32] = {0};
|
|
unsigned sig_len;
|
|
|
|
TimeResults results;
|
|
if (!TimeFunction(&results,
|
|
[key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
|
|
return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
|
|
sig.get(), &sig_len, key);
|
|
})) {
|
|
fprintf(stderr, "RSA_sign failed.\n");
|
|
return false;
|
|
}
|
|
results.Print(key_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);
|
|
})) {
|
|
fprintf(stderr, "RSA_verify failed.\n");
|
|
return false;
|
|
}
|
|
results.Print(key_name + " verify");
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool SpeedRandomChunk(const std::string name, size_t chunk_len) {
|
|
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);
|
|
}
|
|
|
|
static bool SpeedECDHCurve(const std::string &name,
|
|
EC_GROUP_new_fn ec_group_new,
|
|
const std::string &selected) {
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
return true;
|
|
}
|
|
|
|
ScopedEC_KEY peer_key(EC_KEY_generate_key_ex(ec_group_new));
|
|
if (!peer_key) {
|
|
return false;
|
|
}
|
|
uint8_t peer_key_der[512];
|
|
size_t peer_key_der_len =
|
|
EC_KEY_public_key_to_oct(peer_key.get(), peer_key_der, sizeof(peer_key_der));
|
|
if (peer_key_der_len == 0) {
|
|
return false;
|
|
}
|
|
int peer_key_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(peer_key.get()));
|
|
|
|
TimeResults results;
|
|
if (!TimeFunction(&results,
|
|
[ec_group_new, &peer_key_der, peer_key_der_len,
|
|
peer_key_nid]() -> bool {
|
|
ScopedEC_KEY my_key(EC_KEY_generate_key_ex(ec_group_new));
|
|
if (!my_key) {
|
|
return false;
|
|
}
|
|
uint8_t buf[1024];
|
|
size_t buf_len;
|
|
if (!ECDH_compute_key_ex(buf, &buf_len, sizeof(buf), my_key.get(),
|
|
peer_key_nid, peer_key_der, peer_key_der_len)) {
|
|
return false;
|
|
}
|
|
return true;
|
|
})) {
|
|
return false;
|
|
}
|
|
|
|
results.Print(name);
|
|
return true;
|
|
}
|
|
|
|
static bool SpeedECDSACurve(const std::string &name,
|
|
EC_GROUP_new_fn ec_group_new,
|
|
const std::string &selected) {
|
|
if (!selected.empty() && name.find(selected) == std::string::npos) {
|
|
return true;
|
|
}
|
|
|
|
ScopedEC_KEY key(EC_KEY_generate_key_ex(ec_group_new));
|
|
if (!key) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t signature[256];
|
|
if (ECDSA_size(key.get()) > sizeof(signature)) {
|
|
return false;
|
|
}
|
|
uint8_t digest[20];
|
|
memset(digest, 42, sizeof(digest));
|
|
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");
|
|
|
|
uint8_t key_der[512];
|
|
size_t key_der_len =
|
|
EC_KEY_public_key_to_oct(key.get(), key_der, sizeof(key_der));
|
|
if (key_der_len == 0) {
|
|
return false;
|
|
}
|
|
|
|
if (!TimeFunction(&results, [ec_group_new, &key_der, key_der_len, &signature,
|
|
&digest, sig_len]() -> bool {
|
|
return ECDSA_verify_signed_digest(NID_sha1, digest, sizeof(digest),
|
|
signature, sig_len, ec_group_new,
|
|
key_der, key_der_len) == 1;
|
|
})) {
|
|
return false;
|
|
}
|
|
|
|
results.Print(name + " verify");
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool SpeedECDH(const std::string &selected) {
|
|
return SpeedECDHCurve("ECDH P-224", EC_GROUP_new_p224, selected) &&
|
|
SpeedECDHCurve("ECDH P-256", EC_GROUP_new_p256, selected) &&
|
|
SpeedECDHCurve("ECDH P-384", EC_GROUP_new_p384, selected) &&
|
|
SpeedECDHCurve("ECDH P-521", EC_GROUP_new_p521, selected);
|
|
}
|
|
|
|
static bool SpeedECDSA(const std::string &selected) {
|
|
return SpeedECDSACurve("ECDSA P-224", EC_GROUP_new_p224, selected) &&
|
|
SpeedECDSACurve("ECDSA P-256", EC_GROUP_new_p256, selected) &&
|
|
SpeedECDSACurve("ECDSA P-384", EC_GROUP_new_p384, selected) &&
|
|
SpeedECDSACurve("ECDSA P-521", EC_GROUP_new_p521, selected);
|
|
}
|
|
|
|
bool Speed(const std::vector<std::string> &args) {
|
|
std::string selected;
|
|
if (args.size() > 1) {
|
|
fprintf(stderr, "Usage: bssl speed [speed test selector, i.e. 'RNG']\n");
|
|
return false;
|
|
}
|
|
if (args.size() > 0) {
|
|
selected = args[0];
|
|
}
|
|
|
|
RSA *key = RSA_private_key_from_bytes(kDERRSAPrivate2048,
|
|
kDERRSAPrivate2048Len);
|
|
if (key == NULL) {
|
|
fprintf(stderr, "Failed to parse RSA key.\n");
|
|
return false;
|
|
}
|
|
|
|
if (!SpeedRSA("RSA 2048", key, selected)) {
|
|
return false;
|
|
}
|
|
|
|
RSA_free(key);
|
|
key = RSA_private_key_from_bytes(kDERRSAPrivate4096,
|
|
kDERRSAPrivate4096Len);
|
|
if (key == NULL) {
|
|
fprintf(stderr, "Failed to parse RSA key.\n");
|
|
return false;
|
|
}
|
|
if (!SpeedRSA("RSA 4096", key, selected)) {
|
|
return false;
|
|
}
|
|
|
|
RSA_free(key);
|
|
|
|
// kTLSADLen is the number of bytes of additional data that TLS passes to
|
|
// AEADs.
|
|
static const size_t kTLSADLen = 13;
|
|
|
|
if (!SpeedRandom(selected) ||
|
|
!SpeedECDH(selected) ||
|
|
!SpeedECDSA(selected)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|