Skip to content

Commit 1791e08

Browse files
mbedtls files changed and cmake instruction added to support MbedTLS V4.0
Signed-off-by: Sayed Naser Moravej <seyednasermoravej@gmail.com> mbedtls and error code removed from aes_gcm_mbedtls.c aes_icm_mbedtls done aes_icm_ext.h done hmac also done
1 parent 9964b61 commit 1791e08

File tree

6 files changed

+241
-84
lines changed

6 files changed

+241
-84
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ make:
290290
make
291291
~~~
292292

293+
Assuming the mbedtls V4.0 is installed in `mbedtls-4` directory.
294+
~~~.txt
295+
cmake -S . -B build -DCRYPTO_LIBRARY=mbedtls -DMBEDTLS_INCLUDE_DIRS=/opt/mbedtls-4/include -DMBEDTLS_LIBRARY="/opt/mbedtls-4/lib/libmbedtls.a;/opt/mbedtls-4/lib/libmbedcrypto.a" && cmake --build build
296+
~~~
297+
298+
293299
The configure script accepts the following options:
294300

295301
Option | Description

crypto/cipher/aes_gcm_mbedtls.c

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
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
}

crypto/cipher/aes_icm_mbedtls.c

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
#ifdef HAVE_CONFIG_H
4646
#include <config.h>
4747
#endif
48-
#include <mbedtls/aes.h>
48+
#include <psa/crypto_types.h>
49+
#include <psa/crypto.h>
50+
4951
#include "aes_icm_ext.h"
5052
#include "crypto_types.h"
5153
#include "err.h" /* for srtp_debug */
@@ -230,15 +232,17 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_alloc(srtp_cipher_t **c,
230232
}
231233

232234
icm->ctx =
233-
(mbedtls_aes_context *)srtp_crypto_alloc(sizeof(mbedtls_aes_context));
235+
(psa_aes_icm_ctx_t *)srtp_crypto_alloc(sizeof(psa_aes_icm_ctx_t));
236+
234237
if (icm->ctx == NULL) {
235238
srtp_crypto_free(icm);
236239
srtp_crypto_free(*c);
237240
*c = NULL;
238241
return srtp_err_status_alloc_fail;
239242
}
240243

241-
mbedtls_aes_init(icm->ctx);
244+
((icm->ctx))->key_id = PSA_KEY_ID_NULL;
245+
((icm->ctx)->op) = psa_cipher_operation_init();
242246

243247
/* set pointers */
244248
(*c)->state = icm;
@@ -284,7 +288,7 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_dealloc(srtp_cipher_t *c)
284288
*/
285289
ctx = (srtp_aes_icm_ctx_t *)c->state;
286290
if (ctx != NULL) {
287-
mbedtls_aes_free(ctx->ctx);
291+
psa_destroy_key(ctx->ctx->key_id);
288292
srtp_crypto_free(ctx->ctx);
289293
/* zeroize the key material */
290294
octet_string_set_to_zero(ctx, sizeof(srtp_aes_icm_ctx_t));
@@ -302,7 +306,8 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
302306
{
303307
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
304308
uint32_t key_size_in_bits = (c->key_size << 3);
305-
int errcode = 0;
309+
310+
psa_status_t status = psa_crypto_init();
306311

307312
/*
308313
* set counter and initial values to 'offset' value, being careful not to
@@ -330,9 +335,25 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_context_init(void *cv,
330335
break;
331336
}
332337

333-
errcode = mbedtls_aes_setkey_enc(c->ctx, key, key_size_in_bits);
334-
if (errcode != 0) {
335-
debug_print(srtp_mod_aes_icm, "errCode: %d", errcode);
338+
/* Set key attributes */
339+
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
340+
341+
psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
342+
psa_set_key_bits(&attr, key_size_in_bits);
343+
psa_set_key_usage_flags(&attr,
344+
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
345+
psa_set_key_algorithm(&attr, PSA_ALG_CTR);
346+
347+
if (c->ctx->key_id != PSA_KEY_ID_NULL) {
348+
psa_destroy_key(c->ctx->key_id);
349+
c->ctx->key_id = PSA_KEY_ID_NULL;
350+
}
351+
352+
status =
353+
psa_import_key(&attr, key, key_size_in_bits / 8, &(c->ctx->key_id));
354+
355+
if (status != PSA_SUCCESS) {
356+
debug_print(srtp_mod_aes_icm, "status: %d", status);
336357
}
337358

338359
return srtp_err_status_ok;
@@ -355,6 +376,8 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
355376
/* set nonce (for alignment) */
356377
v128_copy_octet_string(&nonce, iv);
357378

379+
psa_cipher_set_iv(&(c->ctx->op), iv, 16);
380+
358381
debug_print(srtp_mod_aes_icm, "setting iv: %s", v128_hex_string(&nonce));
359382

360383
v128_xor(&c->counter, &c->offset, &nonce);
@@ -369,7 +392,7 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_set_iv(
369392
* This function encrypts a buffer using AES CTR mode
370393
*
371394
* Parameters:
372-
* c Crypto context
395+
* c Crypto contextsrtp_aes_icm_mbedtls_encrypt
373396
* buf data to encrypt
374397
* enc_len length of encrypt buffer
375398
*/
@@ -381,22 +404,35 @@ static srtp_err_status_t srtp_aes_icm_mbedtls_encrypt(void *cv,
381404
{
382405
srtp_aes_icm_ctx_t *c = (srtp_aes_icm_ctx_t *)cv;
383406

384-
int errCode = 0;
407+
psa_status_t status = PSA_SUCCESS;
408+
size_t out_len = 0;
409+
size_t total_len = 0;
410+
385411
debug_print(srtp_mod_aes_icm, "rs0: %s", v128_hex_string(&c->counter));
412+
debug_print(srtp_mod_aes_icm, "source: %s",
413+
srtp_octet_string_hex_string(src, src_len));
386414

387415
if (*dst_len < src_len) {
388416
return srtp_err_status_buffer_small;
389417
}
390418

391-
errCode =
392-
mbedtls_aes_crypt_ctr(c->ctx, src_len, &(c->nc_off), c->counter.v8,
393-
c->stream_block.v8, src, dst);
394-
if (errCode != 0) {
395-
debug_print(srtp_mod_aes_icm, "encrypt error: %d", errCode);
419+
status =
420+
psa_cipher_encrypt_setup(&(c->ctx->op), c->ctx->key_id, PSA_ALG_CTR);
421+
status = psa_cipher_set_iv(&c->ctx->op, c->counter.v8, 16);
422+
status =
423+
psa_cipher_update(&(c->ctx->op), src, src_len, dst, *dst_len, &out_len);
424+
425+
total_len = out_len;
426+
status = psa_cipher_finish(&(c->ctx->op), dst + total_len,
427+
*dst_len - total_len, &out_len);
428+
if (status != PSA_SUCCESS) {
429+
debug_print(srtp_mod_aes_icm, "encrypt error: %d", status);
396430
return srtp_err_status_cipher_fail;
397431
}
398-
399-
*dst_len = src_len;
432+
total_len += out_len;
433+
*dst_len = total_len;
434+
debug_print(srtp_mod_aes_icm, "encrypted: %s",
435+
srtp_octet_string_hex_string(dst, *dst_len));
400436

401437
return srtp_err_status_ok;
402-
}
438+
}

0 commit comments

Comments
 (0)