1*62c56f98SSadaf Ebrahimi /* 2*62c56f98SSadaf Ebrahimi * PSA hashing layer on top of Mbed TLS software crypto 3*62c56f98SSadaf Ebrahimi */ 4*62c56f98SSadaf Ebrahimi /* 5*62c56f98SSadaf Ebrahimi * Copyright The Mbed TLS Contributors 6*62c56f98SSadaf Ebrahimi * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 7*62c56f98SSadaf Ebrahimi */ 8*62c56f98SSadaf Ebrahimi 9*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_HASH_H 10*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_HASH_H 11*62c56f98SSadaf Ebrahimi 12*62c56f98SSadaf Ebrahimi #include <psa/crypto.h> 13*62c56f98SSadaf Ebrahimi 14*62c56f98SSadaf Ebrahimi /** Calculate the hash (digest) of a message using Mbed TLS routines. 15*62c56f98SSadaf Ebrahimi * 16*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver hash_compute 17*62c56f98SSadaf Ebrahimi * entry point. This function behaves as a hash_compute entry point as 18*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 19*62c56f98SSadaf Ebrahimi * drivers. 20*62c56f98SSadaf Ebrahimi * 21*62c56f98SSadaf Ebrahimi * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value 22*62c56f98SSadaf Ebrahimi * such that #PSA_ALG_IS_HASH(\p alg) is true). 23*62c56f98SSadaf Ebrahimi * \param[in] input Buffer containing the message to hash. 24*62c56f98SSadaf Ebrahimi * \param input_length Size of the \p input buffer in bytes. 25*62c56f98SSadaf Ebrahimi * \param[out] hash Buffer where the hash is to be written. 26*62c56f98SSadaf Ebrahimi * \param hash_size Size of the \p hash buffer in bytes. 27*62c56f98SSadaf Ebrahimi * \param[out] hash_length On success, the number of bytes 28*62c56f98SSadaf Ebrahimi * that make up the hash value. This is always 29*62c56f98SSadaf Ebrahimi * #PSA_HASH_LENGTH(\p alg). 30*62c56f98SSadaf Ebrahimi * 31*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 32*62c56f98SSadaf Ebrahimi * Success. 33*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 34*62c56f98SSadaf Ebrahimi * \p alg is not supported 35*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 36*62c56f98SSadaf Ebrahimi * \p hash_size is too small 37*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 38*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 39*62c56f98SSadaf Ebrahimi */ 40*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_hash_compute( 41*62c56f98SSadaf Ebrahimi psa_algorithm_t alg, 42*62c56f98SSadaf Ebrahimi const uint8_t *input, 43*62c56f98SSadaf Ebrahimi size_t input_length, 44*62c56f98SSadaf Ebrahimi uint8_t *hash, 45*62c56f98SSadaf Ebrahimi size_t hash_size, 46*62c56f98SSadaf Ebrahimi size_t *hash_length); 47*62c56f98SSadaf Ebrahimi 48*62c56f98SSadaf Ebrahimi /** Set up a multipart hash operation using Mbed TLS routines. 49*62c56f98SSadaf Ebrahimi * 50*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver hash_setup 51*62c56f98SSadaf Ebrahimi * entry point. This function behaves as a hash_setup entry point as 52*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 53*62c56f98SSadaf Ebrahimi * drivers. 54*62c56f98SSadaf Ebrahimi * 55*62c56f98SSadaf Ebrahimi * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the 56*62c56f98SSadaf Ebrahimi * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The 57*62c56f98SSadaf Ebrahimi * core may call mbedtls_psa_hash_abort() at any time after the operation 58*62c56f98SSadaf Ebrahimi * has been initialized. 59*62c56f98SSadaf Ebrahimi * 60*62c56f98SSadaf Ebrahimi * After a successful call to mbedtls_psa_hash_setup(), the core must 61*62c56f98SSadaf Ebrahimi * eventually terminate the operation. The following events terminate an 62*62c56f98SSadaf Ebrahimi * operation: 63*62c56f98SSadaf Ebrahimi * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify(). 64*62c56f98SSadaf Ebrahimi * - A call to mbedtls_psa_hash_abort(). 65*62c56f98SSadaf Ebrahimi * 66*62c56f98SSadaf Ebrahimi * \param[in,out] operation The operation object to set up. It must have 67*62c56f98SSadaf Ebrahimi * been initialized to all-zero and not yet be in use. 68*62c56f98SSadaf Ebrahimi * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value 69*62c56f98SSadaf Ebrahimi * such that #PSA_ALG_IS_HASH(\p alg) is true). 70*62c56f98SSadaf Ebrahimi * 71*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 72*62c56f98SSadaf Ebrahimi * Success. 73*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_NOT_SUPPORTED 74*62c56f98SSadaf Ebrahimi * \p alg is not supported 75*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BAD_STATE 76*62c56f98SSadaf Ebrahimi * The operation state is not valid (it must be inactive). 77*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 78*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 79*62c56f98SSadaf Ebrahimi */ 80*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_hash_setup( 81*62c56f98SSadaf Ebrahimi mbedtls_psa_hash_operation_t *operation, 82*62c56f98SSadaf Ebrahimi psa_algorithm_t alg); 83*62c56f98SSadaf Ebrahimi 84*62c56f98SSadaf Ebrahimi /** Clone an Mbed TLS hash operation. 85*62c56f98SSadaf Ebrahimi * 86*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver hash_clone 87*62c56f98SSadaf Ebrahimi * entry point. This function behaves as a hash_clone entry point as 88*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 89*62c56f98SSadaf Ebrahimi * drivers. 90*62c56f98SSadaf Ebrahimi * 91*62c56f98SSadaf Ebrahimi * This function copies the state of an ongoing hash operation to 92*62c56f98SSadaf Ebrahimi * a new operation object. In other words, this function is equivalent 93*62c56f98SSadaf Ebrahimi * to calling mbedtls_psa_hash_setup() on \p target_operation with the same 94*62c56f98SSadaf Ebrahimi * algorithm that \p source_operation was set up for, then 95*62c56f98SSadaf Ebrahimi * mbedtls_psa_hash_update() on \p target_operation with the same input that 96*62c56f98SSadaf Ebrahimi * that was passed to \p source_operation. After this function returns, the 97*62c56f98SSadaf Ebrahimi * two objects are independent, i.e. subsequent calls involving one of 98*62c56f98SSadaf Ebrahimi * the objects do not affect the other object. 99*62c56f98SSadaf Ebrahimi * 100*62c56f98SSadaf Ebrahimi * \param[in] source_operation The active hash operation to clone. 101*62c56f98SSadaf Ebrahimi * \param[in,out] target_operation The operation object to set up. 102*62c56f98SSadaf Ebrahimi * It must be initialized but not active. 103*62c56f98SSadaf Ebrahimi * 104*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 105*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BAD_STATE 106*62c56f98SSadaf Ebrahimi * The \p source_operation state is not valid (it must be active). 107*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BAD_STATE 108*62c56f98SSadaf Ebrahimi * The \p target_operation state is not valid (it must be inactive). 109*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 110*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 111*62c56f98SSadaf Ebrahimi */ 112*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_hash_clone( 113*62c56f98SSadaf Ebrahimi const mbedtls_psa_hash_operation_t *source_operation, 114*62c56f98SSadaf Ebrahimi mbedtls_psa_hash_operation_t *target_operation); 115*62c56f98SSadaf Ebrahimi 116*62c56f98SSadaf Ebrahimi /** Add a message fragment to a multipart Mbed TLS hash operation. 117*62c56f98SSadaf Ebrahimi * 118*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver hash_update 119*62c56f98SSadaf Ebrahimi * entry point. This function behaves as a hash_update entry point as 120*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 121*62c56f98SSadaf Ebrahimi * drivers. 122*62c56f98SSadaf Ebrahimi * 123*62c56f98SSadaf Ebrahimi * The application must call mbedtls_psa_hash_setup() before calling this function. 124*62c56f98SSadaf Ebrahimi * 125*62c56f98SSadaf Ebrahimi * If this function returns an error status, the operation enters an error 126*62c56f98SSadaf Ebrahimi * state and must be aborted by calling mbedtls_psa_hash_abort(). 127*62c56f98SSadaf Ebrahimi * 128*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active hash operation. 129*62c56f98SSadaf Ebrahimi * \param[in] input Buffer containing the message fragment to hash. 130*62c56f98SSadaf Ebrahimi * \param input_length Size of the \p input buffer in bytes. 131*62c56f98SSadaf Ebrahimi * 132*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 133*62c56f98SSadaf Ebrahimi * Success. 134*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BAD_STATE 135*62c56f98SSadaf Ebrahimi * The operation state is not valid (it must be active). 136*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 137*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 138*62c56f98SSadaf Ebrahimi */ 139*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_hash_update( 140*62c56f98SSadaf Ebrahimi mbedtls_psa_hash_operation_t *operation, 141*62c56f98SSadaf Ebrahimi const uint8_t *input, 142*62c56f98SSadaf Ebrahimi size_t input_length); 143*62c56f98SSadaf Ebrahimi 144*62c56f98SSadaf Ebrahimi /** Finish the calculation of the Mbed TLS-calculated hash of a message. 145*62c56f98SSadaf Ebrahimi * 146*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver hash_finish 147*62c56f98SSadaf Ebrahimi * entry point. This function behaves as a hash_finish entry point as 148*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 149*62c56f98SSadaf Ebrahimi * drivers. 150*62c56f98SSadaf Ebrahimi * 151*62c56f98SSadaf Ebrahimi * The application must call mbedtls_psa_hash_setup() before calling this function. 152*62c56f98SSadaf Ebrahimi * This function calculates the hash of the message formed by concatenating 153*62c56f98SSadaf Ebrahimi * the inputs passed to preceding calls to mbedtls_psa_hash_update(). 154*62c56f98SSadaf Ebrahimi * 155*62c56f98SSadaf Ebrahimi * When this function returns successfully, the operation becomes inactive. 156*62c56f98SSadaf Ebrahimi * If this function returns an error status, the operation enters an error 157*62c56f98SSadaf Ebrahimi * state and must be aborted by calling mbedtls_psa_hash_abort(). 158*62c56f98SSadaf Ebrahimi * 159*62c56f98SSadaf Ebrahimi * \param[in,out] operation Active hash operation. 160*62c56f98SSadaf Ebrahimi * \param[out] hash Buffer where the hash is to be written. 161*62c56f98SSadaf Ebrahimi * \param hash_size Size of the \p hash buffer in bytes. 162*62c56f98SSadaf Ebrahimi * \param[out] hash_length On success, the number of bytes 163*62c56f98SSadaf Ebrahimi * that make up the hash value. This is always 164*62c56f98SSadaf Ebrahimi * #PSA_HASH_LENGTH(\c alg) where \c alg is the 165*62c56f98SSadaf Ebrahimi * hash algorithm that is calculated. 166*62c56f98SSadaf Ebrahimi * 167*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS 168*62c56f98SSadaf Ebrahimi * Success. 169*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BAD_STATE 170*62c56f98SSadaf Ebrahimi * The operation state is not valid (it must be active). 171*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_BUFFER_TOO_SMALL 172*62c56f98SSadaf Ebrahimi * The size of the \p hash buffer is too small. You can determine a 173*62c56f98SSadaf Ebrahimi * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg) 174*62c56f98SSadaf Ebrahimi * where \c alg is the hash algorithm that is calculated. 175*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription 176*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 177*62c56f98SSadaf Ebrahimi */ 178*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_hash_finish( 179*62c56f98SSadaf Ebrahimi mbedtls_psa_hash_operation_t *operation, 180*62c56f98SSadaf Ebrahimi uint8_t *hash, 181*62c56f98SSadaf Ebrahimi size_t hash_size, 182*62c56f98SSadaf Ebrahimi size_t *hash_length); 183*62c56f98SSadaf Ebrahimi 184*62c56f98SSadaf Ebrahimi /** Abort an Mbed TLS hash operation. 185*62c56f98SSadaf Ebrahimi * 186*62c56f98SSadaf Ebrahimi * \note The signature of this function is that of a PSA driver hash_abort 187*62c56f98SSadaf Ebrahimi * entry point. This function behaves as a hash_abort entry point as 188*62c56f98SSadaf Ebrahimi * defined in the PSA driver interface specification for transparent 189*62c56f98SSadaf Ebrahimi * drivers. 190*62c56f98SSadaf Ebrahimi * 191*62c56f98SSadaf Ebrahimi * Aborting an operation frees all associated resources except for the 192*62c56f98SSadaf Ebrahimi * \p operation structure itself. Once aborted, the operation object 193*62c56f98SSadaf Ebrahimi * can be reused for another operation by calling 194*62c56f98SSadaf Ebrahimi * mbedtls_psa_hash_setup() again. 195*62c56f98SSadaf Ebrahimi * 196*62c56f98SSadaf Ebrahimi * You may call this function any time after the operation object has 197*62c56f98SSadaf Ebrahimi * been initialized by one of the methods described in #psa_hash_operation_t. 198*62c56f98SSadaf Ebrahimi * 199*62c56f98SSadaf Ebrahimi * In particular, calling mbedtls_psa_hash_abort() after the operation has been 200*62c56f98SSadaf Ebrahimi * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or 201*62c56f98SSadaf Ebrahimi * mbedtls_psa_hash_verify() is safe and has no effect. 202*62c56f98SSadaf Ebrahimi * 203*62c56f98SSadaf Ebrahimi * \param[in,out] operation Initialized hash operation. 204*62c56f98SSadaf Ebrahimi * 205*62c56f98SSadaf Ebrahimi * \retval #PSA_SUCCESS \emptydescription 206*62c56f98SSadaf Ebrahimi * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription 207*62c56f98SSadaf Ebrahimi */ 208*62c56f98SSadaf Ebrahimi psa_status_t mbedtls_psa_hash_abort( 209*62c56f98SSadaf Ebrahimi mbedtls_psa_hash_operation_t *operation); 210*62c56f98SSadaf Ebrahimi 211*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_HASH_H */ 212