blob: f8f7fc6ba4f0c0f3cc68fe0729f47ea897cff4d3 [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
Steven Cooreman83f300e2021-03-08 17:09:48 +010077/* Implement the PSA driver hash interface on top of mbed TLS if either the
78 * software driver or the test driver requires it. */
Ronald Cron0266cfe2021-03-13 18:50:11 +010079#if defined(MBEDTLS_PSA_BUILTIN_HASH)
80psa_status_t mbedtls_psa_hash_abort(
Steven Cooreman83f300e2021-03-08 17:09:48 +010081 mbedtls_psa_hash_operation_t *operation )
Steven Cooreman0e307642021-02-18 16:18:32 +010082{
Steven Cooreman83f300e2021-03-08 17:09:48 +010083 switch( operation->alg )
84 {
85 case 0:
86 /* The object has (apparently) been initialized but it is not
87 * in use. It's ok to call abort on such an object, and there's
88 * nothing to do. */
89 break;
Ronald Cron0266cfe2021-03-13 18:50:11 +010090#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman83f300e2021-03-08 17:09:48 +010091 case PSA_ALG_MD5:
92 mbedtls_md5_free( &operation->ctx.md5 );
93 break;
94#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +010095#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman83f300e2021-03-08 17:09:48 +010096 case PSA_ALG_RIPEMD160:
97 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
98 break;
99#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100100#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100101 case PSA_ALG_SHA_1:
102 mbedtls_sha1_free( &operation->ctx.sha1 );
103 break;
104#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100105#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100106 case PSA_ALG_SHA_224:
107 mbedtls_sha256_free( &operation->ctx.sha256 );
108 break;
109#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100110#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100111 case PSA_ALG_SHA_256:
112 mbedtls_sha256_free( &operation->ctx.sha256 );
113 break;
114#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100115#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100116 case PSA_ALG_SHA_384:
117 mbedtls_sha512_free( &operation->ctx.sha512 );
118 break;
119#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100120#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman83f300e2021-03-08 17:09:48 +0100121 case PSA_ALG_SHA_512:
122 mbedtls_sha512_free( &operation->ctx.sha512 );
123 break;
124#endif
125 default:
126 return( PSA_ERROR_BAD_STATE );
127 }
128 operation->alg = 0;
129 return( PSA_SUCCESS );
Steven Cooreman0e307642021-02-18 16:18:32 +0100130}
131
Ronald Cron0266cfe2021-03-13 18:50:11 +0100132psa_status_t mbedtls_psa_hash_setup(
Steven Cooreman0e307642021-02-18 16:18:32 +0100133 mbedtls_psa_hash_operation_t *operation,
134 psa_algorithm_t alg )
135{
136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
137
138 /* A context must be freshly initialized before it can be set up. */
139 if( operation->alg != 0 )
140 {
141 return( PSA_ERROR_BAD_STATE );
142 }
143
144 switch( alg )
145 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100146#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100147 case PSA_ALG_MD5:
148 mbedtls_md5_init( &operation->ctx.md5 );
TRodziewicz26371e42021-06-08 16:45:41 +0200149 ret = mbedtls_md5_starts( &operation->ctx.md5 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100150 break;
151#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100152#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100153 case PSA_ALG_RIPEMD160:
154 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
TRodziewicz26371e42021-06-08 16:45:41 +0200155 ret = mbedtls_ripemd160_starts( &operation->ctx.ripemd160 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100156 break;
157#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100158#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100159 case PSA_ALG_SHA_1:
160 mbedtls_sha1_init( &operation->ctx.sha1 );
TRodziewicz26371e42021-06-08 16:45:41 +0200161 ret = mbedtls_sha1_starts( &operation->ctx.sha1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100162 break;
163#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100164#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100165 case PSA_ALG_SHA_224:
166 mbedtls_sha256_init( &operation->ctx.sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200167 ret = mbedtls_sha256_starts( &operation->ctx.sha256, 1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100168 break;
169#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100170#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100171 case PSA_ALG_SHA_256:
172 mbedtls_sha256_init( &operation->ctx.sha256 );
TRodziewicz26371e42021-06-08 16:45:41 +0200173 ret = mbedtls_sha256_starts( &operation->ctx.sha256, 0 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100174 break;
175#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100176#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100177 case PSA_ALG_SHA_384:
178 mbedtls_sha512_init( &operation->ctx.sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200179 ret = mbedtls_sha512_starts( &operation->ctx.sha512, 1 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100180 break;
181#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100182#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100183 case PSA_ALG_SHA_512:
184 mbedtls_sha512_init( &operation->ctx.sha512 );
TRodziewicz26371e42021-06-08 16:45:41 +0200185 ret = mbedtls_sha512_starts( &operation->ctx.sha512, 0 );
Steven Cooreman0e307642021-02-18 16:18:32 +0100186 break;
187#endif
188 default:
189 return( PSA_ALG_IS_HASH( alg ) ?
190 PSA_ERROR_NOT_SUPPORTED :
191 PSA_ERROR_INVALID_ARGUMENT );
192 }
193 if( ret == 0 )
194 operation->alg = alg;
195 else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100196 mbedtls_psa_hash_abort( operation );
Steven Cooreman0e307642021-02-18 16:18:32 +0100197 return( mbedtls_to_psa_error( ret ) );
198}
199
Ronald Cron0266cfe2021-03-13 18:50:11 +0100200psa_status_t mbedtls_psa_hash_clone(
Steven Cooreman0e307642021-02-18 16:18:32 +0100201 const mbedtls_psa_hash_operation_t *source_operation,
202 mbedtls_psa_hash_operation_t *target_operation )
203{
204 switch( source_operation->alg )
205 {
206 case 0:
207 return( PSA_ERROR_BAD_STATE );
Ronald Cron0266cfe2021-03-13 18:50:11 +0100208#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100209 case PSA_ALG_MD5:
210 mbedtls_md5_clone( &target_operation->ctx.md5,
211 &source_operation->ctx.md5 );
212 break;
213#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100214#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100215 case PSA_ALG_RIPEMD160:
216 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
217 &source_operation->ctx.ripemd160 );
218 break;
219#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100220#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100221 case PSA_ALG_SHA_1:
222 mbedtls_sha1_clone( &target_operation->ctx.sha1,
223 &source_operation->ctx.sha1 );
224 break;
225#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100226#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100227 case PSA_ALG_SHA_224:
228 mbedtls_sha256_clone( &target_operation->ctx.sha256,
229 &source_operation->ctx.sha256 );
230 break;
231#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100232#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100233 case PSA_ALG_SHA_256:
234 mbedtls_sha256_clone( &target_operation->ctx.sha256,
235 &source_operation->ctx.sha256 );
236 break;
237#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100238#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100239 case PSA_ALG_SHA_384:
240 mbedtls_sha512_clone( &target_operation->ctx.sha512,
241 &source_operation->ctx.sha512 );
242 break;
243#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100244#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100245 case PSA_ALG_SHA_512:
246 mbedtls_sha512_clone( &target_operation->ctx.sha512,
247 &source_operation->ctx.sha512 );
248 break;
249#endif
250 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100251 (void) source_operation;
252 (void) target_operation;
Steven Cooreman0e307642021-02-18 16:18:32 +0100253 return( PSA_ERROR_NOT_SUPPORTED );
254 }
255
256 target_operation->alg = source_operation->alg;
257 return( PSA_SUCCESS );
258}
259
Ronald Cron0266cfe2021-03-13 18:50:11 +0100260psa_status_t mbedtls_psa_hash_update(
Steven Cooreman0e307642021-02-18 16:18:32 +0100261 mbedtls_psa_hash_operation_t *operation,
262 const uint8_t *input,
263 size_t input_length )
264{
265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
266
Steven Cooreman0e307642021-02-18 16:18:32 +0100267 switch( operation->alg )
268 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100269#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100270 case PSA_ALG_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200271 ret = mbedtls_md5_update( &operation->ctx.md5,
Steven Cooreman0e307642021-02-18 16:18:32 +0100272 input, input_length );
273 break;
274#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100275#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100276 case PSA_ALG_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200277 ret = mbedtls_ripemd160_update( &operation->ctx.ripemd160,
Steven Cooreman0e307642021-02-18 16:18:32 +0100278 input, input_length );
279 break;
280#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100281#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100282 case PSA_ALG_SHA_1:
TRodziewicz26371e42021-06-08 16:45:41 +0200283 ret = mbedtls_sha1_update( &operation->ctx.sha1,
Steven Cooreman0e307642021-02-18 16:18:32 +0100284 input, input_length );
285 break;
286#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100287#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100288 case PSA_ALG_SHA_224:
TRodziewicz26371e42021-06-08 16:45:41 +0200289 ret = mbedtls_sha256_update( &operation->ctx.sha256,
Steven Cooreman0e307642021-02-18 16:18:32 +0100290 input, input_length );
291 break;
292#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100293#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100294 case PSA_ALG_SHA_256:
TRodziewicz26371e42021-06-08 16:45:41 +0200295 ret = mbedtls_sha256_update( &operation->ctx.sha256,
Steven Cooreman0e307642021-02-18 16:18:32 +0100296 input, input_length );
297 break;
298#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100299#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100300 case PSA_ALG_SHA_384:
TRodziewicz26371e42021-06-08 16:45:41 +0200301 ret = mbedtls_sha512_update( &operation->ctx.sha512,
Steven Cooreman0e307642021-02-18 16:18:32 +0100302 input, input_length );
303 break;
304#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100305#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100306 case PSA_ALG_SHA_512:
TRodziewicz26371e42021-06-08 16:45:41 +0200307 ret = mbedtls_sha512_update( &operation->ctx.sha512,
Steven Cooreman0e307642021-02-18 16:18:32 +0100308 input, input_length );
309 break;
310#endif
311 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100312 (void) input;
313 (void) input_length;
Steven Cooreman0e307642021-02-18 16:18:32 +0100314 return( PSA_ERROR_BAD_STATE );
315 }
316
Steven Cooreman0e307642021-02-18 16:18:32 +0100317 return( mbedtls_to_psa_error( ret ) );
318}
319
Ronald Cron0266cfe2021-03-13 18:50:11 +0100320psa_status_t mbedtls_psa_hash_finish(
Steven Cooreman0e307642021-02-18 16:18:32 +0100321 mbedtls_psa_hash_operation_t *operation,
322 uint8_t *hash,
323 size_t hash_size,
324 size_t *hash_length )
325{
326 psa_status_t status;
327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
328 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
329
330 /* Fill the output buffer with something that isn't a valid hash
331 * (barring an attack on the hash and deliberately-crafted input),
332 * in case the caller doesn't check the return status properly. */
333 *hash_length = hash_size;
334 /* If hash_size is 0 then hash may be NULL and then the
335 * call to memset would have undefined behavior. */
336 if( hash_size != 0 )
337 memset( hash, '!', hash_size );
338
339 if( hash_size < actual_hash_length )
340 {
341 status = PSA_ERROR_BUFFER_TOO_SMALL;
342 goto exit;
343 }
344
345 switch( operation->alg )
346 {
Ronald Cron0266cfe2021-03-13 18:50:11 +0100347#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Steven Cooreman0e307642021-02-18 16:18:32 +0100348 case PSA_ALG_MD5:
TRodziewicz26371e42021-06-08 16:45:41 +0200349 ret = mbedtls_md5_finish( &operation->ctx.md5, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100350 break;
351#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100352#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Steven Cooreman0e307642021-02-18 16:18:32 +0100353 case PSA_ALG_RIPEMD160:
TRodziewicz26371e42021-06-08 16:45:41 +0200354 ret = mbedtls_ripemd160_finish( &operation->ctx.ripemd160, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100355 break;
356#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100357#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Steven Cooreman0e307642021-02-18 16:18:32 +0100358 case PSA_ALG_SHA_1:
TRodziewicz26371e42021-06-08 16:45:41 +0200359 ret = mbedtls_sha1_finish( &operation->ctx.sha1, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100360 break;
361#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100362#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Steven Cooreman0e307642021-02-18 16:18:32 +0100363 case PSA_ALG_SHA_224:
TRodziewicz26371e42021-06-08 16:45:41 +0200364 ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100365 break;
366#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100367#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Steven Cooreman0e307642021-02-18 16:18:32 +0100368 case PSA_ALG_SHA_256:
TRodziewicz26371e42021-06-08 16:45:41 +0200369 ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100370 break;
371#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100372#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Steven Cooreman0e307642021-02-18 16:18:32 +0100373 case PSA_ALG_SHA_384:
TRodziewicz26371e42021-06-08 16:45:41 +0200374 ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100375 break;
376#endif
Ronald Cron0266cfe2021-03-13 18:50:11 +0100377#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Steven Cooreman0e307642021-02-18 16:18:32 +0100378 case PSA_ALG_SHA_512:
TRodziewicz26371e42021-06-08 16:45:41 +0200379 ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
Steven Cooreman0e307642021-02-18 16:18:32 +0100380 break;
381#endif
382 default:
Steven Cooreman5adf52c2021-03-04 18:09:49 +0100383 (void) hash;
Steven Cooreman0e307642021-02-18 16:18:32 +0100384 return( PSA_ERROR_BAD_STATE );
385 }
386 status = mbedtls_to_psa_error( ret );
387
388exit:
389 if( status == PSA_SUCCESS )
Steven Cooreman0e307642021-02-18 16:18:32 +0100390 *hash_length = actual_hash_length;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100391 return( status );
Steven Cooreman0e307642021-02-18 16:18:32 +0100392}
393
Ronald Cron0266cfe2021-03-13 18:50:11 +0100394psa_status_t mbedtls_psa_hash_compute(
Steven Cooreman83f300e2021-03-08 17:09:48 +0100395 psa_algorithm_t alg,
396 const uint8_t *input,
397 size_t input_length,
398 uint8_t *hash,
399 size_t hash_size,
400 size_t *hash_length)
401{
402 mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100404 psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
Steven Cooreman83f300e2021-03-08 17:09:48 +0100405
406 *hash_length = hash_size;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100407 status = mbedtls_psa_hash_setup( &operation, alg );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100408 if( status != PSA_SUCCESS )
409 goto exit;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100410 status = mbedtls_psa_hash_update( &operation, input, input_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100411 if( status != PSA_SUCCESS )
412 goto exit;
Ronald Cron0266cfe2021-03-13 18:50:11 +0100413 status = mbedtls_psa_hash_finish( &operation, hash, hash_size, hash_length );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100414 if( status != PSA_SUCCESS )
415 goto exit;
416
417exit:
Ronald Cron0266cfe2021-03-13 18:50:11 +0100418 abort_status = mbedtls_psa_hash_abort( &operation );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100419 if( status == PSA_SUCCESS )
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100420 return( abort_status );
Steven Cooreman83f300e2021-03-08 17:09:48 +0100421 else
Steven Cooreman61bb8fc2021-03-15 12:32:48 +0100422 return( status );
423
Steven Cooreman83f300e2021-03-08 17:09:48 +0100424}
Steven Cooreman0d586662021-03-08 20:28:18 +0100425#endif /* MBEDTLS_PSA_BUILTIN_HASH */
Steven Cooreman0e307642021-02-18 16:18:32 +0100426
Steven Cooreman0e307642021-02-18 16:18:32 +0100427#endif /* MBEDTLS_PSA_CRYPTO_C */