Skip to content

Commit 0a749c8

Browse files
Implement and test psa_hash_compute, psa_hash_compare
1 parent f712e16 commit 0a749c8

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

library/psa_crypto.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,6 +2351,58 @@ psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
23512351
return( PSA_SUCCESS );
23522352
}
23532353

2354+
psa_status_t psa_hash_compute( psa_algorithm_t alg,
2355+
const uint8_t *input, size_t input_length,
2356+
uint8_t *hash, size_t hash_size,
2357+
size_t *hash_length )
2358+
{
2359+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2360+
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2361+
2362+
*hash_length = hash_size;
2363+
status = psa_hash_setup( &operation, alg );
2364+
if( status != PSA_SUCCESS )
2365+
goto exit;
2366+
status = psa_hash_update( &operation, input, input_length );
2367+
if( status != PSA_SUCCESS )
2368+
goto exit;
2369+
status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2370+
if( status != PSA_SUCCESS )
2371+
goto exit;
2372+
2373+
exit:
2374+
if( status == PSA_SUCCESS )
2375+
status = psa_hash_abort( &operation );
2376+
else
2377+
psa_hash_abort( &operation );
2378+
return( status );
2379+
}
2380+
2381+
psa_status_t psa_hash_compare( psa_algorithm_t alg,
2382+
const uint8_t *input, size_t input_length,
2383+
const uint8_t *hash, size_t hash_length )
2384+
{
2385+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2386+
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2387+
2388+
status = psa_hash_setup( &operation, alg );
2389+
if( status != PSA_SUCCESS )
2390+
goto exit;
2391+
status = psa_hash_update( &operation, input, input_length );
2392+
if( status != PSA_SUCCESS )
2393+
goto exit;
2394+
status = psa_hash_verify( &operation, hash, hash_length );
2395+
if( status != PSA_SUCCESS )
2396+
goto exit;
2397+
2398+
exit:
2399+
if( status == PSA_SUCCESS )
2400+
status = psa_hash_abort( &operation );
2401+
else
2402+
psa_hash_abort( &operation );
2403+
return( status );
2404+
}
2405+
23542406
psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
23552407
psa_hash_operation_t *target_operation )
23562408
{

tests/suites/test_suite_psa_crypto.data

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,58 @@ hash_verify_bad_args:
774774
PSA hash finish: bad arguments
775775
hash_finish_bad_args:
776776

777+
PSA hash compute: bad algorithm (unknown hash)
778+
depends_on:MBEDTLS_SHA256_C
779+
hash_compute_fail:PSA_ALG_CATEGORY_HASH:"":32:PSA_ERROR_NOT_SUPPORTED
780+
781+
PSA hash compute: bad algorithm (wildcard)
782+
depends_on:MBEDTLS_SHA256_C
783+
hash_compute_fail:PSA_ALG_ANY_HASH:"":32:PSA_ERROR_NOT_SUPPORTED
784+
785+
PSA hash compute: bad algorithm (not a hash)
786+
depends_on:MBEDTLS_SHA256_C
787+
hash_compute_fail:PSA_ALG_HMAC(PSA_ALG_SHA_256):"":32:PSA_ERROR_INVALID_ARGUMENT
788+
789+
PSA hash compute: output buffer too small
790+
depends_on:MBEDTLS_SHA256_C
791+
hash_compute_fail:PSA_ALG_SHA_256:"":31:PSA_ERROR_BUFFER_TOO_SMALL
792+
793+
PSA hash compute: good, SHA-1
794+
depends_on:MBEDTLS_SHA1_C
795+
hash_compute_compare:PSA_ALG_SHA_1:"42749e":"a444319e9b6cc1e8464c511ec0969c37d6bb2619"
796+
797+
PSA hash compute: good, SHA-224
798+
depends_on:MBEDTLS_SHA256_C
799+
hash_compute_compare:PSA_ALG_SHA_224:"50efd0":"b5a9820413c2bf8211fbbf5df1337043b32fa4eafaf61a0c8e9ccede"
800+
801+
PSA hash compute: good, SHA-256
802+
depends_on:MBEDTLS_SHA256_C
803+
hash_compute_compare:PSA_ALG_SHA_256:"b0bd69":"4096804221093ddccfbf46831490ea63e9e99414858f8d75ff7f642c7ca61803"
804+
805+
PSA hash compute: good, SHA-384
806+
depends_on:MBEDTLS_SHA512_C:!MBEDTLS_SHA512_NO_SHA384
807+
hash_compute_compare:PSA_ALG_SHA_384:"31f5ca":"78d54b943421fdf7ba90a7fb9637c2073aa480454bd841d39ff72f4511fc21fb67797b652c0c823229342873d3bef955"
808+
809+
PSA hash compute: good, SHA-512
810+
depends_on:MBEDTLS_SHA512_C
811+
hash_compute_compare:PSA_ALG_SHA_512:"de4c90":"33ce98281045a5c4c9df0363d8196f1d7dfcd5ee46ac89776fd8a4344c12f123a66788af5bd41ceff1941aa5637654b4064c88c14e00465ab79a2fc6c97e1014"
812+
813+
PSA hash compute: good, MD2
814+
depends_on:MBEDTLS_MD2_C
815+
hash_compute_compare:PSA_ALG_MD2:"616263":"da853b0d3f88d99b30283a69e6ded6bb"
816+
817+
PSA hash compute: good, MD4
818+
depends_on:MBEDTLS_MD4_C
819+
hash_compute_compare:PSA_ALG_MD4:"616263":"a448017aaf21d8525fc10ae87aa6729d"
820+
821+
PSA hash compute: good, MD5
822+
depends_on:MBEDTLS_MD5_C
823+
hash_compute_compare:PSA_ALG_MD5:"616263":"900150983cd24fb0d6963f7d28e17f72"
824+
825+
PSA hash compute: good, RIPEMD160
826+
depends_on:MBEDTLS_RIPEMD160_C
827+
hash_compute_compare:PSA_ALG_RIPEMD160:"616263":"8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"
828+
777829
PSA hash clone: source state
778830
hash_clone_source_state:
779831

tests/suites/test_suite_psa_crypto.function

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,6 +2430,89 @@ exit:
24302430
}
24312431
/* END_CASE */
24322432

2433+
/* BEGIN_CASE */
2434+
void hash_compute_fail( int alg_arg, data_t *input,
2435+
int output_size_arg, int expected_status_arg )
2436+
{
2437+
psa_algorithm_t alg = alg_arg;
2438+
uint8_t *output = NULL;
2439+
size_t output_size = output_size_arg;
2440+
size_t output_length = INVALID_EXPORT_LENGTH;
2441+
psa_status_t expected_status = expected_status_arg;
2442+
psa_status_t status;
2443+
2444+
ASSERT_ALLOC( output, output_size );
2445+
2446+
PSA_ASSERT( psa_crypto_init( ) );
2447+
2448+
status = psa_hash_compute( alg, input->x, input->len,
2449+
output, output_size, &output_length );
2450+
TEST_EQUAL( status, expected_status );
2451+
TEST_ASSERT( output_length <= output_size );
2452+
2453+
exit:
2454+
mbedtls_free( output );
2455+
PSA_DONE( );
2456+
}
2457+
/* END_CASE */
2458+
2459+
/* BEGIN_CASE */
2460+
void hash_compute_compare( int alg_arg, data_t *input,
2461+
data_t *expected_output )
2462+
{
2463+
psa_algorithm_t alg = alg_arg;
2464+
uint8_t output[PSA_HASH_MAX_SIZE + 1];
2465+
size_t output_length = INVALID_EXPORT_LENGTH;
2466+
size_t i;
2467+
2468+
PSA_ASSERT( psa_crypto_init( ) );
2469+
2470+
/* Compute with tight buffer */
2471+
PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2472+
output, PSA_HASH_SIZE( alg ),
2473+
&output_length ) );
2474+
TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2475+
ASSERT_COMPARE( output, output_length,
2476+
expected_output->x, expected_output->len );
2477+
2478+
/* Compute with larger buffer */
2479+
PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2480+
output, sizeof( output ),
2481+
&output_length ) );
2482+
TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2483+
ASSERT_COMPARE( output, output_length,
2484+
expected_output->x, expected_output->len );
2485+
2486+
/* Compare with correct hash */
2487+
PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2488+
output, output_length ) );
2489+
2490+
/* Compare with trailing garbage */
2491+
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2492+
output, output_length + 1 ),
2493+
PSA_ERROR_INVALID_SIGNATURE );
2494+
2495+
/* Compare with truncated hash */
2496+
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2497+
output, output_length - 1 ),
2498+
PSA_ERROR_INVALID_SIGNATURE );
2499+
2500+
/* Compare with corrupted value */
2501+
for( i = 0; i < output_length; i++ )
2502+
{
2503+
test_set_step( i );
2504+
output[i] ^= 1;
2505+
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2506+
output, output_length ),
2507+
PSA_ERROR_INVALID_SIGNATURE );
2508+
output[i] ^= 1;
2509+
}
2510+
2511+
exit:
2512+
PSA_DONE( );
2513+
}
2514+
/* END_CASE */
2515+
24332516
/* BEGIN_CASE */
24342517
void hash_bad_order( )
24352518
{

0 commit comments

Comments
 (0)