Skip to content

Commit 0fb45d3

Browse files
committed
psa: Add initializers for hash operation objects
Add new initializers for hash operation objects and use them in our tests and library code. Prefer using the macro initializers due to their straightforwardness.
1 parent 9aa37e0 commit 0fb45d3

File tree

5 files changed

+76
-9
lines changed

5 files changed

+76
-9
lines changed

include/psa/crypto.h

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,18 +705,59 @@ psa_status_t psa_get_key_policy(psa_key_handle_t handle,
705705
*/
706706

707707
/** The type of the state data structure for multipart hash operations.
708+
*
709+
* Before calling any function on a hash operation object, the application must
710+
* initialize it by any of the following means:
711+
* - Set the structure to all-bits-zero, for example:
712+
* \code
713+
* psa_hash_operation_t operation;
714+
* memset(&operation, 0, sizeof(operation));
715+
* \endcode
716+
* - Initialize the structure to logical zero values, for example:
717+
* \code
718+
* psa_hash_operation_t operation = {0};
719+
* \endcode
720+
* - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
721+
* for example:
722+
* \code
723+
* psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
724+
* \endcode
725+
* - Assign the result of the function psa_hash_operation_init()
726+
* to the structure, for example:
727+
* \code
728+
* psa_hash_operation_t operation;
729+
* operation = psa_hash_operation_init();
730+
* \endcode
708731
*
709732
* This is an implementation-defined \c struct. Applications should not
710733
* make any assumptions about the content of this structure except
711734
* as directed by the documentation of a specific implementation. */
712735
typedef struct psa_hash_operation_s psa_hash_operation_t;
713736

737+
/** \def PSA_HASH_OPERATION_INIT
738+
*
739+
* This macro returns a suitable initializer for a hash operation object
740+
* of type #psa_hash_operation_t.
741+
*/
742+
#ifdef __DOXYGEN_ONLY__
743+
/* This is an example definition for documentation purposes.
744+
* Implementations should define a suitable value in `crypto_struct.h`.
745+
*/
746+
#define PSA_HASH_OPERATION_INIT {0}
747+
#endif
748+
749+
/** Return an initial value for a hash operation object.
750+
*/
751+
static psa_hash_operation_t psa_hash_operation_init(void);
752+
714753
/** Start a multipart hash operation.
715754
*
716755
* The sequence of operations to calculate a hash (message digest)
717756
* is as follows:
718757
* -# Allocate an operation object which will be passed to all the functions
719758
* listed here.
759+
* -# Initialize the operation object with one of the methods described in the
760+
* documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
720761
* -# Call psa_hash_setup() to specify the algorithm.
721762
* -# Call psa_hash_update() zero, one or more times, passing a fragment
722763
* of the message each time. The hash that is calculated is the hash
@@ -725,15 +766,17 @@ typedef struct psa_hash_operation_s psa_hash_operation_t;
725766
* To compare the hash with an expected value, call psa_hash_verify().
726767
*
727768
* The application may call psa_hash_abort() at any time after the operation
728-
* has been initialized with psa_hash_setup().
769+
* has been initialized.
729770
*
730771
* After a successful call to psa_hash_setup(), the application must
731772
* eventually terminate the operation. The following events terminate an
732773
* operation:
733774
* - A failed call to psa_hash_update().
734775
* - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
735776
*
736-
* \param[out] operation The operation object to use.
777+
* \param[in,out] operation The operation object to set up. It must have
778+
* been initialized as per the documentation for
779+
* #psa_hash_operation_t and not yet in use.
737780
* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
738781
* such that #PSA_ALG_IS_HASH(\p alg) is true).
739782
*

include/psa/crypto_struct.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ struct psa_hash_operation_s
8585
} ctx;
8686
};
8787

88+
#define PSA_HASH_OPERATION_INIT {0, {0}}
89+
static inline struct psa_hash_operation_s psa_hash_operation_init( void )
90+
{
91+
const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
92+
return( v );
93+
}
94+
8895
#if defined(MBEDTLS_MD_C)
8996
typedef struct
9097
{

tests/suites/test_suite_psa_crypto.data

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ PSA key policy: agreement, wrong algorithm
471471
depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ECDH_C
472472
agreement_key_policy:PSA_KEY_USAGE_DERIVE:PSA_ALG_ECDH(PSA_ALG_SELECT_RAW):PSA_KEY_TYPE_ECC_KEYPAIR(PSA_ECC_CURVE_SECP256R1):"49c9a8c18c4b885638c431cf1df1c994131609b580d4fd43a0cab17db2f13eee":PSA_ALG_FFDH(PSA_ALG_SELECT_RAW)
473473

474+
Hash operation object initializers zero properly
475+
hash_operation_init:
476+
474477
PSA hash setup: good, SHA-1
475478
depends_on:MBEDTLS_SHA1_C
476479
hash_setup:PSA_ALG_SHA_1:PSA_SUCCESS

tests/suites/test_suite_psa_crypto.function

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,13 +1776,27 @@ exit:
17761776
}
17771777
/* END_CASE */
17781778

1779+
/* BEGIN_CASE */
1780+
void hash_operation_init( )
1781+
{
1782+
psa_hash_operation_t func = psa_hash_operation_init( );
1783+
psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1784+
psa_hash_operation_t zero;
1785+
1786+
memset( &zero, 0, sizeof( zero ) );
1787+
1788+
TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1789+
TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1790+
}
1791+
/* END_CASE */
1792+
17791793
/* BEGIN_CASE */
17801794
void hash_setup( int alg_arg,
17811795
int expected_status_arg )
17821796
{
17831797
psa_algorithm_t alg = alg_arg;
17841798
psa_status_t expected_status = expected_status_arg;
1785-
psa_hash_operation_t operation;
1799+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
17861800
psa_status_t status;
17871801

17881802
PSA_ASSERT( psa_crypto_init( ) );
@@ -1806,7 +1820,7 @@ void hash_bad_order( )
18061820
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
18071821
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
18081822
size_t hash_len;
1809-
psa_hash_operation_t operation;
1823+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
18101824

18111825
PSA_ASSERT( psa_crypto_init( ) );
18121826

@@ -1842,7 +1856,7 @@ void hash_verify_bad_args( )
18421856
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
18431857
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
18441858
size_t expected_size = PSA_HASH_SIZE( alg );
1845-
psa_hash_operation_t operation;
1859+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
18461860

18471861
PSA_ASSERT( psa_crypto_init( ) );
18481862

@@ -1872,7 +1886,7 @@ void hash_finish_bad_args( )
18721886
psa_algorithm_t alg = PSA_ALG_SHA_256;
18731887
unsigned char hash[PSA_HASH_MAX_SIZE];
18741888
size_t expected_size = PSA_HASH_SIZE( alg );
1875-
psa_hash_operation_t operation;
1889+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
18761890
size_t hash_len;
18771891

18781892
PSA_ASSERT( psa_crypto_init( ) );

tests/suites/test_suite_psa_crypto_hash.function

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void hash_finish( int alg_arg, data_t *input, data_t *expected_hash )
2121
psa_algorithm_t alg = alg_arg;
2222
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
2323
size_t actual_hash_length;
24-
psa_hash_operation_t operation;
24+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2525

2626
PSA_ASSERT( psa_crypto_init( ) );
2727

@@ -43,7 +43,7 @@ exit:
4343
void hash_verify( int alg_arg, data_t *input, data_t *expected_hash )
4444
{
4545
psa_algorithm_t alg = alg_arg;
46-
psa_hash_operation_t operation;
46+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
4747

4848
PSA_ASSERT( psa_crypto_init( ) );
4949

@@ -66,7 +66,7 @@ void hash_multi_part( int alg_arg, data_t *input, data_t *expected_hash )
6666
psa_algorithm_t alg = alg_arg;
6767
unsigned char actual_hash[PSA_HASH_MAX_SIZE];
6868
size_t actual_hash_length;
69-
psa_hash_operation_t operation;
69+
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
7070
uint32_t len = 0;
7171

7272
PSA_ASSERT( psa_crypto_init( ) );

0 commit comments

Comments
 (0)