From 797e396fd94305e31acb964af88c0dc04dc10b50 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Thu, 27 Jul 2023 16:19:00 +0200 Subject: [PATCH 1/3] debug: use proper guard for PK_DEBUG_MPI items Signed-off-by: Valerio Setti --- library/debug.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/debug.c b/library/debug.c index 0f0292915f..973516bd43 100644 --- a/library/debug.c +++ b/library/debug.c @@ -324,15 +324,17 @@ static void debug_print_pk(const mbedtls_ssl_context *ssl, int level, mbedtls_snprintf(name, sizeof(name), "%s%s", text, items[i].name); name[sizeof(name) - 1] = '\0'; +#if defined(MBEDTLS_RSA_C) if (items[i].type == MBEDTLS_PK_DEBUG_MPI) { mbedtls_debug_print_mpi(ssl, level, file, line, name, items[i].value); } else +#endif /* MBEDTLS_RSA_C */ #if defined(MBEDTLS_ECP_LIGHT) if (items[i].type == MBEDTLS_PK_DEBUG_ECP) { mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value); } else -#endif -#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) +#endif /* MBEDTLS_ECP_LIGHT */ +#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_BIGNUM_C) if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) { mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value); } else From c1319f4a26b7113e14921eee42c0b85cfffb1dda Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Thu, 27 Jul 2023 16:20:07 +0200 Subject: [PATCH 2/3] debug: improve debug_print_psa_ec() as not to depend on bignum Signed-off-by: Valerio Setti --- library/debug.c | 75 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/library/debug.c b/library/debug.c index 973516bd43..92fe903de2 100644 --- a/library/debug.c +++ b/library/debug.c @@ -202,17 +202,55 @@ void mbedtls_debug_print_ecp(const mbedtls_ssl_context *ssl, int level, } #endif /* MBEDTLS_ECP_LIGHT */ -#if defined(MBEDTLS_BIGNUM_C) #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) +static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int level, + const char *file, int line, const char *text, + const unsigned char *buf, size_t len) +{ + char str[DEBUG_BUF_SIZE]; + size_t i, idx = 0; + + mbedtls_snprintf(str + idx, sizeof(str) - idx, "value of '%s' (%u bits) is:\n", + text, (unsigned int) len * 8); + + debug_send_line(ssl, level, file, line, str); + + idx = 0; + for (i = 0; i < len; i++) { + if (i >= 4096) { + break; + } + + if (i % 16 == 0) { + if (i > 0) { + mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); + debug_send_line(ssl, level, file, line, str); + + idx = 0; + } + } + + idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " %02x", + (unsigned int) buf[i]); + } + + if (len > 0) { + for (/* i = i */; i % 16 != 0; i++) { + idx += mbedtls_snprintf(str + idx, sizeof(str) - idx, " "); + } + + mbedtls_snprintf(str + idx, sizeof(str) - idx, "\n"); + debug_send_line(ssl, level, file, line, str); + } +} + void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, const mbedtls_pk_context *pk) { char str[DEBUG_BUF_SIZE]; - mbedtls_mpi mpi; - const uint8_t *mpi_start; - size_t mpi_len; - int ret; + const uint8_t *coord_start; + size_t coord_len; if (NULL == ssl || NULL == ssl->conf || @@ -223,32 +261,21 @@ void mbedtls_debug_print_psa_ec(const mbedtls_ssl_context *ssl, int level, /* For the description of pk->pk_raw content please refer to the description * psa_export_public_key() function. */ - mpi_len = (pk->pub_raw_len - 1)/2; + coord_len = (pk->pub_raw_len - 1)/2; /* X coordinate */ - mbedtls_mpi_init(&mpi); - mpi_start = pk->pub_raw + 1; - ret = mbedtls_mpi_read_binary(&mpi, mpi_start, mpi_len); - if (ret != 0) { - return; - } + coord_start = pk->pub_raw + 1; mbedtls_snprintf(str, sizeof(str), "%s(X)", text); - mbedtls_debug_print_mpi(ssl, level, file, line, str, &mpi); - mbedtls_mpi_free(&mpi); + mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len); /* Y coordinate */ - mbedtls_mpi_init(&mpi); - mpi_start = mpi_start + mpi_len; - ret = mbedtls_mpi_read_binary(&mpi, mpi_start, mpi_len); - if (ret != 0) { - return; - } + coord_start = coord_start + coord_len; mbedtls_snprintf(str, sizeof(str), "%s(Y)", text); - mbedtls_debug_print_mpi(ssl, level, file, line, str, &mpi); - mbedtls_mpi_free(&mpi); + mbedtls_debug_print_ec_coord(ssl, level, file, line, str, coord_start, coord_len); } #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ +#if defined(MBEDTLS_BIGNUM_C) void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level, const char *file, int line, const char *text, const mbedtls_mpi *X) @@ -334,11 +361,11 @@ static void debug_print_pk(const mbedtls_ssl_context *ssl, int level, mbedtls_debug_print_ecp(ssl, level, file, line, name, items[i].value); } else #endif /* MBEDTLS_ECP_LIGHT */ -#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) && defined(MBEDTLS_BIGNUM_C) +#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) if (items[i].type == MBEDTLS_PK_DEBUG_PSA_EC) { mbedtls_debug_print_psa_ec(ssl, level, file, line, name, items[i].value); } else -#endif +#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ { debug_send_line(ssl, level, file, line, "should not happen\n"); } } From ca4c15dd25394dd7d961b41c03f7212f14024299 Mon Sep 17 00:00:00 2001 From: Valerio Setti Date: Thu, 10 Aug 2023 06:41:31 +0200 Subject: [PATCH 3/3] debug: remove redundant variable assignments Signed-off-by: Valerio Setti --- library/debug.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/debug.c b/library/debug.c index 92fe903de2..c392f63081 100644 --- a/library/debug.c +++ b/library/debug.c @@ -144,7 +144,6 @@ void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level, debug_send_line(ssl, level, file, line, str); - idx = 0; memset(txt, 0, sizeof(txt)); for (i = 0; i < len; i++) { if (i >= 4096) { @@ -215,7 +214,6 @@ static void mbedtls_debug_print_ec_coord(const mbedtls_ssl_context *ssl, int lev debug_send_line(ssl, level, file, line, str); - idx = 0; for (i = 0; i < len; i++) { if (i >= 4096) { break;