MAC commands wip

feature-lorawan-1-1
Kimmo Vaisanen 2018-08-30 15:09:17 +03:00 committed by Antti Kauppila
parent 88781d067c
commit 6e60f2e161
3 changed files with 17 additions and 9 deletions

View File

@ -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;
}

View File

@ -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,

View File

@ -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);
/**