1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-17 03:48:09 +00:00
trezor-firmware/embed/extmod/modtrezorcrypto/rand.c

33 lines
539 B
C
Raw Normal View History

2016-10-11 12:05:55 +00:00
/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
2016-04-27 16:45:00 +00:00
#include "rand.h"
2017-12-15 15:56:08 +00:00
#include "rng.h"
2016-04-27 16:45:00 +00:00
uint32_t random32(void)
{
return rng_get();
}
uint32_t random_uniform(uint32_t n)
{
uint32_t x, max = 0xFFFFFFFF - (0xFFFFFFFF % n);
while ((x = random32()) >= max);
return x / (max / n);
}
void random_buffer(uint8_t *buf, size_t len)
{
uint32_t r = 0;
2017-12-15 15:56:08 +00:00
for (size_t i = 0; i < len; i++) {
2016-04-27 16:45:00 +00:00
if (i % 4 == 0) {
r = random32();
}
buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
}
}