diff --git a/include/mbedtls/sha3.h b/include/mbedtls/sha3.h index 278655ac81..2ddb22a3be 100644 --- a/include/mbedtls/sha3.h +++ b/include/mbedtls/sha3.h @@ -46,8 +46,7 @@ extern "C" { * It identifies the family (SHA3-256, SHA3-512, etc.) */ -typedef enum -{ +typedef enum { MBEDTLS_SHA3_NONE = 0, /*!< Operation not defined. */ MBEDTLS_SHA3_224, /*!< SHA3-224 */ MBEDTLS_SHA3_256, /*!< SHA3-256 */ @@ -56,8 +55,7 @@ typedef enum } mbedtls_sha3_id; struct mbedtls_sha3_context; -typedef struct mbedtls_sha3_family_functions -{ +typedef struct mbedtls_sha3_family_functions { mbedtls_sha3_id id; uint16_t r; @@ -88,7 +86,7 @@ mbedtls_sha3_context; * * \param ctx The SHA-3 context to initialize. This must not be \c NULL. */ -void mbedtls_sha3_init( mbedtls_sha3_context *ctx ); +void mbedtls_sha3_init(mbedtls_sha3_context *ctx); /** * \brief This function clears a SHA-3 context. @@ -97,7 +95,7 @@ void mbedtls_sha3_init( mbedtls_sha3_context *ctx ); * case this function returns immediately. If it is not \c NULL, * it must point to an initialized SHA-3 context. */ -void mbedtls_sha3_free( mbedtls_sha3_context *ctx ); +void mbedtls_sha3_free(mbedtls_sha3_context *ctx); /** * \brief This function clones the state of a SHA-3 context. @@ -105,8 +103,8 @@ void mbedtls_sha3_free( mbedtls_sha3_context *ctx ); * \param dst The destination context. This must be initialized. * \param src The context to clone. This must be initialized. */ -void mbedtls_sha3_clone( mbedtls_sha3_context *dst, - const mbedtls_sha3_context *src ); +void mbedtls_sha3_clone(mbedtls_sha3_context *dst, + const mbedtls_sha3_context *src); /** * \brief This function starts a SHA-3 checksum @@ -118,7 +116,7 @@ void mbedtls_sha3_clone( mbedtls_sha3_context *dst, * \return \c 0 on success. * \return A negative error code on failure. */ -int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id ); +int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id); /** * \brief This function feeds an input buffer into an ongoing @@ -133,9 +131,9 @@ int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id ); * \return \c 0 on success. * \return A negative error code on failure. */ -int mbedtls_sha3_update( mbedtls_sha3_context *ctx, - const uint8_t *input, - size_t ilen ); +int mbedtls_sha3_update(mbedtls_sha3_context *ctx, + const uint8_t *input, + size_t ilen); /** * \brief This function finishes the SHA-3 operation, and writes @@ -152,8 +150,8 @@ int mbedtls_sha3_update( mbedtls_sha3_context *ctx, * \return \c 0 on success. * \return A negative error code on failure. */ -int mbedtls_sha3_finish( mbedtls_sha3_context *ctx, - uint8_t *output, size_t olen ); +int mbedtls_sha3_finish(mbedtls_sha3_context *ctx, + uint8_t *output, size_t olen); /** * \brief This function calculates the SHA-3 @@ -178,10 +176,10 @@ int mbedtls_sha3_finish( mbedtls_sha3_context *ctx, * \return \c 0 on success. * \return A negative error code on failure. */ -int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input, - size_t ilen, - uint8_t *output, - size_t olen ); +int mbedtls_sha3(mbedtls_sha3_id id, const uint8_t *input, + size_t ilen, + uint8_t *output, + size_t olen); #if defined(MBEDTLS_SELF_TEST) /** @@ -191,7 +189,7 @@ int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input, * * \return 0 if successful, or 1 if the test failed. */ -int mbedtls_sha3_self_test( int verbose ); +int mbedtls_sha3_self_test(int verbose); #endif /* MBEDTLS_SELF_TEST */ #ifdef __cplusplus @@ -199,4 +197,3 @@ int mbedtls_sha3_self_test( int verbose ); #endif #endif /* mbedtls_sha3.h */ - diff --git a/library/md.c b/library/md.c index 0be1b7db85..bc0a1152c9 100644 --- a/library/md.c +++ b/library/md.c @@ -277,13 +277,13 @@ const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type) #endif #if defined(MBEDTLS_SHA3_C) case MBEDTLS_MD_SHA3_224: - return( &mbedtls_sha3_224_info ); + return &mbedtls_sha3_224_info; case MBEDTLS_MD_SHA3_256: - return( &mbedtls_sha3_256_info ); + return &mbedtls_sha3_256_info; case MBEDTLS_MD_SHA3_384: - return( &mbedtls_sha3_384_info ); + return &mbedtls_sha3_384_info; case MBEDTLS_MD_SHA3_512: - return( &mbedtls_sha3_512_info ); + return &mbedtls_sha3_512_info; #endif default: return NULL; @@ -353,7 +353,7 @@ void mbedtls_md_free(mbedtls_md_context_t *ctx) case MBEDTLS_MD_SHA3_256: case MBEDTLS_MD_SHA3_384: case MBEDTLS_MD_SHA3_512: - mbedtls_sha3_free( ctx->md_ctx ); + mbedtls_sha3_free(ctx->md_ctx); break; #endif default: @@ -422,7 +422,7 @@ int mbedtls_md_clone(mbedtls_md_context_t *dst, case MBEDTLS_MD_SHA3_256: case MBEDTLS_MD_SHA3_384: case MBEDTLS_MD_SHA3_512: - mbedtls_sha3_clone( dst->md_ctx, src->md_ctx ); + mbedtls_sha3_clone(dst->md_ctx, src->md_ctx); break; #endif default: @@ -492,7 +492,7 @@ int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info case MBEDTLS_MD_SHA3_256: case MBEDTLS_MD_SHA3_384: case MBEDTLS_MD_SHA3_512: - ALLOC( sha3 ); + ALLOC(sha3); break; #endif default: @@ -548,13 +548,13 @@ int mbedtls_md_starts(mbedtls_md_context_t *ctx) #endif #if defined(MBEDTLS_SHA3_C) case MBEDTLS_MD_SHA3_224: - return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_224 ) ); + return mbedtls_sha3_starts(ctx->md_ctx, MBEDTLS_SHA3_224); case MBEDTLS_MD_SHA3_256: - return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_256 ) ); + return mbedtls_sha3_starts(ctx->md_ctx, MBEDTLS_SHA3_256); case MBEDTLS_MD_SHA3_384: - return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_384 ) ); + return mbedtls_sha3_starts(ctx->md_ctx, MBEDTLS_SHA3_384); case MBEDTLS_MD_SHA3_512: - return( mbedtls_sha3_starts( ctx->md_ctx, MBEDTLS_SHA3_512 ) ); + return mbedtls_sha3_starts(ctx->md_ctx, MBEDTLS_SHA3_512); #endif default: return MBEDTLS_ERR_MD_BAD_INPUT_DATA; @@ -601,7 +601,7 @@ int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, siz case MBEDTLS_MD_SHA3_256: case MBEDTLS_MD_SHA3_384: case MBEDTLS_MD_SHA3_512: - return( mbedtls_sha3_update( ctx->md_ctx, input, ilen ) ); + return mbedtls_sha3_update(ctx->md_ctx, input, ilen); #endif default: return MBEDTLS_ERR_MD_BAD_INPUT_DATA; @@ -648,7 +648,7 @@ int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output) case MBEDTLS_MD_SHA3_256: case MBEDTLS_MD_SHA3_384: case MBEDTLS_MD_SHA3_512: - return( mbedtls_sha3_finish( ctx->md_ctx, output, ctx->md_info->size ) ); + return mbedtls_sha3_finish(ctx->md_ctx, output, ctx->md_info->size); #endif default: return MBEDTLS_ERR_MD_BAD_INPUT_DATA; @@ -693,13 +693,13 @@ int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, siz #endif #if defined(MBEDTLS_SHA3_C) case MBEDTLS_MD_SHA3_224: - return( mbedtls_sha3( MBEDTLS_SHA3_224, input, ilen, output, md_info->size ) ); + return mbedtls_sha3(MBEDTLS_SHA3_224, input, ilen, output, md_info->size); case MBEDTLS_MD_SHA3_256: - return( mbedtls_sha3( MBEDTLS_SHA3_256, input, ilen, output, md_info->size ) ); + return mbedtls_sha3(MBEDTLS_SHA3_256, input, ilen, output, md_info->size); case MBEDTLS_MD_SHA3_384: - return( mbedtls_sha3( MBEDTLS_SHA3_384, input, ilen, output, md_info->size ) ); + return mbedtls_sha3(MBEDTLS_SHA3_384, input, ilen, output, md_info->size); case MBEDTLS_MD_SHA3_512: - return( mbedtls_sha3( MBEDTLS_SHA3_512, input, ilen, output, md_info->size ) ); + return mbedtls_sha3(MBEDTLS_SHA3_512, input, ilen, output, md_info->size); #endif default: return MBEDTLS_ERR_MD_BAD_INPUT_DATA; @@ -936,7 +936,7 @@ int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data) case MBEDTLS_MD_SHA3_256: case MBEDTLS_MD_SHA3_384: case MBEDTLS_MD_SHA3_512: - return( 0 ); + return 0; #endif default: return MBEDTLS_ERR_MD_BAD_INPUT_DATA; diff --git a/library/sha3.c b/library/sha3.c index dbaf8b613e..959928e9da 100644 --- a/library/sha3.c +++ b/library/sha3.c @@ -67,10 +67,11 @@ static const uint8_t pi[24] = { 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1, }; -#define ROT64( x , y ) ( ( ( x ) << ( y ) ) | ( ( x ) >> ( 64U - ( y ) ) ) ) -#define ABSORB( ctx, idx, v ) do { ctx->state[( idx ) >> 3] ^= ( ( uint64_t ) ( v ) ) << ( ( ( idx ) & 0x7 ) << 3 ); } while( 0 ) -#define SQUEEZE( ctx, idx ) ( ( uint8_t )( ctx->state[( idx ) >> 3] >> ( ( ( idx ) & 0x7 ) << 3 ) ) ) -#define SWAP( x, y ) do { uint64_t tmp = ( x ); ( x ) = ( y ); ( y ) = tmp; } while( 0 ) +#define ROT64(x, y) (((x) << (y)) | ((x) >> (64U - (y)))) +#define ABSORB(ctx, idx, v) do { ctx->state[(idx) >> 3] ^= ((uint64_t) (v)) << (((idx) & 0x7) << 3); \ +} while (0) +#define SQUEEZE(ctx, idx) ((uint8_t) (ctx->state[(idx) >> 3] >> (((idx) & 0x7) << 3))) +#define SWAP(x, y) do { uint64_t tmp = (x); (x) = (y); (y) = tmp; } while (0) /* The permutation function. */ static void keccak_f1600(mbedtls_sha3_context *ctx) @@ -79,8 +80,7 @@ static void keccak_f1600(mbedtls_sha3_context *ctx) uint64_t *s = ctx->state; int i; - for( int round = 0; round < 24; round++ ) - { + for (int round = 0; round < 24; round++) { uint64_t t; /* Theta */ @@ -90,29 +90,31 @@ static void keccak_f1600(mbedtls_sha3_context *ctx) lane[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23]; lane[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24]; - t = lane[4] ^ ROT64( lane[1], 1 ); + t = lane[4] ^ ROT64(lane[1], 1); s[0] ^= t; s[5] ^= t; s[10] ^= t; s[15] ^= t; s[20] ^= t; - t = lane[0] ^ ROT64( lane[2], 1 ); + t = lane[0] ^ ROT64(lane[2], 1); s[1] ^= t; s[6] ^= t; s[11] ^= t; s[16] ^= t; s[21] ^= t; - t = lane[1] ^ ROT64( lane[3], 1 ); + t = lane[1] ^ ROT64(lane[3], 1); s[2] ^= t; s[7] ^= t; s[12] ^= t; s[17] ^= t; s[22] ^= t; - t = lane[2] ^ ROT64( lane[4], 1 ); + t = lane[2] ^ ROT64(lane[4], 1); s[3] ^= t; s[8] ^= t; s[13] ^= t; s[18] ^= t; s[23] ^= t; - t = lane[3] ^ ROT64( lane[0], 1 ); + t = lane[3] ^ ROT64(lane[0], 1); s[4] ^= t; s[9] ^= t; s[14] ^= t; s[19] ^= t; s[24] ^= t; /* Rho */ - for( i = 1; i < 25; i++ ) - s[i] = ROT64( s[i], rho[i-1] ); + for (i = 1; i < 25; i++) { + s[i] = ROT64(s[i], rho[i-1]); + } /* Pi */ t = s[1]; - for( i = 0; i < 24; i++ ) - SWAP( s[pi[i]], t ); + for (i = 0; i < 24; i++) { + SWAP(s[pi[i]], t); + } /* Chi */ lane[0] = s[0]; lane[1] = s[1]; lane[2] = s[2]; lane[3] = s[3]; lane[4] = s[4]; @@ -155,27 +157,30 @@ static void keccak_f1600(mbedtls_sha3_context *ctx) } } -void mbedtls_sha3_init( mbedtls_sha3_context *ctx ) +void mbedtls_sha3_init(mbedtls_sha3_context *ctx) { - if( ctx == NULL ) + if (ctx == NULL) { return; + } - memset( ctx, 0, sizeof( mbedtls_sha3_context ) ); + memset(ctx, 0, sizeof(mbedtls_sha3_context)); } -void mbedtls_sha3_free( mbedtls_sha3_context *ctx ) +void mbedtls_sha3_free(mbedtls_sha3_context *ctx) { - if( ctx == NULL ) + if (ctx == NULL) { return; + } - mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha3_context ) ); + mbedtls_platform_zeroize(ctx, sizeof(mbedtls_sha3_context)); } -void mbedtls_sha3_clone( mbedtls_sha3_context *dst, - const mbedtls_sha3_context *src ) +void mbedtls_sha3_clone(mbedtls_sha3_context *dst, + const mbedtls_sha3_context *src) { - if ( dst == NULL || src == NULL ) + if (dst == NULL || src == NULL) { return; + } *dst = *src; } @@ -183,20 +188,22 @@ void mbedtls_sha3_clone( mbedtls_sha3_context *dst, /* * SHA-3 context setup */ -int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id ) +int mbedtls_sha3_starts(mbedtls_sha3_context *ctx, mbedtls_sha3_id id) { mbedtls_sha3_family_functions *p = NULL; - if( ctx == NULL ) - return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); - - for( p = sha3_families; p->id != MBEDTLS_SHA3_NONE; p++ ) - { - if( p->id == id ) - break; + if (ctx == NULL) { + return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA; } - if( p == NULL || p->id == MBEDTLS_SHA3_NONE ) - return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); + for (p = sha3_families; p->id != MBEDTLS_SHA3_NONE; p++) { + if (p->id == id) { + break; + } + } + + if (p == NULL || p->id == MBEDTLS_SHA3_NONE) { + return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA; + } ctx->id = id; ctx->r = p->r; @@ -204,90 +211,96 @@ int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id ) ctx->xor_byte = p->xor_byte; ctx->max_block_size = ctx->r / 8; - memset( ctx->state, 0, sizeof( ctx->state ) ); + memset(ctx->state, 0, sizeof(ctx->state)); ctx->index = 0; - return( 0 ); + return 0; } /* * SHA-3 process buffer */ -int mbedtls_sha3_update( mbedtls_sha3_context *ctx, - const uint8_t *input, - size_t ilen ) +int mbedtls_sha3_update(mbedtls_sha3_context *ctx, + const uint8_t *input, + size_t ilen) { - if( ctx == NULL ) - return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); - - if( ilen == 0 || input == NULL ) - return( 0 ); - - while( ilen-- > 0 ) - { - ABSORB( ctx, ctx->index, *input++ ); - if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 ) - keccak_f1600( ctx ); + if (ctx == NULL) { + return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA; } - return( 0 ); + if (ilen == 0 || input == NULL) { + return 0; + } + + while (ilen-- > 0) { + ABSORB(ctx, ctx->index, *input++); + if ((ctx->index = (ctx->index + 1) % ctx->max_block_size) == 0) { + keccak_f1600(ctx); + } + } + + return 0; } -int mbedtls_sha3_finish( mbedtls_sha3_context *ctx, - uint8_t *output, size_t olen ) +int mbedtls_sha3_finish(mbedtls_sha3_context *ctx, + uint8_t *output, size_t olen) { - if( ctx == NULL || output == NULL ) - return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); + if (ctx == NULL || output == NULL) { + return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA; + } /* Catch SHA-3 families, with fixed output length */ - if( ctx->olen > 0 ) - { - if ( ctx->olen > olen ) - return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); + if (ctx->olen > 0) { + if (ctx->olen > olen) { + return MBEDTLS_ERR_SHA3_BAD_INPUT_DATA; + } olen = ctx->olen; } - ABSORB( ctx, ctx->index, ctx->xor_byte ); - ABSORB( ctx, ctx->max_block_size - 1, 0x80 ); - keccak_f1600( ctx ); + ABSORB(ctx, ctx->index, ctx->xor_byte); + ABSORB(ctx, ctx->max_block_size - 1, 0x80); + keccak_f1600(ctx); ctx->index = 0; - while( olen-- > 0 ) - { - *output++ = SQUEEZE( ctx, ctx->index ); + while (olen-- > 0) { + *output++ = SQUEEZE(ctx, ctx->index); - if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 ) - keccak_f1600( ctx ); + if ((ctx->index = (ctx->index + 1) % ctx->max_block_size) == 0) { + keccak_f1600(ctx); + } } - return( 0 ); + return 0; } /* * output = SHA3( input buffer ) */ -int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input, - size_t ilen, uint8_t *output, size_t olen ) +int mbedtls_sha3(mbedtls_sha3_id id, const uint8_t *input, + size_t ilen, uint8_t *output, size_t olen) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; mbedtls_sha3_context ctx; - mbedtls_sha3_init( &ctx ); + mbedtls_sha3_init(&ctx); /* Sanity checks are performed in every mbedtls_sha3_xxx() */ - if( ( ret = mbedtls_sha3_starts( &ctx, id ) ) != 0 ) + if ((ret = mbedtls_sha3_starts(&ctx, id)) != 0) { goto exit; + } - if( ( ret = mbedtls_sha3_update( &ctx, input, ilen ) ) != 0 ) + if ((ret = mbedtls_sha3_update(&ctx, input, ilen)) != 0) { goto exit; + } - if( ( ret = mbedtls_sha3_finish( &ctx, output, olen ) ) != 0 ) + if ((ret = mbedtls_sha3_finish(&ctx, output, olen)) != 0) { goto exit; + } exit: - mbedtls_sha3_free( &ctx ); + mbedtls_sha3_free(&ctx); - return( ret ); + return ret; } /**************** Self-tests ****************/ @@ -420,67 +433,61 @@ static const unsigned char long_kat_hash_sha3_512[64] = 0x0D, 0xB5, 0x96, 0xC9, 0x0B, 0x0A, 0x7B, 0x87 }; -static int mbedtls_sha3_kat_test( int verbose, - const char* type_name, - mbedtls_sha3_id id, - int test_num ) +static int mbedtls_sha3_kat_test(int verbose, + const char *type_name, + mbedtls_sha3_id id, + int test_num) { uint8_t hash[64]; int result; - result = mbedtls_sha3( id, - test_data[test_num], test_data_len[test_num], - hash, sizeof( hash ) ); - if( result != 0 ) - { - if( verbose != 0 ) - { - mbedtls_printf( " %s test %d error code: %d\n", - type_name, test_num, result ); + result = mbedtls_sha3(id, + test_data[test_num], test_data_len[test_num], + hash, sizeof(hash)); + if (result != 0) { + if (verbose != 0) { + mbedtls_printf(" %s test %d error code: %d\n", + type_name, test_num, result); } - return( result ); + return result; } - switch( id ) - { + switch (id) { case MBEDTLS_SHA3_224: - result = memcmp( hash, test_hash_sha3_224[test_num], 28 ); + result = memcmp(hash, test_hash_sha3_224[test_num], 28); break; case MBEDTLS_SHA3_256: - result = memcmp( hash, test_hash_sha3_256[test_num], 32 ); + result = memcmp(hash, test_hash_sha3_256[test_num], 32); break; case MBEDTLS_SHA3_384: - result = memcmp( hash, test_hash_sha3_384[test_num], 48 ); + result = memcmp(hash, test_hash_sha3_384[test_num], 48); break; case MBEDTLS_SHA3_512: - result = memcmp( hash, test_hash_sha3_512[test_num], 64 ); + result = memcmp(hash, test_hash_sha3_512[test_num], 64); break; default: break; } - if( 0 != result ) - { - if( verbose != 0 ) - { - mbedtls_printf( " %s test %d failed\n", type_name, test_num ); + if (0 != result) { + if (verbose != 0) { + mbedtls_printf(" %s test %d failed\n", type_name, test_num); } - return( -1 ); + return -1; } - if( verbose != 0 ) - { - mbedtls_printf( " %s test %d passed\n", type_name, test_num ); + if (verbose != 0) { + mbedtls_printf(" %s test %d passed\n", type_name, test_num); } - return( 0 ); + return 0; } -static int mbedtls_sha3_long_kat_test( int verbose, - const char* type_name, - mbedtls_sha3_id id ) +static int mbedtls_sha3_long_kat_test(int verbose, + const char *type_name, + mbedtls_sha3_id id) { mbedtls_sha3_context ctx; unsigned char buffer[1000]; @@ -488,133 +495,127 @@ static int mbedtls_sha3_long_kat_test( int verbose, int i; int result = 0; - memset( buffer, 'a', 1000 ); + memset(buffer, 'a', 1000); - if( verbose != 0 ) - { - mbedtls_printf( " %s long KAT test ", type_name ); + if (verbose != 0) { + mbedtls_printf(" %s long KAT test ", type_name); } - mbedtls_sha3_init( &ctx ); + mbedtls_sha3_init(&ctx); - result = mbedtls_sha3_starts( &ctx, id ); - if( result != 0 ) - { - if( verbose != 0 ) - { - mbedtls_printf( "setup failed\n " ); + result = mbedtls_sha3_starts(&ctx, id); + if (result != 0) { + if (verbose != 0) { + mbedtls_printf("setup failed\n "); } } /* Process 1,000,000 (one million) 'a' characters */ - for( i = 0; i < 1000; i++ ) - { - result = mbedtls_sha3_update( &ctx, buffer, 1000 ); - if( result != 0 ) - { - if( verbose != 0 ) - { - mbedtls_printf( "update error code: %i\n", result ); + for (i = 0; i < 1000; i++) { + result = mbedtls_sha3_update(&ctx, buffer, 1000); + if (result != 0) { + if (verbose != 0) { + mbedtls_printf("update error code: %i\n", result); } goto cleanup; } } - result = mbedtls_sha3_finish( &ctx, hash, sizeof( hash ) ); - if( result != 0 ) - { - if( verbose != 0 ) - { - mbedtls_printf( "finish error code: %d\n", result ); + result = mbedtls_sha3_finish(&ctx, hash, sizeof(hash)); + if (result != 0) { + if (verbose != 0) { + mbedtls_printf("finish error code: %d\n", result); } goto cleanup; } - switch( id ) - { + switch (id) { case MBEDTLS_SHA3_224: - result = memcmp( hash, long_kat_hash_sha3_224, 28 ); + result = memcmp(hash, long_kat_hash_sha3_224, 28); break; case MBEDTLS_SHA3_256: - result = memcmp( hash, long_kat_hash_sha3_256, 32 ); + result = memcmp(hash, long_kat_hash_sha3_256, 32); break; case MBEDTLS_SHA3_384: - result = memcmp( hash, long_kat_hash_sha3_384, 48 ); + result = memcmp(hash, long_kat_hash_sha3_384, 48); break; case MBEDTLS_SHA3_512: - result = memcmp( hash, long_kat_hash_sha3_512, 64 ); + result = memcmp(hash, long_kat_hash_sha3_512, 64); break; default: break; } - if( result != 0 ) - { - if( verbose != 0 ) - { - mbedtls_printf( "failed\n" ); + if (result != 0) { + if (verbose != 0) { + mbedtls_printf("failed\n"); } } - if( verbose != 0 ) - { - mbedtls_printf( "passed\n" ); + if (verbose != 0) { + mbedtls_printf("passed\n"); } cleanup: - mbedtls_sha3_free( &ctx ); - return( result ); + mbedtls_sha3_free(&ctx); + return result; } -int mbedtls_sha3_self_test( int verbose ) +int mbedtls_sha3_self_test(int verbose) { int i; /* SHA3 Known Answer Tests (KAT) */ - for( i = 0; i < 2; i++ ) - { - if( 0 != mbedtls_sha3_kat_test( verbose, - "SHA3-224", MBEDTLS_SHA3_224, i ) ) - return( 1 ); + for (i = 0; i < 2; i++) { + if (0 != mbedtls_sha3_kat_test(verbose, + "SHA3-224", MBEDTLS_SHA3_224, i)) { + return 1; + } - if( 0 != mbedtls_sha3_kat_test( verbose, - "SHA3-256", MBEDTLS_SHA3_256, i ) ) - return( 1 ); + if (0 != mbedtls_sha3_kat_test(verbose, + "SHA3-256", MBEDTLS_SHA3_256, i)) { + return 1; + } - if( 0 != mbedtls_sha3_kat_test( verbose, - "SHA3-384", MBEDTLS_SHA3_384, i ) ) - return( 1 ); + if (0 != mbedtls_sha3_kat_test(verbose, + "SHA3-384", MBEDTLS_SHA3_384, i)) { + return 1; + } - if( 0 != mbedtls_sha3_kat_test( verbose, - "SHA3-512", MBEDTLS_SHA3_512, i ) ) - return( 1 ); + if (0 != mbedtls_sha3_kat_test(verbose, + "SHA3-512", MBEDTLS_SHA3_512, i)) { + return 1; + } } /* SHA3 long KAT tests */ - if( 0 != mbedtls_sha3_long_kat_test( verbose, - "SHA3-224", MBEDTLS_SHA3_224 ) ) - return( 1 ); - - if( 0 != mbedtls_sha3_long_kat_test( verbose, - "SHA3-256", MBEDTLS_SHA3_256 ) ) - return( 1 ); - - if( 0 != mbedtls_sha3_long_kat_test( verbose, - "SHA3-384", MBEDTLS_SHA3_384 ) ) - return( 1 ); - - if( 0 != mbedtls_sha3_long_kat_test( verbose, - "SHA3-512", MBEDTLS_SHA3_512 ) ) - return( 1 ); - - if( verbose != 0 ) - { - mbedtls_printf( "\n" ); + if (0 != mbedtls_sha3_long_kat_test(verbose, + "SHA3-224", MBEDTLS_SHA3_224)) { + return 1; } - return( 0 ); + if (0 != mbedtls_sha3_long_kat_test(verbose, + "SHA3-256", MBEDTLS_SHA3_256)) { + return 1; + } + + if (0 != mbedtls_sha3_long_kat_test(verbose, + "SHA3-384", MBEDTLS_SHA3_384)) { + return 1; + } + + if (0 != mbedtls_sha3_long_kat_test(verbose, + "SHA3-512", MBEDTLS_SHA3_512)) { + return 1; + } + + if (verbose != 0) { + mbedtls_printf("\n"); + } + + return 0; } #endif /* MBEDTLS_SELF_TEST */ diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c index 05f9084ccc..80bb63d4be 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -657,14 +657,18 @@ int main(int argc, char *argv[]) } #endif #if defined(MBEDTLS_SHA3_C) - if ( todo.sha3_224 ) - TIME_AND_TSC( "SHA3-224", mbedtls_sha3( MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28 ) ); - if ( todo.sha3_256 ) - TIME_AND_TSC( "SHA3-256", mbedtls_sha3( MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32 ) ); - if ( todo.sha3_384 ) - TIME_AND_TSC( "SHA3-384", mbedtls_sha3( MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48 ) ); - if ( todo.sha3_512 ) - TIME_AND_TSC( "SHA3-512", mbedtls_sha3( MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64 ) ); + if (todo.sha3_224) { + TIME_AND_TSC("SHA3-224", mbedtls_sha3(MBEDTLS_SHA3_224, buf, BUFSIZE, tmp, 28)); + } + if (todo.sha3_256) { + TIME_AND_TSC("SHA3-256", mbedtls_sha3(MBEDTLS_SHA3_256, buf, BUFSIZE, tmp, 32)); + } + if (todo.sha3_384) { + TIME_AND_TSC("SHA3-384", mbedtls_sha3(MBEDTLS_SHA3_384, buf, BUFSIZE, tmp, 48)); + } + if (todo.sha3_512) { + TIME_AND_TSC("SHA3-512", mbedtls_sha3(MBEDTLS_SHA3_512, buf, BUFSIZE, tmp, 64)); + } #endif #if defined(MBEDTLS_DES_C)