mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
ci(mbedtls): added a test for the mbedtls_internal_shaX_process API
This commit is contained in:
parent
edef8d5fae
commit
9e61344890
@ -141,4 +141,92 @@ TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
|
||||
#if SOC_SHA_SUPPORT_SHA512
|
||||
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_espsha, sha512_mbedtls, sizeof(sha512_espsha), "SHA512 results should match");
|
||||
#endif
|
||||
|
||||
#if CONFIG_MBEDTLS_HARDWARE_SHA
|
||||
|
||||
TEST_CASE("Test mbedtls_internal_sha_process()", "[hw_crypto]")
|
||||
{
|
||||
const size_t BUFFER_SZ = 128;
|
||||
int ret;
|
||||
unsigned char output[64] = { 0 };
|
||||
void *buffer = heap_caps_malloc(BUFFER_SZ, MALLOC_CAP_DMA | MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
|
||||
TEST_ASSERT_NOT_NULL(buffer);
|
||||
memset(buffer, 0xEE, BUFFER_SZ);
|
||||
|
||||
mbedtls_sha1_context sha1_ctx;
|
||||
|
||||
const uint8_t sha1_expected[20] = { 0x41, 0x63, 0x12, 0x5b, 0x9c, 0x68, 0x85, 0xc8,
|
||||
0x01, 0x40, 0xf4, 0x03, 0x5d, 0x0d, 0x84, 0x0e,
|
||||
0xa4, 0xae, 0x4d, 0xe9 };
|
||||
|
||||
mbedtls_sha1_init(&sha1_ctx);
|
||||
mbedtls_sha1_starts(&sha1_ctx);
|
||||
|
||||
ret = mbedtls_internal_sha1_process(&sha1_ctx, buffer);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
|
||||
ret = mbedtls_internal_sha1_process(&sha1_ctx, buffer);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
|
||||
#if SOC_SHA_ENDIANNESS_BE
|
||||
for (int i = 0; i < sizeof(sha1_ctx.state)/sizeof(sha1_ctx.state[0]); i++)
|
||||
{
|
||||
*(uint32_t *)(output + i*4) = __builtin_bswap32(sha1_ctx.state[i]);
|
||||
}
|
||||
#else
|
||||
memcpy(output, sha1_ctx.state, 20);
|
||||
#endif
|
||||
|
||||
// Check if the intermediate states are correct
|
||||
TEST_ASSERT_EQUAL_HEX8_ARRAY(sha1_expected, output, sizeof(sha1_expected));
|
||||
|
||||
ret = mbedtls_sha1_finish(&sha1_ctx, output);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
|
||||
mbedtls_sha1_free(&sha1_ctx);
|
||||
|
||||
#if SOC_SHA_SUPPORT_SHA512
|
||||
mbedtls_sha512_context sha512_ctx;
|
||||
|
||||
const uint8_t sha512_expected[64] = { 0x3c, 0x77, 0x5f, 0xb0, 0x3b, 0x25, 0x8d, 0x3b,
|
||||
0xa9, 0x28, 0xa2, 0x29, 0xf2, 0x14, 0x7d, 0xb3,
|
||||
0x64, 0x1e, 0x76, 0xd5, 0x0b, 0xbc, 0xdf, 0xb4,
|
||||
0x75, 0x1d, 0xe7, 0x7f, 0x62, 0x83, 0xdd, 0x78,
|
||||
0x6b, 0x0e, 0xa4, 0xd2, 0xbe, 0x51, 0x56, 0xd4,
|
||||
0xfe, 0x3b, 0xa3, 0x3a, 0xd7, 0xf6, 0xd3, 0xb3,
|
||||
0xe7, 0x9d, 0xb5, 0xe6, 0x76, 0x35, 0x2a, 0xae,
|
||||
0x07, 0x0a, 0x3a, 0x03, 0x44, 0xf0, 0xb8, 0xfe };
|
||||
|
||||
mbedtls_sha512_init(&sha512_ctx);
|
||||
mbedtls_sha512_starts(&sha512_ctx, 0);
|
||||
|
||||
ret = mbedtls_internal_sha512_process(&sha512_ctx, buffer);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
|
||||
ret = mbedtls_internal_sha512_process(&sha512_ctx, buffer);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
|
||||
#if SOC_SHA_ENDIANNESS_BE
|
||||
for (int i = 0; i < sizeof(sha512_ctx.state)/sizeof(sha512_ctx.state[0]); i++)
|
||||
{
|
||||
*(uint64_t *)(output + i*8) = __builtin_bswap64(sha512_ctx.state[i]);
|
||||
}
|
||||
#else
|
||||
memcpy(output, sha512_ctx.state, 64);
|
||||
#endif
|
||||
|
||||
// Check if the intermediate states are correct
|
||||
TEST_ASSERT_EQUAL_HEX8_ARRAY(sha512_expected, output, sizeof(sha512_expected));
|
||||
|
||||
ret = mbedtls_sha512_finish(&sha512_ctx, output);
|
||||
TEST_ASSERT_EQUAL(0, ret);
|
||||
|
||||
mbedtls_sha512_free(&sha512_ctx);
|
||||
|
||||
#endif
|
||||
free(buffer);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
@ -276,6 +276,9 @@
|
||||
/* ESP32 style SHA engine, where multiple states can be stored in parallel */
|
||||
#define SOC_SHA_SUPPORT_PARALLEL_ENG (1)
|
||||
|
||||
/* ESP32's SHA peripheral processes and stores data in big-endian format */
|
||||
#define SOC_SHA_ENDIANNESS_BE (1)
|
||||
|
||||
/* Supported HW algorithms */
|
||||
#define SOC_SHA_SUPPORT_SHA1 (1)
|
||||
#define SOC_SHA_SUPPORT_SHA256 (1)
|
||||
|
Loading…
Reference in New Issue
Block a user