1 /*
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 
32 /* Use builtin defines specific to this compilation unit, since the test driver
33  * relies on the software driver. */
34 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_MD5) || \
35     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_MD5) ) )
36 #define BUILTIN_ALG_MD5         1
37 #endif
38 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160) || \
39     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) ) )
40 #define BUILTIN_ALG_RIPEMD160   1
41 #endif
42 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1) || \
43     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) ) )
44 #define BUILTIN_ALG_SHA_1       1
45 #endif
46 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224) || \
47     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) ) )
48 #define BUILTIN_ALG_SHA_224     1
49 #endif
50 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256) || \
51     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) ) )
52 #define BUILTIN_ALG_SHA_256     1
53 #endif
54 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384) || \
55     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) ) )
56 #define BUILTIN_ALG_SHA_384     1
57 #endif
58 #if( defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512) || \
59     ( defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) ) )
60 #define BUILTIN_ALG_SHA_512     1
61 #endif
62 
63 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
64     defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
65     defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
66     defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
mbedtls_md_info_from_psa(psa_algorithm_t alg)67 const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
68 {
69     switch( alg )
70     {
71 #if defined(MBEDTLS_MD5_C)
72         case PSA_ALG_MD5:
73             return( &mbedtls_md5_info );
74 #endif
75 #if defined(MBEDTLS_RIPEMD160_C)
76         case PSA_ALG_RIPEMD160:
77             return( &mbedtls_ripemd160_info );
78 #endif
79 #if defined(MBEDTLS_SHA1_C)
80         case PSA_ALG_SHA_1:
81             return( &mbedtls_sha1_info );
82 #endif
83 #if defined(MBEDTLS_SHA224_C)
84         case PSA_ALG_SHA_224:
85             return( &mbedtls_sha224_info );
86 #endif
87 #if defined(MBEDTLS_SHA256_C)
88         case PSA_ALG_SHA_256:
89             return( &mbedtls_sha256_info );
90 #endif
91 #if defined(MBEDTLS_SHA384_C)
92         case PSA_ALG_SHA_384:
93             return( &mbedtls_sha384_info );
94 #endif
95 #if defined(MBEDTLS_SHA512_C)
96         case PSA_ALG_SHA_512:
97             return( &mbedtls_sha512_info );
98 #endif
99         default:
100             return( NULL );
101     }
102 }
103 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
104         * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
105         * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
106         * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
107 
108 /* Implement the PSA driver hash interface on top of mbed TLS if either the
109  * software driver or the test driver requires it. */
110 #if defined(MBEDTLS_PSA_BUILTIN_HASH) || defined(PSA_CRYPTO_DRIVER_TEST)
hash_abort(mbedtls_psa_hash_operation_t * operation)111 static psa_status_t hash_abort(
112     mbedtls_psa_hash_operation_t *operation )
113 {
114     switch( operation->alg )
115     {
116         case 0:
117             /* The object has (apparently) been initialized but it is not
118              * in use. It's ok to call abort on such an object, and there's
119              * nothing to do. */
120             break;
121 #if defined(BUILTIN_ALG_MD5)
122         case PSA_ALG_MD5:
123             mbedtls_md5_free( &operation->ctx.md5 );
124             break;
125 #endif
126 #if defined(BUILTIN_ALG_RIPEMD160)
127         case PSA_ALG_RIPEMD160:
128             mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
129             break;
130 #endif
131 #if defined(BUILTIN_ALG_SHA_1)
132         case PSA_ALG_SHA_1:
133             mbedtls_sha1_free( &operation->ctx.sha1 );
134             break;
135 #endif
136 #if defined(BUILTIN_ALG_SHA_224)
137         case PSA_ALG_SHA_224:
138             mbedtls_sha256_free( &operation->ctx.sha256 );
139             break;
140 #endif
141 #if defined(BUILTIN_ALG_SHA_256)
142         case PSA_ALG_SHA_256:
143             mbedtls_sha256_free( &operation->ctx.sha256 );
144             break;
145 #endif
146 #if defined(BUILTIN_ALG_SHA_384)
147         case PSA_ALG_SHA_384:
148             mbedtls_sha512_free( &operation->ctx.sha512 );
149             break;
150 #endif
151 #if defined(BUILTIN_ALG_SHA_512)
152         case PSA_ALG_SHA_512:
153             mbedtls_sha512_free( &operation->ctx.sha512 );
154             break;
155 #endif
156         default:
157             return( PSA_ERROR_BAD_STATE );
158     }
159     operation->alg = 0;
160     return( PSA_SUCCESS );
161 }
162 
hash_setup(mbedtls_psa_hash_operation_t * operation,psa_algorithm_t alg)163 static psa_status_t hash_setup(
164     mbedtls_psa_hash_operation_t *operation,
165     psa_algorithm_t alg )
166 {
167     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
168 
169     /* A context must be freshly initialized before it can be set up. */
170     if( operation->alg != 0 )
171     {
172         return( PSA_ERROR_BAD_STATE );
173     }
174 
175     switch( alg )
176     {
177 #if defined(BUILTIN_ALG_MD5)
178         case PSA_ALG_MD5:
179             mbedtls_md5_init( &operation->ctx.md5 );
180             ret = mbedtls_md5_starts( &operation->ctx.md5 );
181             break;
182 #endif
183 #if defined(BUILTIN_ALG_RIPEMD160)
184         case PSA_ALG_RIPEMD160:
185             mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
186             ret = mbedtls_ripemd160_starts( &operation->ctx.ripemd160 );
187             break;
188 #endif
189 #if defined(BUILTIN_ALG_SHA_1)
190         case PSA_ALG_SHA_1:
191             mbedtls_sha1_init( &operation->ctx.sha1 );
192             ret = mbedtls_sha1_starts( &operation->ctx.sha1 );
193             break;
194 #endif
195 #if defined(BUILTIN_ALG_SHA_224)
196         case PSA_ALG_SHA_224:
197             mbedtls_sha256_init( &operation->ctx.sha256 );
198             ret = mbedtls_sha256_starts( &operation->ctx.sha256, 1 );
199             break;
200 #endif
201 #if defined(BUILTIN_ALG_SHA_256)
202         case PSA_ALG_SHA_256:
203             mbedtls_sha256_init( &operation->ctx.sha256 );
204             ret = mbedtls_sha256_starts( &operation->ctx.sha256, 0 );
205             break;
206 #endif
207 #if defined(BUILTIN_ALG_SHA_384)
208         case PSA_ALG_SHA_384:
209             mbedtls_sha512_init( &operation->ctx.sha512 );
210             ret = mbedtls_sha512_starts( &operation->ctx.sha512, 1 );
211             break;
212 #endif
213 #if defined(BUILTIN_ALG_SHA_512)
214         case PSA_ALG_SHA_512:
215             mbedtls_sha512_init( &operation->ctx.sha512 );
216             ret = mbedtls_sha512_starts( &operation->ctx.sha512, 0 );
217             break;
218 #endif
219         default:
220             return( PSA_ALG_IS_HASH( alg ) ?
221                     PSA_ERROR_NOT_SUPPORTED :
222                     PSA_ERROR_INVALID_ARGUMENT );
223     }
224     if( ret == 0 )
225         operation->alg = alg;
226     else
227         hash_abort( operation );
228     return( mbedtls_to_psa_error( ret ) );
229 }
230 
hash_clone(const mbedtls_psa_hash_operation_t * source_operation,mbedtls_psa_hash_operation_t * target_operation)231 static psa_status_t hash_clone(
232     const mbedtls_psa_hash_operation_t *source_operation,
233     mbedtls_psa_hash_operation_t *target_operation )
234 {
235     switch( source_operation->alg )
236     {
237         case 0:
238             return( PSA_ERROR_BAD_STATE );
239 #if defined(BUILTIN_ALG_MD5)
240         case PSA_ALG_MD5:
241             mbedtls_md5_clone( &target_operation->ctx.md5,
242                                &source_operation->ctx.md5 );
243             break;
244 #endif
245 #if defined(BUILTIN_ALG_RIPEMD160)
246         case PSA_ALG_RIPEMD160:
247             mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
248                                      &source_operation->ctx.ripemd160 );
249             break;
250 #endif
251 #if defined(BUILTIN_ALG_SHA_1)
252         case PSA_ALG_SHA_1:
253             mbedtls_sha1_clone( &target_operation->ctx.sha1,
254                                 &source_operation->ctx.sha1 );
255             break;
256 #endif
257 #if defined(BUILTIN_ALG_SHA_224)
258         case PSA_ALG_SHA_224:
259             mbedtls_sha256_clone( &target_operation->ctx.sha256,
260                                   &source_operation->ctx.sha256 );
261             break;
262 #endif
263 #if defined(BUILTIN_ALG_SHA_256)
264         case PSA_ALG_SHA_256:
265             mbedtls_sha256_clone( &target_operation->ctx.sha256,
266                                   &source_operation->ctx.sha256 );
267             break;
268 #endif
269 #if defined(BUILTIN_ALG_SHA_384)
270         case PSA_ALG_SHA_384:
271             mbedtls_sha512_clone( &target_operation->ctx.sha512,
272                                   &source_operation->ctx.sha512 );
273             break;
274 #endif
275 #if defined(BUILTIN_ALG_SHA_512)
276         case PSA_ALG_SHA_512:
277             mbedtls_sha512_clone( &target_operation->ctx.sha512,
278                                   &source_operation->ctx.sha512 );
279             break;
280 #endif
281         default:
282             (void) source_operation;
283             (void) target_operation;
284             return( PSA_ERROR_NOT_SUPPORTED );
285     }
286 
287     target_operation->alg = source_operation->alg;
288     return( PSA_SUCCESS );
289 }
290 
hash_update(mbedtls_psa_hash_operation_t * operation,const uint8_t * input,size_t input_length)291 static psa_status_t hash_update(
292     mbedtls_psa_hash_operation_t *operation,
293     const uint8_t *input,
294     size_t input_length )
295 {
296     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
297 
298     switch( operation->alg )
299     {
300 #if defined(BUILTIN_ALG_MD5)
301         case PSA_ALG_MD5:
302             ret = mbedtls_md5_update( &operation->ctx.md5,
303                                           input, input_length );
304             break;
305 #endif
306 #if defined(BUILTIN_ALG_RIPEMD160)
307         case PSA_ALG_RIPEMD160:
308             ret = mbedtls_ripemd160_update( &operation->ctx.ripemd160,
309                                                 input, input_length );
310             break;
311 #endif
312 #if defined(BUILTIN_ALG_SHA_1)
313         case PSA_ALG_SHA_1:
314             ret = mbedtls_sha1_update( &operation->ctx.sha1,
315                                            input, input_length );
316             break;
317 #endif
318 #if defined(BUILTIN_ALG_SHA_224)
319         case PSA_ALG_SHA_224:
320             ret = mbedtls_sha256_update( &operation->ctx.sha256,
321                                              input, input_length );
322             break;
323 #endif
324 #if defined(BUILTIN_ALG_SHA_256)
325         case PSA_ALG_SHA_256:
326             ret = mbedtls_sha256_update( &operation->ctx.sha256,
327                                              input, input_length );
328             break;
329 #endif
330 #if defined(BUILTIN_ALG_SHA_384)
331         case PSA_ALG_SHA_384:
332             ret = mbedtls_sha512_update( &operation->ctx.sha512,
333                                              input, input_length );
334             break;
335 #endif
336 #if defined(BUILTIN_ALG_SHA_512)
337         case PSA_ALG_SHA_512:
338             ret = mbedtls_sha512_update( &operation->ctx.sha512,
339                                              input, input_length );
340             break;
341 #endif
342         default:
343             (void) input;
344             (void) input_length;
345             return( PSA_ERROR_BAD_STATE );
346     }
347 
348     return( mbedtls_to_psa_error( ret ) );
349 }
350 
hash_finish(mbedtls_psa_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)351 static psa_status_t hash_finish(
352     mbedtls_psa_hash_operation_t *operation,
353     uint8_t *hash,
354     size_t hash_size,
355     size_t *hash_length )
356 {
357     psa_status_t status;
358     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
359     size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
360 
361     /* Fill the output buffer with something that isn't a valid hash
362      * (barring an attack on the hash and deliberately-crafted input),
363      * in case the caller doesn't check the return status properly. */
364     *hash_length = hash_size;
365     /* If hash_size is 0 then hash may be NULL and then the
366      * call to memset would have undefined behavior. */
367     if( hash_size != 0 )
368         memset( hash, '!', hash_size );
369 
370     if( hash_size < actual_hash_length )
371     {
372         status = PSA_ERROR_BUFFER_TOO_SMALL;
373         goto exit;
374     }
375 
376     switch( operation->alg )
377     {
378 #if defined(BUILTIN_ALG_MD5)
379         case PSA_ALG_MD5:
380             ret = mbedtls_md5_finish( &operation->ctx.md5, hash );
381             break;
382 #endif
383 #if defined(BUILTIN_ALG_RIPEMD160)
384         case PSA_ALG_RIPEMD160:
385             ret = mbedtls_ripemd160_finish( &operation->ctx.ripemd160, hash );
386             break;
387 #endif
388 #if defined(BUILTIN_ALG_SHA_1)
389         case PSA_ALG_SHA_1:
390             ret = mbedtls_sha1_finish( &operation->ctx.sha1, hash );
391             break;
392 #endif
393 #if defined(BUILTIN_ALG_SHA_224)
394         case PSA_ALG_SHA_224:
395             ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
396             break;
397 #endif
398 #if defined(BUILTIN_ALG_SHA_256)
399         case PSA_ALG_SHA_256:
400             ret = mbedtls_sha256_finish( &operation->ctx.sha256, hash );
401             break;
402 #endif
403 #if defined(BUILTIN_ALG_SHA_384)
404         case PSA_ALG_SHA_384:
405             ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
406             break;
407 #endif
408 #if defined(BUILTIN_ALG_SHA_512)
409         case PSA_ALG_SHA_512:
410             ret = mbedtls_sha512_finish( &operation->ctx.sha512, hash );
411             break;
412 #endif
413         default:
414             (void) hash;
415             return( PSA_ERROR_BAD_STATE );
416     }
417     status = mbedtls_to_psa_error( ret );
418 
419 exit:
420     if( status == PSA_SUCCESS )
421         *hash_length = actual_hash_length;
422     return( status );
423 }
424 
hash_compute(psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * hash,size_t hash_size,size_t * hash_length)425 static psa_status_t hash_compute(
426     psa_algorithm_t alg,
427     const uint8_t *input,
428     size_t input_length,
429     uint8_t *hash,
430     size_t hash_size,
431     size_t *hash_length)
432 {
433     mbedtls_psa_hash_operation_t operation = MBEDTLS_PSA_HASH_OPERATION_INIT;
434     psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
435     psa_status_t abort_status = PSA_ERROR_CORRUPTION_DETECTED;
436 
437     *hash_length = hash_size;
438     status = hash_setup( &operation, alg );
439     if( status != PSA_SUCCESS )
440         goto exit;
441     status = hash_update( &operation, input, input_length );
442     if( status != PSA_SUCCESS )
443         goto exit;
444     status = hash_finish( &operation, hash, hash_size, hash_length );
445     if( status != PSA_SUCCESS )
446         goto exit;
447 
448 exit:
449     abort_status = hash_abort( &operation );
450     if( status == PSA_SUCCESS )
451         return( abort_status );
452     else
453         return( status );
454 
455 }
456 #endif /* MBEDTLS_PSA_BUILTIN_HASH || PSA_CRYPTO_DRIVER_TEST */
457 
458 #if defined(MBEDTLS_PSA_BUILTIN_HASH)
mbedtls_psa_hash_compute(psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * hash,size_t hash_size,size_t * hash_length)459 psa_status_t mbedtls_psa_hash_compute(
460     psa_algorithm_t alg,
461     const uint8_t *input,
462     size_t input_length,
463     uint8_t *hash,
464     size_t hash_size,
465     size_t *hash_length)
466 {
467     return( hash_compute( alg, input, input_length,
468                           hash, hash_size, hash_length ) );
469 }
470 
mbedtls_psa_hash_setup(mbedtls_psa_hash_operation_t * operation,psa_algorithm_t alg)471 psa_status_t mbedtls_psa_hash_setup(
472     mbedtls_psa_hash_operation_t *operation,
473     psa_algorithm_t alg )
474 {
475     return( hash_setup( operation, alg ) );
476 }
477 
mbedtls_psa_hash_clone(const mbedtls_psa_hash_operation_t * source_operation,mbedtls_psa_hash_operation_t * target_operation)478 psa_status_t mbedtls_psa_hash_clone(
479     const mbedtls_psa_hash_operation_t *source_operation,
480     mbedtls_psa_hash_operation_t *target_operation )
481 {
482     return( hash_clone( source_operation, target_operation ) );
483 }
484 
mbedtls_psa_hash_update(mbedtls_psa_hash_operation_t * operation,const uint8_t * input,size_t input_length)485 psa_status_t mbedtls_psa_hash_update(
486     mbedtls_psa_hash_operation_t *operation,
487     const uint8_t *input,
488     size_t input_length )
489 {
490     return( hash_update( operation, input, input_length ) );
491 }
492 
mbedtls_psa_hash_finish(mbedtls_psa_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)493 psa_status_t mbedtls_psa_hash_finish(
494     mbedtls_psa_hash_operation_t *operation,
495     uint8_t *hash,
496     size_t hash_size,
497     size_t *hash_length )
498 {
499     return( hash_finish( operation, hash, hash_size, hash_length ) );
500 }
501 
mbedtls_psa_hash_abort(mbedtls_psa_hash_operation_t * operation)502 psa_status_t mbedtls_psa_hash_abort(
503     mbedtls_psa_hash_operation_t *operation )
504 {
505     return( hash_abort( operation ) );
506 }
507 #endif /* MBEDTLS_PSA_BUILTIN_HASH */
508 
509  /*
510   * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
511   */
512 #if defined(PSA_CRYPTO_DRIVER_TEST)
513 
is_hash_accelerated(psa_algorithm_t alg)514 static int is_hash_accelerated( psa_algorithm_t alg )
515 {
516     switch( alg )
517     {
518 #if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
519         case PSA_ALG_MD5:
520             return( 1 );
521 #endif
522 #if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
523         case PSA_ALG_RIPEMD160:
524             return( 1 );
525 #endif
526 #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
527         case PSA_ALG_SHA_1:
528             return( 1 );
529 #endif
530 #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
531         case PSA_ALG_SHA_224:
532             return( 1 );
533 #endif
534 #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
535         case PSA_ALG_SHA_256:
536             return( 1 );
537 #endif
538 #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
539         case PSA_ALG_SHA_384:
540             return( 1 );
541 #endif
542 #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
543         case PSA_ALG_SHA_512:
544             return( 1 );
545 #endif
546         default:
547             return( 0 );
548     }
549 }
550 
mbedtls_transparent_test_driver_hash_compute(psa_algorithm_t alg,const uint8_t * input,size_t input_length,uint8_t * hash,size_t hash_size,size_t * hash_length)551 psa_status_t mbedtls_transparent_test_driver_hash_compute(
552     psa_algorithm_t alg,
553     const uint8_t *input,
554     size_t input_length,
555     uint8_t *hash,
556     size_t hash_size,
557     size_t *hash_length)
558 {
559     if( is_hash_accelerated( alg ) )
560         return( hash_compute( alg, input, input_length,
561                               hash, hash_size, hash_length ) );
562     else
563         return( PSA_ERROR_NOT_SUPPORTED );
564 }
565 
mbedtls_transparent_test_driver_hash_setup(mbedtls_transparent_test_driver_hash_operation_t * operation,psa_algorithm_t alg)566 psa_status_t mbedtls_transparent_test_driver_hash_setup(
567     mbedtls_transparent_test_driver_hash_operation_t *operation,
568     psa_algorithm_t alg )
569 {
570     if( is_hash_accelerated( alg ) )
571         return( hash_setup( operation, alg ) );
572     else
573         return( PSA_ERROR_NOT_SUPPORTED );
574 }
575 
mbedtls_transparent_test_driver_hash_clone(const mbedtls_transparent_test_driver_hash_operation_t * source_operation,mbedtls_transparent_test_driver_hash_operation_t * target_operation)576 psa_status_t mbedtls_transparent_test_driver_hash_clone(
577     const mbedtls_transparent_test_driver_hash_operation_t *source_operation,
578     mbedtls_transparent_test_driver_hash_operation_t *target_operation )
579 {
580     if( is_hash_accelerated( source_operation->alg ) )
581         return( hash_clone( source_operation, target_operation ) );
582     else
583         return( PSA_ERROR_BAD_STATE );
584 }
585 
mbedtls_transparent_test_driver_hash_update(mbedtls_transparent_test_driver_hash_operation_t * operation,const uint8_t * input,size_t input_length)586 psa_status_t mbedtls_transparent_test_driver_hash_update(
587     mbedtls_transparent_test_driver_hash_operation_t *operation,
588     const uint8_t *input,
589     size_t input_length )
590 {
591     if( is_hash_accelerated( operation->alg ) )
592         return( hash_update( operation, input, input_length ) );
593     else
594         return( PSA_ERROR_BAD_STATE );
595 }
596 
mbedtls_transparent_test_driver_hash_finish(mbedtls_transparent_test_driver_hash_operation_t * operation,uint8_t * hash,size_t hash_size,size_t * hash_length)597 psa_status_t mbedtls_transparent_test_driver_hash_finish(
598     mbedtls_transparent_test_driver_hash_operation_t *operation,
599     uint8_t *hash,
600     size_t hash_size,
601     size_t *hash_length )
602 {
603     if( is_hash_accelerated( operation->alg ) )
604         return( hash_finish( operation, hash, hash_size, hash_length ) );
605     else
606         return( PSA_ERROR_BAD_STATE );
607 }
608 
mbedtls_transparent_test_driver_hash_abort(mbedtls_transparent_test_driver_hash_operation_t * operation)609 psa_status_t mbedtls_transparent_test_driver_hash_abort(
610     mbedtls_transparent_test_driver_hash_operation_t *operation )
611 {
612     return( hash_abort( operation ) );
613 }
614 
615 #endif /* PSA_CRYPTO_DRIVER_TEST */
616 
617 #endif /* MBEDTLS_PSA_CRYPTO_C */
618