main.c 6.8 KB

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