@@ -447,27 +447,32 @@ static void psa_hash_operation(void)
447
447
size_t size_to_read = 0 ;
448
448
size_t allocation_size = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
449
449
450
- input_buffer = mbedtls_calloc ( 1 , allocation_size );
451
- if ( input_buffer == NULL ) {
452
- psa_hash_abort ( msg . rhandle );
453
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
454
- } else {
455
- while ( data_remaining > 0 ) {
456
- size_to_read = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
457
- bytes_read = psa_read ( msg . handle , 1 , input_buffer , size_to_read );
450
+ if ( allocation_size > 0 ) {
451
+ input_buffer = mbedtls_calloc ( 1 , allocation_size );
452
+ if ( input_buffer == NULL ) {
453
+ psa_hash_abort ( msg . rhandle ) ;
454
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
455
+ } else {
456
+ while (data_remaining > 0 ) {
457
+ size_to_read = MIN ( data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
458
458
459
- if (bytes_read != size_to_read ) {
460
- SPM_PANIC ("SPM read length mismatch" );
461
- }
459
+ bytes_read = psa_read (msg .handle , 1 , input_buffer , size_to_read );
460
+ if (bytes_read != size_to_read ) {
461
+ SPM_PANIC ("SPM read length mismatch" );
462
+ }
462
463
463
- status = psa_hash_update (msg .rhandle , input_buffer , bytes_read );
464
- // stop on error
465
- if (status != PSA_SUCCESS ) {
466
- break ;
464
+ status = psa_hash_update (msg .rhandle , input_buffer , bytes_read );
465
+ // stop on error
466
+ if (status != PSA_SUCCESS ) {
467
+ break ;
468
+ }
469
+ data_remaining = data_remaining - bytes_read ;
467
470
}
468
- data_remaining = data_remaining - bytes_read ;
471
+
472
+ mbedtls_free (input_buffer );
469
473
}
470
- mbedtls_free (input_buffer );
474
+ } else {
475
+ status = psa_hash_update (msg .rhandle , input_buffer , allocation_size );
471
476
}
472
477
473
478
if (status != PSA_SUCCESS ) {
@@ -479,25 +484,30 @@ static void psa_hash_operation(void)
479
484
}
480
485
481
486
case PSA_HASH_FINISH : {
482
- size_t hash_size = 0 ;
483
- bytes_read = psa_read (msg .handle , 1 , & hash_size ,
484
- msg .in_size [1 ]);
487
+ uint8_t * hash = NULL ;
488
+ size_t hash_size = 0 , hash_length = 0 ;
489
+
490
+ bytes_read = psa_read (msg .handle , 1 , & hash_size , msg .in_size [1 ]);
485
491
if (bytes_read != msg .in_size [1 ]) {
486
492
SPM_PANIC ("SPM read length mismatch" );
487
493
}
488
494
489
- size_t hash_length = 0 ;
490
- uint8_t * hash = mbedtls_calloc (1 , hash_size );
491
- if (hash == NULL ) {
492
- psa_hash_abort (msg .rhandle );
493
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
494
- } else {
495
+ if (hash_size > 0 ) {
496
+ hash = mbedtls_calloc (1 , hash_size );
497
+ if (hash == NULL ) {
498
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
499
+ }
500
+ }
501
+
502
+ if (status == PSA_SUCCESS ) {
495
503
status = psa_hash_finish (msg .rhandle , hash , hash_size , & hash_length );
496
504
if (status == PSA_SUCCESS ) {
497
505
psa_write (msg .handle , 0 , hash , hash_length );
498
506
psa_write (msg .handle , 1 , & hash_length , sizeof (hash_length ));
499
507
}
500
508
mbedtls_free (hash );
509
+ } else {
510
+ psa_hash_abort (msg .rhandle );
501
511
}
502
512
503
513
destroy_hash_clone (msg .rhandle );
@@ -507,26 +517,31 @@ static void psa_hash_operation(void)
507
517
}
508
518
509
519
case PSA_HASH_VERIFY : {
520
+ uint8_t * hash = NULL ;
510
521
size_t hash_length = 0 ;
511
- bytes_read = psa_read (msg .handle , 1 , & hash_length ,
512
- msg .in_size [1 ]);
513
- if (bytes_read != msg .in_size [1 ] ||
514
- hash_length != msg .in_size [2 ]) {
522
+
523
+ bytes_read = psa_read (msg .handle , 1 , & hash_length , msg .in_size [1 ]);
524
+ if (bytes_read != msg .in_size [1 ] || hash_length != msg .in_size [2 ]) {
515
525
SPM_PANIC ("SPM read length mismatch" );
516
526
}
517
527
518
- uint8_t * hash = mbedtls_calloc (1 , hash_length );
519
- if (hash == NULL ) {
520
- psa_hash_abort (msg .rhandle );
521
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
522
- } else {
523
- bytes_read = psa_read (msg .handle , 2 , hash , msg .in_size [2 ]);
524
- if (bytes_read != msg .in_size [2 ]) {
525
- SPM_PANIC ("SPM read length mismatch" );
528
+ if (hash_length > 0 ) {
529
+ hash = mbedtls_calloc (1 , hash_length );
530
+ if (hash == NULL ) {
531
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
532
+ } else {
533
+ bytes_read = psa_read (msg .handle , 2 , hash , hash_length );
534
+ if (bytes_read != hash_length ) {
535
+ SPM_PANIC ("SPM read length mismatch" );
536
+ }
526
537
}
538
+ }
527
539
540
+ if (status == PSA_SUCCESS ) {
528
541
status = psa_hash_verify (msg .rhandle , hash , hash_length );
529
542
mbedtls_free (hash );
543
+ } else {
544
+ psa_hash_abort (msg .rhandle );
530
545
}
531
546
532
547
destroy_hash_clone (msg .rhandle );
0 commit comments