blob: 28e0b87abeaf51aa8f72c43961793b72bdd20a19 [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
Manuel Pégourié-Gonnard3f477892022-07-05 11:30:31 +020032#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Steven Cooreman5f88e772021-03-15 11:07:12 +010033 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
34 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
35const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
36{
37 switch( alg )
38 {
Steven Cooreman5f88e772021-03-15 11:07:12 +010039#if defined(MBEDTLS_MD5_C)
40 case PSA_ALG_MD5:
41 return( &mbedtls_md5_info );
42#endif
43#if defined(MBEDTLS_RIPEMD160_C)
44 case PSA_ALG_RIPEMD160:
45 return( &mbedtls_ripemd160_info );
46#endif
47#if defined(MBEDTLS_SHA1_C)
48 case PSA_ALG_SHA_1:
49 return( &mbedtls_sha1_info );
50#endif
Mateusz Starzyke3c48b42021-04-19 16:46:28 +020051#if defined(MBEDTLS_SHA224_C)
Steven Cooreman5f88e772021-03-15 11:07:12 +010052 case PSA_ALG_SHA_224:
53 return( &mbedtls_sha224_info );
54#endif
55#if defined(MBEDTLS_SHA256_C)
56 case PSA_ALG_SHA_256:
57 return( &mbedtls_sha256_info );
58#endif
Mateusz Starzyk3352a532021-04-06 14:28:22 +020059#if defined(MBEDTLS_SHA384_C)
Steven Cooreman5f88e772021-03-15 11:07:12 +010060 case PSA_ALG_SHA_384:
61 return( &mbedtls_sha384_info );
62#endif
63#if defined(MBEDTLS_SHA512_C)
64 case PSA_ALG_SHA_512:
65 return( &mbedtls_sha512_info );
66#endif
67 default:
68 return( NULL );
69 }
70}
Manuel Pégourié-Gonnard3f477892022-07-05 11:30:31 +020071#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Steven Cooreman5f88e772021-03-15 11:07:12 +010072 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
73 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
74
Ronald Cron0266cfe2021-03-13 18:50:11 +010075#if defined(MBEDTLS_PSA_BUILTIN_HASH)
76psa_status_t mbedtls_psa_hash_abort(
Steven Cooreman83f300e2021-03-08 17:09:48 +010077 mbedtls_psa_hash_operation_t *operation )
Steven Cooreman0e307642021-02-18 16:18:32 +010078{
Steven Cooreman83f300e2021-03-08 17:09:48 +010079 switch( operation->alg )
80 {
81 case 0:
82 /* The object has (apparently) been initialized but it is not
83 * in use. It's ok to call abort on such an object, and there's
84 * nothing to do. */
85 break;
Ronald Cron0266cfe2021-03-13 18:50:11 +010086#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +010087 case PSA_ALG_MD5:
88 mbedtls_md5_free( &operation->ctx.md5 );
89 break;
90#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010091#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +010092 case PSA_ALG_RIPEMD160:
93 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
94 break;
95#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010096#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +010097 case PSA_ALG_SHA_1:
98 mbedtls_sha1_free( &operation->ctx.sha1 );
99 break;
100#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100101#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100102 case PSA_ALG_SHA_224:
103 mbedtls_sha256_free( &operation->ctx.sha256 );
104 break;
105#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100106#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100107 case PSA_ALG_SHA_256:
108 mbedtls_sha256_free( &operation->ctx.sha256 );
109 break;
110#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100111#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100112 case PSA_ALG_SHA_384:
113 mbedtls_sha512_free( &operation->ctx.sha512 );
114 break;
115#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100116#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100117 case PSA_ALG_SHA_512:
118 mbedtls_sha512_free( &operation->ctx.sha512 );
119 break;
120#endif
121 default:
122 return( PSA_ERROR_BAD_STATE );
123 }
124 operation->alg = 0;
125 return( PSA_SUCCESS );
Steven Cooreman0e307642021-02-18 16:18:32 +0100126}
127
Ronald Cron0266cfe2021-03-13 18:50:11 +0100128psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100129 mbedtls_psa_hash_operation_t *operation,
130 psa_algorithm_t alg )
131{
132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
133
134 /* A context must be freshly initialized before it can be set up. */
135 if( operation->alg != 0 )
136 {
137 return( PSA_ERROR_BAD_STATE );
138 }
139
140 switch( alg )
141 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100142#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100143 case PSA_ALG_MD5:
144 mbedtls_md5_init( &operation->ctx.md5 );
TRodziewicz26371e42021-06-08 16:45:41 +0200145 ret = mbedtls_md5_starts( &operation->ctx.md5 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100146 break;
147#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100148#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100149 case PSA_ALG_RIPEMD160:
150 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
TRodziewicz26371e42021-06-08 16:45:41 +0200151 ret = mbedtls_ripemd160_starts( &operation->ctx.ripemd160 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100152 break;
153#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100154#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100155 case PSA_ALG_SHA_1:
156 mbedtls_sha1_init( &operation->ctx.sha1 );
TRodziewicz26371e42021-06-08 16:45:41 +0200157 ret = mbedtls_sha1_starts( &operation->ctx.sha1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100158 break;
159#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100160#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100161 case PSA_ALG_SHA_224:
162 mbedtls_sha256_init( &operation->ctx.sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200163 ret = mbedtls_sha256_starts( &operation->ctx.sha256, 1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100164 break;
165#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100166#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100167 case PSA_ALG_SHA_256:
168 mbedtls_sha256_init( &operation->ctx.sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200169 ret = mbedtls_sha256_starts( &operation->ctx.sha256, 0 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100170 break;
171#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100172#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100173 case PSA_ALG_SHA_384:
174 mbedtls_sha512_init( &operation->ctx.sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200175 ret = mbedtls_sha512_starts( &operation->ctx.sha512, 1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100176 break;
177#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100178#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100179 case PSA_ALG_SHA_512:
180 mbedtls_sha512_init( &operation->ctx.sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200181 ret = mbedtls_sha512_starts( &operation->ctx.sha512, 0 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100182 break;
183#endif
184 default:
185 return( PSA_ALG_IS_HASH( alg ) ?
186 PSA_ERROR_NOT_SUPPORTED :
187 PSA_ERROR_INVALID_ARGUMENT );
188 }
189 if( ret == 0 )
190 operation->alg = alg;
191 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100192 mbedtls_psa_hash_abort( operation );
Steven Cooreman0e307642021-02-18 16:18:32 +0100193 return( mbedtls_to_psa_error( ret ) );
194}
195
Ronald Cron0266cfe2021-03-13 18:50:11 +0100196psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100197 const mbedtls_psa_hash_operation_t *source_operation,
198 mbedtls_psa_hash_operation_t *target_operation )
199{
200 switch( source_operation->alg )
201 {
202 case 0:
203 return( PSA_ERROR_BAD_STATE );
Ronald Cron0266cfe2021-03-13 18:50:11 +0100204#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100205 case PSA_ALG_MD5:
206 mbedtls_md5_clone( &target_operation->ctx.md5,
207 &source_operation->ctx.md5 );
208 break;
209#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100210#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100211 case PSA_ALG_RIPEMD160:
212 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
213 &source_operation->ctx.ripemd160 );
214 break;
215#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100216#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100217 case PSA_ALG_SHA_1:
218 mbedtls_sha1_clone( &target_operation->ctx.sha1,
219 &source_operation->ctx.sha1 );
220 break;
221#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100222#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100223 case PSA_ALG_SHA_224:
224 mbedtls_sha256_clone( &target_operation->ctx.sha256,
225 &source_operation->ctx.sha256 );
226 break;
227#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100228#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100229 case PSA_ALG_SHA_256:
230 mbedtls_sha256_clone( &target_operation->ctx.sha256,
231 &source_operation->ctx.sha256 );
232 break;
233#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100234#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100235 case PSA_ALG_SHA_384:
236 mbedtls_sha512_clone( &target_operation->ctx.sha512,
237 &source_operation->ctx.sha512 );
238 break;
239#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100240#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100241 case PSA_ALG_SHA_512:
242 mbedtls_sha512_clone( &target_operation->ctx.sha512,
243 &source_operation->ctx.sha512 );
244 break;
245#endif
246 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100247 (void) source_operation;
248 (void) target_operation;
Steven Cooreman0e307642021-02-18 16:18:32 +0100249 return( PSA_ERROR_NOT_SUPPORTED );
250 }
251
252 target_operation->alg = source_operation->alg;
253 return( PSA_SUCCESS );
254}
255
Ronald Cron0266cfe2021-03-13 18:50:11 +0100256psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100257 mbedtls_psa_hash_operation_t *operation,
258 const uint8_t *input,
259 size_t input_length )
260{
261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
262
Steven Cooreman0e307642021-02-18 16:18:32 +0100263 switch( operation->alg )
264 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100265#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100266 case PSA_ALG_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200267 ret = mbedtls_md5_update( &operation->ctx.md5,
Steven Cooreman0e307642021-02-18 16:18:32 +0100268 input, input_length );
269 break;
270#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100271#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100272 case PSA_ALG_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200273 ret = mbedtls_ripemd160_update( &operation->ctx.ripemd160,
Steven Cooreman0e307642021-02-18 16:18:32 +0100274 input, input_length );
275 break;
276#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100277#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100278 case PSA_ALG_SHA_1:
TRodziewicz26371e42021-06-08 16:45:41 +0200279 ret = mbedtls_sha1_update( &operation->ctx.sha1,
Steven Cooreman0e307642021-02-18 16:18:32 +0100280 input, input_length );
281 break;
282#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100283#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100284 case PSA_ALG_SHA_224:
TRodziewicz26371e42021-06-08 16:45:41 +0200285 ret = mbedtls_sha256_update( &operation->ctx.sha256,
Steven Cooreman0e307642021-02-18 16:18:32 +0100286 input, input_length );
287 break;
288#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100289#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100290 case PSA_ALG_SHA_256:
TRodziewicz26371e42021-06-08 16:45:41 +0200291 ret = mbedtls_sha256_update( &operation->ctx.sha256,
Steven Cooreman0e307642021-02-18 16:18:32 +0100292 input, input_length );
293 break;
294#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100295#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100296 case PSA_ALG_SHA_384:
TRodziewicz26371e42021-06-08 16:45:41 +0200297 ret = mbedtls_sha512_update( &operation->ctx.sha512,
Steven Cooreman0e307642021-02-18 16:18:32 +0100298 input, input_length );
299 break;
300#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100301#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100302 case PSA_ALG_SHA_512:
TRodziewicz26371e42021-06-08 16:45:41 +0200303 ret = mbedtls_sha512_update( &operation->ctx.sha512,
Steven Cooreman0e307642021-02-18 16:18:32 +0100304 input, input_length );
305 break;
306#endif
307 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100308 (void) input;
309 (void) input_length;
Steven Cooreman0e307642021-02-18 16:18:32 +0100310 return( PSA_ERROR_BAD_STATE );
311 }
312
Steven Cooreman0e307642021-02-18 16:18:32 +0100313 return( mbedtls_to_psa_error( ret ) );
314}
315
Ronald Cron0266cfe2021-03-13 18:50:11 +0100316psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100317 mbedtls_psa_hash_operation_t *operation,
318 uint8_t *hash,
319 size_t hash_size,
320 size_t *hash_length )
321{
322 psa_status_t status;
323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
324 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
325
326 /* Fill the output buffer with something that isn't a valid hash
327 * (barring an attack on the hash and deliberately-crafted input),
328 * in case the caller doesn't check the return status properly. */
329 *hash_length = hash_size;
330 /* If hash_size is 0 then hash may be NULL and then the
331 * call to memset would have undefined behavior. */
332 if( hash_size != 0 )
333 memset( hash, '!', hash_size );
334
335 if( hash_size < actual_hash_length )
336 {
337 status = PSA_ERROR_BUFFER_TOO_SMALL;
338 goto exit;
339 }
340
341 switch( operation->alg )
342 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100343#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100344 case PSA_ALG_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200345 ret = mbedtls_md5_finish( &operation->ctx.md5, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100346 break;
347#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100348#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100349 case PSA_ALG_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200350 ret = mbedtls_ripemd160_finish( &operation->ctx.ripemd160, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100351 break;
352#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100353#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100354 case PSA_ALG_SHA_1:
TRodziewicz26371e42021-06-08 16:45:41 +0200355 ret = mbedtls_sha1_finish( &operation->ctx.sha1, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100356 break;
357#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100358#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100359 case PSA_ALG_SHA_224:
TRodziewicz26371e42021-06-08 16:45:41 +0200360 ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100361 break;
362#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100363#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100364 case PSA_ALG_SHA_256:
TRodziewicz26371e42021-06-08 16:45:41 +0200365 ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100366 break;
367#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100368#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100369 case PSA_ALG_SHA_384:
TRodziewicz26371e42021-06-08 16:45:41 +0200370 ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100371 break;
372#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100373#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100374 case PSA_ALG_SHA_512:
TRodziewicz26371e42021-06-08 16:45:41 +0200375 ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100376 break;
377#endif
378 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100379 (void) hash;
Steven Cooreman0e307642021-02-18 16:18:32 +0100380 return( PSA_ERROR_BAD_STATE );
381 }
382 status = mbedtls_to_psa_error( ret );
383
384exit:
385 if( status == PSA_SUCCESS )
Steven Cooreman0e307642021-02-18 16:18:32 +0100386 *hash_length = actual_hash_length;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100387 return( status );
Steven Cooreman0e307642021-02-18 16:18:32 +0100388}
389
Ronald Cron0266cfe2021-03-13 18:50:11 +0100390psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100391 psa_algorithm_t alg,
392 const uint8_t *input,
393 size_t input_length,
394 uint8_t *hash,
395 size_t hash_size,
396 size_t *hash_length)
397{
398 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100400 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100401
402 *hash_length = hash_size;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100403 status = mbedtls_psa_hash_setup( &operation, alg );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100404 if( status != PSA_SUCCESS )
405 goto exit;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100406 status = mbedtls_psa_hash_update( &operation, input, input_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100407 if( status != PSA_SUCCESS )
408 goto exit;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100409 status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100410 if( status != PSA_SUCCESS )
411 goto exit;
412
413exit:
Ronald Cron0266cfe2021-03-13 18:50:11 +0100414 abort_status = mbedtls_psa_hash_abort( &operation );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100415 if( status == PSA_SUCCESS )
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100416 return( abort_status );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100417 else
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100418 return( status );
419
Steven Cooreman83f300e2021-03-08 17:09:48 +0100420}
Steven Cooreman0d586662021-03-08 20:28:18 +0100421#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100422
Steven Cooreman0e307642021-02-18 16:18:32 +0100423#endif /* MBEDTLS_PSA_CRYPTO_C */