Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 1 | /* |
| 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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 25 | |
| 26 | /** Calculate the hash (digest) of a message using Mbed TLS routines. |
| 27 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 28 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 33 | * \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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 46 | * \p alg is not supported |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 47 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 48 | * \p hash_size is too small |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 49 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 50 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 51 | */ |
| 52 | psa_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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 62 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 67 | * 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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 86 | * \p alg is not supported |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 87 | * \retval #PSA_ERROR_BAD_STATE |
| 88 | * The operation state is not valid (it must be inactive). |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 89 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 90 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 91 | */ |
| 92 | psa_status_t mbedtls_psa_hash_setup( |
| 93 | mbedtls_psa_hash_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | psa_algorithm_t alg); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 95 | |
| 96 | /** Clone an Mbed TLS hash operation. |
| 97 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 98 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 103 | * 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 Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 116 | * \retval #PSA_SUCCESS \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 117 | * \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 Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 121 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
| 122 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 123 | */ |
| 124 | psa_status_t mbedtls_psa_hash_clone( |
| 125 | const mbedtls_psa_hash_operation_t *source_operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | mbedtls_psa_hash_operation_t *target_operation); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 127 | |
| 128 | /** Add a message fragment to a multipart Mbed TLS hash operation. |
| 129 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 130 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 135 | * 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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 147 | * The operation state is not valid (it must be active). |
Gilles Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 148 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 149 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 150 | */ |
| 151 | psa_status_t mbedtls_psa_hash_update( |
| 152 | mbedtls_psa_hash_operation_t *operation, |
| 153 | const uint8_t *input, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | size_t input_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 155 | |
| 156 | /** Finish the calculation of the Mbed TLS-calculated hash of a message. |
| 157 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 158 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 163 | * 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 Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 167 | * When this function returns successfully, the operation becomes inactive. |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 168 | * 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 Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 187 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription |
| 188 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 189 | */ |
| 190 | psa_status_t mbedtls_psa_hash_finish( |
| 191 | mbedtls_psa_hash_operation_t *operation, |
| 192 | uint8_t *hash, |
| 193 | size_t hash_size, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | size_t *hash_length); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 195 | |
| 196 | /** Abort an Mbed TLS hash operation. |
| 197 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 198 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 203 | * 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 Peskine | ed73355 | 2023-02-14 19:21:09 +0100 | [diff] [blame] | 217 | * \retval #PSA_SUCCESS \emptydescription |
| 218 | * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 219 | */ |
| 220 | psa_status_t mbedtls_psa_hash_abort( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | mbedtls_psa_hash_operation_t *operation); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 222 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 223 | #endif /* PSA_CRYPTO_HASH_H */ |