Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
LoRaMacCrypto.h
00001 /** 00002 \code 00003 00004 / _____) _ | | 00005 ( (____ _____ ____ _| |_ _____ ____| |__ 00006 \____ \| ___ | (_ _) ___ |/ ___) _ \ 00007 _____) ) ____| | | || |_| ____( (___| | | | 00008 (______/|_____)_|_|_| \__)_____)\____)_| |_| 00009 (C)2013 Semtech 00010 ___ _____ _ ___ _ _____ ___ ___ ___ ___ 00011 / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __| 00012 \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _| 00013 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___| 00014 embedded.connectivity.solutions=============== 00015 00016 \endcode 00017 00018 Description: LoRa MAC Crypto implementation 00019 00020 License: Revised BSD License, see LICENSE.TXT file include in the project 00021 00022 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE ) 00023 00024 00025 Copyright (c) 2017, Arm Limited and affiliates. 00026 00027 SPDX-License-Identifier: BSD-3-Clause 00028 */ 00029 00030 #ifndef MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__ 00031 #define MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__ 00032 00033 #include "mbedtls/config.h" 00034 #include "mbedtls/aes.h" 00035 #include "mbedtls/cmac.h" 00036 00037 00038 class LoRaMacCrypto { 00039 public: 00040 /** 00041 * Constructor 00042 */ 00043 LoRaMacCrypto(); 00044 00045 /** 00046 * Destructor 00047 */ 00048 ~LoRaMacCrypto(); 00049 00050 /** 00051 * Computes the LoRaMAC frame MIC field 00052 * 00053 * @param [in] buffer - Data buffer 00054 * @param [in] size - Data buffer size 00055 * @param [in] key - AES key to be used 00056 * @param [in] key_length - Length of the key (bits) 00057 * @param [in] address - Frame address 00058 * @param [in] dir - Frame direction [0: uplink, 1: downlink] 00059 * @param [in] seq_counter - Frame sequence counter 00060 * @param [out] mic - Computed MIC field 00061 * 00062 * @return 0 if successful, or a cipher specific error code 00063 */ 00064 int compute_mic(const uint8_t *buffer, uint16_t size, 00065 const uint8_t *key, uint32_t key_length, 00066 uint32_t address, uint8_t dir, uint32_t seq_counter, 00067 uint32_t *mic); 00068 00069 /** 00070 * Performs payload encryption 00071 * 00072 * @param [in] buffer - Data buffer 00073 * @param [in] size - Data buffer size 00074 * @param [in] key - AES key to be used 00075 * @param [in] key_length - Length of the key (bits) 00076 * @param [in] address - Frame address 00077 * @param [in] dir - Frame direction [0: uplink, 1: downlink] 00078 * @param [in] seq_counter - Frame sequence counter 00079 * @param [out] enc_buffer - Encrypted buffer 00080 * 00081 * @return 0 if successful, or a cipher specific error code 00082 */ 00083 int encrypt_payload(const uint8_t *buffer, uint16_t size, 00084 const uint8_t *key, uint32_t key_length, 00085 uint32_t address, uint8_t dir, uint32_t seq_counter, 00086 uint8_t *enc_buffer); 00087 00088 /** 00089 * Performs payload decryption 00090 * 00091 * @param [in] buffer - Data buffer 00092 * @param [in] size - Data buffer size 00093 * @param [in] key - AES key to be used 00094 * @param [in] key_length - Length of the key (bits) 00095 * @param [in] address - Frame address 00096 * @param [in] dir - Frame direction [0: uplink, 1: downlink] 00097 * @param [in] seq_counter - Frame sequence counter 00098 * @param [out] dec_buffer - Decrypted buffer 00099 * 00100 * @return 0 if successful, or a cipher specific error code 00101 */ 00102 int decrypt_payload(const uint8_t *buffer, uint16_t size, 00103 const uint8_t *key, uint32_t key_length, 00104 uint32_t address, uint8_t dir, uint32_t seq_counter, 00105 uint8_t *dec_buffer); 00106 00107 /** 00108 * Computes the LoRaMAC Join Request frame MIC field 00109 * 00110 * @param [in] buffer - Data buffer 00111 * @param [in] size - Data buffer size 00112 * @param [in] key - AES key to be used 00113 * @param [in] key_length - Length of the key (bits) 00114 * @param [out] mic - Computed MIC field 00115 * 00116 * @return 0 if successful, or a cipher specific error code 00117 * 00118 */ 00119 int compute_join_frame_mic(const uint8_t *buffer, uint16_t size, 00120 const uint8_t *key, uint32_t key_length, 00121 uint32_t *mic); 00122 00123 /** 00124 * Computes the LoRaMAC join frame decryption 00125 * 00126 * @param [in] buffer - Data buffer 00127 * @param [in] size - Data buffer size 00128 * @param [in] key - AES key to be used 00129 * @param [in] key_length - Length of the key (bits) 00130 * @param [out] dec_buffer - Decrypted buffer 00131 * 00132 * @return 0 if successful, or a cipher specific error code 00133 */ 00134 int decrypt_join_frame(const uint8_t *buffer, uint16_t size, 00135 const uint8_t *key, uint32_t key_length, 00136 uint8_t *dec_buffer); 00137 00138 /** 00139 * Computes the LoRaMAC join frame decryption 00140 * 00141 * @param [in] key - AES key to be used 00142 * @param [in] key_length - Length of the key (bits) 00143 * @param [in] app_nonce - Application nonce 00144 * @param [in] dev_nonce - Device nonce 00145 * @param [out] nwk_skey - Network session key 00146 * @param [out] app_skey - Application session key 00147 * 00148 * @return 0 if successful, or a cipher specific error code 00149 */ 00150 int compute_skeys_for_join_frame(const uint8_t *key, uint32_t key_length, 00151 const uint8_t *app_nonce, uint16_t dev_nonce, 00152 uint8_t *nwk_skey, uint8_t *app_skey); 00153 00154 private: 00155 /** 00156 * AES computation context variable 00157 */ 00158 mbedtls_aes_context aes_ctx; 00159 00160 /** 00161 * CMAC computation context variable 00162 */ 00163 mbedtls_cipher_context_t aes_cmac_ctx[1]; 00164 }; 00165 00166 #endif // MBED_LORAWAN_MAC_LORAMAC_CRYPTO_H__
Generated on Tue Jul 12 2022 13:54:26 by
