@@ -414,6 +414,10 @@ static void psa_hash_operation(void)
414
414
case PSA_HASH_SETUP : {
415
415
status = psa_hash_setup (msg .rhandle ,
416
416
psa_crypto .alg );
417
+ if (status != PSA_SUCCESS ) {
418
+ mbedtls_free (msg .rhandle );
419
+ psa_set_rhandle (msg .handle , NULL );
420
+ }
417
421
break ;
418
422
}
419
423
@@ -425,32 +429,32 @@ static void psa_hash_operation(void)
425
429
426
430
input_buffer = mbedtls_calloc (1 , allocation_size );
427
431
if (input_buffer == NULL ) {
432
+ psa_hash_abort (msg .rhandle );
428
433
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
429
- break ;
430
- }
431
-
432
- while (data_remaining > 0 ) {
433
- size_to_read = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
434
- bytes_read = psa_read (msg .handle , 1 , input_buffer ,
435
- size_to_read );
436
-
437
- if (bytes_read != size_to_read ) {
438
- SPM_PANIC ("SPM read length mismatch" );
439
- }
440
-
441
- status = psa_hash_update (msg .rhandle ,
442
- input_buffer ,
443
- bytes_read );
444
-
445
- // stop on error
446
- if (status != PSA_SUCCESS ) {
447
- break ;
434
+ } else {
435
+ while (data_remaining > 0 ) {
436
+ size_to_read = MIN (data_remaining , MAX_DATA_CHUNK_SIZE_IN_BYTES );
437
+ bytes_read = psa_read (msg .handle , 1 , input_buffer , size_to_read );
438
+
439
+ if (bytes_read != size_to_read ) {
440
+ SPM_PANIC ("SPM read length mismatch" );
441
+ }
442
+
443
+ status = psa_hash_update (msg .rhandle , input_buffer , bytes_read );
444
+ // stop on error
445
+ if (status != PSA_SUCCESS ) {
446
+ break ;
447
+ }
448
+ data_remaining = data_remaining - bytes_read ;
448
449
}
449
- data_remaining = data_remaining - bytes_read ;
450
+ mbedtls_free ( input_buffer ) ;
450
451
}
451
452
452
- mbedtls_free (input_buffer );
453
-
453
+ if (status != PSA_SUCCESS ) {
454
+ destroy_hash_clone (msg .rhandle );
455
+ mbedtls_free (msg .rhandle );
456
+ psa_set_rhandle (msg .handle , NULL );
457
+ }
454
458
break ;
455
459
}
456
460
@@ -465,20 +469,20 @@ static void psa_hash_operation(void)
465
469
size_t hash_length = 0 ;
466
470
uint8_t * hash = mbedtls_calloc (1 , hash_size );
467
471
if (hash == NULL ) {
472
+ psa_hash_abort (msg .rhandle );
468
473
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
469
- break ;
470
- }
471
-
472
- status = psa_hash_finish (msg .rhandle , hash , hash_size ,
473
- & hash_length );
474
- if (status == PSA_SUCCESS ) {
475
- psa_write (msg .handle , 0 , hash , hash_length );
476
- psa_write (msg .handle , 1 , & hash_length ,
477
- sizeof (hash_length ));
474
+ } else {
475
+ status = psa_hash_finish (msg .rhandle , hash , hash_size , & hash_length );
476
+ if (status == PSA_SUCCESS ) {
477
+ psa_write (msg .handle , 0 , hash , hash_length );
478
+ psa_write (msg .handle , 1 , & hash_length , sizeof (hash_length ));
479
+ }
480
+ mbedtls_free (hash );
478
481
}
479
482
480
- mbedtls_free (hash );
481
483
destroy_hash_clone (msg .rhandle );
484
+ mbedtls_free (msg .rhandle );
485
+ psa_set_rhandle (msg .handle , NULL );
482
486
break ;
483
487
}
484
488
@@ -493,24 +497,29 @@ static void psa_hash_operation(void)
493
497
494
498
uint8_t * hash = mbedtls_calloc (1 , hash_length );
495
499
if (hash == NULL ) {
500
+ psa_hash_abort (msg .rhandle );
496
501
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
497
- break ;
498
- }
502
+ } else {
503
+ bytes_read = psa_read (msg .handle , 2 , hash , msg .in_size [2 ]);
504
+ if (bytes_read != msg .in_size [2 ]) {
505
+ SPM_PANIC ("SPM read length mismatch" );
506
+ }
499
507
500
- bytes_read = psa_read (msg .handle , 2 , hash , msg .in_size [2 ]);
501
- if (bytes_read != msg .in_size [2 ]) {
502
- SPM_PANIC ("SPM read length mismatch" );
508
+ status = psa_hash_verify (msg .rhandle , hash , hash_length );
509
+ mbedtls_free (hash );
503
510
}
504
511
505
- status = psa_hash_verify (msg .rhandle , hash , hash_length );
506
- mbedtls_free (hash );
507
512
destroy_hash_clone (msg .rhandle );
513
+ mbedtls_free (msg .rhandle );
514
+ psa_set_rhandle (msg .handle , NULL );
508
515
break ;
509
516
}
510
517
511
518
case PSA_HASH_ABORT : {
512
519
status = psa_hash_abort (msg .rhandle );
513
520
destroy_hash_clone (msg .rhandle );
521
+ mbedtls_free (msg .rhandle );
522
+ psa_set_rhandle (msg .handle , NULL );
514
523
break ;
515
524
}
516
525
@@ -537,6 +546,10 @@ static void psa_hash_operation(void)
537
546
status = psa_hash_clone (hash_clone -> source_operation , msg .rhandle );
538
547
release_hash_clone (hash_clone );
539
548
}
549
+ if (status != PSA_SUCCESS ) {
550
+ mbedtls_free (msg .rhandle );
551
+ psa_set_rhandle (msg .handle , NULL );
552
+ }
540
553
break ;
541
554
}
542
555
@@ -550,8 +563,8 @@ static void psa_hash_operation(void)
550
563
}
551
564
552
565
case PSA_IPC_DISCONNECT : {
553
- psa_hash_abort (msg .rhandle );
554
566
if (msg .rhandle != NULL ) {
567
+ psa_hash_abort (msg .rhandle );
555
568
destroy_hash_clone (msg .rhandle );
556
569
mbedtls_free (msg .rhandle );
557
570
}
0 commit comments