blob: 2dfb0115e8ae8191a28e83bdc2f9881c74f7c82d [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 Cooreman0e307642021-02-18 16:18:32 +010025
26/** Calculate the hash (digest) of a message using Mbed TLS routines.
27 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010028 * \note The signature of this function is that of a PSA driver hash_compute
29 * entry point. This function behaves as a hash_compute entry point as
30 * defined in the PSA driver interface specification for transparent
31 * drivers.
32 *
Steven Cooreman0e307642021-02-18 16:18:32 +010033 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
34 * such that #PSA_ALG_IS_HASH(\p alg) is true).
35 * \param[in] input Buffer containing the message to hash.
36 * \param input_length Size of the \p input buffer in bytes.
37 * \param[out] hash Buffer where the hash is to be written.
38 * \param hash_size Size of the \p hash buffer in bytes.
39 * \param[out] hash_length On success, the number of bytes
40 * that make up the hash value. This is always
41 * #PSA_HASH_LENGTH(\p alg).
42 *
43 * \retval #PSA_SUCCESS
44 * Success.
45 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010046 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010047 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
48 * \p hash_size is too small
Gilles Peskineed733552023-02-14 19:21:09 +010049 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
50 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +010051 */
52psa_status_t mbedtls_psa_hash_compute(
53 psa_algorithm_t alg,
54 const uint8_t *input,
55 size_t input_length,
56 uint8_t *hash,
57 size_t hash_size,
58 size_t *hash_length);
59
60/** Set up a multipart hash operation using Mbed TLS routines.
61 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010062 * \note The signature of this function is that of a PSA driver hash_setup
63 * entry point. This function behaves as a hash_setup entry point as
64 * defined in the PSA driver interface specification for transparent
65 * drivers.
66 *
Steven Cooreman0e307642021-02-18 16:18:32 +010067 * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
68 * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
69 * core may call mbedtls_psa_hash_abort() at any time after the operation
70 * has been initialized.
71 *
72 * After a successful call to mbedtls_psa_hash_setup(), the core must
73 * eventually terminate the operation. The following events terminate an
74 * operation:
75 * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
76 * - A call to mbedtls_psa_hash_abort().
77 *
78 * \param[in,out] operation The operation object to set up. It must have
79 * been initialized to all-zero and not yet be in use.
80 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
81 * such that #PSA_ALG_IS_HASH(\p alg) is true).
82 *
83 * \retval #PSA_SUCCESS
84 * Success.
85 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010086 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010087 * \retval #PSA_ERROR_BAD_STATE
88 * The operation state is not valid (it must be inactive).
Gilles Peskineed733552023-02-14 19:21:09 +010089 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
90 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +010091 */
92psa_status_t mbedtls_psa_hash_setup(
93 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +010094 psa_algorithm_t alg);
Steven Cooreman0e307642021-02-18 16:18:32 +010095
96/** Clone an Mbed TLS hash operation.
97 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010098 * \note The signature of this function is that of a PSA driver hash_clone
99 * entry point. This function behaves as a hash_clone entry point as
100 * defined in the PSA driver interface specification for transparent
101 * drivers.
102 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100103 * This function copies the state of an ongoing hash operation to
104 * a new operation object. In other words, this function is equivalent
105 * to calling mbedtls_psa_hash_setup() on \p target_operation with the same
106 * algorithm that \p source_operation was set up for, then
107 * mbedtls_psa_hash_update() on \p target_operation with the same input that
108 * that was passed to \p source_operation. After this function returns, the
109 * two objects are independent, i.e. subsequent calls involving one of
110 * the objects do not affect the other object.
111 *
112 * \param[in] source_operation The active hash operation to clone.
113 * \param[in,out] target_operation The operation object to set up.
114 * It must be initialized but not active.
115 *
Gilles Peskineed733552023-02-14 19:21:09 +0100116 * \retval #PSA_SUCCESS \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100117 * \retval #PSA_ERROR_BAD_STATE
118 * The \p source_operation state is not valid (it must be active).
119 * \retval #PSA_ERROR_BAD_STATE
120 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineed733552023-02-14 19:21:09 +0100121 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
122 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100123 */
124psa_status_t mbedtls_psa_hash_clone(
125 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_psa_hash_operation_t *target_operation);
Steven Cooreman0e307642021-02-18 16:18:32 +0100127
128/** Add a message fragment to a multipart Mbed TLS hash operation.
129 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100130 * \note The signature of this function is that of a PSA driver hash_update
131 * entry point. This function behaves as a hash_update entry point as
132 * defined in the PSA driver interface specification for transparent
133 * drivers.
134 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100135 * The application must call mbedtls_psa_hash_setup() before calling this function.
136 *
137 * If this function returns an error status, the operation enters an error
138 * state and must be aborted by calling mbedtls_psa_hash_abort().
139 *
140 * \param[in,out] operation Active hash operation.
141 * \param[in] input Buffer containing the message fragment to hash.
142 * \param input_length Size of the \p input buffer in bytes.
143 *
144 * \retval #PSA_SUCCESS
145 * Success.
146 * \retval #PSA_ERROR_BAD_STATE
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100147 * The operation state is not valid (it must be active).
Gilles Peskineed733552023-02-14 19:21:09 +0100148 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
149 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100150 */
151psa_status_t mbedtls_psa_hash_update(
152 mbedtls_psa_hash_operation_t *operation,
153 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 size_t input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100155
156/** Finish the calculation of the Mbed TLS-calculated hash of a message.
157 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100158 * \note The signature of this function is that of a PSA driver hash_finish
159 * entry point. This function behaves as a hash_finish entry point as
160 * defined in the PSA driver interface specification for transparent
161 * drivers.
162 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100163 * The application must call mbedtls_psa_hash_setup() before calling this function.
164 * This function calculates the hash of the message formed by concatenating
165 * the inputs passed to preceding calls to mbedtls_psa_hash_update().
166 *
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800167 * When this function returns successfully, the operation becomes inactive.
Steven Cooreman0e307642021-02-18 16:18:32 +0100168 * If this function returns an error status, the operation enters an error
169 * state and must be aborted by calling mbedtls_psa_hash_abort().
170 *
171 * \param[in,out] operation Active hash operation.
172 * \param[out] hash Buffer where the hash is to be written.
173 * \param hash_size Size of the \p hash buffer in bytes.
174 * \param[out] hash_length On success, the number of bytes
175 * that make up the hash value. This is always
176 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
177 * hash algorithm that is calculated.
178 *
179 * \retval #PSA_SUCCESS
180 * Success.
181 * \retval #PSA_ERROR_BAD_STATE
182 * The operation state is not valid (it must be active).
183 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
184 * The size of the \p hash buffer is too small. You can determine a
185 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
186 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineed733552023-02-14 19:21:09 +0100187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
188 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100189 */
190psa_status_t mbedtls_psa_hash_finish(
191 mbedtls_psa_hash_operation_t *operation,
192 uint8_t *hash,
193 size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 size_t *hash_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100195
196/** Abort an Mbed TLS hash operation.
197 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100198 * \note The signature of this function is that of a PSA driver hash_abort
199 * entry point. This function behaves as a hash_abort entry point as
200 * defined in the PSA driver interface specification for transparent
201 * drivers.
202 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100203 * Aborting an operation frees all associated resources except for the
204 * \p operation structure itself. Once aborted, the operation object
205 * can be reused for another operation by calling
206 * mbedtls_psa_hash_setup() again.
207 *
208 * You may call this function any time after the operation object has
209 * been initialized by one of the methods described in #psa_hash_operation_t.
210 *
211 * In particular, calling mbedtls_psa_hash_abort() after the operation has been
212 * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
213 * mbedtls_psa_hash_verify() is safe and has no effect.
214 *
215 * \param[in,out] operation Initialized hash operation.
216 *
Gilles Peskineed733552023-02-14 19:21:09 +0100217 * \retval #PSA_SUCCESS \emptydescription
218 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100219 */
220psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_psa_hash_operation_t *operation);
Steven Cooreman0e307642021-02-18 16:18:32 +0100222
Steven Cooreman0e307642021-02-18 16:18:32 +0100223#endif /* PSA_CRYPTO_HASH_H */