Skip to content

Commit 8e4cbdf

Browse files
committed
Merge branch 'feat/configurable_mbedtls_sha1' into 'master'
feat(mbedtls): Make mbedtls SHA1 support configurable See merge request espressif/esp-idf!37795
2 parents 578c8b8 + 1a5b2a2 commit 8e4cbdf

File tree

9 files changed

+132
-67
lines changed

9 files changed

+132
-67
lines changed

components/mbedtls/Kconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,21 @@ menu "mbedTLS"
698698
Standard ECDSA is "fragile" in the sense that lack of entropy when signing
699699
may result in a compromise of the long-term signing key.
700700

701+
config MBEDTLS_SHA1_C
702+
bool "Enable the SHA-1 cryptographic hash algorithm"
703+
default y
704+
help
705+
Enabling MBEDTLS_SHA1_C adds support for SHA-1.
706+
SHA-1 is considered a weak message digest and its use constitutes
707+
a security risk.
708+
Disabling this configuration option could impact TLS 1.2 / Wi-Fi Enterprise compatibility
709+
with certain older certificates that rely on SHA-1 for digital signatures.
710+
Before proceeding, ensure that all your certificates are using stronger hash algorithms,
711+
such as SHA-256 (part of the SHA-2 family).
712+
If you're using older certificates or if you're unsure about the impact on your product,
713+
please consider testing the changes in a controlled environment for individual features
714+
like OTA updates, cloud connectivity, secure local control, etc.
715+
701716
config MBEDTLS_SHA512_C
702717
bool "Enable the SHA-384 and SHA-512 cryptographic hash algorithms"
703718
default y

components/mbedtls/port/include/mbedtls/esp_config.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2504,8 +2504,11 @@
25042504
* on it, and considering stronger message digests instead.
25052505
*
25062506
*/
2507+
#if CONFIG_MBEDTLS_SHA1_C
25072508
#define MBEDTLS_SHA1_C
2508-
2509+
#else
2510+
#undef MBEDTLS_SHA1_C
2511+
#endif
25092512
/**
25102513
* \def MBEDTLS_SHA224_C
25112514
*

components/mbedtls/port/sha/core/esp_sha1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include <mbedtls/build_info.h>
1717

18-
#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_SHA1_ALT)
18+
#if defined(MBEDTLS_SHA1_ALT)
1919

2020
#include "mbedtls/sha1.h"
2121

@@ -241,4 +241,4 @@ int mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
241241
return ret;
242242
}
243243

244-
#endif /* MBEDTLS_SHA1_C && MBEDTLS_SHA1_ALT */
244+
#endif /* MBEDTLS_SHA1_ALT */

components/mbedtls/port/sha/parallel_engine/esp_sha1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <mbedtls/build_info.h>
1919

20-
#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_SHA1_ALT)
20+
#if defined(MBEDTLS_SHA1_ALT)
2121

2222
#include "mbedtls/sha1.h"
2323

@@ -420,4 +420,4 @@ int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
420420
return ret;
421421
}
422422

423-
#endif /* MBEDTLS_SHA1_C && MBEDTLS_SHA1_ALT */
423+
#endif /* MBEDTLS_SHA1_ALT */

components/mbedtls/test_apps/main/test_mbedtls_sha.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -27,9 +27,13 @@
2727
TEST_CASE("mbedtls SHA self-tests", "[mbedtls]")
2828
{
2929
start_apb_access_loop();
30+
#if CONFIG_MBEDTLS_SHA1_C
3031
TEST_ASSERT_FALSE_MESSAGE(mbedtls_sha1_self_test(1), "SHA1 self-tests should pass.");
32+
#endif
3133
TEST_ASSERT_FALSE_MESSAGE(mbedtls_sha256_self_test(1), "SHA256 self-tests should pass.");
34+
#if CONFIG_MBEDTLS_SHA512_C
3235
TEST_ASSERT_FALSE_MESSAGE(mbedtls_sha512_self_test(1), "SHA512 self-tests should pass.");
36+
#endif
3337
verify_apb_access_loop();
3438
}
3539

@@ -158,17 +162,19 @@ TEST_CASE("mbedtls SHA multithreading", "[mbedtls]")
158162
void tskRunSHASelftests(void *param)
159163
{
160164
for (int i = 0; i < 5; i++) {
165+
#if CONFIG_MBEDTLS_SHA1_C
161166
if (mbedtls_sha1_self_test(1)) {
162167
printf("SHA1 self-tests failed.\n");
163168
while (1) {}
164169
}
170+
#endif
165171

166172
if (mbedtls_sha256_self_test(1)) {
167173
printf("SHA256 self-tests failed.\n");
168174
while (1) {}
169175
}
170176

171-
#if SOC_SHA_SUPPORT_SHA512
177+
#if SOC_SHA_SUPPORT_SHA512 && CONFIG_MBEDTLS_SHA512_C
172178
if (mbedtls_sha512_self_test(1)) {
173179
printf("SHA512 self-tests failed.\n");
174180
while (1) {}
@@ -178,7 +184,7 @@ void tskRunSHASelftests(void *param)
178184
printf("SHA512 self-tests failed.\n");
179185
while (1) {}
180186
}
181-
#endif //SOC_SHA_SUPPORT_SHA512
187+
#endif //SOC_SHA_SUPPORT_SHA512 && CONFIG_MBEDTLS_SHA512_C
182188
}
183189
xSemaphoreGive(done_sem);
184190
vTaskDelete(NULL);

components/mbedtls/test_apps/main/test_rsa.c

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Focus on testing functionality where we use ESP32 hardware
44
* accelerated crypto features
55
*
6-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
6+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
77
*
88
* SPDX-License-Identifier: Apache-2.0
99
*/
@@ -88,25 +88,33 @@ static const char *rsa3072_cert = "-----BEGIN CERTIFICATE-----\n"\
8888
/* Root cert from openssl s_client -connect google.com:443 -showcerts
8989
*/
9090
static const char *rsa2048_cert = "-----BEGIN CERTIF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qw\n"\
116+
"qDTBU39CluVIQeuQRgwG3MuSxl7zRERDRilGoKb8uY45JzmxWuKxrfwT/478JuHU\n"\
117+
"/oTxUFqOl2stKnn7QGTq8z29W+GgBLCXSBxC9epaHM0myFH/FJlniXJfHeytWt0=\n"\
110118
"-----END CERTIFICATE-----\n";
111119

112120

@@ -211,38 +219,38 @@ static const uint8_t pki_rsa3072_output[] = {
211219
};
212220

213221
static const uint8_t pki_rsa2048_output[] = {
214-
0x47, 0x0b, 0xe5, 0x8a, 0xcd, 0x2f, 0x78, 0x07,
215-
0x69, 0x69, 0x70, 0xff, 0x81, 0xdf, 0x96, 0xf0,
216-
0xed, 0x82, 0x3a, 0x3d, 0x46, 0xab, 0xe9, 0xc3,
217-
0xb5, 0xd9, 0xca, 0xa2, 0x05, 0xa9, 0xf6, 0x6e,
218-
0xad, 0x6c, 0xe0, 0xd1, 0xa2, 0xb4, 0xf2, 0x78,
219-
0x4a, 0x93, 0xfc, 0x45, 0xe1, 0x9b, 0xdd, 0x62,
220-
0xf9, 0x66, 0x2a, 0x14, 0x38, 0x12, 0xb6, 0x50,
221-
0x0b, 0xe3, 0x53, 0x9c, 0x12, 0x56, 0xf1, 0xb7,
222-
0x83, 0xd5, 0xf3, 0x24, 0x81, 0xcc, 0x5a, 0xeb,
223-
0xec, 0xac, 0x68, 0xa8, 0x0c, 0xd7, 0x84, 0x7a,
224-
0xbb, 0x77, 0x7b, 0xd5, 0x5b, 0xcf, 0x7b, 0x25,
225-
0xd0, 0x75, 0x80, 0x21, 0x12, 0x97, 0x6b, 0xe1,
226-
0xb6, 0x51, 0x12, 0x52, 0x6e, 0x01, 0x92, 0xb7,
227-
0xcc, 0x70, 0x4b, 0x46, 0x11, 0x98, 0x5a, 0x84,
228-
0x1c, 0x90, 0x45, 0x0f, 0x15, 0x77, 0xdb, 0x79,
229-
0xe8, 0xff, 0x1f, 0xaa, 0x58, 0x95, 0xce, 0x3c,
230-
0x65, 0x0c, 0x66, 0x29, 0xe1, 0x9c, 0x41, 0xbb,
231-
0xde, 0x65, 0xb8, 0x29, 0x36, 0x94, 0xbd, 0x87,
232-
0x93, 0x39, 0xc5, 0xeb, 0x49, 0x21, 0xc1, 0xeb,
233-
0x48, 0xbd, 0x19, 0x13, 0x4d, 0x40, 0x90, 0x88,
234-
0xc6, 0x12, 0xd9, 0xf7, 0xdd, 0xc8, 0x4f, 0x89,
235-
0xc0, 0x91, 0xf8, 0xeb, 0xcf, 0xe3, 0x12, 0x17,
236-
0x88, 0x9c, 0x88, 0xf4, 0xf5, 0xae, 0xf4, 0x15,
237-
0xfe, 0x17, 0xf6, 0xa4, 0x74, 0x49, 0x02, 0x05,
238-
0x11, 0x3b, 0x92, 0x25, 0x39, 0x2c, 0x4b, 0x08,
239-
0x19, 0x76, 0x13, 0x8d, 0xf9, 0xda, 0xae, 0xdf,
240-
0x30, 0xda, 0xcc, 0xbb, 0x3f, 0xb9, 0xb0, 0xd6,
241-
0x5c, 0x78, 0x4b, 0x2b, 0x35, 0x51, 0x17, 0x48,
242-
0xf5, 0xd4, 0x39, 0x7e, 0x05, 0x83, 0x68, 0x86,
243-
0x44, 0x5f, 0x56, 0x1d, 0x2c, 0x53, 0xd3, 0x64,
244-
0x3a, 0xb2, 0x0c, 0x4a, 0x85, 0xd6, 0x5b, 0x7e,
245-
0xf9, 0xe9, 0x50, 0x29, 0x5d, 0x4f, 0xcc, 0xc9,
222+
0x3c, 0xd6, 0xc2, 0xbf, 0x01, 0x4a, 0x00, 0x95,
223+
0x2c, 0x32, 0x11, 0xc0, 0xc9, 0x7e, 0x8f, 0x0a,
224+
0x15, 0xee, 0xfb, 0x34, 0x1d, 0xaa, 0xae, 0x15,
225+
0x11, 0x6d, 0x99, 0x2b, 0x09, 0xeb, 0x3f, 0x89,
226+
0x46, 0x98, 0x08, 0x2f, 0x10, 0x13, 0xa1, 0x17,
227+
0xc7, 0xec, 0x67, 0x3a, 0x34, 0x4f, 0x40, 0xcd,
228+
0xe2, 0xc0, 0xbe, 0x99, 0xc7, 0xe7, 0xff, 0xea,
229+
0xd0, 0x82, 0xd2, 0x62, 0x73, 0xde, 0x56, 0xe8,
230+
0xb6, 0xa7, 0xe7, 0xe1, 0x64, 0x90, 0x00, 0x56,
231+
0x1d, 0x2c, 0x1c, 0xc5, 0xec, 0x7f, 0xb1, 0x87,
232+
0x59, 0xb1, 0xd6, 0x44, 0x0f, 0x67, 0x35, 0xb4,
233+
0x91, 0x49, 0xed, 0x10, 0x4c, 0xef, 0xe5, 0xc8,
234+
0xea, 0x0d, 0xbd, 0xaf, 0xb9, 0xad, 0x12, 0x41,
235+
0xaa, 0xf4, 0x68, 0x54, 0x08, 0xec, 0x70, 0x8c,
236+
0xac, 0x6b, 0x57, 0xcf, 0x0a, 0x0c, 0x08, 0x34,
237+
0x28, 0x29, 0x27, 0xa4, 0x71, 0x80, 0x43, 0x59,
238+
0xd9, 0x35, 0x88, 0x28, 0x1d, 0xfa, 0x0b, 0x72,
239+
0xa0, 0xe1, 0x03, 0x65, 0x7a, 0xf8, 0x1c, 0x76,
240+
0x9a, 0xad, 0x21, 0x23, 0x11, 0x2f, 0x45, 0x40,
241+
0x72, 0x05, 0x69, 0x1b, 0x2a, 0x74, 0x9f, 0x95,
242+
0x44, 0x60, 0x05, 0x6a, 0x17, 0x80, 0x4a, 0xa0,
243+
0xed, 0x23, 0xa6, 0xef, 0x79, 0x5d, 0x83, 0xd8,
244+
0x8d, 0xd8, 0xe1, 0x4c, 0x5e, 0xf8, 0xfa, 0x11,
245+
0x57, 0xbe, 0xca, 0x22, 0x93, 0x5b, 0xe6, 0x8b,
246+
0xe1, 0x31, 0xde, 0x70, 0x80, 0x4a, 0xa2, 0xd3,
247+
0x91, 0xe8, 0xde, 0x88, 0xa2, 0x98, 0x73, 0x49,
248+
0x0d, 0x26, 0xe1, 0x42, 0xd7, 0xb9, 0x5e, 0xf6,
249+
0x05, 0x09, 0x27, 0xc6, 0x8c, 0xc2, 0xb1, 0x53,
250+
0x5f, 0x19, 0xaf, 0x2b, 0xfe, 0xac, 0x6a, 0x27,
251+
0xde, 0x89, 0xbc, 0x72, 0x3e, 0xd5, 0x9f, 0x36,
252+
0xc2, 0x91, 0x68, 0x30, 0xe7, 0x76, 0x96, 0x56,
253+
0x8f, 0x01, 0xc4, 0x5b, 0xb7, 0xb3, 0x90, 0x7f,
246254
};
247255

248256
#ifdef CONFIG_MBEDTLS_HARDWARE_MPI
@@ -573,7 +581,7 @@ TEST_CASE("mbedtls RSA Generate Key", "[mbedtls][timeout=60]")
573581
const int exponent = 65537;
574582

575583
#if CONFIG_MBEDTLS_MPI_USE_INTERRUPT && CONFIG_ESP_TASK_WDT_EN && !CONFIG_ESP_TASK_WDT_INIT
576-
/* Check that generating keys doesnt starve the watchdog if interrupt-based driver is used */
584+
/* Check that generating keys doesn't starve the watchdog if interrupt-based driver is used */
577585
esp_task_wdt_config_t twdt_config = {
578586
.timeout_ms = 1000,
579587
.idle_core_mask = (1 << 0), // Watch core 0 idle

components/mbedtls/test_apps/main/test_sha.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -119,14 +119,17 @@ TEST_CASE("Test esp_sha()", "[hw_crypto]")
119119

120120
TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
121121
{
122+
int r = -1;
122123
const void* ptr;
123124
spi_flash_mmap_handle_t handle;
125+
#if CONFIG_MBEDTLS_SHA1_C
124126
uint8_t sha1_espsha[20] = { 0 };
125127
uint8_t sha1_mbedtls[20] = { 0 };
128+
#endif
126129
uint8_t sha256_espsha[32] = { 0 };
127130
uint8_t sha256_mbedtls[32] = { 0 };
128131

129-
#if SOC_SHA_SUPPORT_SHA512
132+
#if SOC_SHA_SUPPORT_SHA512 && CONFIG_MBEDTLS_SHA512_C
130133
uint8_t sha512_espsha[64] = { 0 };
131134
uint8_t sha512_mbedtls[64] = { 0 };
132135
#endif
@@ -140,16 +143,17 @@ TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
140143
TEST_ASSERT_NOT_NULL(ptr);
141144

142145
/* Compare esp_sha() result to the mbedTLS result, should always be the same */
143-
146+
#if CONFIG_MBEDTLS_SHA1_C
144147
esp_sha(SHA1, ptr, LEN, sha1_espsha);
145-
int r = mbedtls_sha1(ptr, LEN, sha1_mbedtls);
148+
r = mbedtls_sha1(ptr, LEN, sha1_mbedtls);
146149
TEST_ASSERT_EQUAL(0, r);
150+
#endif
147151

148152
esp_sha(SHA2_256, ptr, LEN, sha256_espsha);
149153
r = mbedtls_sha256(ptr, LEN, sha256_mbedtls, 0);
150154
TEST_ASSERT_EQUAL(0, r);
151155

152-
#if SOC_SHA_SUPPORT_SHA512
156+
#if SOC_SHA_SUPPORT_SHA512 && CONFIG_MBEDTLS_SHA512_C
153157
esp_sha(SHA2_512, ptr, LEN, sha512_espsha);
154158
r = mbedtls_sha512(ptr, LEN, sha512_mbedtls, 0);
155159
TEST_ASSERT_EQUAL(0, r);
@@ -158,11 +162,13 @@ TEST_CASE("Test esp_sha() function with long input", "[hw_crypto]")
158162
/* munmap() 1MB of flash when the usge of memory-mapped ptr is over */
159163
spi_flash_munmap(handle);
160164

165+
#if CONFIG_MBEDTLS_SHA1_C
161166
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha1_espsha, sha1_mbedtls, sizeof(sha1_espsha), "SHA1 results should match");
167+
#endif
162168

163169
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha256_espsha, sha256_mbedtls, sizeof(sha256_espsha), "SHA256 results should match");
164170

165-
#if SOC_SHA_SUPPORT_SHA512
171+
#if SOC_SHA_SUPPORT_SHA512 && CONFIG_MBEDTLS_SHA512_C
166172
TEST_ASSERT_EQUAL_MEMORY_MESSAGE(sha512_espsha, sha512_mbedtls, sizeof(sha512_espsha), "SHA512 results should match");
167173
#endif
168174
}

components/wpa_supplicant/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ if(CONFIG_ESP_WIFI_MBEDTLS_CRYPTO)
127127
if(NOT CONFIG_MBEDTLS_DES_C)
128128
set(crypto_src ${crypto_src} "src/crypto/des-internal.c")
129129
endif()
130+
if(NOT CONFIG_MBEDTLS_SHA1_C)
131+
set(crypto_src ${crypto_src} "src/crypto/sha1.c")
132+
endif()
130133
# Enabling this only for WiFi is probably not a good idea since MbedTLS
131134
# uses generic crypto init/update functions for this. That causes
132135
# binary size increment since all the other enabled module

components/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "aes_wrap.h"
3636
#include "crypto.h"
3737
#include "mbedtls/esp_config.h"
38+
#include "mbedtls/sha1.h"
3839

3940
#ifdef CONFIG_FAST_PBKDF2
4041
#include "fastpbkdf2.h"
@@ -105,7 +106,28 @@ int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
105106

106107
int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
107108
{
109+
#if defined(MBEDTLS_SHA1_C)
108110
return digest_vector(MBEDTLS_MD_SHA1, num_elem, addr, len, mac);
111+
#elif defined(MBEDTLS_SHA1_ALT)
112+
mbedtls_sha1_context ctx;
113+
size_t i;
114+
int ret;
115+
116+
mbedtls_sha1_init(&ctx);
117+
for (i = 0; i < num_elem; i++) {
118+
ret = mbedtls_sha1_update(&ctx, addr[i], len[i]);
119+
if (ret != 0) {
120+
goto exit;
121+
}
122+
}
123+
ret = mbedtls_sha1_finish(&ctx, mac);
124+
125+
exit:
126+
mbedtls_sha1_free(&ctx);
127+
return ret;
128+
#else
129+
return -ENOTSUP;
130+
#endif
109131
}
110132

111133
int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
@@ -363,6 +385,7 @@ int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
363385
return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
364386
}
365387

388+
#ifdef MBEDTLS_SHA1_C
366389
int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
367390
const u8 *addr[], const size_t *len, u8 *mac)
368391
{
@@ -375,6 +398,7 @@ int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
375398
{
376399
return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
377400
}
401+
#endif
378402

379403
static void *aes_crypt_init(int mode, const u8 *key, size_t len)
380404
{

0 commit comments

Comments
 (0)