main_wip.c.ex 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #if defined(WOLFSSL_SHA512)
  128. ESP_LOGI(TAG, "Found WOLFSSL_SHA512");
  129. #else
  130. ESP_LOGI(TAG, "Missing WOLFSSL_SHA512");
  131. #endif
  132. #if defined(HAVE_ECC)
  133. ESP_LOGI(TAG, "Found HAVE_ECC");
  134. #else
  135. ESP_LOGI(TAG, "Missing HAVE_ECC");
  136. #endif
  137. #if defined(HAVE_CURVE25519)
  138. ESP_LOGI(TAG, "Found HAVE_CURVE25519");
  139. #else
  140. ESP_LOGI(TAG, "Missing HAVE_CURVE25519");
  141. #endif
  142. #if defined(CURVE25519_SMALL)
  143. ESP_LOGI(TAG, "Found CURVE25519_SMALL");
  144. #else
  145. ESP_LOGI(TAG, "Missing CURVE25519_SMALL");
  146. #endif
  147. #if defined(CURVED25519_SMALL)
  148. ESP_LOGI(TAG, "Found CURVED25519_SMALL");
  149. #else
  150. ESP_LOGI(TAG, "Missing CURVED25519_SMALL");
  151. #endif
  152. #if defined(HAVE_ED25519)
  153. ESP_LOGI(TAG, "Found HAVE_ED25519");
  154. #else
  155. ESP_LOGI(TAG, "Missing HAVE_ED25519");
  156. #endif
  157. #if defined(ED25519_SMALL)
  158. ESP_LOGI(TAG, "Found ED25519_SMALL");
  159. #else
  160. ESP_LOGI(TAG, "Missing ED25519_SMALL");
  161. #endif
  162. /* some interesting settings are target specific (ESP32, -C3, -S3, etc */
  163. #if defined(CONFIG_IDF_TARGET_ESP32C3)
  164. /* not available for C3 at this time */
  165. #elif defined(CONFIG_IDF_TARGET_ESP32S3)
  166. ESP_LOGI(TAG, "CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ = %u MHz",
  167. CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ
  168. );
  169. ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
  170. #else
  171. ESP_LOGI(TAG, "CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ = %u MHz",
  172. CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ
  173. );
  174. ESP_LOGI(TAG, "Xthal_have_ccount = %u", Xthal_have_ccount);
  175. #endif
  176. /* all platforms: stack high water mark check */
  177. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  178. /* check to see if we are using hardware encryption */
  179. #if defined(NO_ESP32WROOM32_CRYPT)
  180. ESP_LOGI(TAG, "NO_ESP32WROOM32_CRYPT defined! HW acceleration DISABLED.");
  181. #else
  182. #if defined(CONFIG_IDF_TARGET_ESP32C3)
  183. #error "ESP32WROOM32_CRYPT not yet supported on ESP32-C3"
  184. #elif defined(CONFIG_IDF_TARGET_ESP32S2)
  185. #error "ESP32WROOM32_CRYPT not yet supported on ESP32-S2"
  186. #elif defined(CONFIG_IDF_TARGET_ESP32S3)
  187. #error "ESP32WROOM32_CRYPT not yet supported on ESP32-S3"
  188. #else
  189. ESP_LOGI(TAG, "ESP32WROOM32_CRYPT is enabled.");
  190. #endif
  191. #endif
  192. #if defined (WOLFSSL_USE_TIME_HELPER)
  193. set_time();
  194. #endif
  195. /* when using atecc608a on esp32-wroom-32se */
  196. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  197. && defined(WOLFSSL_ATECC508A)
  198. #if defined(CUSTOM_SLOT_ALLOCATION)
  199. my_atmel_slotInit();
  200. /* to register the callback, it needs to be initialized. */
  201. if ((wolfCrypt_Init()) != 0) {
  202. ESP_LOGE(TAG, "wolfCrypt_Init failed");
  203. return;
  204. }
  205. atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
  206. #endif
  207. #endif
  208. #ifdef NO_CRYPT_TEST
  209. ESP_LOGI(TAG, "NO_CRYPT_TEST defined, skipping wolf_test_task");
  210. #else
  211. /* Although wolfCrypt_Init() may be explicitly called above,
  212. ** Note it is still always called in wolf_test_task.
  213. */
  214. rc = wolf_test_task();
  215. /* note wolfCrypt_Cleanup() should always be called when finished.
  216. ** This is called at the end of wolf_test_task();
  217. */
  218. if (rc == 0) {
  219. ESP_LOGI(TAG, "wolf_test_task complete success result code = %d", rc);
  220. }
  221. else {
  222. ESP_LOGE(TAG, "wolf_test_task FAIL result code = %d", rc);
  223. /* see wolfssl/wolfcrypt/error-crypt.h */
  224. }
  225. /* all platforms: stack high water mark check */
  226. ESP_LOGI(TAG, "Stack HWM: %d\n", uxTaskGetStackHighWaterMark(NULL));
  227. /* after the test, we'll just wait */
  228. while (1) {
  229. /* nothing */
  230. }
  231. #endif
  232. }