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 | 830aff2 | 2021-03-09 09:50:44 +0100 | [diff] [blame] | 25 | #include <psa/crypto_builtin_hash.h> |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 26 | |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame^] | 27 | #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 | */ |
| 36 | const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg ); |
| 37 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 38 | /** Calculate the hash (digest) of a message using Mbed TLS routines. |
| 39 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 40 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 45 | * \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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 58 | * \p alg is not supported |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 59 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 60 | * \p hash_size is too small |
| 61 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 62 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 63 | */ |
| 64 | psa_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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 74 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 79 | * 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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 98 | * \p alg is not supported |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 99 | * \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 | */ |
| 104 | psa_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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 110 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 115 | * 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 | */ |
| 136 | psa_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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 142 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 147 | * 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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 159 | * The operation state is not valid (it must be active). |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 160 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 161 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 162 | */ |
| 163 | psa_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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 170 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 175 | * 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 | */ |
| 202 | psa_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 Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 210 | * \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 Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 215 | * 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 | */ |
| 232 | psa_status_t mbedtls_psa_hash_abort( |
| 233 | mbedtls_psa_hash_operation_t *operation ); |
| 234 | |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 235 | /* |
| 236 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 237 | */ |
| 238 | |
| 239 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 240 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 241 | psa_status_t mbedtls_transparent_test_driver_hash_compute( |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 242 | 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 Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 249 | psa_status_t mbedtls_transparent_test_driver_hash_setup( |
| 250 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 251 | psa_algorithm_t alg ); |
| 252 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 253 | psa_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 Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 256 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 257 | psa_status_t mbedtls_transparent_test_driver_hash_update( |
| 258 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 259 | const uint8_t *input, |
| 260 | size_t input_length ); |
| 261 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 262 | psa_status_t mbedtls_transparent_test_driver_hash_finish( |
| 263 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 264 | uint8_t *hash, |
| 265 | size_t hash_size, |
| 266 | size_t *hash_length ); |
| 267 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame] | 268 | psa_status_t mbedtls_transparent_test_driver_hash_abort( |
| 269 | mbedtls_transparent_test_driver_hash_operation_t *operation ); |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 270 | |
| 271 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 272 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 273 | #endif /* PSA_CRYPTO_HASH_H */ |