From 3bb185daad2296c02d4efd13760b4b884d70d7e4 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Thu, 4 Aug 2022 11:47:32 +0200 Subject: [PATCH] nimble/phy/nrf: Wait for CCM to finish decryption In extremely rare cases seems like ENDCRYPT flag is not set before we check for MISTATUS, so just spin a bit in that case and wait for CCM to finish. --- nimble/drivers/nrf52/src/ble_phy.c | 14 ++++---------- nimble/drivers/nrf5340/src/ble_phy.c | 14 ++++---------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/nimble/drivers/nrf52/src/ble_phy.c b/nimble/drivers/nrf52/src/ble_phy.c index 9b4412388..c06536a9a 100644 --- a/nimble/drivers/nrf52/src/ble_phy.c +++ b/nimble/drivers/nrf52/src/ble_phy.c @@ -1100,6 +1100,10 @@ ble_phy_rx_end_isr(void) ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK; #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) if (g_ble_phy_data.phy_encrypted) { + while (NRF_CCM->EVENTS_ENDCRYPT == 0) { + /* Make sure CCM finished */ + }; + /* Only set MIC failure flag if frame is not zero length */ if ((dptr[1] != 0) && (NRF_CCM->MICSTATUS == 0)) { ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_MIC_FAILURE; @@ -1115,16 +1119,6 @@ ble_phy_rx_end_isr(void) STATS_INC(ble_phy_stats, rx_hw_err); ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK; } - - /* - * XXX: This is a total hack work-around for now but I dont - * know what else to do. If ENDCRYPT is not set and we are - * encrypted we need to not trust this frame and drop it. - */ - if (NRF_CCM->EVENTS_ENDCRYPT == 0) { - STATS_INC(ble_phy_stats, rx_hw_err); - ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK; - } } #endif } diff --git a/nimble/drivers/nrf5340/src/ble_phy.c b/nimble/drivers/nrf5340/src/ble_phy.c index 060e32eef..51b80a561 100644 --- a/nimble/drivers/nrf5340/src/ble_phy.c +++ b/nimble/drivers/nrf5340/src/ble_phy.c @@ -958,6 +958,10 @@ ble_phy_rx_end_isr(void) ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK; #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) if (g_ble_phy_data.phy_encrypted) { + while (NRF_CCM_NS->EVENTS_ENDCRYPT == 0) { + /* Make sure CCM finished */ + }; + /* Only set MIC failure flag if frame is not zero length */ if ((dptr[1] != 0) && (NRF_CCM_NS->MICSTATUS == 0)) { ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_MIC_FAILURE; @@ -973,16 +977,6 @@ ble_phy_rx_end_isr(void) STATS_INC(ble_phy_stats, rx_hw_err); ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK; } - - /* - * XXX: This is a total hack work-around for now but I dont - * know what else to do. If ENDCRYPT is not set and we are - * encrypted we need to not trust this frame and drop it. - */ - if (NRF_CCM_NS->EVENTS_ENDCRYPT == 0) { - STATS_INC(ble_phy_stats, rx_hw_err); - ble_hdr->rxinfo.flags &= ~BLE_MBUF_HDR_F_CRC_OK; - } } #endif }