fsl_puf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright 2018-2021 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _PUF_H_
  9. #define _PUF_H_
  10. #include <stddef.h>
  11. #include <stdint.h>
  12. #include "fsl_common.h"
  13. /*******************************************************************************
  14. * Definitions
  15. *******************************************************************************/
  16. /*!
  17. * @addtogroup puf_driver
  18. * @{
  19. */
  20. /*! @name Driver version */
  21. /*@{*/
  22. /*! @brief PUF driver version. Version 2.1.6.
  23. *
  24. * Current version: 2.1.6
  25. *
  26. * Change log:
  27. * - 2.0.0
  28. * - Initial version.
  29. * - 2.0.1
  30. * - Fixed puf_wait_usec function optimization issue.
  31. * - 2.0.2
  32. * - Add PUF configuration structure and support for PUF SRAM controller.
  33. * Remove magic constants.
  34. * - 2.0.3
  35. * - Fix MISRA C-2012 issue.
  36. * - 2.1.0
  37. * - Align driver with PUF SRAM controller registers on LPCXpresso55s16.
  38. * - Update initizalition logic .
  39. * - 2.1.1
  40. * - Fix ARMGCC build warning .
  41. * - 2.1.2
  42. * - Update: Add automatic big to little endian swap for user
  43. * (pre-shared) keys destinated to secret hardware bus (PUF key index 0).
  44. * - 2.1.3
  45. * - Fix MISRA C-2012 issue.
  46. * - 2.1.4
  47. * - Replace register uint32_t ticksCount with volatile uint32_t ticksCount in puf_wait_usec() to prevent optimization
  48. * out delay loop.
  49. * - 2.1.5
  50. * - Use common SDK delay in puf_wait_usec()
  51. * - 2.1.6
  52. * - Changed wait time in PUF_Init(), when initialization fails it will try PUF_Powercycle() with shorter time. If
  53. * this shorter time will also fail, initialization will be tried with worst case time as before.
  54. */
  55. #define FSL_PUF_DRIVER_VERSION (MAKE_VERSION(2, 1, 6))
  56. /*@}*/
  57. typedef enum _puf_key_index_register
  58. {
  59. kPUF_KeyIndex_00 = 0x00U,
  60. kPUF_KeyIndex_01 = 0x01U,
  61. kPUF_KeyIndex_02 = 0x02U,
  62. kPUF_KeyIndex_03 = 0x03U,
  63. kPUF_KeyIndex_04 = 0x04U,
  64. kPUF_KeyIndex_05 = 0x05U,
  65. kPUF_KeyIndex_06 = 0x06U,
  66. kPUF_KeyIndex_07 = 0x07U,
  67. kPUF_KeyIndex_08 = 0x08U,
  68. kPUF_KeyIndex_09 = 0x09U,
  69. kPUF_KeyIndex_10 = 0x0AU,
  70. kPUF_KeyIndex_11 = 0x0BU,
  71. kPUF_KeyIndex_12 = 0x0CU,
  72. kPUF_KeyIndex_13 = 0x0DU,
  73. kPUF_KeyIndex_14 = 0x0EU,
  74. kPUF_KeyIndex_15 = 0x0FU,
  75. } puf_key_index_register_t;
  76. typedef enum _puf_min_max
  77. {
  78. kPUF_KeySizeMin = 8u,
  79. kPUF_KeySizeMax = 512u,
  80. kPUF_KeyIndexMax = kPUF_KeyIndex_15,
  81. } puf_min_max_t;
  82. /*! @brief PUF key slot. */
  83. typedef enum _puf_key_slot
  84. {
  85. kPUF_KeySlot0 = 0U, /*!< PUF key slot 0 */
  86. kPUF_KeySlot1 = 1U, /*!< PUF key slot 1 */
  87. #if defined(PUF_KEYMASK_COUNT) && (PUF_KEYMASK_COUNT > 2)
  88. kPUF_KeySlot2 = 2U, /*!< PUF key slot 2 */
  89. kPUF_KeySlot3 = 3U, /*!< PUF key slot 3 */
  90. #endif
  91. } puf_key_slot_t;
  92. typedef struct
  93. {
  94. uint32_t dischargeTimeMsec;
  95. uint32_t coreClockFrequencyHz;
  96. #if defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  97. /* LPCXpresso55s16 */
  98. PUF_SRAM_CTRL_Type *puf_sram_base;
  99. uint8_t CKGATING;
  100. #endif /* FSL_FEATURE_PUF_HAS_SRAM_CTRL */
  101. } puf_config_t;
  102. /*! @brief Get Key Code size in bytes from key size in bytes at compile time. */
  103. #define PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(x) ((160u + (((((x) << 3) + 255u) >> 8) << 8)) >> 3)
  104. #define PUF_MIN_KEY_CODE_SIZE PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(8UL)
  105. #define PUF_ACTIVATION_CODE_SIZE 1192U
  106. #define KEYSTORE_PUF_DISCHARGE_TIME_FIRST_TRY_MS 50
  107. #define KEYSTORE_PUF_DISCHARGE_TIME_MAX_MS 400
  108. /*! PUF status return codes. */
  109. enum
  110. {
  111. kStatus_EnrollNotAllowed = MAKE_STATUS(kStatusGroup_PUF, 1),
  112. kStatus_StartNotAllowed = MAKE_STATUS(kStatusGroup_PUF, 2)
  113. };
  114. /*! @} */
  115. /*******************************************************************************
  116. * API
  117. *******************************************************************************/
  118. #if defined(__cplusplus)
  119. extern "C" {
  120. #endif /* __cplusplus */
  121. /*!
  122. * @brief Sets the default configuration of PUF
  123. *
  124. * This function initialize PUF config structure to default values.
  125. *
  126. * @param conf PUF configuration structure
  127. */
  128. void PUF_GetDefaultConfig(puf_config_t *conf);
  129. /*!
  130. * @brief Initialize PUF
  131. *
  132. * This function enables power to PUF block and waits until the block initializes.
  133. *
  134. * @param base PUF peripheral base address
  135. * @param conf PUF configuration structure
  136. * @return Status of the init operation
  137. */
  138. status_t PUF_Init(PUF_Type *base, puf_config_t *conf);
  139. /*!
  140. * @brief Denitialize PUF
  141. *
  142. * This function disables power to PUF SRAM and peripheral clock.
  143. *
  144. * @param base PUF peripheral base address
  145. * @param conf PUF configuration structure
  146. */
  147. void PUF_Deinit(PUF_Type *base, puf_config_t *conf);
  148. /*!
  149. * @brief Enroll PUF
  150. *
  151. * This function derives a digital fingerprint, generates the corresponding Activation Code (AC)
  152. * and returns it to be stored in an NVM or a file. This step needs to be
  153. * performed only once for each device. This function may be permanently disallowed by a fuse.
  154. *
  155. * @param base PUF peripheral base address
  156. * @param[out] activationCode Word aligned address of the resulting activation code.
  157. * @param activationCodeSize Size of the activationCode buffer in bytes. Shall be 1192 bytes.
  158. * @return Status of enroll operation.
  159. */
  160. status_t PUF_Enroll(PUF_Type *base, uint8_t *activationCode, size_t activationCodeSize);
  161. /*!
  162. * @brief Start PUF
  163. *
  164. * The Activation Code generated during the Enroll operation is used to
  165. * reconstruct the digital fingerprint. This needs to be done after every power-up
  166. * and reset.
  167. *
  168. * @param base PUF peripheral base address
  169. * @param activationCode Word aligned address of the input activation code.
  170. * @param activationCodeSize Size of the activationCode buffer in bytes. Shall be 1192 bytes.
  171. * @return Status of start operation.
  172. */
  173. status_t PUF_Start(PUF_Type *base, const uint8_t *activationCode, size_t activationCodeSize);
  174. /*!
  175. * @brief Set intrinsic key
  176. *
  177. * The digital fingerprint generated during the Enroll/Start
  178. * operations is used to generate a Key Code (KC) that defines a unique intrinsic
  179. * key. This KC is returned to be stored in an NVM or a file. This operation
  180. * needs to be done only once for each intrinsic key.
  181. * Each time a Set Intrinsic Key operation is executed a new unique key is
  182. * generated.
  183. *
  184. * @param base PUF peripheral base address
  185. * @param keyIndex PUF key index register
  186. * @param keySize Size of the intrinsic key to generate in bytes.
  187. * @param[out] keyCode Word aligned address of the resulting key code.
  188. * @param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize).
  189. * @return Status of set intrinsic key operation.
  190. */
  191. status_t PUF_SetIntrinsicKey(
  192. PUF_Type *base, puf_key_index_register_t keyIndex, size_t keySize, uint8_t *keyCode, size_t keyCodeSize);
  193. /*!
  194. * @brief Set user key
  195. *
  196. * The digital fingerprint generated during the Enroll/Start
  197. * operations and a user key (UK) provided as input are used to
  198. * generate a Key Code (KC). This KC is sent returned to be stored
  199. * in an NVM or a file. This operation needs to be done only once for each user key.
  200. *
  201. * @param base PUF peripheral base address
  202. * @param keyIndex PUF key index register
  203. * @param userKey Word aligned address of input user key.
  204. * @param userKeySize Size of the input user key in bytes.
  205. * @param[out] keyCode Word aligned address of the resulting key code.
  206. * @param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(userKeySize).
  207. * @return Status of set user key operation.
  208. */
  209. status_t PUF_SetUserKey(PUF_Type *base,
  210. puf_key_index_register_t keyIndex,
  211. const uint8_t *userKey,
  212. size_t userKeySize,
  213. uint8_t *keyCode,
  214. size_t keyCodeSize);
  215. /*!
  216. * @brief Reconstruct key from a key code
  217. *
  218. * The digital fingerprint generated during the Start operation and the KC
  219. * generated during a Set Key operation (Set intrinsic key or Set user key) are used to retrieve a stored key. This
  220. * operation needs to be done every time a key is needed.
  221. * This function accepts only Key Codes created for PUF index registers kPUF_KeyIndex_01 to kPUF_KeyIndex_15.
  222. *
  223. * @param base PUF peripheral base address
  224. * @param keyCode Word aligned address of the input key code.
  225. * @param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize).
  226. * @param[out] key Word aligned address of output key.
  227. * @param keySize Size of the output key in bytes.
  228. * @return Status of get key operation.
  229. */
  230. status_t PUF_GetKey(PUF_Type *base, const uint8_t *keyCode, size_t keyCodeSize, uint8_t *key, size_t keySize);
  231. /*!
  232. * @brief Reconstruct hw bus key from a key code
  233. *
  234. * The digital fingerprint generated during the Start operation and the KC
  235. * generated during a Set Key operation (Set intrinsic key or Set user key) are used to retrieve a stored key. This
  236. * operation needs to be done every time a key is needed.
  237. * This function accepts only Key Codes created for PUF index register kPUF_KeyIndex_00.
  238. * Such a key is output directly to a dedicated hardware bus. The reconstructed key is not exposed to system memory.
  239. *
  240. * @param base PUF peripheral base address
  241. * @param keyCode Word aligned address of the input key code.
  242. * @param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize).
  243. * @param keySlot key slot to output on hw bus. Parameter is ignored on devices with less than two key slots.
  244. * @param keyMask key masking value. Shall be random for each POR/reset. Value does not have to be cryptographicaly
  245. * secure.
  246. * @return Status of get key operation.
  247. */
  248. status_t PUF_GetHwKey(
  249. PUF_Type *base, const uint8_t *keyCode, size_t keyCodeSize, puf_key_slot_t keySlot, uint32_t keyMask);
  250. /*!
  251. * @brief Zeroize PUF
  252. *
  253. * This function clears all PUF internal logic and puts the PUF to error state.
  254. *
  255. * @param base PUF peripheral base address
  256. * @return Status of the zeroize operation.
  257. */
  258. status_t PUF_Zeroize(PUF_Type *base);
  259. /*!
  260. * @brief Checks if Get Key operation is allowed.
  261. *
  262. * This function returns true if get key operation is allowed.
  263. *
  264. * @param base PUF peripheral base address
  265. * @return true if get key operation is allowed
  266. */
  267. bool PUF_IsGetKeyAllowed(PUF_Type *base);
  268. #if defined(PUF_CFG_BLOCKKEYOUTPUT_MASK) && PUF_CFG_BLOCKKEYOUTPUT_MASK
  269. static inline void PUF_BlockSetKey(PUF_Type *base)
  270. {
  271. base->CFG |= PUF_CFG_BLOCKKEYOUTPUT_MASK; /* block set key */
  272. }
  273. #endif /* PUF_CFG_BLOCKKEYOUTPUT_MASK */
  274. #if defined(PUF_CFG_PUF_BLOCK_SET_KEY_MASK) && PUF_CFG_PUF_BLOCK_SET_KEY_MASK
  275. static inline void PUF_BlockSetKey(PUF_Type *base)
  276. {
  277. base->CFG |= PUF_CFG_PUF_BLOCK_SET_KEY_MASK; /* block set key */
  278. }
  279. #endif /* PUF_CFG_PUF_BLOCK_SET_KEY_MASK */
  280. #if defined(PUF_CFG_BLOCKENROLL_SETKEY_MASK) && PUF_CFG_BLOCKENROLL_SETKEY_MASK
  281. static inline void PUF_BlockEnroll(PUF_Type *base)
  282. {
  283. base->CFG |= PUF_CFG_BLOCKENROLL_SETKEY_MASK; /* block enroll */
  284. }
  285. #endif /* PUF_CFG_BLOCKENROLL_SETKEY_MASK */
  286. #if defined(PUF_CFG_PUF_BLOCK_ENROLL_MASK) && PUF_CFG_PUF_BLOCK_ENROLL_MASK
  287. static inline void PUF_BlockEnroll(PUF_Type *base)
  288. {
  289. base->CFG |= PUF_CFG_PUF_BLOCK_ENROLL_MASK; /* block enroll */
  290. }
  291. #endif /* PUF_CFG_PUF_BLOCK_ENROLL_MASK */
  292. /*!
  293. * @brief Powercycle PUF
  294. *
  295. * This function make powercycle.
  296. *
  297. * @param base PUF peripheral base address
  298. * @param conf PUF configuration structure
  299. * @return Status of the powercycle operation.
  300. */
  301. status_t PUF_PowerCycle(PUF_Type *base, puf_config_t *conf);
  302. #if defined(__cplusplus)
  303. }
  304. #endif /* __cplusplus */
  305. #endif /* _PUF_H_ */