Steven Cooreman | c7f0a57 | 2021-04-29 21:10:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Test driver for MAC entry points. |
| 3 | */ |
| 4 | /* Copyright The Mbed TLS Contributors |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 21 | #include "mbedtls/config.h" |
| 22 | #else |
| 23 | #include MBEDTLS_CONFIG_FILE |
| 24 | #endif |
| 25 | |
| 26 | #if defined(MBEDTLS_PSA_CRYPTO_DRIVERS) && defined(PSA_CRYPTO_DRIVER_TEST) |
| 27 | #include "psa_crypto_mac.h" |
| 28 | |
| 29 | #include "test/drivers/mac.h" |
| 30 | |
| 31 | test_driver_mac_hooks_t test_driver_mac_hooks = MBEDTLS_TEST_DRIVER_MAC_INIT; |
| 32 | |
| 33 | psa_status_t mbedtls_test_transparent_mac_compute( |
| 34 | const psa_key_attributes_t *attributes, |
| 35 | const uint8_t *key_buffer, |
| 36 | size_t key_buffer_size, |
| 37 | psa_algorithm_t alg, |
| 38 | const uint8_t *input, |
| 39 | size_t input_length, |
| 40 | uint8_t *mac, |
| 41 | size_t mac_size, |
| 42 | size_t *mac_length ) |
| 43 | { |
| 44 | test_driver_mac_hooks.hits++; |
| 45 | |
| 46 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 47 | { |
| 48 | test_driver_mac_hooks.driver_status = |
| 49 | test_driver_mac_hooks.forced_status; |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | test_driver_mac_hooks.driver_status = |
| 54 | mbedtls_transparent_test_driver_mac_compute( |
| 55 | attributes, key_buffer, key_buffer_size, alg, |
| 56 | input, input_length, |
| 57 | mac, mac_size, mac_length ); |
| 58 | } |
| 59 | |
| 60 | return( test_driver_mac_hooks.driver_status ); |
| 61 | } |
| 62 | |
| 63 | psa_status_t mbedtls_test_transparent_mac_sign_setup( |
| 64 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 65 | const psa_key_attributes_t *attributes, |
| 66 | const uint8_t *key_buffer, |
| 67 | size_t key_buffer_size, |
| 68 | psa_algorithm_t alg ) |
| 69 | { |
| 70 | test_driver_mac_hooks.hits++; |
| 71 | |
| 72 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 73 | { |
| 74 | test_driver_mac_hooks.driver_status = |
| 75 | test_driver_mac_hooks.forced_status; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | test_driver_mac_hooks.driver_status = |
| 80 | mbedtls_transparent_test_driver_mac_sign_setup( |
| 81 | operation, attributes, key_buffer, key_buffer_size, alg ); |
| 82 | } |
| 83 | |
| 84 | return( test_driver_mac_hooks.driver_status ); |
| 85 | } |
| 86 | |
| 87 | psa_status_t mbedtls_test_transparent_mac_verify_setup( |
| 88 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 89 | const psa_key_attributes_t *attributes, |
| 90 | const uint8_t *key_buffer, |
| 91 | size_t key_buffer_size, |
| 92 | psa_algorithm_t alg ) |
| 93 | { |
| 94 | test_driver_mac_hooks.hits++; |
| 95 | |
| 96 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 97 | { |
| 98 | test_driver_mac_hooks.driver_status = |
| 99 | test_driver_mac_hooks.forced_status; |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | test_driver_mac_hooks.driver_status = |
| 104 | mbedtls_transparent_test_driver_mac_verify_setup( |
| 105 | operation, attributes, key_buffer, key_buffer_size, alg ); |
| 106 | } |
| 107 | |
| 108 | return( test_driver_mac_hooks.driver_status ); |
| 109 | } |
| 110 | |
| 111 | psa_status_t mbedtls_test_transparent_mac_update( |
| 112 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 113 | const uint8_t *input, |
| 114 | size_t input_length ) |
| 115 | { |
| 116 | test_driver_mac_hooks.hits++; |
| 117 | |
| 118 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 119 | { |
| 120 | test_driver_mac_hooks.driver_status = |
| 121 | test_driver_mac_hooks.forced_status; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | test_driver_mac_hooks.driver_status = |
| 126 | mbedtls_transparent_test_driver_mac_update( |
| 127 | operation, input, input_length ); |
| 128 | } |
| 129 | |
| 130 | return( test_driver_mac_hooks.driver_status ); |
| 131 | } |
| 132 | |
| 133 | psa_status_t mbedtls_test_transparent_mac_sign_finish( |
| 134 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 135 | uint8_t *mac, |
| 136 | size_t mac_size, |
| 137 | size_t *mac_length ) |
| 138 | { |
| 139 | test_driver_mac_hooks.hits++; |
| 140 | |
| 141 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 142 | { |
| 143 | test_driver_mac_hooks.driver_status = |
| 144 | test_driver_mac_hooks.forced_status; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | test_driver_mac_hooks.driver_status = |
| 149 | mbedtls_transparent_test_driver_mac_sign_finish( |
| 150 | operation, mac, mac_size, mac_length ); |
| 151 | } |
| 152 | |
| 153 | return( test_driver_mac_hooks.driver_status ); |
| 154 | } |
| 155 | |
| 156 | psa_status_t mbedtls_test_transparent_mac_verify_finish( |
| 157 | mbedtls_transparent_test_driver_mac_operation_t *operation, |
| 158 | const uint8_t *mac, |
| 159 | size_t mac_length ) |
| 160 | { |
| 161 | test_driver_mac_hooks.hits++; |
| 162 | |
| 163 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 164 | { |
| 165 | test_driver_mac_hooks.driver_status = |
| 166 | test_driver_mac_hooks.forced_status; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | test_driver_mac_hooks.driver_status = |
| 171 | mbedtls_transparent_test_driver_mac_verify_finish( |
| 172 | operation, mac, mac_length ); |
| 173 | } |
| 174 | |
| 175 | return( test_driver_mac_hooks.driver_status ); |
| 176 | } |
| 177 | |
| 178 | psa_status_t mbedtls_test_transparent_mac_abort( |
| 179 | mbedtls_transparent_test_driver_mac_operation_t *operation ) |
| 180 | { |
| 181 | test_driver_mac_hooks.hits++; |
| 182 | |
| 183 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 184 | { |
| 185 | test_driver_mac_hooks.driver_status = |
| 186 | test_driver_mac_hooks.forced_status; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | test_driver_mac_hooks.driver_status = |
| 191 | mbedtls_transparent_test_driver_mac_abort( operation ); |
| 192 | } |
| 193 | |
| 194 | return( test_driver_mac_hooks.driver_status ); |
| 195 | } |
| 196 | |
| 197 | psa_status_t mbedtls_test_opaque_mac_compute( |
| 198 | const psa_key_attributes_t *attributes, |
| 199 | const uint8_t *key_buffer, |
| 200 | size_t key_buffer_size, |
| 201 | psa_algorithm_t alg, |
| 202 | const uint8_t *input, |
| 203 | size_t input_length, |
| 204 | uint8_t *mac, |
| 205 | size_t mac_size, |
| 206 | size_t *mac_length ) |
| 207 | { |
| 208 | test_driver_mac_hooks.hits++; |
| 209 | |
| 210 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 211 | { |
| 212 | test_driver_mac_hooks.driver_status = |
| 213 | test_driver_mac_hooks.forced_status; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | test_driver_mac_hooks.driver_status = |
| 218 | mbedtls_opaque_test_driver_mac_compute( |
| 219 | attributes, key_buffer, key_buffer_size, alg, |
| 220 | input, input_length, |
| 221 | mac, mac_size, mac_length ); |
| 222 | } |
| 223 | |
| 224 | return( test_driver_mac_hooks.driver_status ); |
| 225 | } |
| 226 | |
| 227 | psa_status_t mbedtls_test_opaque_mac_sign_setup( |
| 228 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 229 | const psa_key_attributes_t *attributes, |
| 230 | const uint8_t *key_buffer, |
| 231 | size_t key_buffer_size, |
| 232 | psa_algorithm_t alg ) |
| 233 | { |
| 234 | test_driver_mac_hooks.hits++; |
| 235 | |
| 236 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 237 | { |
| 238 | test_driver_mac_hooks.driver_status = |
| 239 | test_driver_mac_hooks.forced_status; |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | test_driver_mac_hooks.driver_status = |
| 244 | mbedtls_opaque_test_driver_mac_sign_setup( |
| 245 | operation, attributes, key_buffer, key_buffer_size, alg ); |
| 246 | } |
| 247 | |
| 248 | return( test_driver_mac_hooks.driver_status ); |
| 249 | } |
| 250 | |
| 251 | psa_status_t mbedtls_test_opaque_mac_verify_setup( |
| 252 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 253 | const psa_key_attributes_t *attributes, |
| 254 | const uint8_t *key_buffer, |
| 255 | size_t key_buffer_size, |
| 256 | psa_algorithm_t alg ) |
| 257 | { |
| 258 | test_driver_mac_hooks.hits++; |
| 259 | |
| 260 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 261 | { |
| 262 | test_driver_mac_hooks.driver_status = |
| 263 | test_driver_mac_hooks.forced_status; |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | test_driver_mac_hooks.driver_status = |
| 268 | mbedtls_opaque_test_driver_mac_verify_setup( |
| 269 | operation, attributes, key_buffer, key_buffer_size, alg ); |
| 270 | } |
| 271 | |
| 272 | return( test_driver_mac_hooks.driver_status ); |
| 273 | } |
| 274 | |
| 275 | psa_status_t mbedtls_test_opaque_mac_update( |
| 276 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 277 | const uint8_t *input, |
| 278 | size_t input_length ) |
| 279 | { |
| 280 | test_driver_mac_hooks.hits++; |
| 281 | |
| 282 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 283 | { |
| 284 | test_driver_mac_hooks.driver_status = |
| 285 | test_driver_mac_hooks.forced_status; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | test_driver_mac_hooks.driver_status = |
| 290 | mbedtls_opaque_test_driver_mac_update( |
| 291 | operation, input, input_length ); |
| 292 | } |
| 293 | |
| 294 | return( test_driver_mac_hooks.driver_status ); |
| 295 | } |
| 296 | |
| 297 | psa_status_t mbedtls_test_opaque_mac_sign_finish( |
| 298 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 299 | uint8_t *mac, |
| 300 | size_t mac_size, |
| 301 | size_t *mac_length ) |
| 302 | { |
| 303 | test_driver_mac_hooks.hits++; |
| 304 | |
| 305 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 306 | { |
| 307 | test_driver_mac_hooks.driver_status = |
| 308 | test_driver_mac_hooks.forced_status; |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | test_driver_mac_hooks.driver_status = |
| 313 | mbedtls_opaque_test_driver_mac_sign_finish( |
| 314 | operation, mac, mac_size, mac_length ); |
| 315 | } |
| 316 | |
| 317 | return( test_driver_mac_hooks.driver_status ); |
| 318 | } |
| 319 | |
| 320 | psa_status_t mbedtls_test_opaque_mac_verify_finish( |
| 321 | mbedtls_opaque_test_driver_mac_operation_t *operation, |
| 322 | const uint8_t *mac, |
| 323 | size_t mac_length ) |
| 324 | { |
| 325 | test_driver_mac_hooks.hits++; |
| 326 | |
| 327 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 328 | { |
| 329 | test_driver_mac_hooks.driver_status = |
| 330 | test_driver_mac_hooks.forced_status; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | test_driver_mac_hooks.driver_status = |
| 335 | mbedtls_opaque_test_driver_mac_verify_finish( |
| 336 | operation, mac, mac_length ); |
| 337 | } |
| 338 | |
| 339 | return( test_driver_mac_hooks.driver_status ); |
| 340 | } |
| 341 | |
| 342 | psa_status_t mbedtls_test_opaque_mac_abort( |
| 343 | mbedtls_opaque_test_driver_mac_operation_t *operation ) |
| 344 | { |
| 345 | test_driver_mac_hooks.hits++; |
| 346 | |
| 347 | if( test_driver_mac_hooks.forced_status != PSA_SUCCESS ) |
| 348 | { |
| 349 | test_driver_mac_hooks.driver_status = |
| 350 | test_driver_mac_hooks.forced_status; |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | test_driver_mac_hooks.driver_status = |
| 355 | mbedtls_opaque_test_driver_mac_abort( operation ); |
| 356 | } |
| 357 | |
| 358 | return( test_driver_mac_hooks.driver_status ); |
| 359 | } |
| 360 | |
| 361 | #endif /* MBEDTLS_PSA_CRYPTO_DRIVERS && PSA_CRYPTO_DRIVER_TEST */ |