Skip to content

Commit b37af92

Browse files
Merge branch 'psa-hash_clone' into psa-api-1.0-beta
Add psa_hash_clone.
2 parents 9dcc80e + ebb2c3e commit b37af92

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
@@ -1048,6 +1048,24 @@ psa_status_t psa_hash_verify(psa_hash_operation_t *operation,
10481048
*/
10491049
psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
10501050

1051+
/** Clone a hash operation.
1052+
*
1053+
* \param[in] source_operation The active hash operation to clone.
1054+
* \param[in,out] target_operation The operation object to set up.
1055+
* It must be initialized but not active.
1056+
*
1057+
* \retval #PSA_SUCCESS
1058+
* \retval #PSA_ERROR_BAD_STATE
1059+
* \p source_operation is not an active hash operation.
1060+
* \retval #PSA_ERROR_BAD_STATE
1061+
* \p source_operation is active.
1062+
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
1063+
* \retval #PSA_ERROR_HARDWARE_FAILURE
1064+
* \retval #PSA_ERROR_TAMPERING_DETECTED
1065+
*/
1066+
psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1067+
psa_hash_operation_t *target_operation);
1068+
10511069
/**@}*/
10521070

10531071
/** \defgroup MAC Message authentication codes

library/psa_crypto.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,67 @@ psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
15071507
return( PSA_SUCCESS );
15081508
}
15091509

1510+
psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1511+
psa_hash_operation_t *target_operation)
1512+
{
1513+
if( target_operation->alg != 0 )
1514+
return( PSA_ERROR_BAD_STATE );
1515+
1516+
switch( source_operation->alg )
1517+
{
1518+
case 0:
1519+
return( PSA_ERROR_BAD_STATE );
1520+
#if defined(MBEDTLS_MD2_C)
1521+
case PSA_ALG_MD2:
1522+
mbedtls_md2_clone( &target_operation->ctx.md2,
1523+
&source_operation->ctx.md2 );
1524+
break;
1525+
#endif
1526+
#if defined(MBEDTLS_MD4_C)
1527+
case PSA_ALG_MD4:
1528+
mbedtls_md4_clone( &target_operation->ctx.md4,
1529+
&source_operation->ctx.md4 );
1530+
break;
1531+
#endif
1532+
#if defined(MBEDTLS_MD5_C)
1533+
case PSA_ALG_MD5:
1534+
mbedtls_md5_clone( &target_operation->ctx.md5,
1535+
&source_operation->ctx.md5 );
1536+
break;
1537+
#endif
1538+
#if defined(MBEDTLS_RIPEMD160_C)
1539+
case PSA_ALG_RIPEMD160:
1540+
mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1541+
&source_operation->ctx.ripemd160 );
1542+
break;
1543+
#endif
1544+
#if defined(MBEDTLS_SHA1_C)
1545+
case PSA_ALG_SHA_1:
1546+
mbedtls_sha1_clone( &target_operation->ctx.sha1,
1547+
&source_operation->ctx.sha1 );
1548+
break;
1549+
#endif
1550+
#if defined(MBEDTLS_SHA256_C)
1551+
case PSA_ALG_SHA_224:
1552+
case PSA_ALG_SHA_256:
1553+
mbedtls_sha256_clone( &target_operation->ctx.sha256,
1554+
&source_operation->ctx.sha256 );
1555+
break;
1556+
#endif
1557+
#if defined(MBEDTLS_SHA512_C)
1558+
case PSA_ALG_SHA_384:
1559+
case PSA_ALG_SHA_512:
1560+
mbedtls_sha512_clone( &target_operation->ctx.sha512,
1561+
&source_operation->ctx.sha512 );
1562+
break;
1563+
#endif
1564+
default:
1565+
return( PSA_ERROR_NOT_SUPPORTED );
1566+
}
1567+
1568+
target_operation->alg = source_operation->alg;
1569+
return( PSA_SUCCESS );
1570+
}
15101571

15111572

15121573
/****************************************************************/

tests/suites/test_suite_psa_crypto.data

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,12 @@ hash_verify_bad_args:
544544
PSA hash finish: bad arguments
545545
hash_finish_bad_args:
546546

547+
PSA hash clone: source state
548+
hash_clone_source_state:
549+
550+
PSA hash clone: target state
551+
hash_clone_target_state:
552+
547553
MAC operation object initializers zero properly
548554
mac_operation_init:
549555

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)