From 5fafdbb5a143c12bd48b459bfba2c513fe9d2b8b Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 10:29:32 +0100 Subject: [PATCH 01/11] programs: remove dh_client and dh_server These sample programs depend on MBEDTLS_DHM_C which is being removed, so they should be as well. Signed-off-by: Valerio Setti --- programs/Makefile | 10 -- programs/pkey/CMakeLists.txt | 15 +- programs/pkey/dh_client.c | 288 --------------------------------- programs/pkey/dh_server.c | 306 ----------------------------------- 4 files changed, 1 insertion(+), 618 deletions(-) delete mode 100644 programs/pkey/dh_client.c delete mode 100644 programs/pkey/dh_server.c diff --git a/programs/Makefile b/programs/Makefile index c177c28a2542..2e49da298c83 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -38,9 +38,7 @@ APPS = \ hash/generic_sum \ hash/hello \ hash/md_hmac_demo \ - pkey/dh_client \ pkey/dh_genprime \ - pkey/dh_server \ pkey/ecdh_curve25519 \ pkey/ecdsa \ pkey/gen_key \ @@ -175,18 +173,10 @@ hash/md_hmac_demo$(EXEXT): hash/md_hmac_demo.c $(DEP) echo " CC hash/md_hmac_demo.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) hash/md_hmac_demo.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ -pkey/dh_client$(EXEXT): pkey/dh_client.c $(DEP) - echo " CC pkey/dh_client.c" - $(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/dh_client.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ - pkey/dh_genprime$(EXEXT): pkey/dh_genprime.c $(DEP) echo " CC pkey/dh_genprime.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/dh_genprime.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ -pkey/dh_server$(EXEXT): pkey/dh_server.c $(DEP) - echo " CC pkey/dh_server.c" - $(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/dh_server.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ - pkey/ecdh_curve25519$(EXEXT): pkey/ecdh_curve25519.c $(DEP) echo " CC pkey/ecdh_curve25519.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) pkey/ecdh_curve25519.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ diff --git a/programs/pkey/CMakeLists.txt b/programs/pkey/CMakeLists.txt index c782ad4655b8..df63ffc89cf8 100644 --- a/programs/pkey/CMakeLists.txt +++ b/programs/pkey/CMakeLists.txt @@ -1,16 +1,3 @@ -set(executables_mbedtls - dh_client - dh_server -) -add_dependencies(${programs_target} ${executables_mbedtls}) - -foreach(exe IN LISTS executables_mbedtls) - add_executable(${exe} ${exe}.c $) - set_base_compile_options(${exe}) - target_link_libraries(${exe} ${mbedtls_target} ${CMAKE_THREAD_LIBS_INIT}) - target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../framework/tests/include) -endforeach() - set(executables_mbedcrypto dh_genprime ecdh_curve25519 @@ -40,6 +27,6 @@ foreach(exe IN LISTS executables_mbedcrypto) target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../framework/tests/include) endforeach() -install(TARGETS ${executables_mbedtls} ${executables_mbedcrypto} +install(TARGETS ${executables_mbedcrypto} DESTINATION "bin" PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) diff --git a/programs/pkey/dh_client.c b/programs/pkey/dh_client.c deleted file mode 100644 index a3bc49d3f8e3..000000000000 --- a/programs/pkey/dh_client.c +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Diffie-Hellman-Merkle key exchange (client side) - * - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - */ - -#include "mbedtls/build_info.h" - -#include "mbedtls/platform.h" -/* md.h is included this early since MD_CAN_XXX macros are defined there. */ -#include "mbedtls/md.h" - -#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \ - defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \ - defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \ - defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) -#include "mbedtls/net_sockets.h" -#include "mbedtls/aes.h" -#include "mbedtls/dhm.h" -#include "mbedtls/rsa.h" -#include "mbedtls/sha256.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" - -#include -#include -#endif - -#define SERVER_NAME "localhost" -#define SERVER_PORT "11999" - -#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \ - !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \ - !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \ - !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) -int main(void) -{ - mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C " - "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or " - "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_FS_IO and/or " - "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_SHA1_C not defined.\n"); - mbedtls_exit(0); -} - -#elif defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT) -int main(void) -{ - mbedtls_printf("MBEDTLS_BLOCK_CIPHER_NO_DECRYPT defined.\n"); - mbedtls_exit(0); -} -#else - - -int main(void) -{ - FILE *f; - - int ret = 1; - int exit_code = MBEDTLS_EXIT_FAILURE; - unsigned int mdlen; - size_t n, buflen; - mbedtls_net_context server_fd; - - unsigned char *p, *end; - unsigned char buf[2048]; - unsigned char hash[MBEDTLS_MD_MAX_SIZE]; - mbedtls_mpi N, E; - const char *pers = "dh_client"; - - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_rsa_context rsa; - mbedtls_dhm_context dhm; - mbedtls_aes_context aes; - - mbedtls_net_init(&server_fd); - mbedtls_dhm_init(&dhm); - mbedtls_aes_init(&aes); - mbedtls_ctr_drbg_init(&ctr_drbg); - mbedtls_mpi_init(&N); - mbedtls_mpi_init(&E); - - /* - * 1. Setup the RNG - */ - mbedtls_printf("\n . Seeding the random number generator"); - fflush(stdout); - - mbedtls_entropy_init(&entropy); - if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - (const unsigned char *) pers, - strlen(pers))) != 0) { - mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret); - goto exit; - } - - /* - * 2. Read the server's public RSA key - */ - mbedtls_printf("\n . Reading public key from rsa_pub.txt"); - fflush(stdout); - - if ((f = fopen("rsa_pub.txt", "rb")) == NULL) { - mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \ - " ! Please run rsa_genkey first\n\n"); - goto exit; - } - - mbedtls_rsa_init(&rsa); - if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 || - (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 || - (ret = mbedtls_rsa_import(&rsa, &N, NULL, NULL, NULL, &E) != 0)) { - mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret); - fclose(f); - goto exit; - } - fclose(f); - - /* - * 3. Initiate the connection - */ - mbedtls_printf("\n . Connecting to tcp/%s/%s", SERVER_NAME, - SERVER_PORT); - fflush(stdout); - - if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME, - SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret); - goto exit; - } - - /* - * 4a. First get the buffer length - */ - mbedtls_printf("\n . Receiving the server's DH parameters"); - fflush(stdout); - - memset(buf, 0, sizeof(buf)); - - if ((ret = mbedtls_net_recv(&server_fd, buf, 2)) != 2) { - mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret); - goto exit; - } - - n = buflen = (buf[0] << 8) | buf[1]; - if (buflen < 1 || buflen > sizeof(buf)) { - mbedtls_printf(" failed\n ! Got an invalid buffer length\n\n"); - goto exit; - } - - /* - * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P - */ - memset(buf, 0, sizeof(buf)); - - if ((ret = mbedtls_net_recv(&server_fd, buf, n)) != (int) n) { - mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret); - goto exit; - } - - p = buf, end = buf + buflen; - - if ((ret = mbedtls_dhm_read_params(&dhm, &p, end)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret); - goto exit; - } - - n = mbedtls_dhm_get_len(&dhm); - if (n < 64 || n > 512) { - mbedtls_printf(" failed\n ! Invalid DHM modulus size\n\n"); - goto exit; - } - - /* - * 5. Check that the server's RSA signature matches - * the SHA-256 hash of (P,G,Ys) - */ - mbedtls_printf("\n . Verifying the server's RSA signature"); - fflush(stdout); - - p += 2; - - if ((n = (size_t) (end - p)) != mbedtls_rsa_get_len(&rsa)) { - mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n"); - goto exit; - } - - mdlen = (unsigned int) mbedtls_md_get_size(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)); - if (mdlen == 0) { - mbedtls_printf(" failed\n ! Invalid digest type\n\n"); - goto exit; - } - - if ((ret = mbedtls_sha256(buf, (int) (p - 2 - buf), hash, 0)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_sha256 returned %d\n\n", ret); - goto exit; - } - - if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, MBEDTLS_MD_SHA256, - mdlen, hash, p)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret); - goto exit; - } - - /* - * 6. Send our public value: Yc = G ^ Xc mod P - */ - mbedtls_printf("\n . Sending own public value to server"); - fflush(stdout); - - n = mbedtls_dhm_get_len(&dhm); - if ((ret = mbedtls_dhm_make_public(&dhm, (int) n, buf, n, - mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret); - goto exit; - } - - if ((ret = mbedtls_net_send(&server_fd, buf, n)) != (int) n) { - mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret); - goto exit; - } - - /* - * 7. Derive the shared secret: K = Ys ^ Xc mod P - */ - mbedtls_printf("\n . Shared secret: "); - fflush(stdout); - - if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n, - mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret); - goto exit; - } - - for (n = 0; n < 16; n++) { - mbedtls_printf("%02x", buf[n]); - } - - /* - * 8. Setup the AES-256 decryption key - * - * This is an overly simplified example; best practice is - * to hash the shared secret with a random value to derive - * the keying material for the encryption/decryption keys, - * IVs and MACs. - */ - mbedtls_printf("...\n . Receiving and decrypting the ciphertext"); - fflush(stdout); - - ret = mbedtls_aes_setkey_dec(&aes, buf, 256); - if (ret != 0) { - goto exit; - } - - memset(buf, 0, sizeof(buf)); - - if ((ret = mbedtls_net_recv(&server_fd, buf, 16)) != 16) { - mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret); - goto exit; - } - - ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, buf); - if (ret != 0) { - goto exit; - } - buf[16] = '\0'; - mbedtls_printf("\n . Plaintext is \"%s\"\n\n", (char *) buf); - - exit_code = MBEDTLS_EXIT_SUCCESS; - -exit: - - mbedtls_net_free(&server_fd); - - mbedtls_aes_free(&aes); - mbedtls_rsa_free(&rsa); - mbedtls_dhm_free(&dhm); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - mbedtls_mpi_free(&N); - mbedtls_mpi_free(&E); - - mbedtls_exit(exit_code); -} -#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C && - MBEDTLS_NET_C && MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 && - MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ diff --git a/programs/pkey/dh_server.c b/programs/pkey/dh_server.c deleted file mode 100644 index 26b48e3ff2b1..000000000000 --- a/programs/pkey/dh_server.c +++ /dev/null @@ -1,306 +0,0 @@ -/* - * Diffie-Hellman-Merkle key exchange (server side) - * - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later - */ - -#include "mbedtls/build_info.h" - -#include "mbedtls/platform.h" -/* md.h is included this early since MD_CAN_XXX macros are defined there. */ -#include "mbedtls/md.h" - -#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \ - defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \ - defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \ - defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) -#include "mbedtls/net_sockets.h" -#include "mbedtls/aes.h" -#include "mbedtls/dhm.h" -#include "mbedtls/rsa.h" -#include "mbedtls/sha256.h" -#include "mbedtls/entropy.h" -#include "mbedtls/ctr_drbg.h" - -#include -#include -#endif - -#define SERVER_PORT "11999" -#define PLAINTEXT "==Hello there!==" - -#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \ - !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \ - !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \ - !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) -int main(void) -{ - mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C " - "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or " - "PSA_WANT_ALG_SHA_256 and/or MBEDTLS_FS_IO and/or " - "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_SHA1_C not defined.\n"); - mbedtls_exit(0); -} -#else - - -int main(void) -{ - FILE *f; - - int ret = 1; - int exit_code = MBEDTLS_EXIT_FAILURE; - unsigned int mdlen; - size_t n, buflen; - mbedtls_net_context listen_fd, client_fd; - - unsigned char buf[2048]; - unsigned char hash[MBEDTLS_MD_MAX_SIZE]; - unsigned char buf2[2]; - const char *pers = "dh_server"; - - mbedtls_entropy_context entropy; - mbedtls_ctr_drbg_context ctr_drbg; - mbedtls_rsa_context rsa; - mbedtls_dhm_context dhm; - mbedtls_aes_context aes; - - mbedtls_mpi N, P, Q, D, E, dhm_P, dhm_G; - - mbedtls_net_init(&listen_fd); - mbedtls_net_init(&client_fd); - mbedtls_dhm_init(&dhm); - mbedtls_aes_init(&aes); - mbedtls_ctr_drbg_init(&ctr_drbg); - - mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); - mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&dhm_P); - mbedtls_mpi_init(&dhm_G); - /* - * 1. Setup the RNG - */ - mbedtls_printf("\n . Seeding the random number generator"); - fflush(stdout); - - mbedtls_entropy_init(&entropy); - if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, - (const unsigned char *) pers, - strlen(pers))) != 0) { - mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret); - goto exit; - } - - /* - * 2a. Read the server's private RSA key - */ - mbedtls_printf("\n . Reading private key from rsa_priv.txt"); - fflush(stdout); - - if ((f = fopen("rsa_priv.txt", "rb")) == NULL) { - mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \ - " ! Please run rsa_genkey first\n\n"); - goto exit; - } - - mbedtls_rsa_init(&rsa); - - if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 || - (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 || - (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 || - (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 || - (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", - ret); - fclose(f); - goto exit; - } - fclose(f); - - if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n", - ret); - goto exit; - } - - if ((ret = mbedtls_rsa_complete(&rsa)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n", - ret); - goto exit; - } - - /* - * 2b. Get the DHM modulus and generator - */ - mbedtls_printf("\n . Reading DH parameters from dh_prime.txt"); - fflush(stdout); - - if ((f = fopen("dh_prime.txt", "rb")) == NULL) { - mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \ - " ! Please run dh_genprime first\n\n"); - goto exit; - } - - if ((ret = mbedtls_mpi_read_file(&dhm_P, 16, f)) != 0 || - (ret = mbedtls_mpi_read_file(&dhm_G, 16, f)) != 0 || - (ret = mbedtls_dhm_set_group(&dhm, &dhm_P, &dhm_G) != 0)) { - mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n"); - fclose(f); - goto exit; - } - - fclose(f); - - /* - * 3. Wait for a client to connect - */ - mbedtls_printf("\n . Waiting for a remote connection"); - fflush(stdout); - - if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret); - goto exit; - } - - if ((ret = mbedtls_net_accept(&listen_fd, &client_fd, - NULL, 0, NULL)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret); - goto exit; - } - - /* - * 4. Setup the DH parameters (P,G,Ys) - */ - mbedtls_printf("\n . Sending the server's DH parameters"); - fflush(stdout); - - memset(buf, 0, sizeof(buf)); - - if ((ret = - mbedtls_dhm_make_params(&dhm, (int) mbedtls_dhm_get_len(&dhm), buf, &n, - mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret); - goto exit; - } - - /* - * 5. Sign the parameters and send them - */ - - mdlen = (unsigned int) mbedtls_md_get_size(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256)); - if (mdlen == 0) { - mbedtls_printf(" failed\n ! Invalid digest type\n\n"); - goto exit; - } - - if ((ret = mbedtls_sha256(buf, n, hash, 0)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_sha256 returned %d\n\n", ret); - goto exit; - } - - const size_t rsa_key_len = mbedtls_rsa_get_len(&rsa); - buf[n] = (unsigned char) (rsa_key_len >> 8); - buf[n + 1] = (unsigned char) (rsa_key_len); - - if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, - MBEDTLS_MD_SHA256, mdlen, - hash, buf + n + 2)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret); - goto exit; - } - - buflen = n + 2 + rsa_key_len; - buf2[0] = (unsigned char) (buflen >> 8); - buf2[1] = (unsigned char) (buflen); - - if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 || - (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) { - mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret); - goto exit; - } - - /* - * 6. Get the client's public value: Yc = G ^ Xc mod P - */ - mbedtls_printf("\n . Receiving the client's public value"); - fflush(stdout); - - memset(buf, 0, sizeof(buf)); - - n = mbedtls_dhm_get_len(&dhm); - if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) { - mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret); - goto exit; - } - - if ((ret = mbedtls_dhm_read_public(&dhm, buf, n)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret); - goto exit; - } - - /* - * 7. Derive the shared secret: K = Ys ^ Xc mod P - */ - mbedtls_printf("\n . Shared secret: "); - fflush(stdout); - - if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n, - mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret); - goto exit; - } - - for (n = 0; n < 16; n++) { - mbedtls_printf("%02x", buf[n]); - } - - /* - * 8. Setup the AES-256 encryption key - * - * This is an overly simplified example; best practice is - * to hash the shared secret with a random value to derive - * the keying material for the encryption/decryption keys - * and MACs. - */ - mbedtls_printf("...\n . Encrypting and sending the ciphertext"); - fflush(stdout); - - ret = mbedtls_aes_setkey_enc(&aes, buf, 256); - if (ret != 0) { - goto exit; - } - memcpy(buf, PLAINTEXT, 16); - ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf); - if (ret != 0) { - goto exit; - } - - if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) { - mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret); - goto exit; - } - - mbedtls_printf("\n\n"); - - exit_code = MBEDTLS_EXIT_SUCCESS; - -exit: - - mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); - mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&dhm_P); - mbedtls_mpi_free(&dhm_G); - - mbedtls_net_free(&client_fd); - mbedtls_net_free(&listen_fd); - - mbedtls_aes_free(&aes); - mbedtls_rsa_free(&rsa); - mbedtls_dhm_free(&dhm); - mbedtls_ctr_drbg_free(&ctr_drbg); - mbedtls_entropy_free(&entropy); - - mbedtls_exit(exit_code); -} -#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C && - MBEDTLS_NET_C && MBEDTLS_RSA_C && PSA_WANT_ALG_SHA_256 && - MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */ From 0afe58cee066269bc1365cbd86c7ab4b06b33550 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 10:46:54 +0100 Subject: [PATCH 02/11] programs: remove DHM_C from ssl_client2 and ssl_server2 MBEDTLS_DHM_C is being removed so all its occurencies should be removed as well. Signed-off-by: Valerio Setti --- programs/ssl/ssl_client2.c | 22 ---------------- programs/ssl/ssl_server2.c | 51 +------------------------------------- 2 files changed, 1 insertion(+), 72 deletions(-) diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c index f009a3169b49..6742925f2a8f 100644 --- a/programs/ssl/ssl_client2.c +++ b/programs/ssl/ssl_client2.c @@ -71,7 +71,6 @@ int main(void) #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE #define DFL_TRUNC_HMAC -1 #define DFL_RECSPLIT -1 -#define DFL_DHMLEN -1 #define DFL_RECONNECT 0 #define DFL_RECO_SERVER_NAME NULL #define DFL_RECO_DELAY 0 @@ -234,13 +233,6 @@ int main(void) #define USAGE_MAX_FRAG_LEN "" #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ -#if defined(MBEDTLS_DHM_C) -#define USAGE_DHMLEN \ - " dhmlen=%%d default: (library default: 1024 bits)\n" -#else -#define USAGE_DHMLEN -#endif - #if defined(MBEDTLS_SSL_ALPN) #define USAGE_ALPN \ " alpn=%%s default: \"\" (disabled)\n" \ @@ -433,7 +425,6 @@ int main(void) USAGE_GROUPS \ USAGE_SIG_ALGS \ USAGE_EARLY_DATA \ - USAGE_DHMLEN \ USAGE_KEY_OPAQUE_ALGS \ "\n" @@ -508,7 +499,6 @@ struct options { unsigned char mfl_code; /* code for maximum fragment length */ int trunc_hmac; /* negotiate truncated hmac or not */ int recsplit; /* enable record splitting? */ - int dhmlen; /* minimum DHM params len in bits */ int reconnect; /* attempt to resume session */ const char *reco_server_name; /* hostname of the server (re-connect) */ int reco_delay; /* delay in seconds before resuming session */ @@ -956,7 +946,6 @@ int main(int argc, char *argv[]) opt.mfl_code = DFL_MFL_CODE; opt.trunc_hmac = DFL_TRUNC_HMAC; opt.recsplit = DFL_RECSPLIT; - opt.dhmlen = DFL_DHMLEN; opt.reconnect = DFL_RECONNECT; opt.reco_server_name = DFL_RECO_SERVER_NAME; opt.reco_delay = DFL_RECO_DELAY; @@ -1388,11 +1377,6 @@ int main(int argc, char *argv[]) if (opt.recsplit < 0 || opt.recsplit > 1) { goto usage; } - } else if (strcmp(p, "dhmlen") == 0) { - opt.dhmlen = atoi(q); - if (opt.dhmlen < 0) { - goto usage; - } } else if (strcmp(p, "query_config") == 0) { opt.query_config_mode = 1; query_config_ret = query_config(q); @@ -1898,12 +1882,6 @@ int main(int argc, char *argv[]) } #endif -#if defined(MBEDTLS_DHM_C) - if (opt.dhmlen != DFL_DHMLEN) { - mbedtls_ssl_conf_dhm_min_bitlen(&conf, opt.dhmlen); - } -#endif - #if defined(MBEDTLS_SSL_ALPN) if (opt.alpn_string != NULL) { if ((ret = mbedtls_ssl_conf_alpn_protocols(&conf, alpn_list)) != 0) { diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c index d9e57018ae18..dc7ca8f51c6b 100644 --- a/programs/ssl/ssl_server2.c +++ b/programs/ssl/ssl_server2.c @@ -58,7 +58,6 @@ int main(void) #endif #include "mbedtls/pk.h" -#include "mbedtls/dhm.h" /* Size of memory to be allocated for the heap, when using the library's memory * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */ @@ -127,7 +126,6 @@ int main(void) #define DFL_EARLY_DATA -1 #define DFL_MAX_EARLY_DATA_SIZE ((uint32_t) -1) #define DFL_SIG_ALGS NULL -#define DFL_DHM_FILE NULL #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM #define DFL_COOKIES 1 #define DFL_ANTI_REPLAY -1 @@ -192,9 +190,7 @@ int main(void) " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \ " preloaded certificate(s) and key(s) are used if available\n" \ " key_pwd2=%%s Password for key specified by key_file2 argument\n" \ - " default: none\n" \ - " dhm_file=%%s File containing Diffie-Hellman parameters\n" \ - " default: preloaded parameters\n" + " default: none\n" #else #define USAGE_IO \ "\n" \ @@ -675,7 +671,6 @@ struct options { const char *groups; /* list of supported groups */ const char *sig_algs; /* supported TLS 1.3 signature algorithms */ const char *alpn_string; /* ALPN supported protocols */ - const char *dhm_file; /* the file with the DH parameters */ int extended_ms; /* allow negotiation of extended MS? */ int etm; /* allow negotiation of encrypt-then-MAC? */ int transport; /* TLS or DTLS? */ @@ -1590,9 +1585,6 @@ int main(int argc, char *argv[]) #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) ssl_async_key_context_t ssl_async_keys; #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */ -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) - mbedtls_dhm_context dhm; -#endif #if defined(MBEDTLS_SSL_CACHE_C) mbedtls_ssl_cache_context cache; #endif @@ -1681,9 +1673,6 @@ int main(int argc, char *argv[]) #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) memset(&ssl_async_keys, 0, sizeof(ssl_async_keys)); #endif -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) - mbedtls_dhm_init(&dhm); -#endif #if defined(MBEDTLS_SSL_CACHE_C) mbedtls_ssl_cache_init(&cache); #endif @@ -1793,7 +1782,6 @@ int main(int argc, char *argv[]) opt.max_early_data_size = DFL_MAX_EARLY_DATA_SIZE; #endif opt.sig_algs = DFL_SIG_ALGS; - opt.dhm_file = DFL_DHM_FILE; opt.transport = DFL_TRANSPORT; opt.cookies = DFL_COOKIES; opt.anti_replay = DFL_ANTI_REPLAY; @@ -1943,8 +1931,6 @@ int main(int argc, char *argv[]) opt.key_file2 = q; } else if (strcmp(p, "key_pwd2") == 0) { opt.key_pwd2 = q; - } else if (strcmp(p, "dhm_file") == 0) { - opt.dhm_file = q; } #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) else if (strcmp(p, "async_operations") == 0) { @@ -2787,21 +2773,6 @@ int main(int argc, char *argv[]) key_cert_init2 ? mbedtls_pk_get_name(&pkey2) : "none"); #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) - if (opt.dhm_file != NULL) { - mbedtls_printf(" . Loading DHM parameters..."); - fflush(stdout); - - if ((ret = mbedtls_dhm_parse_dhmfile(&dhm, opt.dhm_file)) != 0) { - mbedtls_printf(" failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n", - (unsigned int) -ret); - goto exit; - } - - mbedtls_printf(" ok\n"); - } -#endif - #if defined(SNI_OPTION) if (opt.sni != NULL) { mbedtls_printf(" . Setting up SNI information..."); @@ -3269,22 +3240,6 @@ int main(int argc, char *argv[]) } #endif -#if defined(MBEDTLS_DHM_C) - /* - * Use different group than default DHM group - */ -#if defined(MBEDTLS_FS_IO) - if (opt.dhm_file != NULL) { - ret = mbedtls_ssl_conf_dh_param_ctx(&conf, &dhm); - } -#endif - if (ret != 0) { - mbedtls_printf(" failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", - (unsigned int) -ret); - goto exit; - } -#endif - if (opt.min_version != DFL_MIN_VERSION) { mbedtls_ssl_conf_min_tls_version(&conf, opt.min_version); } @@ -4284,10 +4239,6 @@ int main(int argc, char *argv[]) #endif #endif -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO) - mbedtls_dhm_free(&dhm); -#endif - #if defined(MBEDTLS_SSL_ASYNC_PRIVATE) for (i = 0; (size_t) i < ssl_async_keys.slots_used; i++) { if (ssl_async_keys.slots[i].pk_owned) { From 052a77a8a76de77a3e987f309e37caa4da54b6a9 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 10:51:31 +0100 Subject: [PATCH 03/11] programs: remove DHM_C usage from benchmark and selftest Signed-off-by: Valerio Setti --- programs/test/benchmark.c | 69 ++------------------------------------- programs/test/selftest.c | 4 --- 2 files changed, 2 insertions(+), 71 deletions(-) diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c index c878e3426d75..5b414920a3a1 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -42,7 +42,6 @@ int main(void) #include "mbedtls/hmac_drbg.h" #include "mbedtls/rsa.h" -#include "mbedtls/dhm.h" #include "mbedtls/ecdsa.h" #include "mbedtls/ecdh.h" @@ -113,7 +112,7 @@ static unsigned long mbedtls_timing_hardclock(void); "aes_cbc, aes_cfb128, aes_cfb8, aes_gcm, aes_ccm, aes_xts, chachapoly\n" \ "aes_cmac, des3_cmac, poly1305\n" \ "ctr_drbg, hmac_drbg\n" \ - "rsa, dhm, ecdsa, ecdh.\n" + "rsa, ecdsa, ecdh.\n" #if defined(MBEDTLS_ERROR_C) #define PRINT_ERROR \ @@ -511,7 +510,7 @@ typedef struct { aria, camellia, chacha20, poly1305, ctr_drbg, hmac_drbg, - rsa, dhm, ecdsa, ecdh; + rsa, ecdsa, ecdh; } todo_list; @@ -598,8 +597,6 @@ int main(int argc, char *argv[]) todo.hmac_drbg = 1; } else if (strcmp(argv[i], "rsa") == 0) { todo.rsa = 1; - } else if (strcmp(argv[i], "dhm") == 0) { - todo.dhm = 1; } else if (strcmp(argv[i], "ecdsa") == 0) { todo.ecdsa = 1; } else if (strcmp(argv[i], "ecdh") == 0) { @@ -1065,68 +1062,6 @@ int main(int argc, char *argv[]) } #endif -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_BIGNUM_C) - if (todo.dhm) { - int dhm_sizes[] = { 2048, 3072 }; - static const unsigned char dhm_P_2048[] = - MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; - static const unsigned char dhm_P_3072[] = - MBEDTLS_DHM_RFC3526_MODP_3072_P_BIN; - static const unsigned char dhm_G_2048[] = - MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; - static const unsigned char dhm_G_3072[] = - MBEDTLS_DHM_RFC3526_MODP_3072_G_BIN; - - const unsigned char *dhm_P[] = { dhm_P_2048, dhm_P_3072 }; - const size_t dhm_P_size[] = { sizeof(dhm_P_2048), - sizeof(dhm_P_3072) }; - - const unsigned char *dhm_G[] = { dhm_G_2048, dhm_G_3072 }; - const size_t dhm_G_size[] = { sizeof(dhm_G_2048), - sizeof(dhm_G_3072) }; - - mbedtls_dhm_context dhm; - size_t olen; - size_t n; - mbedtls_mpi P, G; - mbedtls_mpi_init(&P); mbedtls_mpi_init(&G); - - for (i = 0; (size_t) i < sizeof(dhm_sizes) / sizeof(dhm_sizes[0]); i++) { - mbedtls_dhm_init(&dhm); - - if (mbedtls_mpi_read_binary(&P, dhm_P[i], - dhm_P_size[i]) != 0 || - mbedtls_mpi_read_binary(&G, dhm_G[i], - dhm_G_size[i]) != 0 || - mbedtls_dhm_set_group(&dhm, &P, &G) != 0) { - mbedtls_exit(1); - } - - n = mbedtls_dhm_get_len(&dhm); - mbedtls_dhm_make_public(&dhm, (int) n, buf, n, myrand, NULL); - - if (mbedtls_dhm_read_public(&dhm, buf, n) != 0) { - mbedtls_exit(1); - } - - mbedtls_snprintf(title, sizeof(title), "DHE-%d", dhm_sizes[i]); - TIME_PUBLIC(title, "handshake", - ret |= mbedtls_dhm_make_public(&dhm, (int) n, buf, n, - myrand, NULL); - ret |= - mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL)); - - mbedtls_snprintf(title, sizeof(title), "DH-%d", dhm_sizes[i]); - TIME_PUBLIC(title, "handshake", - ret |= - mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &olen, myrand, NULL)); - - mbedtls_dhm_free(&dhm); - mbedtls_mpi_free(&P), mbedtls_mpi_free(&G); - } - } -#endif - #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_SHA256_C) if (todo.ecdsa) { mbedtls_ecdsa_context ecdsa; diff --git a/programs/test/selftest.c b/programs/test/selftest.c index e72386f02353..41252b6e4c56 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -10,7 +10,6 @@ #include "mbedtls/entropy.h" #include "mbedtls/hmac_drbg.h" #include "mbedtls/ctr_drbg.h" -#include "mbedtls/dhm.h" #include "mbedtls/gcm.h" #include "mbedtls/ccm.h" #include "mbedtls/cmac.h" @@ -350,9 +349,6 @@ const selftest_t selftests[] = #if defined(MBEDTLS_ECJPAKE_C) { "ecjpake", mbedtls_ecjpake_self_test }, #endif -#if defined(MBEDTLS_DHM_C) - { "dhm", mbedtls_dhm_self_test }, -#endif #if defined(MBEDTLS_ENTROPY_C) { "entropy", mbedtls_entropy_self_test_wrapper }, #endif From 0e51c54be04cfd4e8a7de242ed26433b00e2ebc0 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 10:54:02 +0100 Subject: [PATCH 04/11] scripts: query_config.fmt: do not include "dhm.h" The file is being removed together with the removal of MBEDTLS_DHM_C. Signed-off-by: Valerio Setti --- scripts/data_files/query_config.fmt | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/data_files/query_config.fmt b/scripts/data_files/query_config.fmt index b60aba010d54..9be9674c1d2e 100644 --- a/scripts/data_files/query_config.fmt +++ b/scripts/data_files/query_config.fmt @@ -34,7 +34,6 @@ #include "mbedtls/ctr_drbg.h" #include "mbedtls/debug.h" #include "mbedtls/des.h" -#include "mbedtls/dhm.h" #include "mbedtls/ecdh.h" #include "mbedtls/ecdsa.h" #include "mbedtls/ecjpake.h" From 672a9274f1e611f6be828af71535580659f4107a Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 13:32:49 +0100 Subject: [PATCH 05/11] tests: remove MBEDTLS_DHM_C/DHM occurrencies Signed-off-by: Valerio Setti --- tests/include/test/certs.h | 2 +- .../components-configuration-crypto.sh | 20 +++---------------- tests/scripts/components-configuration-tls.sh | 1 - tests/scripts/set_psa_test_dependencies.py | 1 - 4 files changed, 4 insertions(+), 20 deletions(-) diff --git a/tests/include/test/certs.h b/tests/include/test/certs.h index db69536a6f29..31f4477c2b54 100644 --- a/tests/include/test/certs.h +++ b/tests/include/test/certs.h @@ -1,7 +1,7 @@ /** * \file certs.h * - * \brief Sample certificates and DHM parameters for testing + * \brief Sample certificates for testing */ /* * Copyright The Mbed TLS Contributors diff --git a/tests/scripts/components-configuration-crypto.sh b/tests/scripts/components-configuration-crypto.sh index 34b310781548..9337d03dfafa 100644 --- a/tests/scripts/components-configuration-crypto.sh +++ b/tests/scripts/components-configuration-crypto.sh @@ -668,9 +668,6 @@ component_test_psa_crypto_config_accel_ffdh () { # start with full (USE_PSA and TLS 1.3) helper_libtestdriver1_adjust_config "full" - # Disable the module that's accelerated - scripts/config.py unset MBEDTLS_DHM_C - # Build # ----- @@ -678,9 +675,6 @@ component_test_psa_crypto_config_accel_ffdh () { helper_libtestdriver1_make_main "$loc_accel_list" - # Make sure this was not re-enabled by accident (additive config) - not grep mbedtls_dhm_ ${BUILTIN_SRC_PATH}/dhm.o - # Run the tests # ------------- @@ -1178,12 +1172,6 @@ config_psa_crypto_config_accel_ecc_ffdh_no_bignum () { scripts/config.py -f "$CRYPTO_CONFIG_H" unset PSA_WANT_ALG_FFDH scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_KEY_TYPE_DH_[0-9A-Z_a-z]*" scripts/config.py -f "$CRYPTO_CONFIG_H" unset-all "PSA_WANT_DH_RFC7919_[0-9]*" - scripts/config.py unset MBEDTLS_DHM_C - else - # When testing ECC and DH instead, we disable DHM. - if [ "$driver_only" -eq 1 ]; then - scripts/config.py unset MBEDTLS_DHM_C - fi fi # Restartable feature is not yet supported by PSA. Once it will in @@ -1255,16 +1243,15 @@ common_test_psa_crypto_config_accel_ecc_ffdh_no_bignum () { not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o not grep mbedtls_ecjpake_ ${BUILTIN_SRC_PATH}/ecjpake.o - # Also ensure that ECP, RSA, [DHM] or BIGNUM modules were not re-enabled + # Also ensure that ECP, RSA or BIGNUM modules were not re-enabled not grep mbedtls_ecp_ ${BUILTIN_SRC_PATH}/ecp.o not grep mbedtls_rsa_ ${BUILTIN_SRC_PATH}/rsa.o not grep mbedtls_mpi_ ${BUILTIN_SRC_PATH}/bignum.o - not grep mbedtls_dhm_ ${BUILTIN_SRC_PATH}/dhm.o # Run the tests # ------------- - msg "test suites: full + accelerated $accel_text algs + USE_PSA - $removed_text - DHM - BIGNUM" + msg "test suites: full + accelerated $accel_text algs + USE_PSA - $removed_text - BIGNUM" make test @@ -1362,10 +1349,9 @@ component_test_tfm_config_p256m_driver_accel_ec () { not grep mbedtls_ecdsa_ ${BUILTIN_SRC_PATH}/ecdsa.o not grep mbedtls_ecdh_ ${BUILTIN_SRC_PATH}/ecdh.o not grep mbedtls_ecjpake_ ${BUILTIN_SRC_PATH}/ecjpake.o - # Also ensure that ECP, RSA, DHM or BIGNUM modules were not re-enabled + # Also ensure that ECP, RSA or BIGNUM modules were not re-enabled not grep mbedtls_ecp_ ${BUILTIN_SRC_PATH}/ecp.o not grep mbedtls_rsa_ ${BUILTIN_SRC_PATH}/rsa.o - not grep mbedtls_dhm_ ${BUILTIN_SRC_PATH}/dhm.o not grep mbedtls_mpi_ ${BUILTIN_SRC_PATH}/bignum.o # Check that p256m was built grep -q p256_ecdsa_ library/libmbedcrypto.a diff --git a/tests/scripts/components-configuration-tls.sh b/tests/scripts/components-configuration-tls.sh index 83795012f3b2..917ceefaa921 100644 --- a/tests/scripts/components-configuration-tls.sh +++ b/tests/scripts/components-configuration-tls.sh @@ -469,7 +469,6 @@ component_test_tls13_only_psk () { scripts/config.py unset MBEDTLS_ECDH_C scripts/config.py unset MBEDTLS_ECDSA_C scripts/config.py unset MBEDTLS_PKCS1_V21 - scripts/config.py unset MBEDTLS_DHM_C make CFLAGS="'-DMBEDTLS_USER_CONFIG_FILE=\"../tests/configs/tls13-only.h\"'" diff --git a/tests/scripts/set_psa_test_dependencies.py b/tests/scripts/set_psa_test_dependencies.py index f68dfcb72b11..2267311e44c6 100755 --- a/tests/scripts/set_psa_test_dependencies.py +++ b/tests/scripts/set_psa_test_dependencies.py @@ -58,7 +58,6 @@ 'MBEDTLS_CMAC_C', 'MBEDTLS_CTR_DRBG_C', 'MBEDTLS_DES_C', - 'MBEDTLS_DHM_C', 'MBEDTLS_ECDH_C', 'MBEDTLS_ECDSA_C', 'MBEDTLS_ECJPAKE_C', From 4751261d810e7fb1b5da7c79b1d8e2a430e49c93 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 13:34:25 +0100 Subject: [PATCH 06/11] analyze_outcomes.py: remove exceptions for MBEDTLS_DHM_C Signed-off-by: Valerio Setti --- tests/scripts/analyze_outcomes.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/scripts/analyze_outcomes.py b/tests/scripts/analyze_outcomes.py index e68c2cbf09f3..5f8f910a62b4 100755 --- a/tests/scripts/analyze_outcomes.py +++ b/tests/scripts/analyze_outcomes.py @@ -474,7 +474,7 @@ class DriverVSReference_ecc_ffdh_no_bignum(outcome_analysis.DriverVSReference): DRIVER = 'test_psa_crypto_config_accel_ecc_ffdh_no_bignum' IGNORED_SUITES = [ # Modules replaced by drivers - 'ecp', 'ecdsa', 'ecdh', 'ecjpake', 'dhm', + 'ecp', 'ecdsa', 'ecdh', 'ecjpake', 'bignum_core', 'bignum_random', 'bignum_mod', 'bignum_mod_raw', 'bignum.generated', 'bignum.misc', # Unit tests for the built-in implementation @@ -483,7 +483,6 @@ class DriverVSReference_ecc_ffdh_no_bignum(outcome_analysis.DriverVSReference): IGNORED_TESTS = { 'test_suite_config': [ re.compile(r'.*\bMBEDTLS_BIGNUM_C\b.*'), - re.compile(r'.*\bMBEDTLS_DHM_C\b.*'), re.compile(r'.*\bMBEDTLS_(ECDH|ECDSA|ECJPAKE|ECP)_.*'), re.compile(r'.*\bMBEDTLS_PK_PARSE_EC_COMPRESSED\b.*'), ], @@ -516,11 +515,7 @@ class DriverVSReference_ecc_ffdh_no_bignum(outcome_analysis.DriverVSReference): class DriverVSReference_ffdh_alg(outcome_analysis.DriverVSReference): REFERENCE = 'test_psa_crypto_config_reference_ffdh' DRIVER = 'test_psa_crypto_config_accel_ffdh' - IGNORED_SUITES = ['dhm'] IGNORED_TESTS = { - 'test_suite_config': [ - re.compile(r'.*\bMBEDTLS_DHM_C\b.*'), - ], 'test_suite_platform': [ # Incompatible with sanitizers (e.g. ASan). If the driver # component uses a sanitizer but the reference component From 06728dbc9d3dfbe4dbb01a7968a43e8167cae335 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 13:38:24 +0100 Subject: [PATCH 07/11] ssl: remove support for MBEDTLS_DHM_C Signed-off-by: Valerio Setti --- include/mbedtls/ssl.h | 56 ---------------------------- library/ssl_misc.h | 4 -- library/ssl_tls.c | 86 ------------------------------------------- 3 files changed, 146 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index e0c0eae4e296..958ee9bce71b 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -24,10 +24,6 @@ #include "mbedtls/x509_crl.h" #endif -#if defined(MBEDTLS_DHM_C) -#include "mbedtls/dhm.h" -#endif - #include "mbedtls/md.h" #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) @@ -1562,11 +1558,6 @@ struct mbedtls_ssl_config { const uint16_t *MBEDTLS_PRIVATE(group_list); /*!< allowed IANA NamedGroups */ -#if defined(MBEDTLS_DHM_C) - mbedtls_mpi MBEDTLS_PRIVATE(dhm_P); /*!< prime modulus for DHM */ - mbedtls_mpi MBEDTLS_PRIVATE(dhm_G); /*!< generator for DHM */ -#endif - #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) mbedtls_svc_key_id_t MBEDTLS_PRIVATE(psk_opaque); /*!< PSA key slot holding opaque PSK. This field @@ -1642,10 +1633,6 @@ struct mbedtls_ssl_config { unsigned int MBEDTLS_PRIVATE(badmac_limit); /*!< limit of records with a bad MAC */ -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) - unsigned int MBEDTLS_PRIVATE(dhm_min_bitlen); /*!< min. bit length of the DHM prime */ -#endif - /** User data pointer or handle. * * The library sets this to \p 0 when creating a context and does not @@ -3753,49 +3740,6 @@ void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf, #endif /* MBEDTLS_SSL_SRV_C */ #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) -/** - * \brief Set the Diffie-Hellman public P and G values - * from big-endian binary presentations. - * (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG]_BIN) - * - * \param conf SSL configuration - * \param dhm_P Diffie-Hellman-Merkle modulus in big-endian binary form - * \param P_len Length of DHM modulus - * \param dhm_G Diffie-Hellman-Merkle generator in big-endian binary form - * \param G_len Length of DHM generator - * - * \return 0 if successful - */ -int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, - const unsigned char *dhm_P, size_t P_len, - const unsigned char *dhm_G, size_t G_len); - -/** - * \brief Set the Diffie-Hellman public P and G values, - * read from existing context (server-side only) - * - * \param conf SSL configuration - * \param dhm_ctx Diffie-Hellman-Merkle context - * - * \return 0 if successful - */ -int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx); -#endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */ - -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) -/** - * \brief Set the minimum length for Diffie-Hellman parameters. - * (Client-side only.) - * (Default: 1024 bits.) - * - * \param conf SSL configuration - * \param bitlen Minimum bit length of the DHM prime - */ -void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, - unsigned int bitlen); -#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ - /** * \brief Set the allowed groups in order of preference. * diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 9f91861f64f8..9ff0fcaf7580 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -763,10 +763,6 @@ struct mbedtls_ssl_handshake_params { const uint16_t *sig_algs; #endif -#if defined(MBEDTLS_DHM_C) - mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */ -#endif - #if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED) psa_key_type_t xxdh_psa_type; size_t xxdh_psa_bits; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 60f2e1cd6d04..ec4272a05f81 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -911,9 +911,6 @@ static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake) handshake->update_checksum = ssl_update_checksum_start; -#if defined(MBEDTLS_DHM_C) - mbedtls_dhm_init(&handshake->dhm_ctx); -#endif #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) handshake->psa_pake_ctx = psa_pake_operation_init(); handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT; @@ -2431,57 +2428,6 @@ psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type return PSA_SUCCESS; } -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) -int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf, - const unsigned char *dhm_P, size_t P_len, - const unsigned char *dhm_G, size_t G_len) -{ - int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - - mbedtls_mpi_free(&conf->dhm_P); - mbedtls_mpi_free(&conf->dhm_G); - - if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 || - (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) { - mbedtls_mpi_free(&conf->dhm_P); - mbedtls_mpi_free(&conf->dhm_G); - return ret; - } - - return 0; -} - -int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx) -{ - int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; - - mbedtls_mpi_free(&conf->dhm_P); - mbedtls_mpi_free(&conf->dhm_G); - - if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P, - &conf->dhm_P)) != 0 || - (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G, - &conf->dhm_G)) != 0) { - mbedtls_mpi_free(&conf->dhm_P); - mbedtls_mpi_free(&conf->dhm_G); - return ret; - } - - return 0; -} -#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */ - -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) -/* - * Set the minimum length for Diffie-Hellman parameters - */ -void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf, - unsigned int bitlen) -{ - conf->dhm_min_bitlen = bitlen; -} -#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */ - #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) #if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2) /* @@ -4537,10 +4483,6 @@ void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl) psa_hash_abort(&handshake->fin_sha384_psa); #endif -#if defined(MBEDTLS_DHM_C) - mbedtls_dhm_free(&handshake->dhm_ctx); -#endif - #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) psa_pake_abort(&handshake->psa_pake_ctx); /* @@ -5551,10 +5493,6 @@ static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs) int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, int endpoint, int transport, int preset) { -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) - int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; -#endif - #if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) { mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n"); @@ -5629,21 +5567,6 @@ int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, memset(conf->renego_period + 2, 0xFF, 6); #endif -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C) - if (endpoint == MBEDTLS_SSL_IS_SERVER) { - const unsigned char dhm_p[] = - MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN; - const unsigned char dhm_g[] = - MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN; - - if ((ret = mbedtls_ssl_conf_dh_param_bin(conf, - dhm_p, sizeof(dhm_p), - dhm_g, sizeof(dhm_g))) != 0) { - return ret; - } - } -#endif - #if defined(MBEDTLS_SSL_PROTO_TLS1_3) #if defined(MBEDTLS_SSL_EARLY_DATA) @@ -5733,10 +5656,6 @@ int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf, #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ conf->group_list = ssl_preset_default_groups; - -#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C) - conf->dhm_min_bitlen = 1024; -#endif } return 0; @@ -5751,11 +5670,6 @@ void mbedtls_ssl_config_free(mbedtls_ssl_config *conf) return; } -#if defined(MBEDTLS_DHM_C) - mbedtls_mpi_free(&conf->dhm_P); - mbedtls_mpi_free(&conf->dhm_G); -#endif - #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) { conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT; From 5830be801dad680bc089b81408695d8294c9c371 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 13:49:32 +0100 Subject: [PATCH 08/11] scripts: generate_errors: remove DHM occurrence Signed-off-by: Valerio Setti --- scripts/generate_errors.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl index c05184227c1b..aae1fc8870bc 100755 --- a/scripts/generate_errors.pl +++ b/scripts/generate_errors.pl @@ -40,7 +40,7 @@ ENTROPY ERROR GCM HKDF HMAC_DRBG LMS MD5 NET OID PBKDF2 PLATFORM POLY1305 RIPEMD160 SHA1 SHA256 SHA512 SHA3 THREADING ); -my @high_level_modules = qw( CIPHER DHM ECP MD +my @high_level_modules = qw( CIPHER ECP MD PEM PK PKCS12 PKCS5 RSA SSL X509 PKCS7 ); From 2d343cef006b3a2c3644ed9d0a8a64fcf3938fc9 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 14:33:59 +0100 Subject: [PATCH 09/11] library: do not include dhm.c in the build The file was cancelled from the tf-psa-crypto repo following the removal of MBEDTLS_DHM_C. Signed-off-by: Valerio Setti --- library/Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/library/Makefile b/library/Makefile index b874acf27af4..61b2623e2a2a 100644 --- a/library/Makefile +++ b/library/Makefile @@ -139,7 +139,6 @@ OBJS_CRYPTO= \ $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/constant_time.o \ $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/ctr_drbg.o \ $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/des.o \ - $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/dhm.o \ $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/ecdh.o \ $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/ecdsa.o \ $(TF_PSA_CRYPTO_DRIVERS_BUILTIN_SRC_PATH)/ecjpake.o \ From c003d94607f2c72bd8b174f1be08fa01425ec531 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 14:34:54 +0100 Subject: [PATCH 10/11] docs: remove references to DHM Signed-off-by: Valerio Setti --- doxygen/input/doc_encdec.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/doxygen/input/doc_encdec.h b/doxygen/input/doc_encdec.h index cf77690b36bd..068e716bf40a 100644 --- a/doxygen/input/doc_encdec.h +++ b/doxygen/input/doc_encdec.h @@ -39,8 +39,6 @@ * and \c mbedtls_des3_crypt_cbc()). * - GCM (AES-GCM and CAMELLIA-GCM) (see \c mbedtls_gcm_init()) * - Asymmetric: - * - Diffie-Hellman-Merkle (see \c mbedtls_dhm_read_public(), \c mbedtls_dhm_make_public() - * and \c mbedtls_dhm_calc_secret()). * - RSA (see \c mbedtls_rsa_public() and \c mbedtls_rsa_private()). * - Elliptic Curves over GF(p) (see \c mbedtls_ecp_point_init()). * - Elliptic Curve Digital Signature Algorithm (ECDSA) (see \c mbedtls_ecdsa_init()). From 58a00851b6687884e568a632f65e6ae1327c0942 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Wed, 12 Feb 2025 15:53:19 +0100 Subject: [PATCH 11/11] tf-psa-crypto: udpate reference Signed-off-by: Valerio Setti --- tf-psa-crypto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tf-psa-crypto b/tf-psa-crypto index dcbe6fc1da16..b1706aa10000 160000 --- a/tf-psa-crypto +++ b/tf-psa-crypto @@ -1 +1 @@ -Subproject commit dcbe6fc1da160e17ffa6ad8d2f503e13d7f505ff +Subproject commit b1706aa10000de417838aefc025ca3dffcfa4609