blob: ddf70949c48e6993e6ed000fc6ecb1f230561e2d [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
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
Ronald Cron0266cfe2021-03-13 18:50:11 +010032#if defined(MBEDTLS_PSA_BUILTIN_HASH)
33psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine449bd832023-01-11 14:50:10 +010034 mbedtls_psa_hash_operation_t *operation)
Steven Cooreman0e307642021-02-18 16:18:32 +010035{
Gilles Peskine449bd832023-01-11 14:50:10 +010036 switch (operation->alg) {
Steven Cooreman83f300e2021-03-08 17:09:48 +010037 case 0:
38 /* The object has (apparently) been initialized but it is not
39 * in use. It's ok to call abort on such an object, and there's
40 * nothing to do. */
41 break;
Ronald Cron0266cfe2021-03-13 18:50:11 +010042#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +010043 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_md5_free(&operation->ctx.md5);
Steven Cooreman83f300e2021-03-08 17:09:48 +010045 break;
46#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010047#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +010048 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_ripemd160_free(&operation->ctx.ripemd160);
Steven Cooreman83f300e2021-03-08 17:09:48 +010050 break;
51#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010052#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +010053 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_sha1_free(&operation->ctx.sha1);
Steven Cooreman83f300e2021-03-08 17:09:48 +010055 break;
56#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010057#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +010058 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +010060 break;
61#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010062#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +010063 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +010064 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +010065 break;
66#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010067#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +010068 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +010070 break;
71#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010072#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +010073 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +010075 break;
76#endif
77 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return PSA_ERROR_BAD_STATE;
Steven Cooreman83f300e2021-03-08 17:09:48 +010079 }
80 operation->alg = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +010082}
83
Ronald Cron0266cfe2021-03-13 18:50:11 +010084psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +010085 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +010086 psa_algorithm_t alg)
Steven Cooreman0e307642021-02-18 16:18:32 +010087{
88 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
89
90 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (operation->alg != 0) {
92 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +010093 }
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 switch (alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +010096#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +010097 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_md5_init(&operation->ctx.md5);
99 ret = mbedtls_md5_starts(&operation->ctx.md5);
Steven Cooreman0e307642021-02-18 16:18:32 +0100100 break;
101#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100102#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100103 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_ripemd160_init(&operation->ctx.ripemd160);
105 ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160);
Steven Cooreman0e307642021-02-18 16:18:32 +0100106 break;
107#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100108#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100109 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_sha1_init(&operation->ctx.sha1);
111 ret = mbedtls_sha1_starts(&operation->ctx.sha1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100112 break;
113#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100114#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100115 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_sha256_init(&operation->ctx.sha256);
117 ret = mbedtls_sha256_starts(&operation->ctx.sha256, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100118 break;
119#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100120#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100121 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_sha256_init(&operation->ctx.sha256);
123 ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100124 break;
125#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100126#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100127 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_sha512_init(&operation->ctx.sha512);
129 ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100130 break;
131#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100132#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100133 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_sha512_init(&operation->ctx.sha512);
135 ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100136 break;
137#endif
138 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 return PSA_ALG_IS_HASH(alg) ?
140 PSA_ERROR_NOT_SUPPORTED :
141 PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman0e307642021-02-18 16:18:32 +0100142 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (ret == 0) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100144 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 } else {
146 mbedtls_psa_hash_abort(operation);
147 }
148 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100149}
150
Ronald Cron0266cfe2021-03-13 18:50:11 +0100151psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100152 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_psa_hash_operation_t *target_operation)
Steven Cooreman0e307642021-02-18 16:18:32 +0100154{
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 switch (source_operation->alg) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100156 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_BAD_STATE;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100158#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100159 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_md5_clone(&target_operation->ctx.md5,
161 &source_operation->ctx.md5);
Steven Cooreman0e307642021-02-18 16:18:32 +0100162 break;
163#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100164#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100165 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
167 &source_operation->ctx.ripemd160);
Steven Cooreman0e307642021-02-18 16:18:32 +0100168 break;
169#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100170#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100171 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_sha1_clone(&target_operation->ctx.sha1,
173 &source_operation->ctx.sha1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100174 break;
175#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100176#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100177 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_sha256_clone(&target_operation->ctx.sha256,
179 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100180 break;
181#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100182#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100183 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_sha256_clone(&target_operation->ctx.sha256,
185 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100186 break;
187#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100188#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100189 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 mbedtls_sha512_clone(&target_operation->ctx.sha512,
191 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100192 break;
193#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100194#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100195 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_sha512_clone(&target_operation->ctx.sha512,
197 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100198 break;
199#endif
200 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100201 (void) source_operation;
202 (void) target_operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman0e307642021-02-18 16:18:32 +0100204 }
205
206 target_operation->alg = source_operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100208}
209
Ronald Cron0266cfe2021-03-13 18:50:11 +0100210psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100211 mbedtls_psa_hash_operation_t *operation,
212 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 size_t input_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100214{
215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 switch (operation->alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100218#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100219 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 ret = mbedtls_md5_update(&operation->ctx.md5,
221 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100222 break;
223#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100224#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100225 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160,
227 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100228 break;
229#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100230#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100231 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 ret = mbedtls_sha1_update(&operation->ctx.sha1,
233 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100234 break;
235#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100236#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100237 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 ret = mbedtls_sha256_update(&operation->ctx.sha256,
239 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100240 break;
241#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100242#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100243 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 ret = mbedtls_sha256_update(&operation->ctx.sha256,
245 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100246 break;
247#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100248#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100249 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 ret = mbedtls_sha512_update(&operation->ctx.sha512,
251 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100252 break;
253#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100254#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100255 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 ret = mbedtls_sha512_update(&operation->ctx.sha512,
257 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100258 break;
259#endif
260 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100261 (void) input;
262 (void) input_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100264 }
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100267}
268
Ronald Cron0266cfe2021-03-13 18:50:11 +0100269psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100270 mbedtls_psa_hash_operation_t *operation,
271 uint8_t *hash,
272 size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 size_t *hash_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100274{
275 psa_status_t status;
276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
Steven Cooreman0e307642021-02-18 16:18:32 +0100278
279 /* Fill the output buffer with something that isn't a valid hash
280 * (barring an attack on the hash and deliberately-crafted input),
281 * in case the caller doesn't check the return status properly. */
282 *hash_length = hash_size;
283 /* If hash_size is 0 then hash may be NULL and then the
284 * call to memset would have undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (hash_size != 0) {
286 memset(hash, '!', hash_size);
287 }
Steven Cooreman0e307642021-02-18 16:18:32 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (hash_size < actual_hash_length) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100290 status = PSA_ERROR_BUFFER_TOO_SMALL;
291 goto exit;
292 }
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 switch (operation->alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100295#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100296 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 ret = mbedtls_md5_finish(&operation->ctx.md5, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100298 break;
299#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100300#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100301 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash);
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_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100306 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100308 break;
309#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100310#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100311 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100313 break;
314#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100315#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100316 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100318 break;
319#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100320#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100321 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100323 break;
324#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100325#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100326 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100328 break;
329#endif
330 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100331 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100333 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 status = mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100335
336exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (status == PSA_SUCCESS) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100338 *hash_length = actual_hash_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 }
340 return status;
Steven Cooreman0e307642021-02-18 16:18:32 +0100341}
342
Ronald Cron0266cfe2021-03-13 18:50:11 +0100343psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100344 psa_algorithm_t alg,
345 const uint8_t *input,
346 size_t input_length,
347 uint8_t *hash,
348 size_t hash_size,
349 size_t *hash_length)
350{
351 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100353 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100354
355 *hash_length = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 status = mbedtls_psa_hash_setup(&operation, alg);
357 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100358 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 }
360 status = mbedtls_psa_hash_update(&operation, input, input_length);
361 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100362 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 }
364 status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
365 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100366 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 }
Steven Cooreman83f300e2021-03-08 17:09:48 +0100368
369exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 abort_status = mbedtls_psa_hash_abort(&operation);
371 if (status == PSA_SUCCESS) {
372 return abort_status;
373 } else {
374 return status;
375 }
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100376
Steven Cooreman83f300e2021-03-08 17:09:48 +0100377}
Steven Cooreman0d586662021-03-08 20:28:18 +0100378#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100379
Steven Cooreman0e307642021-02-18 16:18:32 +0100380#endif /* MBEDTLS_PSA_CRYPTO_C */