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> |
| 25 | #include "mbedtls/md2.h" |
| 26 | #include "mbedtls/md4.h" |
| 27 | #include "mbedtls/md5.h" |
| 28 | #include "mbedtls/ripemd160.h" |
| 29 | #include "mbedtls/sha1.h" |
| 30 | #include "mbedtls/sha256.h" |
| 31 | #include "mbedtls/sha512.h" |
| 32 | |
Steven Cooreman | 1e58235 | 2021-02-18 17:24:37 +0100 | [diff] [blame] | 33 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) || \ |
| 34 | defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) || \ |
| 35 | defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) || \ |
| 36 | defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) || \ |
| 37 | defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) || \ |
| 38 | defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) || \ |
| 39 | defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) || \ |
| 40 | defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) || \ |
| 41 | defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 42 | #define MBEDTLS_PSA_BUILTIN_HASH |
| 43 | #endif |
| 44 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 45 | typedef struct |
| 46 | { |
| 47 | psa_algorithm_t alg; |
| 48 | union |
| 49 | { |
| 50 | unsigned dummy; /* Make the union non-empty even with no supported algorithms. */ |
| 51 | #if defined(MBEDTLS_MD2_C) |
| 52 | mbedtls_md2_context md2; |
| 53 | #endif |
| 54 | #if defined(MBEDTLS_MD4_C) |
| 55 | mbedtls_md4_context md4; |
| 56 | #endif |
| 57 | #if defined(MBEDTLS_MD5_C) |
| 58 | mbedtls_md5_context md5; |
| 59 | #endif |
| 60 | #if defined(MBEDTLS_RIPEMD160_C) |
| 61 | mbedtls_ripemd160_context ripemd160; |
| 62 | #endif |
| 63 | #if defined(MBEDTLS_SHA1_C) |
| 64 | mbedtls_sha1_context sha1; |
| 65 | #endif |
| 66 | #if defined(MBEDTLS_SHA256_C) |
| 67 | mbedtls_sha256_context sha256; |
| 68 | #endif |
| 69 | #if defined(MBEDTLS_SHA512_C) |
| 70 | mbedtls_sha512_context sha512; |
| 71 | #endif |
| 72 | } ctx; |
| 73 | } mbedtls_psa_hash_operation_t; |
| 74 | |
| 75 | #define MBEDTLS_PSA_HASH_OPERATION_INIT {0, {0}} |
| 76 | |
| 77 | /** Calculate the hash (digest) of a message using Mbed TLS routines. |
| 78 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 79 | * \note The signature of this function is that of a PSA driver hash_compute |
| 80 | * entry point. This function behaves as a hash_compute entry point as |
| 81 | * defined in the PSA driver interface specification for transparent |
| 82 | * drivers. |
| 83 | * |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 84 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 85 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
| 86 | * \param[in] input Buffer containing the message to hash. |
| 87 | * \param input_length Size of the \p input buffer in bytes. |
| 88 | * \param[out] hash Buffer where the hash is to be written. |
| 89 | * \param hash_size Size of the \p hash buffer in bytes. |
| 90 | * \param[out] hash_length On success, the number of bytes |
| 91 | * that make up the hash value. This is always |
| 92 | * #PSA_HASH_LENGTH(\p alg). |
| 93 | * |
| 94 | * \retval #PSA_SUCCESS |
| 95 | * Success. |
| 96 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 97 | * \p alg is not supported |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 98 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 99 | * \p hash_size is too small |
| 100 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 101 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 102 | */ |
| 103 | psa_status_t mbedtls_psa_hash_compute( |
| 104 | psa_algorithm_t alg, |
| 105 | const uint8_t *input, |
| 106 | size_t input_length, |
| 107 | uint8_t *hash, |
| 108 | size_t hash_size, |
| 109 | size_t *hash_length); |
| 110 | |
| 111 | /** Set up a multipart hash operation using Mbed TLS routines. |
| 112 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 113 | * \note The signature of this function is that of a PSA driver hash_setup |
| 114 | * entry point. This function behaves as a hash_setup entry point as |
| 115 | * defined in the PSA driver interface specification for transparent |
| 116 | * drivers. |
| 117 | * |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 118 | * If an error occurs at any step after a call to mbedtls_psa_hash_setup(), the |
| 119 | * operation will need to be reset by a call to mbedtls_psa_hash_abort(). The |
| 120 | * core may call mbedtls_psa_hash_abort() at any time after the operation |
| 121 | * has been initialized. |
| 122 | * |
| 123 | * After a successful call to mbedtls_psa_hash_setup(), the core must |
| 124 | * eventually terminate the operation. The following events terminate an |
| 125 | * operation: |
| 126 | * - A successful call to mbedtls_psa_hash_finish() or mbedtls_psa_hash_verify(). |
| 127 | * - A call to mbedtls_psa_hash_abort(). |
| 128 | * |
| 129 | * \param[in,out] operation The operation object to set up. It must have |
| 130 | * been initialized to all-zero and not yet be in use. |
| 131 | * \param alg The hash algorithm to compute (\c PSA_ALG_XXX value |
| 132 | * such that #PSA_ALG_IS_HASH(\p alg) is true). |
| 133 | * |
| 134 | * \retval #PSA_SUCCESS |
| 135 | * Success. |
| 136 | * \retval #PSA_ERROR_NOT_SUPPORTED |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 137 | * \p alg is not supported |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 138 | * \retval #PSA_ERROR_BAD_STATE |
| 139 | * The operation state is not valid (it must be inactive). |
| 140 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 141 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 142 | */ |
| 143 | psa_status_t mbedtls_psa_hash_setup( |
| 144 | mbedtls_psa_hash_operation_t *operation, |
| 145 | psa_algorithm_t alg ); |
| 146 | |
| 147 | /** Clone an Mbed TLS hash operation. |
| 148 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 149 | * \note The signature of this function is that of a PSA driver hash_clone |
| 150 | * entry point. This function behaves as a hash_clone entry point as |
| 151 | * defined in the PSA driver interface specification for transparent |
| 152 | * drivers. |
| 153 | * |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 154 | * This function copies the state of an ongoing hash operation to |
| 155 | * a new operation object. In other words, this function is equivalent |
| 156 | * to calling mbedtls_psa_hash_setup() on \p target_operation with the same |
| 157 | * algorithm that \p source_operation was set up for, then |
| 158 | * mbedtls_psa_hash_update() on \p target_operation with the same input that |
| 159 | * that was passed to \p source_operation. After this function returns, the |
| 160 | * two objects are independent, i.e. subsequent calls involving one of |
| 161 | * the objects do not affect the other object. |
| 162 | * |
| 163 | * \param[in] source_operation The active hash operation to clone. |
| 164 | * \param[in,out] target_operation The operation object to set up. |
| 165 | * It must be initialized but not active. |
| 166 | * |
| 167 | * \retval #PSA_SUCCESS |
| 168 | * \retval #PSA_ERROR_BAD_STATE |
| 169 | * The \p source_operation state is not valid (it must be active). |
| 170 | * \retval #PSA_ERROR_BAD_STATE |
| 171 | * The \p target_operation state is not valid (it must be inactive). |
| 172 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 173 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 174 | */ |
| 175 | psa_status_t mbedtls_psa_hash_clone( |
| 176 | const mbedtls_psa_hash_operation_t *source_operation, |
| 177 | mbedtls_psa_hash_operation_t *target_operation ); |
| 178 | |
| 179 | /** Add a message fragment to a multipart Mbed TLS hash operation. |
| 180 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 181 | * \note The signature of this function is that of a PSA driver hash_update |
| 182 | * entry point. This function behaves as a hash_update entry point as |
| 183 | * defined in the PSA driver interface specification for transparent |
| 184 | * drivers. |
| 185 | * |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 186 | * The application must call mbedtls_psa_hash_setup() before calling this function. |
| 187 | * |
| 188 | * If this function returns an error status, the operation enters an error |
| 189 | * state and must be aborted by calling mbedtls_psa_hash_abort(). |
| 190 | * |
| 191 | * \param[in,out] operation Active hash operation. |
| 192 | * \param[in] input Buffer containing the message fragment to hash. |
| 193 | * \param input_length Size of the \p input buffer in bytes. |
| 194 | * |
| 195 | * \retval #PSA_SUCCESS |
| 196 | * Success. |
| 197 | * \retval #PSA_ERROR_BAD_STATE |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 198 | * The operation state is not valid (it must be active). |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 199 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 200 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 201 | */ |
| 202 | psa_status_t mbedtls_psa_hash_update( |
| 203 | mbedtls_psa_hash_operation_t *operation, |
| 204 | const uint8_t *input, |
| 205 | size_t input_length ); |
| 206 | |
| 207 | /** Finish the calculation of the Mbed TLS-calculated hash of a message. |
| 208 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 209 | * \note The signature of this function is that of a PSA driver hash_finish |
| 210 | * entry point. This function behaves as a hash_finish entry point as |
| 211 | * defined in the PSA driver interface specification for transparent |
| 212 | * drivers. |
| 213 | * |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 214 | * The application must call mbedtls_psa_hash_setup() before calling this function. |
| 215 | * This function calculates the hash of the message formed by concatenating |
| 216 | * the inputs passed to preceding calls to mbedtls_psa_hash_update(). |
| 217 | * |
| 218 | * When this function returns successfuly, the operation becomes inactive. |
| 219 | * If this function returns an error status, the operation enters an error |
| 220 | * state and must be aborted by calling mbedtls_psa_hash_abort(). |
| 221 | * |
| 222 | * \param[in,out] operation Active hash operation. |
| 223 | * \param[out] hash Buffer where the hash is to be written. |
| 224 | * \param hash_size Size of the \p hash buffer in bytes. |
| 225 | * \param[out] hash_length On success, the number of bytes |
| 226 | * that make up the hash value. This is always |
| 227 | * #PSA_HASH_LENGTH(\c alg) where \c alg is the |
| 228 | * hash algorithm that is calculated. |
| 229 | * |
| 230 | * \retval #PSA_SUCCESS |
| 231 | * Success. |
| 232 | * \retval #PSA_ERROR_BAD_STATE |
| 233 | * The operation state is not valid (it must be active). |
| 234 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 235 | * The size of the \p hash buffer is too small. You can determine a |
| 236 | * sufficient buffer size by calling #PSA_HASH_LENGTH(\c alg) |
| 237 | * where \c alg is the hash algorithm that is calculated. |
| 238 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 239 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 240 | */ |
| 241 | psa_status_t mbedtls_psa_hash_finish( |
| 242 | mbedtls_psa_hash_operation_t *operation, |
| 243 | uint8_t *hash, |
| 244 | size_t hash_size, |
| 245 | size_t *hash_length ); |
| 246 | |
| 247 | /** Abort an Mbed TLS hash operation. |
| 248 | * |
Steven Cooreman | 8e9e407 | 2021-03-04 11:07:23 +0100 | [diff] [blame] | 249 | * \note The signature of this function is that of a PSA driver hash_abort |
| 250 | * entry point. This function behaves as a hash_abort entry point as |
| 251 | * defined in the PSA driver interface specification for transparent |
| 252 | * drivers. |
| 253 | * |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 254 | * Aborting an operation frees all associated resources except for the |
| 255 | * \p operation structure itself. Once aborted, the operation object |
| 256 | * can be reused for another operation by calling |
| 257 | * mbedtls_psa_hash_setup() again. |
| 258 | * |
| 259 | * You may call this function any time after the operation object has |
| 260 | * been initialized by one of the methods described in #psa_hash_operation_t. |
| 261 | * |
| 262 | * In particular, calling mbedtls_psa_hash_abort() after the operation has been |
| 263 | * terminated by a call to mbedtls_psa_hash_abort(), mbedtls_psa_hash_finish() or |
| 264 | * mbedtls_psa_hash_verify() is safe and has no effect. |
| 265 | * |
| 266 | * \param[in,out] operation Initialized hash operation. |
| 267 | * |
| 268 | * \retval #PSA_SUCCESS |
| 269 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 270 | */ |
| 271 | psa_status_t mbedtls_psa_hash_abort( |
| 272 | mbedtls_psa_hash_operation_t *operation ); |
| 273 | |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 274 | /* |
| 275 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 276 | */ |
| 277 | |
| 278 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 279 | typedef struct { |
| 280 | mbedtls_psa_hash_operation_t operation; |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 281 | } mbedtls_transparent_test_driver_hash_operation_t; |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 282 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 283 | psa_status_t mbedtls_transparent_test_driver_hash_compute( |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 284 | psa_algorithm_t alg, |
| 285 | const uint8_t *input, |
| 286 | size_t input_length, |
| 287 | uint8_t *hash, |
| 288 | size_t hash_size, |
| 289 | size_t *hash_length); |
| 290 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 291 | psa_status_t mbedtls_transparent_test_driver_hash_setup( |
| 292 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 293 | psa_algorithm_t alg ); |
| 294 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 295 | psa_status_t mbedtls_transparent_test_driver_hash_clone( |
| 296 | const mbedtls_transparent_test_driver_hash_operation_t *source_operation, |
| 297 | mbedtls_transparent_test_driver_hash_operation_t *target_operation ); |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 298 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 299 | psa_status_t mbedtls_transparent_test_driver_hash_update( |
| 300 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 301 | const uint8_t *input, |
| 302 | size_t input_length ); |
| 303 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 304 | psa_status_t mbedtls_transparent_test_driver_hash_finish( |
| 305 | mbedtls_transparent_test_driver_hash_operation_t *operation, |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 306 | uint8_t *hash, |
| 307 | size_t hash_size, |
| 308 | size_t *hash_length ); |
| 309 | |
Steven Cooreman | 2555522 | 2021-03-08 16:20:04 +0100 | [diff] [blame^] | 310 | psa_status_t mbedtls_transparent_test_driver_hash_abort( |
| 311 | mbedtls_transparent_test_driver_hash_operation_t *operation ); |
Steven Cooreman | d029b60 | 2021-03-08 16:16:53 +0100 | [diff] [blame] | 312 | |
| 313 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 314 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 315 | #endif /* PSA_CRYPTO_HASH_H */ |