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 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
| 27 | #include "psa_crypto_mac.h" |
| 28 | |
| 29 | #include <mbedtls/error.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /* Use builtin defines specific to this compilation unit, since the test driver |
| 33 | * relies on the software driver. */ |
| 34 | #if( defined(MBEDTLS_PSA_BUILTIN_ALG_CMAC) || \ |
| 35 | ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) ) ) |
| 36 | #define BUILTIN_ALG_CMAC 1 |
| 37 | #endif |
| 38 | #if( defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) || \ |
| 39 | ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) ) ) |
| 40 | #define BUILTIN_ALG_HMAC 1 |
| 41 | #endif |
| 42 | |
| 43 | /* Implement the PSA driver MAC interface on top of mbed TLS if either the |
| 44 | * software driver or the test driver requires it. */ |
| 45 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) || defined(PSA_CRYPTO_DRIVER_TEST) |
| 46 | static psa_status_t mac_compute( |
| 47 | const psa_key_attributes_t *attributes, |
| 48 | const uint8_t *key_buffer, |
| 49 | size_t key_buffer_size, |
| 50 | psa_algorithm_t alg, |
| 51 | const uint8_t *input, |
| 52 | size_t input_length, |
| 53 | uint8_t *mac, |
| 54 | size_t mac_size, |
| 55 | size_t *mac_length ) |
| 56 | { |
| 57 | /* To be fleshed out in a subsequent commit */ |
| 58 | (void) attributes; |
| 59 | (void) key_buffer; |
| 60 | (void) key_buffer_size; |
| 61 | (void) alg; |
| 62 | (void) input; |
| 63 | (void) input_length; |
| 64 | (void) mac; |
| 65 | (void) mac_size; |
| 66 | (void) mac_length; |
| 67 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 68 | } |
| 69 | |
| 70 | static psa_status_t mac_sign_setup( |
| 71 | mbedtls_psa_mac_operation_t *operation, |
| 72 | const psa_key_attributes_t *attributes, |
| 73 | const uint8_t *key_buffer, |
| 74 | size_t key_buffer_size, |
| 75 | psa_algorithm_t alg ) |
| 76 | { |
| 77 | /* To be fleshed out in a subsequent commit */ |
| 78 | (void) operation; |
| 79 | (void) attributes; |
| 80 | (void) key_buffer; |
| 81 | (void) key_buffer_size; |
| 82 | (void) alg; |
| 83 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 84 | } |
| 85 | |
| 86 | static psa_status_t mac_verify_setup( |
| 87 | mbedtls_psa_mac_operation_t *operation, |
| 88 | const psa_key_attributes_t *attributes, |
| 89 | const uint8_t *key_buffer, |
| 90 | size_t key_buffer_size, |
| 91 | psa_algorithm_t alg ) |
| 92 | { |
| 93 | /* To be fleshed out in a subsequent commit */ |
| 94 | (void) operation; |
| 95 | (void) attributes; |
| 96 | (void) key_buffer; |
| 97 | (void) key_buffer_size; |
| 98 | (void) alg; |
| 99 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 100 | } |
| 101 | |
| 102 | static psa_status_t mac_update( |
| 103 | mbedtls_psa_mac_operation_t *operation, |
| 104 | const uint8_t *input, |
| 105 | size_t input_length ) |
| 106 | { |
| 107 | /* To be fleshed out in a subsequent commit */ |
| 108 | (void) operation; |
| 109 | (void) input; |
| 110 | (void) input_length; |
| 111 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 112 | } |
| 113 | |
| 114 | static psa_status_t mac_sign_finish( |
| 115 | mbedtls_psa_mac_operation_t *operation, |
| 116 | uint8_t *mac, |
| 117 | size_t mac_size, |
| 118 | size_t *mac_length ) |
| 119 | { |
| 120 | /* To be fleshed out in a subsequent commit */ |
| 121 | (void) operation; |
| 122 | (void) mac; |
| 123 | (void) mac_size; |
| 124 | (void) mac_length; |
| 125 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 126 | } |
| 127 | |
| 128 | static psa_status_t mac_verify_finish( |
| 129 | mbedtls_psa_mac_operation_t *operation, |
| 130 | const uint8_t *mac, |
| 131 | size_t mac_length ) |
| 132 | { |
| 133 | /* To be fleshed out in a subsequent commit */ |
| 134 | (void) operation; |
| 135 | (void) mac; |
| 136 | (void) mac_length; |
| 137 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 138 | } |
| 139 | |
| 140 | static psa_status_t mac_abort( |
| 141 | mbedtls_psa_mac_operation_t *operation ) |
| 142 | { |
| 143 | /* To be fleshed out in a subsequent commit */ |
| 144 | (void) operation; |
| 145 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 146 | } |
| 147 | #endif /* MBEDTLS_PSA_BUILTIN_MAC || PSA_CRYPTO_DRIVER_TEST */ |
| 148 | |
| 149 | #if defined(MBEDTLS_PSA_BUILTIN_MAC) |
| 150 | psa_status_t mbedtls_psa_mac_compute( |
| 151 | const psa_key_attributes_t *attributes, |
| 152 | const uint8_t *key_buffer, |
| 153 | size_t key_buffer_size, |
| 154 | psa_algorithm_t alg, |
| 155 | const uint8_t *input, |
| 156 | size_t input_length, |
| 157 | uint8_t *mac, |
| 158 | size_t mac_size, |
| 159 | size_t *mac_length ) |
| 160 | { |
| 161 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 162 | input, input_length, |
| 163 | mac, mac_size, mac_length ) ); |
| 164 | } |
| 165 | |
| 166 | psa_status_t mbedtls_psa_mac_sign_setup( |
| 167 | mbedtls_psa_mac_operation_t *operation, |
| 168 | const psa_key_attributes_t *attributes, |
| 169 | const uint8_t *key_buffer, |
| 170 | size_t key_buffer_size, |
| 171 | psa_algorithm_t alg ) |
| 172 | { |
| 173 | return( mac_sign_setup( operation, attributes, |
| 174 | key_buffer, key_buffer_size, alg ) ); |
| 175 | } |
| 176 | |
| 177 | psa_status_t mbedtls_psa_mac_verify_setup( |
| 178 | mbedtls_psa_mac_operation_t *operation, |
| 179 | const psa_key_attributes_t *attributes, |
| 180 | const uint8_t *key_buffer, |
| 181 | size_t key_buffer_size, |
| 182 | psa_algorithm_t alg ) |
| 183 | { |
| 184 | return( mac_verify_setup( operation, attributes, |
| 185 | key_buffer, key_buffer_size, alg ) ); |
| 186 | } |
| 187 | |
| 188 | psa_status_t mbedtls_psa_mac_update( |
| 189 | mbedtls_psa_mac_operation_t *operation, |
| 190 | const uint8_t *input, |
| 191 | size_t input_length ) |
| 192 | { |
| 193 | return( mac_update( operation, input, input_length ) ); |
| 194 | } |
| 195 | |
| 196 | psa_status_t mbedtls_psa_mac_sign_finish( |
| 197 | mbedtls_psa_mac_operation_t *operation, |
| 198 | uint8_t *mac, |
| 199 | size_t mac_size, |
| 200 | size_t *mac_length ) |
| 201 | { |
| 202 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 203 | } |
| 204 | |
| 205 | psa_status_t mbedtls_psa_mac_verify_finish( |
| 206 | mbedtls_psa_mac_operation_t *operation, |
| 207 | const uint8_t *mac, |
| 208 | size_t mac_length ) |
| 209 | { |
| 210 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 211 | } |
| 212 | |
| 213 | psa_status_t mbedtls_psa_mac_abort( |
| 214 | mbedtls_psa_mac_operation_t *operation ) |
| 215 | { |
| 216 | return( mac_abort( operation ) ); |
| 217 | } |
| 218 | #endif /* MBEDTLS_PSA_BUILTIN_MAC */ |
| 219 | |
| 220 | /* |
| 221 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 222 | */ |
| 223 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 224 | |
| 225 | static int is_mac_accelerated( psa_algorithm_t alg ) |
| 226 | { |
| 227 | #if defined(MBEDTLS_PSA_ACCEL_ALG_HMAC) |
| 228 | if( PSA_ALG_IS_HMAC( alg ) ) |
| 229 | return( 1 ); |
| 230 | #endif |
| 231 | |
| 232 | switch( PSA_ALG_FULL_LENGTH_MAC( alg ) ) |
| 233 | { |
| 234 | #if defined(MBEDTLS_PSA_ACCEL_ALG_CMAC) |
| 235 | case PSA_ALG_CMAC: |
| 236 | return( 1 ); |
| 237 | #endif |
| 238 | default: |
| 239 | return( 0 ); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | psa_status_t mbedtls_transparent_test_driver_mac_compute( |
| 244 | const psa_key_attributes_t *attributes, |
| 245 | const uint8_t *key_buffer, |
| 246 | size_t key_buffer_size, |
| 247 | psa_algorithm_t alg, |
| 248 | const uint8_t *input, |
| 249 | size_t input_length, |
| 250 | uint8_t *mac, |
| 251 | size_t mac_size, |
| 252 | size_t *mac_length ) |
| 253 | { |
| 254 | if( is_mac_accelerated( alg ) ) |
| 255 | return( mac_compute( attributes, key_buffer, key_buffer_size, alg, |
| 256 | input, input_length, |
| 257 | mac, mac_size, mac_length ) ); |
| 258 | else |
| 259 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 260 | } |
| 261 | |
| 262 | psa_status_t mbedtls_transparent_test_driver_mac_sign_setup( |
| 263 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 264 | const psa_key_attributes_t *attributes, |
| 265 | const uint8_t *key_buffer, |
| 266 | size_t key_buffer_size, |
| 267 | psa_algorithm_t alg ) |
| 268 | { |
| 269 | if( is_mac_accelerated( alg ) ) |
| 270 | return( mac_sign_setup( operation, attributes, |
| 271 | key_buffer, key_buffer_size, alg ) ); |
| 272 | else |
| 273 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 274 | } |
| 275 | |
| 276 | psa_status_t mbedtls_transparent_test_driver_mac_verify_setup( |
| 277 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 278 | const psa_key_attributes_t *attributes, |
| 279 | const uint8_t *key_buffer, |
| 280 | size_t key_buffer_size, |
| 281 | psa_algorithm_t alg ) |
| 282 | { |
| 283 | if( is_mac_accelerated( alg ) ) |
| 284 | return( mac_verify_setup( operation, attributes, |
| 285 | key_buffer, key_buffer_size, alg ) ); |
| 286 | else |
| 287 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 288 | } |
| 289 | |
| 290 | psa_status_t mbedtls_transparent_test_driver_mac_update( |
| 291 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 292 | const uint8_t *input, |
| 293 | size_t input_length ) |
| 294 | { |
| 295 | if( is_mac_accelerated( operation->alg ) ) |
| 296 | return( mac_update( operation, input, input_length ) ); |
| 297 | else |
| 298 | return( PSA_ERROR_BAD_STATE ); |
| 299 | } |
| 300 | |
| 301 | psa_status_t mbedtls_transparent_test_driver_mac_sign_finish( |
| 302 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 303 | uint8_t *mac, |
| 304 | size_t mac_size, |
| 305 | size_t *mac_length ) |
| 306 | { |
| 307 | if( is_mac_accelerated( operation->alg ) ) |
| 308 | return( mac_sign_finish( operation, mac, mac_size, mac_length ) ); |
| 309 | else |
| 310 | return( PSA_ERROR_BAD_STATE ); |
| 311 | } |
| 312 | |
| 313 | psa_status_t mbedtls_transparent_test_driver_mac_verify_finish( |
| 314 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 315 | const uint8_t *mac, |
| 316 | size_t mac_length ) |
| 317 | { |
| 318 | if( is_mac_accelerated( operation->alg ) ) |
| 319 | return( mac_verify_finish( operation, mac, mac_length ) ); |
| 320 | else |
| 321 | return( PSA_ERROR_BAD_STATE ); |
| 322 | } |
| 323 | |
| 324 | psa_status_t mbedtls_transparent_test_driver_mac_abort( |
| 325 | mbedtls_transparent_test_driver_mac_operation_t *operation ) |
| 326 | { |
| 327 | return( mac_abort( operation ) ); |
| 328 | } |
| 329 | |
| 330 | psa_status_t mbedtls_opaque_test_driver_mac_compute( |
| 331 | const psa_key_attributes_t *attributes, |
| 332 | const uint8_t *key_buffer, |
| 333 | size_t key_buffer_size, |
| 334 | psa_algorithm_t alg, |
| 335 | const uint8_t *input, |
| 336 | size_t input_length, |
| 337 | uint8_t *mac, |
| 338 | size_t mac_size, |
| 339 | size_t *mac_length ) |
| 340 | { |
| 341 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 342 | (void) attributes; |
| 343 | (void) key_buffer; |
| 344 | (void) key_buffer_size; |
| 345 | (void) alg; |
| 346 | (void) input; |
| 347 | (void) input_length; |
| 348 | (void) mac; |
| 349 | (void) mac_size; |
| 350 | (void) mac_length; |
| 351 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 352 | } |
| 353 | |
| 354 | psa_status_t mbedtls_opaque_test_driver_mac_sign_setup( |
| 355 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 356 | const psa_key_attributes_t *attributes, |
| 357 | const uint8_t *key_buffer, |
| 358 | size_t key_buffer_size, |
| 359 | psa_algorithm_t alg ) |
| 360 | { |
| 361 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 362 | (void) operation; |
| 363 | (void) attributes; |
| 364 | (void) key_buffer; |
| 365 | (void) key_buffer_size; |
| 366 | (void) alg; |
| 367 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 368 | } |
| 369 | |
| 370 | psa_status_t mbedtls_opaque_test_driver_mac_verify_setup( |
| 371 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 372 | const psa_key_attributes_t *attributes, |
| 373 | const uint8_t *key_buffer, |
| 374 | size_t key_buffer_size, |
| 375 | psa_algorithm_t alg ) |
| 376 | { |
| 377 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 378 | (void) operation; |
| 379 | (void) attributes; |
| 380 | (void) key_buffer; |
| 381 | (void) key_buffer_size; |
| 382 | (void) alg; |
| 383 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 384 | } |
| 385 | |
| 386 | psa_status_t mbedtls_opaque_test_driver_mac_update( |
| 387 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 388 | const uint8_t *input, |
| 389 | size_t input_length ) |
| 390 | { |
| 391 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 392 | (void) operation; |
| 393 | (void) input; |
| 394 | (void) input_length; |
| 395 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 396 | } |
| 397 | |
| 398 | psa_status_t mbedtls_opaque_test_driver_mac_sign_finish( |
| 399 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 400 | uint8_t *mac, |
| 401 | size_t mac_size, |
| 402 | size_t *mac_length ) |
| 403 | { |
| 404 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 405 | (void) operation; |
| 406 | (void) mac; |
| 407 | (void) mac_size; |
| 408 | (void) mac_length; |
| 409 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 410 | } |
| 411 | |
| 412 | psa_status_t mbedtls_opaque_test_driver_mac_verify_finish( |
| 413 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 414 | const uint8_t *mac, |
| 415 | size_t mac_length ) |
| 416 | { |
| 417 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 418 | (void) operation; |
| 419 | (void) mac; |
| 420 | (void) mac_length; |
| 421 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 422 | } |
| 423 | |
| 424 | psa_status_t mbedtls_opaque_test_driver_mac_abort( |
| 425 | mbedtls_opaque_test_driver_mac_operation_t *operation ) |
| 426 | { |
| 427 | /* Opaque driver testing is not implemented yet through this mechanism. */ |
| 428 | (void) operation; |
| 429 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 430 | } |
| 431 | |
| 432 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 433 | |
| 434 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |