Steven Cooreman | d13a70f | 2021-03-19 15:24:23 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * PSA MAC 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_MAC_H |
| 22 | #define PSA_CRYPTO_MAC_H |
| 23 | |
| 24 | #include <psa/crypto.h> |
| 25 | |
| 26 | /** Calculate the MAC (message authentication code) of a message using Mbed TLS. |
| 27 | * |
| 28 | * \note The signature of this function is that of a PSA driver mac_compute |
| 29 | * entry point. This function behaves as a mac_compute entry point as |
| 30 | * defined in the PSA driver interface specification for transparent |
| 31 | * drivers. |
| 32 | * |
| 33 | * \param[in] attributes The attributes of the key to use for the |
| 34 | * operation. |
| 35 | * \param[in] key_buffer The buffer containing the key to use for |
| 36 | * computing the MAC. This buffer contains the key |
| 37 | * in export representation as defined by |
| 38 | * psa_export_key() (i.e. the raw key bytes). |
| 39 | * \param key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 40 | * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value |
| 41 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
| 42 | * \param[in] input Buffer containing the input message. |
| 43 | * \param input_length Size of the \p input buffer in bytes. |
| 44 | * \param[out] mac Buffer where the MAC value is to be written. |
| 45 | * \param mac_size Size of the \p mac buffer in bytes. |
| 46 | * \param[out] mac_length On success, the number of bytes |
| 47 | * that make up the MAC value. |
| 48 | * |
| 49 | * \retval #PSA_SUCCESS |
| 50 | * Success. |
| 51 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 52 | * The key is not compatible with \p alg. |
| 53 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 54 | * \p alg is not supported. |
| 55 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 56 | * \p mac_size is too small |
| 57 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 58 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 59 | */ |
| 60 | psa_status_t mbedtls_psa_mac_compute( |
| 61 | const psa_key_attributes_t *attributes, |
| 62 | const uint8_t *key_buffer, |
| 63 | size_t key_buffer_size, |
| 64 | psa_algorithm_t alg, |
| 65 | const uint8_t *input, |
| 66 | size_t input_length, |
| 67 | uint8_t *mac, |
| 68 | size_t mac_size, |
| 69 | size_t *mac_length); |
| 70 | |
| 71 | /** Set up a multipart MAC calculation operation using Mbed TLS. |
| 72 | * |
| 73 | * \note The signature of this function is that of a PSA driver mac_sign_setup |
| 74 | * entry point. This function behaves as a mac_sign_setup entry point as |
| 75 | * defined in the PSA driver interface specification for transparent |
| 76 | * drivers. |
| 77 | * |
| 78 | * \param[in,out] operation The operation object to set up. It must have |
| 79 | * been initialized and not yet in use. |
| 80 | * \param[in] attributes The attributes of the key to use for the |
| 81 | * operation. |
| 82 | * \param[in] key_buffer The buffer containing the key to use for |
| 83 | * computing the MAC. This buffer contains the key |
| 84 | * in export representation as defined by |
| 85 | * psa_export_key() (i.e. the raw key bytes). |
| 86 | * \param key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 87 | * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value |
| 88 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
| 89 | * |
| 90 | * \retval #PSA_SUCCESS |
| 91 | * Success. |
| 92 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 93 | * The key is not compatible with \p alg. |
| 94 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 95 | * \p alg is not supported. |
| 96 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 97 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 98 | * \retval #PSA_ERROR_BAD_STATE |
| 99 | * The operation state is not valid (it must be inactive). |
| 100 | */ |
| 101 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 102 | mbedtls_psa_mac_operation_t *operation, |
| 103 | const psa_key_attributes_t *attributes, |
| 104 | const uint8_t *key_buffer, |
| 105 | size_t key_buffer_size, |
| 106 | psa_algorithm_t alg); |
| 107 | |
| 108 | /** Set up a multipart MAC verification operation using Mbed TLS. |
| 109 | * |
| 110 | * \note The signature of this function is that of a PSA driver mac_verify_setup |
| 111 | * entry point. This function behaves as a mac_verify_setup entry point as |
| 112 | * defined in the PSA driver interface specification for transparent |
| 113 | * drivers. |
| 114 | * |
| 115 | * \param[in,out] operation The operation object to set up. It must have |
| 116 | * been initialized and not yet in use. |
| 117 | * \param[in] attributes The attributes of the key to use for the |
| 118 | * operation. |
| 119 | * \param[in] key_buffer The buffer containing the key to use for |
| 120 | * computing the MAC. This buffer contains the key |
| 121 | * in export representation as defined by |
| 122 | * psa_export_key() (i.e. the raw key bytes). |
| 123 | * \param key_buffer_size Size of the \p key_buffer buffer in bytes. |
| 124 | * \param alg The MAC algorithm to use (\c PSA_ALG_XXX value |
| 125 | * such that #PSA_ALG_IS_MAC(\p alg) is true). |
| 126 | * |
| 127 | * \retval #PSA_SUCCESS |
| 128 | * Success. |
| 129 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 130 | * The key is not compatible with \p alg. |
| 131 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 132 | * \p alg is not supported. |
| 133 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 134 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 135 | * \retval #PSA_ERROR_BAD_STATE |
| 136 | * The operation state is not valid (it must be inactive). |
| 137 | */ |
| 138 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 139 | mbedtls_psa_mac_operation_t *operation, |
| 140 | const psa_key_attributes_t *attributes, |
| 141 | const uint8_t *key_buffer, |
| 142 | size_t key_buffer_size, |
| 143 | psa_algorithm_t alg); |
| 144 | |
| 145 | /** Add a message fragment to a multipart MAC operation using Mbed TLS. |
| 146 | * |
| 147 | * \note The signature of this function is that of a PSA driver mac_update |
| 148 | * entry point. This function behaves as a mac_update entry point as |
| 149 | * defined in the PSA driver interface specification for transparent |
| 150 | * drivers. |
| 151 | * |
| 152 | * The core must call mbedtls_psa_mac_sign_setup() or |
| 153 | * mbedtls_psa_mac_verify_setup() before calling this function. |
| 154 | * |
| 155 | * If this function returns an error status, the operation enters an error |
| 156 | * state and must be aborted by calling psa_mac_abort(). |
| 157 | * |
| 158 | * \param[in,out] operation Active MAC operation. |
| 159 | * \param[in] input Buffer containing the message fragment to add to |
| 160 | * the MAC calculation. |
| 161 | * \param input_length Size of the \p input buffer in bytes. |
| 162 | * |
| 163 | * \retval #PSA_SUCCESS |
| 164 | * Success. |
| 165 | * \retval #PSA_ERROR_BAD_STATE |
| 166 | * The operation state is not valid (it must be active). |
| 167 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 168 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 169 | */ |
| 170 | psa_status_t mbedtls_psa_mac_update( |
| 171 | mbedtls_psa_mac_operation_t *operation, |
| 172 | const uint8_t *input, |
| 173 | size_t input_length ); |
| 174 | |
| 175 | /** Finish the calculation of the MAC of a message using Mbed TLS. |
| 176 | * |
| 177 | * \note The signature of this function is that of a PSA driver mac_sign_finish |
| 178 | * entry point. This function behaves as a mac_sign_finish entry point as |
| 179 | * defined in the PSA driver interface specification for transparent |
| 180 | * drivers. |
| 181 | * |
| 182 | * The core must call mbedtls_psa_mac_sign_setup() before calling this function. |
| 183 | * This function calculates the MAC of the message formed by concatenating |
| 184 | * the inputs passed to preceding calls to mbedtls_psa_mac_update(). |
| 185 | * |
| 186 | * When this function returns successfully, the operation becomes inactive. |
| 187 | * If this function returns an error status, the operation enters an error |
| 188 | * state and must be aborted by calling mbedtls_psa_mac_abort(). |
| 189 | * |
| 190 | * \param[in,out] operation Active MAC operation. |
| 191 | * \param[out] mac Buffer where the MAC value is to be written. |
| 192 | * \param mac_size Size of the \p mac buffer in bytes. |
| 193 | * \param[out] mac_length On success, the number of bytes |
| 194 | * that make up the MAC value. This is always |
| 195 | * #PSA_MAC_LENGTH(\c key_type, \c key_bits, \c alg) |
| 196 | * where \c key_type and \c key_bits are the type and |
| 197 | * bit-size respectively of the key and \c alg is the |
| 198 | * MAC algorithm that is calculated. |
| 199 | * |
| 200 | * \retval #PSA_SUCCESS |
| 201 | * Success. |
| 202 | * \retval #PSA_ERROR_BAD_STATE |
| 203 | * The operation state is not valid (it must be an active mac sign |
| 204 | * operation). |
| 205 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 206 | * The size of the \p mac buffer is too small. A sufficient buffer size |
| 207 | * can be determined by calling PSA_MAC_LENGTH(). |
| 208 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 209 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 210 | */ |
| 211 | psa_status_t mbedtls_psa_mac_sign_finish( |
| 212 | mbedtls_psa_mac_operation_t *operation, |
| 213 | uint8_t *mac, |
| 214 | size_t mac_size, |
| 215 | size_t *mac_length ); |
| 216 | |
| 217 | /** Finish the calculation of the MAC of a message and compare it with |
| 218 | * an expected value using Mbed TLS. |
| 219 | * |
| 220 | * \note The signature of this function is that of a PSA driver |
| 221 | * mac_verify_finish entry point. This function behaves as a |
| 222 | * mac_verify_finish entry point as defined in the PSA driver interface |
| 223 | * specification for transparent drivers. |
| 224 | * |
| 225 | * The core must call mbedtls_psa_mac_verify_setup() before calling this |
| 226 | * function. This function calculates the MAC of the message formed by |
| 227 | * concatenating the inputs passed to preceding calls to |
| 228 | * mbedtls_psa_mac_update(). It then compares the calculated MAC with the |
| 229 | * expected MAC passed as a parameter to this function. |
| 230 | * |
| 231 | * When this function returns successfully, the operation becomes inactive. |
| 232 | * If this function returns an error status, the operation enters an error |
| 233 | * state and must be aborted by calling mbedtls_psa_mac_abort(). |
| 234 | * |
| 235 | * \param[in,out] operation Active MAC operation. |
| 236 | * \param[in] mac Buffer containing the expected MAC value. |
| 237 | * \param mac_length Size of the \p mac buffer in bytes. |
| 238 | * |
| 239 | * \retval #PSA_SUCCESS |
| 240 | * The expected MAC is identical to the actual MAC of the message. |
| 241 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 242 | * The MAC of the message was calculated successfully, but it |
| 243 | * differs from the expected MAC. |
| 244 | * \retval #PSA_ERROR_BAD_STATE |
| 245 | * The operation state is not valid (it must be an active mac verify |
| 246 | * operation). |
| 247 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 248 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 249 | */ |
| 250 | psa_status_t mbedtls_psa_mac_verify_finish( |
| 251 | mbedtls_psa_mac_operation_t *operation, |
| 252 | const uint8_t *mac, |
| 253 | size_t mac_length ); |
| 254 | |
| 255 | /** Abort a MAC operation using Mbed TLS. |
| 256 | * |
| 257 | * Aborting an operation frees all associated resources except for the |
| 258 | * \p operation structure itself. Once aborted, the operation object |
| 259 | * can be reused for another operation by calling |
| 260 | * mbedtls_psa_mac_sign_setup() or mbedtls_psa_mac_verify_setup() again. |
| 261 | * |
| 262 | * The core may call this function any time after the operation object has |
| 263 | * been initialized by one of the methods described in |
| 264 | * #mbedtls_psa_mac_operation_t. |
| 265 | * |
| 266 | * In particular, calling mbedtls_psa_mac_abort() after the operation has been |
| 267 | * terminated by a call to mbedtls_psa_mac_abort(), |
| 268 | * mbedtls_psa_mac_sign_finish() or mbedtls_psa_mac_verify_finish() is safe and |
| 269 | * has no effect. |
| 270 | * |
| 271 | * \param[in,out] operation Initialized MAC operation. |
| 272 | * |
| 273 | * \retval #PSA_SUCCESS |
| 274 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 275 | */ |
| 276 | psa_status_t mbedtls_psa_mac_abort( |
| 277 | mbedtls_psa_mac_operation_t *operation ); |
| 278 | |
| 279 | /* |
| 280 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 281 | */ |
| 282 | |
| 283 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 284 | |
| 285 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 286 | const psa_key_attributes_t *attributes, |
| 287 | const uint8_t *key_buffer, |
| 288 | size_t key_buffer_size, |
| 289 | psa_algorithm_t alg, |
| 290 | const uint8_t *input, |
| 291 | size_t input_length, |
| 292 | uint8_t *mac, |
| 293 | size_t mac_size, |
| 294 | size_t *mac_length ); |
| 295 | |
| 296 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
| 297 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 298 | const psa_key_attributes_t *attributes, |
| 299 | const uint8_t *key_buffer, |
| 300 | size_t key_buffer_size, |
| 301 | psa_algorithm_t alg ); |
| 302 | |
| 303 | psa_status_t mbedtls_transparent_test_driver_mac_verify_setup( |
| 304 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 305 | const psa_key_attributes_t *attributes, |
| 306 | const uint8_t *key_buffer, |
| 307 | size_t key_buffer_size, |
| 308 | psa_algorithm_t alg ); |
| 309 | |
| 310 | psa_status_t mbedtls_transparent_test_driver_mac_update( |
| 311 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 312 | const uint8_t *input, |
| 313 | size_t input_length ); |
| 314 | |
| 315 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
| 316 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 317 | uint8_t *mac, |
| 318 | size_t mac_size, |
| 319 | size_t *mac_length ); |
| 320 | |
| 321 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
| 322 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 323 | const uint8_t *mac, |
| 324 | size_t mac_length ); |
| 325 | |
| 326 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
| 327 | mbedtls_transparent_test_driver_mac_operation_t *operation ); |
| 328 | |
| 329 | psa_status_t mbedtls_opaque_test_driver_mac_compute( |
| 330 | const psa_key_attributes_t *attributes, |
| 331 | const uint8_t *key_buffer, |
| 332 | size_t key_buffer_size, |
| 333 | psa_algorithm_t alg, |
| 334 | const uint8_t *input, |
| 335 | size_t input_length, |
| 336 | uint8_t *mac, |
| 337 | size_t mac_size, |
| 338 | size_t *mac_length ); |
| 339 | |
| 340 | psa_status_t mbedtls_opaque_test_driver_mac_sign_setup( |
| 341 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 342 | const psa_key_attributes_t *attributes, |
| 343 | const uint8_t *key_buffer, |
| 344 | size_t key_buffer_size, |
| 345 | psa_algorithm_t alg ); |
| 346 | |
| 347 | psa_status_t mbedtls_opaque_test_driver_mac_verify_setup( |
| 348 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 349 | const psa_key_attributes_t *attributes, |
| 350 | const uint8_t *key_buffer, |
| 351 | size_t key_buffer_size, |
| 352 | psa_algorithm_t alg ); |
| 353 | |
| 354 | psa_status_t mbedtls_opaque_test_driver_mac_update( |
| 355 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 356 | const uint8_t *input, |
| 357 | size_t input_length ); |
| 358 | |
| 359 | psa_status_t mbedtls_opaque_test_driver_mac_sign_finish( |
| 360 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 361 | uint8_t *mac, |
| 362 | size_t mac_size, |
| 363 | size_t *mac_length ); |
| 364 | |
| 365 | psa_status_t mbedtls_opaque_test_driver_mac_verify_finish( |
| 366 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 367 | const uint8_t *mac, |
| 368 | size_t mac_length ); |
| 369 | |
| 370 | psa_status_t mbedtls_opaque_test_driver_mac_abort( |
| 371 | mbedtls_opaque_test_driver_mac_operation_t *operation ); |
| 372 | |
| 373 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 374 | |
| 375 | #endif /* PSA_CRYPTO_MAC_H */ |