@@ -919,28 +919,30 @@ static void psa_symmetric_operation(void)
919
919
920
920
switch (psa_crypto_ipc .func ) {
921
921
case PSA_CIPHER_ENCRYPT_SETUP : {
922
- if (!psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle ,
923
- msg .client_id )) {
922
+ if (psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle , msg .client_id )) {
923
+ status = psa_cipher_encrypt_setup (msg .rhandle , psa_crypto_ipc .handle , psa_crypto_ipc .alg );
924
+ } else {
924
925
status = PSA_ERROR_INVALID_HANDLE ;
925
- break ;
926
926
}
927
927
928
- status = psa_cipher_encrypt_setup (msg .rhandle ,
929
- psa_crypto_ipc .handle ,
930
- psa_crypto_ipc .alg );
928
+ if (status != PSA_SUCCESS ) {
929
+ mbedtls_free (msg .rhandle );
930
+ psa_set_rhandle (msg .handle , NULL );
931
+ }
931
932
break ;
932
933
}
933
934
934
935
case PSA_CIPHER_DECRYPT_SETUP : {
935
- if (!psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle ,
936
- msg .client_id )) {
936
+ if (psa_crypto_access_control_is_handle_permitted (psa_crypto_ipc .handle , msg .client_id )) {
937
+ status = psa_cipher_decrypt_setup (msg .rhandle , psa_crypto_ipc .handle , psa_crypto_ipc .alg );
938
+ } else {
937
939
status = PSA_ERROR_INVALID_HANDLE ;
938
- break ;
939
940
}
940
941
941
- status = psa_cipher_decrypt_setup (msg .rhandle ,
942
- psa_crypto_ipc .handle ,
943
- psa_crypto_ipc .alg );
942
+ if (status != PSA_SUCCESS ) {
943
+ mbedtls_free (msg .rhandle );
944
+ psa_set_rhandle (msg .handle , NULL );
945
+ }
944
946
break ;
945
947
}
946
948
@@ -955,6 +957,9 @@ static void psa_symmetric_operation(void)
955
957
psa_write (msg .handle , 0 , iv , iv_length );
956
958
psa_write (msg .handle , 1 , & iv_length ,
957
959
sizeof (iv_length ));
960
+ } else {
961
+ mbedtls_free (msg .rhandle );
962
+ psa_set_rhandle (msg .handle , NULL );
958
963
}
959
964
break ;
960
965
}
@@ -968,46 +973,46 @@ static void psa_symmetric_operation(void)
968
973
SPM_PANIC ("SPM read length mismatch" );
969
974
}
970
975
status = psa_cipher_set_iv (msg .rhandle , iv , iv_length );
971
-
976
+ if (status != PSA_SUCCESS ) {
977
+ mbedtls_free (msg .rhandle );
978
+ psa_set_rhandle (msg .handle , NULL );
979
+ }
972
980
break ;
973
981
}
974
982
975
983
case PSA_CIPHER_UPDATE : {
976
984
size_t input_length = msg .in_size [1 ];
977
985
size_t output_size = msg .out_size [0 ];
978
986
size_t output_length = 0 ;
979
- uint8_t * input ;
980
- unsigned char * output ;
987
+ uint8_t * input = NULL ;
988
+ unsigned char * output = NULL ;
981
989
982
990
input = mbedtls_calloc (1 , input_length );
983
- if (input == NULL ) {
984
- status = PSA_ERROR_INSUFFICIENT_MEMORY ;
985
- break ;
986
- }
987
-
988
- bytes_read = psa_read (msg .handle , 1 , input ,
989
- input_length );
990
- if (bytes_read != input_length ) {
991
- SPM_PANIC ("SPM read length mismatch" );
992
- }
993
-
994
991
output = mbedtls_calloc (1 , output_size );
995
- if (output == NULL ) {
996
- mbedtls_free ( input );
992
+ if (input == NULL || output == NULL ) {
993
+ psa_cipher_abort ( msg . rhandle );
997
994
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
998
- break ;
999
- }
995
+ } else {
996
+ bytes_read = psa_read (msg .handle , 1 , input , input_length );
997
+ if (bytes_read != input_length ) {
998
+ SPM_PANIC ("SPM read length mismatch" );
999
+ }
1000
+
1001
+ status = psa_cipher_update (msg .rhandle , input , input_length , output , output_size ,
1002
+ & output_length );
1003
+ if (status == PSA_SUCCESS ) {
1004
+ psa_write (msg .handle , 0 , output , output_length );
1005
+ psa_write (msg .handle , 1 , & output_length , sizeof (output_length ));
1006
+ }
1000
1007
1001
- status = psa_cipher_update (msg .rhandle , input ,
1002
- input_length , output , output_size , & output_length );
1003
- if (status == PSA_SUCCESS ) {
1004
- psa_write (msg .handle , 0 , output , output_length );
1005
- psa_write (msg .handle , 1 ,
1006
- & output_length , sizeof (output_length ));
1007
1008
}
1008
1009
1009
1010
mbedtls_free (input );
1010
1011
mbedtls_free (output );
1012
+ if (status != PSA_SUCCESS ) {
1013
+ mbedtls_free (msg .rhandle );
1014
+ psa_set_rhandle (msg .handle , NULL );
1015
+ }
1011
1016
break ;
1012
1017
}
1013
1018
@@ -1018,23 +1023,26 @@ static void psa_symmetric_operation(void)
1018
1023
1019
1024
output = mbedtls_calloc (1 , output_size );
1020
1025
if (output == NULL ) {
1026
+ psa_cipher_abort (msg .rhandle );
1021
1027
status = PSA_ERROR_INSUFFICIENT_MEMORY ;
1022
- break ;
1028
+ } else {
1029
+ status = psa_cipher_finish (msg .rhandle , output , output_size , & output_length );
1030
+ if (status == PSA_SUCCESS ) {
1031
+ psa_write (msg .handle , 0 , output , output_length );
1032
+ psa_write (msg .handle , 1 , & output_length , sizeof (output_length ));
1033
+ }
1034
+ mbedtls_free (output );
1023
1035
}
1024
1036
1025
- status = psa_cipher_finish (msg .rhandle , output ,
1026
- output_size , & output_length );
1027
- if (status == PSA_SUCCESS ) {
1028
- psa_write (msg .handle , 0 , output , output_length );
1029
- psa_write (msg .handle , 1 ,
1030
- & output_length , sizeof (output_length ));
1031
- }
1032
- mbedtls_free (output );
1037
+ mbedtls_free (msg .rhandle );
1038
+ psa_set_rhandle (msg .handle , NULL );
1033
1039
break ;
1034
1040
}
1035
1041
1036
1042
case PSA_CIPHER_ABORT : {
1037
1043
status = psa_cipher_abort (msg .rhandle );
1044
+ mbedtls_free (msg .rhandle );
1045
+ psa_set_rhandle (msg .handle , NULL );
1038
1046
break ;
1039
1047
}
1040
1048
@@ -1048,8 +1056,8 @@ static void psa_symmetric_operation(void)
1048
1056
}
1049
1057
1050
1058
case PSA_IPC_DISCONNECT : {
1051
- psa_cipher_abort (msg .rhandle );
1052
1059
if (msg .rhandle != NULL ) {
1060
+ psa_cipher_abort (msg .rhandle );
1053
1061
mbedtls_free (msg .rhandle );
1054
1062
}
1055
1063
0 commit comments