@@ -652,142 +652,132 @@ static void psa_asymmetric_operation(void)
652
652
653
653
switch (psa_crypto .func ) {
654
654
case PSA_ASYMMETRIC_SIGN : {
655
- uint8_t * signature ;
656
- uint8_t * hash ;
657
- size_t signature_length = 0 ;
658
-
659
- signature = mbedtls_calloc (1 , msg .out_size [0 ]);
660
- if (signature == NULL ) {
661
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
662
- break ;
663
- }
655
+ uint8_t * signature = NULL ;
656
+ uint8_t * hash = NULL ;
657
+ size_t signature_length = 0 ,
658
+ signature_size = msg .out_size [0 ],
659
+ hash_size = msg .in_size [1 ];
664
660
665
- hash = mbedtls_calloc ( 1 , msg . in_size [ 1 ]);
666
- if ( hash == NULL ) {
667
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
668
- mbedtls_free ( signature ) ;
669
- break ;
661
+ if ( signature_size > 0 ) {
662
+ signature = mbedtls_calloc ( 1 , signature_size );
663
+ if ( signature == NULL ) {
664
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
665
+ }
670
666
}
671
-
672
- bytes_read = psa_read (msg .handle , 1 ,
673
- hash , msg .in_size [1 ]);
674
- if (bytes_read != msg .in_size [1 ]) {
675
- SPM_PANIC ("SPM read length mismatch" );
667
+ if (status == PSA_SUCCESS && hash_size > 0 ) {
668
+ hash = mbedtls_calloc (1 , hash_size );
669
+ if (hash == NULL ) {
670
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
671
+ } else {
672
+ bytes_read = psa_read (msg .handle , 1 , hash , hash_size );
673
+ if (bytes_read != hash_size ) {
674
+ SPM_PANIC ("SPM read length mismatch" );
675
+ }
676
+ }
676
677
}
677
678
678
- status = psa_asymmetric_sign (psa_crypto .handle ,
679
- psa_crypto .alg ,
680
- hash ,
681
- msg .in_size [1 ],
682
- signature ,
683
- msg .out_size [0 ],
684
- & signature_length );
685
-
686
679
if (status == PSA_SUCCESS ) {
687
- psa_write (msg .handle , 0 , signature , signature_length );
680
+ status = psa_asymmetric_sign (psa_crypto .handle , psa_crypto .alg ,
681
+ hash , hash_size ,
682
+ signature , signature_size , & signature_length );
683
+
684
+ if (status == PSA_SUCCESS ) {
685
+ psa_write (msg .handle , 0 , signature , signature_length );
686
+ }
687
+ psa_write (msg .handle , 1 , & signature_length , sizeof (signature_length ));
688
688
}
689
689
690
- psa_write (msg .handle , 1 ,
691
- & signature_length , sizeof (signature_length ));
692
690
mbedtls_free (hash );
693
691
mbedtls_free (signature );
694
692
break ;
695
693
}
696
694
697
695
case PSA_ASYMMETRIC_VERIFY : {
698
- uint8_t * signature ;
699
- uint8_t * hash ;
700
- signature = mbedtls_calloc (1 , msg .in_size [1 ]);
701
- if (signature == NULL ) {
702
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
703
- break ;
704
- }
696
+ uint8_t * signature = NULL ;
697
+ uint8_t * hash = NULL ;
698
+ size_t signature_size = msg .in_size [1 ],
699
+ hash_size = msg .in_size [2 ];
705
700
706
- bytes_read = psa_read (msg .handle , 1 ,
707
- signature , msg .in_size [1 ]);
708
- if (bytes_read != msg .in_size [1 ]) {
709
- SPM_PANIC ("SPM read length mismatch" );
701
+ if (signature_size > 0 ) {
702
+ signature = mbedtls_calloc (1 , signature_size );
703
+ if (signature == NULL ) {
704
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
705
+ } else {
706
+ bytes_read = psa_read (msg .handle , 1 , signature , signature_size );
707
+ if (bytes_read != signature_size ) {
708
+ SPM_PANIC ("SPM read length mismatch" );
709
+ }
710
+ }
710
711
}
711
-
712
- hash = mbedtls_calloc (1 , msg .in_size [2 ]);
713
- if (hash == NULL ) {
714
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
715
- mbedtls_free (signature );
716
- break ;
712
+ if (status == PSA_SUCCESS && hash_size > 0 ) {
713
+ hash = mbedtls_calloc (1 , hash_size );
714
+ if (hash == NULL ) {
715
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
716
+ } else {
717
+ bytes_read = psa_read (msg .handle , 2 , hash , hash_size );
718
+ if (bytes_read != hash_size ) {
719
+ SPM_PANIC ("SPM read length mismatch" );
720
+ }
721
+ }
717
722
}
718
723
719
- bytes_read = psa_read ( msg . handle , 2 ,
720
- hash , msg . in_size [ 2 ]);
721
- if ( bytes_read != msg . in_size [ 2 ]) {
722
- SPM_PANIC ( "SPM read length mismatch" );
724
+ if ( status == PSA_SUCCESS ) {
725
+ status = psa_asymmetric_verify ( psa_crypto . handle , psa_crypto . alg ,
726
+ hash , hash_size ,
727
+ signature , signature_size );
723
728
}
724
729
725
- status = psa_asymmetric_verify (psa_crypto .handle ,
726
- psa_crypto .alg ,
727
- hash ,
728
- msg .in_size [2 ],
729
- signature ,
730
- msg .in_size [1 ]);
731
730
mbedtls_free (signature );
732
731
mbedtls_free (hash );
733
732
break ;
734
733
}
735
734
736
735
case PSA_ASYMMETRIC_ENCRYPT :
737
736
case PSA_ASYMMETRIC_DECRYPT : {
738
- uint8_t * input ;
739
- uint8_t * salt ;
740
- uint8_t * output ;
741
- size_t output_length = 0 ;
742
-
743
- uint8_t * buffer = mbedtls_calloc (1 , msg .in_size [1 ]);
744
- if (buffer == NULL ) {
745
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
746
- break ;
747
- }
737
+ uint8_t * input = NULL , * salt = NULL , * output = NULL , * buffer = NULL ;
738
+ size_t output_length = 0 ,
739
+ buffer_size = msg .in_size [1 ],
740
+ output_size = msg .out_size [0 ];
741
+
742
+ if (buffer_size > 0 ) {
743
+ buffer = mbedtls_calloc (1 , buffer_size );
744
+ if (buffer == NULL ) {
745
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
746
+ } else {
747
+ bytes_read = psa_read (msg .handle , 1 , buffer , buffer_size );
748
+ if (bytes_read != buffer_size ) {
749
+ SPM_PANIC ("SPM read length mismatch" );
750
+ }
748
751
749
- bytes_read = psa_read (msg .handle , 1 , buffer ,
750
- msg .in_size [1 ]);
751
- if (bytes_read != msg .in_size [1 ]) {
752
- SPM_PANIC ("SPM read length mismatch" );
752
+ input = buffer ;
753
+ salt = buffer + psa_crypto .input_length ;
754
+ }
753
755
}
754
-
755
- input = buffer ;
756
- salt = buffer + psa_crypto .input_length ;
757
- output = mbedtls_calloc (1 , msg .out_size [0 ]);
758
- if (output == NULL ) {
759
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
760
- mbedtls_free (buffer );
761
- break ;
756
+ if (status == PSA_SUCCESS && output_size > 0 ) {
757
+ output = mbedtls_calloc (1 , output_size );
758
+ if (output == NULL ) {
759
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
760
+ }
762
761
}
763
762
764
- if (psa_crypto .func == PSA_ASYMMETRIC_ENCRYPT )
765
- status = psa_asymmetric_encrypt (psa_crypto .handle ,
766
- psa_crypto .alg ,
767
- input ,
768
- psa_crypto .input_length ,
769
- salt ,
770
- psa_crypto .salt_length ,
771
- output ,
772
- msg .out_size [0 ],
773
- & output_length );
774
- else
775
- status = psa_asymmetric_decrypt (psa_crypto .handle ,
776
- psa_crypto .alg ,
777
- input ,
778
- psa_crypto .input_length ,
779
- salt ,
780
- psa_crypto .salt_length ,
781
- output ,
782
- msg .out_size [0 ],
783
- & output_length );
784
-
785
763
if (status == PSA_SUCCESS ) {
786
- psa_write (msg .handle , 0 , output , output_length );
787
- }
764
+ if (psa_crypto .func == PSA_ASYMMETRIC_ENCRYPT ) {
765
+ status = psa_asymmetric_encrypt (psa_crypto .handle , psa_crypto .alg ,
766
+ input , psa_crypto .input_length ,
767
+ salt , psa_crypto .salt_length ,
768
+ output , output_size , & output_length );
769
+ } else {
770
+ status = psa_asymmetric_decrypt (psa_crypto .handle , psa_crypto .alg ,
771
+ input , psa_crypto .input_length ,
772
+ salt , psa_crypto .salt_length ,
773
+ output , output_size , & output_length );
774
+ }
788
775
789
- psa_write (msg .handle , 1 ,
790
- & output_length , sizeof (output_length ));
776
+ if (status == PSA_SUCCESS ) {
777
+ psa_write (msg .handle , 0 , output , output_length );
778
+ }
779
+ psa_write (msg .handle , 1 , & output_length , sizeof (output_length ));
780
+ }
791
781
792
782
mbedtls_free (output );
793
783
mbedtls_free (buffer );
0 commit comments