main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* benchmark main.c
  2. *
  3. * Copyright (C) 2006-2022 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. /* ESP-IDF */
  22. #include <esp_log.h>
  23. #include "sdkconfig.h"
  24. /* wolfSSL */
  25. #include <user_settings.h>
  26. #ifndef WOLFSSL_ESPIDF
  27. #warning "problem with wolfSSL user_settings. Check components/wolfssl/include"
  28. #endif
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #include <wolfssl/wolfcrypt/types.h>
  31. #include <wolfcrypt/benchmark/benchmark.h>
  32. /* check BENCH_ARGV in sdkconfig to determine need to set WOLFSSL_BENCH_ARGV */
  33. #ifdef CONFIG_BENCH_ARGV
  34. #define WOLFSSL_BENCH_ARGV CONFIG_BENCH_ARGV
  35. #define WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS 22 /* arbitrary number of max args */
  36. #endif
  37. /*
  38. ** the wolfssl component can be installed in either:
  39. **
  40. ** - the ESP-IDF component directory
  41. **
  42. ** ** OR **
  43. **
  44. ** - the local project component directory
  45. **
  46. ** it is not recommended to install in both.
  47. **
  48. */
  49. #include "main.h"
  50. static const char* const TAG = "wolfssl_benchmark";
  51. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  52. && defined(WOLFSSL_ATECC508A)
  53. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  54. /* when you need to use a custom slot allocation, */
  55. /* enable the definition CUSTOM_SLOT_ALLOCAION. */
  56. #if defined(CUSTOM_SLOT_ALLOCATION)
  57. static byte mSlotList[ATECC_MAX_SLOT];
  58. /* initialize slot array */
  59. void my_atmel_slotInit()
  60. {
  61. int i;
  62. for(i = 0;i < ATECC_MAX_SLOT;i++) {
  63. mSlotList[i] = ATECC_INVALID_SLOT;
  64. }
  65. }
  66. /* allocate slot depending on slotType */
  67. int my_atmel_alloc(int slotType)
  68. {
  69. int i, slot = -1;
  70. switch(slotType){
  71. case ATMEL_SLOT_ENCKEY:
  72. slot = 4;
  73. break;
  74. case ATMEL_SLOT_DEVICE:
  75. slot = 0;
  76. break;
  77. case ATMEL_SLOT_ECDHE:
  78. slot = 0;
  79. break;
  80. case ATMEL_SLOT_ECDHE_ENC:
  81. slot = 4;
  82. break;
  83. case ATMEL_SLOT_ANY:
  84. for(i = 0;i < ATECC_MAX_SLOT;i++){
  85. if(mSlotList[i] == ATECC_INVALID_SLOT){
  86. slot = i;
  87. break;
  88. }
  89. }
  90. }
  91. return slot;
  92. }
  93. /* free slot array */
  94. void my_atmel_free(int slotId)
  95. {
  96. if(slotId >= 0 && slotId < ATECC_MAX_SLOT){
  97. mSlotList[slotId] = ATECC_INVALID_SLOT;
  98. }
  99. }
  100. #endif /* CUSTOM_SLOT_ALLOCATION */
  101. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  102. /* the following are needed by benchmark.c with args */
  103. #ifdef WOLFSSL_BENCH_ARGV
  104. char* __argv[WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS];
  105. int construct_argv()
  106. {
  107. int cnt = 0;
  108. int i = 0;
  109. int len = 0;
  110. char *_argv; /* buffer for copying the string */
  111. char *ch; /* char pointer to trace the string */
  112. char buff[16] = { 0 }; /* buffer for a argument copy */
  113. ESP_LOGI(TAG, "construct_argv arg:%s\n", CONFIG_BENCH_ARGV);
  114. len = strlen(CONFIG_BENCH_ARGV);
  115. _argv = (char*)malloc(len + 1);
  116. if (!_argv) {
  117. return -1;
  118. }
  119. memset(_argv, 0, len + 1);
  120. memcpy(_argv, CONFIG_BENCH_ARGV, len);
  121. _argv[len] = '\0';
  122. ch = _argv;
  123. __argv[cnt] = malloc(10);
  124. sprintf(__argv[cnt], "benchmark");
  125. __argv[cnt][9] = '\0';
  126. cnt = 1;
  127. while (*ch != '\0') {
  128. /* check that we don't overflow manual arg assembly */
  129. if (cnt >= (WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS)) {
  130. ESP_LOGE(TAG, "Abort construct_argv;"
  131. "Reached maximum defined arguments = %d",
  132. WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS);
  133. break;
  134. }
  135. /* skip white-space */
  136. while (*ch == ' ') { ++ch; }
  137. memset(buff, 0, sizeof(buff));
  138. /* copy each args into buffer */
  139. i = 0;
  140. while ((*ch != ' ') && (*ch != '\0') && (i < 16)) {
  141. buff[i] = *ch;
  142. ++i;
  143. ++ch;
  144. }
  145. /* copy the string into argv */
  146. __argv[cnt] = (char*)malloc(i + 1);
  147. memset(__argv[cnt], 0, i + 1);
  148. memcpy(__argv[cnt], buff, i + 1);
  149. /* next args */
  150. ++cnt;
  151. }
  152. free(_argv);
  153. return (cnt);
  154. }
  155. #endif
  156. /* entry point */
  157. void app_main(void)
  158. {
  159. ESP_LOGI(TAG, "app_main CONFIG_BENCH_ARGV = %s", WOLFSSL_BENCH_ARGV);
  160. /* when using atecc608a on esp32-wroom-32se */
  161. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  162. && defined(WOLFSSL_ATECC508A)
  163. #if defined(CUSTOM_SLOT_ALLOCATION)
  164. my_atmel_slotInit();
  165. /* to register the callback, it needs to be initialized. */
  166. if ((wolfCrypt_Init()) != 0) {
  167. ESP_LOGE(TAG, "wolfCrypt_Init failed");
  168. return;
  169. }
  170. atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
  171. #endif
  172. #endif
  173. #ifdef NO_CRYPT_BENCHMARK
  174. ESP_LOGI(TAG, "NO_CRYPT_BENCHMARK defined, skipping wolf_benchmark_task")
  175. #else
  176. /* although wolfCrypt_Init() may be explicitly called above,
  177. ** note it is still always called in wolf_benchmark_task.
  178. */
  179. wolf_benchmark_task();
  180. /* wolfCrypt_Cleanup should always be called at completion,
  181. ** and is called in wolf_benchmark_task().
  182. */
  183. /* after the test, we'll just wait */
  184. while (1) {
  185. /* nothing */
  186. }
  187. #endif /* NO_CRYPT_BENCHMARK */
  188. } /* main */