From 589a1f216fa2a74a31fc974a33118dcd59cafba0 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 11 Oct 2019 07:57:26 +0200 Subject: [PATCH 1/2] mbedtls: add SHA performance test Results with this revision: SHA256 rate 2.599MB/sec Debug 240MHz SW SHA256 rate 1.147MB/sec Release 80MHz SW SHA256 rate 3.469MB/sec Release 240MHz SW SHA256 rate 2.687MB/sec Release 240MHz SW + PSRAM workaround SHA256 rate 9.433MB/sec Debug 240MHz HW rev1 SHA256 rate 3.727MB/sec Release 80MHz HW rev1 SHA256 rate 10.961MB/sec Release 240MHz HW rev1 SHA256 rate 9.966MB/sec Release 240MHz HW rev1 + PRAM workaround SHA256 rate 10.974MB/sec Debug 240MHz HW rev3 SHA256 rate 4.362MB/sec Release 80MHz HW rev3 SHA256 rate 13.207MB/sec Release 240MHz HW rev3 Debug = Og, assertions enabled Release = O2, assertions disabled --- components/idf_test/include/idf_performance.h | 2 + components/mbedtls/test/CMakeLists.txt | 2 +- components/mbedtls/test/test_sha_perf.c | 58 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 components/mbedtls/test/test_sha_perf.c diff --git a/components/idf_test/include/idf_performance.h b/components/idf_test/include/idf_performance.h index 8a952c406b..4049f76675 100644 --- a/components/idf_test/include/idf_performance.h +++ b/components/idf_test/include/idf_performance.h @@ -30,4 +30,6 @@ // floating point instructions per divide and per sqrt (configured for worst-case with PSRAM workaround) #define IDF_PERFORMANCE_MAX_ESP32_CYCLES_PER_DIV 70 #define IDF_PERFORMANCE_MAX_ESP32_CYCLES_PER_SQRT 140 +// SHA256 hardware throughput at 240MHz, threshold set lower than worst case +#define IDF_PERFORMANCE_MIN_SHA256_THROUGHPUT_MBSEC 9.0 diff --git a/components/mbedtls/test/CMakeLists.txt b/components/mbedtls/test/CMakeLists.txt index ea3ab04172..e9efe9b510 100644 --- a/components/mbedtls/test/CMakeLists.txt +++ b/components/mbedtls/test/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register(SRC_DIRS "." INCLUDE_DIRS "." - REQUIRES unity test_utils mbedtls) + REQUIRES unity test_utils mbedtls libsodium) idf_component_get_property(mbedtls mbedtls COMPONENT_LIB) target_compile_definitions(${mbedtls} INTERFACE "-DMBEDTLS_DEPRECATED_WARNING") diff --git a/components/mbedtls/test/test_sha_perf.c b/components/mbedtls/test/test_sha_perf.c new file mode 100644 index 0000000000..9bd609239c --- /dev/null +++ b/components/mbedtls/test/test_sha_perf.c @@ -0,0 +1,58 @@ +/* mbedTLS SHA performance test +*/ +#include +#include +#include +#include "mbedtls/sha256.h" +#include "unity.h" +#include "sdkconfig.h" +#include "esp_timer.h" +#include "esp_heap_caps.h" +#include "test_utils.h" +#include "sodium/utils.h" + +TEST_CASE_ESP32("mbedtls SHA performance", "[aes]") +{ + const unsigned CALLS = 256; + const unsigned CALL_SZ = 16*1024; + mbedtls_sha256_context sha256_ctx; + int64_t start, end; + unsigned char sha256[32]; + + // allocate internal memory + uint8_t *buf = heap_caps_malloc(CALL_SZ, MALLOC_CAP_8BIT|MALLOC_CAP_INTERNAL); + TEST_ASSERT_NOT_NULL(buf); + memset(buf, 0x55, CALL_SZ); + + mbedtls_sha256_init(&sha256_ctx); + start = esp_timer_get_time(); + TEST_ASSERT_EQUAL(0, mbedtls_sha256_starts_ret(&sha256_ctx, false)); + for (int c = 0; c < CALLS; c++) { + TEST_ASSERT_EQUAL(0, mbedtls_sha256_update_ret(&sha256_ctx, buf, CALL_SZ)); + } + TEST_ASSERT_EQUAL(0, mbedtls_sha256_finish_ret(&sha256_ctx, sha256)); + end = esp_timer_get_time(); + + free(buf); + mbedtls_sha256_free(&sha256_ctx); + + /* Check the result. Reference value can be calculated using: + * dd if=/dev/zero bs=$((16*1024)) count=256 | tr '\000' '\125' | sha256sum + */ + const char* expected_hash = "c88df2638fb9699abaad05780fa5e0fdb6058f477069040eac8bed3231286275"; + char hash_str[sizeof(sha256) * 2 + 1]; + sodium_bin2hex(hash_str, sizeof(hash_str), sha256, sizeof(sha256)); + + TEST_ASSERT_EQUAL_STRING(expected_hash, hash_str); + + float usecs = end - start; + // bytes/usec = MB/sec + float mb_sec = (CALL_SZ * CALLS) / usecs; + printf("SHA256 rate %.3fMB/sec\n", mb_sec); +#ifdef CONFIG_MBEDTLS_HARDWARE + // Don't put a hard limit on software SHA performance + TEST_PERFORMANCE_GREATER_THAN(SHA256_THROUGHPUT_MBSEC, "%.3fMB/sec", mb_sec); +#endif +} + + From 71b73e61e1cfe951398d86271078d0d8f85bde28 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Thu, 17 Oct 2019 10:30:46 +0200 Subject: [PATCH 2/2] mbedtls: Enable SHA hardware acceleration by default --- components/mbedtls/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/mbedtls/Kconfig b/components/mbedtls/Kconfig index bcc0b9c450..f6db56b6d8 100644 --- a/components/mbedtls/Kconfig +++ b/components/mbedtls/Kconfig @@ -163,7 +163,7 @@ menu "mbedTLS" config MBEDTLS_HARDWARE_SHA bool "Enable hardware SHA acceleration" - default n + default y help Enable hardware accelerated SHA1, SHA256, SHA384 & SHA512 in mbedTLS.