Skip to content

Commit 3357061

Browse files
mbedtls: improve debug output and fix AES-ICM issues
- aes_gcm: Add debug print when PSA status fails. - aes_icm: Destroy previous key before importing new key. - aes_icm: Add missing destination buffer argument to encrypt function. Signed-off-by: Sayed Naser Moravej <seyednasermoravej@gmail.com>
1 parent 260cc0b commit 3357061

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

crypto/cipher/aes_gcm_mbedtls.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,9 @@ static srtp_err_status_t srtp_aes_gcm_mbedtls_decrypt(void *cv,
455455
*dst_len = out_len;
456456
c->aad_size = 0;
457457
if (status != PSA_SUCCESS) {
458+
// debug_print(srtp_mod_aes_gcm, "mbedtls error code: %d", status);
458459
return srtp_err_status_auth_fail;
459460
}
461+
460462
return srtp_err_status_ok;
461463
}

crypto/cipher/aes_icm_mbedtls.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
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

Comments
 (0)