blob: 5c196b2ab44147cd7c2b4d9a85924f821fb7a6b3 [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 Rodgman7ff79652023-11-03 12:04:52 +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
Steven Cooreman5f88e772021-03-15 11:07:12 +010014#include <mbedtls/md_internal.h>
15
16/** Get Mbed TLS MD information of a hash algorithm given its PSA identifier
17 *
18 * \param[in] alg PSA hash algorithm identifier
19 *
20 * \return The Mbed TLS MD information of the hash algorithm. \c NULL if the
21 * PSA hash algorithm is not supported.
22 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023const mbedtls_md_info_t *mbedtls_md_info_from_psa(psa_algorithm_t alg);
Steven Cooreman5f88e772021-03-15 11:07:12 +010024
Steven Cooreman0e307642021-02-18 16:18:32 +010025/** Calculate the hash (digest) of a message using Mbed TLS routines.
26 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010027 * \note The signature of this function is that of a PSA driver hash_compute
28 * entry point. This function behaves as a hash_compute entry point as
29 * defined in the PSA driver interface specification for transparent
30 * drivers.
31 *
Steven Cooreman0e307642021-02-18 16:18:32 +010032 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
33 * such that #PSA_ALG_IS_HASH(\p alg) is true).
34 * \param[in] input Buffer containing the message to hash.
35 * \param input_length Size of the \p input buffer in bytes.
36 * \param[out] hash Buffer where the hash is to be written.
37 * \param hash_size Size of the \p hash buffer in bytes.
38 * \param[out] hash_length On success, the number of bytes
39 * that make up the hash value. This is always
40 * #PSA_HASH_LENGTH(\p alg).
41 *
42 * \retval #PSA_SUCCESS
43 * Success.
44 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010045 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010046 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
47 * \p hash_size is too small
Gilles Peskineec1eff32023-02-14 19:21:09 +010048 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
49 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +010050 */
51psa_status_t mbedtls_psa_hash_compute(
52 psa_algorithm_t alg,
53 const uint8_t *input,
54 size_t input_length,
55 uint8_t *hash,
56 size_t hash_size,
57 size_t *hash_length);
58
59/** Set up a multipart hash operation using Mbed TLS routines.
60 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010061 * \note The signature of this function is that of a PSA driver hash_setup
62 * entry point. This function behaves as a hash_setup entry point as
63 * defined in the PSA driver interface specification for transparent
64 * drivers.
65 *
Steven Cooreman0e307642021-02-18 16:18:32 +010066 * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the
67 * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The
68 * core may call mbedtls_psa_hash_abort() at any time after the operation
69 * has been initialized.
70 *
71 * After a successful call to mbedtls_psa_hash_setup(), the core must
72 * eventually terminate the operation. The following events terminate an
73 * operation:
74 * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify().
75 * - A call to mbedtls_psa_hash_abort().
76 *
77 * \param[in,out] operation The operation object to set up. It must have
78 * been initialized to all-zero and not yet be in use.
79 * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value
80 * such that #PSA_ALG_IS_HASH(\p alg) is true).
81 *
82 * \retval #PSA_SUCCESS
83 * Success.
84 * \retval #PSA_ERROR_NOT_SUPPORTED
Steven Cooreman8e9e4072021-03-04 11:07:23 +010085 * \p alg is not supported
Steven Cooreman0e307642021-02-18 16:18:32 +010086 * \retval #PSA_ERROR_BAD_STATE
87 * The operation state is not valid (it must be inactive).
Gilles Peskineec1eff32023-02-14 19:21:09 +010088 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
89 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +010090 */
91psa_status_t mbedtls_psa_hash_setup(
92 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 psa_algorithm_t alg);
Steven Cooreman0e307642021-02-18 16:18:32 +010094
95/** Clone an Mbed TLS hash operation.
96 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +010097 * \note The signature of this function is that of a PSA driver hash_clone
98 * entry point. This function behaves as a hash_clone entry point as
99 * defined in the PSA driver interface specification for transparent
100 * drivers.
101 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100102 * This function copies the state of an ongoing hash operation to
103 * a new operation object. In other words, this function is equivalent
104 * to calling mbedtls_psa_hash_setup() on \p target_operation with the same
105 * algorithm that \p source_operation was set up for, then
106 * mbedtls_psa_hash_update() on \p target_operation with the same input that
107 * that was passed to \p source_operation. After this function returns, the
108 * two objects are independent, i.e. subsequent calls involving one of
109 * the objects do not affect the other object.
110 *
111 * \param[in] source_operation The active hash operation to clone.
112 * \param[in,out] target_operation The operation object to set up.
113 * It must be initialized but not active.
114 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100115 * \retval #PSA_SUCCESS \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100116 * \retval #PSA_ERROR_BAD_STATE
117 * The \p source_operation state is not valid (it must be active).
118 * \retval #PSA_ERROR_BAD_STATE
119 * The \p target_operation state is not valid (it must be inactive).
Gilles Peskineec1eff32023-02-14 19:21:09 +0100120 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
121 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100122 */
123psa_status_t mbedtls_psa_hash_clone(
124 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_psa_hash_operation_t *target_operation);
Steven Cooreman0e307642021-02-18 16:18:32 +0100126
127/** Add a message fragment to a multipart Mbed TLS hash operation.
128 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100129 * \note The signature of this function is that of a PSA driver hash_update
130 * entry point. This function behaves as a hash_update entry point as
131 * defined in the PSA driver interface specification for transparent
132 * drivers.
133 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100134 * The application must call mbedtls_psa_hash_setup() before calling this function.
135 *
136 * If this function returns an error status, the operation enters an error
137 * state and must be aborted by calling mbedtls_psa_hash_abort().
138 *
139 * \param[in,out] operation Active hash operation.
140 * \param[in] input Buffer containing the message fragment to hash.
141 * \param input_length Size of the \p input buffer in bytes.
142 *
143 * \retval #PSA_SUCCESS
144 * Success.
145 * \retval #PSA_ERROR_BAD_STATE
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100146 * The operation state is not valid (it must be active).
Gilles Peskineec1eff32023-02-14 19:21:09 +0100147 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
148 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100149 */
150psa_status_t mbedtls_psa_hash_update(
151 mbedtls_psa_hash_operation_t *operation,
152 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 size_t input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100154
155/** Finish the calculation of the Mbed TLS-calculated hash of a message.
156 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100157 * \note The signature of this function is that of a PSA driver hash_finish
158 * entry point. This function behaves as a hash_finish entry point as
159 * defined in the PSA driver interface specification for transparent
160 * drivers.
161 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100162 * The application must call mbedtls_psa_hash_setup() before calling this function.
163 * This function calculates the hash of the message formed by concatenating
164 * the inputs passed to preceding calls to mbedtls_psa_hash_update().
165 *
Shaun Case0e7791f2021-12-20 21:14:10 -0800166 * When this function returns successfully, the operation becomes inactive.
Steven Cooreman0e307642021-02-18 16:18:32 +0100167 * If this function returns an error status, the operation enters an error
168 * state and must be aborted by calling mbedtls_psa_hash_abort().
169 *
170 * \param[in,out] operation Active hash operation.
171 * \param[out] hash Buffer where the hash is to be written.
172 * \param hash_size Size of the \p hash buffer in bytes.
173 * \param[out] hash_length On success, the number of bytes
174 * that make up the hash value. This is always
175 * #PSA_HASH_LENGTH(\c alg) where \c alg is the
176 * hash algorithm that is calculated.
177 *
178 * \retval #PSA_SUCCESS
179 * Success.
180 * \retval #PSA_ERROR_BAD_STATE
181 * The operation state is not valid (it must be active).
182 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
183 * The size of the \p hash buffer is too small. You can determine a
184 * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg)
185 * where \c alg is the hash algorithm that is calculated.
Gilles Peskineec1eff32023-02-14 19:21:09 +0100186 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
187 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100188 */
189psa_status_t mbedtls_psa_hash_finish(
190 mbedtls_psa_hash_operation_t *operation,
191 uint8_t *hash,
192 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100193 size_t *hash_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100194
195/** Abort an Mbed TLS hash operation.
196 *
Steven Cooreman8e9e4072021-03-04 11:07:23 +0100197 * \note The signature of this function is that of a PSA driver hash_abort
198 * entry point. This function behaves as a hash_abort entry point as
199 * defined in the PSA driver interface specification for transparent
200 * drivers.
201 *
Steven Cooreman0e307642021-02-18 16:18:32 +0100202 * Aborting an operation frees all associated resources except for the
203 * \p operation structure itself. Once aborted, the operation object
204 * can be reused for another operation by calling
205 * mbedtls_psa_hash_setup() again.
206 *
207 * You may call this function any time after the operation object has
208 * been initialized by one of the methods described in #psa_hash_operation_t.
209 *
210 * In particular, calling mbedtls_psa_hash_abort() after the operation has been
211 * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or
212 * mbedtls_psa_hash_verify() is safe and has no effect.
213 *
214 * \param[in,out] operation Initialized hash operation.
215 *
Gilles Peskineec1eff32023-02-14 19:21:09 +0100216 * \retval #PSA_SUCCESS \emptydescription
217 * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
Steven Cooreman0e307642021-02-18 16:18:32 +0100218 */
219psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 mbedtls_psa_hash_operation_t *operation);
Steven Cooreman0e307642021-02-18 16:18:32 +0100221
Steven Cooreman0e307642021-02-18 16:18:32 +0100222#endif /* PSA_CRYPTO_HASH_H */