Improve style in the Cipher module

This commit is contained in:
k-stachowiak 2018-12-19 16:52:45 +01:00
parent e4b8d28ca7
commit 1a9df6bcb7

View File

@ -86,7 +86,7 @@ static int mbedtls_constant_time_memcmp( const void *v1, const void *v2, size_t
for( diff = 0, i = 0; i < len; i++ ) for( diff = 0, i = 0; i < len; i++ )
diff |= p1[i] ^ p2[i]; diff |= p1[i] ^ p2[i];
return (int)diff; return( (int)diff );
} }
#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */ #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
@ -183,7 +183,7 @@ int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_in
{ {
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
if( cipher_info == NULL ) if( cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) ); memset( ctx, 0, sizeof( mbedtls_cipher_context_t ) );
@ -216,7 +216,7 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT || CIPHER_VALIDATE_RET( operation == MBEDTLS_ENCRYPT ||
operation == MBEDTLS_DECRYPT ); operation == MBEDTLS_DECRYPT );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 && if( ( ctx->cipher_info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ) == 0 &&
(int) ctx->cipher_info->key_bitlen != key_bitlen ) (int) ctx->cipher_info->key_bitlen != key_bitlen )
@ -235,13 +235,13 @@ int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
MBEDTLS_MODE_OFB == ctx->cipher_info->mode || MBEDTLS_MODE_OFB == ctx->cipher_info->mode ||
MBEDTLS_MODE_CTR == ctx->cipher_info->mode ) MBEDTLS_MODE_CTR == ctx->cipher_info->mode )
{ {
return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key, return( ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
ctx->key_bitlen ); ctx->key_bitlen ) );
} }
if( MBEDTLS_DECRYPT == operation ) if( MBEDTLS_DECRYPT == operation )
return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key, return( ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
ctx->key_bitlen ); ctx->key_bitlen ) );
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
} }
@ -255,7 +255,7 @@ int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL ); CIPHER_VALIDATE_RET( iv_len == 0 || iv != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
/* avoid buffer overflow in ctx->iv */ /* avoid buffer overflow in ctx->iv */
if( iv_len > MBEDTLS_MAX_IV_LENGTH ) if( iv_len > MBEDTLS_MAX_IV_LENGTH )
@ -297,7 +297,7 @@ int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx )
{ {
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
ctx->unprocessed_len = 0; ctx->unprocessed_len = 0;
@ -311,13 +311,13 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL ); CIPHER_VALIDATE_RET( ad_len == 0 || ad != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_GCM_C)
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
{ {
return mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation, return( mbedtls_gcm_starts( (mbedtls_gcm_context *) ctx->cipher_ctx, ctx->operation,
ctx->iv, ctx->iv_size, ad, ad_len ); ctx->iv, ctx->iv_size, ad, ad_len ) );
} }
#endif #endif
@ -337,8 +337,8 @@ int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
if ( result != 0 ) if ( result != 0 )
return( result ); return( result );
return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx, return( mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
ad, ad_len ); ad, ad_len ) );
} }
#endif #endif
@ -357,7 +357,7 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
CIPHER_VALIDATE_RET( output != NULL ); CIPHER_VALIDATE_RET( output != NULL );
CIPHER_VALIDATE_RET( olen != NULL ); CIPHER_VALIDATE_RET( olen != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
*olen = 0; *olen = 0;
block_size = mbedtls_cipher_get_block_size( ctx ); block_size = mbedtls_cipher_get_block_size( ctx );
@ -382,8 +382,8 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM ) if( ctx->cipher_info->mode == MBEDTLS_MODE_GCM )
{ {
*olen = ilen; *olen = ilen;
return mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input, return( mbedtls_gcm_update( (mbedtls_gcm_context *) ctx->cipher_ctx, ilen, input,
output ); output ) );
} }
#endif #endif
@ -391,14 +391,14 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 ) if ( ctx->cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
{ {
*olen = ilen; *olen = ilen;
return mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx, return( mbedtls_chachapoly_update( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
ilen, input, output ); ilen, input, output ) );
} }
#endif #endif
if ( 0 == block_size ) if ( 0 == block_size )
{ {
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
} }
if( input == output && if( input == output &&
@ -461,7 +461,7 @@ int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *i
{ {
if( 0 == block_size ) if( 0 == block_size )
{ {
return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; return( MBEDTLS_ERR_CIPHER_INVALID_CONTEXT );
} }
/* Encryption: only cache partial blocks /* Encryption: only cache partial blocks
@ -766,7 +766,7 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( output != NULL ); CIPHER_VALIDATE_RET( output != NULL );
CIPHER_VALIDATE_RET( olen != NULL ); CIPHER_VALIDATE_RET( olen != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
*olen = 0; *olen = 0;
@ -835,8 +835,8 @@ int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
/* Set output size for decryption */ /* Set output size for decryption */
if( MBEDTLS_DECRYPT == ctx->operation ) if( MBEDTLS_DECRYPT == ctx->operation )
return ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ), return( ctx->get_padding( output, mbedtls_cipher_get_block_size( ctx ),
olen ); olen ) );
/* Set output size for encryption */ /* Set output size for encryption */
*olen = mbedtls_cipher_get_block_size( ctx ); *olen = mbedtls_cipher_get_block_size( ctx );
@ -854,10 +854,8 @@ int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
mbedtls_cipher_padding_t mode ) mbedtls_cipher_padding_t mode )
{ {
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
if( MBEDTLS_MODE_CBC != ctx->cipher_info->mode ) if( NULL == ctx->cipher_info || MBEDTLS_MODE_CBC != ctx->cipher_info->mode )
{ {
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
} }
@ -908,14 +906,15 @@ int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
if( MBEDTLS_ENCRYPT != ctx->operation ) if( MBEDTLS_ENCRYPT != ctx->operation )
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_GCM_C)
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
return mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx, tag, tag_len ); return( mbedtls_gcm_finish( (mbedtls_gcm_context *) ctx->cipher_ctx,
tag, tag_len ) );
#endif #endif
#if defined(MBEDTLS_CHACHAPOLY_C) #if defined(MBEDTLS_CHACHAPOLY_C)
@ -925,8 +924,8 @@ int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
if ( tag_len != 16U ) if ( tag_len != 16U )
return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx, return( mbedtls_chachapoly_finish( (mbedtls_chachapoly_context*) ctx->cipher_ctx,
tag ); tag ) );
} }
#endif #endif
@ -942,7 +941,7 @@ int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( ctx != NULL ); CIPHER_VALIDATE_RET( ctx != NULL );
CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
if( MBEDTLS_DECRYPT != ctx->operation ) if( MBEDTLS_DECRYPT != ctx->operation )
{ {
@ -1012,7 +1011,7 @@ int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( output != NULL ); CIPHER_VALIDATE_RET( output != NULL );
CIPHER_VALIDATE_RET( olen != NULL ); CIPHER_VALIDATE_RET( olen != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 ) if( ( ret = mbedtls_cipher_set_iv( ctx, iv, iv_len ) ) != 0 )
return( ret ); return( ret );
@ -1050,7 +1049,7 @@ int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( olen != NULL ); CIPHER_VALIDATE_RET( olen != NULL );
CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_GCM_C)
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )
@ -1107,7 +1106,7 @@ int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
CIPHER_VALIDATE_RET( olen != NULL ); CIPHER_VALIDATE_RET( olen != NULL );
CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL ); CIPHER_VALIDATE_RET( tag_len == 0 || tag != NULL );
if( ctx->cipher_info == NULL ) if( ctx->cipher_info == NULL )
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
#if defined(MBEDTLS_GCM_C) #if defined(MBEDTLS_GCM_C)
if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode ) if( MBEDTLS_MODE_GCM == ctx->cipher_info->mode )