@@ -1009,30 +1009,39 @@ static void psa_symmetric_operation(void)
1009
1009
}
1010
1010
1011
1011
case PSA_CIPHER_UPDATE : {
1012
- size_t input_length = msg .in_size [1 ];
1013
- size_t output_size = msg .out_size [0 ];
1014
- size_t output_length = 0 ;
1012
+ size_t input_length = msg .in_size [1 ],
1013
+ output_size = msg .out_size [0 ],
1014
+ output_length = 0 ;
1015
1015
uint8_t * input = NULL ;
1016
1016
unsigned char * output = NULL ;
1017
1017
1018
- input = mbedtls_calloc ( 1 , input_length );
1019
- output = mbedtls_calloc (1 , output_size );
1020
- if ( input == NULL || output == NULL ) {
1021
- psa_cipher_abort ( msg . rhandle ) ;
1022
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1023
- } else {
1024
- bytes_read = psa_read ( msg . handle , 1 , input , input_length );
1025
- if ( bytes_read != input_length ) {
1026
- SPM_PANIC ( "SPM read length mismatch" );
1018
+ if ( input_length > 0 ) {
1019
+ input = mbedtls_calloc (1 , input_length );
1020
+ if ( input == NULL ) {
1021
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1022
+ } else {
1023
+ bytes_read = psa_read ( msg . handle , 1 , input , input_length );
1024
+ if ( bytes_read != input_length ) {
1025
+ SPM_PANIC ( "SPM read length mismatch" );
1026
+ }
1027
1027
}
1028
+ }
1029
+ if (status == PSA_SUCCESS && output_size > 0 ) {
1030
+ output = mbedtls_calloc (1 , output_size );
1031
+ if (output == NULL ) {
1032
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1033
+ }
1034
+ }
1028
1035
1036
+ if (status == PSA_SUCCESS ) {
1029
1037
status = psa_cipher_update (msg .rhandle , input , input_length , output , output_size ,
1030
1038
& output_length );
1031
1039
if (status == PSA_SUCCESS ) {
1032
1040
psa_write (msg .handle , 0 , output , output_length );
1033
1041
psa_write (msg .handle , 1 , & output_length , sizeof (output_length ));
1034
1042
}
1035
-
1043
+ } else {
1044
+ psa_cipher_abort (msg .rhandle );
1036
1045
}
1037
1046
1038
1047
mbedtls_free (input );
@@ -1045,21 +1054,26 @@ static void psa_symmetric_operation(void)
1045
1054
}
1046
1055
1047
1056
case PSA_CIPHER_FINISH : {
1048
- uint8_t * output ;
1049
- size_t output_size = msg .out_size [0 ];
1050
- size_t output_length = 0 ;
1057
+ uint8_t * output = NULL ;
1058
+ size_t output_size = msg .out_size [0 ],
1059
+ output_length = 0 ;
1051
1060
1052
- output = mbedtls_calloc (1 , output_size );
1053
- if (output == NULL ) {
1054
- psa_cipher_abort (msg .rhandle );
1055
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1056
- } else {
1061
+ if (output_size > 0 ) {
1062
+ output = mbedtls_calloc (1 , output_size );
1063
+ if (output == NULL ) {
1064
+ status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1065
+ }
1066
+ }
1067
+
1068
+ if (status == PSA_SUCCESS ) {
1057
1069
status = psa_cipher_finish (msg .rhandle , output , output_size , & output_length );
1058
1070
if (status == PSA_SUCCESS ) {
1059
1071
psa_write (msg .handle , 0 , output , output_length );
1060
1072
psa_write (msg .handle , 1 , & output_length , sizeof (output_length ));
1061
1073
}
1062
1074
mbedtls_free (output );
1075
+ } else {
1076
+ psa_cipher_abort (msg .rhandle );
1063
1077
}
1064
1078
1065
1079
mbedtls_free (msg .rhandle );
0 commit comments