fsl_trng.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2018, 2020-2021 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_TRNG_DRIVER_H_
  9. #define _FSL_TRNG_DRIVER_H_
  10. #include "fsl_common.h"
  11. #if defined(FSL_FEATURE_SOC_TRNG_COUNT) && FSL_FEATURE_SOC_TRNG_COUNT
  12. /*!
  13. * @addtogroup trng
  14. * @{
  15. */
  16. /*******************************************************************************
  17. * Definitions
  18. *******************************************************************************/
  19. /*! @name Driver version */
  20. /*@{*/
  21. /*! @brief TRNG driver version 2.0.13.
  22. *
  23. * Current version: 2.0.13
  24. *
  25. * Change log:
  26. * - version 2.0.13
  27. * - After deepsleep it might return error, added clearing bits in TRNG_GetRandomData() and generating new entropy.
  28. * - Modified reloading entropy in TRNG_GetRandomData(), for some data length it doesn't reloading entropy correctly.
  29. * - version 2.0.12
  30. * - For KW34A4_SERIES, KW35A4_SERIES, KW36A4_SERIES set TRNG_USER_CONFIG_DEFAULT_OSC_DIV to kTRNG_RingOscDiv8.
  31. * - version 2.0.11
  32. * - Add clearing pending errors in TRNG_Init().
  33. * - version 2.0.10
  34. * - Fixed doxygen issues.
  35. * - version 2.0.9
  36. * - Fix HIS_CCM metrics issues.
  37. * - version 2.0.8
  38. * - For K32L2A41A_SERIES set TRNG_USER_CONFIG_DEFAULT_OSC_DIV to kTRNG_RingOscDiv4.
  39. * - version 2.0.7
  40. * - Fix MISRA 2004 issue rule 12.5.
  41. * - version 2.0.6
  42. * - For KW35Z4_SERIES set TRNG_USER_CONFIG_DEFAULT_OSC_DIV to kTRNG_RingOscDiv8.
  43. * - version 2.0.5
  44. * - Add possibility to define default TRNG configuration by device specific preprocessor macros
  45. * for FRQMIN, FRQMAX and OSCDIV.
  46. * - version 2.0.4
  47. * - Fix MISRA-2012 issues.
  48. * - Version 2.0.3
  49. * - update TRNG_Init to restart entropy generation
  50. * - Version 2.0.2
  51. * - fix MISRA issues
  52. * - Version 2.0.1
  53. * - add support for KL8x and KL28Z
  54. * - update default OSCDIV for K81 to divide by 2
  55. */
  56. #define FSL_TRNG_DRIVER_VERSION (MAKE_VERSION(2, 0, 13))
  57. /*@}*/
  58. /*! @brief TRNG sample mode. Used by trng_config_t. */
  59. typedef enum _trng_sample_mode
  60. {
  61. kTRNG_SampleModeVonNeumann = 0U, /*!< Use von Neumann data in both Entropy shifter and Statistical Checker. */
  62. kTRNG_SampleModeRaw = 1U, /*!< Use raw data into both Entropy shifter and Statistical Checker. */
  63. kTRNG_SampleModeVonNeumannRaw =
  64. 2U /*!< Use von Neumann data in Entropy shifter. Use raw data into Statistical Checker. */
  65. } trng_sample_mode_t;
  66. /*! @brief TRNG clock mode. Used by trng_config_t. */
  67. typedef enum _trng_clock_mode
  68. {
  69. kTRNG_ClockModeRingOscillator = 0U, /*!< Ring oscillator is used to operate the TRNG (default). */
  70. kTRNG_ClockModeSystem = 1U /*!< System clock is used to operate the TRNG. This is for test use only, and
  71. indeterminate results may occur. */
  72. } trng_clock_mode_t;
  73. /*! @brief TRNG ring oscillator divide. Used by trng_config_t. */
  74. typedef enum _trng_ring_osc_div
  75. {
  76. kTRNG_RingOscDiv0 = 0U, /*!< Ring oscillator with no divide */
  77. kTRNG_RingOscDiv2 = 1U, /*!< Ring oscillator divided-by-2. */
  78. kTRNG_RingOscDiv4 = 2U, /*!< Ring oscillator divided-by-4. */
  79. kTRNG_RingOscDiv8 = 3U /*!< Ring oscillator divided-by-8. */
  80. } trng_ring_osc_div_t;
  81. /*! @brief Data structure for definition of statistical check limits. Used by trng_config_t. */
  82. typedef struct _trng_statistical_check_limit
  83. {
  84. uint32_t maximum; /*!< Maximum limit.*/
  85. uint32_t minimum; /*!< Minimum limit.*/
  86. } trng_statistical_check_limit_t;
  87. /*!
  88. * @brief Data structure for the TRNG initialization
  89. *
  90. * This structure initializes the TRNG by calling the TRNG_Init() function.
  91. * It contains all TRNG configurations.
  92. */
  93. typedef struct _trng_user_config
  94. {
  95. bool lock; /*!< @brief Disable programmability of TRNG registers. */
  96. trng_clock_mode_t clockMode; /*!< @brief Clock mode used to operate TRNG.*/
  97. trng_ring_osc_div_t ringOscDiv; /*!< @brief Ring oscillator divide used by TRNG. */
  98. trng_sample_mode_t sampleMode; /*!< @brief Sample mode of the TRNG ring oscillator. */
  99. /* Seed Control*/
  100. uint16_t
  101. entropyDelay; /*!< @brief Entropy Delay. Defines the length (in system clocks) of each Entropy sample taken. */
  102. uint16_t sampleSize; /*!< @brief Sample Size. Defines the total number of Entropy samples that will be taken during
  103. Entropy generation. */
  104. uint16_t sparseBitLimit; /*!< @brief Sparse Bit Limit which defines the maximum number of
  105. * consecutive samples that may be discarded before an error is generated.
  106. * This limit is used only for during von Neumann sampling (enabled by
  107. * TRNG_HAL_SetSampleMode()). Samples are discarded if two consecutive raw samples are both
  108. * 0 or both 1. If this discarding occurs for a long period of time, it indicates that
  109. * there is insufficient Entropy. */
  110. /* Statistical Check Parameters.*/
  111. uint8_t retryCount; /*!< @brief Retry count. It defines the number of times a statistical check may fails
  112. * during the TRNG Entropy Generation before generating an error. */
  113. uint8_t longRunMaxLimit; /*!< @brief Largest allowable number of consecutive samples of all 1, or all 0,
  114. * that is allowed during the Entropy generation. */
  115. trng_statistical_check_limit_t monobitLimit; /*!< @brief Maximum and minimum limits for statistical check of number
  116. of ones/zero detected during entropy generation. */
  117. trng_statistical_check_limit_t runBit1Limit; /*!< @brief Maximum and minimum limits for statistical check of number
  118. of runs of length 1 detected during entropy generation. */
  119. trng_statistical_check_limit_t runBit2Limit; /*!< @brief Maximum and minimum limits for statistical check of number
  120. of runs of length 2 detected during entropy generation. */
  121. trng_statistical_check_limit_t runBit3Limit; /*!< @brief Maximum and minimum limits for statistical check of number
  122. of runs of length 3 detected during entropy generation. */
  123. trng_statistical_check_limit_t runBit4Limit; /*!< @brief Maximum and minimum limits for statistical check of number
  124. of runs of length 4 detected during entropy generation. */
  125. trng_statistical_check_limit_t runBit5Limit; /*!< @brief Maximum and minimum limits for statistical check of number
  126. of runs of length 5 detected during entropy generation. */
  127. trng_statistical_check_limit_t runBit6PlusLimit; /*!< @brief Maximum and minimum limits for statistical check of
  128. number of runs of length 6 or more detected during entropy
  129. generation. */
  130. trng_statistical_check_limit_t
  131. pokerLimit; /*!< @brief Maximum and minimum limits for statistical check of "Poker Test". */
  132. trng_statistical_check_limit_t frequencyCountLimit; /*!< @brief Maximum and minimum limits for statistical check of
  133. entropy sample frequency count. */
  134. } trng_config_t;
  135. /*******************************************************************************
  136. * API
  137. *******************************************************************************/
  138. #if defined(__cplusplus)
  139. extern "C" {
  140. #endif
  141. /*!
  142. * @brief Initializes the user configuration structure to default values.
  143. *
  144. * This function initializes the configuration structure to default values. The default
  145. * values are as follows.
  146. * @code
  147. * userConfig->lock = 0;
  148. * userConfig->clockMode = kTRNG_ClockModeRingOscillator;
  149. * userConfig->ringOscDiv = kTRNG_RingOscDiv0; Or to other kTRNG_RingOscDiv[2|8] depending on the platform.
  150. * userConfig->sampleMode = kTRNG_SampleModeRaw;
  151. * userConfig->entropyDelay = 3200;
  152. * userConfig->sampleSize = 2500;
  153. * userConfig->sparseBitLimit = TRNG_USER_CONFIG_DEFAULT_SPARSE_BIT_LIMIT;
  154. * userConfig->retryCount = 63;
  155. * userConfig->longRunMaxLimit = 34;
  156. * userConfig->monobitLimit.maximum = 1384;
  157. * userConfig->monobitLimit.minimum = 1116;
  158. * userConfig->runBit1Limit.maximum = 405;
  159. * userConfig->runBit1Limit.minimum = 227;
  160. * userConfig->runBit2Limit.maximum = 220;
  161. * userConfig->runBit2Limit.minimum = 98;
  162. * userConfig->runBit3Limit.maximum = 125;
  163. * userConfig->runBit3Limit.minimum = 37;
  164. * userConfig->runBit4Limit.maximum = 75;
  165. * userConfig->runBit4Limit.minimum = 11;
  166. * userConfig->runBit5Limit.maximum = 47;
  167. * userConfig->runBit5Limit.minimum = 1;
  168. * userConfig->runBit6PlusLimit.maximum = 47;
  169. * userConfig->runBit6PlusLimit.minimum = 1;
  170. * userConfig->pokerLimit.maximum = 26912;
  171. * userConfig->pokerLimit.minimum = 24445;
  172. * userConfig->frequencyCountLimit.maximum = 25600;
  173. * userConfig->frequencyCountLimit.minimum = 1600;
  174. * @endcode
  175. *
  176. * @param userConfig User configuration structure.
  177. * @return If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
  178. */
  179. status_t TRNG_GetDefaultConfig(trng_config_t *userConfig);
  180. /*!
  181. * @brief Initializes the TRNG.
  182. *
  183. * This function initializes the TRNG.
  184. * When called, the TRNG entropy generation starts immediately.
  185. *
  186. * @param base TRNG base address
  187. * @param userConfig Pointer to the initialization configuration structure.
  188. * @return If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
  189. */
  190. status_t TRNG_Init(TRNG_Type *base, const trng_config_t *userConfig);
  191. /*!
  192. * @brief Shuts down the TRNG.
  193. *
  194. * This function shuts down the TRNG.
  195. *
  196. * @param base TRNG base address.
  197. */
  198. void TRNG_Deinit(TRNG_Type *base);
  199. /*!
  200. * @brief Gets random data.
  201. *
  202. * This function gets random data from the TRNG.
  203. *
  204. * @param base TRNG base address.
  205. * @param data Pointer address used to store random data.
  206. * @param dataSize Size of the buffer pointed by the data parameter.
  207. * @return random data
  208. */
  209. status_t TRNG_GetRandomData(TRNG_Type *base, void *data, size_t dataSize);
  210. #if defined(__cplusplus)
  211. }
  212. #endif
  213. /*! @}*/
  214. #endif /* FSL_FEATURE_SOC_TRNG_COUNT */
  215. #endif /*_FSL_TRNG_H_*/