You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

229 lines
9.9 KiB

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