fota lib for mdot
Embed:
(wiki syntax)
Show/hide line numbers
Crypto.h
00001 /* ______ _ 00002 / _____) _ | | 00003 ( (____ _____ ____ _| |_ _____ ____| |__ 00004 \____ \| ___ | (_ _) ___ |/ ___) _ \ 00005 _____) ) ____| | | || |_| ____( (___| | | | 00006 (______/|_____)_|_|_| \__)_____)\____)_| |_| 00007 (C)2013 Semtech 00008 00009 Description: LoRa MAC layer implementation 00010 00011 License: Revised BSD License, see LICENSE.TXT file include in the project 00012 00013 Maintainer: Miguel Luis and Gregory Cristian 00014 */ 00015 #ifndef __LORA_CRYPTO_H__ 00016 #define __LORA_CRYPTO_H__ 00017 00018 #include "casado/aes.h" 00019 #include "gladman/cmac.h" 00020 #include <cstring> 00021 #include <inttypes.h> 00022 00023 namespace lora { 00024 00025 class Crypto 00026 { 00027 public: 00028 Crypto(void); 00029 00030 /*! 00031 * Computes the LoRaMAC frame MIC field 00032 * 00033 * \param [IN] buffer Data buffer 00034 * \param [IN] size Data buffer size 00035 * \param [IN] key AES key to be used 00036 * \param [IN] address Frame address 00037 * \param [IN] dir Frame direction [0: uplink, 1: downlink, 2:peer] 00038 * \param [IN] sequenceCounter Frame sequence counter 00039 * \param [OUT] mic Computed MIC field 00040 */ 00041 void ComputeMic(uint8_t *buffer, uint16_t size, uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint32_t *mic); 00042 00043 /*! 00044 * Computes the LoRaMAC payload encryption 00045 * 00046 * \param [IN] buffer Data buffer 00047 * \param [IN] size Data buffer size 00048 * \param [IN] key AES key to be used 00049 * \param [IN] address Frame address 00050 * \param [IN] dir Frame direction [0: uplink, 1: downlink, 2:peer] 00051 * \param [IN] sequenceCounter Frame sequence counter 00052 * \param [OUT] encBuffer Encrypted buffer 00053 */ 00054 void PayloadEncrypt(const uint8_t *buffer, uint16_t size, uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *encBuffer); 00055 00056 /*! 00057 * Computes the LoRaMAC payload decryption 00058 * 00059 * \param [IN] buffer Data buffer 00060 * \param [IN] size Data buffer size 00061 * \param [IN] key AES key to be used 00062 * \param [IN] address Frame address 00063 * \param [IN] dir Frame direction [0: uplink, 1: downlink, 2:peer] 00064 * \param [IN] sequenceCounter Frame sequence counter 00065 * \param [OUT] decBuffer Decrypted buffer 00066 */ 00067 void PayloadDecrypt(uint8_t *buffer, uint16_t size, uint8_t *key, uint32_t address, uint8_t dir, uint32_t sequenceCounter, uint8_t *decBuffer); 00068 00069 /*! 00070 * Computes the LoRaMAC Join Request frame MIC field 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 [OUT] mic Computed MIC field 00076 */ 00077 void JoinComputeMic(const uint8_t *buffer, uint16_t size, const uint8_t *key, uint32_t *mic); 00078 00079 /*! 00080 * Computes the LoRaMAC join frame decryption 00081 * 00082 * \param [IN] buffer Data buffer 00083 * \param [IN] size Data buffer size 00084 * \param [IN] key AES key to be used 00085 * \param [OUT] decBuffer Decrypted buffer 00086 */ 00087 void JoinDecrypt(uint8_t *buffer, uint16_t size, uint8_t *key, uint8_t *decBuffer); 00088 00089 /*! 00090 * Computes the LoRaMAC join frame decryption 00091 * 00092 * \param [IN] key AES key to be used 00093 * \param [IN] appNonce Application nonce 00094 * \param [IN] devNonce Device nonce 00095 * \param [OUT] nwkSKey Network session key 00096 * \param [OUT] appSKey Application session key 00097 */ 00098 void DeriveSessionKeys(uint8_t *key, uint8_t *appNonce, uint8_t *netID, uint16_t devNonce, uint8_t *nwkSKey, uint8_t *appSKey); 00099 00100 /*! 00101 * Compute ping slot for current beacon frame 00102 * \param [IN] beaconTime Time of current beacon 00103 * \param [IN] devAddr Device Address 00104 * \param [IN] period Period of downlink 00105 * \return current pinslot 00106 */ 00107 uint32_t ComputePingSlot(uint32_t beaconTime, uint32_t devAddr, uint32_t period); 00108 00109 /*! 00110 * Helper to copy mic bytes onto buffer 00111 * \param [IN] mic integer mic value 00112 * \param [IN] buff location to copy to 00113 */ 00114 void CopyMicToArray(uint32_t mic, uint8_t* buff); 00115 00116 void DeriveMcKEKey(uint8_t *mcKEKey, uint8_t *appKey, uint8_t *EUI); 00117 00118 void DeriveMcKey(uint8_t *mcKey, uint8_t *mcKeyEncrypt, uint8_t *mcKEKey); 00119 00120 void DeriveMcSessionKeys(uint8_t *mcAppKey, uint8_t *mcNetKey, uint8_t *mcKey, uint8_t *mcAddr); 00121 00122 private: 00123 /*! 00124 * MIC field computation initial data 00125 */ 00126 uint8_t MicBlockB0[16]; 00127 00128 /*! 00129 * Contains the computed MIC field. 00130 * 00131 * \remark Only the 4 first bytes are used 00132 */ 00133 uint8_t Mic[16]; 00134 00135 /*! 00136 * Encryption aBlock and sBlock 00137 */ 00138 uint8_t aBlock[16]; 00139 uint8_t sBlock[16]; 00140 00141 /*! 00142 * AES computation context variable 00143 */ 00144 aes_context AesContext; 00145 00146 /*! 00147 * CMAC computation context variable 00148 */ 00149 AES_CMAC_CTX AesCmacCtx[1]; 00150 }; 00151 00152 } 00153 #endif // __LORAMAC_CRYPTO_H__
Generated on Tue Jul 12 2022 12:07:34 by 1.7.2