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 | #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_hash.h" |
| 28 | |
| 29 | #include <mbedtls/error.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | psa_status_t mbedtls_psa_hash_compute( |
| 33 | psa_algorithm_t alg, |
| 34 | const uint8_t *input, |
| 35 | size_t input_length, |
| 36 | uint8_t *hash, |
| 37 | size_t hash_size, |
| 38 | size_t *hash_length) |
| 39 | { |
| 40 | mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT; |
| 41 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 42 | |
| 43 | *hash_length = hash_size; |
| 44 | status = mbedtls_psa_hash_setup( &operation, alg ); |
| 45 | if( status != PSA_SUCCESS ) |
| 46 | goto exit; |
| 47 | status = mbedtls_psa_hash_update( &operation, input, input_length ); |
| 48 | if( status != PSA_SUCCESS ) |
| 49 | goto exit; |
| 50 | status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length ); |
| 51 | if( status != PSA_SUCCESS ) |
| 52 | goto exit; |
| 53 | |
| 54 | exit: |
| 55 | if( status == PSA_SUCCESS ) |
| 56 | status = mbedtls_psa_hash_abort( &operation ); |
| 57 | else |
| 58 | mbedtls_psa_hash_abort( &operation ); |
| 59 | return( status ); |
| 60 | } |
| 61 | |
| 62 | psa_status_t mbedtls_psa_hash_setup( |
| 63 | mbedtls_psa_hash_operation_t *operation, |
| 64 | psa_algorithm_t alg ) |
| 65 | { |
| 66 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 67 | |
| 68 | /* A context must be freshly initialized before it can be set up. */ |
| 69 | if( operation->alg != 0 ) |
| 70 | { |
| 71 | return( PSA_ERROR_BAD_STATE ); |
| 72 | } |
| 73 | |
| 74 | switch( alg ) |
| 75 | { |
| 76 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
| 77 | case PSA_ALG_MD2: |
| 78 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 79 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 80 | break; |
| 81 | #endif |
| 82 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
| 83 | case PSA_ALG_MD4: |
| 84 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 85 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 86 | break; |
| 87 | #endif |
| 88 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 89 | case PSA_ALG_MD5: |
| 90 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 91 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 92 | break; |
| 93 | #endif |
| 94 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 95 | case PSA_ALG_RIPEMD160: |
| 96 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 97 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 98 | break; |
| 99 | #endif |
| 100 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 101 | case PSA_ALG_SHA_1: |
| 102 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 103 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 104 | break; |
| 105 | #endif |
| 106 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 107 | case PSA_ALG_SHA_224: |
| 108 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 109 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 110 | break; |
| 111 | #endif |
| 112 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 113 | case PSA_ALG_SHA_256: |
| 114 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 115 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 116 | break; |
| 117 | #endif |
| 118 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 119 | case PSA_ALG_SHA_384: |
| 120 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 121 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 122 | break; |
| 123 | #endif |
| 124 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 125 | case PSA_ALG_SHA_512: |
| 126 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 127 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 128 | break; |
| 129 | #endif |
| 130 | default: |
| 131 | return( PSA_ALG_IS_HASH( alg ) ? |
| 132 | PSA_ERROR_NOT_SUPPORTED : |
| 133 | PSA_ERROR_INVALID_ARGUMENT ); |
| 134 | } |
| 135 | if( ret == 0 ) |
| 136 | operation->alg = alg; |
| 137 | else |
| 138 | mbedtls_psa_hash_abort( operation ); |
| 139 | return( mbedtls_to_psa_error( ret ) ); |
| 140 | } |
| 141 | |
| 142 | psa_status_t mbedtls_psa_hash_clone( |
| 143 | const mbedtls_psa_hash_operation_t *source_operation, |
| 144 | mbedtls_psa_hash_operation_t *target_operation ) |
| 145 | { |
| 146 | switch( source_operation->alg ) |
| 147 | { |
| 148 | case 0: |
| 149 | return( PSA_ERROR_BAD_STATE ); |
| 150 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
| 151 | case PSA_ALG_MD2: |
| 152 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 153 | &source_operation->ctx.md2 ); |
| 154 | break; |
| 155 | #endif |
| 156 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
| 157 | case PSA_ALG_MD4: |
| 158 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 159 | &source_operation->ctx.md4 ); |
| 160 | break; |
| 161 | #endif |
| 162 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 163 | case PSA_ALG_MD5: |
| 164 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 165 | &source_operation->ctx.md5 ); |
| 166 | break; |
| 167 | #endif |
| 168 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 169 | case PSA_ALG_RIPEMD160: |
| 170 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 171 | &source_operation->ctx.ripemd160 ); |
| 172 | break; |
| 173 | #endif |
| 174 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 175 | case PSA_ALG_SHA_1: |
| 176 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 177 | &source_operation->ctx.sha1 ); |
| 178 | break; |
| 179 | #endif |
| 180 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 181 | case PSA_ALG_SHA_224: |
| 182 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 183 | &source_operation->ctx.sha256 ); |
| 184 | break; |
| 185 | #endif |
| 186 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 187 | case PSA_ALG_SHA_256: |
| 188 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 189 | &source_operation->ctx.sha256 ); |
| 190 | break; |
| 191 | #endif |
| 192 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 193 | case PSA_ALG_SHA_384: |
| 194 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 195 | &source_operation->ctx.sha512 ); |
| 196 | break; |
| 197 | #endif |
| 198 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 199 | case PSA_ALG_SHA_512: |
| 200 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 201 | &source_operation->ctx.sha512 ); |
| 202 | break; |
| 203 | #endif |
| 204 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 205 | (void) source_operation; |
| 206 | (void) target_operation; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 207 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 208 | } |
| 209 | |
| 210 | target_operation->alg = source_operation->alg; |
| 211 | return( PSA_SUCCESS ); |
| 212 | } |
| 213 | |
| 214 | psa_status_t mbedtls_psa_hash_update( |
| 215 | mbedtls_psa_hash_operation_t *operation, |
| 216 | const uint8_t *input, |
| 217 | size_t input_length ) |
| 218 | { |
| 219 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 220 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 221 | switch( operation->alg ) |
| 222 | { |
| 223 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
| 224 | case PSA_ALG_MD2: |
| 225 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 226 | input, input_length ); |
| 227 | break; |
| 228 | #endif |
| 229 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
| 230 | case PSA_ALG_MD4: |
| 231 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 232 | input, input_length ); |
| 233 | break; |
| 234 | #endif |
| 235 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 236 | case PSA_ALG_MD5: |
| 237 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 238 | input, input_length ); |
| 239 | break; |
| 240 | #endif |
| 241 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 242 | case PSA_ALG_RIPEMD160: |
| 243 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 244 | input, input_length ); |
| 245 | break; |
| 246 | #endif |
| 247 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 248 | case PSA_ALG_SHA_1: |
| 249 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 250 | input, input_length ); |
| 251 | break; |
| 252 | #endif |
| 253 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 254 | case PSA_ALG_SHA_224: |
| 255 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 256 | input, input_length ); |
| 257 | break; |
| 258 | #endif |
| 259 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 260 | case PSA_ALG_SHA_256: |
| 261 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 262 | input, input_length ); |
| 263 | break; |
| 264 | #endif |
| 265 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 266 | case PSA_ALG_SHA_384: |
| 267 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 268 | input, input_length ); |
| 269 | break; |
| 270 | #endif |
| 271 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 272 | case PSA_ALG_SHA_512: |
| 273 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 274 | input, input_length ); |
| 275 | break; |
| 276 | #endif |
| 277 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 278 | (void) input; |
| 279 | (void) input_length; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 280 | return( PSA_ERROR_BAD_STATE ); |
| 281 | } |
| 282 | |
| 283 | if( ret != 0 ) |
| 284 | mbedtls_psa_hash_abort( operation ); |
| 285 | return( mbedtls_to_psa_error( ret ) ); |
| 286 | } |
| 287 | |
| 288 | psa_status_t mbedtls_psa_hash_finish( |
| 289 | mbedtls_psa_hash_operation_t *operation, |
| 290 | uint8_t *hash, |
| 291 | size_t hash_size, |
| 292 | size_t *hash_length ) |
| 293 | { |
| 294 | psa_status_t status; |
| 295 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 296 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
| 297 | |
| 298 | /* Fill the output buffer with something that isn't a valid hash |
| 299 | * (barring an attack on the hash and deliberately-crafted input), |
| 300 | * in case the caller doesn't check the return status properly. */ |
| 301 | *hash_length = hash_size; |
| 302 | /* If hash_size is 0 then hash may be NULL and then the |
| 303 | * call to memset would have undefined behavior. */ |
| 304 | if( hash_size != 0 ) |
| 305 | memset( hash, '!', hash_size ); |
| 306 | |
| 307 | if( hash_size < actual_hash_length ) |
| 308 | { |
| 309 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 310 | goto exit; |
| 311 | } |
| 312 | |
| 313 | switch( operation->alg ) |
| 314 | { |
| 315 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
| 316 | case PSA_ALG_MD2: |
| 317 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 318 | break; |
| 319 | #endif |
| 320 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
| 321 | case PSA_ALG_MD4: |
| 322 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 323 | break; |
| 324 | #endif |
| 325 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 326 | case PSA_ALG_MD5: |
| 327 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 328 | break; |
| 329 | #endif |
| 330 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 331 | case PSA_ALG_RIPEMD160: |
| 332 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 333 | break; |
| 334 | #endif |
| 335 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 336 | case PSA_ALG_SHA_1: |
| 337 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 338 | break; |
| 339 | #endif |
| 340 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 341 | case PSA_ALG_SHA_224: |
| 342 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 343 | break; |
| 344 | #endif |
| 345 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 346 | case PSA_ALG_SHA_256: |
| 347 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 348 | break; |
| 349 | #endif |
| 350 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 351 | case PSA_ALG_SHA_384: |
| 352 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 353 | break; |
| 354 | #endif |
| 355 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 356 | case PSA_ALG_SHA_512: |
| 357 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 358 | break; |
| 359 | #endif |
| 360 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 361 | (void) hash; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 362 | return( PSA_ERROR_BAD_STATE ); |
| 363 | } |
| 364 | status = mbedtls_to_psa_error( ret ); |
| 365 | |
| 366 | exit: |
| 367 | if( status == PSA_SUCCESS ) |
| 368 | { |
| 369 | *hash_length = actual_hash_length; |
| 370 | return( mbedtls_psa_hash_abort( operation ) ); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | mbedtls_psa_hash_abort( operation ); |
| 375 | return( status ); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | psa_status_t mbedtls_psa_hash_abort( |
| 380 | mbedtls_psa_hash_operation_t *operation ) |
| 381 | { |
| 382 | switch( operation->alg ) |
| 383 | { |
| 384 | case 0: |
| 385 | /* The object has (apparently) been initialized but it is not |
| 386 | * in use. It's ok to call abort on such an object, and there's |
| 387 | * nothing to do. */ |
| 388 | break; |
| 389 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
| 390 | case PSA_ALG_MD2: |
| 391 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 392 | break; |
| 393 | #endif |
| 394 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
| 395 | case PSA_ALG_MD4: |
| 396 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 397 | break; |
| 398 | #endif |
| 399 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 400 | case PSA_ALG_MD5: |
| 401 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 402 | break; |
| 403 | #endif |
| 404 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 405 | case PSA_ALG_RIPEMD160: |
| 406 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 407 | break; |
| 408 | #endif |
| 409 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 410 | case PSA_ALG_SHA_1: |
| 411 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 412 | break; |
| 413 | #endif |
| 414 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 415 | case PSA_ALG_SHA_224: |
| 416 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 417 | break; |
| 418 | #endif |
| 419 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 420 | case PSA_ALG_SHA_256: |
| 421 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 422 | break; |
| 423 | #endif |
| 424 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 425 | case PSA_ALG_SHA_384: |
| 426 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 427 | break; |
| 428 | #endif |
| 429 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 430 | case PSA_ALG_SHA_512: |
| 431 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 432 | break; |
| 433 | #endif |
| 434 | default: |
| 435 | return( PSA_ERROR_BAD_STATE ); |
| 436 | } |
| 437 | operation->alg = 0; |
| 438 | return( PSA_SUCCESS ); |
| 439 | } |
| 440 | |
Steven Cooreman | f763810 | 2021-03-04 15:14:36 +0100 | [diff] [blame] | 441 | /* |
| 442 | * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY. |
| 443 | */ |
| 444 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 445 | |
| 446 | #if defined(MBEDTLS_PSA_ACCEL_ALG_MD2) || \ |
| 447 | defined(MBEDTLS_PSA_ACCEL_ALG_MD4) || \ |
| 448 | defined(MBEDTLS_PSA_ACCEL_ALG_MD5) || \ |
| 449 | defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) || \ |
| 450 | defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) || \ |
| 451 | defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) || \ |
| 452 | defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) || \ |
| 453 | defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) || \ |
| 454 | defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) |
| 455 | #define INCLUDE_HASH_TEST_DRIVER |
| 456 | #endif |
| 457 | |
| 458 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 459 | psa_status_t is_hash_accelerated( psa_algorithm_t alg ) |
| 460 | { |
| 461 | switch( alg ) |
| 462 | { |
| 463 | #if defined(MBEDTLS_PSA_ACCEL_ALG_MD2) |
| 464 | case PSA_ALG_MD2: |
| 465 | return( PSA_SUCCESS ); |
| 466 | #endif |
| 467 | #if defined(MBEDTLS_PSA_ACCEL_ALG_MD4) |
| 468 | case PSA_ALG_MD4: |
| 469 | return( PSA_SUCCESS ); |
| 470 | #endif |
| 471 | #if defined(MBEDTLS_PSA_ACCEL_ALG_MD5) |
| 472 | case PSA_ALG_MD5: |
| 473 | return( PSA_SUCCESS ); |
| 474 | #endif |
| 475 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) |
| 476 | case PSA_ALG_RIPEMD160: |
| 477 | return( PSA_SUCCESS ); |
| 478 | #endif |
| 479 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) |
| 480 | case PSA_ALG_SHA_1: |
| 481 | return( PSA_SUCCESS ); |
| 482 | #endif |
| 483 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) |
| 484 | case PSA_ALG_SHA_224: |
| 485 | return( PSA_SUCCESS ); |
| 486 | #endif |
| 487 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) |
| 488 | case PSA_ALG_SHA_256: |
| 489 | return( PSA_SUCCESS ); |
| 490 | #endif |
| 491 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) |
| 492 | case PSA_ALG_SHA_384: |
| 493 | return( PSA_SUCCESS ); |
| 494 | #endif |
| 495 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) |
| 496 | case PSA_ALG_SHA_512: |
| 497 | return( PSA_SUCCESS ); |
| 498 | #endif |
| 499 | default: |
| 500 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 501 | } |
| 502 | } |
| 503 | #endif /* INCLUDE_HASH_TEST_DRIVER */ |
| 504 | |
| 505 | psa_status_t test_transparent_hash_compute( |
| 506 | psa_algorithm_t alg, |
| 507 | const uint8_t *input, |
| 508 | size_t input_length, |
| 509 | uint8_t *hash, |
| 510 | size_t hash_size, |
| 511 | size_t *hash_length) |
| 512 | { |
| 513 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 514 | if( is_hash_accelerated( alg ) == PSA_SUCCESS ) |
| 515 | return( mbedtls_psa_hash_compute( alg, input, input_length, |
| 516 | hash, hash_size, hash_length ) ); |
| 517 | else |
| 518 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 519 | #else |
| 520 | (void) alg; |
| 521 | (void) input; |
| 522 | (void) input_length; |
| 523 | (void) hash; |
| 524 | (void) hash_size; |
| 525 | (void) hash_length; |
| 526 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 527 | #endif |
| 528 | } |
| 529 | |
| 530 | psa_status_t test_transparent_hash_setup( |
| 531 | test_transparent_hash_operation_t *operation, |
| 532 | psa_algorithm_t alg ) |
| 533 | { |
| 534 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 535 | if( is_hash_accelerated( alg ) == PSA_SUCCESS ) |
| 536 | return( mbedtls_psa_hash_setup( &operation->operation, alg ) ); |
| 537 | else |
| 538 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 539 | #else |
| 540 | (void) alg; |
| 541 | (void) operation; |
| 542 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 543 | #endif |
| 544 | } |
| 545 | |
| 546 | psa_status_t test_transparent_hash_clone( |
| 547 | const test_transparent_hash_operation_t *source_operation, |
| 548 | test_transparent_hash_operation_t *target_operation ) |
| 549 | { |
| 550 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 551 | if( is_hash_accelerated( source_operation->operation.alg ) == PSA_SUCCESS ) |
| 552 | return( mbedtls_psa_hash_clone( &source_operation->operation, |
| 553 | &target_operation->operation ) ); |
| 554 | else |
| 555 | return( PSA_ERROR_BAD_STATE ); |
| 556 | #else |
| 557 | (void) source_operation; |
| 558 | (void) target_operation; |
| 559 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 560 | #endif |
| 561 | } |
| 562 | |
| 563 | psa_status_t test_transparent_hash_update( |
| 564 | test_transparent_hash_operation_t *operation, |
| 565 | const uint8_t *input, |
| 566 | size_t input_length ) |
| 567 | { |
| 568 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 569 | if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS ) |
| 570 | return( mbedtls_psa_hash_update( &operation->operation, |
| 571 | input, input_length ) ); |
| 572 | else |
| 573 | return( PSA_ERROR_BAD_STATE ); |
| 574 | #else |
| 575 | (void) operation; |
| 576 | (void) input; |
| 577 | (void) input_length; |
| 578 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 579 | #endif |
| 580 | } |
| 581 | |
| 582 | psa_status_t test_transparent_hash_finish( |
| 583 | test_transparent_hash_operation_t *operation, |
| 584 | uint8_t *hash, |
| 585 | size_t hash_size, |
| 586 | size_t *hash_length ) |
| 587 | { |
| 588 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 589 | if( is_hash_accelerated( operation->operation.alg ) == PSA_SUCCESS ) |
| 590 | return( mbedtls_psa_hash_finish( &operation->operation, |
| 591 | hash, hash_size, hash_length ) ); |
| 592 | else |
| 593 | return( PSA_ERROR_BAD_STATE ); |
| 594 | #else |
| 595 | (void) operation; |
| 596 | (void) hash; |
| 597 | (void) hash_size; |
| 598 | (void) hash_length; |
| 599 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 600 | #endif |
| 601 | } |
| 602 | |
| 603 | psa_status_t test_transparent_hash_abort( |
| 604 | test_transparent_hash_operation_t *operation ) |
| 605 | { |
| 606 | #if defined(INCLUDE_HASH_TEST_DRIVER) |
| 607 | return( mbedtls_psa_hash_abort( &operation->operation ) ); |
| 608 | #else |
| 609 | (void) operation; |
| 610 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 611 | #endif |
| 612 | } |
| 613 | |
| 614 | #endif /* PSA_CRYPTO_DRIVER_TEST */ |
| 615 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 616 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |