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