From d3a3f21ad5affee01dea1a5401d5f42648775985 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 21 Jul 2021 15:22:47 +0100 Subject: [PATCH] Improve documentation and add more uses of MBEDTLS_PUT minor changes, such as improving the documentation for the byte reading macros, and using MBEDTLS_PUT_UINT16_xy in place of byte reading macro combinations Signed-off-by: Joe Subbiani --- library/ccm.c | 3 +-- library/common.h | 5 ++--- library/ecp.c | 3 +-- library/ssl_cli.c | 2 +- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/library/ccm.c b/library/ccm.c index 0188075f5..a21a37f55 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -221,8 +221,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, src = add; memset( b, 0, 16 ); - b[0] = MBEDTLS_BYTE_1( add_len ); - b[1] = MBEDTLS_BYTE_0( add_len ); + MBEDTLS_PUT_UINT16_BE( add_len, b, 0 ); use_len = len_left < 16 - 2 ? len_left : 16 - 2; memcpy( b + 2, src, use_len ); diff --git a/library/common.h b/library/common.h index fb4194e27..c35bc0426 100644 --- a/library/common.h +++ b/library/common.h @@ -61,9 +61,8 @@ /** Byte Reading Macros * - * Obtain the most significant byte of x using 0xff - * Using MBEDTLS_BYTE_a will shift a*8 bits - * to retrieve the next byte of information + * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th + * byte from x, where byte 0 is the least significant byte. */ #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) diff --git a/library/ecp.c b/library/ecp.c index cc8a26cee..7f9e1045d 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1160,8 +1160,7 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, /* * Next two bytes are the namedcurve value */ - buf[0] = MBEDTLS_BYTE_1( curve_info->tls_id ); - buf[1] = MBEDTLS_BYTE_0( curve_info->tls_id ); + MBEDTLS_PUT_UINT16_BE( curve_info->tls_id, buf, 0 ); return( 0 ); } diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 3cdfca67b..e37e63ddb 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -794,7 +794,7 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_USE_SRTP ); - *p++ = MBEDTLS_BYTE_1( ext_len & 0xFF00 ); + *p++ = MBEDTLS_BYTE_1( ext_len ); *p++ = MBEDTLS_BYTE_0( ext_len ); /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */