From 0a72d98750326a66dd509cb2f93bbac4b8ab4219 Mon Sep 17 00:00:00 2001 From: jgromes Date: Mon, 14 Aug 2023 21:38:31 +0200 Subject: [PATCH] [LoRaWAN] Fixed MIC calculation for downlink packets --- src/protocols/LoRaWAN/LoRaWAN.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/protocols/LoRaWAN/LoRaWAN.cpp b/src/protocols/LoRaWAN/LoRaWAN.cpp index c2cd77d7..0c36377d 100644 --- a/src/protocols/LoRaWAN/LoRaWAN.cpp +++ b/src/protocols/LoRaWAN/LoRaWAN.cpp @@ -625,6 +625,12 @@ int16_t LoRaWANNode::downlink(uint8_t* data, size_t* len) { state = RADIOLIB_ERR_NONE; } + // get the frame counter and set it to the MIC calculation block + // TODO this will not handle overflow into 32-bits! + // TODO cache the ADR bit? + uint16_t fcnt = LoRaWANNode::ntoh(&downlinkMsg[RADIOLIB_LORAWAN_FHDR_FCNT_POS]); + LoRaWANNode::hton(&downlinkMsg[RADIOLIB_LORAWAN_BLOCK_FCNT_POS], fcnt); + //Module::hexdump(downlinkMsg, RADIOLIB_AES128_BLOCK_SIZE + downlinkMsgLen); if(state != RADIOLIB_ERR_NONE) { @@ -646,10 +652,6 @@ int16_t LoRaWANNode::downlink(uint8_t* data, size_t* len) { return(RADIOLIB_ERR_DOWNLINK_MALFORMED); } - // TODO cache the ADR bit? - // TODO cache and check fcnt? - uint16_t fcnt = LoRaWANNode::ntoh(&downlinkMsg[RADIOLIB_LORAWAN_FHDR_FCNT_POS]); - // check fopts len uint8_t foptsLen = downlinkMsg[RADIOLIB_LORAWAN_FHDR_FCTRL_POS] & RADIOLIB_LORAWAN_FHDR_FOPTS_LEN_MASK; if(foptsLen > 0) {