ecc-verify-benchmark.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* ecc-verify-benchmark.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include <wolfssl/wolfcrypt/settings.h>
  22. #include <wolfssl/wolfcrypt/sha256.h>
  23. #include <wolfssl/wolfcrypt/random.h>
  24. #include <wolfssl/wolfcrypt/ecc.h>
  25. #include <wolfssl/wolfcrypt/asn_public.h>
  26. #include <pthread.h>
  27. #define USE_CERT_BUFFERS_256
  28. #include <wolfssl/certs_test.h>
  29. #define MAX_TIMES 5000
  30. #define MAX_BLOCK_SIZE 1024
  31. #include <sys/time.h>
  32. static double get_time()
  33. {
  34. struct timeval tv;
  35. gettimeofday(&tv, 0);
  36. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  37. }
  38. /* software version */
  39. void* hash_firmware_verify(void* key)
  40. {
  41. int ret, i;
  42. int verify;
  43. const byte hash[] = {
  44. 0XFB, 0XBA, 0XB2, 0X89, 0XF7, 0XF9, 0X4B, 0X25, 0X73, 0X6C, 0X58, 0XBE, 0X46, 0XA9, 0X94, 0XC4, 0X41, 0XFD, 0X02, 0X55, 0X2C, 0XC6, 0X02, 0X23, 0X52, 0XE3, 0XD8, 0X6D, 0X2F, 0XAB, 0X7C, 0X83
  45. };
  46. const byte sigBuf[] = {
  47. 0X30, 0X44, 0X02, 0X20, 0X05, 0X38, 0XBC, 0X16, 0XC7, 0X67, 0X18, 0XEC, 0XE6, 0X1E, 0X43, 0X7B, 0X29, 0X8F, 0X85, 0X01, 0X33, 0XA8, 0X9B, 0XDD, 0X91, 0X32, 0X1F, 0XEC, 0XF7, 0X91, 0X18, 0X72, 0X9C, 0XE2, 0X6F, 0X31, 0X02, 0X20, 0X3E, 0X31, 0XD6, 0X40, 0XF7, 0X38, 0X3C, 0X1B, 0X6D, 0XAD, 0XE3, 0X93, 0X20, 0XE8, 0XB1, 0XBD, 0X3C, 0X59, 0XF2, 0XD2, 0X7C, 0X46, 0X1B, 0XE5, 0XE1, 0XE3, 0XAB, 0X5E, 0X76, 0X73, 0X6F, 0XFB
  48. };
  49. word32 sigLen = (word32)sizeof(sigBuf);
  50. word32 hashLen = (word32)sizeof(hash);
  51. for (i = 0; i < MAX_TIMES; i++) {
  52. ret = wc_ecc_verify_hash((byte*)sigBuf, sigLen, hash, hashLen, &verify, (ecc_key*)key);
  53. if (ret < 0 || verify != 1) {
  54. printf("failed on try %d\n", i);
  55. break;
  56. }
  57. }
  58. if (ret < 0 || verify != 1) {
  59. printf("unable to verify, ret = %d verify = %d\n", ret, verify);
  60. }
  61. return NULL;
  62. }
  63. /* when flag is set then try to use software only if DSP is built in */
  64. static int hash_firmware_verify_default(int numThreads)
  65. {
  66. int ret, i;
  67. word32 idx;
  68. double t;
  69. pthread_t threads[numThreads];
  70. ecc_key eccKey[numThreads];
  71. for (i = 0; i < numThreads; i++) {
  72. wc_ecc_init(&(eccKey[i]));
  73. idx = 0;
  74. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &(eccKey[i]), sizeof_ecc_clikey_der_256);
  75. if (ret < 0)
  76. return ret;
  77. }
  78. t = get_time();
  79. for (i = 0; i < numThreads; i++) {
  80. pthread_create(&threads[i], NULL, hash_firmware_verify, (void*)&(eccKey[i]));
  81. }
  82. for (i = 0; i < numThreads; i++) {
  83. pthread_join(threads[i], NULL);
  84. }
  85. t = get_time() - t;
  86. printf("%d verifies on %d threads took %f seconds\n", MAX_TIMES * numThreads, numThreads, t);
  87. return 0;
  88. }
  89. #ifdef WOLFSSL_DSP
  90. /* domain 0 = cDSP 1 = aDSP */
  91. static int hash_firmware_verify_dsp(int numThreads, int domain)
  92. {
  93. int ret, i;
  94. word32 idx;
  95. double t;
  96. remote_handle64 handle[numThreads];
  97. char *sp_URI_value;
  98. pthread_t threads[numThreads];
  99. ecc_key eccKey[numThreads];
  100. if (domain == 0) {
  101. sp_URI_value = wolfSSL_URI "&_dom=cdsp";
  102. }
  103. else {
  104. sp_URI_value = wolfSSL_URI "&_dom=adsp";
  105. }
  106. for (i = 0; i < numThreads; i++) {
  107. wc_ecc_init(&(eccKey[i]));
  108. idx = 0;
  109. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &(eccKey[i]), sizeof_ecc_clikey_der_256);
  110. if (ret < 0)
  111. return ret;
  112. ret = wolfSSL_open(sp_URI_value, &(handle[i]));
  113. if (ret != 0) {
  114. printf("unable to open CDSP? retVal = %d\n", ret);
  115. return -1;
  116. }
  117. wc_ecc_set_handle(&(eccKey[i]), handle[i]);
  118. }
  119. t = get_time();
  120. for (i = 0; i < numThreads; i++) {
  121. pthread_create(&threads[i], NULL, hash_firmware_verify, (void*)&(eccKey[i]));
  122. }
  123. for (i = 0; i < numThreads; i++) {
  124. pthread_join(threads[i], NULL);
  125. wolfSSL_close(handle[i]);
  126. }
  127. t = get_time() - t;
  128. printf("%d verifies on %d threads took %f seconds\n", MAX_TIMES * numThreads, numThreads, t);
  129. return 0;
  130. }
  131. #endif /* WOLFSSL_DSP */
  132. int main(int argc, char* argv[])
  133. {
  134. wolfCrypt_Init();
  135. printf("benchmarking using default (locks on handle for aDSP)\n");
  136. hash_firmware_verify_default(1);
  137. hash_firmware_verify_default(2);
  138. printf("\nbenchmarking using software (+NEON if built in)\n");
  139. #ifdef WOLFSSL_DSP
  140. wolfSSL_SetHandleCb(NULL); /* remove calls to DSP by default */
  141. #endif
  142. hash_firmware_verify_default(1);
  143. hash_firmware_verify_default(2);
  144. #ifdef WOLFSSL_DSP
  145. printf("\nbenchmarking using threads on aDSP\n");
  146. hash_firmware_verify_dsp(1, 1);
  147. hash_firmware_verify_dsp(2, 1);
  148. hash_firmware_verify_dsp(3, 1);
  149. hash_firmware_verify_dsp(4, 1);
  150. printf("\nbenchmarking 1 thread on cDSP\n");
  151. hash_firmware_verify_dsp(1, 0);
  152. #endif /* WOLFSSL_DSP */
  153. wolfCrypt_Cleanup();
  154. return 0;
  155. }