blob: eeb7666c1cb0b2fc37c2732ddd4299ce39d5ee4d [file] [log] [blame]
Steven Cooreman0e307642021-02-18 16:18:32 +01001/*
2 * PSA hashing layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Steven Cooreman0e307642021-02-18 16:18:32 +01007 */
8
9#include "common.h"
10
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include <psa/crypto.h>
14#include "psa_crypto_core.h"
15#include "psa_crypto_hash.h"
16
17#include <mbedtls/error.h>
18#include <string.h>
19
Ronald Cron0266cfe2021-03-13 18:50:11 +010020#if defined(MBEDTLS_PSA_BUILTIN_HASH)
21psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_psa_hash_operation_t *operation)
Steven Cooreman0e307642021-02-18 16:18:32 +010023{
Gilles Peskine449bd832023-01-11 14:50:10 +010024 switch (operation->alg) {
Steven Cooreman83f300e2021-03-08 17:09:48 +010025 case 0:
26 /* The object has (apparently) been initialized but it is not
27 * in use. It's ok to call abort on such an object, and there's
28 * nothing to do. */
29 break;
Ronald Cron0266cfe2021-03-13 18:50:11 +010030#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +010031 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +010032 mbedtls_md5_free(&operation->ctx.md5);
Steven Cooreman83f300e2021-03-08 17:09:48 +010033 break;
34#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010035#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +010036 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +010037 mbedtls_ripemd160_free(&operation->ctx.ripemd160);
Steven Cooreman83f300e2021-03-08 17:09:48 +010038 break;
39#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010040#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +010041 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_sha1_free(&operation->ctx.sha1);
Steven Cooreman83f300e2021-03-08 17:09:48 +010043 break;
44#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010045#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +010046 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +010048 break;
49#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010050#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +010051 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +010053 break;
54#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010055#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +010056 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +010057 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +010058 break;
59#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010060#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +010061 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +010063 break;
64#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +010065#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +010066 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010067#endif
68#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +010069 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010070#endif
71#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +010072 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010073#endif
74#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +010075 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010076#endif
77#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
Dave Rodgman09822a32023-06-26 11:11:23 +010078 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
79 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
80 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +010081 mbedtls_sha3_free(&operation->ctx.sha3);
82 break;
83#endif
Steven Cooreman83f300e2021-03-08 17:09:48 +010084 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return PSA_ERROR_BAD_STATE;
Steven Cooreman83f300e2021-03-08 17:09:48 +010086 }
87 operation->alg = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +010089}
90
Ronald Cron0266cfe2021-03-13 18:50:11 +010091psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +010092 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +010093 psa_algorithm_t alg)
Steven Cooreman0e307642021-02-18 16:18:32 +010094{
95 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
96
97 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (operation->alg != 0) {
99 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100100 }
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 switch (alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100103#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100104 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_md5_init(&operation->ctx.md5);
106 ret = mbedtls_md5_starts(&operation->ctx.md5);
Steven Cooreman0e307642021-02-18 16:18:32 +0100107 break;
108#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100109#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100110 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_ripemd160_init(&operation->ctx.ripemd160);
112 ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160);
Steven Cooreman0e307642021-02-18 16:18:32 +0100113 break;
114#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100115#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100116 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_sha1_init(&operation->ctx.sha1);
118 ret = mbedtls_sha1_starts(&operation->ctx.sha1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100119 break;
120#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100121#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100122 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_sha256_init(&operation->ctx.sha256);
124 ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100125 break;
126#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100127#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100128 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_sha256_init(&operation->ctx.sha256);
130 ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100131 break;
132#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100133#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100134 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_sha512_init(&operation->ctx.sha512);
136 ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100137 break;
138#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100139#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100140 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_sha512_init(&operation->ctx.sha512);
142 ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100143 break;
144#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100145#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100146 case PSA_ALG_SHA3_224:
147 mbedtls_sha3_init(&operation->ctx.sha3);
148 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_224);
149 break;
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100150#endif
151#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100152 case PSA_ALG_SHA3_256:
153 mbedtls_sha3_init(&operation->ctx.sha3);
154 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_256);
155 break;
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100156#endif
157#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100158 case PSA_ALG_SHA3_384:
159 mbedtls_sha3_init(&operation->ctx.sha3);
160 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_384);
161 break;
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100162#endif
163#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100164 case PSA_ALG_SHA3_512:
165 mbedtls_sha3_init(&operation->ctx.sha3);
166 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_512);
167 break;
168#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100169 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return PSA_ALG_IS_HASH(alg) ?
171 PSA_ERROR_NOT_SUPPORTED :
172 PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman0e307642021-02-18 16:18:32 +0100173 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (ret == 0) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100175 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else {
177 mbedtls_psa_hash_abort(operation);
178 }
179 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100180}
181
Ronald Cron0266cfe2021-03-13 18:50:11 +0100182psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100183 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_psa_hash_operation_t *target_operation)
Steven Cooreman0e307642021-02-18 16:18:32 +0100185{
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 switch (source_operation->alg) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100187 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_BAD_STATE;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100189#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100190 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 mbedtls_md5_clone(&target_operation->ctx.md5,
192 &source_operation->ctx.md5);
Steven Cooreman0e307642021-02-18 16:18:32 +0100193 break;
194#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100195#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100196 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
198 &source_operation->ctx.ripemd160);
Steven Cooreman0e307642021-02-18 16:18:32 +0100199 break;
200#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100201#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100202 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_sha1_clone(&target_operation->ctx.sha1,
204 &source_operation->ctx.sha1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100205 break;
206#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100207#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100208 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_sha256_clone(&target_operation->ctx.sha256,
210 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100211 break;
212#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100213#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100214 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_sha256_clone(&target_operation->ctx.sha256,
216 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100217 break;
218#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100219#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100220 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_sha512_clone(&target_operation->ctx.sha512,
222 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100223 break;
224#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100225#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100226 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_sha512_clone(&target_operation->ctx.sha512,
228 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100229 break;
230#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100231#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100232 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100233#endif
234#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100235 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100236#endif
237#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100238 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100239#endif
240#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100241 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100242#endif
243#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
Dave Rodgman09822a32023-06-26 11:11:23 +0100244 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
245 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
246 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100247 mbedtls_sha3_clone(&target_operation->ctx.sha3,
248 &source_operation->ctx.sha3);
249 break;
250#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100251 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100252 (void) source_operation;
253 (void) target_operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman0e307642021-02-18 16:18:32 +0100255 }
256
257 target_operation->alg = source_operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100259}
260
Ronald Cron0266cfe2021-03-13 18:50:11 +0100261psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100262 mbedtls_psa_hash_operation_t *operation,
263 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 size_t input_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100265{
266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 switch (operation->alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100269#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100270 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 ret = mbedtls_md5_update(&operation->ctx.md5,
272 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100273 break;
274#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100275#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100276 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160,
278 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100279 break;
280#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100281#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100282 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 ret = mbedtls_sha1_update(&operation->ctx.sha1,
284 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100285 break;
286#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100287#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100288 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 ret = mbedtls_sha256_update(&operation->ctx.sha256,
290 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100291 break;
292#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100293#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100294 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 ret = mbedtls_sha256_update(&operation->ctx.sha256,
296 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100297 break;
298#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100299#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100300 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 ret = mbedtls_sha512_update(&operation->ctx.sha512,
302 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100303 break;
304#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100305#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100306 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 ret = mbedtls_sha512_update(&operation->ctx.sha512,
308 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100309 break;
310#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100311#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100312 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100313#endif
314#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100315 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100316#endif
317#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100318 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100319#endif
320#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100321 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100322#endif
323#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
324 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
325 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
326 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman09822a32023-06-26 11:11:23 +0100327 ret = mbedtls_sha3_update(&operation->ctx.sha3,
328 input, input_length);
329 break;
Dave Rodgman98083c62023-06-25 23:27:45 +0100330#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100331 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100332 (void) input;
333 (void) input_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100338}
339
Ronald Cron0266cfe2021-03-13 18:50:11 +0100340psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100341 mbedtls_psa_hash_operation_t *operation,
342 uint8_t *hash,
343 size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 size_t *hash_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100345{
346 psa_status_t status;
347 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
Steven Cooreman0e307642021-02-18 16:18:32 +0100349
350 /* Fill the output buffer with something that isn't a valid hash
351 * (barring an attack on the hash and deliberately-crafted input),
352 * in case the caller doesn't check the return status properly. */
353 *hash_length = hash_size;
354 /* If hash_size is 0 then hash may be NULL and then the
355 * call to memset would have undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (hash_size != 0) {
357 memset(hash, '!', hash_size);
358 }
Steven Cooreman0e307642021-02-18 16:18:32 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (hash_size < actual_hash_length) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100361 status = PSA_ERROR_BUFFER_TOO_SMALL;
362 goto exit;
363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 switch (operation->alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100366#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100367 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 ret = mbedtls_md5_finish(&operation->ctx.md5, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100369 break;
370#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100371#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100372 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100374 break;
375#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100376#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100377 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100379 break;
380#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100381#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100382 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100384 break;
385#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100386#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100387 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100389 break;
390#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100391#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100392 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100394 break;
395#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100396#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100397 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100399 break;
400#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100401#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100402 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100403#endif
404#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100405 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100406#endif
407#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100408 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100409#endif
410#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100411 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100412#endif
413#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
414 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
415 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
416 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman09822a32023-06-26 11:11:23 +0100417 ret = mbedtls_sha3_finish(&operation->ctx.sha3, hash, hash_size);
418 break;
Dave Rodgman98083c62023-06-25 23:27:45 +0100419#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100420 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100421 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100423 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 status = mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100425
426exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (status == PSA_SUCCESS) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100428 *hash_length = actual_hash_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 }
430 return status;
Steven Cooreman0e307642021-02-18 16:18:32 +0100431}
432
Ronald Cron0266cfe2021-03-13 18:50:11 +0100433psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100434 psa_algorithm_t alg,
435 const uint8_t *input,
436 size_t input_length,
437 uint8_t *hash,
438 size_t hash_size,
439 size_t *hash_length)
440{
441 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100443 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100444
445 *hash_length = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 status = mbedtls_psa_hash_setup(&operation, alg);
447 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100448 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 }
450 status = mbedtls_psa_hash_update(&operation, input, input_length);
451 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 }
454 status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
455 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100456 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 }
Steven Cooreman83f300e2021-03-08 17:09:48 +0100458
459exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 abort_status = mbedtls_psa_hash_abort(&operation);
461 if (status == PSA_SUCCESS) {
462 return abort_status;
463 } else {
464 return status;
465 }
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100466
Steven Cooreman83f300e2021-03-08 17:09:48 +0100467}
Steven Cooreman0d586662021-03-08 20:28:18 +0100468#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100469
Steven Cooreman0e307642021-02-18 16:18:32 +0100470#endif /* MBEDTLS_PSA_CRYPTO_C */