perf_bigint.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * Some performance testing of bigint.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "ssl.h"
  37. /**************************************************************************
  38. * BIGINT tests
  39. *
  40. **************************************************************************/
  41. int main(int argc, char *argv[])
  42. {
  43. #ifdef CONFIG_SSL_CERT_VERIFICATION
  44. RSA_CTX *rsa_ctx;
  45. BI_CTX *ctx;
  46. bigint *bi_data, *bi_res;
  47. int diff, res = 1;
  48. struct timeval tv_old, tv_new;
  49. const char *plaintext;
  50. uint8_t compare[MAX_KEY_BYTE_SIZE];
  51. int i, max_biggie = 10; /* really crank performance */
  52. int len;
  53. uint8_t *buf;
  54. /**
  55. * 512 bit key
  56. */
  57. plaintext = /* 64 byte number */
  58. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
  59. len = get_file("../ssl/test/axTLS.key_512", &buf);
  60. asn1_get_private_key(buf, len, &rsa_ctx);
  61. ctx = rsa_ctx->bi_ctx;
  62. bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
  63. bi_res = RSA_public(rsa_ctx, bi_data);
  64. bi_data = bi_res; /* reuse again */
  65. gettimeofday(&tv_old, NULL);
  66. for (i = 0; i < max_biggie; i++)
  67. {
  68. bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
  69. if (i < max_biggie-1)
  70. {
  71. bi_free(ctx, bi_res);
  72. }
  73. }
  74. gettimeofday(&tv_new, NULL);
  75. bi_free(ctx, bi_data);
  76. diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
  77. (tv_new.tv_usec-tv_old.tv_usec)/1000;
  78. printf("512 bit decrypt time: %dms\n", diff/max_biggie);
  79. TTY_FLUSH();
  80. bi_export(ctx, bi_res, compare, 64);
  81. RSA_free(rsa_ctx);
  82. free(buf);
  83. if (memcmp(plaintext, compare, 64) != 0)
  84. goto end;
  85. /**
  86. * 1024 bit key
  87. */
  88. plaintext = /* 128 byte number */
  89. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  90. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
  91. len = get_file("../ssl/test/axTLS.key_1024", &buf);
  92. asn1_get_private_key(buf, len, &rsa_ctx);
  93. ctx = rsa_ctx->bi_ctx;
  94. bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
  95. bi_res = RSA_public(rsa_ctx, bi_data);
  96. bi_data = bi_res; /* reuse again */
  97. gettimeofday(&tv_old, NULL);
  98. for (i = 0; i < max_biggie; i++)
  99. {
  100. bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
  101. if (i < max_biggie-1)
  102. {
  103. bi_free(ctx, bi_res);
  104. }
  105. }
  106. gettimeofday(&tv_new, NULL);
  107. bi_free(ctx, bi_data);
  108. diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
  109. (tv_new.tv_usec-tv_old.tv_usec)/1000;
  110. printf("1024 bit decrypt time: %dms\n", diff/max_biggie);
  111. TTY_FLUSH();
  112. bi_export(ctx, bi_res, compare, 128);
  113. RSA_free(rsa_ctx);
  114. free(buf);
  115. if (memcmp(plaintext, compare, 128) != 0)
  116. goto end;
  117. /**
  118. * 2048 bit key
  119. */
  120. plaintext = /* 256 byte number */
  121. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  122. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  123. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  124. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
  125. len = get_file("../ssl/test/axTLS.key_2048", &buf);
  126. asn1_get_private_key(buf, len, &rsa_ctx);
  127. ctx = rsa_ctx->bi_ctx;
  128. bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
  129. bi_res = RSA_public(rsa_ctx, bi_data);
  130. bi_data = bi_res; /* reuse again */
  131. gettimeofday(&tv_old, NULL);
  132. for (i = 0; i < max_biggie; i++)
  133. {
  134. bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
  135. if (i < max_biggie-1)
  136. {
  137. bi_free(ctx, bi_res);
  138. }
  139. }
  140. gettimeofday(&tv_new, NULL);
  141. bi_free(ctx, bi_data);
  142. diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
  143. (tv_new.tv_usec-tv_old.tv_usec)/1000;
  144. printf("2048 bit decrypt time: %dms\n", diff/max_biggie);
  145. TTY_FLUSH();
  146. bi_export(ctx, bi_res, compare, 256);
  147. RSA_free(rsa_ctx);
  148. free(buf);
  149. if (memcmp(plaintext, compare, 256) != 0)
  150. goto end;
  151. /**
  152. * 4096 bit key
  153. */
  154. plaintext = /* 512 byte number */
  155. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  156. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  157. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  158. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  159. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  160. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  161. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^"
  162. "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*^";
  163. len = get_file("../ssl/test/axTLS.key_4096", &buf);
  164. asn1_get_private_key(buf, len, &rsa_ctx);
  165. ctx = rsa_ctx->bi_ctx;
  166. bi_data = bi_import(ctx, (uint8_t *)plaintext, strlen(plaintext));
  167. gettimeofday(&tv_old, NULL);
  168. bi_res = RSA_public(rsa_ctx, bi_data);
  169. gettimeofday(&tv_new, NULL);
  170. diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
  171. (tv_new.tv_usec-tv_old.tv_usec)/1000;
  172. printf("4096 bit encrypt time: %dms\n", diff);
  173. TTY_FLUSH();
  174. bi_data = bi_res; /* reuse again */
  175. gettimeofday(&tv_old, NULL);
  176. for (i = 0; i < max_biggie; i++)
  177. {
  178. bi_res = RSA_private(rsa_ctx, bi_copy(bi_data));
  179. if (i < max_biggie-1)
  180. {
  181. bi_free(ctx, bi_res);
  182. }
  183. }
  184. gettimeofday(&tv_new, NULL);
  185. bi_free(ctx, bi_data);
  186. diff = (tv_new.tv_sec-tv_old.tv_sec)*1000 +
  187. (tv_new.tv_usec-tv_old.tv_usec)/1000;
  188. printf("4096 bit decrypt time: %dms\n", diff/max_biggie);
  189. TTY_FLUSH();
  190. bi_export(ctx, bi_res, compare, 512);
  191. RSA_free(rsa_ctx);
  192. free(buf);
  193. if (memcmp(plaintext, compare, 512) != 0)
  194. goto end;
  195. /* done */
  196. printf("Bigint performance testing complete\n");
  197. res = 0;
  198. end:
  199. return res;
  200. #else
  201. return 0;
  202. #endif
  203. }