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.
This commit is contained in:
Jaeden Amero 2019-01-04 11:47:44 +00:00 committed by Jaeden Amero
parent 70261c513a
commit 6a25b41ac3
5 changed files with 87 additions and 9 deletions

View file

@ -705,18 +705,59 @@ psa_status_t psa_get_key_policy(psa_key_handle_t handle,
*/
/** The type of the state data structure for multipart hash operations.
*
* Before calling any function on a hash operation object, the application must
* initialize it by any of the following means:
* - Set the structure to all-bits-zero, for example:
* \code
* psa_hash_operation_t operation;
* memset(&operation, 0, sizeof(operation));
* \endcode
* - Initialize the structure to logical zero values, for example:
* \code
* psa_hash_operation_t operation = {0};
* \endcode
* - Initialize the structure to the initializer #PSA_HASH_OPERATION_INIT,
* for example:
* \code
* psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
* \endcode
* - Assign the result of the function psa_hash_operation_init()
* to the structure, for example:
* \code
* psa_hash_operation_t operation;
* operation = psa_hash_operation_init();
* \endcode
*
* This is an implementation-defined \c struct. Applications should not
* make any assumptions about the content of this structure except
* as directed by the documentation of a specific implementation. */
typedef struct psa_hash_operation_s psa_hash_operation_t;
/** \def PSA_HASH_OPERATION_INIT
*
* This macro returns a suitable initializer for a hash operation object
* of type #psa_hash_operation_t.
*/
#ifdef __DOXYGEN_ONLY__
/* This is an example definition for documentation purposes.
* Implementations should define a suitable value in `crypto_struct.h`.
*/
#define PSA_HASH_OPERATION_INIT {0}
#endif
/** Return an initial value for a hash operation object.
*/
static psa_hash_operation_t psa_hash_operation_init(void);
/** Start a multipart hash operation.
*
* The sequence of operations to calculate a hash (message digest)
* is as follows:
* -# Allocate an operation object which will be passed to all the functions
* listed here.
* -# Initialize the operation object with one of the methods described in the
* documentation for #psa_hash_operation_t, e.g. PSA_HASH_OPERATION_INIT.
* -# Call psa_hash_setup() to specify the algorithm.
* -# Call psa_hash_update() zero, one or more times, passing a fragment
* of the message each time. The hash that is calculated is the hash
@ -725,7 +766,7 @@ typedef struct psa_hash_operation_s psa_hash_operation_t;
* To compare the hash with an expected value, call psa_hash_verify().
*
* The application may call psa_hash_abort() at any time after the operation
* has been initialized with psa_hash_setup().
* has been initialized.
*
* After a successful call to psa_hash_setup(), the application must
* eventually terminate the operation. The following events terminate an
@ -733,7 +774,9 @@ typedef struct psa_hash_operation_s psa_hash_operation_t;
* - A failed call to psa_hash_update().
* - A call to psa_hash_finish(), psa_hash_verify() or psa_hash_abort().
*
* \param[out] operation The operation object to use.
* \param[in,out] operation The operation object to set up. It must have
* been initialized as per the documentation for
* #psa_hash_operation_t and not yet in use.
* \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
* such that #PSA_ALG_IS_HASH(\p alg) is true).
*

View file

@ -85,6 +85,13 @@ struct psa_hash_operation_s
} ctx;
};
#define PSA_HASH_OPERATION_INIT {0, {0}}
static inline struct psa_hash_operation_s psa_hash_operation_init( void )
{
const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
return( v );
}
#if defined(MBEDTLS_MD_C)
typedef struct
{