Implementing CR-FcntDwn-usage-in-FOpts-encryption

LoRaWAN specification 1.1.0 is incorrect in handling of FOpts encrytpion.
A CR was made by STMicroelectronics which correctly addresses the issue.
We have taken that CR and implemented in our code base.
feature-lorawan-1-1
Hasnain Virk 2018-10-19 17:35:54 +03:00 committed by Antti Kauppila
parent ed93d7e026
commit 475fe0b47f
6 changed files with 89 additions and 55 deletions

View File

@ -643,12 +643,6 @@ void LoRaWANStack::process_transmission(void)
{ {
tr_debug("Transmission completed"); tr_debug("Transmission completed");
if (_loramac.get_server_type() == LW1_1 && _device_mode_ind_ongoing) {
_device_mode_ind_ongoing = false;
_loramac.set_device_class(device_class_t(_new_class_type), mbed::callback(this, &LoRaWANStack::post_process_tx_no_reception));
send_event_to_application(CLASS_CHANGED);
}
make_tx_metadata_available(); make_tx_metadata_available();
if (_device_current_state == DEVICE_STATE_JOINING) { if (_device_current_state == DEVICE_STATE_JOINING) {
@ -663,6 +657,12 @@ void LoRaWANStack::process_transmission(void)
} }
_loramac.on_radio_tx_done(_tx_timestamp); _loramac.on_radio_tx_done(_tx_timestamp);
if (_loramac.get_server_type() == LW1_1 && _device_mode_ind_ongoing == true) {
_device_mode_ind_ongoing = false;
_loramac.set_device_class(device_class_t(_new_class_type), mbed::callback(this, &LoRaWANStack::post_process_tx_no_reception));
send_event_to_application(CLASS_CHANGED);
}
} }
void LoRaWANStack::post_process_tx_with_reception() void LoRaWANStack::post_process_tx_with_reception()
@ -779,7 +779,7 @@ void LoRaWANStack::process_reception(const uint8_t *const payload, uint16_t size
bool joined = _loramac.nwk_joined(); bool joined = _loramac.nwk_joined();
_loramac.on_radio_rx_done(payload, size, rssi, snr, callback(this, &LoRaWANStack::mlme_confirm_handler)); _loramac.on_radio_rx_done(payload, size, rssi, snr, mbed::callback(this, &LoRaWANStack::mlme_confirm_handler));
if (_loramac.get_mlme_confirmation()->pending) { if (_loramac.get_mlme_confirmation()->pending) {
_loramac.post_process_mlme_request(); _loramac.post_process_mlme_request();
@ -865,6 +865,10 @@ void LoRaWANStack::process_reception_timeout(bool is_timeout)
* never occurs. * never occurs.
*/ */
if (slot == RX_SLOT_WIN_2) { if (slot == RX_SLOT_WIN_2) {
<<<<<<< HEAD
=======
>>>>>>> c998cc4... Implementing CR-FcntDwn-usage-in-FOpts-encryption
post_process_tx_no_reception(); post_process_tx_no_reception();
_loramac.post_process_mcps_req(); _loramac.post_process_mcps_req();

View File

@ -67,6 +67,7 @@ using namespace mbed;
*/ */
#define DOWN_LINK 1 #define DOWN_LINK 1
#define MHDR_LEN 1
#define PORT_FIELD_LEN 1 #define PORT_FIELD_LEN 1
#define FHDR_LEN_WITHOUT_FOPTS 7 #define FHDR_LEN_WITHOUT_FOPTS 7
@ -244,6 +245,7 @@ loramac_event_info_status_t LoRaMac::handle_join_accept_frame(const uint8_t *pay
_params.rx_buffer[9] = _params.dev_nonce & 0xFF; // DevNonce _params.rx_buffer[9] = _params.dev_nonce & 0xFF; // DevNonce
_params.rx_buffer[10] = (_params.dev_nonce >> 8) & 0xFF; _params.rx_buffer[10] = (_params.dev_nonce >> 8) & 0xFF;
// MIC is encrypted as part of payload
mic_start = size + 11 - LORAMAC_MFR_LEN; mic_start = size + 11 - LORAMAC_MFR_LEN;
payload_start += 11; payload_start += 11;
@ -283,13 +285,6 @@ loramac_event_info_status_t LoRaMac::handle_join_accept_frame(const uint8_t *pay
return LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; return LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL;
} }
tr_debug("%s", trace_array(_params.keys.nwk_skey, 16));
tr_debug("%s", trace_array(_params.keys.app_skey, 16));
tr_debug("%s", trace_array(_params.keys.snwk_sintkey, 16));
tr_debug("%s", trace_array(_params.keys.nwk_senckey, 16));
tr_debug("%s", trace_array(decrypt_key, 16));
_params.net_id = (uint32_t) _params.rx_buffer[payload_start + 3]; _params.net_id = (uint32_t) _params.rx_buffer[payload_start + 3];
_params.net_id |= ((uint32_t) _params.rx_buffer[payload_start + 4] << 8); _params.net_id |= ((uint32_t) _params.rx_buffer[payload_start + 4] << 8);
_params.net_id |= ((uint32_t) _params.rx_buffer[payload_start + 5] << 16); _params.net_id |= ((uint32_t) _params.rx_buffer[payload_start + 5] << 16);
@ -393,6 +388,7 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload,
uint8_t *app_skey, uint8_t *app_skey,
uint32_t address, uint32_t address,
uint32_t downlink_counter, uint32_t downlink_counter,
seq_counter_type_t cnt_type,
int16_t rssi, int16_t rssi,
int8_t snr, int8_t snr,
Callback<void(loramac_mlme_confirm_t&)> confirm_handler) Callback<void(loramac_mlme_confirm_t&)> confirm_handler)
@ -414,8 +410,9 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload,
address, address,
DOWN_LINK, DOWN_LINK,
downlink_counter, downlink_counter,
1, // FMRPayload cnt_type,
_params.rx_buffer) != 0) { FRMPAYLOAD,
_params.rx_buffer, _params.server_type) != 0) {
_mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL;
} }
@ -442,7 +439,7 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload,
return; return;
} }
if(!extract_mac_commands_only(payload, snr, fopts_len, confirm_handler)) { if(!extract_mac_commands_only(payload, size, snr, fopts_len, confirm_handler)) {
return; return;
} }
@ -454,8 +451,10 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload,
address, address,
DOWN_LINK, DOWN_LINK,
downlink_counter, downlink_counter,
1, // FMRPayload cnt_type,
_params.rx_buffer) != 0) { FRMPAYLOAD,
_params.rx_buffer,
_params.server_type) != 0) {
_mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL;
} else { } else {
_mcps_indication.buffer = _params.rx_buffer; _mcps_indication.buffer = _params.rx_buffer;
@ -465,30 +464,29 @@ void LoRaMac::extract_data_and_mac_commands(const uint8_t *payload,
} }
bool LoRaMac::extract_mac_commands_only(const uint8_t *payload, bool LoRaMac::extract_mac_commands_only(const uint8_t *payload,
uint16_t size,
int8_t snr, int8_t snr,
uint8_t fopts_len, uint8_t fopts_len,
Callback<void(loramac_mlme_confirm_t&)> confirm_handler) Callback<void(loramac_mlme_confirm_t&)> confirm_handler)
{ {
if (fopts_len > 0) { if (fopts_len > 0) {
uint8_t buffer[15]; uint8_t buffer[15] = {0};
unsigned pld_idx = _dl_fport_available ? unsigned pld_idx = MHDR_LEN + FHDR_LEN_WITHOUT_FOPTS;
FHDR_LEN_WITHOUT_FOPTS + PORT_FIELD_LEN + fopts_len :
FHDR_LEN_WITHOUT_FOPTS + fopts_len;
if (_params.server_type == LW1_1) { if (_params.server_type == LW1_1) {
if (0 != _lora_crypto.decrypt_payload(payload + pld_idx, fopts_len, if (0 != _lora_crypto.decrypt_payload(payload + pld_idx, fopts_len,
_params.keys.nwk_senckey, sizeof(_params.keys.nwk_senckey) * 8, _params.keys.nwk_senckey, sizeof(_params.keys.nwk_senckey) * 8,
_params.dev_addr, DOWN_LINK, _params.dev_addr, DOWN_LINK,
_params.dl_frame_counter, _params.dl_frame_counter,
0, // FOpts field NFCNT_DOWN,
buffer)) { FOPTS,
buffer,
_params.server_type)) {
_mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL; _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_CRYPTO_FAIL;
return false; return false;
} }
tr_debug("%s", trace_array(buffer, 15));
tr_debug("%s", buffer);
} else { } else {
memcpy(buffer, payload + pld_idx, fopts_len); memcpy(buffer, payload + pld_idx, fopts_len);
} }
@ -524,7 +522,7 @@ void LoRaMac::handle_data_frame(const uint8_t *const payload,
multicast_params_t *cur_multicast_params; multicast_params_t *cur_multicast_params;
uint32_t address = 0; uint32_t address = 0;
uint32_t downlink_counter = 0; uint32_t downlink_counter = 0;
uint8_t app_payload_start_index = 0; seq_counter_type_t cnt_type;
uint8_t *nwk_skey = _params.keys.nwk_skey; uint8_t *nwk_skey = _params.keys.nwk_skey;
uint8_t *mic_key = _params.keys.nwk_skey; uint8_t *mic_key = _params.keys.nwk_skey;
uint8_t *app_skey = _params.keys.app_skey; uint8_t *app_skey = _params.keys.app_skey;
@ -541,11 +539,9 @@ void LoRaMac::handle_data_frame(const uint8_t *const payload,
fctrl.value = payload[ptr_pos++]; fctrl.value = payload[ptr_pos++];
int check_frm_len = size - (fctrl.bits.fopts_len + LORA_MAC_FRMPAYLOAD_OVERHEAD); int check_frm_len = size - (fctrl.bits.fopts_len + LORA_MAC_FRMPAYLOAD_OVERHEAD);
if (check_frm_len < 0) { if (check_frm_len < 0) {
tr_debug("Port field was excluded in DOWNLINK");
_dl_fport_available = false; _dl_fport_available = false;
} }
@ -584,10 +580,6 @@ void LoRaMac::handle_data_frame(const uint8_t *const payload,
downlink_counter = _params.dl_frame_counter; downlink_counter = _params.dl_frame_counter;
} }
app_payload_start_index = _dl_fport_available ?
FHDR_LEN_WITHOUT_FOPTS + PORT_FIELD_LEN + fctrl.bits.fopts_len :
FHDR_LEN_WITHOUT_FOPTS + fctrl.bits.fopts_len;
if (_params.server_type == LW1_1) { if (_params.server_type == LW1_1) {
if (_params.is_node_ack_requested && fctrl.bits.ack) { if (_params.is_node_ack_requested && fctrl.bits.ack) {
confFCnt = _mcps_confirmation.ul_frame_counter; confFCnt = _mcps_confirmation.ul_frame_counter;
@ -595,8 +587,10 @@ void LoRaMac::handle_data_frame(const uint8_t *const payload,
if (!is_multicast) { if (!is_multicast) {
if (_dl_fport_available && fport != 0) { if (_dl_fport_available && fport != 0) {
downlink_counter = _params.app_dl_frame_counter; downlink_counter = _params.app_dl_frame_counter;
cnt_type = AFCNT_DOWN;
} else { } else {
nwk_skey = _params.keys.nwk_senckey; nwk_skey = _params.keys.nwk_senckey;
cnt_type = NFCNT_DOWN;
} }
} }
} }
@ -700,10 +694,10 @@ void LoRaMac::handle_data_frame(const uint8_t *const payload,
if (check_frm_len > 0) { if (check_frm_len > 0) {
extract_data_and_mac_commands(payload, size, fctrl.bits.fopts_len, extract_data_and_mac_commands(payload, size, fctrl.bits.fopts_len,
nwk_skey, app_skey, address, nwk_skey, app_skey, address,
downlink_counter, rssi, snr, downlink_counter, cnt_type, rssi, snr,
confirm_handler); confirm_handler);
} else { } else {
extract_mac_commands_only(payload, snr, fctrl.bits.fopts_len, confirm_handler); extract_mac_commands_only(payload, size, snr, fctrl.bits.fopts_len, confirm_handler);
} }
// Handle proprietary messages. // Handle proprietary messages.
@ -1909,8 +1903,10 @@ lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr,
sizeof(_params.keys.nwk_senckey) * 8, sizeof(_params.keys.nwk_senckey) * 8,
_params.dev_addr, UP_LINK, _params.dev_addr, UP_LINK,
_params.ul_frame_counter, _params.ul_frame_counter,
0, //FOpts FCNT_UP,
&_params.tx_buffer[pkt_header_len])) { FOPTS,
&_params.tx_buffer[pkt_header_len],
_params.server_type)) {
status = LORAWAN_STATUS_CRYPTO_FAIL; status = LORAWAN_STATUS_CRYPTO_FAIL;
} }
pkt_header_len += mac_commands_len; pkt_header_len += mac_commands_len;
@ -1949,8 +1945,10 @@ lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr,
key, key_length, key, key_length,
_params.dev_addr, UP_LINK, _params.dev_addr, UP_LINK,
_params.ul_frame_counter, _params.ul_frame_counter,
1, // FMRPayload FCNT_UP,
&_params.tx_buffer[pkt_header_len])) { FRMPAYLOAD,
&_params.tx_buffer[pkt_header_len],
_params.server_type)) {
status = LORAWAN_STATUS_CRYPTO_FAIL; status = LORAWAN_STATUS_CRYPTO_FAIL;
} }
} }

View File

@ -554,6 +554,7 @@ private:
uint8_t fopts_len, uint8_t *nwk_skey, uint8_t fopts_len, uint8_t *nwk_skey,
uint8_t *app_skey, uint32_t address, uint8_t *app_skey, uint32_t address,
uint32_t downlink_frame_counter, uint32_t downlink_frame_counter,
seq_counter_type_t cnt_type,
int16_t rssi, int8_t snr, int16_t rssi, int8_t snr,
mbed::Callback<void(loramac_mlme_confirm_t&)> confirm_handler); mbed::Callback<void(loramac_mlme_confirm_t&)> confirm_handler);
/** /**
@ -561,7 +562,7 @@ private:
* payload if there is no data * payload if there is no data
* @return True if successful, false otherwise * @return True if successful, false otherwise
*/ */
bool extract_mac_commands_only(const uint8_t *payload, int8_t snr, uint8_t fopts_len, bool extract_mac_commands_only(const uint8_t *payload, uint16_t size, int8_t snr, uint8_t fopts_len,
mbed::Callback<void(loramac_mlme_confirm_t&)> confirm_handler); mbed::Callback<void(loramac_mlme_confirm_t&)> confirm_handler);
/** /**

View File

@ -128,13 +128,15 @@ exit:
int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size, int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
const uint8_t *key, const uint32_t key_length, const uint8_t *key, const uint32_t key_length,
uint32_t address, uint8_t dir, uint32_t seq_counter, uint32_t address, uint8_t dir, uint32_t seq_counter,
uint16_t a1_block_start, seq_counter_type_t seq_cnt_type,
uint8_t *enc_buffer) payload_type_t pld_type,
uint8_t *enc_buffer,
server_type_t serv_type)
{ {
uint16_t i; uint16_t i;
uint8_t bufferIndex = 0; uint8_t bufferIndex = 0;
int ret = 0; int ret = 0;
uint8_t a_block[16] = {}; uint8_t a_block[16] = {0};
uint8_t s_block[16] = {}; uint8_t s_block[16] = {};
mbedtls_aes_init(&aes_ctx); mbedtls_aes_init(&aes_ctx);
@ -144,6 +146,17 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
} }
a_block[0] = 0x01; a_block[0] = 0x01;
if (serv_type == LW1_1 && pld_type == FOPTS) {
if ((seq_cnt_type == FCNT_UP || seq_cnt_type == NFCNT_DOWN)) {
a_block[4] = 0x01;
} else if (seq_cnt_type == AFCNT_DOWN) {
a_block[4] = 0x02;
}
}
a_block[5] = dir; a_block[5] = dir;
a_block[6] = (address) & 0xFF; a_block[6] = (address) & 0xFF;
@ -156,9 +169,11 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
a_block[12] = (seq_counter >> 16) & 0xFF; a_block[12] = (seq_counter >> 16) & 0xFF;
a_block[13] = (seq_counter >> 24) & 0xFF; a_block[13] = (seq_counter >> 24) & 0xFF;
a_block[15] = 0x01;
while (size >= 16) { while (size >= 16) {
a_block[15] = ((a1_block_start) & 0xFF); a_block[15]++;
a1_block_start++;
ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block, ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block,
s_block); s_block);
if (0 != ret) { if (0 != ret) {
@ -173,7 +188,6 @@ int LoRaMacCrypto::encrypt_payload(const uint8_t *buffer, uint16_t size,
} }
if (size > 0) { if (size > 0) {
a_block[15] = ((a1_block_start) & 0xFF);
ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block, ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, a_block,
s_block); s_block);
if (0 != ret) { if (0 != ret) {
@ -193,11 +207,13 @@ exit:
int LoRaMacCrypto::decrypt_payload(const uint8_t *buffer, uint16_t size, int LoRaMacCrypto::decrypt_payload(const uint8_t *buffer, uint16_t size,
const uint8_t *key, uint32_t key_length, const uint8_t *key, uint32_t key_length,
uint32_t address, uint8_t dir, uint32_t seq_counter, uint32_t address, uint8_t dir, uint32_t seq_counter,
uint16_t a1_block_start, seq_counter_type_t seq_cnt_type,
uint8_t *dec_buffer) payload_type_t pld_type,
uint8_t *dec_buffer,
server_type_t serv_type)
{ {
return encrypt_payload(buffer, size, key, key_length, address, dir, seq_counter, return encrypt_payload(buffer, size, key, key_length, address, dir, seq_counter,
a1_block_start, dec_buffer); seq_cnt_type, pld_type, dec_buffer, serv_type);
} }
int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size, int LoRaMacCrypto::compute_join_frame_mic(const uint8_t *buffer, uint16_t size,

View File

@ -87,8 +87,10 @@ public:
int encrypt_payload(const uint8_t *buffer, uint16_t size, int encrypt_payload(const uint8_t *buffer, uint16_t size,
const uint8_t *key, uint32_t key_length, const uint8_t *key, uint32_t key_length,
uint32_t address, uint8_t dir, uint32_t seq_counter, uint32_t address, uint8_t dir, uint32_t seq_counter,
uint16_t a1_block_start, seq_counter_type_t seq_cnt_type,
uint8_t *enc_buffer); payload_type_t pld_type,
uint8_t *enc_buffer,
server_type_t serv_type);
/** /**
* Performs payload decryption * Performs payload decryption
@ -108,8 +110,10 @@ public:
int decrypt_payload(const uint8_t *buffer, uint16_t size, int decrypt_payload(const uint8_t *buffer, uint16_t size,
const uint8_t *key, uint32_t key_length, const uint8_t *key, uint32_t key_length,
uint32_t address, uint8_t dir, uint32_t seq_counter, uint32_t address, uint8_t dir, uint32_t seq_counter,
uint16_t a1_block_start, seq_counter_type_t seq_cnt_type,
uint8_t *dec_buffer); payload_type_t pld_type,
uint8_t *dec_buffer,
server_type_t serv_type);
/** /**
* Computes the LoRaMAC Join Request frame MIC field * Computes the LoRaMAC Join Request frame MIC field

View File

@ -161,10 +161,21 @@ typedef enum {
} join_req_type_t; } join_req_type_t;
typedef enum { typedef enum {
LW1_0_2, LW1_0_2 = 0,
LW1_1 LW1_1
} server_type_t; } server_type_t;
typedef enum {
FCNT_UP = 0,
NFCNT_DOWN,
AFCNT_DOWN
} seq_counter_type_t;
typedef enum {
FRMPAYLOAD = 0,
FOPTS
} payload_type_t;
/*! /*!
* The global MAC layer parameters. * The global MAC layer parameters.
*/ */