| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <functional> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 |
| 21 | #include <assert.h> |
| 22 | #include <errno.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <stdint.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 |
| 28 | #include <openssl/aead.h> |
| 29 | #include <openssl/aes.h> |
| 30 | #include <openssl/base64.h> |
| 31 | #include <openssl/bn.h> |
| 32 | #include <openssl/bytestring.h> |
| 33 | #include <openssl/crypto.h> |
| 34 | #include <openssl/curve25519.h> |
| 35 | #include <openssl/digest.h> |
| 36 | #include <openssl/ec.h> |
| 37 | #include <openssl/ec_key.h> |
| 38 | #include <openssl/ecdsa.h> |
| 39 | #include <openssl/err.h> |
| 40 | #include <openssl/evp.h> |
| 41 | #define OPENSSL_UNSTABLE_EXPERIMENTAL_KYBER |
| 42 | #include <openssl/experimental/kyber.h> |
| 43 | #include <openssl/hrss.h> |
| 44 | #include <openssl/mem.h> |
| 45 | #include <openssl/mldsa.h> |
| 46 | #include <openssl/mlkem.h> |
| 47 | #include <openssl/nid.h> |
| 48 | #include <openssl/rand.h> |
| 49 | #include <openssl/rsa.h> |
| 50 | #include <openssl/siphash.h> |
| 51 | #include <openssl/slhdsa.h> |
| 52 | #include <openssl/trust_token.h> |
| 53 |
| 54 | #if defined(OPENSSL_WINDOWS) |
| 55 | #include <windows.h> |
| 56 | #elif defined(OPENSSL_APPLE) |
| 57 | #include <sys/time.h> |
| 58 | #else |
| 59 | #include <time.h> |
| 60 | #endif |
| 61 |
| 62 | #if defined(OPENSSL_THREADS) |
| 63 | #include <condition_variable> |
| 64 | #include <mutex> |
| 65 | #include <thread> |
| 66 | #endif |
| 67 |
| 68 | #include "../crypto/ec/internal.h" |
| 69 | #include "../crypto/fipsmodule/ec/internal.h" |
| 70 | #include "../crypto/internal.h" |
| 71 | #include "../crypto/trust_token/internal.h" |
| 72 | #include "internal.h" |
| 73 |
| 74 | // g_print_json is true if printed output is JSON formatted. |
| 75 | static bool g_print_json = false; |
| 76 |
| 77 | // TimeResults represents the results of benchmarking a function. |
| 78 | struct TimeResults { |
| 79 | // num_calls is the number of function calls done in the time period. |
| 80 | uint64_t num_calls; |
| 81 | // us is the number of microseconds that elapsed in the time period. |
| 82 | uint64_t us; |
| 83 |
| 84 | void Print(const std::string &description) const { |
| 85 | if (g_print_json) { |
| 86 | PrintJSON(description); |
| 87 | } else { |
| 88 | printf( |
| 89 | "Did %" PRIu64 " %s operations in %" PRIu64 "us (%.1f ops/sec)\n", |
| 90 | num_calls, description.c_str(), us, |
| 91 | (static_cast<double>(num_calls) / static_cast<double>(us)) * 1000000); |
| 92 | } |
| 93 | } |
| 94 |
| 95 | void PrintWithBytes(const std::string &description, |
| 96 | size_t bytes_per_call) const { |
| 97 | if (g_print_json) { |
| 98 | PrintJSON(description, bytes_per_call); |
| 99 | } else { |
| 100 | printf( |
| 101 | "Did %" PRIu64 " %s operations in %" PRIu64 |
| 102 | "us (%.1f ops/sec): %.1f MB/s\n", |
| 103 | num_calls, description.c_str(), us, |
| 104 | (static_cast<double>(num_calls) / static_cast<double>(us)) * 1000000, |
| 105 | static_cast<double>(bytes_per_call * num_calls) / |
| 106 | static_cast<double>(us)); |
| 107 | } |
| 108 | } |
| 109 |
| 110 | private: |
| 111 | void PrintJSON(const std::string &description, |
| 112 | size_t bytes_per_call = 0) const { |
| 113 | if (first_json_printed) { |
| 114 | puts(","); |
| 115 | } |
| 116 |
| 117 | printf("{\"description\": \"%s\", \"numCalls\": %" PRIu64 |
| 118 | ", \"microseconds\": %" PRIu64, |
| 119 | description.c_str(), num_calls, us); |
| 120 |
| 121 | if (bytes_per_call > 0) { |
| 122 | printf(", \"bytesPerCall\": %zu", bytes_per_call); |
| 123 | } |
| 124 |
| 125 | printf("}"); |
| 126 | first_json_printed = true; |
| 127 | } |
| 128 |
| 129 | // first_json_printed is true if |g_print_json| is true and the first item in |
| 130 | // the JSON results has been printed already. This is used to handle the |
| 131 | // commas between each item in the result list. |
| 132 | static bool first_json_printed; |
| 133 | }; |
| 134 |
| 135 | bool TimeResults::first_json_printed = false; |
| 136 |
| 137 | #if defined(OPENSSL_WINDOWS) |
| 138 | static uint64_t time_now() { return GetTickCount64() * 1000; } |
| 139 | #elif defined(OPENSSL_APPLE) |
| 140 | static uint64_t time_now() { |
| 141 | struct timeval tv; |
| 142 | uint64_t ret; |
| 143 |
| 144 | gettimeofday(&tv, NULL); |
| 145 | ret = tv.tv_sec; |
| 146 | ret *= 1000000; |
| 147 | ret += tv.tv_usec; |
| 148 | return ret; |
| 149 | } |
| 150 | #else |
| 151 | static uint64_t time_now() { |
| 152 | struct timespec ts; |
| 153 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 154 |
| 155 | uint64_t ret = ts.tv_sec; |
| 156 | ret *= 1000000; |
| 157 | ret += ts.tv_nsec / 1000; |
| 158 | return ret; |
| 159 | } |
| 160 | #endif |
| 161 |
| 162 | static uint64_t g_timeout_seconds = 1; |
| 163 | static std::vector<size_t> g_chunk_lengths = {16, 256, 1350, 8192, 16384}; |
| 164 |
| 165 | // IterationsBetweenTimeChecks returns the number of iterations of |func| to run |
| 166 | // in between checking the time, or zero on error. |
| 167 | static uint32_t IterationsBetweenTimeChecks(std::function<bool()> func) { |
| 168 | uint64_t start = time_now(); |
| 169 | if (!func()) { |
| 170 | return 0; |
| 171 | } |
| 172 | uint64_t delta = time_now() - start; |
| 173 | if (delta == 0) { |
| 174 | return 250; |
| 175 | } |
| 176 |
| 177 | // Aim for about 100ms between time checks. |
| 178 | uint32_t ret = static_cast<double>(100000) / static_cast<double>(delta); |
| 179 | if (ret > 1000) { |
| 180 | ret = 1000; |
| 181 | } else if (ret < 1) { |
| 182 | ret = 1; |
| 183 | } |
| 184 | return ret; |
| 185 | } |
| 186 |
| 187 | static bool TimeFunctionImpl(TimeResults *results, std::function<bool()> func, |
| 188 | uint32_t iterations_between_time_checks) { |
| 189 | // total_us is the total amount of time that we'll aim to measure a function |
| 190 | // for. |
| 191 | const uint64_t total_us = g_timeout_seconds * 1000000; |
| 192 | uint64_t start = time_now(), now; |
| 193 | uint64_t done = 0; |
| 194 | for (;;) { |
| 195 | for (uint32_t i = 0; i < iterations_between_time_checks; i++) { |
| 196 | if (!func()) { |
| 197 | return false; |
| 198 | } |
| 199 | done++; |
| 200 | } |
| 201 |
| 202 | now = time_now(); |
| 203 | if (now - start > total_us) { |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 |
| 208 | results->us = now - start; |
| 209 | results->num_calls = done; |
| 210 | return true; |
| 211 | } |
| 212 |
| 213 | static bool TimeFunction(TimeResults *results, std::function<bool()> func) { |
| 214 | uint32_t iterations_between_time_checks = IterationsBetweenTimeChecks(func); |
| 215 | if (iterations_between_time_checks == 0) { |
| 216 | return false; |
| 217 | } |
| 218 |
| 219 | return TimeFunctionImpl(results, std::move(func), |
| 220 | iterations_between_time_checks); |
| 221 | } |
| 222 |
| 223 | #if defined(OPENSSL_THREADS) |
| 224 | // g_threads is the number of threads to run in parallel benchmarks. |
| 225 | static int g_threads = 1; |
| 226 |
| 227 | // Latch behaves like C++20 std::latch. |
| 228 | class Latch { |
| 229 | public: |
| 230 | explicit Latch(int expected) : expected_(expected) {} |
| 231 | Latch(const Latch &) = delete; |
| 232 | Latch &operator=(const Latch &) = delete; |
| 233 |
| 234 | void ArriveAndWait() { |
| 235 | std::unique_lock<std::mutex> lock(lock_); |
| 236 | expected_--; |
| 237 | if (expected_ > 0) { |
| 238 | cond_.wait(lock, [&] { return expected_ == 0; }); |
| 239 | } else { |
| 240 | cond_.notify_all(); |
| 241 | } |
| 242 | } |
| 243 |
| 244 | private: |
| 245 | int expected_; |
| 246 | std::mutex lock_; |
| 247 | std::condition_variable cond_; |
| 248 | }; |
| 249 |
| 250 | static bool TimeFunctionParallel(TimeResults *results, |
| 251 | std::function<bool()> func) { |
| 252 | if (g_threads <= 1) { |
| 253 | return TimeFunction(results, std::move(func)); |
| 254 | } |
| 255 |
| 256 | uint32_t iterations_between_time_checks = IterationsBetweenTimeChecks(func); |
| 257 | if (iterations_between_time_checks == 0) { |
| 258 | return false; |
| 259 | } |
| 260 |
| 261 | struct ThreadResult { |
| 262 | TimeResults time_result; |
| 263 | bool ok = false; |
| 264 | }; |
| 265 | std::vector<ThreadResult> thread_results(g_threads); |
| 266 | Latch latch(g_threads); |
| 267 | std::vector<std::thread> threads; |
| 268 | for (int i = 0; i < g_threads; i++) { |
| 269 | threads.emplace_back([&, i] { |
| 270 | // Wait for all the threads to be ready before running the benchmark. |
| 271 | latch.ArriveAndWait(); |
| 272 | thread_results[i].ok = TimeFunctionImpl( |
| 273 | &thread_results[i].time_result, func, iterations_between_time_checks); |
| 274 | }); |
| 275 | } |
| 276 |
| 277 | for (auto &thread : threads) { |
| 278 | thread.join(); |
| 279 | } |
| 280 |
| 281 | results->num_calls = 0; |
| 282 | results->us = 0; |
| 283 | for (const auto &pair : thread_results) { |
| 284 | if (!pair.ok) { |
| 285 | return false; |
| 286 | } |
| 287 | results->num_calls += pair.time_result.num_calls; |
| 288 | results->us += pair.time_result.us; |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 |
| 293 | #else |
| 294 | static bool TimeFunctionParallel(TimeResults *results, |
| 295 | std::function<bool()> func) { |
| 296 | return TimeFunction(results, std::move(func)); |
| 297 | } |
| 298 | #endif |
| 299 |
| 300 | static bool SpeedRSA(const std::string &selected) { |
| 301 | if (!selected.empty() && selected.find("RSA") == std::string::npos) { |
| 302 | return true; |
| 303 | } |
| 304 |
| 305 | static const struct { |
| 306 | const char *name; |
| 307 | const uint8_t *key; |
| 308 | const size_t key_len; |
| 309 | } kRSAKeys[] = { |
| 310 | {"RSA 2048", kDERRSAPrivate2048, kDERRSAPrivate2048Len}, |
| 311 | {"RSA 3072", kDERRSAPrivate3072, kDERRSAPrivate3072Len}, |
| 312 | {"RSA 4096", kDERRSAPrivate4096, kDERRSAPrivate4096Len}, |
| 313 | }; |
| 314 |
| 315 | for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRSAKeys); i++) { |
| 316 | const std::string name = kRSAKeys[i].name; |
| 317 |
| 318 | bssl::UniquePtr<RSA> key( |
| 319 | RSA_private_key_from_bytes(kRSAKeys[i].key, kRSAKeys[i].key_len)); |
| 320 | if (key == nullptr) { |
| 321 | fprintf(stderr, "Failed to parse %s key.\n", name.c_str()); |
| 322 | ERR_print_errors_fp(stderr); |
| 323 | return false; |
| 324 | } |
| 325 |
| 326 | static constexpr size_t kMaxSignature = 512; |
| 327 | if (RSA_size(key.get()) > kMaxSignature) { |
| 328 | abort(); |
| 329 | } |
| 330 | const uint8_t fake_sha256_hash[32] = {0}; |
| 331 |
| 332 | TimeResults results; |
| 333 | if (!TimeFunctionParallel(&results, [&key, &fake_sha256_hash]() -> bool { |
| 334 | // Usually during RSA signing we're using a long-lived |RSA| that |
| 335 | // has already had all of its |BN_MONT_CTX|s constructed, so it |
| 336 | // makes sense to use |key| directly here. |
| 337 | uint8_t out[kMaxSignature]; |
| 338 | unsigned out_len; |
| 339 | return RSA_sign(NID_sha256, fake_sha256_hash, |
| 340 | sizeof(fake_sha256_hash), out, &out_len, key.get()); |
| 341 | })) { |
| 342 | fprintf(stderr, "RSA_sign failed.\n"); |
| 343 | ERR_print_errors_fp(stderr); |
| 344 | return false; |
| 345 | } |
| 346 | results.Print(name + " signing"); |
| 347 |
| 348 | uint8_t sig[kMaxSignature]; |
| 349 | unsigned sig_len; |
| 350 | if (!RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash), sig, |
| 351 | &sig_len, key.get())) { |
| 352 | return false; |
| 353 | } |
| 354 | if (!TimeFunctionParallel( |
| 355 | &results, [&key, &fake_sha256_hash, &sig, sig_len]() -> bool { |
| 356 | return RSA_verify(NID_sha256, fake_sha256_hash, |
| 357 | sizeof(fake_sha256_hash), sig, sig_len, |
| 358 | key.get()); |
| 359 | })) { |
| 360 | fprintf(stderr, "RSA_verify failed.\n"); |
| 361 | ERR_print_errors_fp(stderr); |
| 362 | return false; |
| 363 | } |
| 364 | results.Print(name + " verify (same key)"); |
| 365 |
| 366 | if (!TimeFunctionParallel( |
| 367 | &results, [&key, &fake_sha256_hash, &sig, sig_len]() -> bool { |
| 368 | // Usually during RSA verification we have to parse an RSA key |
| 369 | // from a certificate or similar, in which case we'd need to |
| 370 | // construct a new RSA key, with a new |BN_MONT_CTX| for the |
| 371 | // public modulus. If we were to use |key| directly instead, then |
| 372 | // these costs wouldn't be accounted for. |
| 373 | bssl::UniquePtr<RSA> verify_key(RSA_new_public_key( |
| 374 | RSA_get0_n(key.get()), RSA_get0_e(key.get()))); |
| 375 | if (!verify_key) { |
| 376 | return false; |
| 377 | } |
| 378 | return RSA_verify(NID_sha256, fake_sha256_hash, |
| 379 | sizeof(fake_sha256_hash), sig, sig_len, |
| 380 | verify_key.get()); |
| 381 | })) { |
| 382 | fprintf(stderr, "RSA_verify failed.\n"); |
| 383 | ERR_print_errors_fp(stderr); |
| 384 | return false; |
| 385 | } |
| 386 | results.Print(name + " verify (fresh key)"); |
| 387 |
| 388 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 389 | return bssl::UniquePtr<RSA>(RSA_private_key_from_bytes( |
| 390 | kRSAKeys[i].key, kRSAKeys[i].key_len)) != nullptr; |
| 391 | })) { |
| 392 | fprintf(stderr, "Failed to parse %s key.\n", name.c_str()); |
| 393 | ERR_print_errors_fp(stderr); |
| 394 | return false; |
| 395 | } |
| 396 | results.Print(name + " private key parse"); |
| 397 | } |
| 398 |
| 399 | return true; |
| 400 | } |
| 401 |
| 402 | static bool SpeedRSAKeyGen(const std::string &selected) { |
| 403 | // Don't run this by default because it's so slow. |
| 404 | if (selected != "RSAKeyGen") { |
| 405 | return true; |
| 406 | } |
| 407 |
| 408 | bssl::UniquePtr<BIGNUM> e(BN_new()); |
| 409 | if (!BN_set_word(e.get(), 65537)) { |
| 410 | return false; |
| 411 | } |
| 412 |
| 413 | const std::vector<int> kSizes = {2048, 3072, 4096}; |
| 414 | for (int size : kSizes) { |
| 415 | const uint64_t start = time_now(); |
| 416 | uint64_t num_calls = 0; |
| 417 | uint64_t us; |
| 418 | std::vector<uint64_t> durations; |
| 419 |
| 420 | for (;;) { |
| 421 | bssl::UniquePtr<RSA> rsa(RSA_new()); |
| 422 |
| 423 | const uint64_t iteration_start = time_now(); |
| 424 | if (!RSA_generate_key_ex(rsa.get(), size, e.get(), nullptr)) { |
| 425 | fprintf(stderr, "RSA_generate_key_ex failed.\n"); |
| 426 | ERR_print_errors_fp(stderr); |
| 427 | return false; |
| 428 | } |
| 429 | const uint64_t iteration_end = time_now(); |
| 430 |
| 431 | num_calls++; |
| 432 | durations.push_back(iteration_end - iteration_start); |
| 433 |
| 434 | us = iteration_end - start; |
| 435 | if (us > 30 * 1000000 /* 30 secs */) { |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 |
| 440 | std::sort(durations.begin(), durations.end()); |
| 441 | const std::string description = |
| 442 | std::string("RSA ") + std::to_string(size) + std::string(" key-gen"); |
| 443 | const TimeResults results = {num_calls, us}; |
| 444 | results.Print(description); |
| 445 | const size_t n = durations.size(); |
| 446 | assert(n > 0); |
| 447 |
| 448 | // Distribution information is useful, but doesn't fit into the standard |
| 449 | // format used by |g_print_json|. |
| 450 | if (!g_print_json) { |
| 451 | uint64_t min = durations[0]; |
| 452 | uint64_t median = n & 1 ? durations[n / 2] |
| 453 | : (durations[n / 2 - 1] + durations[n / 2]) / 2; |
| 454 | uint64_t max = durations[n - 1]; |
| 455 | printf(" min: %" PRIu64 "us, median: %" PRIu64 "us, max: %" PRIu64 |
| 456 | "us\n", |
| 457 | min, median, max); |
| 458 | } |
| 459 | } |
| 460 |
| 461 | return true; |
| 462 | } |
| 463 |
| 464 | static std::string ChunkLenSuffix(size_t chunk_len) { |
| 465 | char buf[32]; |
| 466 | snprintf(buf, sizeof(buf), " (%zu byte%s)", chunk_len, |
| 467 | chunk_len != 1 ? "s" : ""); |
| 468 | return buf; |
| 469 | } |
| 470 |
| 471 | static bool SpeedAEADChunk(const EVP_AEAD *aead, std::string name, |
| 472 | size_t chunk_len, size_t ad_len, |
| 473 | evp_aead_direction_t direction) { |
| 474 | static const unsigned kAlignment = 16; |
| 475 |
| 476 | name += ChunkLenSuffix(chunk_len); |
| 477 | bssl::ScopedEVP_AEAD_CTX ctx; |
| 478 | const size_t key_len = EVP_AEAD_key_length(aead); |
| 479 | const size_t nonce_len = EVP_AEAD_nonce_length(aead); |
| 480 | const size_t overhead_len = EVP_AEAD_max_overhead(aead); |
| 481 |
| 482 | auto key = std::make_unique<uint8_t[]>(key_len); |
| 483 | OPENSSL_memset(key.get(), 0, key_len); |
| 484 | auto nonce = std::make_unique<uint8_t[]>(nonce_len); |
| 485 | OPENSSL_memset(nonce.get(), 0, nonce_len); |
| 486 | auto in_storage = std::make_unique<uint8_t[]>(chunk_len + kAlignment); |
| 487 | // N.B. for EVP_AEAD_CTX_seal_scatter the input and output buffers may be the |
| 488 | // same size. However, in the direction == evp_aead_open case we still use |
| 489 | // non-scattering seal, hence we add overhead_len to the size of this buffer. |
| 490 | auto out_storage = |
| 491 | std::make_unique<uint8_t[]>(chunk_len + overhead_len + kAlignment); |
| 492 | auto in2_storage = |
| 493 | std::make_unique<uint8_t[]>(chunk_len + overhead_len + kAlignment); |
| 494 | auto ad = std::make_unique<uint8_t[]>(ad_len); |
| 495 | OPENSSL_memset(ad.get(), 0, ad_len); |
| 496 | auto tag_storage = std::make_unique<uint8_t[]>(overhead_len + kAlignment); |
| 497 |
| 498 | uint8_t *const in = |
| 499 | static_cast<uint8_t *>(align_pointer(in_storage.get(), kAlignment)); |
| 500 | OPENSSL_memset(in, 0, chunk_len); |
| 501 | uint8_t *const out = |
| 502 | static_cast<uint8_t *>(align_pointer(out_storage.get(), kAlignment)); |
| 503 | OPENSSL_memset(out, 0, chunk_len + overhead_len); |
| 504 | uint8_t *const tag = |
| 505 | static_cast<uint8_t *>(align_pointer(tag_storage.get(), kAlignment)); |
| 506 | OPENSSL_memset(tag, 0, overhead_len); |
| 507 | uint8_t *const in2 = |
| 508 | static_cast<uint8_t *>(align_pointer(in2_storage.get(), kAlignment)); |
| 509 |
| 510 | if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len, |
| 511 | EVP_AEAD_DEFAULT_TAG_LENGTH, |
| 512 | evp_aead_seal)) { |
| 513 | fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n"); |
| 514 | ERR_print_errors_fp(stderr); |
| 515 | return false; |
| 516 | } |
| 517 |
| 518 | // TODO(davidben): In most cases, this can be |TimeFunctionParallel|, but a |
| 519 | // few stateful AEADs must be run serially. |
| 520 | TimeResults results; |
| 521 | if (direction == evp_aead_seal) { |
| 522 | if (!TimeFunction(&results, |
| 523 | [chunk_len, nonce_len, ad_len, overhead_len, in, out, tag, |
| 524 | &ctx, &nonce, &ad]() -> bool { |
| 525 | size_t tag_len; |
| 526 | return EVP_AEAD_CTX_seal_scatter( |
| 527 | ctx.get(), out, tag, &tag_len, overhead_len, |
| 528 | nonce.get(), nonce_len, in, chunk_len, nullptr, 0, |
| 529 | ad.get(), ad_len); |
| 530 | })) { |
| 531 | fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n"); |
| 532 | ERR_print_errors_fp(stderr); |
| 533 | return false; |
| 534 | } |
| 535 | } else { |
| 536 | size_t out_len; |
| 537 | EVP_AEAD_CTX_seal(ctx.get(), out, &out_len, chunk_len + overhead_len, |
| 538 | nonce.get(), nonce_len, in, chunk_len, ad.get(), ad_len); |
| 539 |
| 540 | ctx.Reset(); |
| 541 | if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len, |
| 542 | EVP_AEAD_DEFAULT_TAG_LENGTH, |
| 543 | evp_aead_open)) { |
| 544 | fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n"); |
| 545 | ERR_print_errors_fp(stderr); |
| 546 | return false; |
| 547 | } |
| 548 |
| 549 | if (!TimeFunction(&results, |
| 550 | [chunk_len, overhead_len, nonce_len, ad_len, in2, out, |
| 551 | out_len, &ctx, &nonce, &ad]() -> bool { |
| 552 | size_t in2_len; |
| 553 | // N.B. EVP_AEAD_CTX_open_gather is not implemented for |
| 554 | // all AEADs. |
| 555 | return EVP_AEAD_CTX_open(ctx.get(), in2, &in2_len, |
| 556 | chunk_len + overhead_len, |
| 557 | nonce.get(), nonce_len, out, |
| 558 | out_len, ad.get(), ad_len); |
| 559 | })) { |
| 560 | fprintf(stderr, "EVP_AEAD_CTX_open failed.\n"); |
| 561 | ERR_print_errors_fp(stderr); |
| 562 | return false; |
| 563 | } |
| 564 | } |
| 565 |
| 566 | results.PrintWithBytes( |
| 567 | name + (direction == evp_aead_seal ? " seal" : " open"), chunk_len); |
| 568 | return true; |
| 569 | } |
| 570 |
| 571 | static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name, |
| 572 | size_t ad_len, const std::string &selected) { |
| 573 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 574 | return true; |
| 575 | } |
| 576 |
| 577 | for (size_t chunk_len : g_chunk_lengths) { |
| 578 | if (!SpeedAEADChunk(aead, name, chunk_len, ad_len, evp_aead_seal)) { |
| 579 | return false; |
| 580 | } |
| 581 | } |
| 582 | return true; |
| 583 | } |
| 584 |
| 585 | static bool SpeedAEADOpen(const EVP_AEAD *aead, const std::string &name, |
| 586 | size_t ad_len, const std::string &selected) { |
| 587 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 588 | return true; |
| 589 | } |
| 590 |
| 591 | for (size_t chunk_len : g_chunk_lengths) { |
| 592 | if (!SpeedAEADChunk(aead, name, chunk_len, ad_len, evp_aead_open)) { |
| 593 | return false; |
| 594 | } |
| 595 | } |
| 596 |
| 597 | return true; |
| 598 | } |
| 599 |
| 600 | static bool SpeedAESBlock(const std::string &name, unsigned bits, |
| 601 | const std::string &selected) { |
| 602 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 603 | return true; |
| 604 | } |
| 605 |
| 606 | static const uint8_t kZero[32] = {0}; |
| 607 |
| 608 | { |
| 609 | TimeResults results; |
| 610 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 611 | AES_KEY key; |
| 612 | return AES_set_encrypt_key(kZero, bits, &key) == 0; |
| 613 | })) { |
| 614 | fprintf(stderr, "AES_set_encrypt_key failed.\n"); |
| 615 | return false; |
| 616 | } |
| 617 | results.Print(name + " encrypt setup"); |
| 618 | } |
| 619 |
| 620 | { |
| 621 | AES_KEY key; |
| 622 | if (AES_set_encrypt_key(kZero, bits, &key) != 0) { |
| 623 | return false; |
| 624 | } |
| 625 | uint8_t block[16] = {0}; |
| 626 | TimeResults results; |
| 627 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 628 | AES_encrypt(block, block, &key); |
| 629 | return true; |
| 630 | })) { |
| 631 | fprintf(stderr, "AES_encrypt failed.\n"); |
| 632 | return false; |
| 633 | } |
| 634 | results.Print(name + " encrypt"); |
| 635 | } |
| 636 |
| 637 | { |
| 638 | TimeResults results; |
| 639 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 640 | AES_KEY key; |
| 641 | return AES_set_decrypt_key(kZero, bits, &key) == 0; |
| 642 | })) { |
| 643 | fprintf(stderr, "AES_set_decrypt_key failed.\n"); |
| 644 | return false; |
| 645 | } |
| 646 | results.Print(name + " decrypt setup"); |
| 647 | } |
| 648 |
| 649 | { |
| 650 | AES_KEY key; |
| 651 | if (AES_set_decrypt_key(kZero, bits, &key) != 0) { |
| 652 | return false; |
| 653 | } |
| 654 | uint8_t block[16] = {0}; |
| 655 | TimeResults results; |
| 656 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 657 | AES_decrypt(block, block, &key); |
| 658 | return true; |
| 659 | })) { |
| 660 | fprintf(stderr, "AES_decrypt failed.\n"); |
| 661 | return false; |
| 662 | } |
| 663 | results.Print(name + " decrypt"); |
| 664 | } |
| 665 |
| 666 | return true; |
| 667 | } |
| 668 |
| 669 | static bool SpeedHashChunk(const EVP_MD *md, std::string name, |
| 670 | size_t chunk_len) { |
| 671 | uint8_t input[16384] = {0}; |
| 672 |
| 673 | if (chunk_len > sizeof(input)) { |
| 674 | return false; |
| 675 | } |
| 676 |
| 677 | name += ChunkLenSuffix(chunk_len); |
| 678 | TimeResults results; |
| 679 | if (!TimeFunctionParallel(&results, [md, chunk_len, &input]() -> bool { |
| 680 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 681 | unsigned int md_len; |
| 682 |
| 683 | bssl::ScopedEVP_MD_CTX ctx; |
| 684 | return EVP_DigestInit_ex(ctx.get(), md, NULL /* ENGINE */) && |
| 685 | EVP_DigestUpdate(ctx.get(), input, chunk_len) && |
| 686 | EVP_DigestFinal_ex(ctx.get(), digest, &md_len); |
| 687 | })) { |
| 688 | fprintf(stderr, "EVP_DigestInit_ex failed.\n"); |
| 689 | ERR_print_errors_fp(stderr); |
| 690 | return false; |
| 691 | } |
| 692 |
| 693 | results.PrintWithBytes(name, chunk_len); |
| 694 | return true; |
| 695 | } |
| 696 |
| 697 | static bool SpeedHash(const EVP_MD *md, const std::string &name, |
| 698 | const std::string &selected) { |
| 699 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 700 | return true; |
| 701 | } |
| 702 |
| 703 | for (size_t chunk_len : g_chunk_lengths) { |
| 704 | if (!SpeedHashChunk(md, name, chunk_len)) { |
| 705 | return false; |
| 706 | } |
| 707 | } |
| 708 |
| 709 | return true; |
| 710 | } |
| 711 |
| 712 | static bool SpeedRandomChunk(std::string name, size_t chunk_len) { |
| 713 | static constexpr size_t kMaxChunk = 16384; |
| 714 | if (chunk_len > kMaxChunk) { |
| 715 | return false; |
| 716 | } |
| 717 |
| 718 | name += ChunkLenSuffix(chunk_len); |
| 719 | TimeResults results; |
| 720 | if (!TimeFunctionParallel(&results, [chunk_len]() -> bool { |
| 721 | uint8_t scratch[kMaxChunk]; |
| 722 | RAND_bytes(scratch, chunk_len); |
| 723 | return true; |
| 724 | })) { |
| 725 | return false; |
| 726 | } |
| 727 |
| 728 | results.PrintWithBytes(name, chunk_len); |
| 729 | return true; |
| 730 | } |
| 731 |
| 732 | static bool SpeedRandom(const std::string &selected) { |
| 733 | if (!selected.empty() && selected != "RNG") { |
| 734 | return true; |
| 735 | } |
| 736 |
| 737 | for (size_t chunk_len : g_chunk_lengths) { |
| 738 | if (!SpeedRandomChunk("RNG", chunk_len)) { |
| 739 | return false; |
| 740 | } |
| 741 | } |
| 742 |
| 743 | return true; |
| 744 | } |
| 745 |
| 746 | static bool SpeedECDHCurve(const std::string &name, const EC_GROUP *group, |
| 747 | const std::string &selected) { |
| 748 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 749 | return true; |
| 750 | } |
| 751 |
| 752 | bssl::UniquePtr<EC_KEY> peer_key(EC_KEY_new()); |
| 753 | if (!peer_key || !EC_KEY_set_group(peer_key.get(), group) || |
| 754 | !EC_KEY_generate_key(peer_key.get())) { |
| 755 | return false; |
| 756 | } |
| 757 |
| 758 | size_t peer_value_len = EC_POINT_point2oct( |
| 759 | EC_KEY_get0_group(peer_key.get()), EC_KEY_get0_public_key(peer_key.get()), |
| 760 | POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr); |
| 761 | if (peer_value_len == 0) { |
| 762 | return false; |
| 763 | } |
| 764 | auto peer_value = std::make_unique<uint8_t[]>(peer_value_len); |
| 765 | peer_value_len = EC_POINT_point2oct( |
| 766 | EC_KEY_get0_group(peer_key.get()), EC_KEY_get0_public_key(peer_key.get()), |
| 767 | POINT_CONVERSION_UNCOMPRESSED, peer_value.get(), peer_value_len, nullptr); |
| 768 | if (peer_value_len == 0) { |
| 769 | return false; |
| 770 | } |
| 771 |
| 772 | TimeResults results; |
| 773 | if (!TimeFunctionParallel( |
| 774 | &results, [group, peer_value_len, &peer_value]() -> bool { |
| 775 | bssl::UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 776 | if (!key || !EC_KEY_set_group(key.get(), group) || |
| 777 | !EC_KEY_generate_key(key.get())) { |
| 778 | return false; |
| 779 | } |
| 780 | bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group)); |
| 781 | bssl::UniquePtr<EC_POINT> peer_point(EC_POINT_new(group)); |
| 782 | bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new()); |
| 783 | bssl::UniquePtr<BIGNUM> x(BN_new()); |
| 784 | if (!point || !peer_point || !ctx || !x || |
| 785 | !EC_POINT_oct2point(group, peer_point.get(), peer_value.get(), |
| 786 | peer_value_len, ctx.get()) || |
| 787 | !EC_POINT_mul(group, point.get(), nullptr, peer_point.get(), |
| 788 | EC_KEY_get0_private_key(key.get()), ctx.get()) || |
| 789 | !EC_POINT_get_affine_coordinates_GFp( |
| 790 | group, point.get(), x.get(), nullptr, ctx.get())) { |
| 791 | return false; |
| 792 | } |
| 793 |
| 794 | return true; |
| 795 | })) { |
| 796 | return false; |
| 797 | } |
| 798 |
| 799 | results.Print(name); |
| 800 | return true; |
| 801 | } |
| 802 |
| 803 | static bool SpeedECDSACurve(const std::string &name, const EC_GROUP *group, |
| 804 | const std::string &selected) { |
| 805 | if (!selected.empty() && name.find(selected) == std::string::npos) { |
| 806 | return true; |
| 807 | } |
| 808 |
| 809 | bssl::UniquePtr<EC_KEY> key(EC_KEY_new()); |
| 810 | if (!key || !EC_KEY_set_group(key.get(), group) || |
| 811 | !EC_KEY_generate_key(key.get())) { |
| 812 | return false; |
| 813 | } |
| 814 |
| 815 | static constexpr size_t kMaxSignature = 256; |
| 816 | if (ECDSA_size(key.get()) > kMaxSignature) { |
| 817 | abort(); |
| 818 | } |
| 819 | uint8_t digest[20]; |
| 820 | OPENSSL_memset(digest, 42, sizeof(digest)); |
| 821 |
| 822 | TimeResults results; |
| 823 | if (!TimeFunctionParallel(&results, [&key, &digest]() -> bool { |
| 824 | uint8_t out[kMaxSignature]; |
| 825 | unsigned out_len; |
| 826 | return ECDSA_sign(0, digest, sizeof(digest), out, &out_len, |
| 827 | key.get()) == 1; |
| 828 | })) { |
| 829 | return false; |
| 830 | } |
| 831 |
| 832 | results.Print(name + " signing"); |
| 833 |
| 834 | uint8_t signature[kMaxSignature]; |
| 835 | unsigned sig_len; |
| 836 | if (!ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len, key.get())) { |
| 837 | return false; |
| 838 | } |
| 839 |
| 840 | if (!TimeFunctionParallel( |
| 841 | &results, [&key, &signature, &digest, sig_len]() -> bool { |
| 842 | return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len, |
| 843 | key.get()) == 1; |
| 844 | })) { |
| 845 | return false; |
| 846 | } |
| 847 |
| 848 | results.Print(name + " verify"); |
| 849 |
| 850 | return true; |
| 851 | } |
| 852 |
| 853 | static bool SpeedECDH(const std::string &selected) { |
| 854 | return SpeedECDHCurve("ECDH P-224", EC_group_p224(), selected) && |
| 855 | SpeedECDHCurve("ECDH P-256", EC_group_p256(), selected) && |
| 856 | SpeedECDHCurve("ECDH P-384", EC_group_p384(), selected) && |
| 857 | SpeedECDHCurve("ECDH P-521", EC_group_p521(), selected); |
| 858 | } |
| 859 |
| 860 | static bool SpeedECDSA(const std::string &selected) { |
| 861 | return SpeedECDSACurve("ECDSA P-224", EC_group_p224(), selected) && |
| 862 | SpeedECDSACurve("ECDSA P-256", EC_group_p256(), selected) && |
| 863 | SpeedECDSACurve("ECDSA P-384", EC_group_p384(), selected) && |
| 864 | SpeedECDSACurve("ECDSA P-521", EC_group_p521(), selected); |
| 865 | } |
| 866 |
| 867 | static bool Speed25519(const std::string &selected) { |
| 868 | if (!selected.empty() && selected.find("25519") == std::string::npos) { |
| 869 | return true; |
| 870 | } |
| 871 |
| 872 | TimeResults results; |
| 873 | if (!TimeFunctionParallel(&results, []() -> bool { |
| 874 | uint8_t public_key[32], private_key[64]; |
| 875 | ED25519_keypair(public_key, private_key); |
| 876 | return true; |
| 877 | })) { |
| 878 | return false; |
| 879 | } |
| 880 |
| 881 | results.Print("Ed25519 key generation"); |
| 882 |
| 883 | uint8_t public_key[32], private_key[64]; |
| 884 | ED25519_keypair(public_key, private_key); |
| 885 | static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5}; |
| 886 |
| 887 | if (!TimeFunctionParallel(&results, [&private_key]() -> bool { |
| 888 | uint8_t out[64]; |
| 889 | return ED25519_sign(out, kMessage, sizeof(kMessage), private_key) == 1; |
| 890 | })) { |
| 891 | return false; |
| 892 | } |
| 893 |
| 894 | results.Print("Ed25519 signing"); |
| 895 |
| 896 | uint8_t signature[64]; |
| 897 | if (!ED25519_sign(signature, kMessage, sizeof(kMessage), private_key)) { |
| 898 | return false; |
| 899 | } |
| 900 |
| 901 | if (!TimeFunctionParallel(&results, [&public_key, &signature]() -> bool { |
| 902 | return ED25519_verify(kMessage, sizeof(kMessage), signature, |
| 903 | public_key) == 1; |
| 904 | })) { |
| 905 | fprintf(stderr, "Ed25519 verify failed.\n"); |
| 906 | return false; |
| 907 | } |
| 908 |
| 909 | results.Print("Ed25519 verify"); |
| 910 |
| 911 | if (!TimeFunctionParallel(&results, []() -> bool { |
| 912 | uint8_t out[32], in[32]; |
| 913 | OPENSSL_memset(in, 0, sizeof(in)); |
| 914 | X25519_public_from_private(out, in); |
| 915 | return true; |
| 916 | })) { |
| 917 | fprintf(stderr, "Curve25519 base-point multiplication failed.\n"); |
| 918 | return false; |
| 919 | } |
| 920 |
| 921 | results.Print("Curve25519 base-point multiplication"); |
| 922 |
| 923 | if (!TimeFunctionParallel(&results, []() -> bool { |
| 924 | uint8_t out[32], in1[32], in2[32]; |
| 925 | OPENSSL_memset(in1, 0, sizeof(in1)); |
| 926 | OPENSSL_memset(in2, 0, sizeof(in2)); |
| 927 | in1[0] = 1; |
| 928 | in2[0] = 9; |
| 929 | return X25519(out, in1, in2) == 1; |
| 930 | })) { |
| 931 | fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n"); |
| 932 | return false; |
| 933 | } |
| 934 |
| 935 | results.Print("Curve25519 arbitrary point multiplication"); |
| 936 |
| 937 | return true; |
| 938 | } |
| 939 |
| 940 | static bool SpeedSPAKE2(const std::string &selected) { |
| 941 | if (!selected.empty() && selected.find("SPAKE2") == std::string::npos) { |
| 942 | return true; |
| 943 | } |
| 944 |
| 945 | TimeResults results; |
| 946 |
| 947 | static const uint8_t kAliceName[] = {'A'}; |
| 948 | static const uint8_t kBobName[] = {'B'}; |
| 949 | static const uint8_t kPassword[] = "password"; |
| 950 | bssl::UniquePtr<SPAKE2_CTX> alice( |
| 951 | SPAKE2_CTX_new(spake2_role_alice, kAliceName, sizeof(kAliceName), |
| 952 | kBobName, sizeof(kBobName))); |
| 953 | uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE]; |
| 954 | size_t alice_msg_len; |
| 955 |
| 956 | if (!SPAKE2_generate_msg(alice.get(), alice_msg, &alice_msg_len, |
| 957 | sizeof(alice_msg), kPassword, sizeof(kPassword))) { |
| 958 | fprintf(stderr, "SPAKE2_generate_msg failed.\n"); |
| 959 | return false; |
| 960 | } |
| 961 |
| 962 | if (!TimeFunctionParallel(&results, [&alice_msg, alice_msg_len]() -> bool { |
| 963 | bssl::UniquePtr<SPAKE2_CTX> bob( |
| 964 | SPAKE2_CTX_new(spake2_role_bob, kBobName, sizeof(kBobName), |
| 965 | kAliceName, sizeof(kAliceName))); |
| 966 | uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE], bob_key[64]; |
| 967 | size_t bob_msg_len, bob_key_len; |
| 968 | if (!SPAKE2_generate_msg(bob.get(), bob_msg, &bob_msg_len, |
| 969 | sizeof(bob_msg), kPassword, |
| 970 | sizeof(kPassword)) || |
| 971 | !SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len, |
| 972 | sizeof(bob_key), alice_msg, alice_msg_len)) { |
| 973 | return false; |
| 974 | } |
| 975 |
| 976 | return true; |
| 977 | })) { |
| 978 | fprintf(stderr, "SPAKE2 failed.\n"); |
| 979 | } |
| 980 |
| 981 | results.Print("SPAKE2 over Ed25519"); |
| 982 |
| 983 | return true; |
| 984 | } |
| 985 |
| 986 | static bool SpeedScrypt(const std::string &selected) { |
| 987 | if (!selected.empty() && selected.find("scrypt") == std::string::npos) { |
| 988 | return true; |
| 989 | } |
| 990 |
| 991 | TimeResults results; |
| 992 |
| 993 | static const char kPassword[] = "password"; |
| 994 | static const uint8_t kSalt[] = "NaCl"; |
| 995 |
| 996 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 997 | uint8_t out[64]; |
| 998 | return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt, |
| 999 | sizeof(kSalt) - 1, 1024, 8, 16, 0 /* max_mem */, |
| 1000 | out, sizeof(out)); |
| 1001 | })) { |
| 1002 | fprintf(stderr, "scrypt failed.\n"); |
| 1003 | return false; |
| 1004 | } |
| 1005 | results.Print("scrypt (N = 1024, r = 8, p = 16)"); |
| 1006 |
| 1007 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1008 | uint8_t out[64]; |
| 1009 | return !!EVP_PBE_scrypt(kPassword, sizeof(kPassword) - 1, kSalt, |
| 1010 | sizeof(kSalt) - 1, 16384, 8, 1, 0 /* max_mem */, |
| 1011 | out, sizeof(out)); |
| 1012 | })) { |
| 1013 | fprintf(stderr, "scrypt failed.\n"); |
| 1014 | return false; |
| 1015 | } |
| 1016 | results.Print("scrypt (N = 16384, r = 8, p = 1)"); |
| 1017 |
| 1018 | return true; |
| 1019 | } |
| 1020 |
| 1021 | static bool SpeedHRSS(const std::string &selected) { |
| 1022 | if (!selected.empty() && selected != "HRSS") { |
| 1023 | return true; |
| 1024 | } |
| 1025 |
| 1026 | TimeResults results; |
| 1027 |
| 1028 | if (!TimeFunctionParallel(&results, []() -> bool { |
| 1029 | struct HRSS_public_key pub; |
| 1030 | struct HRSS_private_key priv; |
| 1031 | uint8_t entropy[HRSS_GENERATE_KEY_BYTES]; |
| 1032 | RAND_bytes(entropy, sizeof(entropy)); |
| 1033 | return HRSS_generate_key(&pub, &priv, entropy); |
| 1034 | })) { |
| 1035 | fprintf(stderr, "Failed to time HRSS_generate_key.\n"); |
| 1036 | return false; |
| 1037 | } |
| 1038 |
| 1039 | results.Print("HRSS generate"); |
| 1040 |
| 1041 | struct HRSS_public_key pub; |
| 1042 | struct HRSS_private_key priv; |
| 1043 | uint8_t key_entropy[HRSS_GENERATE_KEY_BYTES]; |
| 1044 | RAND_bytes(key_entropy, sizeof(key_entropy)); |
| 1045 | if (!HRSS_generate_key(&pub, &priv, key_entropy)) { |
| 1046 | return false; |
| 1047 | } |
| 1048 |
| 1049 | if (!TimeFunctionParallel(&results, [&pub]() -> bool { |
| 1050 | uint8_t entropy[HRSS_ENCAP_BYTES]; |
| 1051 | uint8_t shared_key[HRSS_KEY_BYTES]; |
| 1052 | uint8_t ciphertext[HRSS_CIPHERTEXT_BYTES]; |
| 1053 | RAND_bytes(entropy, sizeof(entropy)); |
| 1054 | return HRSS_encap(ciphertext, shared_key, &pub, entropy); |
| 1055 | })) { |
| 1056 | fprintf(stderr, "Failed to time HRSS_encap.\n"); |
| 1057 | return false; |
| 1058 | } |
| 1059 | results.Print("HRSS encap"); |
| 1060 |
| 1061 | uint8_t entropy[HRSS_ENCAP_BYTES]; |
| 1062 | uint8_t shared_key[HRSS_KEY_BYTES]; |
| 1063 | uint8_t ciphertext[HRSS_CIPHERTEXT_BYTES]; |
| 1064 | RAND_bytes(entropy, sizeof(entropy)); |
| 1065 | if (!HRSS_encap(ciphertext, shared_key, &pub, entropy)) { |
| 1066 | return false; |
| 1067 | } |
| 1068 |
| 1069 | if (!TimeFunctionParallel(&results, [&priv, &ciphertext]() -> bool { |
| 1070 | uint8_t shared_key2[HRSS_KEY_BYTES]; |
| 1071 | return HRSS_decap(shared_key2, &priv, ciphertext, sizeof(ciphertext)); |
| 1072 | })) { |
| 1073 | fprintf(stderr, "Failed to time HRSS_encap.\n"); |
| 1074 | return false; |
| 1075 | } |
| 1076 |
| 1077 | results.Print("HRSS decap"); |
| 1078 |
| 1079 | return true; |
| 1080 | } |
| 1081 |
| 1082 | static bool SpeedKyber(const std::string &selected) { |
| 1083 | if (!selected.empty() && selected != "Kyber") { |
| 1084 | return true; |
| 1085 | } |
| 1086 |
| 1087 | TimeResults results; |
| 1088 |
| 1089 | uint8_t ciphertext[KYBER_CIPHERTEXT_BYTES]; |
| 1090 | // This ciphertext is nonsense, but Kyber decap is constant-time so, for the |
| 1091 | // purposes of timing, it's fine. |
| 1092 | memset(ciphertext, 42, sizeof(ciphertext)); |
| 1093 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1094 | KYBER_private_key priv; |
| 1095 | uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES]; |
| 1096 | KYBER_generate_key(encoded_public_key, &priv); |
| 1097 | uint8_t shared_secret[KYBER_SHARED_SECRET_BYTES]; |
| 1098 | KYBER_decap(shared_secret, ciphertext, &priv); |
| 1099 | return true; |
| 1100 | })) { |
| 1101 | fprintf(stderr, "Failed to time KYBER_generate_key + KYBER_decap.\n"); |
| 1102 | return false; |
| 1103 | } |
| 1104 |
| 1105 | results.Print("Kyber generate + decap"); |
| 1106 |
| 1107 | KYBER_private_key priv; |
| 1108 | uint8_t encoded_public_key[KYBER_PUBLIC_KEY_BYTES]; |
| 1109 | KYBER_generate_key(encoded_public_key, &priv); |
| 1110 | KYBER_public_key pub; |
| 1111 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1112 | CBS encoded_public_key_cbs; |
| 1113 | CBS_init(&encoded_public_key_cbs, encoded_public_key, |
| 1114 | sizeof(encoded_public_key)); |
| 1115 | if (!KYBER_parse_public_key(&pub, &encoded_public_key_cbs)) { |
| 1116 | return false; |
| 1117 | } |
| 1118 | uint8_t shared_secret[KYBER_SHARED_SECRET_BYTES]; |
| 1119 | KYBER_encap(ciphertext, shared_secret, &pub); |
| 1120 | return true; |
| 1121 | })) { |
| 1122 | fprintf(stderr, "Failed to time KYBER_encap.\n"); |
| 1123 | return false; |
| 1124 | } |
| 1125 |
| 1126 | results.Print("Kyber parse + encap"); |
| 1127 |
| 1128 | return true; |
| 1129 | } |
| 1130 |
| 1131 | static bool SpeedMLDSA(const std::string &selected) { |
| 1132 | if (!selected.empty() && selected != "ML-DSA") { |
| 1133 | return true; |
| 1134 | } |
| 1135 |
| 1136 | TimeResults results; |
| 1137 |
| 1138 | auto encoded_public_key = |
| 1139 | std::make_unique<uint8_t[]>(MLDSA65_PUBLIC_KEY_BYTES); |
| 1140 | auto priv = std::make_unique<MLDSA65_private_key>(); |
| 1141 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1142 | uint8_t seed[MLDSA_SEED_BYTES]; |
| 1143 | if (!MLDSA65_generate_key(encoded_public_key.get(), seed, priv.get())) { |
| 1144 | fprintf(stderr, "Failure in MLDSA65_generate_key.\n"); |
| 1145 | return false; |
| 1146 | } |
| 1147 | return true; |
| 1148 | })) { |
| 1149 | fprintf(stderr, "Failed to time MLDSA65_generate_key.\n"); |
| 1150 | return false; |
| 1151 | } |
| 1152 |
| 1153 | results.Print("MLDSA key generation"); |
| 1154 |
| 1155 | const char *message = "Hello world"; |
| 1156 | size_t message_len = strlen(message); |
| 1157 | auto out_encoded_signature = |
| 1158 | std::make_unique<uint8_t[]>(MLDSA65_SIGNATURE_BYTES); |
| 1159 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1160 | if (!MLDSA65_sign(out_encoded_signature.get(), priv.get(), |
| 1161 | (const uint8_t *)message, message_len, nullptr, 0)) { |
| 1162 | fprintf(stderr, "Failure in MLDSA65_sign.\n"); |
| 1163 | return false; |
| 1164 | } |
| 1165 | return true; |
| 1166 | })) { |
| 1167 | fprintf(stderr, "Failed to time MLDSA65_sign.\n"); |
| 1168 | return false; |
| 1169 | } |
| 1170 |
| 1171 | results.Print("MLDSA sign (randomized)"); |
| 1172 |
| 1173 | auto pub = std::make_unique<MLDSA65_public_key>(); |
| 1174 |
| 1175 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1176 | CBS cbs; |
| 1177 | CBS_init(&cbs, encoded_public_key.get(), MLDSA65_PUBLIC_KEY_BYTES); |
| 1178 | if (!MLDSA65_parse_public_key(pub.get(), &cbs)) { |
| 1179 | fprintf(stderr, "Failure in MLDSA65_parse_public_key.\n"); |
| 1180 | return false; |
| 1181 | } |
| 1182 | return true; |
| 1183 | })) { |
| 1184 | fprintf(stderr, "Failed to time MLDSA65_parse_public_key.\n"); |
| 1185 | return false; |
| 1186 | } |
| 1187 |
| 1188 | results.Print("MLDSA parse (valid) public key"); |
| 1189 |
| 1190 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1191 | if (!MLDSA65_verify(pub.get(), out_encoded_signature.get(), |
| 1192 | MLDSA65_SIGNATURE_BYTES, (const uint8_t *)message, |
| 1193 | message_len, nullptr, 0)) { |
| 1194 | fprintf(stderr, "Failed to verify MLDSA signature.\n"); |
| 1195 | return false; |
| 1196 | } |
| 1197 | return true; |
| 1198 | })) { |
| 1199 | fprintf(stderr, "Failed to time MLDSA65_verify.\n"); |
| 1200 | return false; |
| 1201 | } |
| 1202 |
| 1203 | results.Print("MLDSA verify (valid signature)"); |
| 1204 |
| 1205 | out_encoded_signature[42] ^= 0x42; |
| 1206 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1207 | if (MLDSA65_verify(pub.get(), out_encoded_signature.get(), |
| 1208 | MLDSA65_SIGNATURE_BYTES, (const uint8_t *)message, |
| 1209 | message_len, nullptr, 0)) { |
| 1210 | fprintf(stderr, "MLDSA signature unexpectedly verified.\n"); |
| 1211 | return false; |
| 1212 | } |
| 1213 | return true; |
| 1214 | })) { |
| 1215 | fprintf(stderr, "Failed to time MLDSA65_verify.\n"); |
| 1216 | return false; |
| 1217 | } |
| 1218 |
| 1219 | results.Print("MLDSA verify (invalid signature)"); |
| 1220 |
| 1221 | return true; |
| 1222 | } |
| 1223 |
| 1224 | static bool SpeedMLKEM(const std::string &selected) { |
| 1225 | if (!selected.empty() && selected != "ML-KEM-768") { |
| 1226 | return true; |
| 1227 | } |
| 1228 |
| 1229 | TimeResults results; |
| 1230 |
| 1231 | uint8_t ciphertext[MLKEM768_CIPHERTEXT_BYTES]; |
| 1232 | // This ciphertext is nonsense, but decap is constant-time so, for the |
| 1233 | // purposes of timing, it's fine. |
| 1234 | memset(ciphertext, 42, sizeof(ciphertext)); |
| 1235 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1236 | MLKEM768_private_key priv; |
| 1237 | uint8_t encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES]; |
| 1238 | MLKEM768_generate_key(encoded_public_key, nullptr, &priv); |
| 1239 | uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES]; |
| 1240 | MLKEM768_decap(shared_secret, ciphertext, sizeof(ciphertext), &priv); |
| 1241 | return true; |
| 1242 | })) { |
| 1243 | fprintf(stderr, "Failed to time MLKEM768_generate_key + MLKEM768_decap.\n"); |
| 1244 | return false; |
| 1245 | } |
| 1246 |
| 1247 | results.Print("ML-KEM-768 generate + decap"); |
| 1248 |
| 1249 | MLKEM768_private_key priv; |
| 1250 | uint8_t encoded_public_key[MLKEM768_PUBLIC_KEY_BYTES]; |
| 1251 | MLKEM768_generate_key(encoded_public_key, nullptr, &priv); |
| 1252 | MLKEM768_public_key pub; |
| 1253 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1254 | CBS encoded_public_key_cbs; |
| 1255 | CBS_init(&encoded_public_key_cbs, encoded_public_key, |
| 1256 | sizeof(encoded_public_key)); |
| 1257 | if (!MLKEM768_parse_public_key(&pub, &encoded_public_key_cbs)) { |
| 1258 | return false; |
| 1259 | } |
| 1260 | uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES]; |
| 1261 | MLKEM768_encap(ciphertext, shared_secret, &pub); |
| 1262 | return true; |
| 1263 | })) { |
| 1264 | fprintf(stderr, "Failed to time MLKEM768_encap.\n"); |
| 1265 | return false; |
| 1266 | } |
| 1267 |
| 1268 | results.Print("ML-KEM-768 parse + encap"); |
| 1269 |
| 1270 | return true; |
| 1271 | } |
| 1272 |
| 1273 | static bool SpeedMLKEM1024(const std::string &selected) { |
| 1274 | if (!selected.empty() && selected != "ML-KEM-1024") { |
| 1275 | return true; |
| 1276 | } |
| 1277 |
| 1278 | TimeResults results; |
| 1279 |
| 1280 | uint8_t ciphertext[MLKEM1024_CIPHERTEXT_BYTES]; |
| 1281 | auto priv = std::make_unique<MLKEM1024_private_key>(); |
| 1282 | // This ciphertext is nonsense, but decap is constant-time so, for the |
| 1283 | // purposes of timing, it's fine. |
| 1284 | memset(ciphertext, 42, sizeof(ciphertext)); |
| 1285 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1286 | uint8_t encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES]; |
| 1287 | MLKEM1024_generate_key(encoded_public_key, nullptr, priv.get()); |
| 1288 | uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES]; |
| 1289 | MLKEM1024_decap(shared_secret, ciphertext, sizeof(ciphertext), |
| 1290 | priv.get()); |
| 1291 | return true; |
| 1292 | })) { |
| 1293 | fprintf(stderr, "Failed to time MLKEM768_generate_key + MLKEM768_decap.\n"); |
| 1294 | return false; |
| 1295 | } |
| 1296 |
| 1297 | results.Print("ML-KEM-1024 generate + decap"); |
| 1298 |
| 1299 | uint8_t encoded_public_key[MLKEM1024_PUBLIC_KEY_BYTES]; |
| 1300 | MLKEM1024_generate_key(encoded_public_key, nullptr, priv.get()); |
| 1301 | MLKEM1024_public_key pub; |
| 1302 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1303 | CBS encoded_public_key_cbs; |
| 1304 | CBS_init(&encoded_public_key_cbs, encoded_public_key, |
| 1305 | sizeof(encoded_public_key)); |
| 1306 | if (!MLKEM1024_parse_public_key(&pub, &encoded_public_key_cbs)) { |
| 1307 | return false; |
| 1308 | } |
| 1309 | uint8_t shared_secret[MLKEM_SHARED_SECRET_BYTES]; |
| 1310 | MLKEM1024_encap(ciphertext, shared_secret, &pub); |
| 1311 | return true; |
| 1312 | })) { |
| 1313 | fprintf(stderr, "Failed to time MLKEM768_encap.\n"); |
| 1314 | return false; |
| 1315 | } |
| 1316 |
| 1317 | results.Print("ML-KEM-1024 parse + encap"); |
| 1318 |
| 1319 | return true; |
| 1320 | } |
| 1321 |
| 1322 | static bool SpeedSLHDSA(const std::string &selected) { |
| 1323 | if (!selected.empty() && selected.find("SLH-DSA") == std::string::npos) { |
| 1324 | return true; |
| 1325 | } |
| 1326 |
| 1327 | TimeResults results; |
| 1328 | if (!TimeFunctionParallel(&results, []() -> bool { |
| 1329 | uint8_t public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], |
| 1330 | private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]; |
| 1331 | SLHDSA_SHA2_128S_generate_key(public_key, private_key); |
| 1332 | return true; |
| 1333 | })) { |
| 1334 | return false; |
| 1335 | } |
| 1336 |
| 1337 | results.Print("SLHDSA-SHA2-128s key generation"); |
| 1338 |
| 1339 | uint8_t public_key[SLHDSA_SHA2_128S_PUBLIC_KEY_BYTES], |
| 1340 | private_key[SLHDSA_SHA2_128S_PRIVATE_KEY_BYTES]; |
| 1341 | SLHDSA_SHA2_128S_generate_key(public_key, private_key); |
| 1342 | static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5}; |
| 1343 |
| 1344 | if (!TimeFunctionParallel(&results, [&private_key]() -> bool { |
| 1345 | uint8_t out[SLHDSA_SHA2_128S_SIGNATURE_BYTES]; |
| 1346 | SLHDSA_SHA2_128S_sign(out, private_key, kMessage, sizeof(kMessage), |
| 1347 | nullptr, 0); |
| 1348 | return true; |
| 1349 | })) { |
| 1350 | return false; |
| 1351 | } |
| 1352 |
| 1353 | results.Print("SLHDSA-SHA2-128s signing"); |
| 1354 |
| 1355 | uint8_t signature[SLHDSA_SHA2_128S_SIGNATURE_BYTES]; |
| 1356 | SLHDSA_SHA2_128S_sign(signature, private_key, kMessage, sizeof(kMessage), |
| 1357 | nullptr, 0); |
| 1358 |
| 1359 | if (!TimeFunctionParallel(&results, [&public_key, &signature]() -> bool { |
| 1360 | return SLHDSA_SHA2_128S_verify(signature, sizeof(signature), public_key, |
| 1361 | kMessage, sizeof(kMessage), nullptr, |
| 1362 | 0) == 1; |
| 1363 | })) { |
| 1364 | fprintf(stderr, "SLHDSA-SHA2-128s verify failed.\n"); |
| 1365 | return false; |
| 1366 | } |
| 1367 |
| 1368 | results.Print("SLHDSA-SHA2-128s verify"); |
| 1369 |
| 1370 | return true; |
| 1371 | } |
| 1372 |
| 1373 | static bool SpeedHashToCurve(const std::string &selected) { |
| 1374 | if (!selected.empty() && selected.find("hashtocurve") == std::string::npos) { |
| 1375 | return true; |
| 1376 | } |
| 1377 |
| 1378 | uint8_t input[64]; |
| 1379 | RAND_bytes(input, sizeof(input)); |
| 1380 |
| 1381 | static const uint8_t kLabel[] = "label"; |
| 1382 |
| 1383 | TimeResults results; |
| 1384 | { |
| 1385 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1386 | EC_JACOBIAN out; |
| 1387 | return ec_hash_to_curve_p256_xmd_sha256_sswu(EC_group_p256(), &out, |
| 1388 | kLabel, sizeof(kLabel), |
| 1389 | input, sizeof(input)); |
| 1390 | })) { |
| 1391 | fprintf(stderr, "hash-to-curve failed.\n"); |
| 1392 | return false; |
| 1393 | } |
| 1394 | results.Print("hash-to-curve P256_XMD:SHA-256_SSWU_RO_"); |
| 1395 |
| 1396 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1397 | EC_JACOBIAN out; |
| 1398 | return ec_hash_to_curve_p384_xmd_sha384_sswu(EC_group_p384(), &out, |
| 1399 | kLabel, sizeof(kLabel), |
| 1400 | input, sizeof(input)); |
| 1401 | })) { |
| 1402 | fprintf(stderr, "hash-to-curve failed.\n"); |
| 1403 | return false; |
| 1404 | } |
| 1405 | results.Print("hash-to-curve P384_XMD:SHA-384_SSWU_RO_"); |
| 1406 |
| 1407 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1408 | EC_SCALAR out; |
| 1409 | return ec_hash_to_scalar_p384_xmd_sha512_draft07( |
| 1410 | EC_group_p384(), &out, kLabel, sizeof(kLabel), input, |
| 1411 | sizeof(input)); |
| 1412 | })) { |
| 1413 | fprintf(stderr, "hash-to-scalar failed.\n"); |
| 1414 | return false; |
| 1415 | } |
| 1416 | results.Print("hash-to-scalar P384_XMD:SHA-512"); |
| 1417 | } |
| 1418 |
| 1419 | return true; |
| 1420 | } |
| 1421 |
| 1422 | static bool SpeedBase64(const std::string &selected) { |
| 1423 | if (!selected.empty() && selected.find("base64") == std::string::npos) { |
| 1424 | return true; |
| 1425 | } |
| 1426 |
| 1427 | static const char kInput[] = |
| 1428 | "MIIDtTCCAp2gAwIBAgIJALW2IrlaBKUhMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV" |
| 1429 | "BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX" |
| 1430 | "aWRnaXRzIFB0eSBMdGQwHhcNMTYwNzA5MDQzODA5WhcNMTYwODA4MDQzODA5WjBF" |
| 1431 | "MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50" |
| 1432 | "ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB" |
| 1433 | "CgKCAQEAugvahBkSAUF1fC49vb1bvlPrcl80kop1iLpiuYoz4Qptwy57+EWssZBc" |
| 1434 | "HprZ5BkWf6PeGZ7F5AX1PyJbGHZLqvMCvViP6pd4MFox/igESISEHEixoiXCzepB" |
| 1435 | "rhtp5UQSjHD4D4hKtgdMgVxX+LRtwgW3mnu/vBu7rzpr/DS8io99p3lqZ1Aky+aN" |
| 1436 | "lcMj6MYy8U+YFEevb/V0lRY9oqwmW7BHnXikm/vi6sjIS350U8zb/mRzYeIs2R65" |
| 1437 | "LUduTL50+UMgat9ocewI2dv8aO9Dph+8NdGtg8LFYyTTHcUxJoMr1PTOgnmET19W" |
| 1438 | "JH4PrFwk7ZE1QJQQ1L4iKmPeQistuQIDAQABo4GnMIGkMB0GA1UdDgQWBBT5m6Vv" |
| 1439 | "zYjVYHG30iBE+j2XDhUE8jB1BgNVHSMEbjBsgBT5m6VvzYjVYHG30iBE+j2XDhUE" |
| 1440 | "8qFJpEcwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNV" |
| 1441 | "BAoTGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZIIJALW2IrlaBKUhMAwGA1UdEwQF" |
| 1442 | "MAMBAf8wDQYJKoZIhvcNAQELBQADggEBAD7Jg68SArYWlcoHfZAB90Pmyrt5H6D8" |
| 1443 | "LRi+W2Ri1fBNxREELnezWJ2scjl4UMcsKYp4Pi950gVN+62IgrImcCNvtb5I1Cfy" |
| 1444 | "/MNNur9ffas6X334D0hYVIQTePyFk3umI+2mJQrtZZyMPIKSY/sYGQHhGGX6wGK+" |
| 1445 | "GO/og0PQk/Vu6D+GU2XRnDV0YZg1lsAsHd21XryK6fDmNkEMwbIWrts4xc7scRrG" |
| 1446 | "HWy+iMf6/7p/Ak/SIicM4XSwmlQ8pPxAZPr+E2LoVd9pMpWUwpW2UbtO5wsGTrY5" |
| 1447 | "sO45tFNN/y+jtUheB1C2ijObG/tXELaiyCdM+S/waeuv0MXtI4xnn1A="; |
| 1448 |
| 1449 | TimeResults results; |
| 1450 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1451 | uint8_t out[sizeof(kInput)]; |
| 1452 | size_t len; |
| 1453 | return EVP_DecodeBase64(out, &len, sizeof(out), |
| 1454 | reinterpret_cast<const uint8_t *>(kInput), |
| 1455 | strlen(kInput)); |
| 1456 | })) { |
| 1457 | fprintf(stderr, "base64 decode failed.\n"); |
| 1458 | return false; |
| 1459 | } |
| 1460 | results.PrintWithBytes("base64 decode", strlen(kInput)); |
| 1461 | return true; |
| 1462 | } |
| 1463 |
| 1464 | static bool SpeedSipHash(const std::string &selected) { |
| 1465 | if (!selected.empty() && selected.find("siphash") == std::string::npos) { |
| 1466 | return true; |
| 1467 | } |
| 1468 |
| 1469 | uint64_t key[2] = {0}; |
| 1470 | for (size_t len : g_chunk_lengths) { |
| 1471 | std::vector<uint8_t> input(len); |
| 1472 | TimeResults results; |
| 1473 | if (!TimeFunctionParallel(&results, [&]() -> bool { |
| 1474 | SIPHASH_24(key, input.data(), input.size()); |
| 1475 | return true; |
| 1476 | })) { |
| 1477 | fprintf(stderr, "SIPHASH_24 failed.\n"); |
| 1478 | ERR_print_errors_fp(stderr); |
| 1479 | return false; |
| 1480 | } |
| 1481 | results.PrintWithBytes("SipHash-2-4" + ChunkLenSuffix(len), len); |
| 1482 | } |
| 1483 |
| 1484 | return true; |
| 1485 | } |
| 1486 |
| 1487 | static TRUST_TOKEN_PRETOKEN *trust_token_pretoken_dup( |
| 1488 | const TRUST_TOKEN_PRETOKEN *in) { |
| 1489 | return static_cast<TRUST_TOKEN_PRETOKEN *>( |
| 1490 | OPENSSL_memdup(in, sizeof(TRUST_TOKEN_PRETOKEN))); |
| 1491 | } |
| 1492 |
| 1493 | static bool SpeedTrustToken(std::string name, const TRUST_TOKEN_METHOD *method, |
| 1494 | size_t batchsize, const std::string &selected) { |
| 1495 | if (!selected.empty() && selected.find("trusttoken") == std::string::npos) { |
| 1496 | return true; |
| 1497 | } |
| 1498 |
| 1499 | TimeResults results; |
| 1500 | if (!TimeFunction(&results, [&]() -> bool { |
| 1501 | uint8_t priv_key[TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE]; |
| 1502 | uint8_t pub_key[TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE]; |
| 1503 | size_t priv_key_len, pub_key_len; |
| 1504 | return TRUST_TOKEN_generate_key( |
| 1505 | method, priv_key, &priv_key_len, TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE, |
| 1506 | pub_key, &pub_key_len, TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE, 0); |
| 1507 | })) { |
| 1508 | fprintf(stderr, "TRUST_TOKEN_generate_key failed.\n"); |
| 1509 | return false; |
| 1510 | } |
| 1511 | results.Print(name + " generate_key"); |
| 1512 |
| 1513 | bssl::UniquePtr<TRUST_TOKEN_CLIENT> client( |
| 1514 | TRUST_TOKEN_CLIENT_new(method, batchsize)); |
| 1515 | bssl::UniquePtr<TRUST_TOKEN_ISSUER> issuer( |
| 1516 | TRUST_TOKEN_ISSUER_new(method, batchsize)); |
| 1517 | uint8_t priv_key[TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE]; |
| 1518 | uint8_t pub_key[TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE]; |
| 1519 | size_t priv_key_len, pub_key_len, key_index; |
| 1520 | if (!client || !issuer || |
| 1521 | !TRUST_TOKEN_generate_key( |
| 1522 | method, priv_key, &priv_key_len, TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE, |
| 1523 | pub_key, &pub_key_len, TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE, 0) || |
| 1524 | !TRUST_TOKEN_CLIENT_add_key(client.get(), &key_index, pub_key, |
| 1525 | pub_key_len) || |
| 1526 | !TRUST_TOKEN_ISSUER_add_key(issuer.get(), priv_key, priv_key_len)) { |
| 1527 | fprintf(stderr, "failed to generate trust token key.\n"); |
| 1528 | return false; |
| 1529 | } |
| 1530 |
| 1531 | uint8_t public_key[32], private_key[64]; |
| 1532 | ED25519_keypair(public_key, private_key); |
| 1533 | bssl::UniquePtr<EVP_PKEY> priv( |
| 1534 | EVP_PKEY_new_raw_private_key(EVP_PKEY_ED25519, nullptr, private_key, 32)); |
| 1535 | bssl::UniquePtr<EVP_PKEY> pub( |
| 1536 | EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, nullptr, public_key, 32)); |
| 1537 | if (!priv || !pub) { |
| 1538 | fprintf(stderr, "failed to generate trust token SRR key.\n"); |
| 1539 | return false; |
| 1540 | } |
| 1541 |
| 1542 | TRUST_TOKEN_CLIENT_set_srr_key(client.get(), pub.get()); |
| 1543 | TRUST_TOKEN_ISSUER_set_srr_key(issuer.get(), priv.get()); |
| 1544 | uint8_t metadata_key[32]; |
| 1545 | RAND_bytes(metadata_key, sizeof(metadata_key)); |
| 1546 | if (!TRUST_TOKEN_ISSUER_set_metadata_key(issuer.get(), metadata_key, |
| 1547 | sizeof(metadata_key))) { |
| 1548 | fprintf(stderr, "failed to generate trust token metadata key.\n"); |
| 1549 | return false; |
| 1550 | } |
| 1551 |
| 1552 | if (!TimeFunction(&results, [&]() -> bool { |
| 1553 | uint8_t *issue_msg = NULL; |
| 1554 | size_t msg_len; |
| 1555 | int ok = TRUST_TOKEN_CLIENT_begin_issuance(client.get(), &issue_msg, |
| 1556 | &msg_len, batchsize); |
| 1557 | OPENSSL_free(issue_msg); |
| 1558 | // Clear pretokens. |
| 1559 | sk_TRUST_TOKEN_PRETOKEN_pop_free(client->pretokens, |
| 1560 | TRUST_TOKEN_PRETOKEN_free); |
| 1561 | client->pretokens = sk_TRUST_TOKEN_PRETOKEN_new_null(); |
| 1562 | return ok; |
| 1563 | })) { |
| 1564 | fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_issuance failed.\n"); |
| 1565 | return false; |
| 1566 | } |
| 1567 | results.Print(name + " begin_issuance"); |
| 1568 |
| 1569 | uint8_t *issue_msg = NULL; |
| 1570 | size_t msg_len; |
| 1571 | if (!TRUST_TOKEN_CLIENT_begin_issuance(client.get(), &issue_msg, &msg_len, |
| 1572 | batchsize)) { |
| 1573 | fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_issuance failed.\n"); |
| 1574 | return false; |
| 1575 | } |
| 1576 | bssl::UniquePtr<uint8_t> free_issue_msg(issue_msg); |
| 1577 |
| 1578 | bssl::UniquePtr<STACK_OF(TRUST_TOKEN_PRETOKEN)> pretokens( |
| 1579 | sk_TRUST_TOKEN_PRETOKEN_deep_copy(client->pretokens, |
| 1580 | trust_token_pretoken_dup, |
| 1581 | TRUST_TOKEN_PRETOKEN_free)); |
| 1582 |
| 1583 | if (!TimeFunction(&results, [&]() -> bool { |
| 1584 | uint8_t *issue_resp = NULL; |
| 1585 | size_t resp_len, tokens_issued; |
| 1586 | int ok = TRUST_TOKEN_ISSUER_issue(issuer.get(), &issue_resp, &resp_len, |
| 1587 | &tokens_issued, issue_msg, msg_len, |
| 1588 | /*public_metadata=*/0, |
| 1589 | /*private_metadata=*/0, |
| 1590 | /*max_issuance=*/batchsize); |
| 1591 | OPENSSL_free(issue_resp); |
| 1592 | return ok; |
| 1593 | })) { |
| 1594 | fprintf(stderr, "TRUST_TOKEN_ISSUER_issue failed.\n"); |
| 1595 | return false; |
| 1596 | } |
| 1597 | results.Print(name + " issue"); |
| 1598 |
| 1599 | uint8_t *issue_resp = NULL; |
| 1600 | size_t resp_len, tokens_issued; |
| 1601 | if (!TRUST_TOKEN_ISSUER_issue(issuer.get(), &issue_resp, &resp_len, |
| 1602 | &tokens_issued, issue_msg, msg_len, |
| 1603 | /*public_metadata=*/0, /*private_metadata=*/0, |
| 1604 | /*max_issuance=*/batchsize)) { |
| 1605 | fprintf(stderr, "TRUST_TOKEN_ISSUER_issue failed.\n"); |
| 1606 | return false; |
| 1607 | } |
| 1608 | bssl::UniquePtr<uint8_t> free_issue_resp(issue_resp); |
| 1609 |
| 1610 | if (!TimeFunction(&results, [&]() -> bool { |
| 1611 | size_t key_index2; |
| 1612 | bssl::UniquePtr<STACK_OF(TRUST_TOKEN)> tokens( |
| 1613 | TRUST_TOKEN_CLIENT_finish_issuance(client.get(), &key_index2, |
| 1614 | issue_resp, resp_len)); |
| 1615 |
| 1616 | // Reset pretokens. |
| 1617 | client->pretokens = sk_TRUST_TOKEN_PRETOKEN_deep_copy( |
| 1618 | pretokens.get(), trust_token_pretoken_dup, |
| 1619 | TRUST_TOKEN_PRETOKEN_free); |
| 1620 | return !!tokens; |
| 1621 | })) { |
| 1622 | fprintf(stderr, "TRUST_TOKEN_CLIENT_finish_issuance failed.\n"); |
| 1623 | return false; |
| 1624 | } |
| 1625 | results.Print(name + " finish_issuance"); |
| 1626 |
| 1627 | bssl::UniquePtr<STACK_OF(TRUST_TOKEN)> tokens( |
| 1628 | TRUST_TOKEN_CLIENT_finish_issuance(client.get(), &key_index, issue_resp, |
| 1629 | resp_len)); |
| 1630 | if (!tokens || sk_TRUST_TOKEN_num(tokens.get()) < 1) { |
| 1631 | fprintf(stderr, "TRUST_TOKEN_CLIENT_finish_issuance failed.\n"); |
| 1632 | return false; |
| 1633 | } |
| 1634 |
| 1635 | const TRUST_TOKEN *token = sk_TRUST_TOKEN_value(tokens.get(), 0); |
| 1636 |
| 1637 | const uint8_t kClientData[] = "\x70TEST CLIENT DATA"; |
| 1638 | uint64_t kRedemptionTime = 13374242; |
| 1639 |
| 1640 | if (!TimeFunction(&results, [&]() -> bool { |
| 1641 | uint8_t *redeem_msg = NULL; |
| 1642 | size_t redeem_msg_len; |
| 1643 | int ok = TRUST_TOKEN_CLIENT_begin_redemption( |
| 1644 | client.get(), &redeem_msg, &redeem_msg_len, token, kClientData, |
| 1645 | sizeof(kClientData) - 1, kRedemptionTime); |
| 1646 | OPENSSL_free(redeem_msg); |
| 1647 | return ok; |
| 1648 | })) { |
| 1649 | fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_redemption failed.\n"); |
| 1650 | return false; |
| 1651 | } |
| 1652 | results.Print(name + " begin_redemption"); |
| 1653 |
| 1654 | uint8_t *redeem_msg = NULL; |
| 1655 | size_t redeem_msg_len; |
| 1656 | if (!TRUST_TOKEN_CLIENT_begin_redemption( |
| 1657 | client.get(), &redeem_msg, &redeem_msg_len, token, kClientData, |
| 1658 | sizeof(kClientData) - 1, kRedemptionTime)) { |
| 1659 | fprintf(stderr, "TRUST_TOKEN_CLIENT_begin_redemption failed.\n"); |
| 1660 | return false; |
| 1661 | } |
| 1662 | bssl::UniquePtr<uint8_t> free_redeem_msg(redeem_msg); |
| 1663 |
| 1664 | if (!TimeFunction(&results, [&]() -> bool { |
| 1665 | uint32_t public_value; |
| 1666 | uint8_t private_value; |
| 1667 | TRUST_TOKEN *rtoken; |
| 1668 | uint8_t *client_data = NULL; |
| 1669 | size_t client_data_len; |
| 1670 | int ok = TRUST_TOKEN_ISSUER_redeem( |
| 1671 | issuer.get(), &public_value, &private_value, &rtoken, &client_data, |
| 1672 | &client_data_len, redeem_msg, redeem_msg_len); |
| 1673 | OPENSSL_free(client_data); |
| 1674 | TRUST_TOKEN_free(rtoken); |
| 1675 | return ok; |
| 1676 | })) { |
| 1677 | fprintf(stderr, "TRUST_TOKEN_ISSUER_redeem failed.\n"); |
| 1678 | return false; |
| 1679 | } |
| 1680 | results.Print(name + " redeem"); |
| 1681 |
| 1682 | uint32_t public_value; |
| 1683 | uint8_t private_value; |
| 1684 | TRUST_TOKEN *rtoken; |
| 1685 | uint8_t *client_data = NULL; |
| 1686 | size_t client_data_len; |
| 1687 | if (!TRUST_TOKEN_ISSUER_redeem(issuer.get(), &public_value, &private_value, |
| 1688 | &rtoken, &client_data, &client_data_len, |
| 1689 | redeem_msg, redeem_msg_len)) { |
| 1690 | fprintf(stderr, "TRUST_TOKEN_ISSUER_redeem failed.\n"); |
| 1691 | return false; |
| 1692 | } |
| 1693 | bssl::UniquePtr<uint8_t> free_client_data(client_data); |
| 1694 | bssl::UniquePtr<TRUST_TOKEN> free_rtoken(rtoken); |
| 1695 |
| 1696 | return true; |
| 1697 | } |
| 1698 |
| 1699 | #if defined(BORINGSSL_FIPS) |
| 1700 | static bool SpeedSelfTest(const std::string &selected) { |
| 1701 | if (!selected.empty() && selected.find("self-test") == std::string::npos) { |
| 1702 | return true; |
| 1703 | } |
| 1704 |
| 1705 | TimeResults results; |
| 1706 | if (!TimeFunction(&results, []() -> bool { return BORINGSSL_self_test(); })) { |
| 1707 | fprintf(stderr, "BORINGSSL_self_test faileid.\n"); |
| 1708 | ERR_print_errors_fp(stderr); |
| 1709 | return false; |
| 1710 | } |
| 1711 |
| 1712 | results.Print("self-test"); |
| 1713 | return true; |
| 1714 | } |
| 1715 | #endif |
| 1716 |
| 1717 | static const struct argument kArguments[] = { |
| 1718 | { |
| 1719 | "-filter", |
| 1720 | kOptionalArgument, |
| 1721 | "A filter on the speed tests to run", |
| 1722 | }, |
| 1723 | { |
| 1724 | "-timeout", |
| 1725 | kOptionalArgument, |
| 1726 | "The number of seconds to run each test for (default is 1)", |
| 1727 | }, |
| 1728 | { |
| 1729 | "-chunks", |
| 1730 | kOptionalArgument, |
| 1731 | "A comma-separated list of input sizes to run tests at (default is " |
| 1732 | "16,256,1350,8192,16384)", |
| 1733 | }, |
| 1734 | { |
| 1735 | "-json", |
| 1736 | kBooleanArgument, |
| 1737 | "If this flag is set, speed will print the output of each benchmark in " |
| 1738 | "JSON format as follows: \"{\"description\": " |
| 1739 | "\"descriptionOfOperation\", \"numCalls\": 1234, " |
| 1740 | "\"timeInMicroseconds\": 1234567, \"bytesPerCall\": 1234}\". When " |
| 1741 | "there is no information about the bytes per call for an operation, " |
| 1742 | "the JSON field for bytesPerCall will be omitted.", |
| 1743 | }, |
| 1744 | #if defined(OPENSSL_THREADS) |
| 1745 | { |
| 1746 | "-threads", |
| 1747 | kOptionalArgument, |
| 1748 | "The number of threads to benchmark in parallel (default is 1)", |
| 1749 | }, |
| 1750 | #endif |
| 1751 | { |
| 1752 | "", |
| 1753 | kOptionalArgument, |
| 1754 | "", |
| 1755 | }, |
| 1756 | }; |
| 1757 |
| 1758 | bool Speed(const std::vector<std::string> &args) { |
| 1759 | std::map<std::string, std::string> args_map; |
| 1760 | if (!ParseKeyValueArguments(&args_map, args, kArguments)) { |
| 1761 | PrintUsage(kArguments); |
| 1762 | return false; |
| 1763 | } |
| 1764 |
| 1765 | std::string selected; |
| 1766 | if (args_map.count("-filter") != 0) { |
| 1767 | selected = args_map["-filter"]; |
| 1768 | } |
| 1769 |
| 1770 | if (args_map.count("-json") != 0) { |
| 1771 | g_print_json = true; |
| 1772 | } |
| 1773 |
| 1774 | if (args_map.count("-timeout") != 0) { |
| 1775 | g_timeout_seconds = atoi(args_map["-timeout"].c_str()); |
| 1776 | } |
| 1777 |
| 1778 | #if defined(OPENSSL_THREADS) |
| 1779 | if (args_map.count("-threads") != 0) { |
| 1780 | g_threads = atoi(args_map["-threads"].c_str()); |
| 1781 | } |
| 1782 | #endif |
| 1783 |
| 1784 | if (args_map.count("-chunks") != 0) { |
| 1785 | g_chunk_lengths.clear(); |
| 1786 | const char *start = args_map["-chunks"].data(); |
| 1787 | const char *end = start + args_map["-chunks"].size(); |
| 1788 | while (start != end) { |
| 1789 | errno = 0; |
| 1790 | char *ptr; |
| 1791 | unsigned long long val = strtoull(start, &ptr, 10); |
| 1792 | if (ptr == start /* no numeric characters found */ || |
| 1793 | errno == ERANGE /* overflow */ || static_cast<size_t>(val) != val) { |
| 1794 | fprintf(stderr, "Error parsing -chunks argument\n"); |
| 1795 | return false; |
| 1796 | } |
| 1797 | g_chunk_lengths.push_back(static_cast<size_t>(val)); |
| 1798 | start = ptr; |
| 1799 | if (start != end) { |
| 1800 | if (*start != ',') { |
| 1801 | fprintf(stderr, "Error parsing -chunks argument\n"); |
| 1802 | return false; |
| 1803 | } |
| 1804 | start++; |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 |
| 1809 | // kTLSADLen is the number of bytes of additional data that TLS passes to |
| 1810 | // AEADs. |
| 1811 | static const size_t kTLSADLen = 13; |
| 1812 | // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs. |
| 1813 | // These are AEADs that weren't originally defined as AEADs, but which we use |
| 1814 | // via the AEAD interface. In order for that to work, they have some TLS |
| 1815 | // knowledge in them and construct a couple of the AD bytes internally. |
| 1816 | static const size_t kLegacyADLen = kTLSADLen - 2; |
| 1817 |
| 1818 | if (g_print_json) { |
| 1819 | puts("["); |
| 1820 | } |
| 1821 | if (!SpeedRSA(selected) || |
| 1822 | !SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) || |
| 1823 | !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) || |
| 1824 | !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen, |
| 1825 | selected) || |
| 1826 | !SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1", |
| 1827 | kLegacyADLen, selected) || |
| 1828 | !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", |
| 1829 | kLegacyADLen, selected) || |
| 1830 | !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", |
| 1831 | kLegacyADLen, selected) || |
| 1832 | !SpeedAEADOpen(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1", |
| 1833 | kLegacyADLen, selected) || |
| 1834 | !SpeedAEADOpen(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1", |
| 1835 | kLegacyADLen, selected) || |
| 1836 | !SpeedAEAD(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen, |
| 1837 | selected) || |
| 1838 | !SpeedAEAD(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen, |
| 1839 | selected) || |
| 1840 | !SpeedAEADOpen(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen, |
| 1841 | selected) || |
| 1842 | !SpeedAEADOpen(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen, |
| 1843 | selected) || |
| 1844 | !SpeedAEAD(EVP_aead_aes_128_ccm_bluetooth(), "AES-128-CCM-Bluetooth", |
| 1845 | kTLSADLen, selected) || |
| 1846 | !SpeedAESBlock("AES-128", 128, selected) || |
| 1847 | !SpeedAESBlock("AES-256", 256, selected) || |
| 1848 | !SpeedHash(EVP_sha1(), "SHA-1", selected) || |
| 1849 | !SpeedHash(EVP_sha256(), "SHA-256", selected) || |
| 1850 | !SpeedHash(EVP_sha512(), "SHA-512", selected) || |
| 1851 | !SpeedHash(EVP_blake2b256(), "BLAKE2b-256", selected) || |
| 1852 | !SpeedRandom(selected) || // |
| 1853 | !SpeedECDH(selected) || // |
| 1854 | !SpeedECDSA(selected) || // |
| 1855 | !Speed25519(selected) || // |
| 1856 | !SpeedSPAKE2(selected) || // |
| 1857 | !SpeedScrypt(selected) || // |
| 1858 | !SpeedRSAKeyGen(selected) || // |
| 1859 | !SpeedHRSS(selected) || // |
| 1860 | !SpeedKyber(selected) || // |
| 1861 | !SpeedMLDSA(selected) || // |
| 1862 | !SpeedMLKEM(selected) || // |
| 1863 | !SpeedMLKEM1024(selected) || // |
| 1864 | !SpeedSLHDSA(selected) || // |
| 1865 | !SpeedHashToCurve(selected) || // |
| 1866 | !SpeedTrustToken("TrustToken-Exp1-Batch1", TRUST_TOKEN_experiment_v1(), 1, |
| 1867 | selected) || |
| 1868 | !SpeedTrustToken("TrustToken-Exp1-Batch10", TRUST_TOKEN_experiment_v1(), |
| 1869 | 10, selected) || |
| 1870 | !SpeedTrustToken("TrustToken-Exp2VOPRF-Batch1", |
| 1871 | TRUST_TOKEN_experiment_v2_voprf(), 1, selected) || |
| 1872 | !SpeedTrustToken("TrustToken-Exp2VOPRF-Batch10", |
| 1873 | TRUST_TOKEN_experiment_v2_voprf(), 10, selected) || |
| 1874 | !SpeedTrustToken("TrustToken-Exp2PMB-Batch1", |
| 1875 | TRUST_TOKEN_experiment_v2_pmb(), 1, selected) || |
| 1876 | !SpeedTrustToken("TrustToken-Exp2PMB-Batch10", |
| 1877 | TRUST_TOKEN_experiment_v2_pmb(), 10, selected) || |
| 1878 | !SpeedBase64(selected) || // |
| 1879 | !SpeedSipHash(selected)) { |
| 1880 | return false; |
| 1881 | } |
| 1882 | #if defined(BORINGSSL_FIPS) |
| 1883 | if (!SpeedSelfTest(selected)) { |
| 1884 | return false; |
| 1885 | } |
| 1886 | #endif |
| 1887 | if (g_print_json) { |
| 1888 | puts("\n]"); |
| 1889 | } |
| 1890 |
| 1891 | return true; |
| 1892 | } |
| 1893 |