Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <string>
  15. #include <functional>
  16. #include <memory>
  17. #include <vector>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <openssl/aead.h>
  22. #include <openssl/bn.h>
  23. #include <openssl/curve25519.h>
  24. #include <openssl/digest.h>
  25. #include <openssl/err.h>
  26. #include <openssl/ec.h>
  27. #include <openssl/ecdsa.h>
  28. #include <openssl/ec_key.h>
  29. #include <openssl/nid.h>
  30. #include <openssl/rand.h>
  31. #include <openssl/rsa.h>
  32. #if defined(OPENSSL_WINDOWS)
  33. OPENSSL_MSVC_PRAGMA(warning(push, 3))
  34. #include <windows.h>
  35. OPENSSL_MSVC_PRAGMA(warning(pop))
  36. #elif defined(OPENSSL_APPLE)
  37. #include <sys/time.h>
  38. #else
  39. #include <time.h>
  40. #endif
  41. #include "internal.h"
  42. // TimeResults represents the results of benchmarking a function.
  43. struct TimeResults {
  44. // num_calls is the number of function calls done in the time period.
  45. unsigned num_calls;
  46. // us is the number of microseconds that elapsed in the time period.
  47. unsigned us;
  48. void Print(const std::string &description) {
  49. printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls,
  50. description.c_str(), us,
  51. (static_cast<double>(num_calls) / us) * 1000000);
  52. }
  53. void PrintWithBytes(const std::string &description, size_t bytes_per_call) {
  54. printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n",
  55. num_calls, description.c_str(), us,
  56. (static_cast<double>(num_calls) / us) * 1000000,
  57. static_cast<double>(bytes_per_call * num_calls) / us);
  58. }
  59. };
  60. #if defined(OPENSSL_WINDOWS)
  61. static uint64_t time_now() { return GetTickCount64() * 1000; }
  62. #elif defined(OPENSSL_APPLE)
  63. static uint64_t time_now() {
  64. struct timeval tv;
  65. uint64_t ret;
  66. gettimeofday(&tv, NULL);
  67. ret = tv.tv_sec;
  68. ret *= 1000000;
  69. ret += tv.tv_usec;
  70. return ret;
  71. }
  72. #else
  73. static uint64_t time_now() {
  74. struct timespec ts;
  75. clock_gettime(CLOCK_MONOTONIC, &ts);
  76. uint64_t ret = ts.tv_sec;
  77. ret *= 1000000;
  78. ret += ts.tv_nsec / 1000;
  79. return ret;
  80. }
  81. #endif
  82. static uint64_t g_timeout_seconds = 1;
  83. static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
  84. // total_us is the total amount of time that we'll aim to measure a function
  85. // for.
  86. const uint64_t total_us = g_timeout_seconds * 1000000;
  87. uint64_t start = time_now(), now, delta;
  88. unsigned done = 0, iterations_between_time_checks;
  89. if (!func()) {
  90. return false;
  91. }
  92. now = time_now();
  93. delta = now - start;
  94. if (delta == 0) {
  95. iterations_between_time_checks = 250;
  96. } else {
  97. // Aim for about 100ms between time checks.
  98. iterations_between_time_checks =
  99. static_cast<double>(100000) / static_cast<double>(delta);
  100. if (iterations_between_time_checks > 1000) {
  101. iterations_between_time_checks = 1000;
  102. } else if (iterations_between_time_checks < 1) {
  103. iterations_between_time_checks = 1;
  104. }
  105. }
  106. for (;;) {
  107. for (unsigned i = 0; i < iterations_between_time_checks; i++) {
  108. if (!func()) {
  109. return false;
  110. }
  111. done++;
  112. }
  113. now = time_now();
  114. if (now - start > total_us) {
  115. break;
  116. }
  117. }
  118. results->us = now - start;
  119. results->num_calls = done;
  120. return true;
  121. }
  122. static bool SpeedRSA(const std::string &key_name, RSA *key,
  123. const std::string &selected) {
  124. if (!selected.empty() && key_name.find(selected) == std::string::npos) {
  125. return true;
  126. }
  127. std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]);
  128. const uint8_t fake_sha256_hash[32] = {0};
  129. unsigned sig_len;
  130. TimeResults results;
  131. if (!TimeFunction(&results,
  132. [key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
  133. /* Usually during RSA signing we're using a long-lived |RSA| that has
  134. * already had all of its |BN_MONT_CTX|s constructed, so it makes
  135. * sense to use |key| directly here. */
  136. return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
  137. sig.get(), &sig_len, key);
  138. })) {
  139. fprintf(stderr, "RSA_sign failed.\n");
  140. ERR_print_errors_fp(stderr);
  141. return false;
  142. }
  143. results.Print(key_name + " signing");
  144. if (!TimeFunction(&results,
  145. [key, &fake_sha256_hash, &sig, sig_len]() -> bool {
  146. /* Usually during RSA verification we have to parse an RSA key from a
  147. * certificate or similar, in which case we'd need to construct a new
  148. * RSA key, with a new |BN_MONT_CTX| for the public modulus. If we were
  149. * to use |key| directly instead, then these costs wouldn't be
  150. * accounted for. */
  151. bssl::UniquePtr<RSA> verify_key(RSA_new());
  152. if (!verify_key) {
  153. return false;
  154. }
  155. verify_key->n = BN_dup(key->n);
  156. verify_key->e = BN_dup(key->e);
  157. if (!verify_key->n ||
  158. !verify_key->e) {
  159. return false;
  160. }
  161. return RSA_verify(NID_sha256, fake_sha256_hash,
  162. sizeof(fake_sha256_hash), sig.get(), sig_len, key);
  163. })) {
  164. fprintf(stderr, "RSA_verify failed.\n");
  165. ERR_print_errors_fp(stderr);
  166. return false;
  167. }
  168. results.Print(key_name + " verify");
  169. return true;
  170. }
  171. static uint8_t *align(uint8_t *in, unsigned alignment) {
  172. return reinterpret_cast<uint8_t *>(
  173. (reinterpret_cast<uintptr_t>(in) + alignment) &
  174. ~static_cast<size_t>(alignment - 1));
  175. }
  176. static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name,
  177. size_t chunk_len, size_t ad_len) {
  178. static const unsigned kAlignment = 16;
  179. bssl::ScopedEVP_AEAD_CTX ctx;
  180. const size_t key_len = EVP_AEAD_key_length(aead);
  181. const size_t nonce_len = EVP_AEAD_nonce_length(aead);
  182. const size_t overhead_len = EVP_AEAD_max_overhead(aead);
  183. std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
  184. memset(key.get(), 0, key_len);
  185. std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]);
  186. memset(nonce.get(), 0, nonce_len);
  187. std::unique_ptr<uint8_t[]> in_storage(new uint8_t[chunk_len + kAlignment]);
  188. std::unique_ptr<uint8_t[]> out_storage(new uint8_t[chunk_len + overhead_len + kAlignment]);
  189. std::unique_ptr<uint8_t[]> ad(new uint8_t[ad_len]);
  190. memset(ad.get(), 0, ad_len);
  191. uint8_t *const in = align(in_storage.get(), kAlignment);
  192. memset(in, 0, chunk_len);
  193. uint8_t *const out = align(out_storage.get(), kAlignment);
  194. memset(out, 0, chunk_len + overhead_len);
  195. if (!EVP_AEAD_CTX_init_with_direction(ctx.get(), aead, key.get(), key_len,
  196. EVP_AEAD_DEFAULT_TAG_LENGTH,
  197. evp_aead_seal)) {
  198. fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
  199. ERR_print_errors_fp(stderr);
  200. return false;
  201. }
  202. TimeResults results;
  203. if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, ad_len, in,
  204. out, &ctx, &nonce, &ad]() -> bool {
  205. size_t out_len;
  206. return EVP_AEAD_CTX_seal(ctx.get(), out, &out_len,
  207. chunk_len + overhead_len, nonce.get(),
  208. nonce_len, in, chunk_len, ad.get(), ad_len);
  209. })) {
  210. fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
  211. ERR_print_errors_fp(stderr);
  212. return false;
  213. }
  214. results.PrintWithBytes(name + " seal", chunk_len);
  215. return true;
  216. }
  217. static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name,
  218. size_t ad_len, const std::string &selected) {
  219. if (!selected.empty() && name.find(selected) == std::string::npos) {
  220. return true;
  221. }
  222. return SpeedAEADChunk(aead, name + " (16 bytes)", 16, ad_len) &&
  223. SpeedAEADChunk(aead, name + " (1350 bytes)", 1350, ad_len) &&
  224. SpeedAEADChunk(aead, name + " (8192 bytes)", 8192, ad_len);
  225. }
  226. static bool SpeedHashChunk(const EVP_MD *md, const std::string &name,
  227. size_t chunk_len) {
  228. EVP_MD_CTX *ctx = EVP_MD_CTX_create();
  229. uint8_t scratch[8192];
  230. if (chunk_len > sizeof(scratch)) {
  231. return false;
  232. }
  233. TimeResults results;
  234. if (!TimeFunction(&results, [ctx, md, chunk_len, &scratch]() -> bool {
  235. uint8_t digest[EVP_MAX_MD_SIZE];
  236. unsigned int md_len;
  237. return EVP_DigestInit_ex(ctx, md, NULL /* ENGINE */) &&
  238. EVP_DigestUpdate(ctx, scratch, chunk_len) &&
  239. EVP_DigestFinal_ex(ctx, digest, &md_len);
  240. })) {
  241. fprintf(stderr, "EVP_DigestInit_ex failed.\n");
  242. ERR_print_errors_fp(stderr);
  243. return false;
  244. }
  245. results.PrintWithBytes(name, chunk_len);
  246. EVP_MD_CTX_destroy(ctx);
  247. return true;
  248. }
  249. static bool SpeedHash(const EVP_MD *md, const std::string &name,
  250. const std::string &selected) {
  251. if (!selected.empty() && name.find(selected) == std::string::npos) {
  252. return true;
  253. }
  254. return SpeedHashChunk(md, name + " (16 bytes)", 16) &&
  255. SpeedHashChunk(md, name + " (256 bytes)", 256) &&
  256. SpeedHashChunk(md, name + " (8192 bytes)", 8192);
  257. }
  258. static bool SpeedRandomChunk(const std::string &name, size_t chunk_len) {
  259. uint8_t scratch[8192];
  260. if (chunk_len > sizeof(scratch)) {
  261. return false;
  262. }
  263. TimeResults results;
  264. if (!TimeFunction(&results, [chunk_len, &scratch]() -> bool {
  265. RAND_bytes(scratch, chunk_len);
  266. return true;
  267. })) {
  268. return false;
  269. }
  270. results.PrintWithBytes(name, chunk_len);
  271. return true;
  272. }
  273. static bool SpeedRandom(const std::string &selected) {
  274. if (!selected.empty() && selected != "RNG") {
  275. return true;
  276. }
  277. return SpeedRandomChunk("RNG (16 bytes)", 16) &&
  278. SpeedRandomChunk("RNG (256 bytes)", 256) &&
  279. SpeedRandomChunk("RNG (8192 bytes)", 8192);
  280. }
  281. static bool SpeedECDHCurve(const std::string &name, int nid,
  282. const std::string &selected) {
  283. if (!selected.empty() && name.find(selected) == std::string::npos) {
  284. return true;
  285. }
  286. TimeResults results;
  287. if (!TimeFunction(&results, [nid]() -> bool {
  288. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
  289. if (!key ||
  290. !EC_KEY_generate_key(key.get())) {
  291. return false;
  292. }
  293. const EC_GROUP *const group = EC_KEY_get0_group(key.get());
  294. bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group));
  295. bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
  296. bssl::UniquePtr<BIGNUM> x(BN_new());
  297. bssl::UniquePtr<BIGNUM> y(BN_new());
  298. if (!point || !ctx || !x || !y ||
  299. !EC_POINT_mul(group, point.get(), NULL,
  300. EC_KEY_get0_public_key(key.get()),
  301. EC_KEY_get0_private_key(key.get()), ctx.get()) ||
  302. !EC_POINT_get_affine_coordinates_GFp(group, point.get(), x.get(),
  303. y.get(), ctx.get())) {
  304. return false;
  305. }
  306. return true;
  307. })) {
  308. return false;
  309. }
  310. results.Print(name);
  311. return true;
  312. }
  313. static bool SpeedECDSACurve(const std::string &name, int nid,
  314. const std::string &selected) {
  315. if (!selected.empty() && name.find(selected) == std::string::npos) {
  316. return true;
  317. }
  318. bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
  319. if (!key ||
  320. !EC_KEY_generate_key(key.get())) {
  321. return false;
  322. }
  323. uint8_t signature[256];
  324. if (ECDSA_size(key.get()) > sizeof(signature)) {
  325. return false;
  326. }
  327. uint8_t digest[20];
  328. memset(digest, 42, sizeof(digest));
  329. unsigned sig_len;
  330. TimeResults results;
  331. if (!TimeFunction(&results, [&key, &signature, &digest, &sig_len]() -> bool {
  332. return ECDSA_sign(0, digest, sizeof(digest), signature, &sig_len,
  333. key.get()) == 1;
  334. })) {
  335. return false;
  336. }
  337. results.Print(name + " signing");
  338. if (!TimeFunction(&results, [&key, &signature, &digest, sig_len]() -> bool {
  339. return ECDSA_verify(0, digest, sizeof(digest), signature, sig_len,
  340. key.get()) == 1;
  341. })) {
  342. return false;
  343. }
  344. results.Print(name + " verify");
  345. return true;
  346. }
  347. static bool SpeedECDH(const std::string &selected) {
  348. return SpeedECDHCurve("ECDH P-224", NID_secp224r1, selected) &&
  349. SpeedECDHCurve("ECDH P-256", NID_X9_62_prime256v1, selected) &&
  350. SpeedECDHCurve("ECDH P-384", NID_secp384r1, selected) &&
  351. SpeedECDHCurve("ECDH P-521", NID_secp521r1, selected);
  352. }
  353. static bool SpeedECDSA(const std::string &selected) {
  354. return SpeedECDSACurve("ECDSA P-224", NID_secp224r1, selected) &&
  355. SpeedECDSACurve("ECDSA P-256", NID_X9_62_prime256v1, selected) &&
  356. SpeedECDSACurve("ECDSA P-384", NID_secp384r1, selected) &&
  357. SpeedECDSACurve("ECDSA P-521", NID_secp521r1, selected);
  358. }
  359. static bool Speed25519(const std::string &selected) {
  360. if (!selected.empty() && selected.find("25519") == std::string::npos) {
  361. return true;
  362. }
  363. TimeResults results;
  364. uint8_t public_key[32], private_key[64];
  365. if (!TimeFunction(&results, [&public_key, &private_key]() -> bool {
  366. ED25519_keypair(public_key, private_key);
  367. return true;
  368. })) {
  369. return false;
  370. }
  371. results.Print("Ed25519 key generation");
  372. static const uint8_t kMessage[] = {0, 1, 2, 3, 4, 5};
  373. uint8_t signature[64];
  374. if (!TimeFunction(&results, [&private_key, &signature]() -> bool {
  375. return ED25519_sign(signature, kMessage, sizeof(kMessage),
  376. private_key) == 1;
  377. })) {
  378. return false;
  379. }
  380. results.Print("Ed25519 signing");
  381. if (!TimeFunction(&results, [&public_key, &signature]() -> bool {
  382. return ED25519_verify(kMessage, sizeof(kMessage), signature,
  383. public_key) == 1;
  384. })) {
  385. fprintf(stderr, "Ed25519 verify failed.\n");
  386. return false;
  387. }
  388. results.Print("Ed25519 verify");
  389. if (!TimeFunction(&results, []() -> bool {
  390. uint8_t out[32], in[32];
  391. memset(in, 0, sizeof(in));
  392. X25519_public_from_private(out, in);
  393. return true;
  394. })) {
  395. fprintf(stderr, "Curve25519 base-point multiplication failed.\n");
  396. return false;
  397. }
  398. results.Print("Curve25519 base-point multiplication");
  399. if (!TimeFunction(&results, []() -> bool {
  400. uint8_t out[32], in1[32], in2[32];
  401. memset(in1, 0, sizeof(in1));
  402. memset(in2, 0, sizeof(in2));
  403. in1[0] = 1;
  404. in2[0] = 9;
  405. return X25519(out, in1, in2) == 1;
  406. })) {
  407. fprintf(stderr, "Curve25519 arbitrary point multiplication failed.\n");
  408. return false;
  409. }
  410. results.Print("Curve25519 arbitrary point multiplication");
  411. return true;
  412. }
  413. static bool SpeedSPAKE2(const std::string &selected) {
  414. if (!selected.empty() && selected.find("SPAKE2") == std::string::npos) {
  415. return true;
  416. }
  417. TimeResults results;
  418. static const uint8_t kAliceName[] = {'A'};
  419. static const uint8_t kBobName[] = {'B'};
  420. static const uint8_t kPassword[] = "password";
  421. bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(spake2_role_alice,
  422. kAliceName, sizeof(kAliceName), kBobName,
  423. sizeof(kBobName)));
  424. uint8_t alice_msg[SPAKE2_MAX_MSG_SIZE];
  425. size_t alice_msg_len;
  426. if (!SPAKE2_generate_msg(alice.get(), alice_msg, &alice_msg_len,
  427. sizeof(alice_msg),
  428. kPassword, sizeof(kPassword))) {
  429. fprintf(stderr, "SPAKE2_generate_msg failed.\n");
  430. return false;
  431. }
  432. if (!TimeFunction(&results, [&alice_msg, alice_msg_len]() -> bool {
  433. bssl::UniquePtr<SPAKE2_CTX> bob(SPAKE2_CTX_new(spake2_role_bob,
  434. kBobName, sizeof(kBobName), kAliceName,
  435. sizeof(kAliceName)));
  436. uint8_t bob_msg[SPAKE2_MAX_MSG_SIZE], bob_key[64];
  437. size_t bob_msg_len, bob_key_len;
  438. if (!SPAKE2_generate_msg(bob.get(), bob_msg, &bob_msg_len,
  439. sizeof(bob_msg), kPassword,
  440. sizeof(kPassword)) ||
  441. !SPAKE2_process_msg(bob.get(), bob_key, &bob_key_len,
  442. sizeof(bob_key), alice_msg, alice_msg_len)) {
  443. return false;
  444. }
  445. return true;
  446. })) {
  447. fprintf(stderr, "SPAKE2 failed.\n");
  448. }
  449. results.Print("SPAKE2 over Ed25519");
  450. return true;
  451. }
  452. static const struct argument kArguments[] = {
  453. {
  454. "-filter", kOptionalArgument,
  455. "A filter on the speed tests to run",
  456. },
  457. {
  458. "-timeout", kOptionalArgument,
  459. "The number of seconds to run each test for (default is 1)",
  460. },
  461. {
  462. "", kOptionalArgument, "",
  463. },
  464. };
  465. bool Speed(const std::vector<std::string> &args) {
  466. std::map<std::string, std::string> args_map;
  467. if (!ParseKeyValueArguments(&args_map, args, kArguments)) {
  468. PrintUsage(kArguments);
  469. return false;
  470. }
  471. std::string selected;
  472. if (args_map.count("-filter") != 0) {
  473. selected = args_map["-filter"];
  474. }
  475. if (args_map.count("-timeout") != 0) {
  476. g_timeout_seconds = atoi(args_map["-timeout"].c_str());
  477. }
  478. bssl::UniquePtr<RSA> key(
  479. RSA_private_key_from_bytes(kDERRSAPrivate2048, kDERRSAPrivate2048Len));
  480. if (key == nullptr) {
  481. fprintf(stderr, "Failed to parse RSA key.\n");
  482. ERR_print_errors_fp(stderr);
  483. return false;
  484. }
  485. if (!SpeedRSA("RSA 2048", key.get(), selected)) {
  486. return false;
  487. }
  488. key.reset(RSA_private_key_from_bytes(kDERRSAPrivate3Prime2048,
  489. kDERRSAPrivate3Prime2048Len));
  490. if (key == nullptr) {
  491. fprintf(stderr, "Failed to parse RSA key.\n");
  492. ERR_print_errors_fp(stderr);
  493. return false;
  494. }
  495. if (!SpeedRSA("RSA 2048 (3 prime, e=3)", key.get(), selected)) {
  496. return false;
  497. }
  498. key.reset(
  499. RSA_private_key_from_bytes(kDERRSAPrivate4096, kDERRSAPrivate4096Len));
  500. if (key == nullptr) {
  501. fprintf(stderr, "Failed to parse 4096-bit RSA key.\n");
  502. ERR_print_errors_fp(stderr);
  503. return 1;
  504. }
  505. if (!SpeedRSA("RSA 4096", key.get(), selected)) {
  506. return false;
  507. }
  508. key.reset();
  509. // kTLSADLen is the number of bytes of additional data that TLS passes to
  510. // AEADs.
  511. static const size_t kTLSADLen = 13;
  512. // kLegacyADLen is the number of bytes that TLS passes to the "legacy" AEADs.
  513. // These are AEADs that weren't originally defined as AEADs, but which we use
  514. // via the AEAD interface. In order for that to work, they have some TLS
  515. // knowledge in them and construct a couple of the AD bytes internally.
  516. static const size_t kLegacyADLen = kTLSADLen - 2;
  517. if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM", kTLSADLen, selected) ||
  518. !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM", kTLSADLen, selected) ||
  519. !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305", kTLSADLen,
  520. selected) ||
  521. !SpeedAEAD(EVP_aead_chacha20_poly1305_old(), "ChaCha20-Poly1305-Old",
  522. kTLSADLen, selected) ||
  523. !SpeedAEAD(EVP_aead_des_ede3_cbc_sha1_tls(), "DES-EDE3-CBC-SHA1",
  524. kLegacyADLen, selected) ||
  525. !SpeedAEAD(EVP_aead_aes_128_cbc_sha1_tls(), "AES-128-CBC-SHA1",
  526. kLegacyADLen, selected) ||
  527. !SpeedAEAD(EVP_aead_aes_256_cbc_sha1_tls(), "AES-256-CBC-SHA1",
  528. kLegacyADLen, selected) ||
  529. #if !defined(OPENSSL_SMALL)
  530. !SpeedAEAD(EVP_aead_aes_128_gcm_siv(), "AES-128-GCM-SIV", kTLSADLen,
  531. selected) ||
  532. !SpeedAEAD(EVP_aead_aes_256_gcm_siv(), "AES-256-GCM-SIV", kTLSADLen,
  533. selected) ||
  534. #endif
  535. !SpeedHash(EVP_sha1(), "SHA-1", selected) ||
  536. !SpeedHash(EVP_sha256(), "SHA-256", selected) ||
  537. !SpeedHash(EVP_sha512(), "SHA-512", selected) ||
  538. !SpeedRandom(selected) ||
  539. !SpeedECDH(selected) ||
  540. !SpeedECDSA(selected) ||
  541. !Speed25519(selected) ||
  542. !SpeedSPAKE2(selected)) {
  543. return false;
  544. }
  545. return true;
  546. }