A very easy to understand LoRaWAN-node code.

Dependencies:   LoRaWAN-lib SX1272Lib X_NUCLEO_IKS01A1 mbed

Important parameters:

• In comissioning.h: DevEUI, AppEUI, AppKEY, DevADR, NwksKEY, AppsKEY, OTAA and public network. Frequency and channel block to use, confirmed or unconfirmed messages, app port, app data size and OTAA and Tx duty cycles.

• In LoRaMac.h: Maximum payload and MAC commands length, receive delays, max FCNT, adr ack limit, timeout and delay, max ack retries, rssi threshold and sync words.

• In LoRaMac.cpp: Maximum payload, MAC commands and FRMpayload length.

• In LoRaMac-board.h: Tx power, data rates and band settings.

NOTE: Please refer to LoRaWAN regional parameters (page 12 for US band) to know which parameters you can modify.

Committer:
dgabino
Date:
Tue Apr 03 17:09:34 2018 +0000
Revision:
0:60ff878b27b8
A simpler way to customize your LoRaWAN payload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgabino 0:60ff878b27b8 1 /*
dgabino 0:60ff878b27b8 2 ---------------------------------------------------------------------------
dgabino 0:60ff878b27b8 3 Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
dgabino 0:60ff878b27b8 4
dgabino 0:60ff878b27b8 5 LICENSE TERMS
dgabino 0:60ff878b27b8 6
dgabino 0:60ff878b27b8 7 The redistribution and use of this software (with or without changes)
dgabino 0:60ff878b27b8 8 is allowed without the payment of fees or royalties provided that:
dgabino 0:60ff878b27b8 9
dgabino 0:60ff878b27b8 10 1. source code distributions include the above copyright notice, this
dgabino 0:60ff878b27b8 11 list of conditions and the following disclaimer;
dgabino 0:60ff878b27b8 12
dgabino 0:60ff878b27b8 13 2. binary distributions include the above copyright notice, this list
dgabino 0:60ff878b27b8 14 of conditions and the following disclaimer in their documentation;
dgabino 0:60ff878b27b8 15
dgabino 0:60ff878b27b8 16 3. the name of the copyright holder is not used to endorse products
dgabino 0:60ff878b27b8 17 built using this software without specific written permission.
dgabino 0:60ff878b27b8 18
dgabino 0:60ff878b27b8 19 DISCLAIMER
dgabino 0:60ff878b27b8 20
dgabino 0:60ff878b27b8 21 This software is provided 'as is' with no explicit or implied warranties
dgabino 0:60ff878b27b8 22 in respect of its properties, including, but not limited to, correctness
dgabino 0:60ff878b27b8 23 and/or fitness for purpose.
dgabino 0:60ff878b27b8 24 ---------------------------------------------------------------------------
dgabino 0:60ff878b27b8 25 Issue 09/09/2006
dgabino 0:60ff878b27b8 26
dgabino 0:60ff878b27b8 27 This is an AES implementation that uses only 8-bit byte operations on the
dgabino 0:60ff878b27b8 28 cipher state.
dgabino 0:60ff878b27b8 29 */
dgabino 0:60ff878b27b8 30
dgabino 0:60ff878b27b8 31 #ifndef AES_H
dgabino 0:60ff878b27b8 32 #define AES_H
dgabino 0:60ff878b27b8 33
dgabino 0:60ff878b27b8 34 #if 1
dgabino 0:60ff878b27b8 35 # define AES_ENC_PREKEYED /* AES encryption with a precomputed key schedule */
dgabino 0:60ff878b27b8 36 #endif
dgabino 0:60ff878b27b8 37 #if 0
dgabino 0:60ff878b27b8 38 # define AES_DEC_PREKEYED /* AES decryption with a precomputed key schedule */
dgabino 0:60ff878b27b8 39 #endif
dgabino 0:60ff878b27b8 40 #if 0
dgabino 0:60ff878b27b8 41 # define AES_ENC_128_OTFK /* AES encryption with 'on the fly' 128 bit keying */
dgabino 0:60ff878b27b8 42 #endif
dgabino 0:60ff878b27b8 43 #if 0
dgabino 0:60ff878b27b8 44 # define AES_DEC_128_OTFK /* AES decryption with 'on the fly' 128 bit keying */
dgabino 0:60ff878b27b8 45 #endif
dgabino 0:60ff878b27b8 46 #if 0
dgabino 0:60ff878b27b8 47 # define AES_ENC_256_OTFK /* AES encryption with 'on the fly' 256 bit keying */
dgabino 0:60ff878b27b8 48 #endif
dgabino 0:60ff878b27b8 49 #if 0
dgabino 0:60ff878b27b8 50 # define AES_DEC_256_OTFK /* AES decryption with 'on the fly' 256 bit keying */
dgabino 0:60ff878b27b8 51 #endif
dgabino 0:60ff878b27b8 52
dgabino 0:60ff878b27b8 53 #define N_ROW 4
dgabino 0:60ff878b27b8 54 #define N_COL 4
dgabino 0:60ff878b27b8 55 #define N_BLOCK (N_ROW * N_COL)
dgabino 0:60ff878b27b8 56 #define N_MAX_ROUNDS 14
dgabino 0:60ff878b27b8 57
dgabino 0:60ff878b27b8 58 typedef uint8_t return_type;
dgabino 0:60ff878b27b8 59
dgabino 0:60ff878b27b8 60 /* Warning: The key length for 256 bit keys overflows a byte
dgabino 0:60ff878b27b8 61 (see comment below)
dgabino 0:60ff878b27b8 62 */
dgabino 0:60ff878b27b8 63
dgabino 0:60ff878b27b8 64 typedef uint8_t length_type;
dgabino 0:60ff878b27b8 65
dgabino 0:60ff878b27b8 66 typedef struct
dgabino 0:60ff878b27b8 67 { uint8_t ksch[(N_MAX_ROUNDS + 1) * N_BLOCK];
dgabino 0:60ff878b27b8 68 uint8_t rnd;
dgabino 0:60ff878b27b8 69 } aes_context;
dgabino 0:60ff878b27b8 70
dgabino 0:60ff878b27b8 71 /* The following calls are for a precomputed key schedule
dgabino 0:60ff878b27b8 72
dgabino 0:60ff878b27b8 73 NOTE: If the length_type used for the key length is an
dgabino 0:60ff878b27b8 74 unsigned 8-bit character, a key length of 256 bits must
dgabino 0:60ff878b27b8 75 be entered as a length in bytes (valid inputs are hence
dgabino 0:60ff878b27b8 76 128, 192, 16, 24 and 32).
dgabino 0:60ff878b27b8 77 */
dgabino 0:60ff878b27b8 78
dgabino 0:60ff878b27b8 79 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
dgabino 0:60ff878b27b8 80
dgabino 0:60ff878b27b8 81 return_type aes_set_key( const uint8_t key[],
dgabino 0:60ff878b27b8 82 length_type keylen,
dgabino 0:60ff878b27b8 83 aes_context ctx[1] );
dgabino 0:60ff878b27b8 84 #endif
dgabino 0:60ff878b27b8 85
dgabino 0:60ff878b27b8 86 #if defined( AES_ENC_PREKEYED )
dgabino 0:60ff878b27b8 87
dgabino 0:60ff878b27b8 88 return_type aes_encrypt( const uint8_t in[N_BLOCK],
dgabino 0:60ff878b27b8 89 uint8_t out[N_BLOCK],
dgabino 0:60ff878b27b8 90 const aes_context ctx[1] );
dgabino 0:60ff878b27b8 91
dgabino 0:60ff878b27b8 92 return_type aes_cbc_encrypt( const uint8_t *in,
dgabino 0:60ff878b27b8 93 uint8_t *out,
dgabino 0:60ff878b27b8 94 int32_t n_block,
dgabino 0:60ff878b27b8 95 uint8_t iv[N_BLOCK],
dgabino 0:60ff878b27b8 96 const aes_context ctx[1] );
dgabino 0:60ff878b27b8 97 #endif
dgabino 0:60ff878b27b8 98
dgabino 0:60ff878b27b8 99 #if defined( AES_DEC_PREKEYED )
dgabino 0:60ff878b27b8 100
dgabino 0:60ff878b27b8 101 return_type aes_decrypt( const uint8_t in[N_BLOCK],
dgabino 0:60ff878b27b8 102 uint8_t out[N_BLOCK],
dgabino 0:60ff878b27b8 103 const aes_context ctx[1] );
dgabino 0:60ff878b27b8 104
dgabino 0:60ff878b27b8 105 return_type aes_cbc_decrypt( const uint8_t *in,
dgabino 0:60ff878b27b8 106 uint8_t *out,
dgabino 0:60ff878b27b8 107 int32_t n_block,
dgabino 0:60ff878b27b8 108 uint8_t iv[N_BLOCK],
dgabino 0:60ff878b27b8 109 const aes_context ctx[1] );
dgabino 0:60ff878b27b8 110 #endif
dgabino 0:60ff878b27b8 111
dgabino 0:60ff878b27b8 112 /* The following calls are for 'on the fly' keying. In this case the
dgabino 0:60ff878b27b8 113 encryption and decryption keys are different.
dgabino 0:60ff878b27b8 114
dgabino 0:60ff878b27b8 115 The encryption subroutines take a key in an array of bytes in
dgabino 0:60ff878b27b8 116 key[L] where L is 16, 24 or 32 bytes for key lengths of 128,
dgabino 0:60ff878b27b8 117 192, and 256 bits respectively. They then encrypts the input
dgabino 0:60ff878b27b8 118 data, in[] with this key and put the reult in the output array
dgabino 0:60ff878b27b8 119 out[]. In addition, the second key array, o_key[L], is used
dgabino 0:60ff878b27b8 120 to output the key that is needed by the decryption subroutine
dgabino 0:60ff878b27b8 121 to reverse the encryption operation. The two key arrays can
dgabino 0:60ff878b27b8 122 be the same array but in this case the original key will be
dgabino 0:60ff878b27b8 123 overwritten.
dgabino 0:60ff878b27b8 124
dgabino 0:60ff878b27b8 125 In the same way, the decryption subroutines output keys that
dgabino 0:60ff878b27b8 126 can be used to reverse their effect when used for encryption.
dgabino 0:60ff878b27b8 127
dgabino 0:60ff878b27b8 128 Only 128 and 256 bit keys are supported in these 'on the fly'
dgabino 0:60ff878b27b8 129 modes.
dgabino 0:60ff878b27b8 130 */
dgabino 0:60ff878b27b8 131
dgabino 0:60ff878b27b8 132 #if defined( AES_ENC_128_OTFK )
dgabino 0:60ff878b27b8 133 void aes_encrypt_128( const uint8_t in[N_BLOCK],
dgabino 0:60ff878b27b8 134 uint8_t out[N_BLOCK],
dgabino 0:60ff878b27b8 135 const uint8_t key[N_BLOCK],
dgabino 0:60ff878b27b8 136 uint8_t o_key[N_BLOCK] );
dgabino 0:60ff878b27b8 137 #endif
dgabino 0:60ff878b27b8 138
dgabino 0:60ff878b27b8 139 #if defined( AES_DEC_128_OTFK )
dgabino 0:60ff878b27b8 140 void aes_decrypt_128( const uint8_t in[N_BLOCK],
dgabino 0:60ff878b27b8 141 uint8_t out[N_BLOCK],
dgabino 0:60ff878b27b8 142 const uint8_t key[N_BLOCK],
dgabino 0:60ff878b27b8 143 uint8_t o_key[N_BLOCK] );
dgabino 0:60ff878b27b8 144 #endif
dgabino 0:60ff878b27b8 145
dgabino 0:60ff878b27b8 146 #if defined( AES_ENC_256_OTFK )
dgabino 0:60ff878b27b8 147 void aes_encrypt_256( const uint8_t in[N_BLOCK],
dgabino 0:60ff878b27b8 148 uint8_t out[N_BLOCK],
dgabino 0:60ff878b27b8 149 const uint8_t key[2 * N_BLOCK],
dgabino 0:60ff878b27b8 150 uint8_t o_key[2 * N_BLOCK] );
dgabino 0:60ff878b27b8 151 #endif
dgabino 0:60ff878b27b8 152
dgabino 0:60ff878b27b8 153 #if defined( AES_DEC_256_OTFK )
dgabino 0:60ff878b27b8 154 void aes_decrypt_256( const uint8_t in[N_BLOCK],
dgabino 0:60ff878b27b8 155 uint8_t out[N_BLOCK],
dgabino 0:60ff878b27b8 156 const uint8_t key[2 * N_BLOCK],
dgabino 0:60ff878b27b8 157 uint8_t o_key[2 * N_BLOCK] );
dgabino 0:60ff878b27b8 158 #endif
dgabino 0:60ff878b27b8 159
dgabino 0:60ff878b27b8 160 #endif