diff --git a/components/protocomm/src/crypto/srp6a/esp_srp.c b/components/protocomm/src/crypto/srp6a/esp_srp.c index bf3dd25cc5b..9109cbe6c0e 100644 --- a/components/protocomm/src/crypto/srp6a/esp_srp.c +++ b/components/protocomm/src/crypto/srp6a/esp_srp.c @@ -461,6 +461,10 @@ cleanup: esp_err_t esp_srp_set_salt_verifier(esp_srp_handle_t *hd, const char *salt, int salt_len, const char *verifier, int verifier_len) { + if (!hd || !salt || salt_len <= 0 || !verifier || verifier_len <= 0) { + return ESP_ERR_INVALID_ARG; + } + hd->bytes_s = malloc(salt_len); if (!hd->bytes_s) { goto error; @@ -520,6 +524,27 @@ esp_err_t esp_srp_get_session_key(esp_srp_handle_t *hd, char *bytes_A, int len_A if (! hd->A) { goto error; } + + /* RFC 5054 Section 2.5.4: abort if A % N == 0 + * This prevents an attacker from sending A=0 (or A=k*N) which would + * force the shared secret S to zero, yielding a deterministic session + * key regardless of the password. */ + esp_mpi_t *a_mod_n = esp_mpi_new(); + if (!a_mod_n) { + goto error; + } + if (esp_mpi_a_mod_b(a_mod_n, hd->A, hd->n) != 0) { + esp_mpi_free(a_mod_n); + ESP_LOGE(TAG, "Failed to compute A mod N"); + goto error; + } + int a_mod_n_is_zero = (esp_mpi_cmp_int(a_mod_n, 0) == 0); + esp_mpi_free(a_mod_n); + if (a_mod_n_is_zero) { + ESP_LOGE(TAG, "Rejected client public key: A mod N == 0 (RFC 5054 Section 2.5.4)"); + goto error; + } + u = calculate_u(hd, bytes_A, len_A); if (! u) { goto error; diff --git a/components/protocomm/src/crypto/srp6a/esp_srp_mpi.c b/components/protocomm/src/crypto/srp6a/esp_srp_mpi.c index 2cb60e4ee97..a6a1dd5d38c 100644 --- a/components/protocomm/src/crypto/srp6a/esp_srp_mpi.c +++ b/components/protocomm/src/crypto/srp6a/esp_srp_mpi.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -128,6 +128,16 @@ int esp_mpi_a_mul_b_mod_c(esp_mpi_t *result, esp_mpi_t *a, esp_mpi_t *b, esp_mpi return res; } +int esp_mpi_a_mod_b(esp_mpi_t *result, const esp_mpi_t *a, const esp_mpi_t *b) +{ + return mbedtls_mpi_mod_mpi(result, a, b); +} + +int esp_mpi_cmp_int(const esp_mpi_t *a, int value) +{ + return mbedtls_mpi_cmp_int(a, value); +} + int esp_mpi_a_add_b_mod_c(esp_mpi_t *result, esp_mpi_t *a, esp_mpi_t *b, esp_mpi_t *c, esp_mpi_ctx_t *ctx) { (void) ctx; diff --git a/components/protocomm/src/crypto/srp6a/esp_srp_mpi.h b/components/protocomm/src/crypto/srp6a/esp_srp_mpi.h index 46f07bc9ee0..fcc26acf1ee 100644 --- a/components/protocomm/src/crypto/srp6a/esp_srp_mpi.h +++ b/components/protocomm/src/crypto/srp6a/esp_srp_mpi.h @@ -47,6 +47,10 @@ int esp_mpi_a_mul_b_mod_c(esp_mpi_t *result, esp_mpi_t *a, esp_mpi_t *b, esp_mpi int esp_mpi_a_add_b_mod_c(esp_mpi_t *result, esp_mpi_t *a, esp_mpi_t *b, esp_mpi_t *c, esp_mpi_ctx_t *ctx); +int esp_mpi_a_mod_b(esp_mpi_t *result, const esp_mpi_t *a, const esp_mpi_t *b); + +int esp_mpi_cmp_int(const esp_mpi_t *a, int value); + #ifdef __cplusplus } #endif diff --git a/components/protocomm/test_apps/main/CMakeLists.txt b/components/protocomm/test_apps/main/CMakeLists.txt index 74dc603fa6a..f5f0e079ae1 100644 --- a/components/protocomm/test_apps/main/CMakeLists.txt +++ b/components/protocomm/test_apps/main/CMakeLists.txt @@ -1,3 +1,4 @@ idf_component_register(SRC_DIRS "." PRIV_INCLUDE_DIRS "." - PRIV_REQUIRES cmock mbedtls protocomm protobuf-c test_utils unity) + PRIV_REQUIRES cmock mbedtls nvs_flash protocomm protobuf-c test_utils unity + WHOLE_ARCHIVE) diff --git a/components/protocomm/test_apps/main/app_main.c b/components/protocomm/test_apps/main/app_main.c new file mode 100644 index 00000000000..a0f77aa4817 --- /dev/null +++ b/components/protocomm/test_apps/main/app_main.c @@ -0,0 +1,108 @@ +/* + * SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "unity.h" +#include "test_utils.h" +#include "memory_checks.h" +#include "esp_newlib.h" +#include "nvs_flash.h" +#include "mbedtls/aes.h" +#if SOC_SHA_SUPPORT_PARALLEL_ENG +#include "sha/sha_parallel_engine.h" +#else +#include "sha/sha_block.h" +#endif +#include "bignum_impl.h" + +/* setUp runs before every test */ +void setUp(void) +{ +#if CONFIG_MBEDTLS_HARDWARE_SHA + // Execute esp_sha operation to allocate internal SHA semaphore (in case of ESP32) + // and initial DMA setup memory which is considered as leaked otherwise + const uint8_t input_buffer[64] = {0}; + uint8_t output_buffer[64]; +#if SOC_SHA_SUPPORT_SHA1 + esp_sha(SHA1, input_buffer, sizeof(input_buffer), output_buffer); +#endif // SOC_SHA_SUPPORT_SHA1 +#if SOC_SHA_SUPPORT_SHA256 + esp_sha(SHA2_256, input_buffer, sizeof(input_buffer), output_buffer); +#endif // SOC_SHA_SUPPORT_SHA256 +#if SOC_SHA_SUPPORT_SHA512 + esp_sha(SHA2_512, input_buffer, sizeof(input_buffer), output_buffer); +#endif // SOC_SHA_SUPPORT_SHA512 +#endif // CONFIG_MBEDTLS_HARDWARE_SHA + +#if defined(CONFIG_MBEDTLS_HARDWARE_MPI) + esp_mpi_enable_hardware_hw_op(); + esp_mpi_disable_hardware_hw_op(); +#endif // CONFIG_MBEDTLS_HARDWARE_MPI + +#if SOC_AES_SUPPORTED + // Execute mbedtls_aes_crypt_cbc operation to allocate AES interrupt + // allocation memory which is considered as leak otherwise + { + mbedtls_aes_context aes_ctx; + const uint8_t aes_key[16] = {0}; + uint8_t iv[16] = {0}; + const uint8_t plaintext[16] = {0}; + uint8_t ciphertext[16]; + + mbedtls_aes_init(&aes_ctx); + TEST_ASSERT_EQUAL(0, mbedtls_aes_setkey_enc(&aes_ctx, aes_key, 128)); + TEST_ASSERT_EQUAL(0, mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, + sizeof(plaintext), iv, plaintext, ciphertext)); + + const uint8_t plaintext_long[256] = {0}; + uint8_t ciphertext_long[256]; + memset(iv, 0, sizeof(iv)); + TEST_ASSERT_EQUAL(0, mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, + sizeof(plaintext_long), iv, plaintext_long, ciphertext_long)); + mbedtls_aes_free(&aes_ctx); + } +#endif // SOC_AES_SUPPORTED + + test_utils_record_free_mem(); + TEST_ESP_OK(test_utils_set_leak_level(50, ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_GENERAL)); + TEST_ESP_OK(test_utils_set_leak_level(50, ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_GENERAL)); +} + +/* tearDown runs after every test */ +void tearDown(void) +{ + /* some FreeRTOS stuff is cleaned up by idle task */ + vTaskDelay(5); + + /* clean up some of the newlib's lazy allocations */ + esp_reent_cleanup(); + + /* check if unit test has caused heap corruption in any heap */ + TEST_ASSERT_MESSAGE( heap_caps_check_integrity(MALLOC_CAP_INVALID, true), "The test has corrupted the heap"); + + test_utils_finish_and_evaluate_leaks(test_utils_get_leak_level(ESP_LEAK_TYPE_WARNING, ESP_COMP_LEAK_ALL), + test_utils_get_leak_level(ESP_LEAK_TYPE_CRITICAL, ESP_COMP_LEAK_ALL)); +} + +static void test_task(void *pvParameters) +{ + vTaskDelay(2); /* Delay a bit to let the main task be deleted */ + unity_run_menu(); +} + +void app_main(void) +{ + /* Initialize NVS */ + esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + ESP_ERROR_CHECK(ret); + + xTaskCreatePinnedToCore(test_task, "testTask", CONFIG_UNITY_FREERTOS_STACK_SIZE, NULL, CONFIG_UNITY_FREERTOS_PRIORITY, NULL, CONFIG_UNITY_FREERTOS_CPU); +} diff --git a/components/protocomm/test_apps/main/test_protocomm.c b/components/protocomm/test_apps/main/test_protocomm.c index 5ec35485841..8d24d1e874e 100644 --- a/components/protocomm/test_apps/main/test_protocomm.c +++ b/components/protocomm/test_apps/main/test_protocomm.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -1190,8 +1190,3 @@ TEST_CASE("security 1 weak session test", "[PROTOCOMM]") { TEST_ASSERT(test_security1_weak_session() == ESP_OK); } - -void app_main(void) -{ - unity_run_menu(); -} diff --git a/components/protocomm/test_apps/main/test_srp.c b/components/protocomm/test_apps/main/test_srp.c new file mode 100644 index 00000000000..200466106dc --- /dev/null +++ b/components/protocomm/test_apps/main/test_srp.c @@ -0,0 +1,420 @@ +/* + * SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include +#include +#include +#include +#include "esp_srp.h" +#include "esp_log.h" +#include "test_utils.h" +#include "esp_rom_crc.h" + +static const char *TAG = "srp_test"; + +// Example username and password +static const char *username = "wifiprov"; +static const char *password = "abcd1234"; + +static const char sec2_salt[] = { + 0x03, 0x6e, 0xe0, 0xc7, 0xbc, 0xb9, 0xed, 0xa8, 0x4c, 0x9e, 0xac, 0x97, 0xd9, 0x3d, 0xec, 0xf4 +}; + +static const char sec2_verifier[] = { + 0x7c, 0x7c, 0x85, 0x47, 0x65, 0x08, 0x94, 0x6d, 0xd6, 0x36, 0xaf, 0x37, 0xd7, 0xe8, 0x91, 0x43, + 0x78, 0xcf, 0xfd, 0x61, 0x6c, 0x59, 0xd2, 0xf8, 0x39, 0x08, 0x12, 0x72, 0x38, 0xde, 0x9e, 0x24, + 0xa4, 0x70, 0x26, 0x1c, 0xdf, 0xa9, 0x03, 0xc2, 0xb2, 0x70, 0xe7, 0xb1, 0x32, 0x24, 0xda, 0x11, + 0x1d, 0x97, 0x18, 0xdc, 0x60, 0x72, 0x08, 0xcc, 0x9a, 0xc9, 0x0c, 0x48, 0x27, 0xe2, 0xae, 0x89, + 0xaa, 0x16, 0x25, 0xb8, 0x04, 0xd2, 0x1a, 0x9b, 0x3a, 0x8f, 0x37, 0xf6, 0xe4, 0x3a, 0x71, 0x2e, + 0xe1, 0x27, 0x86, 0x6e, 0xad, 0xce, 0x28, 0xff, 0x54, 0x46, 0x60, 0x1f, 0xb9, 0x96, 0x87, 0xdc, + 0x57, 0x40, 0xa7, 0xd4, 0x6c, 0xc9, 0x77, 0x54, 0xdc, 0x16, 0x82, 0xf0, 0xed, 0x35, 0x6a, 0xc4, + 0x70, 0xad, 0x3d, 0x90, 0xb5, 0x81, 0x94, 0x70, 0xd7, 0xbc, 0x65, 0xb2, 0xd5, 0x18, 0xe0, 0x2e, + 0xc3, 0xa5, 0xf9, 0x68, 0xdd, 0x64, 0x7b, 0xb8, 0xb7, 0x3c, 0x9c, 0xfc, 0x00, 0xd8, 0x71, 0x7e, + 0xb7, 0x9a, 0x7c, 0xb1, 0xb7, 0xc2, 0xc3, 0x18, 0x34, 0x29, 0x32, 0x43, 0x3e, 0x00, 0x99, 0xe9, + 0x82, 0x94, 0xe3, 0xd8, 0x2a, 0xb0, 0x96, 0x29, 0xb7, 0xdf, 0x0e, 0x5f, 0x08, 0x33, 0x40, 0x76, + 0x52, 0x91, 0x32, 0x00, 0x9f, 0x97, 0x2c, 0x89, 0x6c, 0x39, 0x1e, 0xc8, 0x28, 0x05, 0x44, 0x17, + 0x3f, 0x68, 0x02, 0x8a, 0x9f, 0x44, 0x61, 0xd1, 0xf5, 0xa1, 0x7e, 0x5a, 0x70, 0xd2, 0xc7, 0x23, + 0x81, 0xcb, 0x38, 0x68, 0xe4, 0x2c, 0x20, 0xbc, 0x40, 0x57, 0x76, 0x17, 0xbd, 0x08, 0xb8, 0x96, + 0xbc, 0x26, 0xeb, 0x32, 0x46, 0x69, 0x35, 0x05, 0x8c, 0x15, 0x70, 0xd9, 0x1b, 0xe9, 0xbe, 0xcc, + 0xa9, 0x38, 0xa6, 0x67, 0xf0, 0xad, 0x50, 0x13, 0x19, 0x72, 0x64, 0xbf, 0x52, 0xc2, 0x34, 0xe2, + 0x1b, 0x11, 0x79, 0x74, 0x72, 0xbd, 0x34, 0x5b, 0xb1, 0xe2, 0xfd, 0x66, 0x73, 0xfe, 0x71, 0x64, + 0x74, 0xd0, 0x4e, 0xbc, 0x51, 0x24, 0x19, 0x40, 0x87, 0x0e, 0x92, 0x40, 0xe6, 0x21, 0xe7, 0x2d, + 0x4e, 0x37, 0x76, 0x2f, 0x2e, 0xe2, 0x68, 0xc7, 0x89, 0xe8, 0x32, 0x13, 0x42, 0x06, 0x84, 0x84, + 0x53, 0x4a, 0xb3, 0x0c, 0x1b, 0x4c, 0x8d, 0x1c, 0x51, 0x97, 0x19, 0xab, 0xae, 0x77, 0xff, 0xdb, + 0xec, 0xf0, 0x10, 0x95, 0x34, 0x33, 0x6b, 0xcb, 0x3e, 0x84, 0x0f, 0xb9, 0xd8, 0x5f, 0xb8, 0xa0, + 0xb8, 0x55, 0x53, 0x3e, 0x70, 0xf7, 0x18, 0xf5, 0xce, 0x7b, 0x4e, 0xbf, 0x27, 0xce, 0xce, 0xa8, + 0xb3, 0xbe, 0x40, 0xc5, 0xc5, 0x32, 0x29, 0x3e, 0x71, 0x64, 0x9e, 0xde, 0x8c, 0xf6, 0x75, 0xa1, + 0xe6, 0xf6, 0x53, 0xc8, 0x31, 0xa8, 0x78, 0xde, 0x50, 0x40, 0xf7, 0x62, 0xde, 0x36, 0xb2, 0xba +}; + +static void test_srp_init_and_free(void) { + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + esp_srp_free(handle); +} + +static void test_srp_gen_salt_verifier(void) { + char *bytes_salt = NULL; + char *verifier = NULL; + int verifier_len = 0; + esp_err_t err = esp_srp_gen_salt_verifier(username, strlen(username), + password, strlen(password), + &bytes_salt, 16, &verifier, &verifier_len); + TEST_ASSERT_EQUAL(ESP_OK, err); + TEST_ASSERT_NOT_NULL(bytes_salt); + TEST_ASSERT_NOT_NULL(verifier); + + // Verify salt length is as requested + TEST_ASSERT_EQUAL(16, 16); + + // Verify verifier length is correct for 3072-bit SRP + TEST_ASSERT_GREATER_THAN(0, verifier_len); + + // Log the generated salt and verifier for debugging + ESP_LOG_BUFFER_HEXDUMP("Generated Salt", bytes_salt, 16, ESP_LOG_INFO); + ESP_LOG_BUFFER_HEXDUMP("Generated Verifier", verifier, verifier_len, ESP_LOG_INFO); + + free(bytes_salt); + free(verifier); +} + +static void test_srp_set_salt_verifier(void) { + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + + esp_err_t err = esp_srp_set_salt_verifier(handle, sec2_salt, sizeof(sec2_salt), + sec2_verifier, sizeof(sec2_verifier)); + TEST_ASSERT_EQUAL(ESP_OK, err); + + char *bytes_B = NULL; + int len_B = 0; + err = esp_srp_srv_pubkey_from_salt_verifier(handle, &bytes_B, &len_B); + TEST_ASSERT_EQUAL(ESP_OK, err); + // Verify B length is correct for 3072-bit SRP (384 bytes) + TEST_ASSERT_EQUAL(384, len_B); + + esp_srp_free(handle); +} + +static void test_srp_srv_pubkey(void) { + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + + char *bytes_B = NULL; + int len_B = 0; + char *bytes_salt = NULL; + esp_err_t err = esp_srp_srv_pubkey(handle, username, strlen(username), + password, strlen(password), 16, + &bytes_B, &len_B, &bytes_salt); + TEST_ASSERT_EQUAL(ESP_OK, err); + TEST_ASSERT_NOT_NULL(bytes_B); + TEST_ASSERT_NOT_NULL(bytes_salt); + + // Verify salt and B length + TEST_ASSERT_EQUAL(16, 16); + TEST_ASSERT_EQUAL(384, len_B); + + // Log for debugging + ESP_LOG_BUFFER_HEXDUMP("Generated Salt", bytes_salt, 16, ESP_LOG_INFO); + ESP_LOG_BUFFER_HEXDUMP("Generated Server Public Key B", bytes_B, len_B, ESP_LOG_INFO); + + esp_srp_free(handle); +} + +static void test_srp_get_session_key(void) { + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + + char *bytes_B = NULL; + int len_B = 0; + char *bytes_salt = NULL; + esp_err_t err = esp_srp_srv_pubkey(handle, username, strlen(username), + password, strlen(password), 16, + &bytes_B, &len_B, &bytes_salt); + TEST_ASSERT_EQUAL(ESP_OK, err); + + // In a real scenario, bytes_A would be from client + // For testing purposes, we use bytes_B as a convenient value (server talks to itself) + char *bytes_key = NULL; + uint16_t len_key = 0; + err = esp_srp_get_session_key(handle, bytes_B, len_B, &bytes_key, &len_key); + TEST_ASSERT_EQUAL(ESP_OK, err); + TEST_ASSERT_NOT_NULL(bytes_key); + + // Verify session key length (SHA512 hash) + TEST_ASSERT_EQUAL(64, len_key); + + // Log session key for debugging + ESP_LOG_BUFFER_HEXDUMP("Session Key", bytes_key, len_key, ESP_LOG_INFO); + + esp_srp_free(handle); +} + +static void test_srp_exchange_proofs(void) { + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + + char *bytes_B = NULL; + int len_B = 0; + char *bytes_salt = NULL; + esp_err_t err = esp_srp_srv_pubkey(handle, username, strlen(username), + password, strlen(password), 16, + &bytes_B, &len_B, &bytes_salt); + TEST_ASSERT_EQUAL(ESP_OK, err); + + char *bytes_key = NULL; + uint16_t len_key = 0; + err = esp_srp_get_session_key(handle, bytes_B, len_B, &bytes_key, &len_key); + TEST_ASSERT_EQUAL(ESP_OK, err); + + // In a real environment, bytes_user_proof would be calculated by the client + // For our test, we'll generate zeros - this simulates an authentication failure scenario + char bytes_user_proof[64] = {0}; // Example proof + char bytes_host_proof[64] = {0}; + + // This should fail since user proof is zeros and doesn't match expected value + err = esp_srp_exchange_proofs(handle, (char *)username, strlen(username), + bytes_user_proof, bytes_host_proof); + TEST_ASSERT_EQUAL(ESP_FAIL, err); + + esp_srp_free(handle); +} + +// Add test for error handling with invalid parameters +static void test_srp_error_handling(void) { + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + + // Test with NULL salt + esp_err_t err = esp_srp_set_salt_verifier(handle, NULL, sizeof(sec2_salt), + sec2_verifier, sizeof(sec2_verifier)); + TEST_ASSERT_NOT_EQUAL(ESP_OK, err); + + // Test with zero salt length + err = esp_srp_set_salt_verifier(handle, sec2_salt, 0, + sec2_verifier, sizeof(sec2_verifier)); + TEST_ASSERT_NOT_EQUAL(ESP_OK, err); + + // Test with NULL verifier + err = esp_srp_set_salt_verifier(handle, sec2_salt, sizeof(sec2_salt), + NULL, sizeof(sec2_verifier)); + TEST_ASSERT_NOT_EQUAL(ESP_OK, err); + + // Test with zero verifier length + err = esp_srp_set_salt_verifier(handle, sec2_salt, sizeof(sec2_salt), + sec2_verifier, 0); + TEST_ASSERT_NOT_EQUAL(ESP_OK, err); + + esp_srp_free(handle); +} + +// Test verifier calculation consistency +static void test_srp_verifier_consistency(void) { + char *bytes_salt1 = NULL; + char *verifier1 = NULL; + int verifier_len1 = 0; + + // Generate first salt/verifier pair + esp_err_t err = esp_srp_gen_salt_verifier(username, strlen(username), + password, strlen(password), + &bytes_salt1, 16, &verifier1, &verifier_len1); + TEST_ASSERT_EQUAL(ESP_OK, err); + + // Generate second salt/verifier pair + char *bytes_salt2 = NULL; + char *verifier2 = NULL; + int verifier_len2 = 0; + err = esp_srp_gen_salt_verifier(username, strlen(username), + password, strlen(password), + &bytes_salt2, 16, &verifier2, &verifier_len2); + TEST_ASSERT_EQUAL(ESP_OK, err); + + // Salts should be different (randomly generated) + TEST_ASSERT_NOT_EQUAL(0, memcmp(bytes_salt1, bytes_salt2, 16)); + + // Verifiers should also be different since they depend on the salt + TEST_ASSERT_NOT_EQUAL(0, memcmp(verifier1, verifier2, verifier_len1)); + + free(bytes_salt1); + free(verifier1); + free(bytes_salt2); + free(verifier2); +} + +static void test_srp_pubkey_randomness(void) { + // Use two separate handles to avoid leaking internal state + // (calling srv_pubkey twice on the same handle overwrites hd->b, hd->B, hd->bytes_B) + esp_srp_handle_t *handle1 = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle1); + esp_err_t err = esp_srp_set_salt_verifier(handle1, sec2_salt, sizeof(sec2_salt), + sec2_verifier, sizeof(sec2_verifier)); + TEST_ASSERT_EQUAL(ESP_OK, err); + + char *bytes_B1 = NULL; + int len_B1 = 0; + err = esp_srp_srv_pubkey_from_salt_verifier(handle1, &bytes_B1, &len_B1); + TEST_ASSERT_EQUAL(ESP_OK, err); + TEST_ASSERT_NOT_NULL(bytes_B1); + TEST_ASSERT_EQUAL(384, len_B1); + + // Copy B1 before freeing handle1 (bytes_B1 is owned by handle1) + char B1_copy[384]; + memcpy(B1_copy, bytes_B1, len_B1); + esp_srp_free(handle1); + + esp_srp_handle_t *handle2 = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle2); + err = esp_srp_set_salt_verifier(handle2, sec2_salt, sizeof(sec2_salt), + sec2_verifier, sizeof(sec2_verifier)); + TEST_ASSERT_EQUAL(ESP_OK, err); + + char *bytes_B2 = NULL; + int len_B2 = 0; + err = esp_srp_srv_pubkey_from_salt_verifier(handle2, &bytes_B2, &len_B2); + TEST_ASSERT_EQUAL(ESP_OK, err); + TEST_ASSERT_NOT_NULL(bytes_B2); + TEST_ASSERT_EQUAL(384, len_B2); + + // Keys should be different due to random b generation + TEST_ASSERT_NOT_EQUAL(0, memcmp(B1_copy, bytes_B2, len_B1)); + + uint32_t crc1 = esp_rom_crc32_le(0, (uint8_t*)B1_copy, len_B1); + uint32_t crc2 = esp_rom_crc32_le(0, (uint8_t*)bytes_B2, len_B2); + ESP_LOGI(TAG, "Public key CRCs: %" PRIu32 ", %" PRIu32 " (should be different)", crc1, crc2); + + esp_srp_free(handle2); +} + +TEST_CASE("SRP init and free test", "[SRP]") +{ + test_srp_init_and_free(); +} + +TEST_CASE("SRP generate salt and verifier test", "[SRP]") +{ + test_srp_gen_salt_verifier(); +} + +TEST_CASE("SRP set salt and verifier test", "[SRP]") +{ + test_srp_set_salt_verifier(); +} + +TEST_CASE("SRP server public key test", "[SRP]") +{ + test_srp_srv_pubkey(); +} + +TEST_CASE("SRP get session key test", "[SRP]") +{ + test_srp_get_session_key(); +} + +TEST_CASE("SRP exchange proofs test", "[SRP]") +{ + test_srp_exchange_proofs(); +} + +TEST_CASE("SRP error handling test", "[SRP]") +{ + test_srp_error_handling(); +} + +TEST_CASE("SRP verifier consistency test", "[SRP]") +{ + test_srp_verifier_consistency(); +} + +TEST_CASE("SRP public key randomness test", "[SRP]") +{ + test_srp_pubkey_randomness(); +} + +/* RFC 5054 Section 2.5.4: server MUST abort if A % N == 0 + * Helper to set up a server-side SRP handle ready for session key derivation */ +static esp_srp_handle_t *setup_srp_server(void) +{ + esp_srp_handle_t *handle = esp_srp_init(ESP_NG_3072); + TEST_ASSERT_NOT_NULL(handle); + + char *bytes_B = NULL; + int len_B = 0; + char *bytes_salt = NULL; + esp_err_t err = esp_srp_srv_pubkey(handle, username, strlen(username), + password, strlen(password), 16, + &bytes_B, &len_B, &bytes_salt); + TEST_ASSERT_EQUAL(ESP_OK, err); + return handle; +} + +static void test_srp_reject_A_zero(void) +{ + esp_srp_handle_t *handle = setup_srp_server(); + + /* A = 0: attacker sends all-zero public key */ + char zero_A[384] = {0}; + char *bytes_key = NULL; + uint16_t len_key = 0; + esp_err_t err = esp_srp_get_session_key(handle, zero_A, sizeof(zero_A), + &bytes_key, &len_key); + TEST_ASSERT_EQUAL(ESP_FAIL, err); + TEST_ASSERT_NULL(bytes_key); + + esp_srp_free(handle); +} + +/* SRP 3072-bit prime N (same as N_3072 in esp_srp.c) */ +static const char test_N_3072[] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, + 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67, 0xCC, 0x74, + 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD, + 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37, + 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, + 0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED, + 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6, + 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE4, 0x5B, 0x3D, 0xC2, 0x00, 0x7C, 0xB8, 0xA1, 0x63, 0xBF, 0x05, + 0x98, 0xDA, 0x48, 0x36, 0x1C, 0x55, 0xD3, 0x9A, 0x69, 0x16, 0x3F, 0xA8, 0xFD, 0x24, 0xCF, 0x5F, + 0x83, 0x65, 0x5D, 0x23, 0xDC, 0xA3, 0xAD, 0x96, 0x1C, 0x62, 0xF3, 0x56, 0x20, 0x85, 0x52, 0xBB, + 0x9E, 0xD5, 0x29, 0x07, 0x70, 0x96, 0x96, 0x6D, 0x67, 0x0C, 0x35, 0x4E, 0x4A, 0xBC, 0x98, 0x04, + 0xF1, 0x74, 0x6C, 0x08, 0xCA, 0x18, 0x21, 0x7C, 0x32, 0x90, 0x5E, 0x46, 0x2E, 0x36, 0xCE, 0x3B, + 0xE3, 0x9E, 0x77, 0x2C, 0x18, 0x0E, 0x86, 0x03, 0x9B, 0x27, 0x83, 0xA2, 0xEC, 0x07, 0xA2, 0x8F, + 0xB5, 0xC5, 0x5D, 0xF0, 0x6F, 0x4C, 0x52, 0xC9, 0xDE, 0x2B, 0xCB, 0xF6, 0x95, 0x58, 0x17, 0x18, + 0x39, 0x95, 0x49, 0x7C, 0xEA, 0x95, 0x6A, 0xE5, 0x15, 0xD2, 0x26, 0x18, 0x98, 0xFA, 0x05, 0x10, + 0x15, 0x72, 0x8E, 0x5A, 0x8A, 0xAA, 0xC4, 0x2D, 0xAD, 0x33, 0x17, 0x0D, 0x04, 0x50, 0x7A, 0x33, + 0xA8, 0x55, 0x21, 0xAB, 0xDF, 0x1C, 0xBA, 0x64, 0xEC, 0xFB, 0x85, 0x04, 0x58, 0xDB, 0xEF, 0x0A, + 0x8A, 0xEA, 0x71, 0x57, 0x5D, 0x06, 0x0C, 0x7D, 0xB3, 0x97, 0x0F, 0x85, 0xA6, 0xE1, 0xE4, 0xC7, + 0xAB, 0xF5, 0xAE, 0x8C, 0xDB, 0x09, 0x33, 0xD7, 0x1E, 0x8C, 0x94, 0xE0, 0x4A, 0x25, 0x61, 0x9D, + 0xCE, 0xE3, 0xD2, 0x26, 0x1A, 0xD2, 0xEE, 0x6B, 0xF1, 0x2F, 0xFA, 0x06, 0xD9, 0x8A, 0x08, 0x64, + 0xD8, 0x76, 0x02, 0x73, 0x3E, 0xC8, 0x6A, 0x64, 0x52, 0x1F, 0x2B, 0x18, 0x17, 0x7B, 0x20, 0x0C, + 0xBB, 0xE1, 0x17, 0x57, 0x7A, 0x61, 0x5D, 0x6C, 0x77, 0x09, 0x88, 0xC0, 0xBA, 0xD9, 0x46, 0xE2, + 0x08, 0xE2, 0x4F, 0xA0, 0x74, 0xE5, 0xAB, 0x31, 0x43, 0xDB, 0x5B, 0xFC, 0xE0, 0xFD, 0x10, 0x8E, + 0x4B, 0x82, 0xD1, 0x20, 0xA9, 0x3A, 0xD2, 0xCA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF +}; + +static void test_srp_reject_A_equal_N(void) +{ + esp_srp_handle_t *handle = setup_srp_server(); + + /* A = N: attacker sends the prime itself (N mod N == 0) */ + char *bytes_key = NULL; + uint16_t len_key = 0; + esp_err_t err = esp_srp_get_session_key(handle, (char *)test_N_3072, + sizeof(test_N_3072), + &bytes_key, &len_key); + TEST_ASSERT_EQUAL(ESP_FAIL, err); + TEST_ASSERT_NULL(bytes_key); + + esp_srp_free(handle); +} + +TEST_CASE("SRP reject A=0 (RFC 5054 2.5.4)", "[SRP]") +{ + test_srp_reject_A_zero(); +} + +TEST_CASE("SRP reject A=N (RFC 5054 2.5.4)", "[SRP]") +{ + test_srp_reject_A_equal_N(); +}