From 6e60f2e16111ec24fab378d5b85ce80e4100a07c Mon Sep 17 00:00:00 2001 From: Kimmo Vaisanen Date: Thu, 30 Aug 2018 15:09:17 +0300 Subject: [PATCH] MAC commands wip --- features/lorawan/lorastack/mac/LoRaMac.cpp | 11 +++++++---- features/lorawan/lorastack/mac/LoRaMacCrypto.cpp | 11 ++++++----- features/lorawan/lorastack/mac/LoRaMacCrypto.h | 4 ++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/features/lorawan/lorastack/mac/LoRaMac.cpp b/features/lorawan/lorastack/mac/LoRaMac.cpp index 89526516cb..95f7ffca16 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.cpp +++ b/features/lorawan/lorastack/mac/LoRaMac.cpp @@ -396,14 +396,14 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload, // special handling of control port 0 if (port == 0) { if (fopts_len == 0) { - // sizeof nws_skey must be the same as _params.keys.nwk_skey, if (_lora_crypto.decrypt_payload(payload + payload_start_index, frame_len, - nwk_skey, - sizeof(_params.keys.nwk_skey) * 8, + _params.keys.nwk_senckey, + sizeof(_params.keys.nwk_senckey) * 8, address, DOWN_LINK, downlink_counter, + 1, // FMRPayload _params.rx_buffer) != 0) { _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; } @@ -443,6 +443,7 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload, address, DOWN_LINK, downlink_counter, + 1, // FMRPayload _params.rx_buffer) != 0) { _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; } else { @@ -465,6 +466,7 @@ bool LoRaMac::extract_mac_commands_only(const uint8_t *payload, _params.keys.nwk_senckey, sizeof(_params.keys.nwk_senckey) * 8, _params.dev_addr, DOWN_LINK, _params.dl_frame_counter, + 0, // FOpts field buffer)) { _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; return false; @@ -1860,7 +1862,6 @@ lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr, _params.ul_frame_counter, &_params.tx_buffer[pkt_header_len])) { status = LORAWAN_STATUS_CRYPTO_FAIL; - } pkt_header_len += mac_commands_len; } else { @@ -1892,11 +1893,13 @@ lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr, key = _params.keys.nwk_senckey; key_length = sizeof(_params.keys.nwk_senckey) * 8; } + if (0 != _lora_crypto.encrypt_payload((uint8_t *) payload, _params.tx_buffer_len, key, key_length, _params.dev_addr, UP_LINK, _params.ul_frame_counter, + 1, // FMRPayload &_params.tx_buffer[pkt_header_len])) { status = LORAWAN_STATUS_CRYPTO_FAIL; } diff --git a/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp b/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp index 3a7e915ad8..c7fa346364 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp +++ b/features/lorawan/lorastack/mac/LoRaMacCrypto.cpp @@ -129,11 +129,11 @@ exit: int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, const uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, + uint16_t a1_block_start, uint8_t *enc_buffer) { uint16_t i; uint8_t bufferIndex = 0; - uint16_t ctr = 1; int ret = 0; uint8_t a_block[16] = {}; uint8_t s_block[16] = {}; @@ -158,8 +158,8 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size, a_block[13] = (seq_counter >> 24) & 0xFF; while (size >= 16) { - a_block[15] = ((ctr) & 0xFF); - ctr++; + a_block[15] = ((a1_block_start) & 0xFF); + a1_block_start++; ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block, s_block); if (0 != ret) { @@ -174,7 +174,7 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size, } if (size > 0) { - a_block[15] = ((ctr) & 0xFF); + a_block[15] = ((a1_block_start) & 0xFF); ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block, s_block); if (0 != ret) { @@ -194,10 +194,11 @@ exit: int LoRaMacCrypto::decrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, + uint16_t a1_block_start, uint8_t *dec_buffer) { return encrypt_payload(buffer, size, key, key_length, address, dir, seq_counter, - dec_buffer); + a1_block_start, dec_buffer); } int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size, diff --git a/features/lorawan/lorastack/mac/LoRaMacCrypto.h b/features/lorawan/lorastack/mac/LoRaMacCrypto.h index 778722fcb7..38dfc5b0bf 100644 --- a/features/lorawan/lorastack/mac/LoRaMacCrypto.h +++ b/features/lorawan/lorastack/mac/LoRaMacCrypto.h @@ -79,6 +79,7 @@ public: * @param [in] address - Frame address * @param [in] dir - Frame direction [0: uplink, 1: downlink] * @param [in] seq_counter - Frame sequence counter + * @param [in] s1_block_start - 0 for FOpts field, 1 for FMRPayload * @param [out] enc_buffer - Encrypted buffer * * @return 0 if successful, or a cipher specific error code @@ -86,6 +87,7 @@ public: int encrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, + uint16_t a1_block_start, uint8_t *enc_buffer); /** @@ -98,6 +100,7 @@ public: * @param [in] address - Frame address * @param [in] dir - Frame direction [0: uplink, 1: downlink] * @param [in] seq_counter - Frame sequence counter + * @param [in] s1_block_start - 0 for FOpts field, 1 for FMRPayload * @param [out] dec_buffer - Decrypted buffer * * @return 0 if successful, or a cipher specific error code @@ -105,6 +108,7 @@ public: int decrypt_payload(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t key_length, uint32_t address, uint8_t dir, uint32_t seq_counter, + uint16_t a1_block_start, uint8_t *dec_buffer); /**