4747#endif
4848#include <psa/crypto_types.h>
4949#include <psa/crypto.h>
50-
50+ #include <stdlib.h>
5151#include "aes_icm_ext.h"
5252#include "crypto_types.h"
5353#include "err.h" /* for srtp_debug */
@@ -310,6 +310,11 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
310310
311311 status = psa_crypto_init ();
312312
313+ if (status != PSA_SUCCESS ) {
314+ debug_print (srtp_mod_aes_icm , "status: %d" , status );
315+ return srtp_err_status_cipher_fail ;
316+ }
317+
313318 /*
314319 * set counter and initial values to 'offset' value, being careful not to
315320 * go past the end of the key buffer
@@ -346,6 +351,7 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
346351 psa_set_key_algorithm (& attr , PSA_ALG_CTR );
347352
348353 if (c -> ctx -> key_id != PSA_KEY_ID_NULL ) {
354+ psa_destroy_key (c -> ctx -> key_id );
349355 c -> ctx -> key_id = PSA_KEY_ID_NULL ;
350356 }
351357
@@ -355,6 +361,7 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
355361 if (status != PSA_SUCCESS ) {
356362 psa_destroy_key (c -> ctx -> key_id );
357363 debug_print (srtp_mod_aes_icm , "status: %d" , status );
364+ return srtp_err_status_cipher_fail ;
358365 }
359366
360367 return srtp_err_status_ok ;
@@ -431,6 +438,7 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
431438
432439 psa_status_t status = PSA_SUCCESS ;
433440 size_t out_len = 0 ;
441+ uint8_t * buffer = malloc (* dst_len );
434442
435443 debug_print (srtp_mod_aes_icm , "rs0: %s" , v128_hex_string (& c -> counter ));
436444 debug_print (srtp_mod_aes_icm , "source: %s" ,
@@ -439,14 +447,16 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
439447 if (* dst_len < src_len ) {
440448 return srtp_err_status_buffer_small ;
441449 }
442- status =
443- psa_cipher_update (& (c -> ctx -> op ), src , src_len , dst , * dst_len , & out_len );
444-
450+ status = psa_cipher_update (& (c -> ctx -> op ), src , src_len , buffer , * dst_len ,
451+ & out_len );
452+ memcpy (dst , buffer , out_len );
453+ free (buffer );
445454 if (status != PSA_SUCCESS ) {
446455 debug_print (srtp_mod_aes_icm , "encrypt error: %d" , status );
447456 psa_cipher_abort (& c -> ctx -> op );
448457 return srtp_err_status_cipher_fail ;
449458 }
459+
450460 * dst_len = out_len ;
451461 debug_print (srtp_mod_aes_icm , "encrypted: %s" ,
452462 srtp_octet_string_hex_string (dst , * dst_len ));
0 commit comments