diff --git a/library/common.h b/library/common.h index 5845766ac..6a9f259d3 100644 --- a/library/common.h +++ b/library/common.h @@ -50,4 +50,21 @@ #define MBEDTLS_STATIC_TESTABLE static #endif +/** Allow library to access its structs' private members. + * + * Although structs defined in header files are publicly available, + * their members are private and should not be accessed by the user. + */ +#define MBEDTLS_ALLOW_PRIVATE_ACCESS + +/** Byte Reading Macros + * + * To tidy up code and save horizontal and vertical space, use byte + * reading macros to cast + */ +#define BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) +#define BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) +#define BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) +#define BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) + #endif /* MBEDTLS_LIBRARY_COMMON_H */ diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index ab52861d5..2d83c6c10 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -152,10 +152,10 @@ static int block_cipher_df( unsigned char *output, * (Total is padded to a multiple of 16-bytes with zeroes) */ p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; - *p++ = ( data_len >> 24 ) & 0xff; - *p++ = ( data_len >> 16 ) & 0xff; - *p++ = ( data_len >> 8 ) & 0xff; - *p++ = ( data_len ) & 0xff; + *p++ = BYTE_3( data_len ); + *p++ = BYTE_2( data_len ); + *p++ = BYTE_1( data_len ); + *p++ = BYTE_0( data_len ); p += 3; *p++ = MBEDTLS_CTR_DRBG_SEEDLEN; memcpy( p, data, data_len ); diff --git a/library/nist_kw.c b/library/nist_kw.c index 5054ca206..3fff2b7f8 100644 --- a/library/nist_kw.c +++ b/library/nist_kw.c @@ -169,7 +169,7 @@ static void calc_a_xor_t( unsigned char A[KW_SEMIBLOCK_LENGTH], uint64_t t ) size_t i = 0; for( i = 0; i < sizeof( t ); i++ ) { - A[i] ^= ( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ) & 0xff; + A[i] ^= BYTE_0( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ); } } diff --git a/library/psa_crypto.c b/library/psa_crypto.c index a424c8959..b275e58e4 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -4570,8 +4570,8 @@ static psa_status_t psa_tls12_prf_psk_to_ms_set_key( * uint16 with the value N, and the PSK itself. */ - *cur++ = ( data_length >> 8 ) & 0xff; - *cur++ = ( data_length >> 0 ) & 0xff; + *cur++ = BYTE_1( data_length ); + *cur++ = BYTE_0( data_length ); memset( cur, 0, data_length ); cur += data_length; *cur++ = pms[0]; diff --git a/library/psa_its_file.c b/library/psa_its_file.c index 7798da615..1a2d2a9af 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -195,14 +195,14 @@ psa_status_t psa_its_set( psa_storage_uid_t uid, size_t n; memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); - header.size[0] = data_length & 0xff; - header.size[1] = ( data_length >> 8 ) & 0xff; - header.size[2] = ( data_length >> 16 ) & 0xff; - header.size[3] = ( data_length >> 24 ) & 0xff; - header.flags[0] = create_flags & 0xff; - header.flags[1] = ( create_flags >> 8 ) & 0xff; - header.flags[2] = ( create_flags >> 16 ) & 0xff; - header.flags[3] = ( create_flags >> 24 ) & 0xff; + header.size[0] = BYTE_0( data_length ); + header.size[1] = BYTE_1( data_length ); + header.size[2] = BYTE_2( data_length ); + header.size[3] = BYTE_3( data_length ); + header.flags[0] = BYTE_0( create_flags ); + header.flags[1] = BYTE_1( create_flags ); + header.flags[2] = BYTE_2( create_flags ); + header.flags[3] = BYTE_3( create_flags ); psa_its_fill_filename( uid, filename ); stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); diff --git a/library/ssl_msg.c b/library/ssl_msg.c index 1352b4943..34db76880 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -2525,14 +2525,14 @@ int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) * copy beginning of headers then fill fragmentation fields. * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ memcpy( ssl->out_msg, cur->p, 6 ); + + ssl->out_msg[6] = BYTE_2( frag_off ); + ssl->out_msg[7] = BYTE_1( frag_off ); + ssl->out_msg[8] = BYTE_0( frag_off ); - ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); - ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); - ssl->out_msg[8] = ( ( frag_off ) & 0xff ); - - ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); - ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); - ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); + ssl->out_msg[ 9] = BYTE_2( cur_hs_frag_len ); + ssl->out_msg[10] = BYTE_1( cur_hs_frag_len ); + ssl->out_msg[11] = BYTE_0( cur_hs_frag_len ); MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); diff --git a/library/ssl_ticket.c b/library/ssl_ticket.c index 626d137cc..b3b8e4c8b 100644 --- a/library/ssl_ticket.c +++ b/library/ssl_ticket.c @@ -245,8 +245,8 @@ int mbedtls_ssl_ticket_write( void *p_ticket, { goto cleanup; } - state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; - state_len_bytes[1] = ( clear_len ) & 0xff; + state_len_bytes[0] = BYTE_1( clear_len ); + state_len_bytes[1] = BYTE_0( clear_len ); /* Encrypt and authenticate */ if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx,