2121
2222#include <otp_crypto.h>
2323
24- #include <stdio.h>
25-
2624#include <context.h>
2725#include <defaultatoms.h>
2826#include <globalcontext.h>
@@ -299,7 +297,7 @@ static term nif_crypto_hash(Context *ctx, int argc, term argv[])
299297 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT ;
300298 psa_status_t status = psa_hash_setup (& operation , alg );
301299 if (UNLIKELY (status != PSA_SUCCESS )) {
302- fprintf ( stderr , "crypto:hash psa_hash_setup failed with status %d for alg 0x%08lx\n" , (int ) status , (unsigned long ) alg );
300+ TRACE ( "crypto:hash psa_hash_setup failed with status %d for alg 0x%08lx\n" , (int ) status , (unsigned long ) alg );
303301 RAISE_ERROR (BADARG_ATOM );
304302 }
305303
@@ -611,6 +609,9 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
611609#if MBEDTLS_VERSION_NUMBER >= 0x04000000
612610 bool encrypt = true;
613611 bool padding_pkcs7 = false;
612+ psa_key_id_t key_id = 0 ;
613+ void * temp_buf = NULL ;
614+ psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT ;
614615
615616 if (term_is_list (flag_or_options )) {
616617 term encrypt_flag = interop_kv_get_value_default (
@@ -660,59 +661,45 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
660661 psa_set_key_type (& attributes , key_type );
661662 psa_set_key_bits (& attributes , key_bits );
662663
663- psa_key_id_t key_id ;
664664 psa_status_t status = psa_import_key (& attributes , key_data , key_len , & key_id );
665665 if (UNLIKELY (status != PSA_SUCCESS )) {
666- free (allocated_key_data );
667- free (allocated_iv_data );
668- free (allocated_data_data );
669666 char err_msg [48 ];
670667 snprintf (err_msg , sizeof (err_msg ), "key import err %d" , (int ) status );
671- RAISE_ERROR (make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx ));
668+ error_atom = make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx );
669+ goto psa_error ;
672670 }
673671
674672 size_t output_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE (key_type , alg , data_size );
675673 if (!encrypt ) {
676674 output_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE (key_type , alg , data_size );
677675 }
678- void * temp_buf = malloc (output_size );
676+ temp_buf = malloc (output_size );
679677 if (IS_NULL_PTR (temp_buf )) {
680- psa_destroy_key (key_id );
681678 error_atom = OUT_OF_MEMORY_ATOM ;
682- goto raise_error ;
679+ goto psa_error ;
683680 }
684681
685682 size_t output_len ;
686- psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT ;
687683 if (encrypt ) {
688684 status = psa_cipher_encrypt_setup (& operation , key_id , alg );
689685 } else {
690686 status = psa_cipher_decrypt_setup (& operation , key_id , alg );
691687 }
692688 if (UNLIKELY (status != PSA_SUCCESS )) {
693- psa_destroy_key (key_id );
694- free (temp_buf );
695- free (allocated_key_data );
696- free (allocated_iv_data );
697- free (allocated_data_data );
698689 char err_msg [48 ];
699690 snprintf (err_msg , sizeof (err_msg ), "cipher setup err %d" , (int ) status );
700- RAISE_ERROR (make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx ));
691+ error_atom = make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx );
692+ goto psa_error ;
701693 }
702694
703695 // PSA rejects IVs for ECB; ignore IV to preserve legacy behavior.
704696 if (iv_len > 0 && alg != PSA_ALG_ECB_NO_PADDING ) {
705697 status = psa_cipher_set_iv (& operation , iv_data , iv_len );
706698 if (UNLIKELY (status != PSA_SUCCESS )) {
707- psa_cipher_abort (& operation );
708- psa_destroy_key (key_id );
709- free (temp_buf );
710- free (allocated_key_data );
711- free (allocated_iv_data );
712- free (allocated_data_data );
713699 char err_msg [24 ];
714700 snprintf (err_msg , sizeof (err_msg ), "IV err %d" , (int ) status );
715- RAISE_ERROR (make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx ));
701+ error_atom = make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx );
702+ goto psa_error ;
716703 }
717704 }
718705
@@ -728,6 +715,9 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
728715 psa_cipher_abort (& operation );
729716 psa_destroy_key (key_id );
730717 free (temp_buf );
718+ if (allocated_key_data ) {
719+ memset (allocated_key_data , 0 , key_len );
720+ }
731721 free (allocated_key_data );
732722 free (allocated_iv_data );
733723 free (allocated_data_data );
@@ -742,34 +732,27 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
742732 size_t update_len = 0 ;
743733 status = psa_cipher_update (& operation , data_data , process_size , temp_buf , output_size , & update_len );
744734 if (UNLIKELY (status != PSA_SUCCESS )) {
745- psa_cipher_abort (& operation );
746- psa_destroy_key (key_id );
747- free (temp_buf );
748- free (allocated_key_data );
749- free (allocated_iv_data );
750- free (allocated_data_data );
751735 char err_msg [24 ];
752736 snprintf (err_msg , sizeof (err_msg ), "update err %d" , (int ) status );
753- RAISE_ERROR (make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx ));
737+ error_atom = make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx );
738+ goto psa_error ;
754739 }
755740
756741 size_t finish_len = 0 ;
757742 status = psa_cipher_finish (& operation , (uint8_t * ) temp_buf + update_len , output_size - update_len , & finish_len );
758743 if (UNLIKELY (status != PSA_SUCCESS )) {
759- psa_cipher_abort (& operation );
760- psa_destroy_key (key_id );
761- free (temp_buf );
762- free (allocated_key_data );
763- free (allocated_iv_data );
764- free (allocated_data_data );
765744 char err_msg [24 ];
766745 snprintf (err_msg , sizeof (err_msg ), "finish err %d" , (int ) status );
767- RAISE_ERROR (make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx ));
746+ error_atom = make_crypto_error (__FILE__ , __LINE__ , err_msg , ctx );
747+ goto psa_error ;
768748 }
769749 output_len = update_len + finish_len ;
770750
771751 psa_destroy_key (key_id );
772752
753+ if (allocated_key_data ) {
754+ memset (allocated_key_data , 0 , key_len );
755+ }
773756 free (allocated_key_data );
774757 free (allocated_iv_data );
775758 free (allocated_data_data );
@@ -783,6 +766,17 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
783766 term out = term_from_literal_binary (temp_buf , output_len , & ctx -> heap , ctx -> global );
784767 free (temp_buf );
785768 return out ;
769+
770+ psa_error :
771+ psa_cipher_abort (& operation );
772+ if (key_id != 0 ) {
773+ psa_destroy_key (key_id );
774+ }
775+ free (temp_buf );
776+ if (allocated_key_data ) {
777+ memset (allocated_key_data , 0 , key_len );
778+ }
779+ goto raise_error ;
786780#else
787781 mbedtls_operation_t operation ;
788782 mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_NONE ;
0 commit comments