test_mbedtls_sha.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* mbedTLS SHA unit tests
  2. */
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include <esp_system.h>
  7. #include "mbedtls/sha1.h"
  8. #include "mbedtls/sha256.h"
  9. #include "mbedtls/sha512.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/semphr.h"
  13. #include "unity.h"
  14. #include "sdkconfig.h"
  15. #include "test_apb_dport_access.h"
  16. #include "sodium/utils.h"
  17. #include "soc/soc_caps.h"
  18. TEST_CASE("mbedtls SHA self-tests", "[mbedtls]")
  19. {
  20. start_apb_access_loop();
  21. TEST_ASSERT_FALSE_MESSAGE(mbedtls_sha1_self_test(1), "SHA1 self-tests should pass.");
  22. TEST_ASSERT_FALSE_MESSAGE(mbedtls_sha256_self_test(1), "SHA256 self-tests should pass.");
  23. TEST_ASSERT_FALSE_MESSAGE(mbedtls_sha512_self_test(1), "SHA512 self-tests should pass.");
  24. verify_apb_access_loop();
  25. }
  26. static const unsigned char *one_hundred_as = (unsigned char *)
  27. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  28. static const unsigned char *one_hundred_bs = (unsigned char *)
  29. "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
  30. static const uint8_t sha256_thousand_as[32] = {
  31. 0x41, 0xed, 0xec, 0xe4, 0x2d, 0x63, 0xe8, 0xd9, 0xbf, 0x51, 0x5a, 0x9b, 0xa6, 0x93, 0x2e, 0x1c,
  32. 0x20, 0xcb, 0xc9, 0xf5, 0xa5, 0xd1, 0x34, 0x64, 0x5a, 0xdb, 0x5d, 0xb1, 0xb9, 0x73, 0x7e, 0xa3
  33. };
  34. static const uint8_t sha256_thousand_bs[32] = {
  35. 0xf6, 0xf1, 0x18, 0xe1, 0x20, 0xe5, 0x2b, 0xe0, 0xbd, 0x0c, 0xfd, 0xf2, 0x79, 0x4c, 0xd1, 0x2c, 0x07, 0x68, 0x6c, 0xc8, 0x71, 0x23, 0x5a, 0xc2, 0xf1, 0x14, 0x59, 0x37, 0x8e, 0x6d, 0x23, 0x5b
  36. };
  37. static const uint8_t sha512_thousand_bs[64] = {
  38. 0xa6, 0x68, 0x68, 0xa3, 0x73, 0x53, 0x2a, 0x5c, 0xc3, 0x3f, 0xbf, 0x43, 0x4e, 0xba, 0x10, 0x86, 0xb3, 0x87, 0x09, 0xe9, 0x14, 0x3f, 0xbf, 0x37, 0x67, 0x8d, 0x43, 0xd9, 0x9b, 0x95, 0x08, 0xd5, 0x80, 0x2d, 0xbe, 0x9d, 0xe9, 0x1a, 0x54, 0xab, 0x9e, 0xbc, 0x8a, 0x08, 0xa0, 0x1a, 0x89, 0xd8, 0x72, 0x68, 0xdf, 0x52, 0x69, 0x7f, 0x1c, 0x70, 0xda, 0xe8, 0x3f, 0xe5, 0xae, 0x5a, 0xfc, 0x9d
  39. };
  40. static const uint8_t sha384_thousand_bs[48] = {
  41. 0x6d, 0xe5, 0xf5, 0x88, 0x57, 0x60, 0x83, 0xff, 0x7c, 0x94, 0x61, 0x5f, 0x8d, 0x96, 0xf2, 0x76, 0xd5, 0x3f, 0x77, 0x0c, 0x8e, 0xc1, 0xbf, 0xb6, 0x04, 0x27, 0xa4, 0xba, 0xea, 0x6c, 0x68, 0x44, 0xbd, 0xb0, 0x9c, 0xef, 0x6a, 0x09, 0x28, 0xe8, 0x1f, 0xfc, 0x95, 0x03, 0x69, 0x99, 0xab, 0x1a
  42. };
  43. static const uint8_t sha1_thousand_as[20] = {
  44. 0x29, 0x1e, 0x9a, 0x6c, 0x66, 0x99, 0x49, 0x49, 0xb5, 0x7b, 0xa5,
  45. 0xe6, 0x50, 0x36, 0x1e, 0x98, 0xfc, 0x36, 0xb1, 0xba
  46. };
  47. TEST_CASE("mbedtls SHA interleaving", "[mbedtls]")
  48. {
  49. mbedtls_sha1_context sha1_ctx;
  50. mbedtls_sha256_context sha256_ctx;
  51. mbedtls_sha512_context sha512_ctx;
  52. unsigned char sha1[20], sha256[32], sha512[64];
  53. mbedtls_sha1_init(&sha1_ctx);
  54. mbedtls_sha256_init(&sha256_ctx);
  55. mbedtls_sha512_init(&sha512_ctx);
  56. TEST_ASSERT_EQUAL(0, mbedtls_sha1_starts_ret(&sha1_ctx));
  57. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&sha256_ctx, false));
  58. TEST_ASSERT_EQUAL(0, mbedtls_sha512_starts_ret(&sha512_ctx, false));
  59. for (int i = 0; i < 10; i++) {
  60. TEST_ASSERT_EQUAL(0, mbedtls_sha1_update_ret(&sha1_ctx, one_hundred_as, 100));
  61. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&sha256_ctx, one_hundred_as, 100));
  62. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&sha512_ctx, one_hundred_bs, 100));
  63. }
  64. TEST_ASSERT_EQUAL(0, mbedtls_sha1_finish_ret(&sha1_ctx, sha1));
  65. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&sha256_ctx, sha256));
  66. TEST_ASSERT_EQUAL(0, mbedtls_sha512_finish_ret(&sha512_ctx, sha512));
  67. mbedtls_sha1_free(&sha1_ctx);
  68. mbedtls_sha256_free(&sha256_ctx);
  69. mbedtls_sha512_free(&sha512_ctx);
  70. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_thousand_bs, sha512, 64, "SHA512 calculation");
  71. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_thousand_as, sha256, 32, "SHA256 calculation");
  72. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha1_thousand_as, sha1, 20, "SHA1 calculation");
  73. }
  74. static xSemaphoreHandle done_sem;
  75. static void tskRunSHA1Test(void *pvParameters)
  76. {
  77. mbedtls_sha1_context sha1_ctx;
  78. unsigned char sha1[20];
  79. for (int i = 0; i < 1000; i++) {
  80. mbedtls_sha1_init(&sha1_ctx);
  81. TEST_ASSERT_EQUAL(0, mbedtls_sha1_starts_ret(&sha1_ctx));
  82. for (int j = 0; j < 10; j++) {
  83. TEST_ASSERT_EQUAL(0, mbedtls_sha1_update_ret(&sha1_ctx, (unsigned char *)one_hundred_as, 100));
  84. }
  85. TEST_ASSERT_EQUAL(0, mbedtls_sha1_finish_ret(&sha1_ctx, sha1));
  86. mbedtls_sha1_free(&sha1_ctx);
  87. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha1_thousand_as, sha1, 20, "SHA1 calculation");
  88. }
  89. xSemaphoreGive(done_sem);
  90. vTaskDelete(NULL);
  91. }
  92. static void tskRunSHA256Test(void *pvParameters)
  93. {
  94. mbedtls_sha256_context sha256_ctx;
  95. unsigned char sha256[32];
  96. for (int i = 0; i < 1000; i++) {
  97. mbedtls_sha256_init(&sha256_ctx);
  98. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&sha256_ctx, false));
  99. for (int j = 0; j < 10; j++) {
  100. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&sha256_ctx, (unsigned char *)one_hundred_bs, 100));
  101. }
  102. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&sha256_ctx, sha256));
  103. mbedtls_sha256_free(&sha256_ctx);
  104. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_thousand_bs, sha256, 32, "SHA256 calculation");
  105. }
  106. xSemaphoreGive(done_sem);
  107. vTaskDelete(NULL);
  108. }
  109. #define SHA_TASK_STACK_SIZE (10*1024)
  110. TEST_CASE("mbedtls SHA multithreading", "[mbedtls]")
  111. {
  112. done_sem = xSemaphoreCreateCounting(4, 0);
  113. xTaskCreate(tskRunSHA1Test, "SHA1Task1", SHA_TASK_STACK_SIZE, NULL, 3, NULL);
  114. xTaskCreate(tskRunSHA1Test, "SHA1Task2", SHA_TASK_STACK_SIZE, NULL, 3, NULL);
  115. xTaskCreate(tskRunSHA256Test, "SHA256Task1", SHA_TASK_STACK_SIZE, NULL, 3, NULL);
  116. xTaskCreate(tskRunSHA256Test, "SHA256Task2", SHA_TASK_STACK_SIZE, NULL, 3, NULL);
  117. for (int i = 0; i < 4; i++) {
  118. if (!xSemaphoreTake(done_sem, 10000 / portTICK_PERIOD_MS)) {
  119. TEST_FAIL_MESSAGE("done_sem not released by test task");
  120. }
  121. }
  122. vSemaphoreDelete(done_sem);
  123. }
  124. void tskRunSHASelftests(void *param)
  125. {
  126. for (int i = 0; i < 5; i++) {
  127. if (mbedtls_sha1_self_test(1)) {
  128. printf("SHA1 self-tests failed.\n");
  129. while (1) {}
  130. }
  131. if (mbedtls_sha256_self_test(1)) {
  132. printf("SHA256 self-tests failed.\n");
  133. while (1) {}
  134. }
  135. #if SOC_SHA_SUPPORT_SHA512
  136. if (mbedtls_sha512_self_test(1)) {
  137. printf("SHA512 self-tests failed.\n");
  138. while (1) {}
  139. }
  140. if (mbedtls_sha512_self_test(1)) {
  141. printf("SHA512 self-tests failed.\n");
  142. while (1) {}
  143. }
  144. #endif //SOC_SHA_SUPPORT_SHA512
  145. }
  146. xSemaphoreGive(done_sem);
  147. vTaskDelete(NULL);
  148. }
  149. TEST_CASE("mbedtls SHA self-tests multithreaded", "[mbedtls]")
  150. {
  151. done_sem = xSemaphoreCreateCounting(2, 0);
  152. xTaskCreate(tskRunSHASelftests, "SHASelftests1", SHA_TASK_STACK_SIZE, NULL, 3, NULL);
  153. xTaskCreate(tskRunSHASelftests, "SHASelftests2", SHA_TASK_STACK_SIZE, NULL, 3, NULL);
  154. const int TIMEOUT_MS = 40000;
  155. for (int i = 0; i < 2; i++) {
  156. if (!xSemaphoreTake(done_sem, TIMEOUT_MS / portTICK_PERIOD_MS)) {
  157. TEST_FAIL_MESSAGE("done_sem not released by test task");
  158. }
  159. }
  160. vSemaphoreDelete(done_sem);
  161. }
  162. TEST_CASE("mbedtls SHA512 clone", "[mbedtls]")
  163. {
  164. mbedtls_sha512_context ctx;
  165. mbedtls_sha512_context clone;
  166. unsigned char sha512[64];
  167. mbedtls_sha512_init(&ctx);
  168. TEST_ASSERT_EQUAL(0, mbedtls_sha512_starts_ret(&ctx, false));
  169. for (int i = 0; i < 5; i++) {
  170. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&ctx, one_hundred_bs, 100));
  171. }
  172. mbedtls_sha512_init(&clone);
  173. mbedtls_sha512_clone(&clone, &ctx);
  174. for (int i = 0; i < 5; i++) {
  175. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&ctx, one_hundred_bs, 100));
  176. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&clone, one_hundred_bs, 100));
  177. }
  178. TEST_ASSERT_EQUAL(0, mbedtls_sha512_finish_ret(&ctx, sha512));
  179. mbedtls_sha512_free(&ctx);
  180. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_thousand_bs, sha512, 64, "SHA512 original calculation");
  181. TEST_ASSERT_EQUAL(0, mbedtls_sha512_finish_ret(&clone, sha512));
  182. mbedtls_sha512_free(&clone);
  183. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_thousand_bs, sha512, 64, "SHA512 cloned calculation");
  184. }
  185. TEST_CASE("mbedtls SHA384 clone", "[mbedtls][")
  186. {
  187. mbedtls_sha512_context ctx;
  188. mbedtls_sha512_context clone;
  189. unsigned char sha384[48];
  190. mbedtls_sha512_init(&ctx);
  191. TEST_ASSERT_EQUAL(0, mbedtls_sha512_starts_ret(&ctx, true));
  192. for (int i = 0; i < 5; i++) {
  193. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&ctx, one_hundred_bs, 100));
  194. }
  195. mbedtls_sha512_init(&clone);
  196. mbedtls_sha512_clone(&clone, &ctx);
  197. for (int i = 0; i < 5; i++) {
  198. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&ctx, one_hundred_bs, 100));
  199. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&clone, one_hundred_bs, 100));
  200. }
  201. TEST_ASSERT_EQUAL(0, mbedtls_sha512_finish_ret(&ctx, sha384));
  202. mbedtls_sha512_free(&ctx);
  203. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha384_thousand_bs, sha384, 48, "SHA512 original calculation");
  204. TEST_ASSERT_EQUAL(0, mbedtls_sha512_finish_ret(&clone, sha384));
  205. mbedtls_sha512_free(&clone);
  206. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha384_thousand_bs, sha384, 48, "SHA512 cloned calculation");
  207. }
  208. TEST_CASE("mbedtls SHA256 clone", "[mbedtls]")
  209. {
  210. mbedtls_sha256_context ctx;
  211. mbedtls_sha256_context clone;
  212. unsigned char sha256[64];
  213. mbedtls_sha256_init(&ctx);
  214. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&ctx, false));
  215. for (int i = 0; i < 5; i++) {
  216. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&ctx, one_hundred_as, 100));
  217. }
  218. mbedtls_sha256_init(&clone);
  219. mbedtls_sha256_clone(&clone, &ctx);
  220. for (int i = 0; i < 5; i++) {
  221. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&ctx, one_hundred_as, 100));
  222. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&clone, one_hundred_as, 100));
  223. }
  224. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&ctx, sha256));
  225. mbedtls_sha256_free(&ctx);
  226. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_thousand_as, sha256, 32, "SHA256 original calculation");
  227. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&clone, sha256));
  228. mbedtls_sha256_free(&clone);
  229. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_thousand_as, sha256, 32, "SHA256 cloned calculation");
  230. }
  231. typedef struct {
  232. mbedtls_sha256_context ctx;
  233. uint8_t result[32];
  234. int ret;
  235. bool done;
  236. } finalise_sha_param_t;
  237. static void tskFinaliseSha(void *v_param)
  238. {
  239. finalise_sha_param_t *param = (finalise_sha_param_t *)v_param;
  240. for (int i = 0; i < 5; i++) {
  241. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&param->ctx, one_hundred_as, 100));
  242. }
  243. param->ret = mbedtls_sha256_finish_ret(&param->ctx, param->result);
  244. mbedtls_sha256_free(&param->ctx);
  245. param->done = true;
  246. vTaskDelete(NULL);
  247. }
  248. TEST_CASE("mbedtls SHA session passed between tasks", "[mbedtls]")
  249. {
  250. finalise_sha_param_t param = { 0 };
  251. mbedtls_sha256_init(&param.ctx);
  252. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&param.ctx, false));
  253. for (int i = 0; i < 5; i++) {
  254. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&param.ctx, one_hundred_as, 100));
  255. }
  256. // pass the SHA context off to a different task
  257. //
  258. // note: at the moment this doesn't crash even if a mutex semaphore is used as the
  259. // engine lock, but it can crash...
  260. xTaskCreate(tskFinaliseSha, "SHAFinalise", SHA_TASK_STACK_SIZE, &param, 3, NULL);
  261. while (!param.done) {
  262. vTaskDelay(1);
  263. }
  264. TEST_ASSERT_EQUAL(0, param.ret);
  265. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_thousand_as, param.result, 32, "SHA256 result from other task");
  266. }
  267. /* Random input generated and hashed using python:
  268. import hashlib
  269. import os, binascii
  270. input = bytearray(os.urandom(150))
  271. arr = ''
  272. for idx, b in enumerate(input):
  273. if idx % 8 == 0:
  274. arr += '\n'
  275. arr += "{}, ".format(hex(b))
  276. digest = hashlib.sha256(input).hexdigest()
  277. */
  278. const uint8_t test_vector[] = {
  279. 0xe4, 0x1a, 0x1a, 0x30, 0x71, 0xd3, 0x94, 0xb0,
  280. 0xc3, 0x7e, 0x99, 0x9f, 0x1a, 0xde, 0x4a, 0x36,
  281. 0xb1, 0x1, 0x81, 0x2b, 0x41, 0x91, 0x11, 0x7f,
  282. 0xd8, 0xe1, 0xd5, 0xe5, 0x52, 0x6d, 0x92, 0xee,
  283. 0x6c, 0xf7, 0x70, 0xea, 0x3a, 0xb, 0xc9, 0x97,
  284. 0xc0, 0x12, 0x6f, 0x10, 0x5b, 0x90, 0xd8, 0x52,
  285. 0x91, 0x69, 0xea, 0xc4, 0x1f, 0xc, 0xcf, 0xc6,
  286. 0xf0, 0x43, 0xc6, 0xa3, 0x1f, 0x46, 0x3c, 0x3d,
  287. 0x25, 0xe5, 0xa8, 0x27, 0x86, 0x85, 0x32, 0x3f,
  288. 0x33, 0xd8, 0x40, 0xc4, 0x41, 0xf6, 0x4b, 0x12,
  289. 0xd8, 0x5e, 0x4, 0x27, 0x42, 0x90, 0x73, 0x4,
  290. 0x8, 0x42, 0xd1, 0x64, 0xd, 0x84, 0x3, 0x1,
  291. 0x76, 0x88, 0xe4, 0x95, 0xdf, 0xe7, 0x62, 0xb4,
  292. 0xb3, 0xb2, 0x7e, 0x6d, 0x78, 0xca, 0x79, 0x82,
  293. 0xcc, 0xba, 0x22, 0xd2, 0x90, 0x2e, 0xe3, 0xa8,
  294. 0x2a, 0x53, 0x3a, 0xb1, 0x9a, 0x7f, 0xb7, 0x8b,
  295. 0xfa, 0x32, 0x47, 0xc1, 0x5c, 0x6, 0x4f, 0x7b,
  296. 0xcd, 0xb3, 0xf4, 0xf1, 0xd0, 0xb5, 0xbf, 0xfb,
  297. 0x7c, 0xc3, 0xa5, 0xb2, 0xc4, 0xd4,
  298. };
  299. const uint8_t test_vector_digest[] = {
  300. 0xff, 0x1c, 0x60, 0xcb, 0x21, 0xf0, 0x63, 0x68,
  301. 0xb9, 0xfc, 0xfe, 0xad, 0x3e, 0xb0, 0x2e, 0xd1,
  302. 0xf9, 0x08, 0x82, 0x82, 0x83, 0x06, 0xc1, 0x8a,
  303. 0x98, 0x5d, 0x36, 0xc0, 0xb7, 0xeb, 0x35, 0xe0,
  304. };
  305. TEST_CASE("mbedtls SHA, input in flash", "[mbedtls]")
  306. {
  307. mbedtls_sha256_context sha256_ctx;
  308. unsigned char sha256[32];
  309. mbedtls_sha256_init(&sha256_ctx);
  310. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&sha256_ctx, false));
  311. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&sha256_ctx, test_vector, sizeof(test_vector)));
  312. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&sha256_ctx, sha256));
  313. mbedtls_sha256_free(&sha256_ctx);
  314. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(test_vector_digest, sha256, 32, "SHA256 calculation");
  315. }
  316. /* Function are not implemented in SW */
  317. #if CONFIG_MBEDTLS_HARDWARE_SHA && SOC_SHA_SUPPORT_SHA512_T
  318. /*
  319. * FIPS-180-2 test vectors
  320. */
  321. static unsigned char sha512T_test_buf[2][113] = {
  322. { "abc" },
  323. {
  324. "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
  325. "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
  326. }
  327. };
  328. static const size_t sha512T_test_buflen[2] = {
  329. 3, 112
  330. };
  331. static const esp_sha_type sha512T_algo[4] = {
  332. SHA2_512224, SHA2_512256, SHA2_512T, SHA2_512T
  333. };
  334. static const size_t sha512T_t_len[4] = { 224, 256, 224, 256 };
  335. static const unsigned char sha512_test_sum[4][32] = {
  336. /* SHA512-224 */
  337. {
  338. 0x46, 0x34, 0x27, 0x0f, 0x70, 0x7b, 0x6a, 0x54,
  339. 0xda, 0xae, 0x75, 0x30, 0x46, 0x08, 0x42, 0xe2,
  340. 0x0e, 0x37, 0xed, 0x26, 0x5c, 0xee, 0xe9, 0xa4,
  341. 0x3e, 0x89, 0x24, 0xaa
  342. },
  343. {
  344. 0x23, 0xfe, 0xc5, 0xbb, 0x94, 0xd6, 0x0b, 0x23,
  345. 0x30, 0x81, 0x92, 0x64, 0x0b, 0x0c, 0x45, 0x33,
  346. 0x35, 0xd6, 0x64, 0x73, 0x4f, 0xe4, 0x0e, 0x72,
  347. 0x68, 0x67, 0x4a, 0xf9
  348. },
  349. /* SHA512-256 */
  350. {
  351. 0x53, 0x04, 0x8e, 0x26, 0x81, 0x94, 0x1e, 0xf9,
  352. 0x9b, 0x2e, 0x29, 0xb7, 0x6b, 0x4c, 0x7d, 0xab,
  353. 0xe4, 0xc2, 0xd0, 0xc6, 0x34, 0xfc, 0x6d, 0x46,
  354. 0xe0, 0xe2, 0xf1, 0x31, 0x07, 0xe7, 0xaf, 0x23
  355. },
  356. {
  357. 0x39, 0x28, 0xe1, 0x84, 0xfb, 0x86, 0x90, 0xf8,
  358. 0x40, 0xda, 0x39, 0x88, 0x12, 0x1d, 0x31, 0xbe,
  359. 0x65, 0xcb, 0x9d, 0x3e, 0xf8, 0x3e, 0xe6, 0x14,
  360. 0x6f, 0xea, 0xc8, 0x61, 0xe1, 0x9b, 0x56, 0x3a
  361. }
  362. /* For SHA512_T testing we use t=224 & t=256
  363. * so the hash digest should be same as above
  364. */
  365. };
  366. /* This will run total of 8 test cases, 2 for each of the below MODE
  367. * SHA512/224, SHA512/256, SHA512/t with t=224 & SHA512/t with t=256
  368. *
  369. * Test is disabled for ESP32 as there is no hardware for SHA512/t
  370. */
  371. TEST_CASE("mbedtls SHA512/t", "[mbedtls]")
  372. {
  373. mbedtls_sha512_context sha512_ctx;
  374. unsigned char sha512[64], k;
  375. for (int i = 0; i < 4; i++) {
  376. for (int j = 0; j < 2; j++) {
  377. k = i * 2 + j;
  378. mbedtls_sha512_init(&sha512_ctx);
  379. TEST_ASSERT_EQUAL(0, mbedtls_sha512_starts_ret(&sha512_ctx, false));
  380. esp_sha512_set_mode(&sha512_ctx, sha512T_algo[i]);
  381. if (i > 1) {
  382. k = (i - 2) * 2 + j;
  383. esp_sha512_set_t(&sha512_ctx, sha512T_t_len[i]);
  384. }
  385. TEST_ASSERT_EQUAL(0, mbedtls_sha512_update_ret(&sha512_ctx, sha512T_test_buf[j], sha512T_test_buflen[j]));
  386. TEST_ASSERT_EQUAL(0, mbedtls_sha512_finish_ret(&sha512_ctx, sha512));
  387. mbedtls_sha512_free(&sha512_ctx);
  388. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_test_sum[k], sha512, sha512T_t_len[i] / 8, "SHA512t calculation");
  389. }
  390. }
  391. }
  392. #endif //CONFIG_MBEDTLS_HARDWARE_SHA
  393. #ifdef CONFIG_SPIRAM_USE_MALLOC
  394. TEST_CASE("mbedtls SHA256 PSRAM DMA", "[mbedtls]")
  395. {
  396. const unsigned CALLS = 256;
  397. const unsigned CALL_SZ = 16 * 1024;
  398. mbedtls_sha256_context sha256_ctx;
  399. unsigned char sha256[32];
  400. // allocate external memory
  401. uint8_t *buf = heap_caps_malloc(CALL_SZ, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM);
  402. TEST_ASSERT(esp_ptr_external_ram(buf));
  403. memset(buf, 0x54, CALL_SZ);
  404. mbedtls_sha256_init(&sha256_ctx);
  405. TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&sha256_ctx, false));
  406. for (int c = 0; c < CALLS; c++) {
  407. TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&sha256_ctx, buf, CALL_SZ));
  408. }
  409. TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&sha256_ctx, sha256));
  410. free(buf);
  411. mbedtls_sha256_free(&sha256_ctx);
  412. /* Check the result. Reference value can be calculated using:
  413. * dd if=/dev/zero bs=$((16*1024)) count=256 | tr '\000' '\124' | sha256sum
  414. */
  415. const char *expected_hash = "8d031167bd706ac337e07aa9129c34ae4ae792d0a79a2c70e7f012102e8adc3d";
  416. char hash_str[sizeof(sha256) * 2 + 1];
  417. sodium_bin2hex(hash_str, sizeof(hash_str), sha256, sizeof(sha256));
  418. TEST_ASSERT_EQUAL_STRING(expected_hash, hash_str);
  419. }
  420. #endif //CONFIG_SPIRAM_USE_MALLOC