Skip to content

Commit 9df33ef

Browse files
Implement and test psa_hash_compute, psa_hash_verify
1 parent 06c2889 commit 9df33ef

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

library/psa_crypto.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,58 @@ psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
23422342
return( PSA_SUCCESS );
23432343
}
23442344

2345+
psa_status_t psa_hash_compute( psa_algorithm_t alg,
2346+
const uint8_t *input, size_t input_length,
2347+
uint8_t *hash, size_t hash_size,
2348+
size_t *hash_length )
2349+
{
2350+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2351+
psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
2352+
2353+
*hash_length = hash_size;
2354+
status = psa_hash_setup( &operation, alg );
2355+
if( status != PSA_SUCCESS )
2356+
goto exit;
2357+
status = psa_hash_update( &operation, input, input_length );
2358+
if( status != PSA_SUCCESS )
2359+
goto exit;
2360+
status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2361+
if( status != PSA_SUCCESS )
2362+
goto exit;
2363+
2364+
exit:
2365+
if( status == PSA_SUCCESS )
2366+
status = psa_hash_abort( &operation );
2367+
else
2368+
psa_hash_abort( &operation );
2369+
return( status );
2370+
}
2371+
2372+
psa_status_t psa_hash_compare( psa_algorithm_t alg,
2373+
const uint8_t *input, size_t input_length,
2374+
const uint8_t *hash, size_t hash_length )
2375+
{
2376+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2377+
psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
2378+
2379+
status = psa_hash_setup( &operation, alg );
2380+
if( status != PSA_SUCCESS )
2381+
goto exit;
2382+
status = psa_hash_update( &operation, input, input_length );
2383+
if( status != PSA_SUCCESS )
2384+
goto exit;
2385+
status = psa_hash_verify( &operation, hash, hash_length );
2386+
if( status != PSA_SUCCESS )
2387+
goto exit;
2388+
2389+
exit:
2390+
if( status == PSA_SUCCESS )
2391+
status = psa_hash_abort( &operation );
2392+
else
2393+
psa_hash_abort( &operation );
2394+
return( status );
2395+
}
2396+
23452397
psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
23462398
psa_hash_operation_t *target_operation )
23472399
{

tests/suites/test_suite_psa_crypto.data

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,57 @@ hash_verify_bad_args:
766766
PSA hash finish: bad arguments
767767
hash_finish_bad_args:
768768

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

tests/suites/test_suite_psa_crypto.function

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,89 @@ exit:
23472347
}
23482348
/* END_CASE */
23492349

2350+
/* BEGIN_CASE */
2351+
void hash_compute_fail( int alg_arg, data_t *input,
2352+
int output_size_arg, int expected_status_arg )
2353+
{
2354+
psa_algorithm_t alg = alg_arg;
2355+
uint8_t *output = NULL;
2356+
size_t output_size = output_size_arg;
2357+
size_t output_length = INVALID_EXPORT_LENGTH;
2358+
psa_status_t expected_status = expected_status_arg;
2359+
psa_status_t status;
2360+
2361+
ASSERT_ALLOC( output, output_size );
2362+
2363+
PSA_ASSERT( psa_crypto_init( ) );
2364+
2365+
status = psa_hash_compute( alg, input->x, input->len,
2366+
output, output_size, &output_length );
2367+
TEST_EQUAL( status, expected_status );
2368+
TEST_ASSERT( output_length <= output_size );
2369+
2370+
exit:
2371+
mbedtls_free( output );
2372+
PSA_DONE( );
2373+
}
2374+
/* END_CASE */
2375+
2376+
/* BEGIN_CASE */
2377+
void hash_compute_verify( int alg_arg, data_t *input,
2378+
data_t *expected_output )
2379+
{
2380+
psa_algorithm_t alg = alg_arg;
2381+
uint8_t output[PSA_HASH_MAX_SIZE + 1];
2382+
size_t output_length = INVALID_EXPORT_LENGTH;
2383+
size_t i;
2384+
2385+
PSA_ASSERT( psa_crypto_init( ) );
2386+
2387+
/* Compute with tight buffer */
2388+
PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2389+
output, PSA_HASH_SIZE( alg ),
2390+
&output_length ) );
2391+
TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2392+
ASSERT_COMPARE( output, output_length,
2393+
expected_output->x, expected_output->len );
2394+
2395+
/* Compute with larger buffer */
2396+
PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2397+
output, sizeof( output ),
2398+
&output_length ) );
2399+
TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2400+
ASSERT_COMPARE( output, output_length,
2401+
expected_output->x, expected_output->len );
2402+
2403+
/* Compare with correct hash */
2404+
PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2405+
output, output_length ) );
2406+
2407+
/* Compare with trailing garbage */
2408+
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2409+
output, output_length + 1 ),
2410+
PSA_ERROR_INVALID_SIGNATURE );
2411+
2412+
/* Compare with truncated hash */
2413+
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2414+
output, output_length - 1 ),
2415+
PSA_ERROR_INVALID_SIGNATURE );
2416+
2417+
/* Compare with corrupted value */
2418+
for( i = 0; i < output_length; i++ )
2419+
{
2420+
test_set_step( i );
2421+
output[i] ^= 1;
2422+
TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2423+
output, output_length ),
2424+
PSA_ERROR_INVALID_SIGNATURE );
2425+
output[i] ^= 1;
2426+
}
2427+
2428+
exit:
2429+
PSA_DONE( );
2430+
}
2431+
/* END_CASE */
2432+
23502433
/* BEGIN_CASE */
23512434
void hash_bad_order( )
23522435
{

0 commit comments

Comments
 (0)