0.2.0-dev Download ZIP
458 lines ·14.1 KB Raw
| 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 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "tf_psa_crypto_common.h" |
| 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
| 13 | #include <psa/crypto.h> |
| 14 | #include "psa_crypto_core.h" |
| 15 | #include "psa_crypto_hash.h" |
| 16 | |
| 17 | #include <mbedtls/private/error_common.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 21 | psa_status_t mbedtls_psa_hash_abort( |
| 22 | mbedtls_psa_hash_operation_t *operation) |
| 23 | { |
| 24 | switch (operation->alg) { |
| 25 | case 0: |
| 26 | /* The object has (apparently) been initialized but it is not |
| 27 | * in use. It's ok to call abort on such an object, and there's |
| 28 | * nothing to do. */ |
| 29 | break; |
| 30 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 31 | case PSA_ALG_MD5: |
| 32 | mbedtls_md5_free(&operation->ctx.md5); |
| 33 | break; |
| 34 | #endif |
| 35 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 36 | case PSA_ALG_RIPEMD160: |
| 37 | mbedtls_ripemd160_free(&operation->ctx.ripemd160); |
| 38 | break; |
| 39 | #endif |
| 40 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 41 | case PSA_ALG_SHA_1: |
| 42 | mbedtls_sha1_free(&operation->ctx.sha1); |
| 43 | break; |
| 44 | #endif |
| 45 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 46 | case PSA_ALG_SHA_224: |
| 47 | mbedtls_sha256_free(&operation->ctx.sha256); |
| 48 | break; |
| 49 | #endif |
| 50 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 51 | case PSA_ALG_SHA_256: |
| 52 | mbedtls_sha256_free(&operation->ctx.sha256); |
| 53 | break; |
| 54 | #endif |
| 55 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 56 | case PSA_ALG_SHA_384: |
| 57 | mbedtls_sha512_free(&operation->ctx.sha512); |
| 58 | break; |
| 59 | #endif |
| 60 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 61 | case PSA_ALG_SHA_512: |
| 62 | mbedtls_sha512_free(&operation->ctx.sha512); |
| 63 | break; |
| 64 | #endif |
| 65 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) |
| 66 | case PSA_ALG_SHA3_224: |
| 67 | #endif |
| 68 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) |
| 69 | case PSA_ALG_SHA3_256: |
| 70 | #endif |
| 71 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) |
| 72 | case PSA_ALG_SHA3_384: |
| 73 | #endif |
| 74 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512) |
| 75 | case PSA_ALG_SHA3_512: |
| 76 | #endif |
| 77 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_SOME_HASH) |
| 78 | mbedtls_sha3_free(&operation->ctx.sha3); |
| 79 | break; |
| 80 | #endif |
| 81 | default: |
| 82 | return PSA_ERROR_BAD_STATE; |
| 83 | } |
| 84 | operation->alg = 0; |
| 85 | return PSA_SUCCESS; |
| 86 | } |
| 87 | |
| 88 | psa_status_t mbedtls_psa_hash_setup( |
| 89 | mbedtls_psa_hash_operation_t *operation, |
| 90 | psa_algorithm_t alg) |
| 91 | { |
| 92 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 93 | |
| 94 | /* A context must be freshly initialized before it can be set up. */ |
| 95 | if (operation->alg != 0) { |
| 96 | return PSA_ERROR_BAD_STATE; |
| 97 | } |
| 98 | |
| 99 | switch (alg) { |
| 100 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 101 | case PSA_ALG_MD5: |
| 102 | mbedtls_md5_init(&operation->ctx.md5); |
| 103 | ret = mbedtls_md5_starts(&operation->ctx.md5); |
| 104 | break; |
| 105 | #endif |
| 106 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 107 | case PSA_ALG_RIPEMD160: |
| 108 | mbedtls_ripemd160_init(&operation->ctx.ripemd160); |
| 109 | ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160); |
| 110 | break; |
| 111 | #endif |
| 112 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 113 | case PSA_ALG_SHA_1: |
| 114 | mbedtls_sha1_init(&operation->ctx.sha1); |
| 115 | ret = mbedtls_sha1_starts(&operation->ctx.sha1); |
| 116 | break; |
| 117 | #endif |
| 118 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 119 | case PSA_ALG_SHA_224: |
| 120 | mbedtls_sha256_init(&operation->ctx.sha256); |
| 121 | ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1); |
| 122 | break; |
| 123 | #endif |
| 124 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 125 | case PSA_ALG_SHA_256: |
| 126 | mbedtls_sha256_init(&operation->ctx.sha256); |
| 127 | ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0); |
| 128 | break; |
| 129 | #endif |
| 130 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 131 | case PSA_ALG_SHA_384: |
| 132 | mbedtls_sha512_init(&operation->ctx.sha512); |
| 133 | ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1); |
| 134 | break; |
| 135 | #endif |
| 136 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 137 | case PSA_ALG_SHA_512: |
| 138 | mbedtls_sha512_init(&operation->ctx.sha512); |
| 139 | ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0); |
| 140 | break; |
| 141 | #endif |
| 142 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) |
| 143 | case PSA_ALG_SHA3_224: |
| 144 | mbedtls_sha3_init(&operation->ctx.sha3); |
| 145 | ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_224); |
| 146 | break; |
| 147 | #endif |
| 148 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) |
| 149 | case PSA_ALG_SHA3_256: |
| 150 | mbedtls_sha3_init(&operation->ctx.sha3); |
| 151 | ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_256); |
| 152 | break; |
| 153 | #endif |
| 154 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) |
| 155 | case PSA_ALG_SHA3_384: |
| 156 | mbedtls_sha3_init(&operation->ctx.sha3); |
| 157 | ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_384); |
| 158 | break; |
| 159 | #endif |
| 160 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512) |
| 161 | case PSA_ALG_SHA3_512: |
| 162 | mbedtls_sha3_init(&operation->ctx.sha3); |
| 163 | ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_512); |
| 164 | break; |
| 165 | #endif |
| 166 | default: |
| 167 | return PSA_ALG_IS_HASH(alg) ? |
| 168 | PSA_ERROR_NOT_SUPPORTED : |
| 169 | PSA_ERROR_INVALID_ARGUMENT; |
| 170 | } |
| 171 | if (ret == 0) { |
| 172 | operation->alg = alg; |
| 173 | } else { |
| 174 | mbedtls_psa_hash_abort(operation); |
| 175 | } |
| 176 | return mbedtls_to_psa_error(ret); |
| 177 | } |
| 178 | |
| 179 | psa_status_t mbedtls_psa_hash_clone( |
| 180 | const mbedtls_psa_hash_operation_t *source_operation, |
| 181 | mbedtls_psa_hash_operation_t *target_operation) |
| 182 | { |
| 183 | switch (source_operation->alg) { |
| 184 | case 0: |
| 185 | return PSA_ERROR_BAD_STATE; |
| 186 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 187 | case PSA_ALG_MD5: |
| 188 | mbedtls_md5_clone(&target_operation->ctx.md5, |
| 189 | &source_operation->ctx.md5); |
| 190 | break; |
| 191 | #endif |
| 192 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 193 | case PSA_ALG_RIPEMD160: |
| 194 | mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160, |
| 195 | &source_operation->ctx.ripemd160); |
| 196 | break; |
| 197 | #endif |
| 198 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 199 | case PSA_ALG_SHA_1: |
| 200 | mbedtls_sha1_clone(&target_operation->ctx.sha1, |
| 201 | &source_operation->ctx.sha1); |
| 202 | break; |
| 203 | #endif |
| 204 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 205 | case PSA_ALG_SHA_224: |
| 206 | mbedtls_sha256_clone(&target_operation->ctx.sha256, |
| 207 | &source_operation->ctx.sha256); |
| 208 | break; |
| 209 | #endif |
| 210 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 211 | case PSA_ALG_SHA_256: |
| 212 | mbedtls_sha256_clone(&target_operation->ctx.sha256, |
| 213 | &source_operation->ctx.sha256); |
| 214 | break; |
| 215 | #endif |
| 216 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 217 | case PSA_ALG_SHA_384: |
| 218 | mbedtls_sha512_clone(&target_operation->ctx.sha512, |
| 219 | &source_operation->ctx.sha512); |
| 220 | break; |
| 221 | #endif |
| 222 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 223 | case PSA_ALG_SHA_512: |
| 224 | mbedtls_sha512_clone(&target_operation->ctx.sha512, |
| 225 | &source_operation->ctx.sha512); |
| 226 | break; |
| 227 | #endif |
| 228 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) |
| 229 | case PSA_ALG_SHA3_224: |
| 230 | #endif |
| 231 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) |
| 232 | case PSA_ALG_SHA3_256: |
| 233 | #endif |
| 234 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) |
| 235 | case PSA_ALG_SHA3_384: |
| 236 | #endif |
| 237 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512) |
| 238 | case PSA_ALG_SHA3_512: |
| 239 | #endif |
| 240 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_SOME_HASH) |
| 241 | mbedtls_sha3_clone(&target_operation->ctx.sha3, |
| 242 | &source_operation->ctx.sha3); |
| 243 | break; |
| 244 | #endif |
| 245 | default: |
| 246 | (void) source_operation; |
| 247 | (void) target_operation; |
| 248 | return PSA_ERROR_NOT_SUPPORTED; |
| 249 | } |
| 250 | |
| 251 | target_operation->alg = source_operation->alg; |
| 252 | return PSA_SUCCESS; |
| 253 | } |
| 254 | |
| 255 | psa_status_t mbedtls_psa_hash_update( |
| 256 | mbedtls_psa_hash_operation_t *operation, |
| 257 | const uint8_t *input, |
| 258 | size_t input_length) |
| 259 | { |
| 260 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 261 | |
| 262 | switch (operation->alg) { |
| 263 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 264 | case PSA_ALG_MD5: |
| 265 | ret = mbedtls_md5_update(&operation->ctx.md5, |
| 266 | input, input_length); |
| 267 | break; |
| 268 | #endif |
| 269 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 270 | case PSA_ALG_RIPEMD160: |
| 271 | ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160, |
| 272 | input, input_length); |
| 273 | break; |
| 274 | #endif |
| 275 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 276 | case PSA_ALG_SHA_1: |
| 277 | ret = mbedtls_sha1_update(&operation->ctx.sha1, |
| 278 | input, input_length); |
| 279 | break; |
| 280 | #endif |
| 281 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 282 | case PSA_ALG_SHA_224: |
| 283 | ret = mbedtls_sha256_update(&operation->ctx.sha256, |
| 284 | input, input_length); |
| 285 | break; |
| 286 | #endif |
| 287 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 288 | case PSA_ALG_SHA_256: |
| 289 | ret = mbedtls_sha256_update(&operation->ctx.sha256, |
| 290 | input, input_length); |
| 291 | break; |
| 292 | #endif |
| 293 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 294 | case PSA_ALG_SHA_384: |
| 295 | ret = mbedtls_sha512_update(&operation->ctx.sha512, |
| 296 | input, input_length); |
| 297 | break; |
| 298 | #endif |
| 299 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 300 | case PSA_ALG_SHA_512: |
| 301 | ret = mbedtls_sha512_update(&operation->ctx.sha512, |
| 302 | input, input_length); |
| 303 | break; |
| 304 | #endif |
| 305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) |
| 306 | case PSA_ALG_SHA3_224: |
| 307 | #endif |
| 308 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) |
| 309 | case PSA_ALG_SHA3_256: |
| 310 | #endif |
| 311 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) |
| 312 | case PSA_ALG_SHA3_384: |
| 313 | #endif |
| 314 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512) |
| 315 | case PSA_ALG_SHA3_512: |
| 316 | #endif |
| 317 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_SOME_HASH) |
| 318 | ret = mbedtls_sha3_update(&operation->ctx.sha3, |
| 319 | input, input_length); |
| 320 | break; |
| 321 | #endif |
| 322 | default: |
| 323 | (void) input; |
| 324 | (void) input_length; |
| 325 | return PSA_ERROR_BAD_STATE; |
| 326 | } |
| 327 | |
| 328 | return mbedtls_to_psa_error(ret); |
| 329 | } |
| 330 | |
| 331 | psa_status_t mbedtls_psa_hash_finish( |
| 332 | mbedtls_psa_hash_operation_t *operation, |
| 333 | uint8_t *hash, |
| 334 | size_t hash_size, |
| 335 | size_t *hash_length) |
| 336 | { |
| 337 | psa_status_t status; |
| 338 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 339 | size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg); |
| 340 | |
| 341 | /* Fill the output buffer with something that isn't a valid hash |
| 342 | * (barring an attack on the hash and deliberately-crafted input), |
| 343 | * in case the caller doesn't check the return status properly. */ |
| 344 | *hash_length = hash_size; |
| 345 | /* If hash_size is 0 then hash may be NULL and then the |
| 346 | * call to memset would have undefined behavior. */ |
| 347 | if (hash_size != 0) { |
| 348 | memset(hash, '!', hash_size); |
| 349 | } |
| 350 | |
| 351 | if (hash_size < actual_hash_length) { |
| 352 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 353 | goto exit; |
| 354 | } |
| 355 | |
| 356 | switch (operation->alg) { |
| 357 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
| 358 | case PSA_ALG_MD5: |
| 359 | ret = mbedtls_md5_finish(&operation->ctx.md5, hash); |
| 360 | break; |
| 361 | #endif |
| 362 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
| 363 | case PSA_ALG_RIPEMD160: |
| 364 | ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash); |
| 365 | break; |
| 366 | #endif |
| 367 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
| 368 | case PSA_ALG_SHA_1: |
| 369 | ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash); |
| 370 | break; |
| 371 | #endif |
| 372 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
| 373 | case PSA_ALG_SHA_224: |
| 374 | ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash); |
| 375 | break; |
| 376 | #endif |
| 377 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
| 378 | case PSA_ALG_SHA_256: |
| 379 | ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash); |
| 380 | break; |
| 381 | #endif |
| 382 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
| 383 | case PSA_ALG_SHA_384: |
| 384 | ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash); |
| 385 | break; |
| 386 | #endif |
| 387 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
| 388 | case PSA_ALG_SHA_512: |
| 389 | ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash); |
| 390 | break; |
| 391 | #endif |
| 392 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) |
| 393 | case PSA_ALG_SHA3_224: |
| 394 | #endif |
| 395 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) |
| 396 | case PSA_ALG_SHA3_256: |
| 397 | #endif |
| 398 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) |
| 399 | case PSA_ALG_SHA3_384: |
| 400 | #endif |
| 401 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512) |
| 402 | case PSA_ALG_SHA3_512: |
| 403 | #endif |
| 404 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_SOME_HASH) |
| 405 | ret = mbedtls_sha3_finish(&operation->ctx.sha3, hash, hash_size); |
| 406 | break; |
| 407 | #endif |
| 408 | default: |
| 409 | (void) hash; |
| 410 | return PSA_ERROR_BAD_STATE; |
| 411 | } |
| 412 | status = mbedtls_to_psa_error(ret); |
| 413 | |
| 414 | exit: |
| 415 | if (status == PSA_SUCCESS) { |
| 416 | *hash_length = actual_hash_length; |
| 417 | } |
| 418 | return status; |
| 419 | } |
| 420 | |
| 421 | psa_status_t mbedtls_psa_hash_compute( |
| 422 | psa_algorithm_t alg, |
| 423 | const uint8_t *input, |
| 424 | size_t input_length, |
| 425 | uint8_t *hash, |
| 426 | size_t hash_size, |
| 427 | size_t *hash_length) |
| 428 | { |
| 429 | mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT; |
| 430 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 431 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
| 432 | |
| 433 | *hash_length = hash_size; |
| 434 | status = mbedtls_psa_hash_setup(&operation, alg); |
| 435 | if (status != PSA_SUCCESS) { |
| 436 | goto exit; |
| 437 | } |
| 438 | status = mbedtls_psa_hash_update(&operation, input, input_length); |
| 439 | if (status != PSA_SUCCESS) { |
| 440 | goto exit; |
| 441 | } |
| 442 | status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length); |
| 443 | if (status != PSA_SUCCESS) { |
| 444 | goto exit; |
| 445 | } |
| 446 | |
| 447 | exit: |
| 448 | abort_status = mbedtls_psa_hash_abort(&operation); |
| 449 | if (status == PSA_SUCCESS) { |
| 450 | return abort_status; |
| 451 | } else { |
| 452 | return status; |
| 453 | } |
| 454 | |
| 455 | } |
| 456 | #endif /* MBEDTLS_PSA_BUILTIN_HASH */ |
| 457 | |
| 458 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |