main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* benchmark main.c
  2. *
  3. * Copyright (C) 2006-2024 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 "sdkconfig.h"
  23. #include <esp_log.h>
  24. /* wolfSSL */
  25. /* The wolfSSL user_settings.h file is automatically included by the settings.h
  26. * file and should never be explicitly included in any other source files.
  27. * The settings.h should also be listed above wolfssl library include files. */
  28. #if defined(WOLFSSL_USER_SETTINGS)
  29. #include <wolfssl/wolfcrypt/settings.h>
  30. #if defined(WOLFSSL_ESPIDF)
  31. #include <wolfssl/version.h>
  32. #include <wolfssl/wolfcrypt/types.h>
  33. #include <wolfcrypt/benchmark/benchmark.h>
  34. #include <wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h>
  35. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  36. #else
  37. #error "Problem with wolfSSL user_settings. " \
  38. "Check components/wolfssl/include " \
  39. "and confirm WOLFSSL_USER_SETTINGS is defined, " \
  40. "typically in the component CMakeLists.txt"
  41. #endif
  42. #else
  43. /* Define WOLFSSL_USER_SETTINGS project wide for settings.h to include */
  44. /* wolfSSL user settings in ./components/wolfssl/include/user_settings.h */
  45. #error "Missing WOLFSSL_USER_SETTINGS in CMakeLists or Makefile:\
  46. CFLAGS +=-DWOLFSSL_USER_SETTINGS"
  47. #endif
  48. /* Hardware; include after other libraries,
  49. * particularly after freeRTOS from settings.h */
  50. #include <driver/uart.h>
  51. /* set to 0 for one benchmark,
  52. ** set to 1 for continuous benchmark loop */
  53. #define BENCHMARK_LOOP 0
  54. #define THIS_MONITOR_UART_RX_BUFFER_SIZE 200
  55. #ifdef CONFIG_ESP8266_XTAL_FREQ_26
  56. /* 26MHz crystal: 74880 bps */
  57. #define THIS_MONITOR_UART_BAUD_DATE 74880
  58. #else
  59. /* 40MHz crystal: 115200 bps */
  60. #define THIS_MONITOR_UART_BAUD_DATE 115200
  61. #endif
  62. /* check BENCH_ARGV in sdkconfig to determine need to set WOLFSSL_BENCH_ARGV */
  63. #ifdef CONFIG_BENCH_ARGV
  64. #define WOLFSSL_BENCH_ARGV CONFIG_BENCH_ARGV
  65. #define WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS 22 /* arbitrary number of max args */
  66. #endif
  67. /*
  68. ** the wolfssl component can be installed in either:
  69. **
  70. ** - the ESP-IDF component directory
  71. **
  72. ** ** OR **
  73. **
  74. ** - the local project component directory
  75. **
  76. ** it is not recommended to install in both.
  77. **
  78. */
  79. #include "main.h"
  80. static const char* const TAG = "wolfssl_benchmark";
  81. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  82. && defined(WOLFSSL_ATECC508A)
  83. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  84. /* when you need to use a custom slot allocation, */
  85. /* enable the definition CUSTOM_SLOT_ALLOCAION. */
  86. #if defined(CUSTOM_SLOT_ALLOCATION)
  87. static byte mSlotList[ATECC_MAX_SLOT];
  88. int atmel_set_slot_allocator(atmel_slot_alloc_cb alloc, atmel_slot_dealloc_cb dealloc);
  89. /* initialize slot array */
  90. void my_atmel_slotInit()
  91. {
  92. int i;
  93. for(i = 0;i < ATECC_MAX_SLOT;i++) {
  94. mSlotList[i] = ATECC_INVALID_SLOT;
  95. }
  96. }
  97. /* allocate slot depending on slotType */
  98. int my_atmel_alloc(int slotType)
  99. {
  100. int i, slot = -1;
  101. switch(slotType){
  102. case ATMEL_SLOT_ENCKEY:
  103. slot = 4;
  104. break;
  105. case ATMEL_SLOT_DEVICE:
  106. slot = 0;
  107. break;
  108. case ATMEL_SLOT_ECDHE:
  109. slot = 0;
  110. break;
  111. case ATMEL_SLOT_ECDHE_ENC:
  112. slot = 4;
  113. break;
  114. case ATMEL_SLOT_ANY:
  115. for(i = 0;i < ATECC_MAX_SLOT;i++){
  116. if(mSlotList[i] == ATECC_INVALID_SLOT){
  117. slot = i;
  118. break;
  119. }
  120. }
  121. }
  122. return slot;
  123. }
  124. /* free slot array */
  125. void my_atmel_free(int slotId)
  126. {
  127. if(slotId >= 0 && slotId < ATECC_MAX_SLOT){
  128. mSlotList[slotId] = ATECC_INVALID_SLOT;
  129. }
  130. }
  131. #endif /* CUSTOM_SLOT_ALLOCATION */
  132. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  133. /* the following are needed by benchmark.c with args */
  134. #ifdef WOLFSSL_BENCH_ARGV
  135. char* __argv[WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS];
  136. #define ARG_BUFF_SIZE 16
  137. int construct_argv()
  138. {
  139. #define ARG_BUFF_SIZE 16
  140. int cnt = 0;
  141. int i = 0;
  142. int len = 0;
  143. char *_argv; /* buffer for copying the string */
  144. char *ch; /* char pointer to trace the string */
  145. char buff[ARG_BUFF_SIZE] = { 0 }; /* buffer for a argument copy */
  146. ESP_LOGI(TAG, "construct_argv arg:%s\n", CONFIG_BENCH_ARGV);
  147. len = strlen(CONFIG_BENCH_ARGV);
  148. _argv = (char*)malloc(len + 1);
  149. if (!_argv) {
  150. return -1;
  151. }
  152. memset(_argv, 0, len + 1);
  153. memcpy(_argv, CONFIG_BENCH_ARGV, len);
  154. _argv[len] = '\0';
  155. ch = _argv;
  156. __argv[cnt] = malloc(10);
  157. sprintf(__argv[cnt], "benchmark");
  158. __argv[cnt][9] = '\0';
  159. cnt = 1;
  160. while (*ch != '\0') {
  161. /* check that we don't overflow manual arg assembly */
  162. if (cnt >= (WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS)) {
  163. ESP_LOGE(TAG, "Abort construct_argv;"
  164. "Reached maximum defined arguments = %d",
  165. WOLFSSL_BENCH_ARGV_MAX_ARGUMENTS);
  166. break;
  167. }
  168. /* skip white-space */
  169. while (*ch == ' ') { ++ch; }
  170. memset(buff, 0, sizeof(buff));
  171. /* copy each args into buffer */
  172. i = 0;
  173. while ((*ch != ' ') && (*ch != '\0') && (i <= ARG_BUFF_SIZE)) {
  174. buff[i] = *ch;
  175. ++i;
  176. ++ch;
  177. }
  178. /* copy the string into argv */
  179. __argv[cnt] = (char*)malloc(i + 1);
  180. memset(__argv[cnt], 0, i + 1);
  181. memcpy(__argv[cnt], buff, i + 1);
  182. /* next args */
  183. ++cnt;
  184. }
  185. free(_argv);
  186. return (cnt);
  187. }
  188. #endif
  189. /* entry point */
  190. void app_main(void)
  191. {
  192. uart_config_t uart_config = {
  193. .baud_rate = THIS_MONITOR_UART_BAUD_DATE,
  194. .data_bits = UART_DATA_8_BITS,
  195. .parity = UART_PARITY_DISABLE,
  196. .stop_bits = UART_STOP_BITS_1,
  197. };
  198. int stack_start = 0;
  199. word32 loops = 0;
  200. esp_err_t ret = 0;
  201. stack_start = esp_sdk_stack_pointer();
  202. /* uart_set_pin(UART_NUM_0, TX_PIN, RX_PIN,
  203. * UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); */
  204. /* Some targets may need to have UART speed set, such as ESP8266 */
  205. ESP_LOGI(TAG, "UART init");
  206. uart_param_config(UART_NUM_0, &uart_config);
  207. uart_driver_install(UART_NUM_0,
  208. THIS_MONITOR_UART_RX_BUFFER_SIZE, 0, 0, NULL, 0);
  209. ESP_LOGI(TAG, "---------------- wolfSSL Benchmark Example -------------");
  210. ESP_LOGI(TAG, "--------------------------------------------------------");
  211. ESP_LOGI(TAG, "--------------------------------------------------------");
  212. ESP_LOGI(TAG, "---------------------- BEGIN MAIN ----------------------");
  213. ESP_LOGI(TAG, "--------------------------------------------------------");
  214. ESP_LOGI(TAG, "--------------------------------------------------------");
  215. ESP_LOGI(TAG, "Stack Start: 0x%x", stack_start);
  216. #ifdef WOLFSSL_ESP_NO_WATCHDOG
  217. ESP_LOGW(TAG, "Found WOLFSSL_ESP_NO_WATCHDOG, disabling...");
  218. esp_DisableWatchdog();
  219. #endif
  220. #if defined(HAVE_VERSION_EXTENDED_INFO) && defined(WOLFSSL_HAS_METRICS)
  221. esp_ShowExtendedSystemInfo();
  222. #endif
  223. /* all platforms: stack high water mark check */
  224. ESP_LOGI(TAG, "app_main CONFIG_BENCH_ARGV = %s", WOLFSSL_BENCH_ARGV);
  225. /* when using atecc608a on esp32-wroom-32se */
  226. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  227. && defined(WOLFSSL_ATECC508A)
  228. #if defined(CUSTOM_SLOT_ALLOCATION)
  229. my_atmel_slotInit();
  230. /* to register the callback, it needs to be initialized. */
  231. if ((wolfCrypt_Init()) != 0) {
  232. ESP_LOGE(TAG, "wolfCrypt_Init failed");
  233. return;
  234. }
  235. atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
  236. #endif
  237. #endif
  238. #ifdef NO_CRYPT_BENCHMARK
  239. ESP_LOGI(TAG, "NO_CRYPT_BENCHMARK defined, skipping wolf_benchmark_task")
  240. #else
  241. /* Although wolfCrypt_Init() may be explicitly called above,
  242. ** note it is still always called in wolf_benchmark_task.
  243. */
  244. stack_start = uxTaskGetStackHighWaterMark(NULL);
  245. do {
  246. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  247. #ifdef WOLFSSL_BENCH_ARGV
  248. ret = benchmark_test(__argv);
  249. #else
  250. ret = benchmark_test(NULL);
  251. #endif
  252. ESP_LOGI(TAG, "Stack used: %d\n",
  253. stack_start - uxTaskGetStackHighWaterMark(NULL));
  254. esp_hw_show_metrics();
  255. loops++; /* count of the number of tests run before fail. */
  256. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  257. ESP_LOGI(TAG, "loops = %d", loops);
  258. } while (BENCHMARK_LOOP && (ret == 0));
  259. /* Reminder: wolfCrypt_Cleanup() should always be called at completion,
  260. ** and is called in wolf_benchmark_task(). */
  261. #if defined BENCHMARK_LOOP && (BENCHMARK_LOOP == 1)
  262. /* If BENCHMARK_LOOP enabled and we get here, there was likely an error. */
  263. ESP_LOGI(TAG, "Benchmark loops completed: %d", loops);
  264. #endif
  265. #if defined(SINGLE_THREADED)
  266. /* need stack monitor for single thread */
  267. #else
  268. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  269. #endif
  270. #ifdef INCLUDE_uxTaskGetStackHighWaterMark
  271. ESP_LOGI(TAG, "Stack HWM: %d", uxTaskGetStackHighWaterMark(NULL));
  272. ESP_LOGI(TAG, "Stack used: %d", CONFIG_ESP_MAIN_TASK_STACK_SIZE
  273. - (uxTaskGetStackHighWaterMark(NULL)));
  274. #endif
  275. #ifdef WOLFSSL_ESPIDF_VERBOSE_EXIT_MESSAGE
  276. if (ret == 0) {
  277. ESP_LOGI(TAG, WOLFSSL_ESPIDF_VERBOSE_EXIT_MESSAGE("Success!", ret));
  278. }
  279. else {
  280. ESP_LOGE(TAG, WOLFSSL_ESPIDF_VERBOSE_EXIT_MESSAGE("Failed!", ret));
  281. }
  282. #elif defined(WOLFSSL_ESPIDF_EXIT_MESSAGE)
  283. ESP_LOGI(TAG, WOLFSSL_ESPIDF_EXIT_MESSAGE);
  284. #else
  285. ESP_LOGI(TAG, "\n\nDone!\n\n"
  286. "If running from idf.py monitor, press twice: Ctrl+]");
  287. #endif
  288. /* After completion, we'll just wait */
  289. while (1) {
  290. #if defined(SINGLE_THREADED)
  291. while (1);
  292. #else
  293. vTaskDelay(60000);
  294. #endif
  295. } /* done while */
  296. #endif /* NO_CRYPT_BENCHMARK */
  297. }