secure-element.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*!
  2. * \file secure-element.h
  3. *
  4. * \brief Secure Element driver API
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013 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. * \defgroup SECUREELEMENT Secure Element API Definition
  34. *
  35. * \{
  36. *
  37. */
  38. #ifndef __SECURE_ELEMENT_H__
  39. #define __SECURE_ELEMENT_H__
  40. #ifdef __cplusplus
  41. extern "C"
  42. {
  43. #endif
  44. #include <stdint.h>
  45. #include "LoRaMacCrypto.h"
  46. #define SE_EUI_SIZE 16
  47. /*!
  48. * Return values.
  49. */
  50. typedef enum eSecureElementStatus
  51. {
  52. /*!
  53. * No error occurred
  54. */
  55. SECURE_ELEMENT_SUCCESS = 0,
  56. /*!
  57. * CMAC does not match
  58. */
  59. SECURE_ELEMENT_FAIL_CMAC,
  60. /*!
  61. * Null pointer exception
  62. */
  63. SECURE_ELEMENT_ERROR_NPE,
  64. /*!
  65. * Invalid key identifier exception
  66. */
  67. SECURE_ELEMENT_ERROR_INVALID_KEY_ID,
  68. /*!
  69. * Invalid LoRaWAN specification version
  70. */
  71. SECURE_ELEMENT_ERROR_INVALID_LORAWAM_SPEC_VERSION,
  72. /*!
  73. * Incompatible buffer size
  74. */
  75. SECURE_ELEMENT_ERROR_BUF_SIZE,
  76. /*!
  77. * Undefined Error occurred
  78. */
  79. SECURE_ELEMENT_ERROR,
  80. }SecureElementStatus_t;
  81. /*!
  82. * Signature of callback function to be called by the Secure Element driver when the
  83. * non volatile context have to be stored.
  84. *
  85. */
  86. typedef void ( *SecureElementNvmEvent )( void );
  87. /*!
  88. * Initialization of Secure Element driver
  89. *
  90. * \param[IN] seNvmCtxChanged - Callback function which will be called when the
  91. * non-volatile context have to be stored.
  92. * \retval - Status of the operation
  93. */
  94. SecureElementStatus_t SecureElementInit( SecureElementNvmEvent seNvmCtxChanged );
  95. /*!
  96. * Restores the internal nvm context from passed pointer.
  97. *
  98. * \param[IN] seNvmCtx - Pointer to non-volatile module context to be restored.
  99. * \retval - Status of the operation
  100. */
  101. SecureElementStatus_t SecureElementRestoreNvmCtx( void* seNvmCtx );
  102. /*!
  103. * Returns a pointer to the internal non-volatile context.
  104. *
  105. * \param[IN] seNvmCtxSize - Size of the module non volatile context
  106. * \retval - Points to a structure where the module store its non volatile context
  107. */
  108. void* SecureElementGetNvmCtx( size_t* seNvmCtxSize );
  109. /*!
  110. * Sets a key
  111. *
  112. * \param[IN] keyID - Key identifier
  113. * \param[IN] key - Key value
  114. * \retval - Status of the operation
  115. */
  116. SecureElementStatus_t SecureElementSetKey( KeyIdentifier_t keyID, uint8_t* key );
  117. /*!
  118. * Computes a CMAC of a message using provided initial Bx block
  119. *
  120. * \param[IN] micBxBuffer - Buffer containing the initial Bx block
  121. * \param[IN] buffer - Data buffer
  122. * \param[IN] size - Data buffer size
  123. * \param[IN] keyID - Key identifier to determine the AES key to be used
  124. * \param[OUT] cmac - Computed cmac
  125. * \retval - Status of the operation
  126. */
  127. SecureElementStatus_t SecureElementComputeAesCmac( uint8_t* micBxBuffer, uint8_t* buffer, uint16_t size, KeyIdentifier_t keyID, uint32_t* cmac );
  128. /*!
  129. * Verifies a CMAC (computes and compare with expected cmac)
  130. *
  131. * \param[IN] buffer - Data buffer
  132. * \param[IN] size - Data buffer size
  133. * \param[in] expectedCmac - Expected cmac
  134. * \param[IN] keyID - Key identifier to determine the AES key to be used
  135. * \retval - Status of the operation
  136. */
  137. SecureElementStatus_t SecureElementVerifyAesCmac( uint8_t* buffer, uint16_t size, uint32_t expectedCmac, KeyIdentifier_t keyID );
  138. /*!
  139. * Encrypt a buffer
  140. *
  141. * \param[IN] buffer - Data buffer
  142. * \param[IN] size - Data buffer size
  143. * \param[IN] keyID - Key identifier to determine the AES key to be used
  144. * \param[OUT] encBuffer - Encrypted buffer
  145. * \retval - Status of the operation
  146. */
  147. SecureElementStatus_t SecureElementAesEncrypt( uint8_t* buffer, uint16_t size, KeyIdentifier_t keyID, uint8_t* encBuffer );
  148. /*!
  149. * Derives and store a key
  150. *
  151. * \param[IN] version - LoRaWAN specification version currently in use.
  152. * \param[IN] input - Input data from which the key is derived ( 16 byte )
  153. * \param[IN] rootKeyID - Key identifier of the root key to use to perform the derivation
  154. * \param[IN] targetKeyID - Key identifier of the key which will be derived
  155. * \retval - Status of the operation
  156. */
  157. SecureElementStatus_t SecureElementDeriveAndStoreKey( Version_t version, uint8_t* input, KeyIdentifier_t rootKeyID, KeyIdentifier_t targetKeyID );
  158. /*!
  159. * Generates a random number
  160. *
  161. * \param[OUT] randomNum - 32 bit random number
  162. * \retval - Status of the operation
  163. */
  164. SecureElementStatus_t SecureElementRandomNumber( uint32_t* randomNum );
  165. /*!
  166. * Sets the DevEUI
  167. *
  168. * \param[IN] devEui - Pointer to the 16-byte devEUI
  169. * \retval - Status of the operation
  170. */
  171. SecureElementStatus_t SecureElementSetDevEui( uint8_t* devEui );
  172. /*!
  173. * Gets the DevEUI
  174. *
  175. * \retval - Pointer to the 16-byte devEUI
  176. */
  177. uint8_t* SecureElementGetDevEui( void );
  178. /*!
  179. * Sets the JoinEUI
  180. *
  181. * \param[IN] joinEui - Pointer to the 16-byte joinEui
  182. * \retval - Status of the operation
  183. */
  184. SecureElementStatus_t SecureElementSetJoinEui( uint8_t* joinEui );
  185. /*!
  186. * Gets the DevEUI
  187. *
  188. * \retval - Pointer to the 16-byte joinEui
  189. */
  190. uint8_t* SecureElementGetJoinEui( void );
  191. /*! \} defgroup SECUREELEMENT */
  192. #ifdef __cplusplus
  193. }
  194. #endif
  195. #endif // __SECURE_ELEMENT_H__