From b2f50ca22596a9faaf75d6d2861c02e92e48f853 Mon Sep 17 00:00:00 2001 From: gaoxu Date: Mon, 24 Aug 2026 17:10:04 +0800 Subject: [PATCH] fix(rng): fix NIST hanged beacuse RNG health test uncompleted --- .../test_apps/crypto/main/ecdsa/test_ecdsa.c | 9 -- components/hal/esp32s31/include/hal/rng_ll.h | 98 ++++++++++++++++++- 2 files changed, 97 insertions(+), 10 deletions(-) diff --git a/components/esp_hal_security/test_apps/crypto/main/ecdsa/test_ecdsa.c b/components/esp_hal_security/test_apps/crypto/main/ecdsa/test_ecdsa.c index b419059b9ee..62aa5c73967 100644 --- a/components/esp_hal_security/test_apps/crypto/main/ecdsa/test_ecdsa.c +++ b/components/esp_hal_security/test_apps/crypto/main/ecdsa/test_ecdsa.c @@ -464,10 +464,7 @@ TEST_GROUP_RUNNER(ecdsa) { /* SECP192R1 test cases */ RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_signature_verification) -#if !CONFIG_IDF_TARGET_ESP32S31 - // TODO: IDF-15703 re-enable TRNG-backed sign_and_verify on esp32s31 once the TRNG support update lands RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_sign_and_verify) -#endif RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_corrupt_signature) #ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE RUN_TEST_CASE(ecdsa, ecdsa_SECP192R1_det_sign_and_verify) @@ -478,10 +475,7 @@ TEST_GROUP_RUNNER(ecdsa) /* SECP256R1 test cases */ RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_signature_verification) -#if !CONFIG_IDF_TARGET_ESP32S31 - // TODO: IDF-15703 re-enable TRNG-backed sign_and_verify on esp32s31 once the TRNG support update lands RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_sign_and_verify) -#endif RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_corrupt_signature) #ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE RUN_TEST_CASE(ecdsa, ecdsa_SECP256R1_det_sign_and_verify) @@ -493,10 +487,7 @@ TEST_GROUP_RUNNER(ecdsa) /* SECP384R1 test cases */ #ifdef SOC_ECDSA_SUPPORT_CURVE_P384 RUN_TEST_CASE(ecdsa, ecdsa_SECP384R1_signature_verification) -#if !CONFIG_IDF_TARGET_ESP32S31 - // TODO: IDF-15703 re-enable TRNG-backed sign_and_verify on esp32s31 once the TRNG support update lands RUN_TEST_CASE(ecdsa, ecdsa_SECP384R1_sign_and_verify) -#endif RUN_TEST_CASE(ecdsa, ecdsa_SECP384R1_corrupt_signature) #ifdef SOC_ECDSA_SUPPORT_DETERMINISTIC_MODE RUN_TEST_CASE(ecdsa, ecdsa_SECP384R1_det_sign_and_verify) diff --git a/components/hal/esp32s31/include/hal/rng_ll.h b/components/hal/esp32s31/include/hal/rng_ll.h index 5d4513b97ac..f062856d5f9 100644 --- a/components/hal/esp32s31/include/hal/rng_ll.h +++ b/components/hal/esp32s31/include/hal/rng_ll.h @@ -69,6 +69,92 @@ static inline void rng_ll_enable_noise_crc(bool enable) LP_TRNG.conf.noise_crc_en = enable; } +/** + * @brief Select the noise source for TRNG health tests + * + * @param source One-hot noise source selection + */ +static inline void rng_ll_set_noise_source(uint32_t source) +{ + REG_SET_FIELD(TRNG_CONF_REG, TRNG_NOISE_SOURCE_SEL, source); +} + +/** + * @brief Select the sampling-enable signal for TRNG health tests + * + * @param position One-hot sampling-enable selection + */ +static inline void rng_ll_set_noise_position(uint32_t position) +{ + REG_SET_FIELD(TRNG_CONF_REG, TRNG_NOISE_POS_SEL, position); +} + +/** + * @brief Configure repetition and adaptive proportion health-test thresholds + * + * @param repetition_cutoff Repetition count test cutoff + * @param adaptive_cutoff Adaptive proportion test cutoff + */ +static inline void rng_ll_set_health_test_thresholds(uint32_t repetition_cutoff, uint32_t adaptive_cutoff) +{ + REG_SET_FIELD(TRNG_CONF_REG, TRNG_REPETITION_VALUE_C, repetition_cutoff); + REG_SET_FIELD(TRNG_CONF_REG, TRNG_ADPATIVE_VALUE_C, adaptive_cutoff); +} + +/** + * @brief Configure the number of samples processed by the startup health test + * + * @param sample_limit Number of startup test samples + */ +static inline void rng_ll_set_startup_test_limit(uint32_t sample_limit) +{ + REG_SET_FIELD(TRNG_DEBUG_CONF_REG, TRNG_STARTUP_TEST_LIMIT, sample_limit); +} + +/** + * @brief Enable or disable standard 256-bit TRNG output mode + * + * @param enable true to enable standard output mode, false otherwise + */ +static inline void rng_ll_enable_random_output_mode(bool enable) +{ + if (enable) { + REG_SET_BIT(TRNG_CONF_REG, TRNG_RANDOM_OUTPUT_MODE); + } else { + REG_CLR_BIT(TRNG_CONF_REG, TRNG_RANDOM_OUTPUT_MODE); + } +} + +/** + * @brief Enable or bypass TRNG health tests + * + * @param enable true to enable health tests, false to bypass them + */ +static inline void rng_ll_enable_health_test(bool enable) +{ + if (enable) { + REG_CLR_BIT(TRNG_DEBUG_CONF_REG, TRNG_HEALTH_TEST_BYPASS); + } else { + REG_SET_BIT(TRNG_DEBUG_CONF_REG, TRNG_HEALTH_TEST_BYPASS); + } +} + +/** + * @brief Start the TRNG startup health test + */ +static inline void rng_ll_start_startup_test(void) +{ + REG_SET_BIT(TRNG_DEBUG_CONF_REG, TRNG_STARTUP_TEST_START); +} + +/** + * @brief Stop TRNG health tests + */ +static inline void rng_ll_stop_health_test(void) +{ + REG_SET_BIT(TRNG_DEBUG_CONF_REG, TRNG_HEALTH_TEST_END); +} + /** * @brief Enable RNG module * @@ -79,8 +165,16 @@ static inline void rng_ll_enable(void) rng_ll_enable_bus_clock(true); rng_ll_enable_clock(true); rng_ll_reset(); - rng_ll_enable_sample(true); + + rng_ll_set_noise_source(BIT(4)); + rng_ll_set_noise_position(BIT(4)); + rng_ll_set_health_test_thresholds(0x1f, 0x12); + rng_ll_set_startup_test_limit(1024); + rng_ll_enable_health_test(true); + rng_ll_enable_random_output_mode(true); rng_ll_enable_noise_crc(true); + rng_ll_enable_sample(true); + rng_ll_start_startup_test(); } /** @@ -90,6 +184,8 @@ static inline void rng_ll_enable(void) */ static inline void rng_ll_disable(void) { + rng_ll_stop_health_test(); + rng_ll_enable_random_output_mode(false); rng_ll_enable_noise_crc(false); rng_ll_enable_sample(false); rng_ll_enable_clock(false);