Skip to content

Commit ebb2c3e

Browse files
New function psa_hash_clone
Clone a hash operation. Test good cases as part as multipart tests. Add new test functions for the state machine.
1 parent 8d4be19 commit ebb2c3e

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed

include/psa/crypto.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,24 @@ psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
921921
*/
922922
psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
923923

924+
/** Clone a hash operation.
925+
*
926+
* \param[in] source_operation The active hash operation to clone.
927+
* \param[in,out] target_operation The operation object to set up.
928+
* It must be initialized but not active.
929+
*
930+
* \retval #PSA_SUCCESS
931+
* \retval #PSA_ERROR_BAD_STATE
932+
* \p source_operation is not an active hash operation.
933+
* \retval #PSA_ERROR_BAD_STATE
934+
* \p source_operation is active.
935+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
936+
* \retval #PSA_ERROR_HARDWARE_FAILURE
937+
* \retval #PSA_ERROR_TAMPERING_DETECTED
938+
*/
939+
psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
940+
psa_hash_operation_t *target_operation);
941+
924942
/**@}*/
925943

926944
/** \defgroup MAC Message authentication codes

library/psa_crypto.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,67 @@ psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
14211421
return( PSA_SUCCESS );
14221422
}
14231423

1424+
psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1425+
psa_hash_operation_t *target_operation)
1426+
{
1427+
if( target_operation->alg != 0 )
1428+
return( PSA_ERROR_BAD_STATE );
1429+
1430+
switch( source_operation->alg )
1431+
{
1432+
case 0:
1433+
return( PSA_ERROR_BAD_STATE );
1434+
#if defined(MBEDTLS_MD2_C)
1435+
case PSA_ALG_MD2:
1436+
mbedtls_md2_clone( &target_operation->ctx.md2,
1437+
&source_operation->ctx.md2 );
1438+
break;
1439+
#endif
1440+
#if defined(MBEDTLS_MD4_C)
1441+
case PSA_ALG_MD4:
1442+
mbedtls_md4_clone( &target_operation->ctx.md4,
1443+
&source_operation->ctx.md4 );
1444+
break;
1445+
#endif
1446+
#if defined(MBEDTLS_MD5_C)
1447+
case PSA_ALG_MD5:
1448+
mbedtls_md5_clone( &target_operation->ctx.md5,
1449+
&source_operation->ctx.md5 );
1450+
break;
1451+
#endif
1452+
#if defined(MBEDTLS_RIPEMD160_C)
1453+
case PSA_ALG_RIPEMD160:
1454+
mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1455+
&source_operation->ctx.ripemd160 );
1456+
break;
1457+
#endif
1458+
#if defined(MBEDTLS_SHA1_C)
1459+
case PSA_ALG_SHA_1:
1460+
mbedtls_sha1_clone( &target_operation->ctx.sha1,
1461+
&source_operation->ctx.sha1 );
1462+
break;
1463+
#endif
1464+
#if defined(MBEDTLS_SHA256_C)
1465+
case PSA_ALG_SHA_224:
1466+
case PSA_ALG_SHA_256:
1467+
mbedtls_sha256_clone( &target_operation->ctx.sha256,
1468+
&source_operation->ctx.sha256 );
1469+
break;
1470+
#endif
1471+
#if defined(MBEDTLS_SHA512_C)
1472+
case PSA_ALG_SHA_384:
1473+
case PSA_ALG_SHA_512:
1474+
mbedtls_sha512_clone( &target_operation->ctx.sha512,
1475+
&source_operation->ctx.sha512 );
1476+
break;
1477+
#endif
1478+
default:
1479+
return( PSA_ERROR_NOT_SUPPORTED );
1480+
}
1481+
1482+
target_operation->alg = source_operation->alg;
1483+
return( PSA_SUCCESS );
1484+
}
14241485

14251486

14261487
/****************************************************************/

tests/suites/test_suite_psa_crypto.data

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,12 @@ hash_verify_bad_args:
527527
PSA hash finish: bad arguments
528528
hash_finish_bad_args:
529529

530+
PSA hash clone: source state
531+
hash_clone_source_state:
532+
533+
PSA hash clone: target state
534+
hash_clone_target_state:
535+
530536
MAC operation object initializers zero properly
531537
mac_operation_init:
532538

tests/suites/test_suite_psa_crypto.function

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,6 +1924,92 @@ exit:
19241924
}
19251925
/* END_CASE */
19261926

1927+
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1928+
void hash_clone_source_state( )
1929+
{
1930+
psa_algorithm_t alg = PSA_ALG_SHA_256;
1931+
unsigned char hash[PSA_HASH_MAX_SIZE];
1932+
psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1933+
psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1934+
psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1935+
psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1936+
psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1937+
size_t hash_len;
1938+
1939+
PSA_ASSERT( psa_crypto_init( ) );
1940+
PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1941+
1942+
PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1943+
PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1944+
PSA_ASSERT( psa_hash_finish( &op_finished,
1945+
hash, sizeof( hash ), &hash_len ) );
1946+
PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1947+
PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1948+
1949+
TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1950+
PSA_ERROR_BAD_STATE );
1951+
1952+
PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1953+
PSA_ASSERT( psa_hash_finish( &op_init,
1954+
hash, sizeof( hash ), &hash_len ) );
1955+
PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1956+
PSA_ASSERT( psa_hash_finish( &op_finished,
1957+
hash, sizeof( hash ), &hash_len ) );
1958+
PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1959+
PSA_ASSERT( psa_hash_finish( &op_aborted,
1960+
hash, sizeof( hash ), &hash_len ) );
1961+
1962+
exit:
1963+
psa_hash_abort( &op_source );
1964+
psa_hash_abort( &op_init );
1965+
psa_hash_abort( &op_setup );
1966+
psa_hash_abort( &op_finished );
1967+
psa_hash_abort( &op_aborted );
1968+
mbedtls_psa_crypto_free( );
1969+
}
1970+
/* END_CASE */
1971+
1972+
/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
1973+
void hash_clone_target_state( )
1974+
{
1975+
psa_algorithm_t alg = PSA_ALG_SHA_256;
1976+
unsigned char hash[PSA_HASH_MAX_SIZE];
1977+
psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1978+
psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1979+
psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1980+
psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1981+
psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1982+
size_t hash_len;
1983+
1984+
PSA_ASSERT( psa_crypto_init( ) );
1985+
1986+
PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1987+
PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1988+
PSA_ASSERT( psa_hash_finish( &op_finished,
1989+
hash, sizeof( hash ), &hash_len ) );
1990+
PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1991+
PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1992+
1993+
PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1994+
PSA_ASSERT( psa_hash_finish( &op_target,
1995+
hash, sizeof( hash ), &hash_len ) );
1996+
1997+
TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1998+
TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1999+
PSA_ERROR_BAD_STATE );
2000+
TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2001+
PSA_ERROR_BAD_STATE );
2002+
2003+
exit:
2004+
psa_hash_abort( &op_target );
2005+
psa_hash_abort( &op_init );
2006+
psa_hash_abort( &op_setup );
2007+
psa_hash_abort( &op_finished );
2008+
psa_hash_abort( &op_aborted );
2009+
mbedtls_psa_crypto_free( );
2010+
}
2011+
/* END_CASE */
2012+
19272013
/* BEGIN_CASE */
19282014
void mac_operation_init( )
19292015
{

tests/suites/test_suite_psa_crypto_hash.function

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void hash_multi_part( int alg_arg, data_t *input, data_t *expected_hash )
6767
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
6868
size_t actual_hash_length;
6969
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
70+
psa_hash_operation_t operation2 = PSA_HASH_OPERATION_INIT;
7071
uint32_t len = 0;
7172

7273
PSA_ASSERT( psa_crypto_init( ) );
@@ -78,16 +79,23 @@ void hash_multi_part( int alg_arg, data_t *input, data_t *expected_hash )
7879

7980
PSA_ASSERT( psa_hash_update( &operation,
8081
input->x, len ) );
82+
PSA_ASSERT( psa_hash_clone( &operation, &operation2 ) );
8183
PSA_ASSERT( psa_hash_update( &operation,
8284
input->x + len, input->len - len ) );
85+
PSA_ASSERT( psa_hash_update( &operation2,
86+
input->x + len, input->len - len ) );
8387

8488
PSA_ASSERT( psa_hash_finish( &operation,
8589
actual_hash, sizeof( actual_hash ),
8690
&actual_hash_length ) );
87-
8891
ASSERT_COMPARE( expected_hash->x, expected_hash->len,
8992
actual_hash, actual_hash_length );
9093

94+
PSA_ASSERT( psa_hash_finish( &operation2,
95+
actual_hash, sizeof( actual_hash ),
96+
&actual_hash_length ) );
97+
ASSERT_COMPARE( expected_hash->x, expected_hash->len,
98+
actual_hash, actual_hash_length );
9199
} while( len++ != input->len );
92100

93101
exit:

0 commit comments

Comments
 (0)