end node on synchronous star LoRa network.

Dependencies:   SX127x sx12xx_hal TSL2561

radio chip selection

Radio chip driver is not included, allowing choice of radio device.
If you're using SX1272 or SX1276, then import sx127x driver into your program.
if you're using SX1261 or SX1262, then import sx126x driver into your program.
if you're using SX1280, then import sx1280 driver into your program.
If you're using NAmote72 or Murata discovery, then you must import only sx127x driver.

This project for use with LoRaWAN_singlechannel_gateway project.

Alternately gateway running on raspberry pi can be used as gateway.

LoRaWAN on single radio channel

Network description is at gateway project page. Synchronous star network.

Hardware Support

This project supports SX1276 and SX1272, sx126x kit, sx126x shield, and sx128x 2.4GHz. The ST board B-L072Z-LRWAN1 is also supported (TypeABZ module). When B-L072Z-LRWAN1 target is selected, TARGET_DISCO_L072CZ_LRWAN1 is defined by tools, allowing correct radio driver configuration for this platform. Alternately, any mbed board that can use LoRa radio shield board should work, but NUCLEO boards are tested.

End-node Unique ID

DevEUI is created from CPU serial number. AppEUI and AppKey are declared as software constants.

End-node Configuration

Data rate definition LORAMAC_DEFAULT_DATARATE configured in LoRaMac-definitions.h. See gateway project page for configuration of gateway.
LoRaWAN addressing is configured in Comissioning.h; only OTA mode is functional.
Header file board/lora_config.h, selects application layer options (i.e. sensors) to be compiled in.

Serial Interface

Serial port operates at 115200bps.
Application layer single_us915_main.cpp User button triggers uplink (i.e. blue button on nucleo board), or jumper enables continuously sends repeated uplink packets. The MAC layer holds each uplink request until the allocated timeslot.

commandargumentsdescription
?-print available commands
. (period)-print status (DevEUI, DevAddr, etc)
ullength integerset payload length of test uplink packets

sensor demo

Selected grove sensors may be plugged into SX1272 shield.
To enable, edit lora_config.h to define SENSORS.

Sensor connections on SX1272MB2xAS:

D8 D9: buttonRX TX: (unused)A3 A4: Rotary Angle Sensor
D6 D7: RGB LEDSCL SDA: digital light sensorA1 A2: Rotary Angle Sensor

Digital input pin, state reported via uplink: PC8
Digital output pin, controlled via downlink: PC6
PWM out: PB_10

Jumper enables auto-repeated transmit: PC10 and PC12 on NUCLEO board, located on end of morpho headers nearby JP4.

Revision:
34:9c8966cd66a2
Parent:
29:ad409c68c0a6
--- a/mac/LoRaMacCrypto.cpp	Wed Dec 19 13:56:04 2018 -0800
+++ b/mac/LoRaMacCrypto.cpp	Fri Jul 10 11:12:59 2020 -0700
@@ -1,30 +1,11 @@
-/*
- / _____)             _              | |
-( (____  _____ ____ _| |_ _____  ____| |__
- \____ \| ___ |    (_   _) ___ |/ ___)  _ \
- _____) ) ____| | | || |_| ____( (___| | | |
-(______/|_____)_|_|_| \__)_____)\____)_| |_|
-    (C)2013 Semtech
- ___ _____ _   ___ _  _____ ___  ___  ___ ___
-/ __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
-\__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
-|___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
-embedded.connectivity.solutions===============
+/* */
 
-Description: LoRa MAC layer implementation
-
-License: Revised BSD License, see LICENSE.TXT file include in the project
-
-Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jäckle ( STACKFORCE )
-*/
 #include <stdlib.h>
 #include <stdint.h>
-#include <stdio.h>  // dbg remove me
 #include "utilities.h"
 
-// TODO: use mbed-os/features/mbedtls/inc/mbedtls/(aes|cmac).h
-#include "gladman_aes.h"
-#include "gladman_cmac.h"
+#include "cmac.h"
+#include "aes.h"
 
 #include "LoRaMacCrypto.h"
 
@@ -57,15 +38,7 @@
                             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
                           };
 
-/*!
- * AES computation context variable
- */
-static aes_context AesContext;
-
-/*!
- * CMAC computation context variable
- */
-static AES_CMAC_CTX AesCmacCtx[1];
+static mbedtls_cipher_context_t ctx;
 
 /*!
  * \brief Computes the LoRaMAC frame MIC field  
@@ -78,8 +51,9 @@
  * \param [IN]  sequenceCounter Frame sequence counter
  * \param [OUT] mic Computed MIC field
  */
-void LoRaMacComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint32_t *mic )
+int LoRaMacComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint32_t *mic )
 {
+    //tls_printf("LoRaMacComputeMic:\r\n");
     MicBlockB0[5] = dir;
     
     MicBlockB0[6] = ( address ) & 0xFF;
@@ -94,27 +68,27 @@
 
     MicBlockB0[15] = size & 0xFF;
 
-    AES_CMAC_Init( AesCmacCtx );
-
-    AES_CMAC_SetKey( AesCmacCtx, key );
-
-    AES_CMAC_Update( AesCmacCtx, MicBlockB0, LORAMAC_MIC_BLOCK_B0_SIZE );
-    
-    AES_CMAC_Update( AesCmacCtx, buffer, size & 0xFF );
-    
-    AES_CMAC_Final( Mic, AesCmacCtx );
-    
+    if (mbedtls_cipher_cmac_starts(&ctx, key, 128))
+        return -1;
+ 
+    mbedtls_cipher_cmac_update(&ctx, MicBlockB0, LORAMAC_MIC_BLOCK_B0_SIZE);
+ 
+    mbedtls_cipher_cmac_update(&ctx, buffer, size & 0xff);
+ 
+    mbedtls_cipher_cmac_finish(&ctx, Mic);
     *mic = ( uint32_t )( ( uint32_t )Mic[3] << 24 | ( uint32_t )Mic[2] << 16 | ( uint32_t )Mic[1] << 8 | ( uint32_t )Mic[0] );
+    return 0;
 }
 
 void LoRaMacPayloadEncrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *encBuffer )
 {
+    mbedtls_aes_context actx;
     uint16_t i;
     uint8_t bufferIndex = 0;
     uint16_t ctr = 1;
 
-    memset1( AesContext.ksch, '\0', 240 );
-    aes_set_key( key, 16, &AesContext );
+    mbedtls_aes_init(&actx);
+    mbedtls_aes_setkey_enc(&actx, key, 128);
 
     aBlock[5] = dir;
 
@@ -132,7 +106,7 @@
     {
         aBlock[15] = ( ( ctr ) & 0xFF );
         ctr++;
-        aes_encrypt( aBlock, sBlock, &AesContext );
+        mbedtls_aes_encrypt(&actx, aBlock, sBlock);
         for( i = 0; i < 16; i++ )
         {
             encBuffer[bufferIndex + i] = buffer[bufferIndex + i] ^ sBlock[i];
@@ -141,76 +115,106 @@
         bufferIndex += 16;
     }
 
-    if( size > 0 )
+    if (size > 0)
     {
         aBlock[15] = ( ( ctr ) & 0xFF );
-        aes_encrypt( aBlock, sBlock, &AesContext );
+        mbedtls_aes_encrypt(&actx, aBlock, sBlock);
         for( i = 0; i < size; i++ )
         {
             encBuffer[bufferIndex + i] = buffer[bufferIndex + i] ^ sBlock[i];
         }
     }
+
+    mbedtls_aes_free(&actx);
 }
 
-void LoRaMacPayloadDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *decBuffer )
+void LoRaMacPayloadDecrypt(
+        const uint8_t *buffer,
+        uint16_t size,
+        const uint8_t *key,
+        uint32_t address,
+        uint8_t dir,
+        uint32_t sequenceCounter,
+        uint8_t *decBuffer )
 {
     LoRaMacPayloadEncrypt( buffer, size, key, address, dir, sequenceCounter, decBuffer );
 }
 
-void LoRaMacJoinComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t *mic )
+int LoRaMacJoinComputeMic( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t *mic )
 {
-    AES_CMAC_Init( AesCmacCtx );
-
-    AES_CMAC_SetKey( AesCmacCtx, key );
-
-    AES_CMAC_Update( AesCmacCtx, buffer, size & 0xFF );
-
-    AES_CMAC_Final( Mic, AesCmacCtx );
-
+    int ret;
+    uint8_t Mic[16];
+    ret = mbedtls_cipher_cmac_starts(&ctx, key, 128);
+    if (ret < 0)
+        return ret;
+    ret = mbedtls_cipher_cmac_update(&ctx, buffer, size & 0xff);
+    if (ret < 0)
+        return ret;
+    ret = mbedtls_cipher_cmac_finish(&ctx, Mic);
+    if (ret < 0)
+        return ret;
     *mic = ( uint32_t )( ( uint32_t )Mic[3] << 24 | ( uint32_t )Mic[2] << 16 | ( uint32_t )Mic[1] << 8 | ( uint32_t )Mic[0] );
+    return 0; 
 }
 
-void LoRaMacJoinDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint8_t *decBuffer )
+int LoRaMacJoinDecrypt( const uint8_t *buffer, uint16_t size, const uint8_t *key, uint8_t *decBuffer )
 {
-    /*int i;
-    pc.printf("LoRaMacJoinDecrypt%u: ", size);
-    for (i = 0; i < size; i++)
-        pc.printf("%02x ", buffer[i]);*/
-    
-    memset1( AesContext.ksch, '\0', 240 );
-    aes_set_key( key, 16, &AesContext );
-    aes_encrypt( buffer, decBuffer, &AesContext );
-    // Check if optional CFList is included
-    /*pc.printf("\r\ndecr:");
-    for (i = 0; i < 16; i++)
-        pc.printf("%02x ", decBuffer[i]);*/
-    if( size >= 16 )
-    {
-        aes_encrypt( buffer + 16, decBuffer + 16, &AesContext );
-        /*pc.printf("\r\ndecr:");
-        for (i = 0; i < 16; i++)
-            pc.printf("%02x ", decBuffer[i+16]);
-        pc.printf("\r\n");*/
+    int ret;
+    mbedtls_aes_context actx;
+
+    mbedtls_aes_init(&actx);
+    ret = mbedtls_aes_setkey_enc(&actx, key, 128);
+    if (ret < 0)
+        return -1;
+
+    mbedtls_aes_encrypt(&actx, buffer, decBuffer);
+
+    if (size >= 16) {
+        mbedtls_aes_encrypt(&actx, buffer + 16, decBuffer + 16);
     }
+
+    mbedtls_aes_free(&actx);
+
+    return 0;
 }
 
-void LoRaMacJoinComputeSKeys( const uint8_t *key, const uint8_t *appNonce, uint16_t devNonce, uint8_t *nwkSKey, uint8_t *appSKey )
+int LoRaMacJoinComputeSKeys( const uint8_t *key, const uint8_t *appNonce, uint16_t devNonce, uint8_t *nwkSKey, uint8_t *appSKey )
 {
+    mbedtls_aes_context actx;
     uint8_t nonce[16];
     uint8_t *pDevNonce = ( uint8_t * )&devNonce;
-    
-    memset1( AesContext.ksch, '\0', 240 );
-    aes_set_key( key, 16, &AesContext );
+
+    mbedtls_aes_init(&actx);
+    if (mbedtls_aes_setkey_enc(&actx, key, 128) < 0)
+        return -1;
 
     memset1( nonce, 0, sizeof( nonce ) );
     nonce[0] = 0x01;
     memcpy1( nonce + 1, appNonce, 6 );
     memcpy1( nonce + 7, pDevNonce, 2 );
-    aes_encrypt( nonce, nwkSKey, &AesContext );
+    mbedtls_aes_encrypt(&actx, nonce, nwkSKey);
 
     memset1( nonce, 0, sizeof( nonce ) );
     nonce[0] = 0x02;
     memcpy1( nonce + 1, appNonce, 6 );
     memcpy1( nonce + 7, pDevNonce, 2 );
-    aes_encrypt( nonce, appSKey, &AesContext );
+    mbedtls_aes_encrypt(&actx, nonce, appSKey);
+
+    mbedtls_aes_free(&actx);
+
+    return 0;
 }
+
+int LoRaMacCryptoInit()
+{
+    int ret;
+    const mbedtls_cipher_info_t *cipher_info;
+    mbedtls_cipher_init(&ctx);
+    cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
+    if (cipher_info == NULL) {
+        return -1;
+    }
+    ret = mbedtls_cipher_setup(&ctx, cipher_info);
+    return ret;
+}
+