blob: 337e557b078a9a69f52e3145beddc5cf315f5118 [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)
36const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
37{
38 switch( alg )
39 {
40#if defined(MBEDTLS_MD2_C)
41 case PSA_ALG_MD2:
42 return( &mbedtls_md2_info );
43#endif
44#if defined(MBEDTLS_MD4_C)
45 case PSA_ALG_MD4:
46 return( &mbedtls_md4_info );
47#endif
48#if defined(MBEDTLS_MD5_C)
49 case PSA_ALG_MD5:
50 return( &mbedtls_md5_info );
51#endif
52#if defined(MBEDTLS_RIPEMD160_C)
53 case PSA_ALG_RIPEMD160:
54 return( &mbedtls_ripemd160_info );
55#endif
56#if defined(MBEDTLS_SHA1_C)
57 case PSA_ALG_SHA_1:
58 return( &mbedtls_sha1_info );
59#endif
60#if defined(MBEDTLS_SHA256_C)
61 case PSA_ALG_SHA_224:
62 return( &mbedtls_sha224_info );
63#endif
64#if defined(MBEDTLS_SHA256_C)
65 case PSA_ALG_SHA_256:
66 return( &mbedtls_sha256_info );
67#endif
68#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_SHA512_NO_SHA384)
69 case PSA_ALG_SHA_384:
70 return( &mbedtls_sha384_info );
71#endif
72#if defined(MBEDTLS_SHA512_C)
73 case PSA_ALG_SHA_512:
74 return( &mbedtls_sha512_info );
75#endif
76 default:
77 return( NULL );
78 }
79}
80#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
81 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
82 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
83 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
84
Ronald Croncfc3c7b2021-03-13 18:50:11 +010085#if defined(MBEDTLS_PSA_BUILTIN_HASH)
86psa_status_t mbedtls_psa_hash_abort(
Steven Cooreman83f300e2021-03-08 17:09:48 +010087 mbedtls_psa_hash_operation_t *operation )
Steven Cooreman0e307642021-02-18 16:18:32 +010088{
Steven Cooreman83f300e2021-03-08 17:09:48 +010089 switch( operation->alg )
90 {
91 case 0:
92 /* The object has (apparently) been initialized but it is not
93 * in use. It's ok to call abort on such an object, and there's
94 * nothing to do. */
95 break;
Ronald Croncfc3c7b2021-03-13 18:50:11 +010096#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman83f300e2021-03-08 17:09:48 +010097 case PSA_ALG_MD2:
98 mbedtls_md2_free( &operation->ctx.md2 );
99 break;
100#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100101#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100102 case PSA_ALG_MD4:
103 mbedtls_md4_free( &operation->ctx.md4 );
104 break;
105#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100106#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100107 case PSA_ALG_MD5:
108 mbedtls_md5_free( &operation->ctx.md5 );
109 break;
110#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100111#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100112 case PSA_ALG_RIPEMD160:
113 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
114 break;
115#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100116#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100117 case PSA_ALG_SHA_1:
118 mbedtls_sha1_free( &operation->ctx.sha1 );
119 break;
120#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100121#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100122 case PSA_ALG_SHA_224:
123 mbedtls_sha256_free( &operation->ctx.sha256 );
124 break;
125#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100126#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100127 case PSA_ALG_SHA_256:
128 mbedtls_sha256_free( &operation->ctx.sha256 );
129 break;
130#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100131#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100132 case PSA_ALG_SHA_384:
133 mbedtls_sha512_free( &operation->ctx.sha512 );
134 break;
135#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100136#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100137 case PSA_ALG_SHA_512:
138 mbedtls_sha512_free( &operation->ctx.sha512 );
139 break;
140#endif
141 default:
142 return( PSA_ERROR_BAD_STATE );
143 }
144 operation->alg = 0;
145 return( PSA_SUCCESS );
Steven Cooreman0e307642021-02-18 16:18:32 +0100146}
147
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100148psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100149 mbedtls_psa_hash_operation_t *operation,
150 psa_algorithm_t alg )
151{
152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
153
154 /* A context must be freshly initialized before it can be set up. */
155 if( operation->alg != 0 )
156 {
157 return( PSA_ERROR_BAD_STATE );
158 }
159
160 switch( alg )
161 {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100162#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100163 case PSA_ALG_MD2:
164 mbedtls_md2_init( &operation->ctx.md2 );
165 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
166 break;
167#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100168#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100169 case PSA_ALG_MD4:
170 mbedtls_md4_init( &operation->ctx.md4 );
171 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
172 break;
173#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100174#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100175 case PSA_ALG_MD5:
176 mbedtls_md5_init( &operation->ctx.md5 );
177 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
178 break;
179#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100180#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100181 case PSA_ALG_RIPEMD160:
182 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
183 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
184 break;
185#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100186#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100187 case PSA_ALG_SHA_1:
188 mbedtls_sha1_init( &operation->ctx.sha1 );
189 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
190 break;
191#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100192#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100193 case PSA_ALG_SHA_224:
194 mbedtls_sha256_init( &operation->ctx.sha256 );
195 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
196 break;
197#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100198#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100199 case PSA_ALG_SHA_256:
200 mbedtls_sha256_init( &operation->ctx.sha256 );
201 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
202 break;
203#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100204#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100205 case PSA_ALG_SHA_384:
206 mbedtls_sha512_init( &operation->ctx.sha512 );
207 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
208 break;
209#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100210#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100211 case PSA_ALG_SHA_512:
212 mbedtls_sha512_init( &operation->ctx.sha512 );
213 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
214 break;
215#endif
216 default:
217 return( PSA_ALG_IS_HASH( alg ) ?
218 PSA_ERROR_NOT_SUPPORTED :
219 PSA_ERROR_INVALID_ARGUMENT );
220 }
221 if( ret == 0 )
222 operation->alg = alg;
223 else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100224 mbedtls_psa_hash_abort( operation );
Steven Cooreman0e307642021-02-18 16:18:32 +0100225 return( mbedtls_to_psa_error( ret ) );
226}
227
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100228psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100229 const mbedtls_psa_hash_operation_t *source_operation,
230 mbedtls_psa_hash_operation_t *target_operation )
231{
232 switch( source_operation->alg )
233 {
234 case 0:
235 return( PSA_ERROR_BAD_STATE );
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100236#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100237 case PSA_ALG_MD2:
238 mbedtls_md2_clone( &target_operation->ctx.md2,
239 &source_operation->ctx.md2 );
240 break;
241#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100242#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100243 case PSA_ALG_MD4:
244 mbedtls_md4_clone( &target_operation->ctx.md4,
245 &source_operation->ctx.md4 );
246 break;
247#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100248#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100249 case PSA_ALG_MD5:
250 mbedtls_md5_clone( &target_operation->ctx.md5,
251 &source_operation->ctx.md5 );
252 break;
253#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100254#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100255 case PSA_ALG_RIPEMD160:
256 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
257 &source_operation->ctx.ripemd160 );
258 break;
259#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100260#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100261 case PSA_ALG_SHA_1:
262 mbedtls_sha1_clone( &target_operation->ctx.sha1,
263 &source_operation->ctx.sha1 );
264 break;
265#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100266#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100267 case PSA_ALG_SHA_224:
268 mbedtls_sha256_clone( &target_operation->ctx.sha256,
269 &source_operation->ctx.sha256 );
270 break;
271#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100272#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100273 case PSA_ALG_SHA_256:
274 mbedtls_sha256_clone( &target_operation->ctx.sha256,
275 &source_operation->ctx.sha256 );
276 break;
277#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100278#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100279 case PSA_ALG_SHA_384:
280 mbedtls_sha512_clone( &target_operation->ctx.sha512,
281 &source_operation->ctx.sha512 );
282 break;
283#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100284#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100285 case PSA_ALG_SHA_512:
286 mbedtls_sha512_clone( &target_operation->ctx.sha512,
287 &source_operation->ctx.sha512 );
288 break;
289#endif
290 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100291 (void) source_operation;
292 (void) target_operation;
Steven Cooreman0e307642021-02-18 16:18:32 +0100293 return( PSA_ERROR_NOT_SUPPORTED );
294 }
295
296 target_operation->alg = source_operation->alg;
297 return( PSA_SUCCESS );
298}
299
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100300psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100301 mbedtls_psa_hash_operation_t *operation,
302 const uint8_t *input,
303 size_t input_length )
304{
305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
Steven Cooreman0e307642021-02-18 16:18:32 +0100307 switch( operation->alg )
308 {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100309#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100310 case PSA_ALG_MD2:
311 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
312 input, input_length );
313 break;
314#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100315#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100316 case PSA_ALG_MD4:
317 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
318 input, input_length );
319 break;
320#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100321#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100322 case PSA_ALG_MD5:
323 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
324 input, input_length );
325 break;
326#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100327#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100328 case PSA_ALG_RIPEMD160:
329 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
330 input, input_length );
331 break;
332#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100333#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100334 case PSA_ALG_SHA_1:
335 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
336 input, input_length );
337 break;
338#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100339#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100340 case PSA_ALG_SHA_224:
341 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
342 input, input_length );
343 break;
344#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100345#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100346 case PSA_ALG_SHA_256:
347 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
348 input, input_length );
349 break;
350#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100351#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100352 case PSA_ALG_SHA_384:
353 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
354 input, input_length );
355 break;
356#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100357#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100358 case PSA_ALG_SHA_512:
359 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
360 input, input_length );
361 break;
362#endif
363 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100364 (void) input;
365 (void) input_length;
Steven Cooreman0e307642021-02-18 16:18:32 +0100366 return( PSA_ERROR_BAD_STATE );
367 }
368
Steven Cooreman0e307642021-02-18 16:18:32 +0100369 return( mbedtls_to_psa_error( ret ) );
370}
371
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100372psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100373 mbedtls_psa_hash_operation_t *operation,
374 uint8_t *hash,
375 size_t hash_size,
376 size_t *hash_length )
377{
378 psa_status_t status;
379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
380 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
381
382 /* Fill the output buffer with something that isn't a valid hash
383 * (barring an attack on the hash and deliberately-crafted input),
384 * in case the caller doesn't check the return status properly. */
385 *hash_length = hash_size;
386 /* If hash_size is 0 then hash may be NULL and then the
387 * call to memset would have undefined behavior. */
388 if( hash_size != 0 )
389 memset( hash, '!', hash_size );
390
391 if( hash_size < actual_hash_length )
392 {
393 status = PSA_ERROR_BUFFER_TOO_SMALL;
394 goto exit;
395 }
396
397 switch( operation->alg )
398 {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100399#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100400 case PSA_ALG_MD2:
401 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
402 break;
403#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100404#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100405 case PSA_ALG_MD4:
406 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
407 break;
408#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100409#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100410 case PSA_ALG_MD5:
411 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
412 break;
413#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100414#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100415 case PSA_ALG_RIPEMD160:
416 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
417 break;
418#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100419#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100420 case PSA_ALG_SHA_1:
421 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
422 break;
423#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100424#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100425 case PSA_ALG_SHA_224:
426 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
427 break;
428#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100429#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100430 case PSA_ALG_SHA_256:
431 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
432 break;
433#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100434#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100435 case PSA_ALG_SHA_384:
436 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
437 break;
438#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100439#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100440 case PSA_ALG_SHA_512:
441 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
442 break;
443#endif
444 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100445 (void) hash;
Steven Cooreman0e307642021-02-18 16:18:32 +0100446 return( PSA_ERROR_BAD_STATE );
447 }
448 status = mbedtls_to_psa_error( ret );
449
450exit:
451 if( status == PSA_SUCCESS )
Steven Cooreman0e307642021-02-18 16:18:32 +0100452 *hash_length = actual_hash_length;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100453 return( status );
Steven Cooreman0e307642021-02-18 16:18:32 +0100454}
455
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100456psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100457 psa_algorithm_t alg,
458 const uint8_t *input,
459 size_t input_length,
460 uint8_t *hash,
461 size_t hash_size,
462 size_t *hash_length)
463{
464 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
465 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100466 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100467
468 *hash_length = hash_size;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100469 status = mbedtls_psa_hash_setup( &operation, alg );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100470 if( status != PSA_SUCCESS )
471 goto exit;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100472 status = mbedtls_psa_hash_update( &operation, input, input_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100473 if( status != PSA_SUCCESS )
474 goto exit;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100475 status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100476 if( status != PSA_SUCCESS )
477 goto exit;
478
479exit:
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100480 abort_status = mbedtls_psa_hash_abort( &operation );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100481 if( status == PSA_SUCCESS )
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100482 return( abort_status );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100483 else
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100484 return( status );
485
Steven Cooreman83f300e2021-03-08 17:09:48 +0100486}
Steven Cooreman0d586662021-03-08 20:28:18 +0100487#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100488
Steven Cooreman0e307642021-02-18 16:18:32 +0100489#endif /* MBEDTLS_PSA_CRYPTO_C */