diff options
Diffstat (limited to 'chip/g/trng.c')
-rw-r--r-- | chip/g/trng.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/chip/g/trng.c b/chip/g/trng.c index e462127da8..b6a062d58d 100644 --- a/chip/g/trng.c +++ b/chip/g/trng.c @@ -20,3 +20,25 @@ uint32_t rand(void) ; return GREAD(TRNG, READ_DATA); } + +void rand_bytes(void *buffer, size_t len) +{ + int random_togo = 0; + int buffer_index = 0; + uint32_t random_value; + uint8_t *buf = (uint8_t *) buffer; + + /* + * Retrieve random numbers in 4 byte quantities and pack as many bytes + * as needed into 'buffer'. If len is not divisible by 4, the + * remaining random bytes get dropped. + */ + while (buffer_index < len) { + if (!random_togo) { + random_value = rand(); + random_togo = sizeof(random_value); + } + buf[buffer_index++] = random_value >> + ((random_togo-- - 1) * 8); + } +} |