nrf_drv_rng.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef NRF_DRV_RNG_H__
  13. #define NRF_DRV_RNG_H__
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include "nrf_rng.h"
  17. #include "sdk_errors.h"
  18. #include "nrf_drv_config.h"
  19. /**
  20. * @addtogroup nrf_rng RNG HAL and driver
  21. * @ingroup nrf_drivers
  22. * @brief Random number generator (RNG) APIs.
  23. * @details The RNG HAL provides basic APIs for accessing the registers of the random number generator.
  24. * The RNG driver provides APIs on a higher level.
  25. *
  26. * @defgroup nrf_drv_rng RNG driver
  27. * @{
  28. * @ingroup nrf_rng
  29. * @brief Driver for managing the random number generator (RNG).
  30. */
  31. /**@brief Struct for RNG configuration. */
  32. typedef struct
  33. {
  34. bool error_correction; /**< Error correction flag. */
  35. uint8_t interrupt_priority; /**< interrupt priority */
  36. } nrf_drv_rng_config_t;
  37. /**@brief RNG default configuration. */
  38. #define NRF_DRV_RNG_DEFAULT_CONFIG \
  39. { \
  40. .error_correction = RNG_CONFIG_ERROR_CORRECTION, \
  41. .interrupt_priority = RNG_CONFIG_IRQ_PRIORITY, \
  42. }
  43. /**
  44. * @brief Function for initializing the nrf_drv_rng module.
  45. *
  46. * @param[in] p_config Initial configuration. Default configuration used if NULL.
  47. *
  48. * @retval NRF_SUCCESS Driver was successfully initialized.
  49. * @retval NRF_ERROR_INVALID_STATE Driver was already initialized.
  50. * @retval NRF_ERROR_INVALID_LENGTH Pool size have to be a power of 2.
  51. * @retval NRF_ERROR_SOFTDEVICE_NOT_ENABLED SoftDevice is present, but not enabled.
  52. */
  53. ret_code_t nrf_drv_rng_init(nrf_drv_rng_config_t const * p_config);
  54. /**
  55. * @brief Function for uninitializing the nrf_drv_rng module.
  56. */
  57. void nrf_drv_rng_uninit(void);
  58. /**
  59. * @brief Function for getting the number of currently available random bytes.
  60. *
  61. * @param[out] p_bytes_available The number of bytes currently available in the pool.
  62. *
  63. * @retval NRF_SUCCESS If the number of available random bytes was written to p_bytes_available.
  64. */
  65. ret_code_t nrf_drv_rng_bytes_available(uint8_t * p_bytes_available);
  66. /**
  67. * @brief Function for querying the capacity of the application random pool.
  68. *
  69. * @param[out] p_pool_capacity The capacity of the pool.
  70. *
  71. * @retval NRF_SUCCESS If the capacity of the pool was written to p_pool_capacity.
  72. */
  73. ret_code_t nrf_drv_rng_pool_capacity(uint8_t * p_pool_capacity);
  74. /**
  75. * @brief Function for getting the vector of random numbers.
  76. *
  77. * @param[out] p_buff Pointer to uint8_t buffer for storing the bytes.
  78. * @param[in] length Number of bytes to take from the pool and place in p_buff.
  79. *
  80. * @retval NRF_SUCCESS If the requested bytes were written to p_buff.
  81. * @retval NRF_ERROR_NO_MEM If no bytes were written to the buffer
  82. * because there were not enough bytes available in p_buff.
  83. * @retval NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUES If no bytes were written to the buffer
  84. * because there were not enough bytes available in the pool.
  85. */
  86. ret_code_t nrf_drv_rng_rand(uint8_t * p_buff, uint8_t length);
  87. /**
  88. * @brief Blocking function for getting an arbitrary array of random numbers.
  89. *
  90. * @note This function may execute for a substantial amount of time depending on the length of the buffer
  91. * required and on the state of the current internal pool of random numbers.
  92. *
  93. * @param[out] p_buff Pointer to uint8_t buffer for storing the bytes.
  94. * @param[in] length Number of bytes place in p_buff.
  95. *
  96. * @retval NRF_SUCCESS If the requested bytes were written to p_buff.
  97. */
  98. ret_code_t nrf_drv_rng_block_rand(uint8_t * p_buff, uint32_t length);
  99. /**
  100. *@}
  101. **/
  102. #endif // NRF_DRV_RNG_H__