EVE 1.0
rng.h
Go to the documentation of this file.
1 #ifndef RNG_H_INCLUDED
2 #define RNG_H_INCLUDED
3 
4 /**********************************************************************/
5 /*
6  * Copyright (c) 2013-2015, Jetro AS
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  * derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONRIBUTORS ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
23  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
25  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29  * OF SUCH DAMAGE.
30  *
31  * This file is part of the EVE platform.
32  */
33 
34 /**
35  * \file
36  * @brief Hardware random number generator driver.
37  *
38  * @author SE, Jetro AS
39  */ /******************************************************************/
40 
41 #include <stdbool.h>
42 #include <stdint.h>
43 
44 #include <hal/nrf_rng.h>
45 #include <sdk_errors.h>
46 
47 /**
48  * \defgroup rng nRF52 HW Random number generator
49  * \brief nRF52 hardware random number generator (RNG) APIs.
50  * \ingroup nrf52
51  * \{
52  */
53 
54 /** @brief Struct for RNG configuration. */
56 {
57  bool error_correction; /**< Error correction flag. */
58  uint8_t interrupt_priority; /**< interrupt priority */
59 };
60 
61 /** @brief RNG default configuration. */
62 #define NRF_DRV_RNG_DEFAULT_CONFIG \
63  { \
64  .error_correction = RNG_CONFIG_ERROR_CORRECTION, \
65  .interrupt_priority = RNG_CONFIG_IRQ_PRIORITY, \
66  }
67 
68 /**
69  * @brief Function for initializing the nrf_drv_rng module.
70  * @retval NRF_SUCCESS Driver was successfully initialized.
71  * @retval NRF_ERROR_INVALID_STATE Driver was already initialized.
72  * @retval NRF_ERROR_INVALID_LENGTH Pool size have to be a power of 2.
73  * @retval NRF_ERROR_SOFTDEVICE_NOT_ENABLED SoftDevice is present, but not enabled.
74  */
75 uint32_t RngInit(void);
76 
77 /**
78  * @brief Function for deinitializing the nrf_drv_rng module.
79  */
80 void RngDeinit(void);
81 
82 /**
83  * @brief Function for getting the number of currently available random bytes.
84  * @param[out] bytes_available The number of bytes currently available in the pool.
85  * @retval NRF_SUCCESS If the number of available random bytes was written to bytes_available.
86  */
87 uint32_t RngBytesAvailable(uint8_t* bytes_available);
88 
89 /**
90  * @brief Function for querying the capacity of the application random pool.
91  * @param[out] pool_capacity The capacity of the pool.
92  * @retval NRF_SUCCESS If the capacity of the pool was written to pool_capacity.
93  */
94 uint32_t RngPoolCapacity(uint8_t* pool_capacity);
95 
96 /**
97  * @brief Function for getting the vector of random numbers.
98  * @param[out] buffer Pointer to uint8_t buffer for storing the bytes.
99  * @param[in] length Number of bytes to take from the pool and place in buffer.
100  * @retval NRF_SUCCESS If the requested bytes were written to buffer.
101  * @retval NRF_ERROR_NO_MEM If no bytes were written to the buffer
102  * @retval NRF_ERROR_SOC_RAND_NOT_ENOUGH_VALUES If no bytes were written to the buffer
103  */
104 uint32_t RngRand(uint8_t* buffer, uint8_t length);
105 
106 /**
107  * @brief Blocking function for getting an arbitrary array of random numbers.
108  * @note This function may execute for a substantial amount of time depending on the length of the buffer
109  * required and on the state of the current internal pool of random numbers.
110  * @param[out] buffer Pointer to buffer for storing the bytes.
111  * @param[in] length Number of bytes place in buffer.
112  * @retval NRF_SUCCESS If the requested bytes were written to buffer.
113  */
114 uint32_t RngBlockRand(uint8_t* buffer, uint32_t length);
115 
116 /** @} rng */
117 
118 #endif // RNG_H_INCLUDED
uint32_t RngRand(uint8_t *buffer, uint8_t length)
Function for getting the vector of random numbers.
uint32_t RngBlockRand(uint8_t *buffer, uint32_t length)
Blocking function for getting an arbitrary array of random numbers.
void RngDeinit(void)
Function for deinitializing the nrf_drv_rng module.
uint32_t RngBytesAvailable(uint8_t *bytes_available)
Function for getting the number of currently available random bytes.
uint8_t interrupt_priority
Definition: rng.h:58
uint32_t RngInit(void)
Function for initializing the nrf_drv_rng module.
bool error_correction
Definition: rng.h:57
uint32_t RngPoolCapacity(uint8_t *pool_capacity)
Function for querying the capacity of the application random pool.
Struct for RNG configuration.
Definition: rng.h:55