blob: 484c81bc996a5ebb9904c1544a7c4811aefed765 [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 Rodgman7ff79652023-11-03 12:04:52 +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
Steven Cooreman5f88e772021-03-15 11:07:12 +010020#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
21 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
22 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
23 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010024const mbedtls_md_info_t *mbedtls_md_info_from_psa(psa_algorithm_t alg)
Steven Cooreman5f88e772021-03-15 11:07:12 +010025{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026 switch (alg) {
Steven Cooreman5f88e772021-03-15 11:07:12 +010027#if defined(MBEDTLS_MD2_C)
28 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029 return &mbedtls_md2_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010030#endif
31#if defined(MBEDTLS_MD4_C)
32 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010033 return &mbedtls_md4_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010034#endif
35#if defined(MBEDTLS_MD5_C)
36 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037 return &mbedtls_md5_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010038#endif
39#if defined(MBEDTLS_RIPEMD160_C)
40 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041 return &mbedtls_ripemd160_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010042#endif
43#if defined(MBEDTLS_SHA1_C)
44 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 return &mbedtls_sha1_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010046#endif
47#if defined(MBEDTLS_SHA256_C)
48 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 return &mbedtls_sha224_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010050#endif
51#if defined(MBEDTLS_SHA256_C)
52 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 return &mbedtls_sha256_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010054#endif
55#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
56 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 return &mbedtls_sha384_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010058#endif
59#if defined(MBEDTLS_SHA512_C)
60 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 return &mbedtls_sha512_info;
Steven Cooreman5f88e772021-03-15 11:07:12 +010062#endif
63 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 return NULL;
Steven Cooreman5f88e772021-03-15 11:07:12 +010065 }
66}
67#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
68 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
69 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
70 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
71
Ronald Croncfc3c7b2021-03-13 18:50:11 +010072#if defined(MBEDTLS_PSA_BUILTIN_HASH)
73psa_status_t mbedtls_psa_hash_abort(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 mbedtls_psa_hash_operation_t *operation)
Steven Cooreman0e307642021-02-18 16:18:32 +010075{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 switch (operation->alg) {
Steven Cooreman83f300e2021-03-08 17:09:48 +010077 case 0:
78 /* The object has (apparently) been initialized but it is not
79 * in use. It's ok to call abort on such an object, and there's
80 * nothing to do. */
81 break;
Ronald Croncfc3c7b2021-03-13 18:50:11 +010082#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman83f300e2021-03-08 17:09:48 +010083 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 mbedtls_md2_free(&operation->ctx.md2);
Steven Cooreman83f300e2021-03-08 17:09:48 +010085 break;
86#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +010087#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman83f300e2021-03-08 17:09:48 +010088 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 mbedtls_md4_free(&operation->ctx.md4);
Steven Cooreman83f300e2021-03-08 17:09:48 +010090 break;
91#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +010092#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +010093 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 mbedtls_md5_free(&operation->ctx.md5);
Steven Cooreman83f300e2021-03-08 17:09:48 +010095 break;
96#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +010097#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +010098 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 mbedtls_ripemd160_free(&operation->ctx.ripemd160);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100100 break;
101#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100102#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100103 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 mbedtls_sha1_free(&operation->ctx.sha1);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100105 break;
106#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100107#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100108 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100110 break;
111#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100112#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100113 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 mbedtls_sha256_free(&operation->ctx.sha256);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100115 break;
116#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100117#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100118 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100120 break;
121#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100122#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100123 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 mbedtls_sha512_free(&operation->ctx.sha512);
Steven Cooreman83f300e2021-03-08 17:09:48 +0100125 break;
126#endif
127 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 return PSA_ERROR_BAD_STATE;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100129 }
130 operation->alg = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100132}
133
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100134psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100135 mbedtls_psa_hash_operation_t *operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 psa_algorithm_t alg)
Steven Cooreman0e307642021-02-18 16:18:32 +0100137{
138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
139
140 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 if (operation->alg != 0) {
142 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100143 }
144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 switch (alg) {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100146#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100147 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 mbedtls_md2_init(&operation->ctx.md2);
149 ret = mbedtls_md2_starts_ret(&operation->ctx.md2);
Steven Cooreman0e307642021-02-18 16:18:32 +0100150 break;
151#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100152#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100153 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 mbedtls_md4_init(&operation->ctx.md4);
155 ret = mbedtls_md4_starts_ret(&operation->ctx.md4);
Steven Cooreman0e307642021-02-18 16:18:32 +0100156 break;
157#endif
Ronald Croncfc3c7b2021-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 Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_md5_init(&operation->ctx.md5);
161 ret = mbedtls_md5_starts_ret(&operation->ctx.md5);
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_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100165 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_ripemd160_init(&operation->ctx.ripemd160);
167 ret = mbedtls_ripemd160_starts_ret(&operation->ctx.ripemd160);
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_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100171 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 mbedtls_sha1_init(&operation->ctx.sha1);
173 ret = mbedtls_sha1_starts_ret(&operation->ctx.sha1);
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_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100177 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 mbedtls_sha256_init(&operation->ctx.sha256);
179 ret = mbedtls_sha256_starts_ret(&operation->ctx.sha256, 1);
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_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100183 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 mbedtls_sha256_init(&operation->ctx.sha256);
185 ret = mbedtls_sha256_starts_ret(&operation->ctx.sha256, 0);
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_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100189 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 mbedtls_sha512_init(&operation->ctx.sha512);
191 ret = mbedtls_sha512_starts_ret(&operation->ctx.sha512, 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_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100195 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 mbedtls_sha512_init(&operation->ctx.sha512);
197 ret = mbedtls_sha512_starts_ret(&operation->ctx.sha512, 0);
Steven Cooreman0e307642021-02-18 16:18:32 +0100198 break;
199#endif
200 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 return PSA_ALG_IS_HASH(alg) ?
202 PSA_ERROR_NOT_SUPPORTED :
203 PSA_ERROR_INVALID_ARGUMENT;
Steven Cooreman0e307642021-02-18 16:18:32 +0100204 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (ret == 0) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100206 operation->alg = alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 } else {
208 mbedtls_psa_hash_abort(operation);
209 }
210 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100211}
212
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100213psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100214 const mbedtls_psa_hash_operation_t *source_operation,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 mbedtls_psa_hash_operation_t *target_operation)
Steven Cooreman0e307642021-02-18 16:18:32 +0100216{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 switch (source_operation->alg) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100218 case 0:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 return PSA_ERROR_BAD_STATE;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100220#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100221 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 mbedtls_md2_clone(&target_operation->ctx.md2,
223 &source_operation->ctx.md2);
Steven Cooreman0e307642021-02-18 16:18:32 +0100224 break;
225#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100226#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100227 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 mbedtls_md4_clone(&target_operation->ctx.md4,
229 &source_operation->ctx.md4);
Steven Cooreman0e307642021-02-18 16:18:32 +0100230 break;
231#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100232#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100233 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 mbedtls_md5_clone(&target_operation->ctx.md5,
235 &source_operation->ctx.md5);
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_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100239 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 mbedtls_ripemd160_clone(&target_operation->ctx.ripemd160,
241 &source_operation->ctx.ripemd160);
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_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100245 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 mbedtls_sha1_clone(&target_operation->ctx.sha1,
247 &source_operation->ctx.sha1);
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_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100251 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 mbedtls_sha256_clone(&target_operation->ctx.sha256,
253 &source_operation->ctx.sha256);
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_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100257 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 mbedtls_sha256_clone(&target_operation->ctx.sha256,
259 &source_operation->ctx.sha256);
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_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100263 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 mbedtls_sha512_clone(&target_operation->ctx.sha512,
265 &source_operation->ctx.sha512);
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_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100269 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 mbedtls_sha512_clone(&target_operation->ctx.sha512,
271 &source_operation->ctx.sha512);
Steven Cooreman0e307642021-02-18 16:18:32 +0100272 break;
273#endif
274 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100275 (void) source_operation;
276 (void) target_operation;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 return PSA_ERROR_NOT_SUPPORTED;
Steven Cooreman0e307642021-02-18 16:18:32 +0100278 }
279
280 target_operation->alg = source_operation->alg;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 return PSA_SUCCESS;
Steven Cooreman0e307642021-02-18 16:18:32 +0100282}
283
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100284psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100285 mbedtls_psa_hash_operation_t *operation,
286 const uint8_t *input,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 size_t input_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100288{
289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 switch (operation->alg) {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100292#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100293 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 ret = mbedtls_md2_update_ret(&operation->ctx.md2,
295 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100296 break;
297#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100298#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100299 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 ret = mbedtls_md4_update_ret(&operation->ctx.md4,
301 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100302 break;
303#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100304#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100305 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 ret = mbedtls_md5_update_ret(&operation->ctx.md5,
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_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100311 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 ret = mbedtls_ripemd160_update_ret(&operation->ctx.ripemd160,
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_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100317 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 ret = mbedtls_sha1_update_ret(&operation->ctx.sha1,
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_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100323 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 ret = mbedtls_sha256_update_ret(&operation->ctx.sha256,
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_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100329 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 ret = mbedtls_sha256_update_ret(&operation->ctx.sha256,
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_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100335 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 ret = mbedtls_sha512_update_ret(&operation->ctx.sha512,
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_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100341 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 ret = mbedtls_sha512_update_ret(&operation->ctx.sha512,
343 input, input_length);
Steven Cooreman0e307642021-02-18 16:18:32 +0100344 break;
345#endif
346 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100347 (void) input;
348 (void) input_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100350 }
351
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100352 return mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100353}
354
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100355psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100356 mbedtls_psa_hash_operation_t *operation,
357 uint8_t *hash,
358 size_t hash_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 size_t *hash_length)
Steven Cooreman0e307642021-02-18 16:18:32 +0100360{
361 psa_status_t status;
362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 size_t actual_hash_length = PSA_HASH_LENGTH(operation->alg);
Steven Cooreman0e307642021-02-18 16:18:32 +0100364
365 /* Fill the output buffer with something that isn't a valid hash
366 * (barring an attack on the hash and deliberately-crafted input),
367 * in case the caller doesn't check the return status properly. */
368 *hash_length = hash_size;
369 /* If hash_size is 0 then hash may be NULL and then the
370 * call to memset would have undefined behavior. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if (hash_size != 0) {
372 memset(hash, '!', hash_size);
373 }
Steven Cooreman0e307642021-02-18 16:18:32 +0100374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 if (hash_size < actual_hash_length) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100376 status = PSA_ERROR_BUFFER_TOO_SMALL;
377 goto exit;
378 }
379
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 switch (operation->alg) {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100381#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100382 case PSA_ALG_MD2:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 ret = mbedtls_md2_finish_ret(&operation->ctx.md2, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100384 break;
385#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100386#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100387 case PSA_ALG_MD4:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 ret = mbedtls_md4_finish_ret(&operation->ctx.md4, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100389 break;
390#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100391#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100392 case PSA_ALG_MD5:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 ret = mbedtls_md5_finish_ret(&operation->ctx.md5, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100394 break;
395#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100396#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100397 case PSA_ALG_RIPEMD160:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 ret = mbedtls_ripemd160_finish_ret(&operation->ctx.ripemd160, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100399 break;
400#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100401#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100402 case PSA_ALG_SHA_1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 ret = mbedtls_sha1_finish_ret(&operation->ctx.sha1, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100404 break;
405#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100406#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100407 case PSA_ALG_SHA_224:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100408 ret = mbedtls_sha256_finish_ret(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100409 break;
410#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100411#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100412 case PSA_ALG_SHA_256:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 ret = mbedtls_sha256_finish_ret(&operation->ctx.sha256, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100414 break;
415#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100416#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100417 case PSA_ALG_SHA_384:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 ret = mbedtls_sha512_finish_ret(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100419 break;
420#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100421#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100422 case PSA_ALG_SHA_512:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 ret = mbedtls_sha512_finish_ret(&operation->ctx.sha512, hash);
Steven Cooreman0e307642021-02-18 16:18:32 +0100424 break;
425#endif
426 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100427 (void) hash;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100428 return PSA_ERROR_BAD_STATE;
Steven Cooreman0e307642021-02-18 16:18:32 +0100429 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 status = mbedtls_to_psa_error(ret);
Steven Cooreman0e307642021-02-18 16:18:32 +0100431
432exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100433 if (status == PSA_SUCCESS) {
Steven Cooreman0e307642021-02-18 16:18:32 +0100434 *hash_length = actual_hash_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 }
436 return status;
Steven Cooreman0e307642021-02-18 16:18:32 +0100437}
438
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100439psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100440 psa_algorithm_t alg,
441 const uint8_t *input,
442 size_t input_length,
443 uint8_t *hash,
444 size_t hash_size,
445 size_t *hash_length)
446{
447 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100449 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100450
451 *hash_length = hash_size;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 status = mbedtls_psa_hash_setup(&operation, alg);
453 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100454 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 }
456 status = mbedtls_psa_hash_update(&operation, input, input_length);
457 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100458 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 }
460 status = mbedtls_psa_hash_finish(&operation, hash, hash_size, hash_length);
461 if (status != PSA_SUCCESS) {
Steven Cooreman83f300e2021-03-08 17:09:48 +0100462 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 }
Steven Cooreman83f300e2021-03-08 17:09:48 +0100464
465exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 abort_status = mbedtls_psa_hash_abort(&operation);
467 if (status == PSA_SUCCESS) {
468 return abort_status;
469 } else {
470 return status;
471 }
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100472
Steven Cooreman83f300e2021-03-08 17:09:48 +0100473}
Steven Cooreman0d586662021-03-08 20:28:18 +0100474#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100475
Steven Cooreman0e307642021-02-18 16:18:32 +0100476#endif /* MBEDTLS_PSA_CRYPTO_C */