test_aes_sha_rsa.c 11 KB

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