fsl_trng.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2018 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_TRNG_DRIVER_H_
  35. #define _FSL_TRNG_DRIVER_H_
  36. #include "fsl_common.h"
  37. #if defined(FSL_FEATURE_SOC_TRNG_COUNT) && FSL_FEATURE_SOC_TRNG_COUNT
  38. /*!
  39. * @addtogroup trng
  40. * @{
  41. */
  42. /*******************************************************************************
  43. * Definitions
  44. *******************************************************************************/
  45. /*! @name Driver version */
  46. /*@{*/
  47. /*! @brief TRNG driver version 2.0.2.
  48. *
  49. * Current version: 2.0.2
  50. *
  51. * Change log:
  52. * - Version 2.0.2
  53. * - fix MISRA issues
  54. * - Version 2.0.1
  55. * - add support for KL8x and KL28Z
  56. * - update default OSCDIV for K81 to divide by 2
  57. */
  58. #define FSL_TRNG_DRIVER_VERSION (MAKE_VERSION(2, 0, 2))
  59. /*@}*/
  60. /*! @brief TRNG sample mode. Used by trng_config_t. */
  61. typedef enum _trng_sample_mode
  62. {
  63. kTRNG_SampleModeVonNeumann = 0U, /*!< Use von Neumann data in both Entropy shifter and Statistical Checker. */
  64. kTRNG_SampleModeRaw = 1U, /*!< Use raw data into both Entropy shifter and Statistical Checker. */
  65. kTRNG_SampleModeVonNeumannRaw =
  66. 2U /*!< Use von Neumann data in Entropy shifter. Use raw data into Statistical Checker. */
  67. } trng_sample_mode_t;
  68. /*! @brief TRNG clock mode. Used by trng_config_t. */
  69. typedef enum _trng_clock_mode
  70. {
  71. kTRNG_ClockModeRingOscillator = 0U, /*!< Ring oscillator is used to operate the TRNG (default). */
  72. kTRNG_ClockModeSystem = 1U /*!< System clock is used to operate the TRNG. This is for test use only, and
  73. indeterminate results may occur. */
  74. } trng_clock_mode_t;
  75. /*! @brief TRNG ring oscillator divide. Used by trng_config_t. */
  76. typedef enum _trng_ring_osc_div
  77. {
  78. kTRNG_RingOscDiv0 = 0U, /*!< Ring oscillator with no divide */
  79. kTRNG_RingOscDiv2 = 1U, /*!< Ring oscillator divided-by-2. */
  80. kTRNG_RingOscDiv4 = 2U, /*!< Ring oscillator divided-by-4. */
  81. kTRNG_RingOscDiv8 = 3U /*!< Ring oscillator divided-by-8. */
  82. } trng_ring_osc_div_t;
  83. /*! @brief Data structure for definition of statistical check limits. Used by trng_config_t. */
  84. typedef struct _trng_statistical_check_limit
  85. {
  86. uint32_t maximum; /*!< Maximum limit.*/
  87. uint32_t minimum; /*!< Minimum limit.*/
  88. } trng_statistical_check_limit_t;
  89. /*!
  90. * @brief Data structure for the TRNG initialization
  91. *
  92. * This structure initializes the TRNG by calling the TRNG_Init() function.
  93. * It contains all TRNG configurations.
  94. */
  95. typedef struct _trng_user_config
  96. {
  97. bool lock; /*!< @brief Disable programmability of TRNG registers. */
  98. trng_clock_mode_t clockMode; /*!< @brief Clock mode used to operate TRNG.*/
  99. trng_ring_osc_div_t ringOscDiv; /*!< @brief Ring oscillator divide used by TRNG. */
  100. trng_sample_mode_t sampleMode; /*!< @brief Sample mode of the TRNG ring oscillator. */
  101. /* Seed Control*/
  102. uint16_t
  103. entropyDelay; /*!< @brief Entropy Delay. Defines the length (in system clocks) of each Entropy sample taken. */
  104. uint16_t sampleSize; /*!< @brief Sample Size. Defines the total number of Entropy samples that will be taken during
  105. Entropy generation. */
  106. uint16_t
  107. sparseBitLimit; /*!< @brief Sparse Bit Limit which defines the maximum number of
  108. * consecutive samples that may be discarded before an error is generated.
  109. * This limit is used only for during von Neumann sampling (enabled by TRNG_HAL_SetSampleMode()).
  110. * Samples are discarded if two consecutive raw samples are both 0 or both 1. If
  111. * this discarding occurs for a long period of time, it indicates that there is
  112. * insufficient Entropy. */
  113. /* Statistical Check Parameters.*/
  114. uint8_t retryCount; /*!< @brief Retry count. It defines the number of times a statistical check may fails
  115. * during the TRNG Entropy Generation before generating an error. */
  116. uint8_t longRunMaxLimit; /*!< @brief Largest allowable number of consecutive samples of all 1, or all 0,
  117. * that is allowed during the Entropy generation. */
  118. trng_statistical_check_limit_t
  119. monobitLimit; /*!< @brief Maximum and minimum limits for statistical check of number of ones/zero detected
  120. during entropy generation. */
  121. trng_statistical_check_limit_t
  122. runBit1Limit; /*!< @brief Maximum and minimum limits for statistical check of number of runs of length 1
  123. detected during entropy generation. */
  124. trng_statistical_check_limit_t
  125. runBit2Limit; /*!< @brief Maximum and minimum limits for statistical check of number of runs of length 2
  126. detected during entropy generation. */
  127. trng_statistical_check_limit_t
  128. runBit3Limit; /*!< @brief Maximum and minimum limits for statistical check of number of runs of length 3
  129. detected during entropy generation. */
  130. trng_statistical_check_limit_t
  131. runBit4Limit; /*!< @brief Maximum and minimum limits for statistical check of number of runs of length 4
  132. detected during entropy generation. */
  133. trng_statistical_check_limit_t
  134. runBit5Limit; /*!< @brief Maximum and minimum limits for statistical check of number of runs of length 5
  135. detected during entropy generation. */
  136. trng_statistical_check_limit_t runBit6PlusLimit; /*!< @brief Maximum and minimum limits for statistical check of
  137. number of runs of length 6 or more detected during entropy
  138. generation. */
  139. trng_statistical_check_limit_t
  140. pokerLimit; /*!< @brief Maximum and minimum limits for statistical check of "Poker Test". */
  141. trng_statistical_check_limit_t
  142. frequencyCountLimit; /*!< @brief Maximum and minimum limits for statistical check of entropy sample frequency
  143. count. */
  144. } trng_config_t;
  145. /*******************************************************************************
  146. * API
  147. *******************************************************************************/
  148. #if defined(__cplusplus)
  149. extern "C" {
  150. #endif
  151. /*!
  152. * @brief Initializes the user configuration structure to default values.
  153. *
  154. * This function initializes the configuration structure to default values. The default
  155. * values are as follows.
  156. * @code
  157. * user_config->lock = 0;
  158. * user_config->clockMode = kTRNG_ClockModeRingOscillator;
  159. * user_config->ringOscDiv = kTRNG_RingOscDiv0; Or to other kTRNG_RingOscDiv[2|8] depending on the platform.
  160. * user_config->sampleMode = kTRNG_SampleModeRaw;
  161. * user_config->entropyDelay = 3200;
  162. * user_config->sampleSize = 2500;
  163. * user_config->sparseBitLimit = TRNG_USER_CONFIG_DEFAULT_SPARSE_BIT_LIMIT;
  164. * user_config->retryCount = 63;
  165. * user_config->longRunMaxLimit = 34;
  166. * user_config->monobitLimit.maximum = 1384;
  167. * user_config->monobitLimit.minimum = 1116;
  168. * user_config->runBit1Limit.maximum = 405;
  169. * user_config->runBit1Limit.minimum = 227;
  170. * user_config->runBit2Limit.maximum = 220;
  171. * user_config->runBit2Limit.minimum = 98;
  172. * user_config->runBit3Limit.maximum = 125;
  173. * user_config->runBit3Limit.minimum = 37;
  174. * user_config->runBit4Limit.maximum = 75;
  175. * user_config->runBit4Limit.minimum = 11;
  176. * user_config->runBit5Limit.maximum = 47;
  177. * user_config->runBit5Limit.minimum = 1;
  178. * user_config->runBit6PlusLimit.maximum = 47;
  179. * user_config->runBit6PlusLimit.minimum = 1;
  180. * user_config->pokerLimit.maximum = 26912;
  181. * user_config->pokerLimit.minimum = 24445;
  182. * user_config->frequencyCountLimit.maximum = 25600;
  183. * user_config->frequencyCountLimit.minimum = 1600;
  184. * @endcode
  185. *
  186. * @param user_config User configuration structure.
  187. * @return If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
  188. */
  189. status_t TRNG_GetDefaultConfig(trng_config_t *userConfig);
  190. /*!
  191. * @brief Initializes the TRNG.
  192. *
  193. * This function initializes the TRNG.
  194. * When called, the TRNG entropy generation starts immediately.
  195. *
  196. * @param base TRNG base address
  197. * @param userConfig Pointer to the initialization configuration structure.
  198. * @return If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
  199. */
  200. status_t TRNG_Init(TRNG_Type *base, const trng_config_t *userConfig);
  201. /*!
  202. * @brief Shuts down the TRNG.
  203. *
  204. * This function shuts down the TRNG.
  205. *
  206. * @param base TRNG base address.
  207. */
  208. void TRNG_Deinit(TRNG_Type *base);
  209. /*!
  210. * @brief Gets random data.
  211. *
  212. * This function gets random data from the TRNG.
  213. *
  214. * @param base TRNG base address.
  215. * @param data Pointer address used to store random data.
  216. * @param dataSize Size of the buffer pointed by the data parameter.
  217. * @return random data
  218. */
  219. status_t TRNG_GetRandomData(TRNG_Type *base, void *data, size_t dataSize);
  220. #if defined(__cplusplus)
  221. }
  222. #endif
  223. /*! @}*/
  224. #endif /* FSL_FEATURE_SOC_TRNG_COUNT */
  225. #endif /*_FSL_TRNG_H_*/