nrf_crypto_rng.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /**
  2. * Copyright (c) 2018 - 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(NRF_CRYPTO)
  42. #include "nrf_crypto_init.h"
  43. #include "nrf_log.h"
  44. #include "nrf_crypto_mem.h"
  45. #include "nrf_crypto_rng.h"
  46. #include "nrf_crypto_rng_shared.h"
  47. #include "nrf_crypto_rng_backend.h"
  48. #include "nrf_stack_info.h"
  49. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG)
  50. #define NRF_CRYPTO_RNG_MODULE_INIT_MAGIC_VALUE (0x4be57265)
  51. static nrf_crypto_backend_rng_context_t * mp_allocated_context = NULL;
  52. static nrf_crypto_backend_rng_context_t * mp_context = NULL;
  53. static uint32_t m_initialized = 0;
  54. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  55. static nrf_crypto_backend_rng_context_t m_context;
  56. static nrf_crypto_rng_temp_buffer_t m_temp_buffer;
  57. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  58. static bool is_vector_greater_or_equal(uint8_t const * const p_vector,
  59. uint8_t const * const p_min,
  60. size_t size)
  61. {
  62. for (size_t i = 0; i < size; i++)
  63. {
  64. if (p_vector[i] != p_min[i])
  65. {
  66. if (p_vector[i] > p_min[i])
  67. {
  68. return true;
  69. }
  70. else
  71. {
  72. return false;
  73. }
  74. }
  75. }
  76. return true;
  77. }
  78. // Return true if value p_vector is between (including) p_min and p_max.
  79. static bool is_vector_in_range(uint8_t const * const p_vector,
  80. uint8_t const * const p_min,
  81. uint8_t const * const p_max,
  82. size_t size)
  83. {
  84. if (!is_vector_greater_or_equal(p_vector, p_min, size))
  85. {
  86. return false;
  87. }
  88. if (!is_vector_greater_or_equal(p_max, p_vector, size))
  89. {
  90. return false;
  91. }
  92. return true;
  93. }
  94. static uint32_t count_leading_zeros(uint8_t const * const p_vector, size_t size)
  95. {
  96. uint32_t leading_zeros = 0;
  97. uint32_t nonzero_byte = 0xFF;
  98. // Find leading all-zero elements.
  99. for (uint32_t i = 0; i < size; i++)
  100. {
  101. if (p_vector[i] == 0)
  102. {
  103. leading_zeros += 8;
  104. }
  105. else
  106. {
  107. nonzero_byte = p_vector[i];
  108. break;
  109. }
  110. }
  111. // Find leading zeros in non-zero element.
  112. for (uint32_t i = 0; i < 8; i++)
  113. {
  114. nonzero_byte <<= 1;
  115. if ((nonzero_byte & ~0xff) > 0)
  116. {
  117. break;
  118. }
  119. leading_zeros ++;
  120. }
  121. return leading_zeros;
  122. }
  123. static ret_code_t generate(uint8_t * const p_target, size_t size, bool use_mutex)
  124. {
  125. ret_code_t ret_code;
  126. VERIFY_TRUE(p_target != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  127. VERIFY_TRUE(size > 0, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  128. VERIFY_TRUE(m_initialized == NRF_CRYPTO_RNG_MODULE_INIT_MAGIC_VALUE,
  129. NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED);
  130. ret_code = nrf_crypto_rng_backend_vector_generate(mp_context, p_target, size, use_mutex);
  131. // Reseed internally and try again if reseed is required by the backend.
  132. // (CC310 only as mbed TLS handles reseeding internally.)
  133. if (ret_code == NRF_ERROR_CRYPTO_RNG_RESEED_REQUIRED)
  134. {
  135. ret_code = nrf_crypto_rng_reseed(NULL, NULL, 0);
  136. if (ret_code != NRF_SUCCESS)
  137. {
  138. return ret_code;
  139. }
  140. ret_code = nrf_crypto_rng_backend_vector_generate(mp_context, p_target, size, use_mutex);
  141. }
  142. return ret_code;
  143. }
  144. static ret_code_t generate_in_range(uint8_t * const p_target,
  145. uint8_t const * const p_min,
  146. uint8_t const * const p_max,
  147. size_t size,
  148. bool use_mutex)
  149. {
  150. uint32_t const max_leading_zeros = count_leading_zeros(p_max, size);
  151. ret_code_t ret_code;
  152. VERIFY_TRUE(p_target != NULL, NRF_ERROR_CRYPTO_OUTPUT_NULL);
  153. VERIFY_TRUE(size > 0, NRF_ERROR_CRYPTO_OUTPUT_LENGTH);
  154. VERIFY_TRUE(p_min != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  155. VERIFY_TRUE(p_max != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  156. VERIFY_TRUE(is_vector_greater_or_equal(p_max, p_min, size), NRF_ERROR_CRYPTO_INVALID_PARAM);
  157. do
  158. {
  159. ret_code = nrf_crypto_rng_backend_vector_generate(mp_context, p_target, size, use_mutex);
  160. if (ret_code != NRF_SUCCESS)
  161. {
  162. return ret_code;
  163. }
  164. // Mask leading zeros in generated vector instead of always discarding a too large vectors.
  165. memset(p_target, 0, max_leading_zeros / 8);
  166. if ((max_leading_zeros & 0x07) > 0)
  167. {
  168. p_target[max_leading_zeros / 8] =
  169. p_target[max_leading_zeros / 8] & (0xff >> (max_leading_zeros & 0x07));
  170. }
  171. } while (!is_vector_in_range(p_target, p_min, p_max, size));
  172. return NRF_SUCCESS;
  173. }
  174. ret_code_t nrf_crypto_rng_vector_generate(uint8_t * const p_target, size_t size)
  175. {
  176. ret_code_t ret_code;
  177. ret_code = generate(p_target, size, true);
  178. return ret_code;
  179. }
  180. ret_code_t nrf_crypto_rng_vector_generate_in_range(uint8_t * const p_target,
  181. uint8_t const * const p_min,
  182. uint8_t const * const p_max,
  183. size_t size)
  184. {
  185. ret_code_t ret_code;
  186. ret_code = generate_in_range(p_target, p_min, p_max, size, true);
  187. return ret_code;
  188. }
  189. ret_code_t nrf_crypto_rng_vector_generate_no_mutex(uint8_t * const p_target, size_t size)
  190. {
  191. ret_code_t ret_code;
  192. ret_code = generate(p_target, size, false);
  193. return ret_code;
  194. }
  195. ret_code_t nrf_crypto_rng_vector_generate_in_range_no_mutex(uint8_t * const p_target,
  196. uint8_t const * const p_min,
  197. uint8_t const * const p_max,
  198. size_t size)
  199. {
  200. ret_code_t ret_code;
  201. ret_code = generate_in_range(p_target, p_min, p_max, size, false);
  202. return ret_code;
  203. }
  204. ret_code_t nrf_crypto_rng_init(nrf_crypto_rng_context_t * p_context,
  205. nrf_crypto_rng_temp_buffer_t * p_temp_buffer)
  206. {
  207. ret_code_t ret_code;
  208. nrf_crypto_rng_temp_buffer_t * p_allocated_temp_buffer = NULL;
  209. // Check if the stack has overflowed. This can typically happen if the application has put the
  210. // ~6 kB large temp buffer for CC310 on the stack.
  211. if (nrf_stack_info_overflowed())
  212. {
  213. NRF_LOG_ERROR("Stack overflow detected.");
  214. return NRF_ERROR_CRYPTO_STACK_OVERFLOW;
  215. }
  216. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_AUTO_INIT)
  217. VERIFY_TRUE(nrf_crypto_is_initializing(), NRF_ERROR_CRYPTO_NOT_INITIALIZED);
  218. #else
  219. VERIFY_TRUE(nrf_crypto_is_initialized(), NRF_ERROR_CRYPTO_NOT_INITIALIZED);
  220. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_AUTO_INIT)
  221. // Do nothing if RNG module is already initialized.
  222. if (mp_context != 0 && (m_initialized == NRF_CRYPTO_RNG_MODULE_INIT_MAGIC_VALUE))
  223. {
  224. return NRF_SUCCESS;
  225. }
  226. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  227. VERIFY_TRUE(p_context == NULL, NRF_ERROR_CRYPTO_INVALID_PARAM);
  228. VERIFY_TRUE(p_temp_buffer == NULL, NRF_ERROR_CRYPTO_INVALID_PARAM);
  229. mp_context = &m_context;
  230. p_temp_buffer = &m_temp_buffer;
  231. #else // !NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  232. if (p_context == NULL)
  233. {
  234. if (NRF_CRYPTO_ALLOC_ON_STACK)
  235. {
  236. NRF_LOG_ERROR("RNG context cannot be allocated on the stack.");
  237. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  238. }
  239. else
  240. {
  241. mp_allocated_context = NRF_CRYPTO_ALLOC(sizeof(nrf_crypto_backend_rng_context_t));
  242. if (mp_allocated_context == NULL)
  243. {
  244. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  245. }
  246. mp_context = mp_allocated_context;
  247. }
  248. }
  249. else
  250. {
  251. mp_context = p_context;
  252. }
  253. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  254. // Allocate temporary buffer internally if not statically allocated or provided by the user.
  255. if (p_temp_buffer == NULL)
  256. {
  257. p_allocated_temp_buffer = NRF_CRYPTO_ALLOC(sizeof(nrf_crypto_rng_temp_buffer_t));
  258. if (p_allocated_temp_buffer == NULL)
  259. {
  260. if (mp_allocated_context != NULL)
  261. {
  262. NRF_CRYPTO_FREE(mp_allocated_context);
  263. }
  264. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  265. }
  266. p_temp_buffer = p_allocated_temp_buffer;
  267. }
  268. ret_code = nrf_crypto_rng_backend_init(mp_context, p_temp_buffer);
  269. if (ret_code == NRF_SUCCESS)
  270. {
  271. m_initialized = NRF_CRYPTO_RNG_MODULE_INIT_MAGIC_VALUE;
  272. mp_context->header.init_value = NRF_CRYPTO_RNG_CONTEXT_INIT_MAGIC_VALUE;
  273. }
  274. if (p_allocated_temp_buffer != NULL)
  275. {
  276. NRF_CRYPTO_FREE(p_allocated_temp_buffer);
  277. }
  278. return ret_code;
  279. }
  280. ret_code_t nrf_crypto_rng_uninit(void)
  281. {
  282. ret_code_t ret_code;
  283. VERIFY_TRUE(m_initialized == NRF_CRYPTO_RNG_MODULE_INIT_MAGIC_VALUE,
  284. NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED);
  285. VERIFY_TRUE(mp_context->header.init_value == NRF_CRYPTO_RNG_CONTEXT_INIT_MAGIC_VALUE,
  286. NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED);
  287. mp_context->header.init_value = 0;
  288. m_initialized = 0;
  289. ret_code = nrf_crypto_rng_backend_uninit(mp_context);
  290. if (mp_allocated_context != NULL)
  291. {
  292. NRF_CRYPTO_FREE(mp_allocated_context);
  293. }
  294. return ret_code;
  295. }
  296. ret_code_t nrf_crypto_rng_reseed(nrf_crypto_rng_temp_buffer_t * p_temp_buffer,
  297. uint8_t * p_input_data,
  298. size_t size)
  299. {
  300. ret_code_t ret_code;
  301. void * p_allocated_temp_buffer = NULL;
  302. // Check if the stack has overflowed. This can typically happen if the application has put the
  303. // ~6 kB large temp buffer for CC310 on the stack.
  304. if (nrf_stack_info_overflowed())
  305. {
  306. NRF_LOG_ERROR("Stack overflow detected.");
  307. return NRF_ERROR_CRYPTO_STACK_OVERFLOW;
  308. }
  309. if (size > 0)
  310. {
  311. VERIFY_TRUE(p_input_data != NULL, NRF_ERROR_CRYPTO_INPUT_NULL);
  312. }
  313. VERIFY_TRUE(m_initialized == NRF_CRYPTO_RNG_MODULE_INIT_MAGIC_VALUE,
  314. NRF_ERROR_CRYPTO_CONTEXT_NOT_INITIALIZED);
  315. #if NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  316. VERIFY_TRUE(p_temp_buffer == NULL, NRF_ERROR_CRYPTO_INVALID_PARAM);
  317. p_temp_buffer = &m_temp_buffer;
  318. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_RNG_STATIC_MEMORY_BUFFERS)
  319. // Allocate temporary buffer internally if not statically allocated or provided by the user.
  320. if (p_temp_buffer == NULL)
  321. {
  322. p_allocated_temp_buffer = NRF_CRYPTO_ALLOC(sizeof(nrf_crypto_rng_temp_buffer_t));
  323. if (p_allocated_temp_buffer == NULL)
  324. {
  325. return NRF_ERROR_CRYPTO_ALLOC_FAILED;
  326. }
  327. p_temp_buffer = (nrf_crypto_rng_temp_buffer_t *)p_allocated_temp_buffer;
  328. }
  329. ret_code = nrf_crypto_rng_backend_reseed(mp_context, p_temp_buffer, p_input_data, size);
  330. if (p_allocated_temp_buffer != NULL)
  331. {
  332. NRF_CRYPTO_FREE(p_allocated_temp_buffer);
  333. }
  334. return ret_code;
  335. }
  336. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO_RNG)
  337. #endif // NRF_MODULE_ENABLED(NRF_CRYPTO)