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