nrf_drv_rng.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA 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. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(RNG)
  42. #include <stdint.h>
  43. #include <stddef.h>
  44. #include "nrf_drv_rng.h"
  45. #include "nordic_common.h"
  46. #include "app_util_platform.h"
  47. #include "nrf_assert.h"
  48. #include "nrf_queue.h"
  49. #ifdef SOFTDEVICE_PRESENT
  50. #include "nrf_sdh.h"
  51. #endif // SOFTDEVICE_PRESENT
  52. #define NRF_LOG_MODULE_NAME rng
  53. #if RNG_CONFIG_LOG_ENABLED
  54. #define NRF_LOG_LEVEL RNG_CONFIG_LOG_LEVEL
  55. #define NRF_LOG_INFO_COLOR RNG_CONFIG_INFO_COLOR
  56. #define NRF_LOG_DEBUG_COLOR RNG_CONFIG_DEBUG_COLOR
  57. #else //RNG_CONFIG_LOG_ENABLED
  58. #define NRF_LOG_LEVEL 0
  59. #endif //RNG_CONFIG_LOG_ENABLED
  60. #include "nrf_log.h"
  61. NRF_LOG_MODULE_REGISTER();
  62. typedef struct
  63. {
  64. nrfx_drv_state_t state;
  65. nrf_drv_rng_config_t config;
  66. } nrf_drv_rng_cb_t;
  67. static nrf_drv_rng_cb_t m_rng_cb;
  68. NRF_QUEUE_DEF(uint8_t, m_rand_pool, RNG_CONFIG_POOL_SIZE, NRF_QUEUE_MODE_OVERFLOW);
  69. static const nrf_drv_rng_config_t m_default_config = NRF_DRV_RNG_DEFAULT_CONFIG;
  70. #ifdef SOFTDEVICE_PRESENT
  71. #define SD_RAND_POOL_SIZE (64)
  72. STATIC_ASSERT(RNG_CONFIG_POOL_SIZE == SD_RAND_POOL_SIZE);
  73. #define NRF_DRV_RNG_LOCK() CRITICAL_REGION_ENTER()
  74. #define NRF_DRV_RNG_RELEASE() CRITICAL_REGION_EXIT()
  75. #define NRF_DRV_RNG_SD_IS_ENABLED() nrf_sdh_is_enabled()
  76. #else
  77. #define NRF_DRV_RNG_LOCK() do { } while (0)
  78. #define NRF_DRV_RNG_RELEASE() do { } while (0)
  79. #define NRF_DRV_RNG_SD_IS_ENABLED() false
  80. #endif // SOFTDEVICE_PRESENT
  81. static void nrfx_rng_handler(uint8_t rng_val)
  82. {
  83. NRF_DRV_RNG_LOCK();
  84. if (!NRF_DRV_RNG_SD_IS_ENABLED())
  85. {
  86. UNUSED_RETURN_VALUE(nrf_queue_push(&m_rand_pool, &rng_val));
  87. if (nrf_queue_is_full(&m_rand_pool))
  88. {
  89. nrfx_rng_stop();
  90. }
  91. NRF_LOG_DEBUG("Event: NRF_RNG_EVENT_VALRDY.");
  92. }
  93. NRF_DRV_RNG_RELEASE();
  94. }
  95. ret_code_t nrf_drv_rng_init(nrf_drv_rng_config_t const * p_config)
  96. {
  97. ret_code_t err_code = NRF_SUCCESS;
  98. if (m_rng_cb.state != NRFX_DRV_STATE_UNINITIALIZED)
  99. {
  100. return NRF_ERROR_MODULE_ALREADY_INITIALIZED;
  101. }
  102. if (p_config == NULL)
  103. {
  104. p_config = &m_default_config;
  105. }
  106. m_rng_cb.config = *p_config;
  107. NRF_DRV_RNG_LOCK();
  108. if (!NRF_DRV_RNG_SD_IS_ENABLED())
  109. {
  110. err_code = nrfx_rng_init(&m_rng_cb.config, nrfx_rng_handler);
  111. if (err_code != NRF_SUCCESS)
  112. {
  113. return err_code;
  114. }
  115. nrfx_rng_start();
  116. }
  117. m_rng_cb.state = NRFX_DRV_STATE_INITIALIZED;
  118. NRF_DRV_RNG_RELEASE();
  119. return err_code;
  120. }
  121. void nrf_drv_rng_uninit(void)
  122. {
  123. ASSERT(m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED);
  124. NRF_DRV_RNG_LOCK();
  125. if (!NRF_DRV_RNG_SD_IS_ENABLED())
  126. {
  127. nrfx_rng_stop();
  128. nrfx_rng_uninit();
  129. }
  130. NRF_DRV_RNG_RELEASE();
  131. nrf_queue_reset(&m_rand_pool);
  132. m_rng_cb.state = NRFX_DRV_STATE_UNINITIALIZED;
  133. NRF_LOG_INFO("Uninitialized.");
  134. }
  135. void nrf_drv_rng_bytes_available(uint8_t * p_bytes_available)
  136. {
  137. ASSERT(m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED);
  138. #ifdef SOFTDEVICE_PRESENT
  139. if (NRF_DRV_RNG_SD_IS_ENABLED())
  140. {
  141. if (NRF_SUCCESS == sd_rand_application_bytes_available_get(p_bytes_available))
  142. {
  143. return;
  144. }
  145. }
  146. #endif // SOFTDEVICE_PRESENT
  147. *p_bytes_available = nrf_queue_utilization_get(&m_rand_pool);
  148. NRF_LOG_INFO("Function: %s, available bytes: %d.", (uint32_t)__func__, *p_bytes_available);
  149. }
  150. ret_code_t nrf_drv_rng_rand(uint8_t * p_buff, uint8_t length)
  151. {
  152. ret_code_t err_code = NRF_SUCCESS;
  153. ASSERT(m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED);
  154. #ifdef SOFTDEVICE_PRESENT
  155. do {
  156. bool sd_is_enabled;
  157. NRF_DRV_RNG_LOCK();
  158. sd_is_enabled = NRF_DRV_RNG_SD_IS_ENABLED();
  159. if (!sd_is_enabled)
  160. #endif // SOFTDEVICE_PRESENT
  161. {
  162. err_code = nrf_queue_read(&m_rand_pool, p_buff, (uint32_t)length);
  163. nrfx_rng_start();
  164. }
  165. #ifdef SOFTDEVICE_PRESENT
  166. NRF_DRV_RNG_RELEASE();
  167. if (sd_is_enabled)
  168. {
  169. err_code = sd_rand_application_vector_get(p_buff, length);
  170. if (err_code == NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUES)
  171. {
  172. err_code = NRF_ERROR_NOT_FOUND;
  173. }
  174. }
  175. } while (err_code == NRF_ERROR_SOFTDEVICE_NOT_ENABLED);
  176. #endif // SOFTDEVICE_PRESENT
  177. ASSERT((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NOT_FOUND));
  178. #if defined(RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED) && (RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED != 0)
  179. NRF_LOG_DEBUG("Rand buffer data:");
  180. NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_buff, length);
  181. #endif // RNG_CONFIG_RANDOM_NUMBER_LOG_ENABLED
  182. NRF_LOG_WARNING("Function: %s, error code: %s.",
  183. (uint32_t)__func__,
  184. (uint32_t)NRF_LOG_ERROR_STRING_GET(err_code));
  185. return err_code;
  186. }
  187. void nrf_drv_rng_block_rand(uint8_t * p_buff, uint32_t length)
  188. {
  189. ASSERT(m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED);
  190. while (length)
  191. {
  192. uint32_t len = MIN(length, RNG_CONFIG_POOL_SIZE);
  193. ret_code_t err_code;
  194. do {
  195. err_code = nrf_drv_rng_rand(p_buff, len);
  196. } while (err_code != NRF_SUCCESS);
  197. length -= len;
  198. p_buff += len;
  199. }
  200. NRF_LOG_DEBUG("Rand buffer data:");
  201. NRF_LOG_HEXDUMP_DEBUG((uint8_t *)p_buff, length);
  202. }
  203. #ifdef SOFTDEVICE_PRESENT
  204. static void sd_state_evt_handler(nrf_sdh_state_evt_t state, void * p_context)
  205. {
  206. switch (state)
  207. {
  208. case NRF_SDH_EVT_STATE_ENABLE_PREPARE:
  209. if (m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED)
  210. {
  211. nrfx_rng_stop();
  212. nrfx_rng_uninit();
  213. }
  214. break;
  215. case NRF_SDH_EVT_STATE_DISABLED:
  216. NRF_DRV_RNG_LOCK();
  217. if (m_rng_cb.state == NRFX_DRV_STATE_INITIALIZED)
  218. {
  219. ret_code_t err_code = nrfx_rng_init(&m_rng_cb.config, nrfx_rng_handler);
  220. if (err_code != NRF_SUCCESS)
  221. {
  222. ASSERT(false);
  223. }
  224. nrfx_rng_start();
  225. }
  226. NRF_DRV_RNG_RELEASE();
  227. break;
  228. default:
  229. break;
  230. }
  231. }
  232. NRF_SDH_STATE_OBSERVER(m_sd_state_observer, RNG_CONFIG_STATE_OBSERVER_PRIO) =
  233. {
  234. .handler = sd_state_evt_handler,
  235. .p_context = NULL,
  236. };
  237. #endif // SOFTDEVICE_PRESENT
  238. #endif // NRF_MODULE_ENABLED(RNG)