blob: ef73320416e2697d40ed150452d46bc5dd7c5fe6 [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
Steven Cooreman5f88e772021-03-15 11:07:12 +010032#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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010036const mbedtls_md_info_t *mbedtls_md_info_from_psa(psa_algorithm_t alg)
Steven Cooreman5f88e772021-03-15 11:07:12 +010037{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010038 switch (alg) {
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#if defined(MBEDTLS_MD2_C)
40 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 return &mbedtls_md2_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010042#endif
43#if defined(MBEDTLS_MD4_C)
44 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 return &mbedtls_md4_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010046#endif
47#if defined(MBEDTLS_MD5_C)
48 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 return &mbedtls_md5_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010050#endif
51#if defined(MBEDTLS_RIPEMD160_C)
52 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 return &mbedtls_ripemd160_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010054#endif
55#if defined(MBEDTLS_SHA1_C)
56 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 return &mbedtls_sha1_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010058#endif
59#if defined(MBEDTLS_SHA256_C)
60 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 return &mbedtls_sha224_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010062#endif
63#if defined(MBEDTLS_SHA256_C)
64 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 return &mbedtls_sha256_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010066#endif
67#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
68 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 return &mbedtls_sha384_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010070#endif
71#if defined(MBEDTLS_SHA512_C)
72 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 return &mbedtls_sha512_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010074#endif
75 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 return NULL;
Steven Cooreman5f88e772021-03-15 11:07:12 +010077 }
78}
79#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
80 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
81 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
82 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
83
Ronald Croncfc3c7b2021-03-13 18:50:11 +010084#if defined(MBEDTLS_PSA_BUILTIN_HASH)
85psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 mbedtls_psa_hash_operation_t *operation)
Steven Cooreman0e307642021-02-18 16:18:32 +010087{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 switch (operation->alg) {
Steven Cooreman83f300e2021-03-08 17:09:48 +010089 case 0:
90 /* The object has (apparently) been initialized but it is not
91 * in use. It's ok to call abort on such an object, and there's
92 * nothing to do. */
93 break;
Ronald Croncfc3c7b2021-03-13 18:50:11 +010094#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman83f300e2021-03-08 17:09:48 +010095 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 mbedtls_md2_free(&operation->ctx.md2);
Steven Cooreman83f300e2021-03-08 17:09:48 +010097 break;
98#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +010099#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100100 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 mbedtls_md4_free(&operation->ctx.md4);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100102 break;
103#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100104#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100105 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_md5_free(&operation->ctx.md5);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100107 break;
108#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100109#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100110 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 mbedtls_ripemd160_free(&operation->ctx.ripemd160);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100112 break;
113#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100114#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100115 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_sha1_free(&operation->ctx.sha1);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100117 break;
118#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100119#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100120 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100122 break;
123#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100124#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100125 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100127 break;
128#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100129#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100130 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100132 break;
133#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100134#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100135 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100137 break;
138#endif
139 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return PSA_ERROR_BAD_STATE;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100141 }
142 operation->alg = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100144}
145
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100146psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100147 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 psa_algorithm_t alg)
Steven Cooreman0e307642021-02-18 16:18:32 +0100149{
150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
151
152 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 if (operation->alg != 0) {
154 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100155 }
156
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 switch (alg) {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100158#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100159 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_md2_init(&operation->ctx.md2);
161 ret = mbedtls_md2_starts_ret(&operation->ctx.md2);
Steven Cooreman0e307642021-02-18 16:18:32 +0100162 break;
163#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100164#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100165 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_md4_init(&operation->ctx.md4);
167 ret = mbedtls_md4_starts_ret(&operation->ctx.md4);
Steven Cooreman0e307642021-02-18 16:18:32 +0100168 break;
169#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100170#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100171 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 mbedtls_md5_init(&operation->ctx.md5);
173 ret = mbedtls_md5_starts_ret(&operation->ctx.md5);
Steven Cooreman0e307642021-02-18 16:18:32 +0100174 break;
175#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100176#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100177 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_ripemd160_init(&operation->ctx.ripemd160);
179 ret = mbedtls_ripemd160_starts_ret(&operation->ctx.ripemd160);
Steven Cooreman0e307642021-02-18 16:18:32 +0100180 break;
181#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100182#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100183 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 mbedtls_sha1_init(&operation->ctx.sha1);
185 ret = mbedtls_sha1_starts_ret(&operation->ctx.sha1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100186 break;
187#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100188#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100189 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_sha256_init(&operation->ctx.sha256);
191 ret = mbedtls_sha256_starts_ret(&operation->ctx.sha256, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100192 break;
193#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100194#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100195 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 mbedtls_sha256_init(&operation->ctx.sha256);
197 ret = mbedtls_sha256_starts_ret(&operation->ctx.sha256, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100198 break;
199#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100200#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100201 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 mbedtls_sha512_init(&operation->ctx.sha512);
203 ret = mbedtls_sha512_starts_ret(&operation->ctx.sha512, 1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100204 break;
205#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100206#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100207 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 mbedtls_sha512_init(&operation->ctx.sha512);
209 ret = mbedtls_sha512_starts_ret(&operation->ctx.sha512, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100210 break;
211#endif
212 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 return PSA_ALG_IS_HASH(alg) ?
214 PSA_ERROR_NOT_SUPPORTED :
215 PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman0e307642021-02-18 16:18:32 +0100216 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 if (ret == 0) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100218 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 } else {
220 mbedtls_psa_hash_abort(operation);
221 }
222 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100223}
224
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100225psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100226 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 mbedtls_psa_hash_operation_t *target_operation)
Steven Cooreman0e307642021-02-18 16:18:32 +0100228{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 switch (source_operation->alg) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100230 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 return PSA_ERROR_BAD_STATE;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100232#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100233 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 mbedtls_md2_clone(&target_operation->ctx.md2,
235 &source_operation->ctx.md2);
Steven Cooreman0e307642021-02-18 16:18:32 +0100236 break;
237#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100238#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100239 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 mbedtls_md4_clone(&target_operation->ctx.md4,
241 &source_operation->ctx.md4);
Steven Cooreman0e307642021-02-18 16:18:32 +0100242 break;
243#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100244#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100245 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 mbedtls_md5_clone(&target_operation->ctx.md5,
247 &source_operation->ctx.md5);
Steven Cooreman0e307642021-02-18 16:18:32 +0100248 break;
249#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100250#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100251 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
253 &source_operation->ctx.ripemd160);
Steven Cooreman0e307642021-02-18 16:18:32 +0100254 break;
255#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100256#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100257 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 mbedtls_sha1_clone(&target_operation->ctx.sha1,
259 &source_operation->ctx.sha1);
Steven Cooreman0e307642021-02-18 16:18:32 +0100260 break;
261#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100262#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100263 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 mbedtls_sha256_clone(&target_operation->ctx.sha256,
265 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100266 break;
267#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100268#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100269 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 mbedtls_sha256_clone(&target_operation->ctx.sha256,
271 &source_operation->ctx.sha256);
Steven Cooreman0e307642021-02-18 16:18:32 +0100272 break;
273#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100274#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100275 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 mbedtls_sha512_clone(&target_operation->ctx.sha512,
277 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100278 break;
279#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100280#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100281 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 mbedtls_sha512_clone(&target_operation->ctx.sha512,
283 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100284 break;
285#endif
286 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100287 (void) source_operation;
288 (void) target_operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman0e307642021-02-18 16:18:32 +0100290 }
291
292 target_operation->alg = source_operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100294}
295
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100296psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100297 mbedtls_psa_hash_operation_t *operation,
298 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 size_t input_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100300{
301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
302
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 switch (operation->alg) {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100304#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100305 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 ret = mbedtls_md2_update_ret(&operation->ctx.md2,
307 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100308 break;
309#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100310#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100311 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 ret = mbedtls_md4_update_ret(&operation->ctx.md4,
313 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100314 break;
315#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100316#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100317 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 ret = mbedtls_md5_update_ret(&operation->ctx.md5,
319 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100320 break;
321#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100322#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100323 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 ret = mbedtls_ripemd160_update_ret(&operation->ctx.ripemd160,
325 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100326 break;
327#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100328#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100329 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 ret = mbedtls_sha1_update_ret(&operation->ctx.sha1,
331 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100332 break;
333#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100334#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100335 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 ret = mbedtls_sha256_update_ret(&operation->ctx.sha256,
337 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100338 break;
339#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100340#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100341 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 ret = mbedtls_sha256_update_ret(&operation->ctx.sha256,
343 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100344 break;
345#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100346#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100347 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 ret = mbedtls_sha512_update_ret(&operation->ctx.sha512,
349 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100350 break;
351#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100352#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100353 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 ret = mbedtls_sha512_update_ret(&operation->ctx.sha512,
355 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100356 break;
357#endif
358 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100359 (void) input;
360 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100362 }
363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100365}
366
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100367psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100368 mbedtls_psa_hash_operation_t *operation,
369 uint8_t *hash,
370 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 size_t *hash_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100372{
373 psa_status_t status;
374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
Steven Cooreman0e307642021-02-18 16:18:32 +0100376
377 /* Fill the output buffer with something that isn't a valid hash
378 * (barring an attack on the hash and deliberately-crafted input),
379 * in case the caller doesn't check the return status properly. */
380 *hash_length = hash_size;
381 /* If hash_size is 0 then hash may be NULL and then the
382 * call to memset would have undefined behavior. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if (hash_size != 0) {
384 memset(hash, '!', hash_size);
385 }
Steven Cooreman0e307642021-02-18 16:18:32 +0100386
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100387 if (hash_size < actual_hash_length) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100388 status = PSA_ERROR_BUFFER_TOO_SMALL;
389 goto exit;
390 }
391
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100392 switch (operation->alg) {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100393#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100394 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 ret = mbedtls_md2_finish_ret(&operation->ctx.md2, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100396 break;
397#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100398#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100399 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 ret = mbedtls_md4_finish_ret(&operation->ctx.md4, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100401 break;
402#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100403#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100404 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 ret = mbedtls_md5_finish_ret(&operation->ctx.md5, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100406 break;
407#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100408#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100409 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 ret = mbedtls_ripemd160_finish_ret(&operation->ctx.ripemd160, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100411 break;
412#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100413#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100414 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 ret = mbedtls_sha1_finish_ret(&operation->ctx.sha1, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100416 break;
417#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100418#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100419 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 ret = mbedtls_sha256_finish_ret(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100421 break;
422#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100423#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100424 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 ret = mbedtls_sha256_finish_ret(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100426 break;
427#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100428#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100429 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 ret = mbedtls_sha512_finish_ret(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100431 break;
432#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100433#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100434 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 ret = mbedtls_sha512_finish_ret(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100436 break;
437#endif
438 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100439 (void) hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100441 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 status = mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100443
444exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 if (status == PSA_SUCCESS) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100446 *hash_length = actual_hash_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 }
448 return status;
Steven Cooreman0e307642021-02-18 16:18:32 +0100449}
450
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100451psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100452 psa_algorithm_t alg,
453 const uint8_t *input,
454 size_t input_length,
455 uint8_t *hash,
456 size_t hash_size,
457 size_t *hash_length)
458{
459 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
460 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100461 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100462
463 *hash_length = hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 status = mbedtls_psa_hash_setup(&operation, alg);
465 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100466 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 }
468 status = mbedtls_psa_hash_update(&operation, input, input_length);
469 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100470 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 }
472 status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
473 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100474 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 }
Steven Cooreman83f300e2021-03-08 17:09:48 +0100476
477exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 abort_status = mbedtls_psa_hash_abort(&operation);
479 if (status == PSA_SUCCESS) {
480 return abort_status;
481 } else {
482 return status;
483 }
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100484
Steven Cooreman83f300e2021-03-08 17:09:48 +0100485}
Steven Cooreman0d586662021-03-08 20:28:18 +0100486#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100487
Steven Cooreman0e307642021-02-18 16:18:32 +0100488#endif /* MBEDTLS_PSA_CRYPTO_C */