test_sha_block.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <inttypes.h>
  10. #include "esp_types.h"
  11. #include "soc/soc_caps.h"
  12. #include "esp_heap_caps.h"
  13. #include "unity.h"
  14. #include "esp_heap_caps.h"
  15. #include "memory_checks.h"
  16. #include "unity_fixture.h"
  17. #include "sha_block.h"
  18. #if SOC_SHA_SUPPORTED
  19. TEST_GROUP(sha);
  20. TEST_SETUP(sha)
  21. {
  22. test_utils_record_free_mem();
  23. TEST_ESP_OK(test_utils_set_leak_level(0, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL));
  24. }
  25. TEST_TEAR_DOWN(sha)
  26. {
  27. test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL),
  28. test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL));
  29. }
  30. #if SOC_SHA_SUPPORT_SHA1
  31. TEST(sha, test_sha1)
  32. {
  33. uint8_t sha1_result[20] = { 0 };
  34. uint8_t *buffer = heap_caps_calloc(BUFFER_SZ, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  35. TEST_ASSERT_NOT_NULL(buffer);
  36. memset(buffer, 0xEE, BUFFER_SZ);
  37. const uint8_t sha1_expected[20] = { 0x09, 0x23, 0x02, 0xfb, 0x2d, 0x36, 0x42, 0xec,
  38. 0xc5, 0xfa, 0xd5, 0x8f, 0xdb, 0xc3, 0x8d, 0x5c,
  39. 0x97, 0xd6, 0x17, 0xee };
  40. sha1_block(SHA1, buffer, BUFFER_SZ, sha1_result);
  41. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha1_expected, sha1_result, sizeof(sha1_expected));
  42. heap_caps_free(buffer);
  43. }
  44. #endif /* SOC_SHA_SUPPORT_SHA1 */
  45. #if SOC_SHA_SUPPORT_SHA224
  46. TEST(sha, test_sha224)
  47. {
  48. uint8_t sha224_result[28] = { 0 };
  49. uint8_t *buffer = heap_caps_calloc(BUFFER_SZ, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  50. TEST_ASSERT_NOT_NULL(buffer);
  51. memset(buffer, 0xEE, BUFFER_SZ);
  52. const uint8_t sha224_expected[28] = { 0x69, 0xfd, 0x84, 0x30, 0xd9, 0x4a, 0x44, 0x96,
  53. 0x41, 0xc4, 0xab, 0xab, 0x89, 0x53, 0xa9, 0x1f,
  54. 0x4b, 0xfa, 0x5f, 0x2c, 0xa0, 0x72, 0x5f, 0x6b,
  55. 0xec, 0xd1, 0x47, 0xf9};
  56. sha256_block(SHA2_224, buffer, BUFFER_SZ, sha224_result);
  57. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha224_expected, sha224_result, sizeof(sha224_expected));
  58. heap_caps_free(buffer);
  59. }
  60. #endif /* SOC_SHA_SUPPORT_SHA224 */
  61. #if SOC_SHA_SUPPORT_SHA256
  62. TEST(sha, test_sha256)
  63. {
  64. uint8_t sha256_result[32] = { 0 };
  65. uint8_t *buffer = heap_caps_calloc(BUFFER_SZ, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  66. TEST_ASSERT_NOT_NULL(buffer);
  67. memset(buffer, 0xEE, BUFFER_SZ);
  68. const uint8_t sha256_expected[32] = { 0x0c, 0x67, 0x8d, 0x7b, 0x8a, 0x3e, 0x9e, 0xc0,
  69. 0xb5, 0x61, 0xaa, 0x51, 0xd8, 0xfd, 0x42, 0x70,
  70. 0xd6, 0x11, 0x2a, 0xec, 0x4c, 0x72, 0x9b, 0x2c,
  71. 0xa4, 0xc6, 0x04, 0x80, 0x93, 0x4d, 0xc9, 0x99 };
  72. sha256_block(SHA2_256, buffer, BUFFER_SZ, sha256_result);
  73. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha256_expected, sha256_result, sizeof(sha256_expected));
  74. heap_caps_free(buffer);
  75. }
  76. #endif /* SOC_SHA_SUPPORT_SHA256 */
  77. #if SOC_SHA_SUPPORT_SHA384
  78. TEST(sha, test_sha384)
  79. {
  80. uint8_t sha384_result[48] = { 0 };
  81. uint8_t *buffer = heap_caps_calloc(BUFFER_SZ, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  82. TEST_ASSERT_NOT_NULL(buffer);
  83. memset(buffer, 0xEE, BUFFER_SZ);
  84. const uint8_t sha384_expected[48] = { 0xf2, 0x7c, 0x75, 0x16, 0xa9, 0xe6, 0xe5, 0xe2,
  85. 0x4d, 0x8b, 0xe4, 0x6b, 0xc5, 0xb3, 0x25, 0xb1,
  86. 0x10, 0xc2, 0xb4, 0x7d, 0xb7, 0xe1, 0xee, 0x1c,
  87. 0xbd, 0xde, 0x52, 0x9d, 0xaa, 0x31, 0xda, 0x88,
  88. 0xfe, 0xec, 0xd5, 0x38, 0x59, 0x28, 0x93, 0xc7,
  89. 0x1c, 0x1a, 0x0b, 0x3b, 0x4e, 0x06, 0x48, 0xa7 };
  90. sha512_block(SHA2_384, buffer, BUFFER_SZ, sha384_result);
  91. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha384_expected, sha384_result, sizeof(sha384_expected));
  92. heap_caps_free(buffer);
  93. }
  94. #endif /* SOC_SHA_SUPPORT_SHA384 */
  95. #if SOC_SHA_SUPPORT_SHA512
  96. TEST(sha, test_sha512)
  97. {
  98. uint8_t sha512_result[64] = { 0 };
  99. uint8_t *buffer = heap_caps_calloc(BUFFER_SZ, sizeof(uint8_t), MALLOC_CAP_INTERNAL);
  100. TEST_ASSERT_NOT_NULL(buffer);
  101. memset(buffer, 0xEE, BUFFER_SZ);
  102. const uint8_t sha512_expected[64] = { 0x7f, 0xca, 0x1c, 0x81, 0xc6, 0xc7, 0x1e, 0x49,
  103. 0x1f, 0x4a, 0x35, 0x50, 0xb0, 0x0c, 0xd9, 0xbf,
  104. 0x3e, 0xba, 0x90, 0x31, 0x08, 0xc7, 0xb3, 0xf0,
  105. 0x58, 0x11, 0xd3, 0x29, 0xee, 0xa0, 0x4f, 0x3b,
  106. 0xe4, 0x60, 0xd2, 0xc7, 0x2e, 0x50, 0x39, 0x68,
  107. 0xf7, 0x27, 0x2e, 0x71, 0xbc, 0x9f, 0x10, 0xfc,
  108. 0x9d, 0x75, 0xb5, 0x57, 0x74, 0x8d, 0xb9, 0x4b,
  109. 0x69, 0x1a, 0x9c, 0x5f, 0x30, 0x61, 0xca, 0x3b };
  110. sha512_block(SHA2_512, buffer, BUFFER_SZ, sha512_result);
  111. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha512_expected, sha512_result, sizeof(sha512_expected));
  112. heap_caps_free(buffer);
  113. }
  114. #endif /* SOC_SHA_SUPPORT_SHA512 */
  115. #if SOC_SHA_SUPPORT_SHA512_T
  116. TEST(sha, test_sha512t)
  117. {
  118. unsigned char sha512[64], k;
  119. for (int i = 0; i < 4; i++) {
  120. for (int j = 0; j < 2; j++) {
  121. k = i * 2 + j;
  122. if (i > 1) {
  123. k = (i - 2) * 2 + j;
  124. }
  125. sha512t_block(sha512T_algo[i], sha512T_test_buf[j], sha512T_test_buflen[j], sha512, sha512T_t_len[i]);
  126. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha512_test_sum[k], sha512, sha512T_t_len[i] / 8);
  127. }
  128. }
  129. }
  130. #endif // SOC_SHA_SUPPORT_SHA512_T
  131. #endif // SOC_SHA_SUPPORTED
  132. TEST_GROUP_RUNNER(sha)
  133. {
  134. #if SOC_SHA_SUPPORTED
  135. #if SOC_SHA_SUPPORT_SHA1
  136. RUN_TEST_CASE(sha, test_sha1);
  137. #endif /* SOC_SHA_SUPPORT_SHA1 */
  138. #if SOC_SHA_SUPPORT_SHA224
  139. RUN_TEST_CASE(sha, test_sha224);
  140. #endif /* SOC_SHA_SUPPORT_SHA224 */
  141. #if SOC_SHA_SUPPORT_SHA256
  142. RUN_TEST_CASE(sha, test_sha256);
  143. #endif /* SOC_SHA_SUPPORT_SHA256 */
  144. #if SOC_SHA_SUPPORT_SHA384
  145. RUN_TEST_CASE(sha, test_sha384);
  146. #endif /* SOC_SHA_SUPPORT_SHA384 */
  147. #if SOC_SHA_SUPPORT_SHA512
  148. RUN_TEST_CASE(sha, test_sha512);
  149. #endif /* SOC_SHA_SUPPORT_SHA512 */
  150. #if SOC_SHA_SUPPORT_SHA512_T
  151. RUN_TEST_CASE(sha, test_sha512t);
  152. #endif // SOC_SHA_SUPPORT_SHA512_T
  153. #endif /* SOC_SHA_SUPPORTED */
  154. }