cmac: fix whitespace/codingstyle issues

This commit is contained in:
Manuel Pégourié-Gonnard 2016-01-13 11:00:47 +00:00 committed by Simon Butcher
parent 690083c21d
commit 3da5402a89

View File

@ -54,7 +54,7 @@
/* /*
* XOR 128-bit * XOR 128-bit
*/ */
#define XOR_128(i1, i2, o) \ #define XOR_128( i1, i2, o ) \
for( i = 0; i < 16; i++ ) \ for( i = 0; i < 16; i++ ) \
( o )[i] = ( i1 )[i] ^ ( i2 )[i]; ( o )[i] = ( i1 )[i] ^ ( i2 )[i];
@ -64,7 +64,8 @@
*/ */
#define UPDATE_CMAC( x ) \ #define UPDATE_CMAC( x ) \
XOR_128( Mn, ( x ), Mn ); \ XOR_128( Mn, ( x ), Mn ); \
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, Mn, 16, Mn, &olen ) ) != 0 ) \ if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, \
Mn, 16, Mn, &olen ) ) != 0 ) \
return( ret ); return( ret );
/* Implementation that should never be optimized out by the compiler */ /* Implementation that should never be optimized out by the compiler */
@ -84,7 +85,7 @@ void mbedtls_cmac_init( mbedtls_cmac_context *ctx )
* Leftshift a 16-byte block by 1 bit * Leftshift a 16-byte block by 1 bit
* \note output can be same as input * \note output can be same as input
*/ */
static void leftshift_onebit(unsigned char *input, unsigned char *output) static void leftshift_onebit( unsigned char *input, unsigned char *output )
{ {
int i; int i;
unsigned char temp; unsigned char temp;
@ -97,22 +98,24 @@ static void leftshift_onebit(unsigned char *input, unsigned char *output)
output[i] |= overflow; output[i] |= overflow;
overflow = temp >> 7; overflow = temp >> 7;
} }
return; return;
} }
/* /*
* Generate subkeys * Generate subkeys
*/ */
static int generate_subkeys(mbedtls_cmac_context *ctx) static int generate_subkeys( mbedtls_cmac_context *ctx )
{ {
static const unsigned char Rb[2] = {0x00, 0x87}; /* Note - block size 16 only */ static const unsigned char Rb[2] = { 0x00, 0x87 }; /* block size 16 only */
int ret; int ret;
unsigned char L[16]; unsigned char L[16];
size_t olen; size_t olen;
/* Calculate Ek(0) */ /* Calculate Ek(0) */
memset( L, 0, 16 ); memset( L, 0, 16 );
if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx, L, 16, L, &olen ) ) != 0 ) if( ( ret = mbedtls_cipher_update( &ctx->cipher_ctx,
L, 16, L, &olen ) ) != 0 )
{ {
return( ret ); return( ret );
} }
@ -144,7 +147,8 @@ int mbedtls_cmac_setkey( mbedtls_cmac_context *ctx,
int ret; int ret;
const mbedtls_cipher_info_t *cipher_info; const mbedtls_cipher_info_t *cipher_info;
cipher_info = mbedtls_cipher_info_from_values( cipher, keybits, MBEDTLS_MODE_ECB ); cipher_info = mbedtls_cipher_info_from_values( cipher, keybits,
MBEDTLS_MODE_ECB );
if( cipher_info == NULL ) if( cipher_info == NULL )
return( MBEDTLS_ERR_CMAC_BAD_INPUT ); return( MBEDTLS_ERR_CMAC_BAD_INPUT );
@ -162,7 +166,7 @@ int mbedtls_cmac_setkey( mbedtls_cmac_context *ctx,
return( ret ); return( ret );
} }
return( generate_subkeys(ctx) ); return( generate_subkeys( ctx ) );
} }
/* /*
@ -175,7 +179,9 @@ void mbedtls_cmac_free( mbedtls_cmac_context *ctx )
} }
/* TODO: Use cipher padding function? */ /* TODO: Use cipher padding function? */
static void padding(const unsigned char *lastb, unsigned char *pad, const size_t length) static void padding( const unsigned char *lastb,
unsigned char *pad,
const size_t length )
{ {
size_t j; size_t j;
@ -236,12 +242,12 @@ static int cmac_generate( mbedtls_cmac_context *ctx,
if( flag ) if( flag )
{ {
/* Last block is complete block */ /* Last block is complete block */
XOR_128( &input[16 * (n - 1)], ctx->K1, M_last ); XOR_128( &input[16 * ( n - 1 )], ctx->K1, M_last );
} }
else else
{ {
/* TODO: Use cipher padding function? */ /* TODO: Use cipher padding function? */
padding( &input[16 * (n - 1)], padded, in_len % 16 ); padding( &input[16 * ( n - 1 )], padded, in_len % 16 );
XOR_128( padded, ctx->K2, M_last ); XOR_128( padded, ctx->K2, M_last );
} }
@ -249,10 +255,10 @@ static int cmac_generate( mbedtls_cmac_context *ctx,
for( j = 0; j < n - 1; j++ ) for( j = 0; j < n - 1; j++ )
{ {
UPDATE_CMAC(&input[16 * j]); UPDATE_CMAC( &input[16 * j] );
} }
UPDATE_CMAC(M_last); UPDATE_CMAC( M_last );
memcpy( tag, Mn, 16 ); memcpy( tag, Mn, 16 );
@ -278,7 +284,7 @@ int mbedtls_cmac_verify( mbedtls_cmac_context *ctx,
unsigned char i; unsigned char i;
int diff; int diff;
if( ( ret = cmac_generate( ctx, input, in_len, check_tag, tag_len) ) != 0 ) if( ( ret = cmac_generate( ctx, input, in_len, check_tag, tag_len ) ) != 0 )
{ {
return ret; return ret;
} }
@ -309,7 +315,7 @@ int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
if( key_length == 16 ) if( key_length == 16 )
{ {
/* Use key as is */ /* Use key as is */
memcpy(int_key, key, 16); memcpy( int_key, key, 16 );
} }
else else
{ {
@ -317,8 +323,9 @@ int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
/* Key is AES_CMAC(0, key) */ /* Key is AES_CMAC(0, key) */
mbedtls_cmac_init( &zero_ctx ); mbedtls_cmac_init( &zero_ctx );
memset(zero_key, 0, 16); memset( zero_key, 0, 16 );
ret = mbedtls_cmac_setkey( &zero_ctx, MBEDTLS_CIPHER_ID_AES, zero_key, 8 * sizeof zero_key ); ret = mbedtls_cmac_setkey( &zero_ctx, MBEDTLS_CIPHER_ID_AES,
zero_key, 8 * sizeof zero_key );
if( ret != 0 ) if( ret != 0 )
{ {
return( ret ); return( ret );
@ -330,7 +337,8 @@ int mbedtls_aes_cmac_prf_128( mbedtls_cmac_context *ctx,
} }
} }
ret = mbedtls_cmac_setkey( ctx, MBEDTLS_CIPHER_ID_AES, int_key, 8 * sizeof int_key ); ret = mbedtls_cmac_setkey( ctx, MBEDTLS_CIPHER_ID_AES,
int_key, 8 * sizeof int_key );
if( ret != 0 ) if( ret != 0 )
{ {
return( ret ); return( ret );
@ -482,6 +490,7 @@ int mbedtls_cmac_self_test( int verbose )
return( 1 ); return( 1 );
} }
ret = mbedtls_cmac_verify( &ctx, M, Mlen[i], T[i], 16 ); ret = mbedtls_cmac_verify( &ctx, M, Mlen[i], T[i], 16 );
if( ret != 0 ) if( ret != 0 )
{ {
@ -499,7 +508,7 @@ int mbedtls_cmac_self_test( int verbose )
{ {
mbedtls_printf( " AES-CMAC-128-PRF #%u: ", i ); mbedtls_printf( " AES-CMAC-128-PRF #%u: ", i );
mbedtls_aes_cmac_prf_128( &ctx, PRFK, PRFKlen[i], PRFM, 20, tag); mbedtls_aes_cmac_prf_128( &ctx, PRFK, PRFKlen[i], PRFM, 20, tag );
if( ret != 0 || if( ret != 0 ||
memcmp( tag, PRFT[i], 16 ) != 0 ) memcmp( tag, PRFT[i], 16 ) != 0 )