main.c 10 KB

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