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