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