File tree Expand file tree Collapse file tree 1 file changed +28
-10
lines changed
targets/TARGET_NORDIC/TARGET_NRF5 Expand file tree Collapse file tree 1 file changed +28
-10
lines changed Original file line number Diff line number Diff line change 42
42
43
43
void trng_init (trng_t * obj )
44
44
{
45
+ (void ) obj ;
46
+
45
47
(void )nrf_drv_rng_init (NULL );
46
48
}
47
49
48
50
void trng_free (trng_t * obj )
49
51
{
52
+ (void ) obj ;
53
+
50
54
nrf_drv_rng_uninit ();
51
55
}
52
56
57
+ /* Get random data from NRF5x TRNG peripheral.
58
+ *
59
+ * This implementation returns num of random bytes in range <1, length>.
60
+ * For parameters description see trng_api.h file.
61
+ */
53
62
int trng_get_bytes (trng_t * obj , uint8_t * output , size_t length , size_t * output_length )
54
63
{
55
- #ifdef NRF_RNG_NON_BLOCKING
56
64
uint8_t bytes_available ;
57
-
65
+
66
+ (void ) obj ;
67
+
58
68
nrf_drv_rng_bytes_available (& bytes_available );
59
-
60
- if ((bytes_available < length ) || (nrf_drv_rng_rand (output , length ) == NRF_ERROR_NOT_FOUND )) {
61
- * output_length = 0 ;
62
- return -1 ;
63
- }
64
- #endif
65
- nrf_drv_rng_block_rand (output , length );
66
69
67
- * output_length = length ;
70
+ if (bytes_available == 0 ) {
71
+ nrf_drv_rng_block_rand (output , 1 );
72
+ * output_length = 1 ;
73
+ } else {
74
+
75
+ if (bytes_available > length ) {
76
+ bytes_available = length ;
77
+ }
78
+
79
+ if (nrf_drv_rng_rand (output , bytes_available ) != NRF_SUCCESS ) {
80
+ * output_length = 0 ;
81
+ return -1 ;
82
+ } else {
83
+ * output_length = bytes_available ;
84
+ }
85
+ }
68
86
69
87
return 0 ;
70
88
}
You can’t perform that action at this time.
0 commit comments