You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

240 line
7.1 KiB

  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 <time.h>
  20. #include <openssl/aead.h>
  21. #include <openssl/bio.h>
  22. #include <openssl/obj.h>
  23. #include <openssl/rsa.h>
  24. #if defined(OPENSSL_WINDOWS)
  25. #include <Windows.h>
  26. #endif
  27. extern "C" {
  28. // These values are DER encoded, RSA private keys.
  29. extern const uint8_t kDERRSAPrivate2048[];
  30. extern size_t kDERRSAPrivate2048Len;
  31. extern const uint8_t kDERRSAPrivate4096[];
  32. extern size_t kDERRSAPrivate4096Len;
  33. }
  34. // TimeResults represents the results of benchmarking a function.
  35. struct TimeResults {
  36. // num_calls is the number of function calls done in the time period.
  37. unsigned num_calls;
  38. // us is the number of microseconds that elapsed in the time period.
  39. unsigned us;
  40. void Print(const std::string &description) {
  41. printf("Did %u %s operations in %uus (%.1f ops/sec)\n", num_calls,
  42. description.c_str(), us,
  43. (static_cast<double>(num_calls) / us) * 1000000);
  44. }
  45. void PrintWithBytes(const std::string &description, size_t bytes_per_call) {
  46. printf("Did %u %s operations in %uus (%.1f ops/sec): %.1f MB/s\n",
  47. num_calls, description.c_str(), us,
  48. (static_cast<double>(num_calls) / us) * 1000000,
  49. static_cast<double>(bytes_per_call * num_calls) / us);
  50. }
  51. };
  52. #if defined(OPENSSL_WINDOWS)
  53. static uint64_t time_now() { return GetTickCount64() * 1000; }
  54. #else
  55. static uint64_t time_now() {
  56. struct timespec ts;
  57. clock_gettime(CLOCK_MONOTONIC, &ts);
  58. uint64_t ret = ts.tv_sec;
  59. ret *= 1000000;
  60. ret += ts.tv_nsec / 1000;
  61. return ret;
  62. }
  63. #endif
  64. static bool TimeFunction(TimeResults *results, std::function<bool()> func) {
  65. // kTotalMS is the total amount of time that we'll aim to measure a function
  66. // for.
  67. static const uint64_t kTotalUS = 3000000;
  68. uint64_t start = time_now(), now, delta;
  69. unsigned done = 0, iterations_between_time_checks;
  70. if (!func()) {
  71. return false;
  72. }
  73. now = time_now();
  74. delta = now - start;
  75. if (delta == 0) {
  76. iterations_between_time_checks = 250;
  77. } else {
  78. // Aim for about 100ms between time checks.
  79. iterations_between_time_checks =
  80. static_cast<double>(100000) / static_cast<double>(delta);
  81. if (iterations_between_time_checks > 1000) {
  82. iterations_between_time_checks = 1000;
  83. } else if (iterations_between_time_checks < 1) {
  84. iterations_between_time_checks = 1;
  85. }
  86. }
  87. for (;;) {
  88. for (unsigned i = 0; i < iterations_between_time_checks; i++) {
  89. if (!func()) {
  90. return false;
  91. }
  92. done++;
  93. }
  94. now = time_now();
  95. if (now - start > kTotalUS) {
  96. break;
  97. }
  98. }
  99. results->us = now - start;
  100. results->num_calls = done;
  101. return true;
  102. }
  103. static bool SpeedRSA(const std::string& key_name, RSA *key) {
  104. TimeResults results;
  105. std::unique_ptr<uint8_t[]> sig(new uint8_t[RSA_size(key)]);
  106. const uint8_t fake_sha256_hash[32] = {0};
  107. unsigned sig_len;
  108. if (!TimeFunction(&results,
  109. [key, &sig, &fake_sha256_hash, &sig_len]() -> bool {
  110. return RSA_sign(NID_sha256, fake_sha256_hash, sizeof(fake_sha256_hash),
  111. sig.get(), &sig_len, key);
  112. })) {
  113. fprintf(stderr, "RSA_sign failed.\n");
  114. BIO_print_errors_fp(stderr);
  115. return false;
  116. }
  117. results.Print(key_name + " signing");
  118. if (!TimeFunction(&results,
  119. [key, &fake_sha256_hash, &sig, sig_len]() -> bool {
  120. return RSA_verify(NID_sha256, fake_sha256_hash,
  121. sizeof(fake_sha256_hash), sig.get(), sig_len, key);
  122. })) {
  123. fprintf(stderr, "RSA_verify failed.\n");
  124. BIO_print_errors_fp(stderr);
  125. return false;
  126. }
  127. results.Print(key_name + " verify");
  128. return true;
  129. }
  130. static bool SpeedAEADChunk(const EVP_AEAD *aead, const std::string &name,
  131. size_t chunk_len) {
  132. EVP_AEAD_CTX ctx;
  133. const size_t key_len = EVP_AEAD_key_length(aead);
  134. const size_t nonce_len = EVP_AEAD_nonce_length(aead);
  135. const size_t overhead_len = EVP_AEAD_max_overhead(aead);
  136. std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
  137. memset(key.get(), 0, key_len);
  138. std::unique_ptr<uint8_t[]> nonce(new uint8_t[nonce_len]);
  139. memset(nonce.get(), 0, nonce_len);
  140. std::unique_ptr<uint8_t[]> in(new uint8_t[chunk_len]);
  141. memset(in.get(), 0, chunk_len);
  142. std::unique_ptr<uint8_t[]> out(new uint8_t[chunk_len + overhead_len]);
  143. memset(out.get(), 0, chunk_len + overhead_len);
  144. if (!EVP_AEAD_CTX_init(&ctx, aead, key.get(), key_len,
  145. EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) {
  146. fprintf(stderr, "Failed to create EVP_AEAD_CTX.\n");
  147. BIO_print_errors_fp(stderr);
  148. return false;
  149. }
  150. TimeResults results;
  151. if (!TimeFunction(&results, [chunk_len, overhead_len, nonce_len, &in, &out,
  152. &ctx, &nonce]() -> bool {
  153. size_t out_len;
  154. return EVP_AEAD_CTX_seal(&ctx, out.get(), &out_len,
  155. chunk_len + overhead_len, nonce.get(),
  156. nonce_len, in.get(), chunk_len, NULL, 0);
  157. })) {
  158. fprintf(stderr, "EVP_AEAD_CTX_seal failed.\n");
  159. BIO_print_errors_fp(stderr);
  160. return false;
  161. }
  162. results.PrintWithBytes(name + " seal", chunk_len);
  163. EVP_AEAD_CTX_cleanup(&ctx);
  164. return true;
  165. }
  166. static bool SpeedAEAD(const EVP_AEAD *aead, const std::string &name) {
  167. return SpeedAEADChunk(aead, name + " (16 bytes)", 16) &&
  168. SpeedAEADChunk(aead, name + " (1350 bytes)", 1350) &&
  169. SpeedAEADChunk(aead, name + " (8192 bytes)", 8192);
  170. }
  171. bool Speed(const std::vector<std::string> &args) {
  172. const uint8_t *inp;
  173. RSA *key = NULL;
  174. inp = kDERRSAPrivate2048;
  175. if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate2048Len)) {
  176. fprintf(stderr, "Failed to parse RSA key.\n");
  177. BIO_print_errors_fp(stderr);
  178. return false;
  179. }
  180. if (!SpeedRSA("RSA 2048", key)) {
  181. return false;
  182. }
  183. RSA_free(key);
  184. key = NULL;
  185. inp = kDERRSAPrivate4096;
  186. if (NULL == d2i_RSAPrivateKey(&key, &inp, kDERRSAPrivate4096Len)) {
  187. fprintf(stderr, "Failed to parse 4096-bit RSA key.\n");
  188. BIO_print_errors_fp(stderr);
  189. return 1;
  190. }
  191. if (!SpeedRSA("RSA 4096", key)) {
  192. return false;
  193. }
  194. RSA_free(key);
  195. if (!SpeedAEAD(EVP_aead_aes_128_gcm(), "AES-128-GCM") ||
  196. !SpeedAEAD(EVP_aead_aes_256_gcm(), "AES-256-GCM") ||
  197. !SpeedAEAD(EVP_aead_chacha20_poly1305(), "ChaCha20-Poly1305")) {
  198. return false;
  199. }
  200. return 0;
  201. }