main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* 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. #ifdef WOLFSSL_USER_SETTINGS
  28. /* Unlike other examples with wolfSSL as a local component, this */
  29. /* example tests wolSSL *in* the ESP-IDF. If you get an error: */
  30. /* wolfssl/wolfcrypt/settings.h: No such file or directory */
  31. /* Then wolfSSL is missing from the ESP-IDF components */
  32. #include <wolfssl/wolfcrypt/settings.h>
  33. #ifndef WOLFSSL_ESPIDF
  34. #warning "Problem with wolfSSL user_settings."
  35. #warning "Check components/wolfssl/include"
  36. #endif
  37. #include <wolfcrypt/benchmark/benchmark.h>
  38. #include <wolfssl/version.h>
  39. #include <wolfcrypt/test/test.h>
  40. #else
  41. #error "Missing WOLFSSL_USER_SETTINGS in CMakeLists or Makefile: \
  42. CFLAGS +=-DWOLFSSL_USER_SETTINGS"
  43. #endif
  44. /*
  45. ** the wolfssl component can be installed in either:
  46. **
  47. ** - the ESP-IDF component directory
  48. **
  49. ** ** OR **
  50. **
  51. ** - the local project component directory
  52. **
  53. ** it is not recommended to install in both.
  54. **
  55. */
  56. /*
  57. ** although the wolfcrypt/test includes a default time setting,
  58. ** see the enclosed optional time helper for adding NNTP.
  59. ** be sure to add "time_helper.c" in main/CMakeLists.txt
  60. */
  61. #undef WOLFSSL_USE_TIME_HELPER
  62. #if defined(WOLFSSL_USE_TIME_HELPER)
  63. #include "time_helper.h" */
  64. #endif
  65. /* see wolfssl/wolfcrypt/test/test.h */
  66. extern void wolf_crypt_task();
  67. static const char* const TAG = "wolfssl_test";
  68. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  69. && defined(WOLFSSL_ATECC508A)
  70. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  71. /* when you need to use a custom slot allocation, */
  72. /* enable the definition CUSTOM_SLOT_ALLOCAION. */
  73. #if defined(CUSTOM_SLOT_ALLOCATION)
  74. static byte mSlotList[ATECC_MAX_SLOT];
  75. /* initialize slot array */
  76. void my_atmel_slotInit()
  77. {
  78. int i;
  79. for (i = 0; i < ATECC_MAX_SLOT; i++) {
  80. mSlotList[i] = ATECC_INVALID_SLOT;
  81. }
  82. }
  83. /* allocate slot depending on slotType */
  84. int my_atmel_alloc(int slotType)
  85. {
  86. int i, slot = ATECC_INVALID_SLOT;
  87. switch (slotType) {
  88. case ATMEL_SLOT_ENCKEY:
  89. slot = 4;
  90. break;
  91. case ATMEL_SLOT_DEVICE:
  92. slot = 0;
  93. break;
  94. case ATMEL_SLOT_ECDHE:
  95. slot = 0;
  96. break;
  97. case ATMEL_SLOT_ECDHE_ENC:
  98. slot = 4;
  99. break;
  100. case ATMEL_SLOT_ANY:
  101. for (i = 0; i < ATECC_MAX_SLOT; i++) {
  102. if (mSlotList[i] == ATECC_INVALID_SLOT) {
  103. slot = i;
  104. break;
  105. } /* if */
  106. } /* for */
  107. } /* switch */
  108. return slot;
  109. }
  110. /* free slot array */
  111. void my_atmel_free(int slotId)
  112. {
  113. if (slotId >= 0 && slotId < ATECC_MAX_SLOT) {
  114. mSlotList[slotId] = ATECC_INVALID_SLOT;
  115. }
  116. }
  117. #endif /* CUSTOM_SLOT_ALLOCATION */
  118. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  119. /* entry point */
  120. void app_main(void)
  121. {
  122. int rc = 0;
  123. ESP_LOGI(TAG, "--------------------------------------------------------");
  124. ESP_LOGI(TAG, "--------------------------------------------------------");
  125. ESP_LOGI(TAG, "---------------------- BEGIN MAIN ----------------------");
  126. ESP_LOGI(TAG, "--------------------------------------------------------");
  127. ESP_LOGI(TAG, "--------------------------------------------------------");
  128. ESP_LOGI(TAG, "CONFIG_IDF_TARGET = %s", CONFIG_IDF_TARGET);
  129. ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_STRING = %s", LIBWOLFSSL_VERSION_STRING);
  130. #if defined(LIBWOLFSSL_VERSION_GIT_HASH)
  131. ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_GIT_HASH = %s", LIBWOLFSSL_VERSION_GIT_HASH);
  132. #endif
  133. #if defined(LIBWOLFSSL_VERSION_GIT_SHORT_HASH )
  134. ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_GIT_SHORT_HASH = %s", LIBWOLFSSL_VERSION_GIT_SHORT_HASH);
  135. #endif
  136. #if defined(LIBWOLFSSL_VERSION_GIT_HASH_DATE)
  137. ESP_LOGI(TAG, "LIBWOLFSSL_VERSION_GIT_HASH_DATE = %s", LIBWOLFSSL_VERSION_GIT_HASH_DATE);
  138. #endif
  139. /* some interesting settings are target specific (ESP32, -C3, -S3, etc */
  140. #if defined(CONFIG_IDF_TARGET_ESP32C2) || defined(CONFIG_IDF_TARGET_ESP32C3)
  141. /* TODO CPU_FREQ_MHZ not available for C2/C3 at this time */
  142. #elif defined(CONFIG_IDF_TARGET_ESP32S3)
  143. ESP_LOGI(TAG, "CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ = %u MHz",
  144. CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  145. );
  146. ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
  147. #else
  148. ESP_LOGI(TAG, "CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ = %u MHz",
  149. CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
  150. );
  151. ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
  152. #endif
  153. /* all platforms: stack high water mark check */
  154. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  155. /* check to see if we are using hardware encryption */
  156. #if defined(NO_ESP32_CRYPT)
  157. ESP_LOGI(TAG, "NO_ESP32_CRYPT defined! HW acceleration DISABLED.");
  158. #else
  159. #if defined(CONFIG_IDF_TARGET_ESP32C2) || defined(CONFIG_IDF_TARGET_ESP32C3)
  160. #error "ESP32_CRYPT not yet supported on ESP32-C3"
  161. #elif defined(CONFIG_IDF_TARGET_ESP32S2)
  162. #error "ESP32_CRYPT not yet supported on ESP32-S2"
  163. #elif defined(CONFIG_IDF_TARGET_ESP32S3)
  164. #error "ESP32_CRYPT not yet supported on ESP32-S3"
  165. #else
  166. ESP_LOGI(TAG, "ESP32_CRYPT is enabled.");
  167. #endif
  168. #endif
  169. #if defined (WOLFSSL_USE_TIME_HELPER)
  170. set_time();
  171. #endif
  172. /* when using atecc608a on esp32-wroom-32se */
  173. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  174. && defined(WOLFSSL_ATECC508A)
  175. #if defined(CUSTOM_SLOT_ALLOCATION)
  176. my_atmel_slotInit();
  177. /* to register the callback, it needs to be initialized. */
  178. if ((wolfCrypt_Init()) != 0) {
  179. ESP_LOGE(TAG, "wolfCrypt_Init failed");
  180. return;
  181. }
  182. atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
  183. #endif
  184. #endif
  185. #ifdef NO_CRYPT_TEST
  186. ESP_LOGI(TAG, "NO_CRYPT_TEST defined, skipping wolf_test_task");
  187. #else
  188. /* Although wolfCrypt_Init() may be explicitly called above,
  189. ** Note it is still always called in wolf_test_task.
  190. */
  191. rc = wolf_test_task();
  192. /* note wolfCrypt_Cleanup() should always be called when finished.
  193. ** This is called at the end of wolf_test_task();
  194. */
  195. if (rc == 0) {
  196. ESP_LOGI(TAG, "wolf_test_task complete success result code = %d", rc);
  197. }
  198. else {
  199. ESP_LOGE(TAG, "wolf_test_task FAIL result code = %d", rc);
  200. /* see wolfssl/wolfcrypt/error-crypt.h */
  201. }
  202. /* after the test, we'll just wait */
  203. while (1) {
  204. /* nothing */
  205. }
  206. #endif
  207. }