Implement byte reading macros into library/

To improve readability by saving horizontal and vertical space.
Removed unecessary & 0xFF.
Byte reading macros implemented in library/common.h, All files
containing "& 0xff" were modified.
Comments/Documentation not yet added to the macro definitions.

Fixes #4274

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
This commit is contained in:
Joe Subbiani 2021-06-22 15:51:53 +01:00
parent 01a78599b0
commit 50dde56543
7 changed files with 34 additions and 24 deletions

View file

@ -66,4 +66,14 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c
*/
#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 */

View file

@ -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 );

View file

@ -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 ) );
}
}

View file

@ -4506,8 +4506,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];

View file

@ -191,14 +191,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" );

View file

@ -2254,14 +2254,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 );

View file

@ -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,