LoRaMacCrypto.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*!
  2. * \file LoRaMacCrypto.h
  3. *
  4. * \brief LoRa MAC layer cryptographic functionality implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * ___ _____ _ ___ _ _____ ___ ___ ___ ___
  18. * / __|_ _/_\ / __| |/ / __/ _ \| _ \/ __| __|
  19. * \__ \ | |/ _ \ (__| ' <| _| (_) | / (__| _|
  20. * |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
  21. * embedded.connectivity.solutions===============
  22. *
  23. * \endcode
  24. *
  25. * \author Miguel Luis ( Semtech )
  26. *
  27. * \author Gregory Cristian ( Semtech )
  28. *
  29. * \author Daniel Jaeckle ( STACKFORCE )
  30. *
  31. * \author Johannes Bruder ( STACKFORCE )
  32. *
  33. * addtogroup LORAMAC
  34. * \{
  35. *
  36. */
  37. #ifndef __LORAMAC_CRYPTO_H__
  38. #define __LORAMAC_CRYPTO_H__
  39. #ifdef __cplusplus
  40. extern "C"
  41. {
  42. #endif
  43. #include <stdlib.h>
  44. #include <stdint.h>
  45. #include <stdbool.h>
  46. #include "utilities.h"
  47. #include "LoRaMacTypes.h"
  48. #include "LoRaMacMessageTypes.h"
  49. /*
  50. * Initial value of the frame counters
  51. */
  52. #define FCNT_DOWN_INITAL_VALUE 0xFFFFFFFF
  53. /*!
  54. * LoRaMac Cryto Status
  55. */
  56. typedef enum eLoRaMacCryptoStatus
  57. {
  58. /*!
  59. * No error occurred
  60. */
  61. LORAMAC_CRYPTO_SUCCESS = 0,
  62. /*!
  63. * MIC does not match
  64. */
  65. LORAMAC_CRYPTO_FAIL_MIC,
  66. /*!
  67. * Address does not match
  68. */
  69. LORAMAC_CRYPTO_FAIL_ADDRESS,
  70. /*!
  71. * JoinNonce was not greater than previous one.
  72. */
  73. LORAMAC_CRYPTO_FAIL_JOIN_NONCE,
  74. /*!
  75. * RJcount0 reached 2^16-1
  76. */
  77. LORAMAC_CRYPTO_FAIL_RJCOUNT0_OVERFLOW,
  78. /*!
  79. * FCNT_ID is not supported
  80. */
  81. LORAMAC_CRYPTO_FAIL_FCNT_ID,
  82. /*!
  83. * FCntUp/Down check failed (new FCnt is smaller than previous one)
  84. */
  85. LORAMAC_CRYPTO_FAIL_FCNT_SMALLER,
  86. /*!
  87. * FCntUp/Down check failed (duplicated)
  88. */
  89. LORAMAC_CRYPTO_FAIL_FCNT_DUPLICATED,
  90. /*!
  91. * MAX_GAP_FCNT check failed
  92. */
  93. LORAMAC_CRYPTO_FAIL_MAX_GAP_FCNT,
  94. /*!
  95. * Not allowed parameter value
  96. */
  97. LORAMAC_CRYPTO_FAIL_PARAM,
  98. /*!
  99. * Null pointer exception
  100. */
  101. LORAMAC_CRYPTO_ERROR_NPE,
  102. /*!
  103. * Invalid key identifier exception
  104. */
  105. LORAMAC_CRYPTO_ERROR_INVALID_KEY_ID,
  106. /*!
  107. * Invalid address identifier exception
  108. */
  109. LORAMAC_CRYPTO_ERROR_INVALID_ADDR_ID,
  110. /*!
  111. * Invalid LoRaWAN specification version
  112. */
  113. LORAMAC_CRYPTO_ERROR_INVALID_VERSION,
  114. /*!
  115. * Incompatible buffer size
  116. */
  117. LORAMAC_CRYPTO_ERROR_BUF_SIZE,
  118. /*!
  119. * The secure element reports an error
  120. */
  121. LORAMAC_CRYPTO_ERROR_SECURE_ELEMENT_FUNC,
  122. /*!
  123. * Error from parser reported
  124. */
  125. LORAMAC_CRYPTO_ERROR_PARSER,
  126. /*!
  127. * Error from serializer reported
  128. */
  129. LORAMAC_CRYPTO_ERROR_SERIALIZER,
  130. /*!
  131. * RJcount1 reached 2^16-1 which should never happen
  132. */
  133. LORAMAC_CRYPTO_ERROR_RJCOUNT1_OVERFLOW,
  134. /*!
  135. * Undefined Error occurred
  136. */
  137. LORAMAC_CRYPTO_ERROR,
  138. }LoRaMacCryptoStatus_t;
  139. /*!
  140. * Signature of callback function to be called by the LoRaMac Crypto module when the
  141. * non-volatile context have to be stored. It is also possible to save the entire
  142. * crypto module context.
  143. *
  144. */
  145. typedef void ( *LoRaMacCryptoNvmEvent )( void );
  146. /*!
  147. * Initialization of LoRaMac Crypto module
  148. * It sets initial values of volatile variables and assigns the non-volatile context.
  149. *
  150. * \param[IN] cryptoNvmCtxChanged - Callback function which will be called when the
  151. * non-volatile context have to be stored.
  152. * \retval - Status of the operation
  153. */
  154. LoRaMacCryptoStatus_t LoRaMacCryptoInit( LoRaMacCryptoNvmEvent cryptoNvmCtxChanged );
  155. /*!
  156. * Sets the LoRaWAN specification version to be used.
  157. *
  158. * \warning This function should be used for ABP only. In case of OTA the version will be set automatically.
  159. *
  160. * \param[IN] version - LoRaWAN specification version to be used.
  161. *
  162. * \retval - Status of the operation
  163. */
  164. LoRaMacCryptoStatus_t LoRaMacCryptoSetLrWanVersion( Version_t version );
  165. /*!
  166. * Restores the internal nvm context from passed pointer.
  167. *
  168. * \param[IN] cryptoNmvCtx - Pointer to non-volatile crypto module context to be restored.
  169. * \retval - Status of the operation
  170. */
  171. LoRaMacCryptoStatus_t LoRaMacCryptoRestoreNvmCtx( void* cryptoNvmCtx );
  172. /*!
  173. * Returns a pointer to the internal non-volatile context.
  174. *
  175. * \param[IN] cryptoNvmCtxSize - Size of the module non-volatile context
  176. * \retval - Points to a structure where the module store its non-volatile context
  177. */
  178. void* LoRaMacCryptoGetNvmCtx( size_t* cryptoNvmCtxSize );
  179. /*!
  180. * Returns updated fCntID downlink counter value.
  181. *
  182. * \param[IN] fCntID - Frame counter identifier
  183. * \param[IN] maxFcntGap - Maximum allowed frame counter difference (only necessary for L2 LW1.0.x)
  184. * \param[IN] frameFcnt - Received frame counter (used to update current counter value)
  185. * \param[OUT] currentDown - Current downlink counter value
  186. * \retval - Status of the operation
  187. */
  188. LoRaMacCryptoStatus_t LoRaMacCryptoGetFCntDown( FCntIdentifier_t fCntID, uint16_t maxFCntGap, uint32_t frameFcnt, uint32_t* currentDown );
  189. /*!
  190. * Returns updated fCntUp uplink counter value.
  191. *
  192. * \param[IN] currentUp - Uplink counter value
  193. * \retval - Status of the operation
  194. */
  195. LoRaMacCryptoStatus_t LoRaMacCryptoGetFCntUp( uint32_t* currentUp );
  196. /*!
  197. * Provides multicast context.
  198. *
  199. * \param[IN] multicastList - Pointer to the multicast context list
  200. *
  201. * \retval - Status of the operation
  202. */
  203. LoRaMacCryptoStatus_t LoRaMacCryptoSetMulticastReference( MulticastCtx_t* multicastList );
  204. /*!
  205. * Sets a key
  206. *
  207. * \param[IN] keyID - Key identifier
  208. * \param[IN] key - Key value (16 byte), if its a multicast key it must be encrypted with McKEKey
  209. * \retval - Status of the operation
  210. */
  211. LoRaMacCryptoStatus_t LoRaMacCryptoSetKey( KeyIdentifier_t keyID, uint8_t* key );
  212. /*!
  213. * Prepares the join-request message.
  214. * It computes the mic and add it to the message.
  215. *
  216. * \param[IN/OUT] macMsg - Join-request message object
  217. * \retval - Status of the operation
  218. */
  219. LoRaMacCryptoStatus_t LoRaMacCryptoPrepareJoinRequest( LoRaMacMessageJoinRequest_t* macMsg );
  220. /*!
  221. * Prepares a rejoin-request type 1 message.
  222. * It computes the mic and add it to the message.
  223. *
  224. * \param[IN/OUT] macMsg - Rejoin message object
  225. * \retval - Status of the operation
  226. */
  227. LoRaMacCryptoStatus_t LoRaMacCryptoPrepareReJoinType1( LoRaMacMessageReJoinType1_t* macMsg );
  228. /*!
  229. * Prepares a rejoin-request type 0 or 2 message.
  230. * It computes the mic and add it to the message.
  231. *
  232. * \param[IN/OUT] macMsg - Rejoin message object
  233. * \retval - Status of the operation
  234. */
  235. LoRaMacCryptoStatus_t LoRaMacCryptoPrepareReJoinType0or2( LoRaMacMessageReJoinType0or2_t* macMsg );
  236. /*!
  237. * Handles the join-accept message.
  238. * It decrypts the message, verifies the MIC and if successful derives the session keys.
  239. *
  240. * \param[IN] joinReqType - Type of last join-request or rejoin which triggered the join-accept response
  241. * \param[IN] joinEUI - Join server EUI (8 byte)
  242. * \param[IN/OUT] macMsg - Join-accept message object
  243. * \retval - Status of the operation
  244. */
  245. LoRaMacCryptoStatus_t LoRaMacCryptoHandleJoinAccept( JoinReqIdentifier_t joinReqType, uint8_t* joinEUI, LoRaMacMessageJoinAccept_t* macMsg );
  246. /*!
  247. * Secures a message (encryption + integrity).
  248. *
  249. * \param[IN] fCntUp - Uplink sequence counter
  250. * \param[IN] txDr - Data rate used for the transmission
  251. * \param[IN] txCh - Index of the channel used for the transmission
  252. * \param[IN/OUT] macMsg - Data message object
  253. * \retval - Status of the operation
  254. */
  255. LoRaMacCryptoStatus_t LoRaMacCryptoSecureMessage( uint32_t fCntUp, uint8_t txDr, uint8_t txCh, LoRaMacMessageData_t* macMsg );
  256. /*!
  257. * Unsecures a message (decryption + integrity verification).
  258. *
  259. * \param[IN] addrID - Address identifier
  260. * \param[IN] address - Address
  261. * \param[IN] fCntID - Frame counter identifier
  262. * \param[IN] fCntDown - Downlink sequence counter
  263. * \param[IN/OUT] macMsg - Data message object
  264. * \retval - Status of the operation
  265. */
  266. LoRaMacCryptoStatus_t LoRaMacCryptoUnsecureMessage( AddressIdentifier_t addrID, uint32_t address, FCntIdentifier_t fCntID, uint32_t fCntDown, LoRaMacMessageData_t* macMsg );
  267. /*!
  268. * Derives the McRootKey from the GenAppKey or AppKey.
  269. *
  270. * 1.0.x
  271. * McRootKey = aes128_encrypt(GenAppKey , 0x00 | pad16)
  272. *
  273. * 1.1.x
  274. * McRootKey = aes128_encrypt(AppKey , 0x20 | pad16)
  275. *
  276. * \param[IN] keyID - Key identifier of the root key to use to perform the derivation ( GenAppKey or AppKey )
  277. * \retval - Status of the operation
  278. */
  279. LoRaMacCryptoStatus_t LoRaMacCryptoDeriveMcRootKey( KeyIdentifier_t keyID );
  280. /*!
  281. * Derives the McKEKey from the McRootKey.
  282. *
  283. * McKEKey = aes128_encrypt(McRootKey , 0x00 | pad16)
  284. *
  285. * \param[IN] keyID - Key identifier of the root key to use to perform the derivation ( McRootKey )
  286. * \retval - Status of the operation
  287. */
  288. LoRaMacCryptoStatus_t LoRaMacCryptoDeriveMcKEKey( KeyIdentifier_t keyID );
  289. /*!
  290. * Derives a Multicast group key pair ( McAppSKey, McNwkSKey ) from McKey
  291. *
  292. * McAppSKey = aes128_encrypt(McKey, 0x01 | McAddr | pad16)
  293. * McNwkSKey = aes128_encrypt(McKey, 0x02 | McAddr | pad16)
  294. *
  295. * \param[IN] addrID - Address identifier to select the multicast group
  296. * \param[IN] mcAddr - Multicast group address (4 bytes)
  297. * \retval - Status of the operation
  298. */
  299. LoRaMacCryptoStatus_t LoRaMacCryptoDeriveMcSessionKeyPair( AddressIdentifier_t addrID, uint32_t mcAddr );
  300. /*! \} addtogroup LORAMAC */
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #endif // __LORAMAC_CRYPTO_H__