fsl_puf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * Copyright 2018-2021 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_puf.h"
  9. #include "fsl_clock.h"
  10. #include "fsl_common.h"
  11. #if !(defined(FSL_FEATURE_PUF_HAS_NO_RESET) && (FSL_FEATURE_PUF_HAS_NO_RESET > 0))
  12. #include "fsl_reset.h"
  13. #endif /* FSL_FEATURE_PUF_HAS_NO_RESET */
  14. /* Component ID definition, used by tools. */
  15. #ifndef FSL_COMPONENT_ID
  16. #define FSL_COMPONENT_ID "platform.drivers.puf"
  17. #endif
  18. /* RT6xx POWER CONTROL bit masks */
  19. #if defined(FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL) && (FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL > 0)
  20. #define PUF_PWRCTRL_CKDIS_MASK (0x4U)
  21. #define PUF_PWRCTRL_RAMINIT_MASK (0x8U)
  22. #define PUF_PWRCTRL_RAMPSWLARGEMA_MASK (0x10U)
  23. #define PUF_PWRCTRL_RAMPSWLARGEMP_MASK (0x20U)
  24. #define PUF_PWRCTRL_RAMPSWSMALLMA_MASK (0x40U)
  25. #define PUF_PWRCTRL_RAMPSWSMALLMP_MASK (0x80U)
  26. #endif
  27. #if defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  28. #define DEFAULT_CKGATING 0x0u
  29. #define PUF_ENABLE_MASK 0xFFFFFFFEu
  30. #define PUF_ENABLE_CTRL 0x1u
  31. #else
  32. static void puf_wait_usec(volatile uint32_t usec, uint32_t coreClockFrequencyMHz)
  33. {
  34. SDK_DelayAtLeastUs(usec, coreClockFrequencyMHz * 1000000U);
  35. /* Instead of calling SDK_DelayAtLeastUs() implement delay loop here */
  36. // while (usec > 0U)
  37. // {
  38. // usec--;
  39. // number of MHz is directly number of core clocks to wait 1 usec.
  40. // the while loop below is actually 4 clocks so divide by 4 for ~1 usec
  41. // volatile uint32_t ticksCount = coreClockFrequencyMHz / 4u + 1u;
  42. // while (0U != ticksCount--)
  43. // {
  44. // }
  45. // }
  46. }
  47. #endif /* defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0) */
  48. static status_t puf_waitForInit(PUF_Type *base)
  49. {
  50. status_t status = kStatus_Fail;
  51. /* wait until status register reads non-zero. All zero is not valid. It should be BUSY or OK or ERROR */
  52. while (0U == base->STAT)
  53. {
  54. }
  55. /* wait if busy */
  56. while ((base->STAT & PUF_STAT_BUSY_MASK) != 0U)
  57. {
  58. }
  59. /* return status */
  60. if (0U != (base->STAT & (PUF_STAT_SUCCESS_MASK | PUF_STAT_ERROR_MASK)))
  61. {
  62. status = kStatus_Success;
  63. }
  64. return status;
  65. }
  66. static void puf_powerOn(PUF_Type *base, puf_config_t *conf)
  67. {
  68. #if defined(FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL) && (FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL > 0)
  69. /* RT6xxs */
  70. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK);
  71. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK);
  72. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_RAMINIT_MASK);
  73. #elif defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  74. /* LPCXpresso55s16 */
  75. conf->puf_sram_base->CFG |= PUF_ENABLE_CTRL;
  76. while (0U == (PUF_SRAM_CTRL_STATUS_READY_MASK & conf->puf_sram_base->STATUS))
  77. {
  78. }
  79. #else /* !FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL */
  80. /* LPCXpresso55s69 & LPCXpresso54S018 */
  81. base->PWRCTRL = PUF_PWRCTRL_RAMON_MASK;
  82. while (0U == (PUF_PWRCTRL_RAMSTAT_MASK & base->PWRCTRL))
  83. {
  84. }
  85. #endif /* FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL */
  86. }
  87. /*!
  88. * brief Powercycle PUF
  89. *
  90. * This function make powercycle of PUF.
  91. *
  92. * param base PUF peripheral base address
  93. * param conf PUF configuration structure
  94. * return Status of the powercycle operation.
  95. */
  96. status_t PUF_PowerCycle(PUF_Type *base, puf_config_t *conf)
  97. {
  98. #if defined(FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL) && (FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL > 0)
  99. /* RT6xxs */
  100. uint32_t coreClockFrequencyMHz = conf->coreClockFrequencyHz / 1000000u;
  101. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK); /* disable RAM CK */
  102. /* enter ASPS mode */
  103. base->PWRCTRL = (PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK); /* SLEEP = 1 */
  104. base->PWRCTRL = (PUF_PWRCTRL_RAMINIT_MASK); /* enable RAM CK */
  105. base->PWRCTRL = (PUF_PWRCTRL_RAMINIT_MASK | PUF_PWRCTRL_RAMPSWLARGEMA_MASK | PUF_PWRCTRL_RAMPSWLARGEMP_MASK |
  106. PUF_PWRCTRL_RAMPSWSMALLMA_MASK | PUF_PWRCTRL_RAMPSWSMALLMP_MASK); /* SLEEP=1, PSW*=1 */
  107. /* Wait enough time to discharge fully */
  108. puf_wait_usec(conf->dischargeTimeMsec * 1000u, conf->coreClockFrequencyHz / 1000000u);
  109. /* write PWRCTRL=0x38. wait time > 1 us */
  110. base->PWRCTRL = (PUF_PWRCTRL_RAMINIT_MASK | PUF_PWRCTRL_RAMPSWLARGEMA_MASK |
  111. PUF_PWRCTRL_RAMPSWLARGEMP_MASK); /* SLEEP=1. PSWSMALL*=0. PSWLARGE*=1. */
  112. puf_wait_usec(1, coreClockFrequencyMHz);
  113. /* write PWRCTRL=0x8. wait time > 1 us */
  114. base->PWRCTRL = PUF_PWRCTRL_RAMINIT_MASK; /* SLEEP=1. PSWSMALL*=0. PSWLARGE*=0 */
  115. puf_wait_usec(1, coreClockFrequencyMHz);
  116. base->PWRCTRL = (PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK);
  117. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK);
  118. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_RAMINIT_MASK);
  119. /* Generate INITN low pulse */
  120. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK);
  121. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK);
  122. base->PWRCTRL = PUF_PWRCTRL_RAM_ON_MASK;
  123. #elif defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  124. /* LPCXpresso55s16 */
  125. conf->puf_sram_base->CFG &= PUF_ENABLE_MASK;
  126. #else
  127. /* LPCXpresso55s69 & LPCXpresso54S018 */
  128. base->PWRCTRL = 0x0u;
  129. while (0U != (PUF_PWRCTRL_RAMSTAT_MASK & base->PWRCTRL))
  130. {
  131. }
  132. /* Wait enough time to discharge fully */
  133. puf_wait_usec(conf->dischargeTimeMsec * 1000u, conf->coreClockFrequencyHz / 1000000u);
  134. #endif
  135. #if !(defined(FSL_FEATURE_PUF_HAS_NO_RESET) && (FSL_FEATURE_PUF_HAS_NO_RESET > 0))
  136. /* Reset PUF and reenable power to PUF SRAM */
  137. RESET_PeripheralReset(kPUF_RST_SHIFT_RSTn);
  138. #endif /* FSL_TEATURE_PUF_HAS_NO_RESET */
  139. puf_powerOn(base, conf);
  140. return kStatus_Success;
  141. }
  142. /*!
  143. * brief Sets the default configuration of PUF
  144. *
  145. * This function initialize PUF config structure to default values.
  146. *
  147. * param conf PUF configuration structure
  148. */
  149. void PUF_GetDefaultConfig(puf_config_t *conf)
  150. {
  151. #if defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  152. /* LPCXpresso55s16 */
  153. conf->puf_sram_base = PUF_SRAM_CTRL;
  154. /* Default configuration after reset */
  155. conf->CKGATING = DEFAULT_CKGATING; /* PUF SRAM Clock Gating */
  156. #endif /* FSL_FEATURE_PUF_HAS_SRAM_CTRL */
  157. conf->dischargeTimeMsec = KEYSTORE_PUF_DISCHARGE_TIME_FIRST_TRY_MS;
  158. conf->coreClockFrequencyHz = CLOCK_GetFreq(kCLOCK_CoreSysClk);
  159. return;
  160. }
  161. /*!
  162. * brief Initialize PUF
  163. *
  164. * This function enables power to PUF block and waits until the block initializes.
  165. *
  166. * param base PUF peripheral base address
  167. * param conf PUF configuration structure
  168. * return Status of the init operation
  169. */
  170. status_t PUF_Init(PUF_Type *base, puf_config_t *conf)
  171. {
  172. status_t status = kStatus_Fail;
  173. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  174. CLOCK_EnableClock(kCLOCK_Puf);
  175. #endif
  176. #if !(defined(FSL_FEATURE_PUF_HAS_NO_RESET) && (FSL_FEATURE_PUF_HAS_NO_RESET > 0))
  177. /* Reset PUF */
  178. RESET_PeripheralReset(kPUF_RST_SHIFT_RSTn);
  179. #endif /* FSL_FEATURE_PUF_HAS_NO_RESET */
  180. #if defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  181. /* Set configuration for SRAM */
  182. conf->puf_sram_base->CFG |= PUF_SRAM_CTRL_CFG_CKGATING(conf->CKGATING);
  183. #endif /* FSL_FEATURE_PUF_HAS_SRAM_CTRL */
  184. /* Enable power to PUF SRAM */
  185. puf_powerOn(base, conf);
  186. /* Wait for peripheral to become ready */
  187. status = puf_waitForInit(base);
  188. /* In case of error or enroll or start not allowed, do power-cycle */
  189. /* First try with shorter discharge time, if then it also fails try with longer time */
  190. /* conf->dischargeTimeMsec = KEYSTORE_PUF_DISCHARGE_TIME_FIRST_TRY_MS; */
  191. if ((status != kStatus_Success) || (0U == (base->ALLOW & (PUF_ALLOW_ALLOWENROLL_MASK | PUF_ALLOW_ALLOWSTART_MASK))))
  192. {
  193. (void)PUF_PowerCycle(base, conf);
  194. status = puf_waitForInit(base);
  195. }
  196. /* In case of error or enroll or start not allowed, do power-cycle with worst discharge timing */
  197. if ((status != kStatus_Success) || (0U == (base->ALLOW & (PUF_ALLOW_ALLOWENROLL_MASK | PUF_ALLOW_ALLOWSTART_MASK))))
  198. {
  199. conf->dischargeTimeMsec = KEYSTORE_PUF_DISCHARGE_TIME_MAX_MS;
  200. (void)PUF_PowerCycle(base, conf);
  201. status = puf_waitForInit(base);
  202. }
  203. return status;
  204. }
  205. /*!
  206. * brief Denitialize PUF
  207. *
  208. * This function disables power to PUF SRAM and peripheral clock.
  209. *
  210. * param base PUF peripheral base address
  211. * param conf PUF configuration structure
  212. */
  213. void PUF_Deinit(PUF_Type *base, puf_config_t *conf)
  214. {
  215. #if defined(FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL) && (FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL > 0)
  216. /* RT6xxs */
  217. base->PWRCTRL = (PUF_PWRCTRL_RAM_ON_MASK | PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK); /* disable RAM CK */
  218. /* enter ASPS mode */
  219. base->PWRCTRL = (PUF_PWRCTRL_CK_DIS_MASK | PUF_PWRCTRL_RAMINIT_MASK); /* SLEEP = 1 */
  220. base->PWRCTRL = PUF_PWRCTRL_RAMINIT_MASK; /* enable RAM CK */
  221. base->PWRCTRL = (PUF_PWRCTRL_RAMINIT_MASK | PUF_PWRCTRL_RAMPSWLARGEMA_MASK | PUF_PWRCTRL_RAMPSWLARGEMP_MASK |
  222. PUF_PWRCTRL_RAMPSWSMALLMA_MASK | PUF_PWRCTRL_RAMPSWSMALLMP_MASK); /* SLEEP=1, PSW*=1 */
  223. puf_wait_usec(conf->dischargeTimeMsec * 1000u, conf->coreClockFrequencyHz / 1000000u);
  224. #elif defined(FSL_FEATURE_PUF_HAS_SRAM_CTRL) && (FSL_FEATURE_PUF_HAS_SRAM_CTRL > 0)
  225. /* LPCXpresso55s16 */
  226. conf->puf_sram_base = PUF_SRAM_CTRL;
  227. conf->puf_sram_base->CFG &= PUF_ENABLE_MASK;
  228. #else /* !FSL_FEATURE_PUF_PWR_HAS_MANUAL_SLEEP_CONTROL */
  229. /* LPCXpresso55s69 & LPCXpresso54S018 */
  230. base->PWRCTRL = 0x00u;
  231. puf_wait_usec(conf->dischargeTimeMsec * 1000u, conf->coreClockFrequencyHz / 1000000u);
  232. #endif
  233. #if !(defined(FSL_FEATURE_PUF_HAS_NO_RESET) && (FSL_FEATURE_PUF_HAS_NO_RESET > 0))
  234. RESET_SetPeripheralReset(kPUF_RST_SHIFT_RSTn);
  235. #endif /* FSL_FEATURE_PUF_HAS_NO_RESET */
  236. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  237. CLOCK_DisableClock(kCLOCK_Puf);
  238. #endif
  239. }
  240. /*!
  241. * brief Enroll PUF
  242. *
  243. * This function derives a digital fingerprint, generates the corresponding Activation Code (AC)
  244. * and returns it to be stored in an NVM or a file. This step needs to be
  245. * performed only once for each device. This function may be permanently disallowed by a fuse.
  246. *
  247. * param base PUF peripheral base address
  248. * param[out] activationCode Word aligned address of the resulting activation code.
  249. * param activationCodeSize Size of the activationCode buffer in bytes. Shall be 1192 bytes.
  250. * return Status of enroll operation.
  251. */
  252. status_t PUF_Enroll(PUF_Type *base, uint8_t *activationCode, size_t activationCodeSize)
  253. {
  254. status_t status = kStatus_Fail;
  255. uint32_t *activationCodeAligned = NULL;
  256. register uint32_t temp32 = 0;
  257. /* check that activation code buffer size is at least 1192 bytes */
  258. if (activationCodeSize < PUF_ACTIVATION_CODE_SIZE)
  259. {
  260. return kStatus_InvalidArgument;
  261. }
  262. /* only work with aligned activationCode */
  263. if (0U != (0x3u & (uintptr_t)activationCode))
  264. {
  265. return kStatus_InvalidArgument;
  266. }
  267. activationCodeAligned = (uint32_t *)(uintptr_t)activationCode;
  268. /* check if ENROLL is allowed */
  269. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWENROLL_MASK))
  270. {
  271. return kStatus_EnrollNotAllowed;
  272. }
  273. /* begin */
  274. base->CTRL = PUF_CTRL_ENROLL_MASK;
  275. /* check status */
  276. while (0U == (base->STAT & (PUF_STAT_BUSY_MASK | PUF_STAT_ERROR_MASK)))
  277. {
  278. }
  279. /* read out AC */
  280. while (0U != (base->STAT & PUF_STAT_BUSY_MASK))
  281. {
  282. if (0U != (PUF_STAT_CODEOUTAVAIL_MASK & base->STAT))
  283. {
  284. temp32 = base->CODEOUTPUT;
  285. if (activationCodeSize >= sizeof(uint32_t))
  286. {
  287. *activationCodeAligned = temp32;
  288. activationCodeAligned++;
  289. activationCodeSize -= sizeof(uint32_t);
  290. }
  291. }
  292. }
  293. if (((base->STAT & PUF_STAT_SUCCESS_MASK) != 0U) && (activationCodeSize == 0U))
  294. {
  295. status = kStatus_Success;
  296. }
  297. return status;
  298. }
  299. /*!
  300. * brief Start PUF
  301. *
  302. * The Activation Code generated during the Enroll operation is used to
  303. * reconstruct the digital fingerprint. This needs to be done after every power-up
  304. * and reset.
  305. *
  306. * param base PUF peripheral base address
  307. * param activationCode Word aligned address of the input activation code.
  308. * param activationCodeSize Size of the activationCode buffer in bytes. Shall be 1192 bytes.
  309. * return Status of start operation.
  310. */
  311. status_t PUF_Start(PUF_Type *base, const uint8_t *activationCode, size_t activationCodeSize)
  312. {
  313. status_t status = kStatus_Fail;
  314. const uint32_t *activationCodeAligned = NULL;
  315. register uint32_t temp32 = 0;
  316. /* check that activation code size is at least 1192 bytes */
  317. if (activationCodeSize < 1192U)
  318. {
  319. return kStatus_InvalidArgument;
  320. }
  321. /* only work with aligned activationCode */
  322. if (0U != (0x3u & (uintptr_t)activationCode))
  323. {
  324. return kStatus_InvalidArgument;
  325. }
  326. activationCodeAligned = (const uint32_t *)(uintptr_t)activationCode;
  327. /* check if START is allowed */
  328. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWSTART_MASK))
  329. {
  330. return kStatus_StartNotAllowed;
  331. }
  332. /* begin */
  333. base->CTRL = PUF_CTRL_START_MASK;
  334. /* check status */
  335. while (0U == (base->STAT & (PUF_STAT_BUSY_MASK | PUF_STAT_ERROR_MASK)))
  336. {
  337. }
  338. /* while busy send AC */
  339. while (0U != (base->STAT & PUF_STAT_BUSY_MASK))
  340. {
  341. if (0U != (PUF_STAT_CODEINREQ_MASK & base->STAT))
  342. {
  343. if (activationCodeSize >= sizeof(uint32_t))
  344. {
  345. temp32 = *activationCodeAligned;
  346. activationCodeAligned++;
  347. activationCodeSize -= sizeof(uint32_t);
  348. }
  349. base->CODEINPUT = temp32;
  350. }
  351. }
  352. /* get status */
  353. if (0U != (base->STAT & PUF_STAT_SUCCESS_MASK))
  354. {
  355. status = kStatus_Success;
  356. }
  357. return status;
  358. }
  359. /*!
  360. * brief Set intrinsic key
  361. *
  362. * The digital fingerprint generated during the Enroll/Start
  363. * operations is used to generate a Key Code (KC) that defines a unique intrinsic
  364. * key. This KC is returned to be stored in an NVM or a file. This operation
  365. * needs to be done only once for each intrinsic key.
  366. * Each time a Set Intrinsic Key operation is executed a new unique key is
  367. * generated.
  368. *
  369. * param base PUF peripheral base address
  370. * param keyIndex PUF key index register
  371. * param keySize Size of the intrinsic key to generate in bytes.
  372. * param[out] keyCode Word aligned address of the resulting key code.
  373. * param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize).
  374. * return Status of set intrinsic key operation.
  375. */
  376. status_t PUF_SetIntrinsicKey(
  377. PUF_Type *base, puf_key_index_register_t keyIndex, size_t keySize, uint8_t *keyCode, size_t keyCodeSize)
  378. {
  379. status_t status = kStatus_Fail;
  380. uint32_t *keyCodeAligned = NULL;
  381. register uint32_t temp32 = 0;
  382. /* check if SET KEY is allowed */
  383. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWSETKEY_MASK))
  384. {
  385. return kStatus_Fail;
  386. }
  387. /* only work with aligned keyCode */
  388. if (0U != (0x3u & (uintptr_t)keyCode))
  389. {
  390. return kStatus_InvalidArgument;
  391. }
  392. /* Check that keySize is in the correct range and that it is multiple of 8 */
  393. if ((keySize < (uint32_t)kPUF_KeySizeMin) || (keySize > (uint32_t)kPUF_KeySizeMax) || (0U != (keySize & 0x7U)))
  394. {
  395. return kStatus_InvalidArgument;
  396. }
  397. /* check that keyCodeSize is correct for given keySize */
  398. if (keyCodeSize < PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize))
  399. {
  400. return kStatus_InvalidArgument;
  401. }
  402. if ((uint32_t)keyIndex > (uint32_t)kPUF_KeyIndexMax)
  403. {
  404. return kStatus_InvalidArgument;
  405. }
  406. keyCodeAligned = (uint32_t *)(uintptr_t)keyCode;
  407. /* program the key size and index */
  408. base->KEYSIZE = keySize >> 3;
  409. base->KEYINDEX = (uint32_t)keyIndex;
  410. /* begin */
  411. base->CTRL = PUF_CTRL_GENERATEKEY_MASK;
  412. /* wait till command is accepted */
  413. while (0U == (base->STAT & (PUF_STAT_BUSY_MASK | PUF_STAT_ERROR_MASK)))
  414. {
  415. }
  416. /* while busy read KC */
  417. while (0U != (base->STAT & PUF_STAT_BUSY_MASK))
  418. {
  419. if (0U != (PUF_STAT_CODEOUTAVAIL_MASK & base->STAT))
  420. {
  421. temp32 = base->CODEOUTPUT;
  422. if (keyCodeSize >= sizeof(uint32_t))
  423. {
  424. *keyCodeAligned = temp32;
  425. keyCodeAligned++;
  426. keyCodeSize -= sizeof(uint32_t);
  427. }
  428. }
  429. }
  430. /* get status */
  431. if (0U != (base->STAT & PUF_STAT_SUCCESS_MASK))
  432. {
  433. status = kStatus_Success;
  434. }
  435. return status;
  436. }
  437. /*!
  438. * brief Set user key
  439. *
  440. * The digital fingerprint generated during the Enroll/Start
  441. * operations and a user key (UK) provided as input are used to
  442. * generate a Key Code (KC). This KC is sent returned to be stored
  443. * in an NVM or a file. This operation needs to be done only once for each user key.
  444. *
  445. * param base PUF peripheral base address
  446. * param keyIndex PUF key index register
  447. * param userKey Word aligned address of input user key.
  448. * param userKeySize Size of the input user key in bytes.
  449. * param[out] keyCode Word aligned address of the resulting key code.
  450. * param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(userKeySize).
  451. * return Status of set user key operation.
  452. */
  453. status_t PUF_SetUserKey(PUF_Type *base,
  454. puf_key_index_register_t keyIndex,
  455. const uint8_t *userKey,
  456. size_t userKeySize,
  457. uint8_t *keyCode,
  458. size_t keyCodeSize)
  459. {
  460. status_t status = kStatus_Fail;
  461. uint32_t *keyCodeAligned = NULL;
  462. const uint32_t *userKeyAligned = NULL;
  463. register uint32_t temp32 = 0;
  464. /* check if SET KEY is allowed */
  465. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWSETKEY_MASK))
  466. {
  467. return kStatus_Fail;
  468. }
  469. /* only work with aligned keyCode */
  470. if (0U != (0x3u & (uintptr_t)keyCode))
  471. {
  472. return kStatus_InvalidArgument;
  473. }
  474. /* Check that userKeySize is in the correct range and that it is multiple of 8 */
  475. if ((userKeySize < (uint32_t)kPUF_KeySizeMin) || (userKeySize > (uint32_t)kPUF_KeySizeMax) ||
  476. (0U != (userKeySize & 0x7U)))
  477. {
  478. return kStatus_InvalidArgument;
  479. }
  480. /* check that keyCodeSize is correct for given userKeySize */
  481. if (keyCodeSize < PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(userKeySize))
  482. {
  483. return kStatus_InvalidArgument;
  484. }
  485. if ((uint32_t)keyIndex > (uint32_t)kPUF_KeyIndexMax)
  486. {
  487. return kStatus_InvalidArgument;
  488. }
  489. keyCodeAligned = (uint32_t *)(uintptr_t)keyCode;
  490. userKeyAligned = (const uint32_t *)(uintptr_t)userKey;
  491. /* program the key size and index */
  492. base->KEYSIZE = userKeySize >> 3; /* convert to 64-bit blocks */
  493. base->KEYINDEX = (uint32_t)keyIndex;
  494. /* We have to store the user key on index 0 swaped for HW bus */
  495. if (keyIndex == kPUF_KeyIndex_00)
  496. {
  497. userKeyAligned = userKeyAligned + (userKeySize / sizeof(uint32_t));
  498. }
  499. /* begin */
  500. base->CTRL = PUF_CTRL_SETKEY_MASK;
  501. /* wait till command is accepted */
  502. while (0U == (base->STAT & (PUF_STAT_BUSY_MASK | PUF_STAT_ERROR_MASK)))
  503. {
  504. }
  505. /* while busy write UK and read KC */
  506. while (0U != (base->STAT & PUF_STAT_BUSY_MASK))
  507. {
  508. if (0U != (PUF_STAT_KEYINREQ_MASK & base->STAT))
  509. {
  510. if (userKeySize >= sizeof(uint32_t))
  511. {
  512. #if defined(LPC54S018_SERIES)
  513. if (keyIndex == kPUF_KeyIndex_00)
  514. {
  515. userKeyAligned--;
  516. temp32 = *userKeyAligned;
  517. userKeySize -= sizeof(uint32_t);
  518. }
  519. #else
  520. if (keyIndex == kPUF_KeyIndex_00)
  521. {
  522. userKeyAligned--;
  523. temp32 = __REV(*userKeyAligned);
  524. userKeySize--;
  525. }
  526. #endif /* defined(LPC54S018_SERIES) */
  527. else if (keyIndex != kPUF_KeyIndex_00)
  528. {
  529. temp32 = *userKeyAligned;
  530. userKeyAligned++;
  531. userKeySize -= sizeof(uint32_t);
  532. }
  533. else
  534. {
  535. /* Intentional empty */
  536. }
  537. }
  538. base->KEYINPUT = temp32;
  539. }
  540. if (0U != (PUF_STAT_CODEOUTAVAIL_MASK & base->STAT))
  541. {
  542. temp32 = base->CODEOUTPUT;
  543. if (keyCodeSize >= sizeof(uint32_t))
  544. {
  545. *keyCodeAligned = temp32;
  546. keyCodeAligned++;
  547. keyCodeSize -= sizeof(uint32_t);
  548. }
  549. }
  550. }
  551. /* get status */
  552. if (0U != (base->STAT & PUF_STAT_SUCCESS_MASK))
  553. {
  554. status = kStatus_Success;
  555. }
  556. return status;
  557. }
  558. static status_t puf_getHwKey(PUF_Type *base, const uint8_t *keyCode, size_t keyCodeSize)
  559. {
  560. status_t status = kStatus_Fail;
  561. uint32_t *keyCodeAligned = NULL;
  562. register uint32_t temp32 = 0;
  563. keyCodeAligned = (uint32_t *)(uintptr_t)keyCode;
  564. /* begin */
  565. base->CTRL = PUF_CTRL_GETKEY_MASK;
  566. /* wait till command is accepted */
  567. while (0U == (base->STAT & (PUF_STAT_BUSY_MASK | PUF_STAT_ERROR_MASK)))
  568. {
  569. }
  570. /* while busy send KC, key is reconstructed to HW bus */
  571. while (0U != (base->STAT & PUF_STAT_BUSY_MASK))
  572. {
  573. if (0U != (PUF_STAT_CODEINREQ_MASK & base->STAT))
  574. {
  575. if (keyCodeSize >= sizeof(uint32_t))
  576. {
  577. temp32 = *keyCodeAligned;
  578. keyCodeAligned++;
  579. keyCodeSize -= sizeof(uint32_t);
  580. }
  581. base->CODEINPUT = temp32;
  582. }
  583. }
  584. /* get status */
  585. if (0U != (base->STAT & PUF_STAT_SUCCESS_MASK))
  586. {
  587. status = kStatus_Success;
  588. }
  589. return status;
  590. }
  591. /*!
  592. * brief Reconstruct hw bus key from a key code
  593. *
  594. * The digital fingerprint generated during the Start operation and the KC
  595. * generated during a Set Key operation (Set intrinsic key or Set user key) are used to retrieve a stored key. This
  596. * operation needs to be done every time a key is needed.
  597. * This function accepts only Key Codes created for PUF index register kPUF_KeyIndex_00.
  598. * Such a key is output directly to a dedicated hardware bus. The reconstructed key is not exposed to system memory.
  599. *
  600. * param base PUF peripheral base address
  601. * param keyCode Word aligned address of the input key code.
  602. * param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize).
  603. * param keySlot key slot to output on hw bus. Parameter is ignored on devices with less than two key slots.
  604. * param keyMask key masking value. Shall be random for each POR/reset. Value does not have to be cryptographicaly
  605. * secure.
  606. * return Status of get key operation.
  607. */
  608. status_t PUF_GetHwKey(
  609. PUF_Type *base, const uint8_t *keyCode, size_t keyCodeSize, puf_key_slot_t keySlot, uint32_t keyMask)
  610. {
  611. status_t status = kStatus_Fail;
  612. uint32_t keyIndex;
  613. /* check if GET KEY is allowed */
  614. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWGETKEY_MASK))
  615. {
  616. return kStatus_Fail;
  617. }
  618. /* only work with aligned keyCode */
  619. if (0U != (0x3u & (uintptr_t)keyCode))
  620. {
  621. return kStatus_Fail;
  622. }
  623. /* check that keyCodeSize is at least PUF_MIN_KEY_CODE_SIZE */
  624. if (keyCodeSize < PUF_MIN_KEY_CODE_SIZE)
  625. {
  626. return kStatus_InvalidArgument;
  627. }
  628. keyIndex = (uint32_t)(0x0Fu & (uint32_t)keyCode[1]);
  629. /* check the Key Code header byte 1. index must be zero for the hw key. */
  630. if (kPUF_KeyIndex_00 != (puf_key_index_register_t)keyIndex)
  631. {
  632. return kStatus_Fail;
  633. }
  634. #if defined(PUF_KEYMASK_COUNT) && (PUF_KEYMASK_COUNT > 0)
  635. volatile uint32_t *keyMask_reg = NULL;
  636. uint32_t regVal = ((uint32_t)2U << ((uint32_t)2U * (uint32_t)keySlot));
  637. switch (keySlot)
  638. {
  639. case kPUF_KeySlot0:
  640. keyMask_reg = &base->KEYMASK[0];
  641. break;
  642. case kPUF_KeySlot1:
  643. keyMask_reg = &base->KEYMASK[1];
  644. break;
  645. #if (PUF_KEYMASK_COUNT > 2)
  646. case kPUF_KeySlot2:
  647. keyMask_reg = &base->KEYMASK[2];
  648. break;
  649. case kPUF_KeySlot3:
  650. keyMask_reg = &base->KEYMASK[3];
  651. break;
  652. #endif /* PUF_KEYMASK_COUNT > 2 */
  653. default:
  654. status = kStatus_InvalidArgument;
  655. break;
  656. }
  657. #endif /* PUF_KEYMASK_COUNT */
  658. if (status != kStatus_InvalidArgument)
  659. {
  660. #if defined(PUF_KEYMASK_COUNT) && (PUF_KEYMASK_COUNT > 0)
  661. base->KEYRESET = regVal;
  662. base->KEYENABLE = regVal;
  663. *keyMask_reg = keyMask;
  664. #endif /* FSL_FEATURE_PUF_HAS_KEYSLOTS */
  665. status = puf_getHwKey(base, keyCode, keyCodeSize);
  666. #if defined(FSL_FEATURE_PUF_HAS_SHIFT_STATUS) && (FSL_FEATURE_PUF_HAS_SHIFT_STATUS > 0)
  667. size_t keyWords = 0;
  668. if (status == kStatus_Success)
  669. {
  670. /* if the corresponding shift count does not match, return fail anyway */
  671. keyWords = ((((size_t)keyCode[3]) * 2U) - 1u) << ((size_t)keySlot << 2U);
  672. if (keyWords != ((0x0FUL << ((uint32_t)keySlot << 2U)) & base->SHIFT_STATUS))
  673. {
  674. status = kStatus_Fail;
  675. }
  676. }
  677. #elif defined(PUF_IDXBLK_SHIFT_IND_KEY0_MASK) && PUF_IDXBLK_SHIFT_IND_KEY0_MASK
  678. size_t keyWords = 0;
  679. if (status == kStatus_Success)
  680. {
  681. /* if the corresponding shift count does not match, return fail anyway */
  682. keyWords = ((((size_t)keyCode[3]) * 2U) - 1u) << ((size_t)keySlot << 2U);
  683. if (keyWords != ((0x0FUL << ((uint32_t)keySlot << 2U)) & base->IDXBLK_SHIFT))
  684. {
  685. status = kStatus_Fail;
  686. }
  687. }
  688. #endif /* FSL_FEATURE_PUF_HAS_SHIFT_STATUS || PUF_IDXBLK_SHIFT_IND_KEY0_MASK */
  689. }
  690. return status;
  691. }
  692. /*!
  693. * brief Checks if Get Key operation is allowed.
  694. *
  695. * This function returns true if get key operation is allowed.
  696. *
  697. * param base PUF peripheral base address
  698. * return true if get key operation is allowed
  699. */
  700. bool PUF_IsGetKeyAllowed(PUF_Type *base)
  701. {
  702. /* check if GET KEY is allowed */
  703. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWGETKEY_MASK))
  704. {
  705. return false;
  706. }
  707. return true;
  708. }
  709. /*!
  710. * brief Reconstruct key from a key code
  711. *
  712. * The digital fingerprint generated during the Start operation and the KC
  713. * generated during a Set Key operation (Set intrinsic key or Set user key) are used to retrieve a stored key. This
  714. * operation needs to be done every time a key is needed.
  715. * This function accepts only Key Codes created for PUF index registers kPUF_KeyIndex_01 to kPUF_KeyIndex_15.
  716. *
  717. * param base PUF peripheral base address
  718. * param keyCode Word aligned address of the input key code.
  719. * param keyCodeSize Size of the keyCode buffer in bytes. Shall be PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize).
  720. * param[out] key Word aligned address of output key.
  721. * param keySize Size of the output key in bytes.
  722. * return Status of get key operation.
  723. */
  724. status_t PUF_GetKey(PUF_Type *base, const uint8_t *keyCode, size_t keyCodeSize, uint8_t *key, size_t keySize)
  725. {
  726. status_t status = kStatus_Fail;
  727. uint32_t *keyCodeAligned = NULL;
  728. uint32_t *keyAligned = NULL;
  729. uint32_t keyIndex;
  730. register uint32_t temp32 = 0;
  731. /* check if GET KEY is allowed */
  732. if (0x0u == (base->ALLOW & PUF_ALLOW_ALLOWGETKEY_MASK))
  733. {
  734. return kStatus_Fail;
  735. }
  736. /* only work with aligned keyCode */
  737. if (0U != (0x3u & (uintptr_t)keyCode))
  738. {
  739. return kStatus_Fail;
  740. }
  741. /* only work with aligned key */
  742. if (0U != (0x3u & (uintptr_t)key))
  743. {
  744. return kStatus_Fail;
  745. }
  746. /* check that keyCodeSize is correct for given keySize */
  747. if (keyCodeSize < PUF_GET_KEY_CODE_SIZE_FOR_KEY_SIZE(keySize))
  748. {
  749. return kStatus_InvalidArgument;
  750. }
  751. keyIndex = (0x0Fu & (uint32_t)keyCode[1]);
  752. /* check the Key Code header byte 1. index must be non-zero for the register key. */
  753. if (kPUF_KeyIndex_00 == (puf_key_index_register_t)keyIndex)
  754. {
  755. return kStatus_Fail;
  756. }
  757. keyCodeAligned = (uint32_t *)(uintptr_t)keyCode;
  758. keyAligned = (uint32_t *)(uintptr_t)key;
  759. /* begin */
  760. base->CTRL = PUF_CTRL_GETKEY_MASK;
  761. /* wait till command is accepted */
  762. while (0U == (base->STAT & (PUF_STAT_BUSY_MASK | PUF_STAT_ERROR_MASK)))
  763. {
  764. }
  765. /* while busy send KC, read key */
  766. while (0U != (base->STAT & PUF_STAT_BUSY_MASK))
  767. {
  768. if (0U != (PUF_STAT_CODEINREQ_MASK & base->STAT))
  769. {
  770. temp32 = 0;
  771. if (keyCodeSize >= sizeof(uint32_t))
  772. {
  773. temp32 = *keyCodeAligned;
  774. keyCodeAligned++;
  775. keyCodeSize -= sizeof(uint32_t);
  776. }
  777. base->CODEINPUT = temp32;
  778. }
  779. if (0U != (PUF_STAT_KEYOUTAVAIL_MASK & base->STAT))
  780. {
  781. keyIndex = base->KEYOUTINDEX;
  782. temp32 = base->KEYOUTPUT;
  783. if (keySize >= sizeof(uint32_t))
  784. {
  785. *keyAligned = temp32;
  786. keyAligned++;
  787. keySize -= sizeof(uint32_t);
  788. }
  789. }
  790. }
  791. /* get status */
  792. if ((keyIndex != 0U) && (0U != (base->STAT & PUF_STAT_SUCCESS_MASK)))
  793. {
  794. status = kStatus_Success;
  795. }
  796. return status;
  797. }
  798. /*!
  799. * brief Zeroize PUF
  800. *
  801. * This function clears all PUF internal logic and puts the PUF to error state.
  802. *
  803. * param base PUF peripheral base address
  804. * return Status of the zeroize operation.
  805. */
  806. status_t PUF_Zeroize(PUF_Type *base)
  807. {
  808. status_t status = kStatus_Fail;
  809. /* zeroize command is always allowed */
  810. base->CTRL = PUF_CTRL_ZEROIZE_MASK;
  811. /* check that command is accepted */
  812. if ((0U != (base->STAT & PUF_STAT_ERROR_MASK)) && (0U == base->ALLOW))
  813. {
  814. status = kStatus_Success;
  815. }
  816. return status;
  817. }