main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* main.c
  2. *
  3. * Copyright (C) 2006-2022 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 <user_settings.h>
  26. #ifndef WOLFSSL_ESPIDF
  27. #warning "problem with wolfSSL user settings. Check components/wolfssl/include"
  28. #endif
  29. #include <wolfcrypt/test/test.h>
  30. /*
  31. ** the wolfssl component can be installed in either:
  32. **
  33. ** - the ESP-IDF component directory
  34. **
  35. ** ** OR **
  36. **
  37. ** - the local project component directory
  38. **
  39. ** it is not recommended to install in both.
  40. **
  41. */
  42. /*
  43. ** although the wolfcrypt/test includes a default time setting,
  44. ** see the enclosed optional time helper for adding NNTP.
  45. ** be sure to add "time_helper.c" in main/CMakeLists.txt
  46. */
  47. #undef WOLFSSL_USE_TIME_HELPER
  48. #if defined(WOLFSSL_USE_TIME_HELPER)
  49. #include "time_helper.h" */
  50. #endif
  51. /* see wolfssl/wolfcrypt/test/test.h */
  52. extern void wolf_crypt_task();
  53. static const char* const TAG = "wolfssl_test";
  54. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  55. && defined(WOLFSSL_ATECC508A)
  56. #include "wolfssl/wolfcrypt/port/atmel/atmel.h"
  57. /* when you need to use a custom slot allocation, */
  58. /* enable the definition CUSTOM_SLOT_ALLOCAION. */
  59. #if defined(CUSTOM_SLOT_ALLOCATION)
  60. static byte mSlotList[ATECC_MAX_SLOT];
  61. /* initialize slot array */
  62. void my_atmel_slotInit()
  63. {
  64. int i;
  65. for (i = 0; i < ATECC_MAX_SLOT; i++) {
  66. mSlotList[i] = ATECC_INVALID_SLOT;
  67. }
  68. }
  69. /* allocate slot depending on slotType */
  70. int my_atmel_alloc(int slotType)
  71. {
  72. int i, slot = ATECC_INVALID_SLOT;
  73. switch (slotType) {
  74. case ATMEL_SLOT_ENCKEY:
  75. slot = 4;
  76. break;
  77. case ATMEL_SLOT_DEVICE:
  78. slot = 0;
  79. break;
  80. case ATMEL_SLOT_ECDHE:
  81. slot = 0;
  82. break;
  83. case ATMEL_SLOT_ECDHE_ENC:
  84. slot = 4;
  85. break;
  86. case ATMEL_SLOT_ANY:
  87. for (i = 0; i < ATECC_MAX_SLOT; i++) {
  88. if (mSlotList[i] == ATECC_INVALID_SLOT) {
  89. slot = i;
  90. break;
  91. } /* if */
  92. } /* for */
  93. } /* switch */
  94. return slot;
  95. }
  96. /* free slot array */
  97. void my_atmel_free(int slotId)
  98. {
  99. if (slotId >= 0 && slotId < ATECC_MAX_SLOT) {
  100. mSlotList[slotId] = ATECC_INVALID_SLOT;
  101. }
  102. }
  103. #endif /* CUSTOM_SLOT_ALLOCATION */
  104. #endif /* WOLFSSL_ESPWROOM32SE && HAVE_PK_CALLBACK && WOLFSSL_ATECC508A */
  105. /* entry point */
  106. void app_main(void)
  107. {
  108. int rc = 0;
  109. #if defined (WOLFSSL_USE_TIME_HELPER)
  110. set_time();
  111. #endif
  112. /* when using atecc608a on esp32-wroom-32se */
  113. #if defined(WOLFSSL_ESPWROOM32SE) && defined(HAVE_PK_CALLBACKS) \
  114. && defined(WOLFSSL_ATECC508A)
  115. #if defined(CUSTOM_SLOT_ALLOCATION)
  116. my_atmel_slotInit();
  117. /* to register the callback, it needs to be initialized. */
  118. if ((wolfCrypt_Init()) != 0) {
  119. ESP_LOGE(TAG, "wolfCrypt_Init failed");
  120. return;
  121. }
  122. atmel_set_slot_allocator(my_atmel_alloc, my_atmel_free);
  123. #endif
  124. #endif
  125. #ifdef NO_CRYPT_TEST
  126. ESP_LOGI(TAG, "NO_CRYPT_TEST defined, skipping wolf_test_task");
  127. #else
  128. /* Although wolfCrypt_Init() may be explicitly called above,
  129. ** Note it is still always called in wolf_test_task.
  130. */
  131. rc = wolf_test_task();
  132. /* note wolfCrypt_Cleanup() should always be called when finished.
  133. ** This is called at the end of wolf_test_task();
  134. */
  135. if (rc == 0) {
  136. ESP_LOGI(TAG, "wolf_test_task complete success result code = %d", rc);
  137. }
  138. else {
  139. ESP_LOGE(TAG, "wolf_test_task FAIL result code = %d", rc);
  140. /* see wolfssl/wolfcrypt/error-crypt.h */
  141. }
  142. /* after the test, we'll just wait */
  143. while (1) {
  144. /* nothing */
  145. }
  146. #endif
  147. }