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 | |
Steven Cooreman | 5f88e77 | 2021-03-15 11:07:12 +0100 | [diff] [blame] | 32 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \ |
| 33 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \ |
| 34 | defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \ |
| 35 | defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) |
| 36 | const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg ) |
| 37 | { |
| 38 | switch( alg ) |
| 39 | { |
| 40 | #if defined(MBEDTLS_MD2_C) |
| 41 | case PSA_ALG_MD2: |
| 42 | return( &mbedtls_md2_info ); |
| 43 | #endif |
| 44 | #if defined(MBEDTLS_MD4_C) |
| 45 | case PSA_ALG_MD4: |
| 46 | return( &mbedtls_md4_info ); |
| 47 | #endif |
| 48 | #if defined(MBEDTLS_MD5_C) |
| 49 | case PSA_ALG_MD5: |
| 50 | return( &mbedtls_md5_info ); |
| 51 | #endif |
| 52 | #if defined(MBEDTLS_RIPEMD160_C) |
| 53 | case PSA_ALG_RIPEMD160: |
| 54 | return( &mbedtls_ripemd160_info ); |
| 55 | #endif |
| 56 | #if defined(MBEDTLS_SHA1_C) |
| 57 | case PSA_ALG_SHA_1: |
| 58 | return( &mbedtls_sha1_info ); |
| 59 | #endif |
| 60 | #if defined(MBEDTLS_SHA256_C) |
| 61 | case PSA_ALG_SHA_224: |
| 62 | return( &mbedtls_sha224_info ); |
| 63 | #endif |
| 64 | #if defined(MBEDTLS_SHA256_C) |
| 65 | case PSA_ALG_SHA_256: |
| 66 | return( &mbedtls_sha256_info ); |
| 67 | #endif |
| 68 | #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384) |
| 69 | case PSA_ALG_SHA_384: |
| 70 | return( &mbedtls_sha384_info ); |
| 71 | #endif |
| 72 | #if defined(MBEDTLS_SHA512_C) |
| 73 | case PSA_ALG_SHA_512: |
| 74 | return( &mbedtls_sha512_info ); |
| 75 | #endif |
| 76 | default: |
| 77 | return( NULL ); |
| 78 | } |
| 79 | } |
| 80 | #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || |
| 81 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || |
| 82 | * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || |
| 83 | * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */ |
| 84 | |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 85 | /* Implement the PSA driver hash interface on top of mbed TLS if either the |
| 86 | * software driver or the test driver requires it. */ |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 87 | #if defined(MBEDTLS_PSA_BUILTIN_HASH) |
| 88 | psa_status_t mbedtls_psa_hash_abort( |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 89 | mbedtls_psa_hash_operation_t *operation ) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 90 | { |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 91 | switch( operation->alg ) |
| 92 | { |
| 93 | case 0: |
| 94 | /* The object has (apparently) been initialized but it is not |
| 95 | * in use. It's ok to call abort on such an object, and there's |
| 96 | * nothing to do. */ |
| 97 | break; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 98 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 99 | case PSA_ALG_MD2: |
| 100 | mbedtls_md2_free( &operation->ctx.md2 ); |
| 101 | break; |
| 102 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 103 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 104 | case PSA_ALG_MD4: |
| 105 | mbedtls_md4_free( &operation->ctx.md4 ); |
| 106 | break; |
| 107 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 108 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 109 | case PSA_ALG_MD5: |
| 110 | mbedtls_md5_free( &operation->ctx.md5 ); |
| 111 | break; |
| 112 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 113 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 114 | case PSA_ALG_RIPEMD160: |
| 115 | mbedtls_ripemd160_free( &operation->ctx.ripemd160 ); |
| 116 | break; |
| 117 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 118 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 119 | case PSA_ALG_SHA_1: |
| 120 | mbedtls_sha1_free( &operation->ctx.sha1 ); |
| 121 | break; |
| 122 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 123 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 124 | case PSA_ALG_SHA_224: |
| 125 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 126 | break; |
| 127 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 128 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 129 | case PSA_ALG_SHA_256: |
| 130 | mbedtls_sha256_free( &operation->ctx.sha256 ); |
| 131 | break; |
| 132 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 133 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 134 | case PSA_ALG_SHA_384: |
| 135 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 136 | break; |
| 137 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 138 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 139 | case PSA_ALG_SHA_512: |
| 140 | mbedtls_sha512_free( &operation->ctx.sha512 ); |
| 141 | break; |
| 142 | #endif |
| 143 | default: |
| 144 | return( PSA_ERROR_BAD_STATE ); |
| 145 | } |
| 146 | operation->alg = 0; |
| 147 | return( PSA_SUCCESS ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 150 | psa_status_t mbedtls_psa_hash_setup( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 151 | mbedtls_psa_hash_operation_t *operation, |
| 152 | psa_algorithm_t alg ) |
| 153 | { |
| 154 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 155 | |
| 156 | /* A context must be freshly initialized before it can be set up. */ |
| 157 | if( operation->alg != 0 ) |
| 158 | { |
| 159 | return( PSA_ERROR_BAD_STATE ); |
| 160 | } |
| 161 | |
| 162 | switch( alg ) |
| 163 | { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 164 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 165 | case PSA_ALG_MD2: |
| 166 | mbedtls_md2_init( &operation->ctx.md2 ); |
| 167 | ret = mbedtls_md2_starts_ret( &operation->ctx.md2 ); |
| 168 | break; |
| 169 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 170 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 171 | case PSA_ALG_MD4: |
| 172 | mbedtls_md4_init( &operation->ctx.md4 ); |
| 173 | ret = mbedtls_md4_starts_ret( &operation->ctx.md4 ); |
| 174 | break; |
| 175 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 176 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 177 | case PSA_ALG_MD5: |
| 178 | mbedtls_md5_init( &operation->ctx.md5 ); |
| 179 | ret = mbedtls_md5_starts_ret( &operation->ctx.md5 ); |
| 180 | break; |
| 181 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 182 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 183 | case PSA_ALG_RIPEMD160: |
| 184 | mbedtls_ripemd160_init( &operation->ctx.ripemd160 ); |
| 185 | ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 ); |
| 186 | break; |
| 187 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 188 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 189 | case PSA_ALG_SHA_1: |
| 190 | mbedtls_sha1_init( &operation->ctx.sha1 ); |
| 191 | ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 ); |
| 192 | break; |
| 193 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 194 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 195 | case PSA_ALG_SHA_224: |
| 196 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 197 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 ); |
| 198 | break; |
| 199 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 200 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 201 | case PSA_ALG_SHA_256: |
| 202 | mbedtls_sha256_init( &operation->ctx.sha256 ); |
| 203 | ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 ); |
| 204 | break; |
| 205 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 206 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 207 | case PSA_ALG_SHA_384: |
| 208 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 209 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 ); |
| 210 | break; |
| 211 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 212 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 213 | case PSA_ALG_SHA_512: |
| 214 | mbedtls_sha512_init( &operation->ctx.sha512 ); |
| 215 | ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 ); |
| 216 | break; |
| 217 | #endif |
| 218 | default: |
| 219 | return( PSA_ALG_IS_HASH( alg ) ? |
| 220 | PSA_ERROR_NOT_SUPPORTED : |
| 221 | PSA_ERROR_INVALID_ARGUMENT ); |
| 222 | } |
| 223 | if( ret == 0 ) |
| 224 | operation->alg = alg; |
| 225 | else |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 226 | mbedtls_psa_hash_abort( operation ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 227 | return( mbedtls_to_psa_error( ret ) ); |
| 228 | } |
| 229 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 230 | psa_status_t mbedtls_psa_hash_clone( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 231 | const mbedtls_psa_hash_operation_t *source_operation, |
| 232 | mbedtls_psa_hash_operation_t *target_operation ) |
| 233 | { |
| 234 | switch( source_operation->alg ) |
| 235 | { |
| 236 | case 0: |
| 237 | return( PSA_ERROR_BAD_STATE ); |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 238 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 239 | case PSA_ALG_MD2: |
| 240 | mbedtls_md2_clone( &target_operation->ctx.md2, |
| 241 | &source_operation->ctx.md2 ); |
| 242 | break; |
| 243 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 244 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 245 | case PSA_ALG_MD4: |
| 246 | mbedtls_md4_clone( &target_operation->ctx.md4, |
| 247 | &source_operation->ctx.md4 ); |
| 248 | break; |
| 249 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 250 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 251 | case PSA_ALG_MD5: |
| 252 | mbedtls_md5_clone( &target_operation->ctx.md5, |
| 253 | &source_operation->ctx.md5 ); |
| 254 | break; |
| 255 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 256 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 257 | case PSA_ALG_RIPEMD160: |
| 258 | mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160, |
| 259 | &source_operation->ctx.ripemd160 ); |
| 260 | break; |
| 261 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 262 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 263 | case PSA_ALG_SHA_1: |
| 264 | mbedtls_sha1_clone( &target_operation->ctx.sha1, |
| 265 | &source_operation->ctx.sha1 ); |
| 266 | break; |
| 267 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 268 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 269 | case PSA_ALG_SHA_224: |
| 270 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 271 | &source_operation->ctx.sha256 ); |
| 272 | break; |
| 273 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 274 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 275 | case PSA_ALG_SHA_256: |
| 276 | mbedtls_sha256_clone( &target_operation->ctx.sha256, |
| 277 | &source_operation->ctx.sha256 ); |
| 278 | break; |
| 279 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 280 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 281 | case PSA_ALG_SHA_384: |
| 282 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 283 | &source_operation->ctx.sha512 ); |
| 284 | break; |
| 285 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 286 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 287 | case PSA_ALG_SHA_512: |
| 288 | mbedtls_sha512_clone( &target_operation->ctx.sha512, |
| 289 | &source_operation->ctx.sha512 ); |
| 290 | break; |
| 291 | #endif |
| 292 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 293 | (void) source_operation; |
| 294 | (void) target_operation; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 295 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 296 | } |
| 297 | |
| 298 | target_operation->alg = source_operation->alg; |
| 299 | return( PSA_SUCCESS ); |
| 300 | } |
| 301 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 302 | psa_status_t mbedtls_psa_hash_update( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 303 | mbedtls_psa_hash_operation_t *operation, |
| 304 | const uint8_t *input, |
| 305 | size_t input_length ) |
| 306 | { |
| 307 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 308 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 309 | switch( operation->alg ) |
| 310 | { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 311 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 312 | case PSA_ALG_MD2: |
| 313 | ret = mbedtls_md2_update_ret( &operation->ctx.md2, |
| 314 | input, input_length ); |
| 315 | break; |
| 316 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 317 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 318 | case PSA_ALG_MD4: |
| 319 | ret = mbedtls_md4_update_ret( &operation->ctx.md4, |
| 320 | input, input_length ); |
| 321 | break; |
| 322 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 323 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 324 | case PSA_ALG_MD5: |
| 325 | ret = mbedtls_md5_update_ret( &operation->ctx.md5, |
| 326 | input, input_length ); |
| 327 | break; |
| 328 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 329 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 330 | case PSA_ALG_RIPEMD160: |
| 331 | ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160, |
| 332 | input, input_length ); |
| 333 | break; |
| 334 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 335 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 336 | case PSA_ALG_SHA_1: |
| 337 | ret = mbedtls_sha1_update_ret( &operation->ctx.sha1, |
| 338 | input, input_length ); |
| 339 | break; |
| 340 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 341 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 342 | case PSA_ALG_SHA_224: |
| 343 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 344 | input, input_length ); |
| 345 | break; |
| 346 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 347 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 348 | case PSA_ALG_SHA_256: |
| 349 | ret = mbedtls_sha256_update_ret( &operation->ctx.sha256, |
| 350 | input, input_length ); |
| 351 | break; |
| 352 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 353 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 354 | case PSA_ALG_SHA_384: |
| 355 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 356 | input, input_length ); |
| 357 | break; |
| 358 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 359 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 360 | case PSA_ALG_SHA_512: |
| 361 | ret = mbedtls_sha512_update_ret( &operation->ctx.sha512, |
| 362 | input, input_length ); |
| 363 | break; |
| 364 | #endif |
| 365 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 366 | (void) input; |
| 367 | (void) input_length; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 368 | return( PSA_ERROR_BAD_STATE ); |
| 369 | } |
| 370 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 371 | return( mbedtls_to_psa_error( ret ) ); |
| 372 | } |
| 373 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 374 | psa_status_t mbedtls_psa_hash_finish( |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 375 | mbedtls_psa_hash_operation_t *operation, |
| 376 | uint8_t *hash, |
| 377 | size_t hash_size, |
| 378 | size_t *hash_length ) |
| 379 | { |
| 380 | psa_status_t status; |
| 381 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 382 | size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg ); |
| 383 | |
| 384 | /* Fill the output buffer with something that isn't a valid hash |
| 385 | * (barring an attack on the hash and deliberately-crafted input), |
| 386 | * in case the caller doesn't check the return status properly. */ |
| 387 | *hash_length = hash_size; |
| 388 | /* If hash_size is 0 then hash may be NULL and then the |
| 389 | * call to memset would have undefined behavior. */ |
| 390 | if( hash_size != 0 ) |
| 391 | memset( hash, '!', hash_size ); |
| 392 | |
| 393 | if( hash_size < actual_hash_length ) |
| 394 | { |
| 395 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 396 | goto exit; |
| 397 | } |
| 398 | |
| 399 | switch( operation->alg ) |
| 400 | { |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 401 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 402 | case PSA_ALG_MD2: |
| 403 | ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash ); |
| 404 | break; |
| 405 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 406 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 407 | case PSA_ALG_MD4: |
| 408 | ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash ); |
| 409 | break; |
| 410 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 411 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 412 | case PSA_ALG_MD5: |
| 413 | ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash ); |
| 414 | break; |
| 415 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 416 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 417 | case PSA_ALG_RIPEMD160: |
| 418 | ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash ); |
| 419 | break; |
| 420 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 421 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 422 | case PSA_ALG_SHA_1: |
| 423 | ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash ); |
| 424 | break; |
| 425 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 426 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 427 | case PSA_ALG_SHA_224: |
| 428 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 429 | break; |
| 430 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 431 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 432 | case PSA_ALG_SHA_256: |
| 433 | ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash ); |
| 434 | break; |
| 435 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 436 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 437 | case PSA_ALG_SHA_384: |
| 438 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 439 | break; |
| 440 | #endif |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 441 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 442 | case PSA_ALG_SHA_512: |
| 443 | ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash ); |
| 444 | break; |
| 445 | #endif |
| 446 | default: |
Steven Cooreman | 5adf52c | 2021-03-04 18:09:49 +0100 | [diff] [blame] | 447 | (void) hash; |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 448 | return( PSA_ERROR_BAD_STATE ); |
| 449 | } |
| 450 | status = mbedtls_to_psa_error( ret ); |
| 451 | |
| 452 | exit: |
| 453 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 454 | *hash_length = actual_hash_length; |
Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 455 | return( status ); |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 458 | psa_status_t mbedtls_psa_hash_compute( |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 459 | psa_algorithm_t alg, |
| 460 | const uint8_t *input, |
| 461 | size_t input_length, |
| 462 | uint8_t *hash, |
| 463 | size_t hash_size, |
| 464 | size_t *hash_length) |
| 465 | { |
| 466 | mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT; |
| 467 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 468 | psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED; |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 469 | |
| 470 | *hash_length = hash_size; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 471 | status = mbedtls_psa_hash_setup( &operation, alg ); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 472 | if( status != PSA_SUCCESS ) |
| 473 | goto exit; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 474 | status = mbedtls_psa_hash_update( &operation, input, input_length ); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 475 | if( status != PSA_SUCCESS ) |
| 476 | goto exit; |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 477 | status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length ); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 478 | if( status != PSA_SUCCESS ) |
| 479 | goto exit; |
| 480 | |
| 481 | exit: |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame^] | 482 | abort_status = mbedtls_psa_hash_abort( &operation ); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 483 | if( status == PSA_SUCCESS ) |
Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 484 | return( abort_status ); |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 485 | else |
Steven Cooreman | 61bb8fc | 2021-03-15 12:32:48 +0100 | [diff] [blame] | 486 | return( status ); |
| 487 | |
Steven Cooreman | 83f300e | 2021-03-08 17:09:48 +0100 | [diff] [blame] | 488 | } |
Steven Cooreman | 0d58666 | 2021-03-08 20:28:18 +0100 | [diff] [blame] | 489 | #endif /* MBEDTLS_PSA_BUILTIN_HASH */ |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 490 | |
Steven Cooreman | 0e30764 | 2021-02-18 16:18:32 +0100 | [diff] [blame] | 491 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |