Skip to content

Commit 83912be

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

File tree

4 files changed

+80
-9
lines changed

4 files changed

+80
-9
lines changed

include/psa/crypto.h

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,12 +928,51 @@ psa_status_t psa_hash_abort(psa_hash_operation_t *operation);
928928
*/
929929

930930
/** The type of the state data structure for multipart MAC operations.
931+
*
932+
* Before calling any function on a MAC operation object, the application must
933+
* initialize it by any of the following means:
934+
* - Set the structure to all-bits-zero, for example:
935+
* \code
936+
* psa_mac_operation_t operation;
937+
* memset(&operation, 0, sizeof(operation));
938+
* \endcode
939+
* - Initialize the structure to logical zero values, for example:
940+
* \code
941+
* psa_mac_operation_t operation = {0};
942+
* \endcode
943+
* - Initialize the structure to the initializer #PSA_MAC_OPERATION_INIT,
944+
* for example:
945+
* \code
946+
* psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
947+
* \endcode
948+
* - Assign the result of the function psa_mac_operation_init()
949+
* to the structure, for example:
950+
* \code
951+
* psa_mac_operation_t operation;
952+
* operation = psa_mac_operation_init();
953+
* \endcode
931954
*
932955
* This is an implementation-defined \c struct. Applications should not
933956
* make any assumptions about the content of this structure except
934957
* as directed by the documentation of a specific implementation. */
935958
typedef struct psa_mac_operation_s psa_mac_operation_t;
936959

960+
/** \def PSA_MAC_OPERATION_INIT
961+
*
962+
* This macro returns a suitable initializer for a MAC operation object of type
963+
* #psa_mac_operation_t.
964+
*/
965+
#ifdef __DOXYGEN_ONLY__
966+
/* This is an example definition for documentation purposes.
967+
* Implementations should define a suitable value in `crypto_struct.h`.
968+
*/
969+
#define PSA_MAC_OPERATION_INIT {0}
970+
#endif
971+
972+
/** Return an initial value for a MAC operation object.
973+
*/
974+
static psa_mac_operation_t psa_mac_operation_init(void);
975+
937976
/** Start a multipart MAC calculation operation.
938977
*
939978
* This function sets up the calculation of the MAC
@@ -944,6 +983,8 @@ typedef struct psa_mac_operation_s psa_mac_operation_t;
944983
* The sequence of operations to calculate a MAC is as follows:
945984
* -# Allocate an operation object which will be passed to all the functions
946985
* listed here.
986+
* -# Initialize the operation object with one of the methods described in the
987+
* documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
947988
* -# Call psa_mac_sign_setup() to specify the algorithm and key.
948989
* The key remains associated with the operation even if the content
949990
* of the key slot changes.
@@ -954,14 +995,16 @@ typedef struct psa_mac_operation_s psa_mac_operation_t;
954995
* calculating the MAC value and retrieve it.
955996
*
956997
* The application may call psa_mac_abort() at any time after the operation
957-
* has been initialized with psa_mac_sign_setup().
998+
* has been initialized.
958999
*
9591000
* After a successful call to psa_mac_sign_setup(), the application must
9601001
* eventually terminate the operation through one of the following methods:
9611002
* - A failed call to psa_mac_update().
9621003
* - A call to psa_mac_sign_finish() or psa_mac_abort().
9631004
*
964-
* \param[out] operation The operation object to use.
1005+
* \param[in,out] operation The operation object to set up. It must have
1006+
* been initialized as per the documentation for
1007+
* #psa_mac_operation_t and not yet in use.
9651008
* \param handle Handle to the key to use for the operation.
9661009
* \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
9671010
* such that #PSA_ALG_IS_MAC(alg) is true).
@@ -996,6 +1039,8 @@ psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
9961039
* The sequence of operations to verify a MAC is as follows:
9971040
* -# Allocate an operation object which will be passed to all the functions
9981041
* listed here.
1042+
* -# Initialize the operation object with one of the methods described in the
1043+
* documentation for #psa_mac_operation_t, e.g. PSA_MAC_OPERATION_INIT.
9991044
* -# Call psa_mac_verify_setup() to specify the algorithm and key.
10001045
* The key remains associated with the operation even if the content
10011046
* of the key slot changes.
@@ -1007,14 +1052,16 @@ psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
10071052
* the expected value.
10081053
*
10091054
* The application may call psa_mac_abort() at any time after the operation
1010-
* has been initialized with psa_mac_verify_setup().
1055+
* has been initialized.
10111056
*
10121057
* After a successful call to psa_mac_verify_setup(), the application must
10131058
* eventually terminate the operation through one of the following methods:
10141059
* - A failed call to psa_mac_update().
10151060
* - A call to psa_mac_verify_finish() or psa_mac_abort().
10161061
*
1017-
* \param[out] operation The operation object to use.
1062+
* \param[in,out] operation The operation object to set up. It must have
1063+
* been initialized as per the documentation for
1064+
* #psa_mac_operation_t and not yet in use.
10181065
* \param handle Handle to the key to use for the operation.
10191066
* \param alg The MAC algorithm to compute (\c PSA_ALG_XXX value
10201067
* such that #PSA_ALG_IS_MAC(\p alg) is true).

include/psa/crypto_struct.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ struct psa_mac_operation_s
123123
} ctx;
124124
};
125125

126+
#define PSA_MAC_OPERATION_INIT {0, 0, 0, 0, 0, 0, 0, {0}}
127+
static inline struct psa_mac_operation_s psa_mac_operation_init( void )
128+
{
129+
const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
130+
return( v );
131+
}
132+
126133
struct psa_cipher_operation_s
127134
{
128135
psa_algorithm_t alg;

tests/suites/test_suite_psa_crypto.data

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

530+
MAC operation object initializers zero properly
531+
mac_operation_init:
532+
530533
PSA MAC setup: good, HMAC-SHA-256
531534
depends_on:MBEDTLS_MD_C:MBEDTLS_SHA256_C
532535
mac_setup:PSA_KEY_TYPE_HMAC:"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS

tests/suites/test_suite_psa_crypto.function

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static int exercise_mac_key( psa_key_handle_t handle,
124124
psa_key_usage_t usage,
125125
psa_algorithm_t alg )
126126
{
127-
psa_mac_operation_t operation;
127+
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
128128
const unsigned char input[] = "foo";
129129
unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
130130
size_t mac_length = sizeof( mac );
@@ -1434,7 +1434,7 @@ void mac_key_policy( int policy_usage,
14341434
{
14351435
psa_key_handle_t handle = 0;
14361436
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
1437-
psa_mac_operation_t operation;
1437+
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
14381438
psa_status_t status;
14391439
unsigned char mac[PSA_MAC_MAX_SIZE];
14401440

@@ -1902,6 +1902,20 @@ exit:
19021902
}
19031903
/* END_CASE */
19041904

1905+
/* BEGIN_CASE */
1906+
void mac_operation_init( )
1907+
{
1908+
psa_mac_operation_t func = psa_mac_operation_init( );
1909+
psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1910+
psa_mac_operation_t zero;
1911+
1912+
memset( &zero, 0, sizeof( zero ) );
1913+
1914+
TEST_EQUAL( memcmp( &func, &zero, sizeof( zero ) ), 0 );
1915+
TEST_EQUAL( memcmp( &init, &zero, sizeof( zero ) ), 0 );
1916+
}
1917+
/* END_CASE */
1918+
19051919
/* BEGIN_CASE */
19061920
void mac_setup( int key_type_arg,
19071921
data_t *key,
@@ -1912,7 +1926,7 @@ void mac_setup( int key_type_arg,
19121926
psa_key_type_t key_type = key_type_arg;
19131927
psa_algorithm_t alg = alg_arg;
19141928
psa_status_t expected_status = expected_status_arg;
1915-
psa_mac_operation_t operation;
1929+
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
19161930
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
19171931
psa_status_t status;
19181932

@@ -1948,7 +1962,7 @@ void mac_sign( int key_type_arg,
19481962
psa_key_handle_t handle = 0;
19491963
psa_key_type_t key_type = key_type_arg;
19501964
psa_algorithm_t alg = alg_arg;
1951-
psa_mac_operation_t operation;
1965+
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
19521966
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
19531967
/* Leave a little extra room in the output buffer. At the end of the
19541968
* test, we'll check that the implementation didn't overwrite onto
@@ -2005,7 +2019,7 @@ void mac_verify( int key_type_arg,
20052019
psa_key_handle_t handle = 0;
20062020
psa_key_type_t key_type = key_type_arg;
20072021
psa_algorithm_t alg = alg_arg;
2008-
psa_mac_operation_t operation;
2022+
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
20092023
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
20102024

20112025
TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );

0 commit comments

Comments
 (0)