test_aes_sha_rsa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sdkconfig.h>
  10. #if CONFIG_IDF_TARGET_ESP32
  11. #include "esp_types.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/semphr.h"
  15. #include "freertos/xtensa_timer.h"
  16. #include "soc/cpu.h"
  17. #include "unity.h"
  18. #include "test_utils.h"
  19. #include "esp32/rom/sha.h"
  20. #include "soc/uart_periph.h"
  21. #include "soc/dport_reg.h"
  22. #include "soc/rtc.h"
  23. #include "esp_log.h"
  24. #include "sha/sha_parallel_engine.h"
  25. #include "aes/esp_aes.h"
  26. #include "mbedtls/rsa.h"
  27. #include "mbedtls/sha256.h"
  28. static const char *TAG = "test";
  29. static volatile bool exit_flag = false;
  30. #define TASK_STACK_SIZE (8*1024)
  31. static void aes_task(void *pvParameters)
  32. {
  33. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  34. ESP_LOGI(TAG, "aes_task is started");
  35. esp_aes_context ctx = {
  36. .key_bytes = 16,
  37. .key = {101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116}
  38. };
  39. const unsigned char input[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  40. unsigned char output[16];
  41. unsigned char output2[16];
  42. while (exit_flag == false) {
  43. memset(output, 0, sizeof(output));
  44. memset(output, 0, sizeof(output2));
  45. esp_internal_aes_encrypt(&ctx, input, output);
  46. esp_internal_aes_decrypt(&ctx, output, output2);
  47. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(input, output2, sizeof(input), "AES must match");
  48. }
  49. xSemaphoreGive(*sema);
  50. vTaskDelete(NULL);
  51. }
  52. static void sha_task(void *pvParameters)
  53. {
  54. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  55. ESP_LOGI(TAG, "sha_task is started");
  56. const char *input = "Space!#$%&()*+,-.0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz~DEL0123456789";
  57. unsigned char output[64];
  58. unsigned char output_origin[64];
  59. esp_sha(SHA2_512, (const unsigned char *)input, sizeof(input), output);
  60. memcpy(output_origin, output, sizeof(output));
  61. while (exit_flag == false) {
  62. memset(output, 0, sizeof(output));
  63. esp_sha(SHA2_512, (const unsigned char *)input, sizeof(input), output);
  64. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(output, output_origin, sizeof(output), "SHA256 must match");
  65. }
  66. xSemaphoreGive(*sema);
  67. vTaskDelete(NULL);
  68. }
  69. static void mbedtls_sha256_task(void *pvParameters)
  70. {
  71. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  72. ESP_LOGI(TAG, "mbedtls_sha256_task is started");
  73. const char *input = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_abcdefghijklmnopqrstuvwxyz~DEL0123456789Space!#$%&()*+,-.0123456789:;<=>?";
  74. mbedtls_sha256_context sha256_ctx;
  75. unsigned char output[32];
  76. unsigned char output_origin[32];
  77. mbedtls_sha256_init(&sha256_ctx);
  78. memset(output, 0, sizeof(output));
  79. mbedtls_sha256_starts_ret(&sha256_ctx, false);
  80. for (int i = 0; i < 3; ++i) {
  81. mbedtls_sha256_update_ret(&sha256_ctx, (unsigned char *)input, 100);
  82. }
  83. mbedtls_sha256_finish_ret(&sha256_ctx, output);
  84. memcpy(output_origin, output, sizeof(output));
  85. while (exit_flag == false) {
  86. mbedtls_sha256_init(&sha256_ctx);
  87. memset(output, 0, sizeof(output));
  88. mbedtls_sha256_starts_ret(&sha256_ctx, false);
  89. for (int i = 0; i < 3; ++i) {
  90. mbedtls_sha256_update_ret(&sha256_ctx, (unsigned char *)input, 100);
  91. }
  92. mbedtls_sha256_finish_ret(&sha256_ctx, output);
  93. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(output, output_origin, sizeof(output), "MBEDTLS SHA256 must match");
  94. }
  95. xSemaphoreGive(*sema);
  96. vTaskDelete(NULL);
  97. }
  98. TEST_CASE("Test shared using AES SHA512 SHA256", "[hw_crypto]")
  99. {
  100. #ifndef CONFIG_FREERTOS_UNICORE
  101. const int max_tasks = 6;
  102. #else
  103. const int max_tasks = 3;
  104. #endif
  105. xSemaphoreHandle exit_sema[max_tasks];
  106. for (int i = 0; i < max_tasks; ++i) {
  107. exit_sema[i] = xSemaphoreCreateBinary();
  108. }
  109. exit_flag = false;
  110. #ifndef CONFIG_FREERTOS_UNICORE
  111. xTaskCreatePinnedToCore(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  112. xTaskCreatePinnedToCore(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  113. xTaskCreatePinnedToCore(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[2], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  114. xTaskCreatePinnedToCore(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[3], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  115. xTaskCreatePinnedToCore(&mbedtls_sha256_task, "mbedtls_sha256_task", TASK_STACK_SIZE, &exit_sema[4], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  116. xTaskCreatePinnedToCore(&mbedtls_sha256_task, "mbedtls_sha256_task", TASK_STACK_SIZE, &exit_sema[5], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  117. #else
  118. xTaskCreate(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL);
  119. xTaskCreate(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL);
  120. xTaskCreate(&mbedtls_sha256_task, "mbedtls_sha256_task", TASK_STACK_SIZE, &exit_sema[2], UNITY_FREERTOS_PRIORITY - 1, NULL);
  121. #endif
  122. ESP_LOGI(TAG, "Waiting for 10s ...");
  123. vTaskDelay(10000 / portTICK_PERIOD_MS);
  124. // set exit flag to let thread exit
  125. exit_flag = true;
  126. for (int i = 0; i < max_tasks; ++i) {
  127. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  128. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  129. }
  130. vSemaphoreDelete(exit_sema[i]);
  131. }
  132. }
  133. static void rsa_task(void *pvParameters)
  134. {
  135. xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
  136. ESP_LOGI(TAG, "rsa_task is started");
  137. while (exit_flag == false) {
  138. mbedtls_rsa_self_test(0);
  139. }
  140. xSemaphoreGive(*sema);
  141. vTaskDelete(NULL);
  142. }
  143. TEST_CASE("Test shared using AES RSA", "[hw_crypto]")
  144. {
  145. #ifndef CONFIG_FREERTOS_UNICORE
  146. const int max_tasks = 2;
  147. #else
  148. const int max_tasks = 2;
  149. #endif
  150. xSemaphoreHandle exit_sema[max_tasks];
  151. for (int i = 0; i < max_tasks; ++i) {
  152. exit_sema[i] = xSemaphoreCreateBinary();
  153. }
  154. exit_flag = false;
  155. #ifndef CONFIG_FREERTOS_UNICORE
  156. xTaskCreatePinnedToCore(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  157. xTaskCreatePinnedToCore(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  158. #else
  159. xTaskCreate(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL);
  160. xTaskCreate(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL);
  161. #endif
  162. ESP_LOGI(TAG, "Waiting for 10s ...");
  163. vTaskDelay(10000 / portTICK_PERIOD_MS);
  164. // set exit flag to let thread exit
  165. exit_flag = true;
  166. for (int i = 0; i < max_tasks; ++i) {
  167. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  168. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  169. }
  170. vSemaphoreDelete(exit_sema[i]);
  171. }
  172. }
  173. TEST_CASE("Test shared using SHA512 RSA", "[hw_crypto]")
  174. {
  175. #ifndef CONFIG_FREERTOS_UNICORE
  176. const int max_tasks = 2;
  177. #else
  178. const int max_tasks = 2;
  179. #endif
  180. xSemaphoreHandle exit_sema[max_tasks];
  181. for (int i = 0; i < max_tasks; ++i) {
  182. exit_sema[i] = xSemaphoreCreateBinary();
  183. }
  184. exit_flag = false;
  185. #ifndef CONFIG_FREERTOS_UNICORE
  186. xTaskCreatePinnedToCore(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 2, NULL, 1);
  187. xTaskCreatePinnedToCore(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  188. #else
  189. xTaskCreate(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL);
  190. xTaskCreate(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL);
  191. #endif
  192. ESP_LOGI(TAG, "Waiting for 10s ...");
  193. vTaskDelay(10000 / portTICK_PERIOD_MS);
  194. // set exit flag to let thread exit
  195. exit_flag = true;
  196. for (int i = 0; i < max_tasks; ++i) {
  197. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  198. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  199. }
  200. vSemaphoreDelete(exit_sema[i]);
  201. }
  202. }
  203. TEST_CASE("Test shared using SHA256 RSA", "[hw_crypto]")
  204. {
  205. #ifndef CONFIG_FREERTOS_UNICORE
  206. const int max_tasks = 2;
  207. #else
  208. const int max_tasks = 2;
  209. #endif
  210. xSemaphoreHandle exit_sema[max_tasks];
  211. for (int i = 0; i < max_tasks; ++i) {
  212. exit_sema[i] = xSemaphoreCreateBinary();
  213. }
  214. exit_flag = false;
  215. #ifndef CONFIG_FREERTOS_UNICORE
  216. xTaskCreatePinnedToCore(&mbedtls_sha256_task, "mbedtls_sha256_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  217. xTaskCreatePinnedToCore(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  218. #else
  219. xTaskCreate(&mbedtls_sha256_task, "mbedtls_sha256_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL);
  220. xTaskCreate(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL);
  221. #endif
  222. ESP_LOGI(TAG, "Waiting for 10s ...");
  223. vTaskDelay(10000 / portTICK_PERIOD_MS);
  224. // set exit flag to let thread exit
  225. exit_flag = true;
  226. for (int i = 0; i < max_tasks; ++i) {
  227. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  228. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  229. }
  230. vSemaphoreDelete(exit_sema[i]);
  231. }
  232. }
  233. TEST_CASE("Test shared using AES SHA RSA", "[hw_crypto]")
  234. {
  235. #ifndef CONFIG_FREERTOS_UNICORE
  236. const int max_tasks = 3;
  237. #else
  238. const int max_tasks = 3;
  239. #endif
  240. xSemaphoreHandle exit_sema[max_tasks];
  241. for (int i = 0; i < max_tasks; ++i) {
  242. exit_sema[i] = xSemaphoreCreateBinary();
  243. }
  244. exit_flag = false;
  245. #ifndef CONFIG_FREERTOS_UNICORE
  246. xTaskCreatePinnedToCore(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  247. xTaskCreatePinnedToCore(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
  248. xTaskCreatePinnedToCore(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[2], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
  249. #else
  250. xTaskCreate(&aes_task, "aes_task", TASK_STACK_SIZE, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL);
  251. xTaskCreate(&sha_task, "sha_task", TASK_STACK_SIZE, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL);
  252. xTaskCreate(&rsa_task, "rsa_task", TASK_STACK_SIZE, &exit_sema[2], UNITY_FREERTOS_PRIORITY - 1, NULL);
  253. #endif
  254. ESP_LOGI(TAG, "Waiting for 10s ...");
  255. vTaskDelay(10000 / portTICK_PERIOD_MS);
  256. // set exit flag to let thread exit
  257. exit_flag = true;
  258. for (int i = 0; i < max_tasks; ++i) {
  259. if (!xSemaphoreTake(exit_sema[i], 2000/portTICK_PERIOD_MS)) {
  260. TEST_FAIL_MESSAGE("exit_sema not released by test task");
  261. }
  262. vSemaphoreDelete(exit_sema[i]);
  263. }
  264. }
  265. #endif // CONFIG_IDF_TARGET_ESP32