blob: dad182616610b8904d29765d64ba3e46ed8eaf3c [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
Dave Rodgmanf66cd612023-06-26 11:02:12 +010077#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +010078 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010079#endif
80#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +010081 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010082#endif
83#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +010084 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010085#endif
86#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +010087 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +010088#endif
89#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
Dave Rodgman09822a32023-06-26 11:11:23 +010090 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
91 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
92 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +010093 mbedtls_sha3_free(&operation->ctx.sha3);
94 break;
95#endif
Steven Cooreman83f300e2021-03-08 17:09:48 +010096 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return PSA_ERROR_BAD_STATE;
Steven Cooreman83f300e2021-03-08 17:09:48 +010098 }
99 operation->alg = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100101}
102
Ronald Cron0266cfe2021-03-13 18:50:11 +0100103psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100104 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 psa_algorithm_t alg)
Steven Cooreman0e307642021-02-18 16:18:32 +0100106{
107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
108
109 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if (operation->alg != 0) {
111 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100112 }
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 switch (alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100115#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100116 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_md5_init(&operation->ctx.md5);
118 ret = mbedtls_md5_starts(&operation->ctx.md5);
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_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100122 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 mbedtls_ripemd160_init(&operation->ctx.ripemd160);
124 ret = mbedtls_ripemd160_starts(&operation->ctx.ripemd160);
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_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100128 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_sha1_init(&operation->ctx.sha1);
130 ret = mbedtls_sha1_starts(&operation->ctx.sha1);
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_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100134 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_sha256_init(&operation->ctx.sha256);
136 ret = mbedtls_sha256_starts(&operation->ctx.sha256, 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_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100140 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_sha256_init(&operation->ctx.sha256);
142 ret = mbedtls_sha256_starts(&operation->ctx.sha256, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100143 break;
144#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100145#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100146 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_sha512_init(&operation->ctx.sha512);
148 ret = mbedtls_sha512_starts(&operation->ctx.sha512, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100149 break;
150#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100151#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100152 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_sha512_init(&operation->ctx.sha512);
154 ret = mbedtls_sha512_starts(&operation->ctx.sha512, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100155 break;
156#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100157#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100158 case PSA_ALG_SHA3_224:
159 mbedtls_sha3_init(&operation->ctx.sha3);
160 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_224);
161 break;
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100162#endif
163#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100164 case PSA_ALG_SHA3_256:
165 mbedtls_sha3_init(&operation->ctx.sha3);
166 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_256);
167 break;
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100168#endif
169#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100170 case PSA_ALG_SHA3_384:
171 mbedtls_sha3_init(&operation->ctx.sha3);
172 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_384);
173 break;
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100174#endif
175#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100176 case PSA_ALG_SHA3_512:
177 mbedtls_sha3_init(&operation->ctx.sha3);
178 ret = mbedtls_sha3_starts(&operation->ctx.sha3, MBEDTLS_SHA3_512);
179 break;
180#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100181 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ALG_IS_HASH(alg) ?
183 PSA_ERROR_NOT_SUPPORTED :
184 PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman0e307642021-02-18 16:18:32 +0100185 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (ret == 0) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100187 operation->alg = alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 } else {
189 mbedtls_psa_hash_abort(operation);
190 }
191 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100192}
193
Ronald Cron0266cfe2021-03-13 18:50:11 +0100194psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100195 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_psa_hash_operation_t *target_operation)
Steven Cooreman0e307642021-02-18 16:18:32 +0100197{
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 switch (source_operation->alg) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100199 case 0:
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_ERROR_BAD_STATE;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100201#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100202 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_md5_clone(&target_operation->ctx.md5,
204 &source_operation->ctx.md5);
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_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100208 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
210 &source_operation->ctx.ripemd160);
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_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100214 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_sha1_clone(&target_operation->ctx.sha1,
216 &source_operation->ctx.sha1);
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_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100220 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_sha256_clone(&target_operation->ctx.sha256,
222 &source_operation->ctx.sha256);
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_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100226 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_sha256_clone(&target_operation->ctx.sha256,
228 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100229 break;
230#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100231#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100232 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_sha512_clone(&target_operation->ctx.sha512,
234 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100235 break;
236#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100237#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100238 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_sha512_clone(&target_operation->ctx.sha512,
240 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100241 break;
242#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100243#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100244 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100245#endif
246#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100247 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100248#endif
249#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100250 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100251#endif
252#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100253 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100254#endif
255#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
Dave Rodgman09822a32023-06-26 11:11:23 +0100256 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
257 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
258 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100259 mbedtls_sha3_clone(&target_operation->ctx.sha3,
260 &source_operation->ctx.sha3);
261 break;
262#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100263 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100264 (void) source_operation;
265 (void) target_operation;
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman0e307642021-02-18 16:18:32 +0100267 }
268
269 target_operation->alg = source_operation->alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100271}
272
Ronald Cron0266cfe2021-03-13 18:50:11 +0100273psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100274 mbedtls_psa_hash_operation_t *operation,
275 const uint8_t *input,
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 size_t input_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100277{
278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 switch (operation->alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100281#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100282 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 ret = mbedtls_md5_update(&operation->ctx.md5,
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_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100288 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 ret = mbedtls_ripemd160_update(&operation->ctx.ripemd160,
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_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100294 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 ret = mbedtls_sha1_update(&operation->ctx.sha1,
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_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100300 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 ret = mbedtls_sha256_update(&operation->ctx.sha256,
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_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100306 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 ret = mbedtls_sha256_update(&operation->ctx.sha256,
308 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100309 break;
310#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100311#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100312 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 ret = mbedtls_sha512_update(&operation->ctx.sha512,
314 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100315 break;
316#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100317#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100318 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 ret = mbedtls_sha512_update(&operation->ctx.sha512,
320 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100321 break;
322#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100323#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100324 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100325#endif
326#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100327 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100328#endif
329#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100330 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100331#endif
332#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100333 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100334#endif
335#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
336 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
337 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
338 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman09822a32023-06-26 11:11:23 +0100339 ret = mbedtls_sha3_update(&operation->ctx.sha3,
340 input, input_length);
341 break;
Dave Rodgman98083c62023-06-25 23:27:45 +0100342#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100343 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100344 (void) input;
345 (void) input_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100347 }
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100350}
351
Ronald Cron0266cfe2021-03-13 18:50:11 +0100352psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100353 mbedtls_psa_hash_operation_t *operation,
354 uint8_t *hash,
355 size_t hash_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 size_t *hash_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100357{
358 psa_status_t status;
359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
Steven Cooreman0e307642021-02-18 16:18:32 +0100361
362 /* Fill the output buffer with something that isn't a valid hash
363 * (barring an attack on the hash and deliberately-crafted input),
364 * in case the caller doesn't check the return status properly. */
365 *hash_length = hash_size;
366 /* If hash_size is 0 then hash may be NULL and then the
367 * call to memset would have undefined behavior. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (hash_size != 0) {
369 memset(hash, '!', hash_size);
370 }
Steven Cooreman0e307642021-02-18 16:18:32 +0100371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (hash_size < actual_hash_length) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100373 status = PSA_ERROR_BUFFER_TOO_SMALL;
374 goto exit;
375 }
376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 switch (operation->alg) {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100378#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100379 case PSA_ALG_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 ret = mbedtls_md5_finish(&operation->ctx.md5, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100381 break;
382#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100383#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100384 case PSA_ALG_RIPEMD160:
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 ret = mbedtls_ripemd160_finish(&operation->ctx.ripemd160, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100386 break;
387#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100388#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100389 case PSA_ALG_SHA_1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 ret = mbedtls_sha1_finish(&operation->ctx.sha1, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100391 break;
392#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100393#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100394 case PSA_ALG_SHA_224:
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100396 break;
397#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100398#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100399 case PSA_ALG_SHA_256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 ret = mbedtls_sha256_finish(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100401 break;
402#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100403#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100404 case PSA_ALG_SHA_384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100406 break;
407#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100408#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100409 case PSA_ALG_SHA_512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 ret = mbedtls_sha512_finish(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100411 break;
412#endif
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100413#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224)
Dave Rodgman98083c62023-06-25 23:27:45 +0100414 case PSA_ALG_SHA3_224:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100415#endif
416#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256)
Dave Rodgman98083c62023-06-25 23:27:45 +0100417 case PSA_ALG_SHA3_256:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100418#endif
419#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384)
Dave Rodgman98083c62023-06-25 23:27:45 +0100420 case PSA_ALG_SHA3_384:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100421#endif
422#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman98083c62023-06-25 23:27:45 +0100423 case PSA_ALG_SHA3_512:
Dave Rodgmanf66cd612023-06-26 11:02:12 +0100424#endif
425#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_224) || \
426 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_256) || \
427 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_384) || \
428 defined(MBEDTLS_PSA_BUILTIN_ALG_SHA3_512)
Dave Rodgman09822a32023-06-26 11:11:23 +0100429 ret = mbedtls_sha3_finish(&operation->ctx.sha3, hash, hash_size);
430 break;
Dave Rodgman98083c62023-06-25 23:27:45 +0100431#endif
Steven Cooreman0e307642021-02-18 16:18:32 +0100432 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100433 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100435 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 status = mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100437
438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (status == PSA_SUCCESS) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100440 *hash_length = actual_hash_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 }
442 return status;
Steven Cooreman0e307642021-02-18 16:18:32 +0100443}
444
Ronald Cron0266cfe2021-03-13 18:50:11 +0100445psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100446 psa_algorithm_t alg,
447 const uint8_t *input,
448 size_t input_length,
449 uint8_t *hash,
450 size_t hash_size,
451 size_t *hash_length)
452{
453 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100455 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100456
457 *hash_length = hash_size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 status = mbedtls_psa_hash_setup(&operation, alg);
459 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100460 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 }
462 status = mbedtls_psa_hash_update(&operation, input, input_length);
463 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100464 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
466 status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
467 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100468 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 }
Steven Cooreman83f300e2021-03-08 17:09:48 +0100470
471exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 abort_status = mbedtls_psa_hash_abort(&operation);
473 if (status == PSA_SUCCESS) {
474 return abort_status;
475 } else {
476 return status;
477 }
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100478
Steven Cooreman83f300e2021-03-08 17:09:48 +0100479}
Steven Cooreman0d586662021-03-08 20:28:18 +0100480#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100481
Steven Cooreman0e307642021-02-18 16:18:32 +0100482#endif /* MBEDTLS_PSA_CRYPTO_C */