From b3b220cbf82dd7f4dd05b9f9516b4a2d9c0d572d Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 8 Feb 2023 12:52:18 +0100 Subject: [PATCH] Correct style. Signed-off-by: Pol Henarejos --- library/md.c | 8 +- programs/test/selftest.c | 2 +- tests/suites/test_suite_shax.function | 107 +++++++++++++------------- 3 files changed, 57 insertions(+), 60 deletions(-) diff --git a/library/md.c b/library/md.c index bc0a1152c..90c6bff9b 100644 --- a/library/md.c +++ b/library/md.c @@ -144,10 +144,10 @@ const mbedtls_md_info_t mbedtls_sha3_512_info = { static const int supported_digests[] = { #if defined(MBEDTLS_SHA3_C) - MBEDTLS_MD_SHA3_512, - MBEDTLS_MD_SHA3_384, - MBEDTLS_MD_SHA3_256, - MBEDTLS_MD_SHA3_224, + MBEDTLS_MD_SHA3_512, + MBEDTLS_MD_SHA3_384, + MBEDTLS_MD_SHA3_256, + MBEDTLS_MD_SHA3_224, #endif #if defined(MBEDTLS_SHA512_C) diff --git a/programs/test/selftest.c b/programs/test/selftest.c index 23a45c943..887476127 100644 --- a/programs/test/selftest.c +++ b/programs/test/selftest.c @@ -253,7 +253,7 @@ const selftest_t selftests[] = { "sha512", mbedtls_sha512_self_test }, #endif #if defined(MBEDTLS_SHA3_C) - {"sha3", mbedtls_sha3_self_test}, + { "sha3", mbedtls_sha3_self_test }, #endif #if defined(MBEDTLS_DES_C) { "des", mbedtls_des_self_test }, diff --git a/tests/suites/test_suite_shax.function b/tests/suites/test_suite_shax.function index 7b45bf32d..dec9f696b 100644 --- a/tests/suites/test_suite_shax.function +++ b/tests/suites/test_suite_shax.function @@ -151,143 +151,140 @@ void sha512_selftest() /* END_CASE */ /* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */ -void mbedtls_sha3( int family, data_t *in, data_t *hash ) +void mbedtls_sha3(int family, data_t *in, data_t *hash) { unsigned char *output = NULL; - ASSERT_ALLOC( output, hash->len ); + ASSERT_ALLOC(output, hash->len); - TEST_ASSERT( mbedtls_sha3( family, in->x, in->len, output, hash->len ) == 0 ); + TEST_ASSERT(mbedtls_sha3(family, in->x, in->len, output, hash->len) == 0); - ASSERT_COMPARE( output, hash->len, hash->x, hash->len ); + ASSERT_COMPARE(output, hash->len, hash->x, hash->len); exit: - mbedtls_free( output ); + mbedtls_free(output); } /* END_CASE */ /* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */ -void mbedtls_sha3_multi( int family, data_t *in, data_t *hash ) +void mbedtls_sha3_multi(int family, data_t *in, data_t *hash) { unsigned char *output = NULL; mbedtls_sha3_context ctx; const unsigned int block_size = 256; - ASSERT_ALLOC( output, hash->len ); + ASSERT_ALLOC(output, hash->len); - mbedtls_sha3_init( &ctx ); - mbedtls_sha3_starts( &ctx, family ); + mbedtls_sha3_init(&ctx); + mbedtls_sha3_starts(&ctx, family); - for( size_t l = 0; l < in->len; l += block_size ) - TEST_ASSERT( mbedtls_sha3_update( &ctx, in->x + l, MIN( in->len - l, block_size ) ) == 0 ); + for (size_t l = 0; l < in->len; l += block_size) { + TEST_ASSERT(mbedtls_sha3_update(&ctx, in->x + l, MIN(in->len - l, block_size)) == 0); + } - TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, hash->len ) == 0 ); + TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, hash->len) == 0); - ASSERT_COMPARE( output, hash->len, hash->x, hash->len ); + ASSERT_COMPARE(output, hash->len, hash->x, hash->len); exit: - mbedtls_free( output ); + mbedtls_free(output); } /* END_CASE */ /* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */ -void sha3_streaming( int type, data_t *input ) +void sha3_streaming(int type, data_t *input) { mbedtls_sha3_context ctx; unsigned char reference_hash[64]; unsigned char hash[64]; size_t chunk_size; - size_t hash_length = ( type == MBEDTLS_SHA3_224 ? 28 : - type == MBEDTLS_SHA3_256 ? 32 : - type == MBEDTLS_SHA3_384 ? 48 : - type == MBEDTLS_SHA3_512 ? 64 : - 0 ); + size_t hash_length = (type == MBEDTLS_SHA3_224 ? 28 : + type == MBEDTLS_SHA3_256 ? 32 : + type == MBEDTLS_SHA3_384 ? 48 : + type == MBEDTLS_SHA3_512 ? 64 : + 0); - mbedtls_sha3_init( &ctx ); - memset( reference_hash, 0, sizeof( reference_hash ) ); - memset( hash, 0, sizeof( hash ) ); - TEST_ASSERT( hash_length != 0 ); + mbedtls_sha3_init(&ctx); + memset(reference_hash, 0, sizeof(reference_hash)); + memset(hash, 0, sizeof(hash)); + TEST_ASSERT(hash_length != 0); /* Generate a reference hash */ - mbedtls_sha3( type, input->x, input->len, reference_hash, hash_length ); + mbedtls_sha3(type, input->x, input->len, reference_hash, hash_length); /* Repeat each test with increasingly-sized data chunks * E.g. start by processing bytes individual bytes, then 2-byte chunks, * then 3-byte chunks, and so on... * At each test ensure that the same hash is generated. */ - for( chunk_size = 1; chunk_size < input->len; chunk_size++ ) - { + for (chunk_size = 1; chunk_size < input->len; chunk_size++) { size_t i; size_t remaining = input->len; - mbedtls_sha3_init( &ctx ); - TEST_ASSERT( mbedtls_sha3_starts( &ctx, type ) == 0 ); + mbedtls_sha3_init(&ctx); + TEST_ASSERT(mbedtls_sha3_starts(&ctx, type) == 0); - for ( i = 0; i < input->len; i += chunk_size ) - { + for (i = 0; i < input->len; i += chunk_size) { size_t len = remaining >= chunk_size ? chunk_size : remaining; - TEST_ASSERT( mbedtls_sha3_update( &ctx, input->x + i, len ) == 0 ); + TEST_ASSERT(mbedtls_sha3_update(&ctx, input->x + i, len) == 0); remaining -= len; } - mbedtls_sha3_finish( &ctx, hash, hash_length ); - mbedtls_sha3_free( &ctx ); + mbedtls_sha3_finish(&ctx, hash, hash_length); + mbedtls_sha3_free(&ctx); - ASSERT_COMPARE( hash, hash_length, reference_hash, hash_length ); + ASSERT_COMPARE(hash, hash_length, reference_hash, hash_length); } exit: - mbedtls_sha3_free( &ctx ); + mbedtls_sha3_free(&ctx); } /* END_CASE */ /* BEGIN_CASE depends_on:MBEDTLS_SHA3_C */ -void sha3_reuse( data_t *input1, data_t *hash1, - data_t *input2, data_t *hash2 ) +void sha3_reuse(data_t *input1, data_t *hash1, + data_t *input2, data_t *hash2) { unsigned char output[64]; mbedtls_sha3_context ctx; mbedtls_sha3_id type1, type2; - mbedtls_sha3_init( &ctx ); - switch( hash1->len ) - { + mbedtls_sha3_init(&ctx); + switch (hash1->len) { case 28: type1 = MBEDTLS_SHA3_224; break; case 32: type1 = MBEDTLS_SHA3_256; break; case 48: type1 = MBEDTLS_SHA3_384; break; case 64: type1 = MBEDTLS_SHA3_512; break; - default: TEST_ASSERT( ! "hash1->len validity" ); break; + default: TEST_ASSERT(!"hash1->len validity"); break; } - switch( hash2->len ) - { + switch (hash2->len) { case 28: type2 = MBEDTLS_SHA3_224; break; case 32: type2 = MBEDTLS_SHA3_256; break; case 48: type2 = MBEDTLS_SHA3_384; break; case 64: type2 = MBEDTLS_SHA3_512; break; - default: TEST_ASSERT( ! "hash2->len validity" ); break; + default: TEST_ASSERT(!"hash2->len validity"); break; } /* Round 1 */ - TEST_ASSERT( mbedtls_sha3_starts( &ctx, type1 ) == 0 ); - TEST_ASSERT( mbedtls_sha3_update( &ctx, input1->x, input1->len ) == 0 ); - TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, sizeof( output ) ) == 0 ); - ASSERT_COMPARE( output, hash1->len, hash1->x, hash1->len ); + TEST_ASSERT(mbedtls_sha3_starts(&ctx, type1) == 0); + TEST_ASSERT(mbedtls_sha3_update(&ctx, input1->x, input1->len) == 0); + TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0); + ASSERT_COMPARE(output, hash1->len, hash1->x, hash1->len); /* Round 2 */ - TEST_ASSERT( mbedtls_sha3_starts( &ctx, type2 ) == 0 ); - TEST_ASSERT( mbedtls_sha3_update( &ctx, input2->x, input2->len ) == 0 ); - TEST_ASSERT( mbedtls_sha3_finish( &ctx, output, sizeof( output ) ) == 0 ); - ASSERT_COMPARE( output, hash2->len, hash2->x, hash2->len ); + TEST_ASSERT(mbedtls_sha3_starts(&ctx, type2) == 0); + TEST_ASSERT(mbedtls_sha3_update(&ctx, input2->x, input2->len) == 0); + TEST_ASSERT(mbedtls_sha3_finish(&ctx, output, sizeof(output)) == 0); + ASSERT_COMPARE(output, hash2->len, hash2->x, hash2->len); exit: - mbedtls_sha3_free( &ctx ); + mbedtls_sha3_free(&ctx); } /* END_CASE */ /* BEGIN_CASE depends_on:MBEDTLS_SHA3_C:MBEDTLS_SELF_TEST */ void sha3_selftest() { - TEST_ASSERT( mbedtls_sha3_self_test( 0 ) == 0 ); + TEST_ASSERT(mbedtls_sha3_self_test(0) == 0); } /* END_CASE */