4646#ifdef HAVE_CONFIG_H
4747#include <config.h>
4848#endif
49- #include <mbedtls/gcm.h>
49+
50+ #include <psa/crypto.h>
5051#include "aes_gcm.h"
5152#include "alloc.h"
5253#include "err.h" /* for srtp_debug */
@@ -198,7 +199,6 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_alloc(srtp_cipher_t **c,
198199 if (tlen != GCM_AUTH_TAG_LEN && tlen != GCM_AUTH_TAG_LEN_8 ) {
199200 return (srtp_err_status_bad_param );
200201 }
201-
202202 /* allocate memory a cipher of type aes_gcm */
203203 * c = (srtp_cipher_t * )srtp_crypto_alloc (sizeof (srtp_cipher_t ));
204204 if (* c == NULL ) {
@@ -212,15 +212,15 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_alloc(srtp_cipher_t **c,
212212 return (srtp_err_status_alloc_fail );
213213 }
214214
215- gcm -> ctx =
216- ( mbedtls_gcm_context * ) srtp_crypto_alloc ( sizeof ( mbedtls_gcm_context ));
215+ gcm -> ctx = ( psa_gcm_context * ) srtp_crypto_alloc ( sizeof ( psa_gcm_context ));
216+
217217 if (gcm -> ctx == NULL ) {
218218 srtp_crypto_free (gcm );
219219 srtp_crypto_free (* c );
220220 * c = NULL ;
221221 return srtp_err_status_alloc_fail ;
222222 }
223- mbedtls_gcm_init ( gcm -> ctx );
223+ gcm -> ctx -> op = psa_aead_operation_init ( );
224224
225225 /* set pointers */
226226 (* c )-> state = gcm ;
@@ -256,7 +256,7 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_dealloc(srtp_cipher_t *c)
256256 FUNC_ENTRY ();
257257 ctx = (srtp_aes_gcm_ctx_t * )c -> state ;
258258 if (ctx ) {
259- mbedtls_gcm_free (ctx -> ctx );
259+ psa_destroy_key (ctx -> ctx -> key_id );
260260 srtp_crypto_free (ctx -> ctx );
261261 /* zeroize the key material */
262262 octet_string_set_to_zero (ctx , sizeof (srtp_aes_gcm_ctx_t ));
@@ -275,7 +275,7 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_context_init(void *cv,
275275 FUNC_ENTRY ();
276276 srtp_aes_gcm_ctx_t * c = (srtp_aes_gcm_ctx_t * )cv ;
277277 uint32_t key_len_in_bits ;
278- int errCode = 0 ;
278+ psa_status_t status = PSA_SUCCESS ;
279279 c -> dir = srtp_direction_any ;
280280 c -> aad_size = 0 ;
281281
@@ -291,10 +291,19 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_context_init(void *cv,
291291 break ;
292292 }
293293
294- errCode =
295- mbedtls_gcm_setkey (c -> ctx , MBEDTLS_CIPHER_ID_AES , key , key_len_in_bits );
296- if (errCode != 0 ) {
297- debug_print (srtp_mod_aes_gcm , "mbedtls error code: %d" , errCode );
294+ psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT ;
295+
296+ psa_set_key_usage_flags (& attr ,
297+ PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
298+ psa_set_key_algorithm (
299+ & attr , PSA_ALG_AEAD_WITH_SHORTENED_TAG (PSA_ALG_GCM , c -> tag_len ));
300+ psa_set_key_type (& attr , PSA_KEY_TYPE_AES );
301+ psa_set_key_bits (& attr , key_len_in_bits );
302+
303+ status = psa_import_key (& attr , key , key_len_in_bits / 8 , & c -> ctx -> key_id );
304+
305+ if (status != PSA_SUCCESS ) {
306+ debug_print (srtp_mod_aes_gcm , "mbedtls error code: %d" , status );
298307 return srtp_err_status_init_fail ;
299308 }
300309
@@ -366,7 +375,8 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
366375{
367376 FUNC_ENTRY ();
368377 srtp_aes_gcm_ctx_t * c = (srtp_aes_gcm_ctx_t * )cv ;
369- int errCode = 0 ;
378+
379+ psa_status_t status = PSA_SUCCESS ;
370380
371381 if (c -> dir != srtp_direction_encrypt ) {
372382 return srtp_err_status_bad_param ;
@@ -376,13 +386,16 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_encrypt(void *cv,
376386 return srtp_err_status_buffer_small ;
377387 }
378388
379- errCode = mbedtls_gcm_crypt_and_tag (c -> ctx , MBEDTLS_GCM_ENCRYPT , src_len ,
380- c -> iv , c -> iv_len , c -> aad , c -> aad_size ,
381- src , dst , c -> tag_len , dst + src_len );
389+ size_t out_len = 0 ;
390+ status = psa_aead_encrypt (
391+ c -> ctx -> key_id ,
392+ PSA_ALG_AEAD_WITH_SHORTENED_TAG (PSA_ALG_GCM , c -> tag_len ), c -> iv ,
393+ c -> iv_len , c -> aad , c -> aad_size , src , src_len , dst ,
394+ PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE (src_len ), & out_len );
382395
383396 c -> aad_size = 0 ;
384- if (errCode != 0 ) {
385- debug_print (srtp_mod_aes_gcm , "mbedtls error code: %d" , errCode );
397+ if (status != PSA_SUCCESS ) {
398+ debug_print (srtp_mod_aes_gcm , "mbedtls error code: %d" , status );
386399 return srtp_err_status_bad_param ;
387400 }
388401
@@ -407,7 +420,9 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
407420{
408421 FUNC_ENTRY ();
409422 srtp_aes_gcm_ctx_t * c = (srtp_aes_gcm_ctx_t * )cv ;
410- int errCode = 0 ;
423+
424+ psa_status_t status = PSA_SUCCESS ;
425+ uint8_t full_tag [16 ] = { 0 };
411426
412427 if (c -> dir != srtp_direction_decrypt ) {
413428 return srtp_err_status_bad_param ;
@@ -417,26 +432,27 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
417432 return srtp_err_status_bad_param ;
418433 }
419434
420- if (* dst_len < (src_len - c -> tag_len )) {
435+ size_t ciphertext_len = src_len - c -> tag_len ;
436+
437+ if (* dst_len < ciphertext_len ) {
421438 return srtp_err_status_buffer_small ;
422439 }
423440
441+ memcpy (full_tag , src + ciphertext_len , c -> tag_len );
442+
424443 debug_print (srtp_mod_aes_gcm , "AAD: %s" ,
425444 srtp_octet_string_hex_string (c -> aad , c -> aad_size ));
426445
427- errCode = mbedtls_gcm_auth_decrypt (
428- c -> ctx , (src_len - c -> tag_len ), c -> iv , c -> iv_len , c -> aad , c -> aad_size ,
429- src + (src_len - c -> tag_len ), c -> tag_len , src , dst );
446+ size_t out_len = 0 ;
447+ status = psa_aead_decrypt (
448+ c -> ctx -> key_id ,
449+ PSA_ALG_AEAD_WITH_SHORTENED_TAG (PSA_ALG_GCM , c -> tag_len ), c -> iv ,
450+ c -> iv_len , c -> aad , c -> aad_size , src , src_len , dst ,
451+ PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE (src_len ), & out_len );
452+ * dst_len = ciphertext_len ;
430453 c -> aad_size = 0 ;
431- if (errCode != 0 ) {
454+ if (status != PSA_SUCCESS ) {
432455 return srtp_err_status_auth_fail ;
433456 }
434-
435- /*
436- * Reduce the buffer size by the tag length since the tag
437- * is not part of the original payload
438- */
439- * dst_len = (src_len - c -> tag_len );
440-
441457 return srtp_err_status_ok ;
442458}
0 commit comments