blob: 443110eaee400e8bcef527ac46738a1783811b2d [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
27/** Calculate the hash (digest) of a message using Mbed TLS routines.
28 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010029 * \note The signature of this function is that of a PSA driver hash_compute
30 * entry point. This function behaves as a hash_compute entry point as
31 * defined in the PSA driver interface specification for transparent
32 * drivers.
33 *
Steven Cooreman0e307642021-02-18 16:18:32 +010034 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
35 * such that #PSA_ALG_IS_HASH(\p alg) is true).
36 * \param[in] input Buffer containing the message to hash.
37 * \param input_length Size of the \p input buffer in bytes.
38 * \param[out] hash Buffer where the hash is to be written.
39 * \param hash_size Size of the \p hash buffer in bytes.
40 * \param[out] hash_length On success, the number of bytes
41 * that make up the hash value. This is always
42 * #PSA_HASH_LENGTH(\p alg).
43 *
44 * \retval #PSA_SUCCESS
45 * Success.
46 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010047 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010048 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
49 * \p hash_size is too small
50 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
Steven Cooreman0e307642021-02-18 16:18:32 +010051 * \retval #PSA_ERROR_CORRUPTION_DETECTED
Steven Cooreman0e307642021-02-18 16:18:32 +010052 */
53psa_status_t mbedtls_psa_hash_compute(
54 psa_algorithm_t alg,
55 const uint8_t *input,
56 size_t input_length,
57 uint8_t *hash,
58 size_t hash_size,
59 size_t *hash_length);
60
61/** Set up a multipart hash operation using Mbed TLS routines.
62 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010063 * \note The signature of this function is that of a PSA driver hash_setup
64 * entry point. This function behaves as a hash_setup entry point as
65 * defined in the PSA driver interface specification for transparent
66 * drivers.
67 *
Steven Cooreman0e307642021-02-18 16:18:32 +010068 * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
69 * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
70 * core may call mbedtls_psa_hash_abort() at any time after the operation
71 * has been initialized.
72 *
73 * After a successful call to mbedtls_psa_hash_setup(), the core must
74 * eventually terminate the operation. The following events terminate an
75 * operation:
76 * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
77 * - A call to mbedtls_psa_hash_abort().
78 *
79 * \param[in,out] operation The operation object to set up. It must have
80 * been initialized to all-zero and not yet be in use.
81 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
82 * such that #PSA_ALG_IS_HASH(\p alg) is true).
83 *
84 * \retval #PSA_SUCCESS
85 * Success.
86 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010087 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010088 * \retval #PSA_ERROR_BAD_STATE
89 * The operation state is not valid (it must be inactive).
90 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
91 * \retval #PSA_ERROR_CORRUPTION_DETECTED
92 */
93psa_status_t mbedtls_psa_hash_setup(
94 mbedtls_psa_hash_operation_t *operation,
95 psa_algorithm_t alg );
96
97/** Clone an Mbed TLS hash operation.
98 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010099 * \note The signature of this function is that of a PSA driver hash_clone
100 * entry point. This function behaves as a hash_clone entry point as
101 * defined in the PSA driver interface specification for transparent
102 * drivers.
103 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100104 * This function copies the state of an ongoing hash operation to
105 * a new operation object. In other words, this function is equivalent
106 * to calling mbedtls_psa_hash_setup() on \p target_operation with the same
107 * algorithm that \p source_operation was set up for, then
108 * mbedtls_psa_hash_update() on \p target_operation with the same input that
109 * that was passed to \p source_operation. After this function returns, the
110 * two objects are independent, i.e. subsequent calls involving one of
111 * the objects do not affect the other object.
112 *
113 * \param[in] source_operation The active hash operation to clone.
114 * \param[in,out] target_operation The operation object to set up.
115 * It must be initialized but not active.
116 *
117 * \retval #PSA_SUCCESS
118 * \retval #PSA_ERROR_BAD_STATE
119 * The \p source_operation state is not valid (it must be active).
120 * \retval #PSA_ERROR_BAD_STATE
121 * The \p target_operation state is not valid (it must be inactive).
122 * \retval #PSA_ERROR_CORRUPTION_DETECTED
123 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
124 */
125psa_status_t mbedtls_psa_hash_clone(
126 const mbedtls_psa_hash_operation_t *source_operation,
127 mbedtls_psa_hash_operation_t *target_operation );
128
129/** Add a message fragment to a multipart Mbed TLS hash operation.
130 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100131 * \note The signature of this function is that of a PSA driver hash_update
132 * entry point. This function behaves as a hash_update entry point as
133 * defined in the PSA driver interface specification for transparent
134 * drivers.
135 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100136 * The application must call mbedtls_psa_hash_setup() before calling this function.
137 *
138 * If this function returns an error status, the operation enters an error
139 * state and must be aborted by calling mbedtls_psa_hash_abort().
140 *
141 * \param[in,out] operation Active hash operation.
142 * \param[in] input Buffer containing the message fragment to hash.
143 * \param input_length Size of the \p input buffer in bytes.
144 *
145 * \retval #PSA_SUCCESS
146 * Success.
147 * \retval #PSA_ERROR_BAD_STATE
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100148 * The operation state is not valid (it must be active).
Steven Cooreman0e307642021-02-18 16:18:32 +0100149 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
150 * \retval #PSA_ERROR_CORRUPTION_DETECTED
151 */
152psa_status_t mbedtls_psa_hash_update(
153 mbedtls_psa_hash_operation_t *operation,
154 const uint8_t *input,
155 size_t input_length );
156
157/** Finish the calculation of the Mbed TLS-calculated hash of a message.
158 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100159 * \note The signature of this function is that of a PSA driver hash_finish
160 * entry point. This function behaves as a hash_finish entry point as
161 * defined in the PSA driver interface specification for transparent
162 * drivers.
163 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100164 * The application must call mbedtls_psa_hash_setup() before calling this function.
165 * This function calculates the hash of the message formed by concatenating
166 * the inputs passed to preceding calls to mbedtls_psa_hash_update().
167 *
168 * When this function returns successfuly, the operation becomes inactive.
169 * If this function returns an error status, the operation enters an error
170 * state and must be aborted by calling mbedtls_psa_hash_abort().
171 *
172 * \param[in,out] operation Active hash operation.
173 * \param[out] hash Buffer where the hash is to be written.
174 * \param hash_size Size of the \p hash buffer in bytes.
175 * \param[out] hash_length On success, the number of bytes
176 * that make up the hash value. This is always
177 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
178 * hash algorithm that is calculated.
179 *
180 * \retval #PSA_SUCCESS
181 * Success.
182 * \retval #PSA_ERROR_BAD_STATE
183 * The operation state is not valid (it must be active).
184 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
185 * The size of the \p hash buffer is too small. You can determine a
186 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
187 * where \c alg is the hash algorithm that is calculated.
188 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
189 * \retval #PSA_ERROR_CORRUPTION_DETECTED
190 */
191psa_status_t mbedtls_psa_hash_finish(
192 mbedtls_psa_hash_operation_t *operation,
193 uint8_t *hash,
194 size_t hash_size,
195 size_t *hash_length );
196
197/** Abort an Mbed TLS hash operation.
198 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100199 * \note The signature of this function is that of a PSA driver hash_abort
200 * entry point. This function behaves as a hash_abort entry point as
201 * defined in the PSA driver interface specification for transparent
202 * drivers.
203 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100204 * Aborting an operation frees all associated resources except for the
205 * \p operation structure itself. Once aborted, the operation object
206 * can be reused for another operation by calling
207 * mbedtls_psa_hash_setup() again.
208 *
209 * You may call this function any time after the operation object has
210 * been initialized by one of the methods described in #psa_hash_operation_t.
211 *
212 * In particular, calling mbedtls_psa_hash_abort() after the operation has been
213 * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
214 * mbedtls_psa_hash_verify() is safe and has no effect.
215 *
216 * \param[in,out] operation Initialized hash operation.
217 *
218 * \retval #PSA_SUCCESS
219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
220 */
221psa_status_t mbedtls_psa_hash_abort(
222 mbedtls_psa_hash_operation_t *operation );
223
Steven Cooremand029b602021-03-08 16:16:53 +0100224/*
225 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
226 */
227
228#if defined(PSA_CRYPTO_DRIVER_TEST)
Steven Cooremand029b602021-03-08 16:16:53 +0100229
Steven Cooreman25555222021-03-08 16:20:04 +0100230psa_status_t mbedtls_transparent_test_driver_hash_compute(
Steven Cooremand029b602021-03-08 16:16:53 +0100231 psa_algorithm_t alg,
232 const uint8_t *input,
233 size_t input_length,
234 uint8_t *hash,
235 size_t hash_size,
236 size_t *hash_length);
237
Steven Cooreman25555222021-03-08 16:20:04 +0100238psa_status_t mbedtls_transparent_test_driver_hash_setup(
239 mbedtls_transparent_test_driver_hash_operation_t *operation,
Steven Cooremand029b602021-03-08 16:16:53 +0100240 psa_algorithm_t alg );
241
Steven Cooreman25555222021-03-08 16:20:04 +0100242psa_status_t mbedtls_transparent_test_driver_hash_clone(
243 const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
244 mbedtls_transparent_test_driver_hash_operation_t *target_operation );
Steven Cooremand029b602021-03-08 16:16:53 +0100245
Steven Cooreman25555222021-03-08 16:20:04 +0100246psa_status_t mbedtls_transparent_test_driver_hash_update(
247 mbedtls_transparent_test_driver_hash_operation_t *operation,
Steven Cooremand029b602021-03-08 16:16:53 +0100248 const uint8_t *input,
249 size_t input_length );
250
Steven Cooreman25555222021-03-08 16:20:04 +0100251psa_status_t mbedtls_transparent_test_driver_hash_finish(
252 mbedtls_transparent_test_driver_hash_operation_t *operation,
Steven Cooremand029b602021-03-08 16:16:53 +0100253 uint8_t *hash,
254 size_t hash_size,
255 size_t *hash_length );
256
Steven Cooreman25555222021-03-08 16:20:04 +0100257psa_status_t mbedtls_transparent_test_driver_hash_abort(
258 mbedtls_transparent_test_driver_hash_operation_t *operation );
Steven Cooremand029b602021-03-08 16:16:53 +0100259
260#endif /* PSA_CRYPTO_DRIVER_TEST */
261
Steven Cooreman0e307642021-02-18 16:18:32 +0100262#endif /* PSA_CRYPTO_HASH_H */