Skip to content

Commit a2192e0

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 a2192e0

2 files changed

Lines changed: 15 additions & 2 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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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",
@@ -440,13 +448,16 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
440448
return srtp_err_status_buffer_small;
441449
}
442450
status =
443-
psa_cipher_update(&(c->ctx->op), src, src_len, dst, *dst_len, &out_len);
444-
451+
psa_cipher_update(&(c->ctx->op), src, src_len, buffer, *dst_len,
452+
&out_len);
453+
memcpy(dst, buffer, out_len);
454+
free(buffer);
445455
if (status != PSA_SUCCESS) {
446456
debug_print(srtp_mod_aes_icm, "encrypt error: %d", status);
447457
psa_cipher_abort(&c->ctx->op);
448458
return srtp_err_status_cipher_fail;
449459
}
460+
450461
*dst_len = out_len;
451462
debug_print(srtp_mod_aes_icm, "encrypted: %s",
452463
srtp_octet_string_hex_string(dst, *dst_len));

0 commit comments

Comments
 (0)