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