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