blob: 9c50ca8db39eed199dcf57a6a95e282b3d942524 [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
Steven Cooreman83f300e2021-03-08 17:09:48 +010085/* Implement the PSA driver hash interface on top of mbed TLS if either the
86 * software driver or the test driver requires it. */
Ronald Croncfc3c7b2021-03-13 18:50:11 +010087#if defined(MBEDTLS_PSA_BUILTIN_HASH)
88psa_status_t mbedtls_psa_hash_abort(
Steven Cooreman83f300e2021-03-08 17:09:48 +010089 mbedtls_psa_hash_operation_t *operation )
Steven Cooreman0e307642021-02-18 16:18:32 +010090{
Steven Cooreman83f300e2021-03-08 17:09:48 +010091 switch( operation->alg )
92 {
93 case 0:
94 /* The object has (apparently) been initialized but it is not
95 * in use. It's ok to call abort on such an object, and there's
96 * nothing to do. */
97 break;
Ronald Croncfc3c7b2021-03-13 18:50:11 +010098#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman83f300e2021-03-08 17:09:48 +010099 case PSA_ALG_MD2:
100 mbedtls_md2_free( &operation->ctx.md2 );
101 break;
102#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100103#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100104 case PSA_ALG_MD4:
105 mbedtls_md4_free( &operation->ctx.md4 );
106 break;
107#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100108#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100109 case PSA_ALG_MD5:
110 mbedtls_md5_free( &operation->ctx.md5 );
111 break;
112#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100113#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100114 case PSA_ALG_RIPEMD160:
115 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
116 break;
117#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100118#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100119 case PSA_ALG_SHA_1:
120 mbedtls_sha1_free( &operation->ctx.sha1 );
121 break;
122#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100123#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100124 case PSA_ALG_SHA_224:
125 mbedtls_sha256_free( &operation->ctx.sha256 );
126 break;
127#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100128#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100129 case PSA_ALG_SHA_256:
130 mbedtls_sha256_free( &operation->ctx.sha256 );
131 break;
132#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100133#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100134 case PSA_ALG_SHA_384:
135 mbedtls_sha512_free( &operation->ctx.sha512 );
136 break;
137#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100138#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100139 case PSA_ALG_SHA_512:
140 mbedtls_sha512_free( &operation->ctx.sha512 );
141 break;
142#endif
143 default:
144 return( PSA_ERROR_BAD_STATE );
145 }
146 operation->alg = 0;
147 return( PSA_SUCCESS );
Steven Cooreman0e307642021-02-18 16:18:32 +0100148}
149
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100150psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100151 mbedtls_psa_hash_operation_t *operation,
152 psa_algorithm_t alg )
153{
154 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
155
156 /* A context must be freshly initialized before it can be set up. */
157 if( operation->alg != 0 )
158 {
159 return( PSA_ERROR_BAD_STATE );
160 }
161
162 switch( alg )
163 {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100164#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100165 case PSA_ALG_MD2:
166 mbedtls_md2_init( &operation->ctx.md2 );
167 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
168 break;
169#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100170#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100171 case PSA_ALG_MD4:
172 mbedtls_md4_init( &operation->ctx.md4 );
173 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
174 break;
175#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100176#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100177 case PSA_ALG_MD5:
178 mbedtls_md5_init( &operation->ctx.md5 );
179 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
180 break;
181#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100182#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100183 case PSA_ALG_RIPEMD160:
184 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
185 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
186 break;
187#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100188#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100189 case PSA_ALG_SHA_1:
190 mbedtls_sha1_init( &operation->ctx.sha1 );
191 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
192 break;
193#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100194#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100195 case PSA_ALG_SHA_224:
196 mbedtls_sha256_init( &operation->ctx.sha256 );
197 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
198 break;
199#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100200#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100201 case PSA_ALG_SHA_256:
202 mbedtls_sha256_init( &operation->ctx.sha256 );
203 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
204 break;
205#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100206#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100207 case PSA_ALG_SHA_384:
208 mbedtls_sha512_init( &operation->ctx.sha512 );
209 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
210 break;
211#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100212#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100213 case PSA_ALG_SHA_512:
214 mbedtls_sha512_init( &operation->ctx.sha512 );
215 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
216 break;
217#endif
218 default:
219 return( PSA_ALG_IS_HASH( alg ) ?
220 PSA_ERROR_NOT_SUPPORTED :
221 PSA_ERROR_INVALID_ARGUMENT );
222 }
223 if( ret == 0 )
224 operation->alg = alg;
225 else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100226 mbedtls_psa_hash_abort( operation );
Steven Cooreman0e307642021-02-18 16:18:32 +0100227 return( mbedtls_to_psa_error( ret ) );
228}
229
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100230psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100231 const mbedtls_psa_hash_operation_t *source_operation,
232 mbedtls_psa_hash_operation_t *target_operation )
233{
234 switch( source_operation->alg )
235 {
236 case 0:
237 return( PSA_ERROR_BAD_STATE );
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100238#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100239 case PSA_ALG_MD2:
240 mbedtls_md2_clone( &target_operation->ctx.md2,
241 &source_operation->ctx.md2 );
242 break;
243#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100244#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100245 case PSA_ALG_MD4:
246 mbedtls_md4_clone( &target_operation->ctx.md4,
247 &source_operation->ctx.md4 );
248 break;
249#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100250#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100251 case PSA_ALG_MD5:
252 mbedtls_md5_clone( &target_operation->ctx.md5,
253 &source_operation->ctx.md5 );
254 break;
255#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100256#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100257 case PSA_ALG_RIPEMD160:
258 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
259 &source_operation->ctx.ripemd160 );
260 break;
261#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100262#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100263 case PSA_ALG_SHA_1:
264 mbedtls_sha1_clone( &target_operation->ctx.sha1,
265 &source_operation->ctx.sha1 );
266 break;
267#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100268#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100269 case PSA_ALG_SHA_224:
270 mbedtls_sha256_clone( &target_operation->ctx.sha256,
271 &source_operation->ctx.sha256 );
272 break;
273#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100274#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100275 case PSA_ALG_SHA_256:
276 mbedtls_sha256_clone( &target_operation->ctx.sha256,
277 &source_operation->ctx.sha256 );
278 break;
279#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100280#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100281 case PSA_ALG_SHA_384:
282 mbedtls_sha512_clone( &target_operation->ctx.sha512,
283 &source_operation->ctx.sha512 );
284 break;
285#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100286#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100287 case PSA_ALG_SHA_512:
288 mbedtls_sha512_clone( &target_operation->ctx.sha512,
289 &source_operation->ctx.sha512 );
290 break;
291#endif
292 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100293 (void) source_operation;
294 (void) target_operation;
Steven Cooreman0e307642021-02-18 16:18:32 +0100295 return( PSA_ERROR_NOT_SUPPORTED );
296 }
297
298 target_operation->alg = source_operation->alg;
299 return( PSA_SUCCESS );
300}
301
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100302psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100303 mbedtls_psa_hash_operation_t *operation,
304 const uint8_t *input,
305 size_t input_length )
306{
307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
308
Steven Cooreman0e307642021-02-18 16:18:32 +0100309 switch( operation->alg )
310 {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100311#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100312 case PSA_ALG_MD2:
313 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
314 input, input_length );
315 break;
316#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100317#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100318 case PSA_ALG_MD4:
319 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
320 input, input_length );
321 break;
322#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100323#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100324 case PSA_ALG_MD5:
325 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
326 input, input_length );
327 break;
328#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100329#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100330 case PSA_ALG_RIPEMD160:
331 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
332 input, input_length );
333 break;
334#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100335#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100336 case PSA_ALG_SHA_1:
337 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
338 input, input_length );
339 break;
340#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100341#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100342 case PSA_ALG_SHA_224:
343 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
344 input, input_length );
345 break;
346#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100347#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100348 case PSA_ALG_SHA_256:
349 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
350 input, input_length );
351 break;
352#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100353#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100354 case PSA_ALG_SHA_384:
355 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
356 input, input_length );
357 break;
358#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100359#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100360 case PSA_ALG_SHA_512:
361 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
362 input, input_length );
363 break;
364#endif
365 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100366 (void) input;
367 (void) input_length;
Steven Cooreman0e307642021-02-18 16:18:32 +0100368 return( PSA_ERROR_BAD_STATE );
369 }
370
Steven Cooreman0e307642021-02-18 16:18:32 +0100371 return( mbedtls_to_psa_error( ret ) );
372}
373
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100374psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100375 mbedtls_psa_hash_operation_t *operation,
376 uint8_t *hash,
377 size_t hash_size,
378 size_t *hash_length )
379{
380 psa_status_t status;
381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
382 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
383
384 /* Fill the output buffer with something that isn't a valid hash
385 * (barring an attack on the hash and deliberately-crafted input),
386 * in case the caller doesn't check the return status properly. */
387 *hash_length = hash_size;
388 /* If hash_size is 0 then hash may be NULL and then the
389 * call to memset would have undefined behavior. */
390 if( hash_size != 0 )
391 memset( hash, '!', hash_size );
392
393 if( hash_size < actual_hash_length )
394 {
395 status = PSA_ERROR_BUFFER_TOO_SMALL;
396 goto exit;
397 }
398
399 switch( operation->alg )
400 {
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100401#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Steven Cooreman0e307642021-02-18 16:18:32 +0100402 case PSA_ALG_MD2:
403 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
404 break;
405#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100406#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Steven Cooreman0e307642021-02-18 16:18:32 +0100407 case PSA_ALG_MD4:
408 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
409 break;
410#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100411#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100412 case PSA_ALG_MD5:
413 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
414 break;
415#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100416#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100417 case PSA_ALG_RIPEMD160:
418 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
419 break;
420#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100421#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100422 case PSA_ALG_SHA_1:
423 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
424 break;
425#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100426#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100427 case PSA_ALG_SHA_224:
428 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
429 break;
430#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100431#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100432 case PSA_ALG_SHA_256:
433 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
434 break;
435#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100436#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100437 case PSA_ALG_SHA_384:
438 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
439 break;
440#endif
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100441#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100442 case PSA_ALG_SHA_512:
443 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
444 break;
445#endif
446 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100447 (void) hash;
Steven Cooreman0e307642021-02-18 16:18:32 +0100448 return( PSA_ERROR_BAD_STATE );
449 }
450 status = mbedtls_to_psa_error( ret );
451
452exit:
453 if( status == PSA_SUCCESS )
Steven Cooreman0e307642021-02-18 16:18:32 +0100454 *hash_length = actual_hash_length;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100455 return( status );
Steven Cooreman0e307642021-02-18 16:18:32 +0100456}
457
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100458psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100459 psa_algorithm_t alg,
460 const uint8_t *input,
461 size_t input_length,
462 uint8_t *hash,
463 size_t hash_size,
464 size_t *hash_length)
465{
466 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100468 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100469
470 *hash_length = hash_size;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100471 status = mbedtls_psa_hash_setup( &operation, alg );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100472 if( status != PSA_SUCCESS )
473 goto exit;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100474 status = mbedtls_psa_hash_update( &operation, input, input_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100475 if( status != PSA_SUCCESS )
476 goto exit;
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100477 status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100478 if( status != PSA_SUCCESS )
479 goto exit;
480
481exit:
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100482 abort_status = mbedtls_psa_hash_abort( &operation );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100483 if( status == PSA_SUCCESS )
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100484 return( abort_status );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100485 else
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100486 return( status );
487
Steven Cooreman83f300e2021-03-08 17:09:48 +0100488}
Steven Cooreman0d586662021-03-08 20:28:18 +0100489#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100490
Steven Cooreman0e307642021-02-18 16:18:32 +0100491#endif /* MBEDTLS_PSA_CRYPTO_C */