blob: 0a7be805520192845de777acaf3e4766f7c839b2 [file] [log] [blame]
Steven Cooreman0e307642021-02-18 16:18:32 +01001/*
2 * PSA hashing layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Steven Cooreman0e307642021-02-18 16:18:32 +01007 */
8
9#ifndef PSA_CRYPTO_HASH_H
10#define PSA_CRYPTO_HASH_H
11
12#include <psa/crypto.h>
Steven Cooreman0e307642021-02-18 16:18:32 +010013
14/** Calculate the hash (digest) of a message using Mbed TLS routines.
15 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010016 * \note The signature of this function is that of a PSA driver hash_compute
17 * entry point. This function behaves as a hash_compute entry point as
18 * defined in the PSA driver interface specification for transparent
19 * drivers.
20 *
Steven Cooreman0e307642021-02-18 16:18:32 +010021 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
22 * such that #PSA_ALG_IS_HASH(\p alg) is true).
23 * \param[in] input Buffer containing the message to hash.
24 * \param input_length Size of the \p input buffer in bytes.
25 * \param[out] hash Buffer where the hash is to be written.
26 * \param hash_size Size of the \p hash buffer in bytes.
27 * \param[out] hash_length On success, the number of bytes
28 * that make up the hash value. This is always
29 * #PSA_HASH_LENGTH(\p alg).
30 *
31 * \retval #PSA_SUCCESS
32 * Success.
33 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010034 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010035 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
36 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +010037 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
38 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +010039 */
40psa_status_t mbedtls_psa_hash_compute(
41 psa_algorithm_t alg,
42 const uint8_t *input,
43 size_t input_length,
44 uint8_t *hash,
45 size_t hash_size,
46 size_t *hash_length);
47
48/** Set up a multipart hash operation using Mbed TLS routines.
49 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010050 * \note The signature of this function is that of a PSA driver hash_setup
51 * entry point. This function behaves as a hash_setup entry point as
52 * defined in the PSA driver interface specification for transparent
53 * drivers.
54 *
Steven Cooreman0e307642021-02-18 16:18:32 +010055 * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
56 * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
57 * core may call mbedtls_psa_hash_abort() at any time after the operation
58 * has been initialized.
59 *
60 * After a successful call to mbedtls_psa_hash_setup(), the core must
61 * eventually terminate the operation. The following events terminate an
62 * operation:
63 * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
64 * - A call to mbedtls_psa_hash_abort().
65 *
66 * \param[in,out] operation The operation object to set up. It must have
67 * been initialized to all-zero and not yet be in use.
68 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
69 * such that #PSA_ALG_IS_HASH(\p alg) is true).
70 *
71 * \retval #PSA_SUCCESS
72 * Success.
73 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010074 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010075 * \retval #PSA_ERROR_BAD_STATE
76 * The operation state is not valid (it must be inactive).
Gilles Peskineed733552023-02-14 19:21:09 +010077 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
78 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +010079 */
80psa_status_t mbedtls_psa_hash_setup(
81 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +010082 psa_algorithm_t alg);
Steven Cooreman0e307642021-02-18 16:18:32 +010083
84/** Clone an Mbed TLS hash operation.
85 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010086 * \note The signature of this function is that of a PSA driver hash_clone
87 * entry point. This function behaves as a hash_clone entry point as
88 * defined in the PSA driver interface specification for transparent
89 * drivers.
90 *
Steven Cooreman0e307642021-02-18 16:18:32 +010091 * This function copies the state of an ongoing hash operation to
92 * a new operation object. In other words, this function is equivalent
93 * to calling mbedtls_psa_hash_setup() on \p target_operation with the same
94 * algorithm that \p source_operation was set up for, then
95 * mbedtls_psa_hash_update() on \p target_operation with the same input that
96 * that was passed to \p source_operation. After this function returns, the
97 * two objects are independent, i.e. subsequent calls involving one of
98 * the objects do not affect the other object.
99 *
100 * \param[in] source_operation The active hash operation to clone.
101 * \param[in,out] target_operation The operation object to set up.
102 * It must be initialized but not active.
103 *
Gilles Peskineed733552023-02-14 19:21:09 +0100104 * \retval #PSA_SUCCESS \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100105 * \retval #PSA_ERROR_BAD_STATE
106 * The \p source_operation state is not valid (it must be active).
107 * \retval #PSA_ERROR_BAD_STATE
108 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineed733552023-02-14 19:21:09 +0100109 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
110 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100111 */
112psa_status_t mbedtls_psa_hash_clone(
113 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_psa_hash_operation_t *target_operation);
Steven Cooreman0e307642021-02-18 16:18:32 +0100115
116/** Add a message fragment to a multipart Mbed TLS hash operation.
117 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100118 * \note The signature of this function is that of a PSA driver hash_update
119 * entry point. This function behaves as a hash_update entry point as
120 * defined in the PSA driver interface specification for transparent
121 * drivers.
122 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100123 * The application must call mbedtls_psa_hash_setup() before calling this function.
124 *
125 * If this function returns an error status, the operation enters an error
126 * state and must be aborted by calling mbedtls_psa_hash_abort().
127 *
128 * \param[in,out] operation Active hash operation.
129 * \param[in] input Buffer containing the message fragment to hash.
130 * \param input_length Size of the \p input buffer in bytes.
131 *
132 * \retval #PSA_SUCCESS
133 * Success.
134 * \retval #PSA_ERROR_BAD_STATE
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100135 * The operation state is not valid (it must be active).
Gilles Peskineed733552023-02-14 19:21:09 +0100136 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
137 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100138 */
139psa_status_t mbedtls_psa_hash_update(
140 mbedtls_psa_hash_operation_t *operation,
141 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 size_t input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100143
144/** Finish the calculation of the Mbed TLS-calculated hash of a message.
145 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100146 * \note The signature of this function is that of a PSA driver hash_finish
147 * entry point. This function behaves as a hash_finish entry point as
148 * defined in the PSA driver interface specification for transparent
149 * drivers.
150 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100151 * The application must call mbedtls_psa_hash_setup() before calling this function.
152 * This function calculates the hash of the message formed by concatenating
153 * the inputs passed to preceding calls to mbedtls_psa_hash_update().
154 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800155 * When this function returns successfully, the operation becomes inactive.
Steven Cooreman0e307642021-02-18 16:18:32 +0100156 * If this function returns an error status, the operation enters an error
157 * state and must be aborted by calling mbedtls_psa_hash_abort().
158 *
159 * \param[in,out] operation Active hash operation.
160 * \param[out] hash Buffer where the hash is to be written.
161 * \param hash_size Size of the \p hash buffer in bytes.
162 * \param[out] hash_length On success, the number of bytes
163 * that make up the hash value. This is always
164 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
165 * hash algorithm that is calculated.
166 *
167 * \retval #PSA_SUCCESS
168 * Success.
169 * \retval #PSA_ERROR_BAD_STATE
170 * The operation state is not valid (it must be active).
171 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
172 * The size of the \p hash buffer is too small. You can determine a
173 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
174 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +0100175 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
176 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100177 */
178psa_status_t mbedtls_psa_hash_finish(
179 mbedtls_psa_hash_operation_t *operation,
180 uint8_t *hash,
181 size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 size_t *hash_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100183
184/** Abort an Mbed TLS hash operation.
185 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100186 * \note The signature of this function is that of a PSA driver hash_abort
187 * entry point. This function behaves as a hash_abort entry point as
188 * defined in the PSA driver interface specification for transparent
189 * drivers.
190 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100191 * Aborting an operation frees all associated resources except for the
192 * \p operation structure itself. Once aborted, the operation object
193 * can be reused for another operation by calling
194 * mbedtls_psa_hash_setup() again.
195 *
196 * You may call this function any time after the operation object has
197 * been initialized by one of the methods described in #psa_hash_operation_t.
198 *
199 * In particular, calling mbedtls_psa_hash_abort() after the operation has been
200 * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
201 * mbedtls_psa_hash_verify() is safe and has no effect.
202 *
203 * \param[in,out] operation Initialized hash operation.
204 *
Gilles Peskineed733552023-02-14 19:21:09 +0100205 * \retval #PSA_SUCCESS \emptydescription
206 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100207 */
208psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_psa_hash_operation_t *operation);
Steven Cooreman0e307642021-02-18 16:18:32 +0100210
Steven Cooreman0e307642021-02-18 16:18:32 +0100211#endif /* PSA_CRYPTO_HASH_H */