From 5ffd5aa174e5c57c5a1dc69ce381545a1ae7eaca Mon Sep 17 00:00:00 2001 From: Prasad Alatkar Date: Wed, 10 Feb 2021 16:35:58 +0530 Subject: [PATCH] NimBLE: Free the AES context after use by calling `mbetls_aes_free` * ESP32C3 allocates DMA channel when using HW AES accelerator. * Deallocate the DMA channel properly i.e. call `mbedtls_aes_free` after using HW AES encryption. Fixes BT-1435 --- nimble/host/mesh/src/glue.c | 3 +++ nimble/host/src/ble_sm_alg.c | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/nimble/host/mesh/src/glue.c b/nimble/host/mesh/src/glue.c index fadff0b66..7da73d15b 100644 --- a/nimble/host/mesh/src/glue.c +++ b/nimble/host/mesh/src/glue.c @@ -120,13 +120,16 @@ bt_encrypt_be(const uint8_t *key, const uint8_t *plaintext, uint8_t *enc_data) mbedtls_aes_init(&s); if (mbedtls_aes_setkey_enc(&s, key, 128) != 0) { + mbedtls_aes_free(&s); return BLE_HS_EUNKNOWN; } if (mbedtls_aes_crypt_ecb(&s, MBEDTLS_AES_ENCRYPT, plaintext, enc_data) != 0) { + mbedtls_aes_free(&s); return BLE_HS_EUNKNOWN; } + mbedtls_aes_free(&s); return 0; } diff --git a/nimble/host/src/ble_sm_alg.c b/nimble/host/src/ble_sm_alg.c index c39f5a6b2..ee8b937c5 100644 --- a/nimble/host/src/ble_sm_alg.c +++ b/nimble/host/src/ble_sm_alg.c @@ -88,14 +88,18 @@ ble_sm_alg_encrypt(const uint8_t *key, const uint8_t *plaintext, mbedtls_aes_init(&s); if (mbedtls_aes_setkey_enc(&s, tmp, 128) != 0) { + mbedtls_aes_free(&s); return BLE_HS_EUNKNOWN; } swap_buf(tmp, plaintext, 16); if (mbedtls_aes_crypt_ecb(&s, MBEDTLS_AES_ENCRYPT, tmp, enc_data) != 0) { + mbedtls_aes_free(&s); return BLE_HS_EUNKNOWN; } + + mbedtls_aes_free(&s); #else struct tc_aes_key_sched_struct s;