test_sha.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "esp_types.h"
  10. #include "esp_log.h"
  11. #include "ccomp_timer.h"
  12. #include "esp_heap_caps.h"
  13. #include "idf_performance.h"
  14. #if CONFIG_IDF_TARGET_ESP32
  15. #include "esp32/clk.h"
  16. #elif CONFIG_IDF_TARGET_ESP32S2
  17. #include "esp32s2/clk.h"
  18. #elif CONFIG_IDF_TARGET_ESP32C3
  19. #include "esp32c3/clk.h"
  20. #endif
  21. #include "soc/soc_caps.h"
  22. #include "unity.h"
  23. #include "test_utils.h"
  24. #include "mbedtls/sha1.h"
  25. #include "mbedtls/sha256.h"
  26. #if SOC_SHA_SUPPORT_SHA512
  27. #include "mbedtls/sha512.h"
  28. #endif
  29. #include "sha/sha_parallel_engine.h"
  30. /* Note: Most of the SHA functions are called as part of mbedTLS, so
  31. are tested as part of mbedTLS tests. Only esp_sha() is different.
  32. */
  33. #define TAG "sha_test"
  34. TEST_CASE("Test esp_sha()", "[hw_crypto]")
  35. {
  36. const size_t BUFFER_SZ = 32 * 1024 + 6; // NB: not an exact multiple of SHA block size
  37. int64_t elapsed;
  38. uint32_t us_sha1;
  39. uint8_t sha1_result[20] = { 0 };
  40. #if SOC_SHA_SUPPORT_SHA512
  41. uint32_t us_sha512;
  42. uint8_t sha512_result[64] = { 0 };
  43. #endif
  44. void *buffer = heap_caps_malloc(BUFFER_SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL);
  45. TEST_ASSERT_NOT_NULL(buffer);
  46. memset(buffer, 0xEE, BUFFER_SZ);
  47. const uint8_t sha1_expected[20] = { 0xc7, 0xbb, 0xd3, 0x74, 0xf2, 0xf6, 0x20, 0x86,
  48. 0x61, 0xf4, 0x50, 0xd5, 0xf5, 0x18, 0x44, 0xcc,
  49. 0x7a, 0xb7, 0xa5, 0x4a };
  50. #if SOC_SHA_SUPPORT_SHA512
  51. const uint8_t sha512_expected[64] = { 0xc7, 0x7f, 0xda, 0x8c, 0xb3, 0x58, 0x14, 0x8a,
  52. 0x52, 0x3b, 0x46, 0x04, 0xc0, 0x85, 0xc5, 0xf0,
  53. 0x46, 0x64, 0x14, 0xd5, 0x96, 0x7a, 0xa2, 0x80,
  54. 0x20, 0x9c, 0x04, 0x27, 0x7d, 0x3b, 0xf9, 0x1f,
  55. 0xb2, 0xa3, 0x45, 0x3c, 0xa1, 0x6a, 0x8d, 0xdd,
  56. 0x35, 0x5e, 0x35, 0x57, 0x76, 0x22, 0x74, 0xd8,
  57. 0x1e, 0x07, 0xc6, 0xa2, 0x9e, 0x3b, 0x65, 0x75,
  58. 0x80, 0x7d, 0xe6, 0x6e, 0x47, 0x61, 0x2c, 0x94 };
  59. #endif
  60. ccomp_timer_start();
  61. esp_sha(SHA1, buffer, BUFFER_SZ, sha1_result);
  62. elapsed = ccomp_timer_stop();
  63. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha1_expected, sha1_result, sizeof(sha1_expected));
  64. us_sha1 = elapsed;
  65. ESP_LOGI(TAG, "esp_sha() 32KB SHA1 in %u us", us_sha1);
  66. #if SOC_SHA_SUPPORT_SHA512
  67. ccomp_timer_start();
  68. esp_sha(SHA2_512, buffer, BUFFER_SZ, sha512_result);
  69. elapsed = ccomp_timer_stop();
  70. TEST_ASSERT_EQUAL_HEX8_ARRAY(sha512_expected, sha512_result, sizeof(sha512_expected));
  71. us_sha512 = elapsed;
  72. ESP_LOGI(TAG, "esp_sha() 32KB SHA512 in %u us", us_sha512);
  73. #endif
  74. free(buffer);
  75. TEST_PERFORMANCE_CCOMP_LESS_THAN(TIME_SHA1_32KB, "%dus", us_sha1);
  76. #if SOC_SHA_SUPPORT_SHA512
  77. TEST_PERFORMANCE_CCOMP_LESS_THAN(TIME_SHA512_32KB, "%dus", us_sha512);
  78. #endif
  79. }
  80. TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
  81. {
  82. const void* ptr;
  83. spi_flash_mmap_handle_t handle;
  84. uint8_t sha1_espsha[20] = { 0 };
  85. uint8_t sha1_mbedtls[20] = { 0 };
  86. uint8_t sha256_espsha[32] = { 0 };
  87. uint8_t sha256_mbedtls[32] = { 0 };
  88. #if SOC_SHA_SUPPORT_SHA512
  89. uint8_t sha512_espsha[64] = { 0 };
  90. uint8_t sha512_mbedtls[64] = { 0 };
  91. #endif
  92. const size_t LEN = 1024 * 1024;
  93. /* mmap() 1MB of flash, we don't care what it is really */
  94. esp_err_t err = spi_flash_mmap(0x0, LEN, SPI_FLASH_MMAP_DATA, &ptr, &handle);
  95. TEST_ASSERT_EQUAL_HEX32(ESP_OK, err);
  96. TEST_ASSERT_NOT_NULL(ptr);
  97. /* Compare esp_sha() result to the mbedTLS result, should always be the same */
  98. esp_sha(SHA1, ptr, LEN, sha1_espsha);
  99. int r = mbedtls_sha1_ret(ptr, LEN, sha1_mbedtls);
  100. TEST_ASSERT_EQUAL(0, r);
  101. esp_sha(SHA2_256, ptr, LEN, sha256_espsha);
  102. r = mbedtls_sha256_ret(ptr, LEN, sha256_mbedtls, 0);
  103. TEST_ASSERT_EQUAL(0, r);
  104. #if SOC_SHA_SUPPORT_SHA512
  105. esp_sha(SHA2_512, ptr, LEN, sha512_espsha);
  106. r = mbedtls_sha512_ret(ptr, LEN, sha512_mbedtls, 0);
  107. TEST_ASSERT_EQUAL(0, r);
  108. #endif
  109. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha1_espsha, sha1_mbedtls, sizeof(sha1_espsha), "SHA1 results should match");
  110. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_espsha, sha256_mbedtls, sizeof(sha256_espsha), "SHA256 results should match");
  111. #if SOC_SHA_SUPPORT_SHA512
  112. TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_espsha, sha512_mbedtls, sizeof(sha512_espsha), "SHA512 results should match");
  113. #endif
  114. }