fsl_trng.h 10 KB

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