| 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 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 32 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) | 
|  | 33 | psa_status_t mbedtls_psa_hash_abort( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | mbedtls_psa_hash_operation_t *operation) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 35 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | switch (operation->alg) { | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 37 | case 0: | 
|  | 38 | /* The object has (apparently) been initialized but it is not | 
|  | 39 | * in use. It's ok to call abort on such an object, and there's | 
|  | 40 | * nothing to do. */ | 
|  | 41 | break; | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 42 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 43 | case PSA_ALG_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | mbedtls_md5_free(&operation->ctx.md5); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 45 | break; | 
|  | 46 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 47 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 48 | case PSA_ALG_RIPEMD160: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | mbedtls_ripemd160_free(&operation->ctx.ripemd160); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 50 | break; | 
|  | 51 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 53 | case PSA_ALG_SHA_1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 54 | mbedtls_sha1_free(&operation->ctx.sha1); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 55 | break; | 
|  | 56 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 58 | case PSA_ALG_SHA_224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | mbedtls_sha256_free(&operation->ctx.sha256); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 60 | break; | 
|  | 61 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 62 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 63 | case PSA_ALG_SHA_256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 64 | mbedtls_sha256_free(&operation->ctx.sha256); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 65 | break; | 
|  | 66 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 67 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 68 | case PSA_ALG_SHA_384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | mbedtls_sha512_free(&operation->ctx.sha512); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 70 | break; | 
|  | 71 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 72 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 73 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | mbedtls_sha512_free(&operation->ctx.sha512); | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 75 | break; | 
|  | 76 | #endif | 
|  | 77 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | return PSA_ERROR_BAD_STATE; | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 79 | } | 
|  | 80 | operation->alg = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return PSA_SUCCESS; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 84 | psa_status_t mbedtls_psa_hash_setup( | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 85 | mbedtls_psa_hash_operation_t *operation, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | psa_algorithm_t alg) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 87 | { | 
|  | 88 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 89 |  | 
|  | 90 | /* A context must be freshly initialized before it can be set up. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | if (operation->alg != 0) { | 
|  | 92 | return PSA_ERROR_BAD_STATE; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | switch (alg) { | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 96 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 97 | case PSA_ALG_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | mbedtls_md5_init(&operation->ctx.md5); | 
|  | 99 | ret = mbedtls_md5_starts(&operation->ctx.md5); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 100 | break; | 
|  | 101 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 102 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 103 | case PSA_ALG_RIPEMD160: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | mbedtls_ripemd160_init(&operation->ctx.ripemd160); | 
|  | 105 | ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 106 | break; | 
|  | 107 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 108 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 109 | case PSA_ALG_SHA_1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | mbedtls_sha1_init(&operation->ctx.sha1); | 
|  | 111 | ret = mbedtls_sha1_starts(&operation->ctx.sha1); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 112 | break; | 
|  | 113 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 114 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 115 | case PSA_ALG_SHA_224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | mbedtls_sha256_init(&operation->ctx.sha256); | 
|  | 117 | ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 118 | break; | 
|  | 119 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 120 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 121 | case PSA_ALG_SHA_256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | mbedtls_sha256_init(&operation->ctx.sha256); | 
|  | 123 | ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 124 | break; | 
|  | 125 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 126 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 127 | case PSA_ALG_SHA_384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | mbedtls_sha512_init(&operation->ctx.sha512); | 
|  | 129 | ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 130 | break; | 
|  | 131 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 132 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 133 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | mbedtls_sha512_init(&operation->ctx.sha512); | 
|  | 135 | ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 136 | break; | 
|  | 137 | #endif | 
|  | 138 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | return PSA_ALG_IS_HASH(alg) ? | 
|  | 140 | PSA_ERROR_NOT_SUPPORTED : | 
|  | 141 | PSA_ERROR_INVALID_ARGUMENT; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 142 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | if (ret == 0) { | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 144 | operation->alg = alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | } else { | 
|  | 146 | mbedtls_psa_hash_abort(operation); | 
|  | 147 | } | 
|  | 148 | return mbedtls_to_psa_error(ret); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 151 | psa_status_t mbedtls_psa_hash_clone( | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 152 | const mbedtls_psa_hash_operation_t *source_operation, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | mbedtls_psa_hash_operation_t *target_operation) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 154 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | switch (source_operation->alg) { | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 156 | case 0: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | return PSA_ERROR_BAD_STATE; | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 158 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 159 | case PSA_ALG_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | mbedtls_md5_clone(&target_operation->ctx.md5, | 
|  | 161 | &source_operation->ctx.md5); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 162 | break; | 
|  | 163 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 164 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 165 | case PSA_ALG_RIPEMD160: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160, | 
|  | 167 | &source_operation->ctx.ripemd160); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 168 | break; | 
|  | 169 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 170 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 171 | case PSA_ALG_SHA_1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | mbedtls_sha1_clone(&target_operation->ctx.sha1, | 
|  | 173 | &source_operation->ctx.sha1); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 174 | break; | 
|  | 175 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 176 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 177 | case PSA_ALG_SHA_224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | mbedtls_sha256_clone(&target_operation->ctx.sha256, | 
|  | 179 | &source_operation->ctx.sha256); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 180 | break; | 
|  | 181 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 182 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 183 | case PSA_ALG_SHA_256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | mbedtls_sha256_clone(&target_operation->ctx.sha256, | 
|  | 185 | &source_operation->ctx.sha256); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 186 | break; | 
|  | 187 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 189 | case PSA_ALG_SHA_384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | mbedtls_sha512_clone(&target_operation->ctx.sha512, | 
|  | 191 | &source_operation->ctx.sha512); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 192 | break; | 
|  | 193 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 195 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | mbedtls_sha512_clone(&target_operation->ctx.sha512, | 
|  | 197 | &source_operation->ctx.sha512); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 198 | break; | 
|  | 199 | #endif | 
|  | 200 | default: | 
| Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 201 | (void) source_operation; | 
|  | 202 | (void) target_operation; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | return PSA_ERROR_NOT_SUPPORTED; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
|  | 206 | target_operation->alg = source_operation->alg; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | return PSA_SUCCESS; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 210 | psa_status_t mbedtls_psa_hash_update( | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 211 | mbedtls_psa_hash_operation_t *operation, | 
|  | 212 | const uint8_t *input, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 213 | size_t input_length) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 214 | { | 
|  | 215 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
|  | 216 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | switch (operation->alg) { | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 218 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 219 | case PSA_ALG_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 220 | ret = mbedtls_md5_update(&operation->ctx.md5, | 
|  | 221 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 222 | break; | 
|  | 223 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 224 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 225 | case PSA_ALG_RIPEMD160: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160, | 
|  | 227 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 228 | break; | 
|  | 229 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 230 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 231 | case PSA_ALG_SHA_1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | ret = mbedtls_sha1_update(&operation->ctx.sha1, | 
|  | 233 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 234 | break; | 
|  | 235 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 236 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 237 | case PSA_ALG_SHA_224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | ret = mbedtls_sha256_update(&operation->ctx.sha256, | 
|  | 239 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 240 | break; | 
|  | 241 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 242 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 243 | case PSA_ALG_SHA_256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 244 | ret = mbedtls_sha256_update(&operation->ctx.sha256, | 
|  | 245 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 246 | break; | 
|  | 247 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 248 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 249 | case PSA_ALG_SHA_384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | ret = mbedtls_sha512_update(&operation->ctx.sha512, | 
|  | 251 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 252 | break; | 
|  | 253 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 254 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 255 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | ret = mbedtls_sha512_update(&operation->ctx.sha512, | 
|  | 257 | input, input_length); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 258 | break; | 
|  | 259 | #endif | 
|  | 260 | default: | 
| Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 261 | (void) input; | 
|  | 262 | (void) input_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | return PSA_ERROR_BAD_STATE; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 264 | } | 
|  | 265 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | return mbedtls_to_psa_error(ret); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 269 | psa_status_t mbedtls_psa_hash_finish( | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 270 | mbedtls_psa_hash_operation_t *operation, | 
|  | 271 | uint8_t *hash, | 
|  | 272 | size_t hash_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | size_t *hash_length) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 274 | { | 
|  | 275 | psa_status_t status; | 
|  | 276 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 278 |  | 
|  | 279 | /* Fill the output buffer with something that isn't a valid hash | 
|  | 280 | * (barring an attack on the hash and deliberately-crafted input), | 
|  | 281 | * in case the caller doesn't check the return status properly. */ | 
|  | 282 | *hash_length = hash_size; | 
|  | 283 | /* If hash_size is 0 then hash may be NULL and then the | 
|  | 284 | * call to memset would have undefined behavior. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | if (hash_size != 0) { | 
|  | 286 | memset(hash, '!', hash_size); | 
|  | 287 | } | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 288 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | if (hash_size < actual_hash_length) { | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 290 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 291 | goto exit; | 
|  | 292 | } | 
|  | 293 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | switch (operation->alg) { | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 296 | case PSA_ALG_MD5: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | ret = mbedtls_md5_finish(&operation->ctx.md5, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 298 | break; | 
|  | 299 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 300 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 301 | case PSA_ALG_RIPEMD160: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 303 | break; | 
|  | 304 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 306 | case PSA_ALG_SHA_1: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 308 | break; | 
|  | 309 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 310 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 311 | case PSA_ALG_SHA_224: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 313 | break; | 
|  | 314 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 315 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 316 | case PSA_ALG_SHA_256: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 318 | break; | 
|  | 319 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 320 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 321 | case PSA_ALG_SHA_384: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 323 | break; | 
|  | 324 | #endif | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 325 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 326 | case PSA_ALG_SHA_512: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 327 | ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 328 | break; | 
|  | 329 | #endif | 
|  | 330 | default: | 
| Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 331 | (void) hash; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | return PSA_ERROR_BAD_STATE; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 333 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | status = mbedtls_to_psa_error(ret); | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 335 |  | 
|  | 336 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | if (status == PSA_SUCCESS) { | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 338 | *hash_length = actual_hash_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | } | 
|  | 340 | return status; | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 341 | } | 
|  | 342 |  | 
| Ronald Cron | 0266cfe | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 343 | psa_status_t mbedtls_psa_hash_compute( | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 344 | psa_algorithm_t alg, | 
|  | 345 | const uint8_t *input, | 
|  | 346 | size_t input_length, | 
|  | 347 | uint8_t *hash, | 
|  | 348 | size_t hash_size, | 
|  | 349 | size_t *hash_length) | 
|  | 350 | { | 
|  | 351 | mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT; | 
|  | 352 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 353 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 354 |  | 
|  | 355 | *hash_length = hash_size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | status = mbedtls_psa_hash_setup(&operation, alg); | 
|  | 357 | if (status != PSA_SUCCESS) { | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 358 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | } | 
|  | 360 | status = mbedtls_psa_hash_update(&operation, input, input_length); | 
|  | 361 | if (status != PSA_SUCCESS) { | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 362 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | } | 
|  | 364 | status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length); | 
|  | 365 | if (status != PSA_SUCCESS) { | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 366 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | } | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 368 |  | 
|  | 369 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | abort_status = mbedtls_psa_hash_abort(&operation); | 
|  | 371 | if (status == PSA_SUCCESS) { | 
|  | 372 | return abort_status; | 
|  | 373 | } else { | 
|  | 374 | return status; | 
|  | 375 | } | 
| Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 376 |  | 
| Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 377 | } | 
| Steven Cooreman | 0d58666 | 2021-03-08 20:28:18 +0100 | [diff] [blame] | 378 | #endif /* MBEDTLS_PSA_BUILTIN_HASH */ | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 379 |  | 
| Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 380 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |