blob: af47c8b579e87098df5831fd5f5009bc6af5d8d4 [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
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_HASH_H
22#define PSA_CRYPTO_HASH_H
23
24#include <psa/crypto.h>
Steven Cooreman830aff22021-03-09 09:50:44 +010025#include <psa/crypto_builtin_hash.h>
Steven Cooreman0e307642021-02-18 16:18:32 +010026
Steven Cooreman5f88e772021-03-15 11:07:12 +010027#include <mbedtls/md_internal.h>
28
29/** Get Mbed TLS MD information of a hash algorithm given its PSA identifier
30 *
31 * \param[in] alg PSA hash algorithm identifier
32 *
33 * \return The Mbed TLS MD information of the hash algorithm. \c NULL if the
34 * PSA hash algorithm is not supported.
35 */
36const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg );
37
Steven Cooreman0e307642021-02-18 16:18:32 +010038/** Calculate the hash (digest) of a message using Mbed TLS routines.
39 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010040 * \note The signature of this function is that of a PSA driver hash_compute
41 * entry point. This function behaves as a hash_compute entry point as
42 * defined in the PSA driver interface specification for transparent
43 * drivers.
44 *
Steven Cooreman0e307642021-02-18 16:18:32 +010045 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
46 * such that #PSA_ALG_IS_HASH(\p alg) is true).
47 * \param[in] input Buffer containing the message to hash.
48 * \param input_length Size of the \p input buffer in bytes.
49 * \param[out] hash Buffer where the hash is to be written.
50 * \param hash_size Size of the \p hash buffer in bytes.
51 * \param[out] hash_length On success, the number of bytes
52 * that make up the hash value. This is always
53 * #PSA_HASH_LENGTH(\p alg).
54 *
55 * \retval #PSA_SUCCESS
56 * Success.
57 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010058 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010059 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
60 * \p hash_size is too small
61 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Steven Cooreman0e307642021-02-18 16:18:32 +010062 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Steven Cooreman0e307642021-02-18 16:18:32 +010063 */
64psa_status_t mbedtls_psa_hash_compute(
65 psa_algorithm_t alg,
66 const uint8_t *input,
67 size_t input_length,
68 uint8_t *hash,
69 size_t hash_size,
70 size_t *hash_length);
71
72/** Set up a multipart hash operation using Mbed TLS routines.
73 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010074 * \note The signature of this function is that of a PSA driver hash_setup
75 * entry point. This function behaves as a hash_setup entry point as
76 * defined in the PSA driver interface specification for transparent
77 * drivers.
78 *
Steven Cooreman0e307642021-02-18 16:18:32 +010079 * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
80 * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
81 * core may call mbedtls_psa_hash_abort() at any time after the operation
82 * has been initialized.
83 *
84 * After a successful call to mbedtls_psa_hash_setup(), the core must
85 * eventually terminate the operation. The following events terminate an
86 * operation:
87 * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
88 * - A call to mbedtls_psa_hash_abort().
89 *
90 * \param[in,out] operation The operation object to set up. It must have
91 * been initialized to all-zero and not yet be in use.
92 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
93 * such that #PSA_ALG_IS_HASH(\p alg) is true).
94 *
95 * \retval #PSA_SUCCESS
96 * Success.
97 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010098 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010099 * \retval #PSA_ERROR_BAD_STATE
100 * The operation state is not valid (it must be inactive).
101 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
102 * \retval #PSA_ERROR_CORRUPTION_DETECTED
103 */
104psa_status_t mbedtls_psa_hash_setup(
105 mbedtls_psa_hash_operation_t *operation,
106 psa_algorithm_t alg );
107
108/** Clone an Mbed TLS hash operation.
109 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100110 * \note The signature of this function is that of a PSA driver hash_clone
111 * entry point. This function behaves as a hash_clone entry point as
112 * defined in the PSA driver interface specification for transparent
113 * drivers.
114 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100115 * This function copies the state of an ongoing hash operation to
116 * a new operation object. In other words, this function is equivalent
117 * to calling mbedtls_psa_hash_setup() on \p target_operation with the same
118 * algorithm that \p source_operation was set up for, then
119 * mbedtls_psa_hash_update() on \p target_operation with the same input that
120 * that was passed to \p source_operation. After this function returns, the
121 * two objects are independent, i.e. subsequent calls involving one of
122 * the objects do not affect the other object.
123 *
124 * \param[in] source_operation The active hash operation to clone.
125 * \param[in,out] target_operation The operation object to set up.
126 * It must be initialized but not active.
127 *
128 * \retval #PSA_SUCCESS
129 * \retval #PSA_ERROR_BAD_STATE
130 * The \p source_operation state is not valid (it must be active).
131 * \retval #PSA_ERROR_BAD_STATE
132 * The \p target_operation state is not valid (it must be inactive).
133 * \retval #PSA_ERROR_CORRUPTION_DETECTED
134 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
135 */
136psa_status_t mbedtls_psa_hash_clone(
137 const mbedtls_psa_hash_operation_t *source_operation,
138 mbedtls_psa_hash_operation_t *target_operation );
139
140/** Add a message fragment to a multipart Mbed TLS hash operation.
141 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100142 * \note The signature of this function is that of a PSA driver hash_update
143 * entry point. This function behaves as a hash_update entry point as
144 * defined in the PSA driver interface specification for transparent
145 * drivers.
146 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100147 * The application must call mbedtls_psa_hash_setup() before calling this function.
148 *
149 * If this function returns an error status, the operation enters an error
150 * state and must be aborted by calling mbedtls_psa_hash_abort().
151 *
152 * \param[in,out] operation Active hash operation.
153 * \param[in] input Buffer containing the message fragment to hash.
154 * \param input_length Size of the \p input buffer in bytes.
155 *
156 * \retval #PSA_SUCCESS
157 * Success.
158 * \retval #PSA_ERROR_BAD_STATE
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100159 * The operation state is not valid (it must be active).
Steven Cooreman0e307642021-02-18 16:18:32 +0100160 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
161 * \retval #PSA_ERROR_CORRUPTION_DETECTED
162 */
163psa_status_t mbedtls_psa_hash_update(
164 mbedtls_psa_hash_operation_t *operation,
165 const uint8_t *input,
166 size_t input_length );
167
168/** Finish the calculation of the Mbed TLS-calculated hash of a message.
169 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100170 * \note The signature of this function is that of a PSA driver hash_finish
171 * entry point. This function behaves as a hash_finish entry point as
172 * defined in the PSA driver interface specification for transparent
173 * drivers.
174 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100175 * The application must call mbedtls_psa_hash_setup() before calling this function.
176 * This function calculates the hash of the message formed by concatenating
177 * the inputs passed to preceding calls to mbedtls_psa_hash_update().
178 *
179 * When this function returns successfuly, the operation becomes inactive.
180 * If this function returns an error status, the operation enters an error
181 * state and must be aborted by calling mbedtls_psa_hash_abort().
182 *
183 * \param[in,out] operation Active hash operation.
184 * \param[out] hash Buffer where the hash is to be written.
185 * \param hash_size Size of the \p hash buffer in bytes.
186 * \param[out] hash_length On success, the number of bytes
187 * that make up the hash value. This is always
188 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
189 * hash algorithm that is calculated.
190 *
191 * \retval #PSA_SUCCESS
192 * Success.
193 * \retval #PSA_ERROR_BAD_STATE
194 * The operation state is not valid (it must be active).
195 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
196 * The size of the \p hash buffer is too small. You can determine a
197 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
198 * where \c alg is the hash algorithm that is calculated.
199 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
200 * \retval #PSA_ERROR_CORRUPTION_DETECTED
201 */
202psa_status_t mbedtls_psa_hash_finish(
203 mbedtls_psa_hash_operation_t *operation,
204 uint8_t *hash,
205 size_t hash_size,
206 size_t *hash_length );
207
208/** Abort an Mbed TLS hash operation.
209 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100210 * \note The signature of this function is that of a PSA driver hash_abort
211 * entry point. This function behaves as a hash_abort entry point as
212 * defined in the PSA driver interface specification for transparent
213 * drivers.
214 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100215 * Aborting an operation frees all associated resources except for the
216 * \p operation structure itself. Once aborted, the operation object
217 * can be reused for another operation by calling
218 * mbedtls_psa_hash_setup() again.
219 *
220 * You may call this function any time after the operation object has
221 * been initialized by one of the methods described in #psa_hash_operation_t.
222 *
223 * In particular, calling mbedtls_psa_hash_abort() after the operation has been
224 * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
225 * mbedtls_psa_hash_verify() is safe and has no effect.
226 *
227 * \param[in,out] operation Initialized hash operation.
228 *
229 * \retval #PSA_SUCCESS
230 * \retval #PSA_ERROR_CORRUPTION_DETECTED
231 */
232psa_status_t mbedtls_psa_hash_abort(
233 mbedtls_psa_hash_operation_t *operation );
234
Steven Cooremand029b602021-03-08 16:16:53 +0100235/*
236 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
237 */
238
239#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremand029b602021-03-08 16:16:53 +0100240
Steven Cooreman25555222021-03-08 16:20:04 +0100241psa_status_t mbedtls_transparent_test_driver_hash_compute(
Steven Cooremand029b602021-03-08 16:16:53 +0100242 psa_algorithm_t alg,
243 const uint8_t *input,
244 size_t input_length,
245 uint8_t *hash,
246 size_t hash_size,
247 size_t *hash_length);
248
Steven Cooreman25555222021-03-08 16:20:04 +0100249psa_status_t mbedtls_transparent_test_driver_hash_setup(
250 mbedtls_transparent_test_driver_hash_operation_t *operation,
Steven Cooremand029b602021-03-08 16:16:53 +0100251 psa_algorithm_t alg );
252
Steven Cooreman25555222021-03-08 16:20:04 +0100253psa_status_t mbedtls_transparent_test_driver_hash_clone(
254 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
255 mbedtls_transparent_test_driver_hash_operation_t *target_operation );
Steven Cooremand029b602021-03-08 16:16:53 +0100256
Steven Cooreman25555222021-03-08 16:20:04 +0100257psa_status_t mbedtls_transparent_test_driver_hash_update(
258 mbedtls_transparent_test_driver_hash_operation_t *operation,
Steven Cooremand029b602021-03-08 16:16:53 +0100259 const uint8_t *input,
260 size_t input_length );
261
Steven Cooreman25555222021-03-08 16:20:04 +0100262psa_status_t mbedtls_transparent_test_driver_hash_finish(
263 mbedtls_transparent_test_driver_hash_operation_t *operation,
Steven Cooremand029b602021-03-08 16:16:53 +0100264 uint8_t *hash,
265 size_t hash_size,
266 size_t *hash_length );
267
Steven Cooreman25555222021-03-08 16:20:04 +0100268psa_status_t mbedtls_transparent_test_driver_hash_abort(
269 mbedtls_transparent_test_driver_hash_operation_t *operation );
Steven Cooremand029b602021-03-08 16:16:53 +0100270
271#endif /* PSA_CRYPTO_DRIVER_TEST */
272
Steven Cooreman0e307642021-02-18 16:18:32 +0100273#endif /* PSA_CRYPTO_HASH_H */