main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* test 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 <esp_log.h>
  23. #include "sdkconfig.h"
  24. /* wolfSSL */
  25. /* Always include wolfcrypt/settings.h before any other wolfSSL file. */
  26. /* Reminder: settings.h pulls in user_settings.h; don't include it here. */
  27. #if defined(WOLFSSL_USER_SETTINGS)
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #if defined(WOLFSSL_ESPIDF)
  30. #include <wolfssl/version.h>
  31. #include <wolfssl/wolfcrypt/types.h>
  32. #include <wolfcrypt/test/test.h>
  33. #include <wolfssl/wolfcrypt/port/Espressif/esp-sdk-lib.h>
  34. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  35. #else
  36. #error "Problem with wolfSSL user_settings. " \
  37. "Check components/wolfssl/include " \
  38. "and confirm WOLFSSL_USER_SETTINGS is defined, " \
  39. "typically in the component CMakeLists.txt"
  40. #endif
  41. #else
  42. /* Define WOLFSSL_USER_SETTINGS project wide for settings.h to include */
  43. /* wolfSSL user settings in ./components/wolfssl/include/user_settings.h */
  44. #error "Missing WOLFSSL_USER_SETTINGS in CMakeLists or Makefile:\
  45. CFLAGS +=-DWOLFSSL_USER_SETTINGS"
  46. #endif
  47. /* Hardware; include after other libraries,
  48. * particularly after freeRTOS from settings.h */
  49. #include <driver/uart.h>
  50. /* set to 0 for one test,
  51. ** set to 1 for continuous test loop */
  52. #define TEST_LOOP 0
  53. #define THIS_MONITOR_UART_RX_BUFFER_SIZE 200
  54. #ifdef CONFIG_ESP8266_XTAL_FREQ_26
  55. /* 26MHz crystal: 74880 bps */
  56. #define THIS_MONITOR_UART_BAUD_DATE 74880
  57. #else
  58. /* 40MHz crystal: 115200 bps */
  59. #define THIS_MONITOR_UART_BAUD_DATE 115200
  60. #endif
  61. /*
  62. ** the wolfssl component can be installed in either:
  63. **
  64. ** - the ESP-IDF component directory
  65. **
  66. ** ** OR **
  67. **
  68. ** - the local project component directory
  69. **
  70. ** it is not recommended to install in both.
  71. **
  72. */
  73. /*
  74. ** although the wolfcrypt/test includes a default time setting,
  75. ** see the enclosed optional time helper for adding NNTP.
  76. ** be sure to add "time_helper.c" in main/CMakeLists.txt
  77. */
  78. #undef WOLFSSL_USE_TIME_HELPER
  79. #if defined(WOLFSSL_USE_TIME_HELPER)
  80. #include "time_helper.h"
  81. #endif
  82. /* see wolfssl/wolfcrypt/test/test.h */
  83. extern void wolf_crypt_task();
  84. static const char* const TAG = "wolfssl_test";
  85. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  86. && defined(WOLFSSL_ATECC508A)
  87. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  88. /* when you need to use a custom slot allocation, */
  89. /* enable the definition CUSTOM_SLOT_ALLOCAION. */
  90. #if defined(CUSTOM_SLOT_ALLOCATION)
  91. static byte mSlotList[ATECC_MAX_SLOT];
  92. /* initialize slot array */
  93. void my_atmel_slotInit()
  94. {
  95. int i;
  96. for (i = 0; i < ATECC_MAX_SLOT; i++) {
  97. mSlotList[i] = ATECC_INVALID_SLOT;
  98. }
  99. }
  100. /* allocate slot depending on slotType */
  101. int my_atmel_alloc(int slotType)
  102. {
  103. int i, slot = ATECC_INVALID_SLOT;
  104. switch (slotType) {
  105. case ATMEL_SLOT_ENCKEY:
  106. slot = 4;
  107. break;
  108. case ATMEL_SLOT_DEVICE:
  109. slot = 0;
  110. break;
  111. case ATMEL_SLOT_ECDHE:
  112. slot = 0;
  113. break;
  114. case ATMEL_SLOT_ECDHE_ENC:
  115. slot = 4;
  116. break;
  117. case ATMEL_SLOT_ANY:
  118. for (i = 0; i < ATECC_MAX_SLOT; i++) {
  119. if (mSlotList[i] == ATECC_INVALID_SLOT) {
  120. slot = i;
  121. break;
  122. } /* if */
  123. } /* for */
  124. } /* switch */
  125. return slot;
  126. }
  127. /* free slot array */
  128. void my_atmel_free(int slotId)
  129. {
  130. if (slotId >= 0 && slotId < ATECC_MAX_SLOT) {
  131. mSlotList[slotId] = ATECC_INVALID_SLOT;
  132. }
  133. }
  134. #endif /* CUSTOM_SLOT_ALLOCATION */
  135. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  136. /* entry point */
  137. void app_main(void)
  138. {
  139. uart_config_t uart_config = {
  140. .baud_rate = THIS_MONITOR_UART_BAUD_DATE,
  141. .data_bits = UART_DATA_8_BITS,
  142. .parity = UART_PARITY_DISABLE,
  143. .stop_bits = UART_STOP_BITS_1,
  144. };
  145. int stack_start = 0;
  146. int loops = 0;
  147. esp_err_t ret = 0;
  148. stack_start = esp_sdk_stack_pointer();
  149. /* uart_set_pin(UART_NUM_0, TX_PIN, RX_PIN,
  150. * UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE); */
  151. /* Some targets may need to have UART speed set, such as ESP8266 */
  152. ESP_LOGI(TAG, "UART init");
  153. uart_param_config(UART_NUM_0, &uart_config);
  154. uart_driver_install(UART_NUM_0,
  155. THIS_MONITOR_UART_RX_BUFFER_SIZE, 0, 0, NULL, 0);
  156. ESP_LOGI(TAG, "------------------ wolfSSL Test Example ----------------");
  157. ESP_LOGI(TAG, "--------------------------------------------------------");
  158. ESP_LOGI(TAG, "--------------------------------------------------------");
  159. ESP_LOGI(TAG, "---------------------- BEGIN MAIN ----------------------");
  160. ESP_LOGI(TAG, "--------------------------------------------------------");
  161. ESP_LOGI(TAG, "--------------------------------------------------------");
  162. ESP_LOGI(TAG, "Stack Start: 0x%x", stack_start);
  163. #ifdef WOLFSSL_ESP_NO_WATCHDOG
  164. ESP_LOGW(TAG, "Found WOLFSSL_ESP_NO_WATCHDOG, disabling...");
  165. esp_DisableWatchdog();
  166. #endif
  167. #ifdef ESP_TASK_MAIN_STACK
  168. ESP_LOGI(TAG, "ESP_TASK_MAIN_STACK: %d", ESP_TASK_MAIN_STACK);
  169. #endif
  170. #ifdef TASK_EXTRA_STACK_SIZE
  171. ESP_LOGI(TAG, "TASK_EXTRA_STACK_SIZE: %d", TASK_EXTRA_STACK_SIZE);
  172. #endif
  173. #ifdef INCLUDE_uxTaskGetStackHighWaterMark
  174. ESP_LOGI(TAG, "CONFIG_ESP_MAIN_TASK_STACK_SIZE = %d bytes (%d words)",
  175. CONFIG_ESP_MAIN_TASK_STACK_SIZE,
  176. (int)(CONFIG_ESP_MAIN_TASK_STACK_SIZE / sizeof(void*)));
  177. /* Returns the high water mark of the stack associated with xTask. That is,
  178. * the minimum free stack space there has been (in bytes not words, unlike
  179. * vanilla FreeRTOS) since the task started. The smaller the returned
  180. * number the closer the task has come to overflowing its stack.
  181. * see Espressif esp32/api-reference/system/freertos_idf.html
  182. */
  183. stack_start = uxTaskGetStackHighWaterMark(NULL);
  184. ESP_LOGI(TAG, "Stack Start HWM: %d bytes", stack_start);
  185. #endif
  186. #if defined(HAVE_VERSION_EXTENDED_INFO)
  187. esp_ShowExtendedSystemInfo();
  188. #endif
  189. /* all platforms: stack high water mark check */
  190. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  191. #if defined (WOLFSSL_USE_TIME_HELPER)
  192. set_time();
  193. #endif
  194. /* when using atecc608a on esp32-WROOM-32se */
  195. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  196. && defined(WOLFSSL_ATECC508A)
  197. #if defined(CUSTOM_SLOT_ALLOCATION)
  198. my_atmel_slotInit();
  199. /* to register the callback, it needs to be initialized. */
  200. if ((wolfCrypt_Init()) != 0) {
  201. ESP_LOGE(TAG, "wolfCrypt_Init failed");
  202. return;
  203. }
  204. atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
  205. #endif
  206. #endif
  207. #ifdef NO_CRYPT_TEST
  208. ESP_LOGI(TAG, "NO_CRYPT_TEST defined, skipping wolf_test_task");
  209. #else
  210. /* Although wolfCrypt_Init() may be explicitly called above,
  211. ** note it is still always called in wolf_test_task.
  212. */
  213. stack_start = uxTaskGetStackHighWaterMark(NULL);
  214. do {
  215. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  216. ret = wolf_test_task();
  217. #if defined(WOLFSSL_ESP32_CRYPT_RSA_PRI) && defined(WOLFSSL_HW_METRICS)
  218. esp_hw_show_metrics();
  219. #endif
  220. loops++; /* count of the number of tests run before fail. */
  221. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  222. ESP_LOGI(TAG, "loops = %d", loops);
  223. } while (TEST_LOOP && (ret == 0));
  224. /* Reminder: wolfCrypt_Cleanup() should always be called at completion,
  225. ** and is called in wolf_test_task(). */
  226. #if defined TEST_LOOP && (TEST_LOOP == 1)
  227. ESP_LOGI(TAG, "Test loops completed: %d", loops);
  228. #endif
  229. #if defined(SINGLE_THREADED)
  230. /* need stack monitor for single thread */
  231. #else
  232. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  233. #endif
  234. #if defined(DEBUG_WOLFSSL) && defined(WOLFSSL_ESP32_CRYPT_RSA_PRI)
  235. esp_hw_show_mp_metrics();
  236. #endif
  237. #ifdef INCLUDE_uxTaskGetStackHighWaterMark
  238. ESP_LOGI(TAG, "Stack HWM: %d", uxTaskGetStackHighWaterMark(NULL));
  239. ESP_LOGI(TAG, "Stack used: %d", CONFIG_ESP_MAIN_TASK_STACK_SIZE
  240. - (uxTaskGetStackHighWaterMark(NULL)));
  241. #endif
  242. #ifdef WOLFSSL_ESPIDF_VERBOSE_EXIT_MESSAGE
  243. if (ret == 0) {
  244. ESP_LOGI(TAG, WOLFSSL_ESPIDF_VERBOSE_EXIT_MESSAGE("Success!", ret));
  245. }
  246. else {
  247. ESP_LOGE(TAG, WOLFSSL_ESPIDF_VERBOSE_EXIT_MESSAGE("Failed!", ret));
  248. }
  249. #elif defined(WOLFSSL_ESPIDF_EXIT_MESSAGE)
  250. ESP_LOGI(TAG, WOLFSSL_ESPIDF_EXIT_MESSAGE);
  251. #else
  252. ESP_LOGI(TAG, "\n\nDone!\n\n"
  253. "If running from idf.py monitor, press twice: Ctrl+]");
  254. #endif
  255. /* After completion, we'll just wait */
  256. while (1) {
  257. #if defined(SINGLE_THREADED)
  258. while (1);
  259. #else
  260. vTaskDelay(60000);
  261. #endif
  262. } /* done while */
  263. #endif
  264. }