blob: 536c6c1188ddeb41b459b641ba84a7afaa27c54d [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 {
Steven Cooreman5f88e772021-03-15 11:07:12 +010040#if defined(MBEDTLS_MD5_C)
41 case PSA_ALG_MD5:
42 return( &mbedtls_md5_info );
43#endif
44#if defined(MBEDTLS_RIPEMD160_C)
45 case PSA_ALG_RIPEMD160:
46 return( &mbedtls_ripemd160_info );
47#endif
48#if defined(MBEDTLS_SHA1_C)
49 case PSA_ALG_SHA_1:
50 return( &mbedtls_sha1_info );
51#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020052#if defined(MBEDTLS_SHA224_C)
Steven Cooreman5f88e772021-03-15 11:07:12 +010053 case PSA_ALG_SHA_224:
54 return( &mbedtls_sha224_info );
55#endif
56#if defined(MBEDTLS_SHA256_C)
57 case PSA_ALG_SHA_256:
58 return( &mbedtls_sha256_info );
59#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +020060#if defined(MBEDTLS_SHA384_C)
Steven Cooreman5f88e772021-03-15 11:07:12 +010061 case PSA_ALG_SHA_384:
62 return( &mbedtls_sha384_info );
63#endif
64#if defined(MBEDTLS_SHA512_C)
65 case PSA_ALG_SHA_512:
66 return( &mbedtls_sha512_info );
67#endif
68 default:
69 return( NULL );
70 }
71}
72#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
73 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
74 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
75 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
76
Ronald Cron0266cfe2021-03-13 18:50:11 +010077#if defined(MBEDTLS_PSA_BUILTIN_HASH)
78psa_status_t mbedtls_psa_hash_abort(
Steven Cooreman83f300e2021-03-08 17:09:48 +010079 mbedtls_psa_hash_operation_t *operation )
Steven Cooreman0e307642021-02-18 16:18:32 +010080{
Steven Cooreman83f300e2021-03-08 17:09:48 +010081 switch( operation->alg )
82 {
83 case 0:
84 /* The object has (apparently) been initialized but it is not
85 * in use. It's ok to call abort on such an object, and there's
86 * nothing to do. */
87 break;
Ronald Cron0266cfe2021-03-13 18:50:11 +010088#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +010089 case PSA_ALG_MD5:
90 mbedtls_md5_free( &operation->ctx.md5 );
91 break;
92#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010093#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +010094 case PSA_ALG_RIPEMD160:
95 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
96 break;
97#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010098#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +010099 case PSA_ALG_SHA_1:
100 mbedtls_sha1_free( &operation->ctx.sha1 );
101 break;
102#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100103#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100104 case PSA_ALG_SHA_224:
105 mbedtls_sha256_free( &operation->ctx.sha256 );
106 break;
107#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100108#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100109 case PSA_ALG_SHA_256:
110 mbedtls_sha256_free( &operation->ctx.sha256 );
111 break;
112#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100113#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100114 case PSA_ALG_SHA_384:
115 mbedtls_sha512_free( &operation->ctx.sha512 );
116 break;
117#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100118#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100119 case PSA_ALG_SHA_512:
120 mbedtls_sha512_free( &operation->ctx.sha512 );
121 break;
122#endif
123 default:
124 return( PSA_ERROR_BAD_STATE );
125 }
126 operation->alg = 0;
127 return( PSA_SUCCESS );
Steven Cooreman0e307642021-02-18 16:18:32 +0100128}
129
Ronald Cron0266cfe2021-03-13 18:50:11 +0100130psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100131 mbedtls_psa_hash_operation_t *operation,
132 psa_algorithm_t alg )
133{
134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
135
136 /* A context must be freshly initialized before it can be set up. */
137 if( operation->alg != 0 )
138 {
139 return( PSA_ERROR_BAD_STATE );
140 }
141
142 switch( alg )
143 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100144#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100145 case PSA_ALG_MD5:
146 mbedtls_md5_init( &operation->ctx.md5 );
TRodziewicz26371e42021-06-08 16:45:41 +0200147 ret = mbedtls_md5_starts( &operation->ctx.md5 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100148 break;
149#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100150#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100151 case PSA_ALG_RIPEMD160:
152 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
TRodziewicz26371e42021-06-08 16:45:41 +0200153 ret = mbedtls_ripemd160_starts( &operation->ctx.ripemd160 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100154 break;
155#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100156#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100157 case PSA_ALG_SHA_1:
158 mbedtls_sha1_init( &operation->ctx.sha1 );
TRodziewicz26371e42021-06-08 16:45:41 +0200159 ret = mbedtls_sha1_starts( &operation->ctx.sha1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100160 break;
161#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100162#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100163 case PSA_ALG_SHA_224:
164 mbedtls_sha256_init( &operation->ctx.sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200165 ret = mbedtls_sha256_starts( &operation->ctx.sha256, 1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100166 break;
167#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100168#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100169 case PSA_ALG_SHA_256:
170 mbedtls_sha256_init( &operation->ctx.sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200171 ret = mbedtls_sha256_starts( &operation->ctx.sha256, 0 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100172 break;
173#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100174#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100175 case PSA_ALG_SHA_384:
176 mbedtls_sha512_init( &operation->ctx.sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200177 ret = mbedtls_sha512_starts( &operation->ctx.sha512, 1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100178 break;
179#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100180#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100181 case PSA_ALG_SHA_512:
182 mbedtls_sha512_init( &operation->ctx.sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200183 ret = mbedtls_sha512_starts( &operation->ctx.sha512, 0 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100184 break;
185#endif
186 default:
187 return( PSA_ALG_IS_HASH( alg ) ?
188 PSA_ERROR_NOT_SUPPORTED :
189 PSA_ERROR_INVALID_ARGUMENT );
190 }
191 if( ret == 0 )
192 operation->alg = alg;
193 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100194 mbedtls_psa_hash_abort( operation );
Steven Cooreman0e307642021-02-18 16:18:32 +0100195 return( mbedtls_to_psa_error( ret ) );
196}
197
Ronald Cron0266cfe2021-03-13 18:50:11 +0100198psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100199 const mbedtls_psa_hash_operation_t *source_operation,
200 mbedtls_psa_hash_operation_t *target_operation )
201{
202 switch( source_operation->alg )
203 {
204 case 0:
205 return( PSA_ERROR_BAD_STATE );
Ronald Cron0266cfe2021-03-13 18:50:11 +0100206#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100207 case PSA_ALG_MD5:
208 mbedtls_md5_clone( &target_operation->ctx.md5,
209 &source_operation->ctx.md5 );
210 break;
211#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100212#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100213 case PSA_ALG_RIPEMD160:
214 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
215 &source_operation->ctx.ripemd160 );
216 break;
217#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100218#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100219 case PSA_ALG_SHA_1:
220 mbedtls_sha1_clone( &target_operation->ctx.sha1,
221 &source_operation->ctx.sha1 );
222 break;
223#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100224#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100225 case PSA_ALG_SHA_224:
226 mbedtls_sha256_clone( &target_operation->ctx.sha256,
227 &source_operation->ctx.sha256 );
228 break;
229#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100230#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100231 case PSA_ALG_SHA_256:
232 mbedtls_sha256_clone( &target_operation->ctx.sha256,
233 &source_operation->ctx.sha256 );
234 break;
235#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100236#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100237 case PSA_ALG_SHA_384:
238 mbedtls_sha512_clone( &target_operation->ctx.sha512,
239 &source_operation->ctx.sha512 );
240 break;
241#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100242#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100243 case PSA_ALG_SHA_512:
244 mbedtls_sha512_clone( &target_operation->ctx.sha512,
245 &source_operation->ctx.sha512 );
246 break;
247#endif
248 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100249 (void) source_operation;
250 (void) target_operation;
Steven Cooreman0e307642021-02-18 16:18:32 +0100251 return( PSA_ERROR_NOT_SUPPORTED );
252 }
253
254 target_operation->alg = source_operation->alg;
255 return( PSA_SUCCESS );
256}
257
Ronald Cron0266cfe2021-03-13 18:50:11 +0100258psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100259 mbedtls_psa_hash_operation_t *operation,
260 const uint8_t *input,
261 size_t input_length )
262{
263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
264
Steven Cooreman0e307642021-02-18 16:18:32 +0100265 switch( operation->alg )
266 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100267#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100268 case PSA_ALG_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200269 ret = mbedtls_md5_update( &operation->ctx.md5,
Steven Cooreman0e307642021-02-18 16:18:32 +0100270 input, input_length );
271 break;
272#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100273#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100274 case PSA_ALG_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200275 ret = mbedtls_ripemd160_update( &operation->ctx.ripemd160,
Steven Cooreman0e307642021-02-18 16:18:32 +0100276 input, input_length );
277 break;
278#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100279#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100280 case PSA_ALG_SHA_1:
TRodziewicz26371e42021-06-08 16:45:41 +0200281 ret = mbedtls_sha1_update( &operation->ctx.sha1,
Steven Cooreman0e307642021-02-18 16:18:32 +0100282 input, input_length );
283 break;
284#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100285#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100286 case PSA_ALG_SHA_224:
TRodziewicz26371e42021-06-08 16:45:41 +0200287 ret = mbedtls_sha256_update( &operation->ctx.sha256,
Steven Cooreman0e307642021-02-18 16:18:32 +0100288 input, input_length );
289 break;
290#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100291#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100292 case PSA_ALG_SHA_256:
TRodziewicz26371e42021-06-08 16:45:41 +0200293 ret = mbedtls_sha256_update( &operation->ctx.sha256,
Steven Cooreman0e307642021-02-18 16:18:32 +0100294 input, input_length );
295 break;
296#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100297#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100298 case PSA_ALG_SHA_384:
TRodziewicz26371e42021-06-08 16:45:41 +0200299 ret = mbedtls_sha512_update( &operation->ctx.sha512,
Steven Cooreman0e307642021-02-18 16:18:32 +0100300 input, input_length );
301 break;
302#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100303#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100304 case PSA_ALG_SHA_512:
TRodziewicz26371e42021-06-08 16:45:41 +0200305 ret = mbedtls_sha512_update( &operation->ctx.sha512,
Steven Cooreman0e307642021-02-18 16:18:32 +0100306 input, input_length );
307 break;
308#endif
309 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100310 (void) input;
311 (void) input_length;
Steven Cooreman0e307642021-02-18 16:18:32 +0100312 return( PSA_ERROR_BAD_STATE );
313 }
314
Steven Cooreman0e307642021-02-18 16:18:32 +0100315 return( mbedtls_to_psa_error( ret ) );
316}
317
Ronald Cron0266cfe2021-03-13 18:50:11 +0100318psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100319 mbedtls_psa_hash_operation_t *operation,
320 uint8_t *hash,
321 size_t hash_size,
322 size_t *hash_length )
323{
324 psa_status_t status;
325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
326 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
327
328 /* Fill the output buffer with something that isn't a valid hash
329 * (barring an attack on the hash and deliberately-crafted input),
330 * in case the caller doesn't check the return status properly. */
331 *hash_length = hash_size;
332 /* If hash_size is 0 then hash may be NULL and then the
333 * call to memset would have undefined behavior. */
334 if( hash_size != 0 )
335 memset( hash, '!', hash_size );
336
337 if( hash_size < actual_hash_length )
338 {
339 status = PSA_ERROR_BUFFER_TOO_SMALL;
340 goto exit;
341 }
342
343 switch( operation->alg )
344 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100345#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100346 case PSA_ALG_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200347 ret = mbedtls_md5_finish( &operation->ctx.md5, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100348 break;
349#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100350#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100351 case PSA_ALG_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200352 ret = mbedtls_ripemd160_finish( &operation->ctx.ripemd160, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100353 break;
354#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100355#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100356 case PSA_ALG_SHA_1:
TRodziewicz26371e42021-06-08 16:45:41 +0200357 ret = mbedtls_sha1_finish( &operation->ctx.sha1, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100358 break;
359#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100360#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100361 case PSA_ALG_SHA_224:
TRodziewicz26371e42021-06-08 16:45:41 +0200362 ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100363 break;
364#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100365#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100366 case PSA_ALG_SHA_256:
TRodziewicz26371e42021-06-08 16:45:41 +0200367 ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100368 break;
369#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100370#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100371 case PSA_ALG_SHA_384:
TRodziewicz26371e42021-06-08 16:45:41 +0200372 ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100373 break;
374#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100375#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100376 case PSA_ALG_SHA_512:
TRodziewicz26371e42021-06-08 16:45:41 +0200377 ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100378 break;
379#endif
380 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100381 (void) hash;
Steven Cooreman0e307642021-02-18 16:18:32 +0100382 return( PSA_ERROR_BAD_STATE );
383 }
384 status = mbedtls_to_psa_error( ret );
385
386exit:
387 if( status == PSA_SUCCESS )
Steven Cooreman0e307642021-02-18 16:18:32 +0100388 *hash_length = actual_hash_length;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100389 return( status );
Steven Cooreman0e307642021-02-18 16:18:32 +0100390}
391
Ronald Cron0266cfe2021-03-13 18:50:11 +0100392psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100393 psa_algorithm_t alg,
394 const uint8_t *input,
395 size_t input_length,
396 uint8_t *hash,
397 size_t hash_size,
398 size_t *hash_length)
399{
400 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
401 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100402 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100403
404 *hash_length = hash_size;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100405 status = mbedtls_psa_hash_setup( &operation, alg );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100406 if( status != PSA_SUCCESS )
407 goto exit;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100408 status = mbedtls_psa_hash_update( &operation, input, input_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100409 if( status != PSA_SUCCESS )
410 goto exit;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100411 status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100412 if( status != PSA_SUCCESS )
413 goto exit;
414
415exit:
Ronald Cron0266cfe2021-03-13 18:50:11 +0100416 abort_status = mbedtls_psa_hash_abort( &operation );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100417 if( status == PSA_SUCCESS )
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100418 return( abort_status );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100419 else
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100420 return( status );
421
Steven Cooreman83f300e2021-03-08 17:09:48 +0100422}
Steven Cooreman0d586662021-03-08 20:28:18 +0100423#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100424
Steven Cooreman0e307642021-02-18 16:18:32 +0100425#endif /* MBEDTLS_PSA_CRYPTO_C */