trezor-core/embed/extmod/modtrezorcrypto/rand.c

33 lines
539 B
C

/*
* Copyright (c) Pavol Rusnak, SatoshiLabs
*
* Licensed under TREZOR License
* see LICENSE file for details
*/
#include "rand.h"
#include "rng.h"
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;
for (size_t i = 0; i < len; i++) {
if (i % 4 == 0) {
r = random32();
}
buf[i] = (r >> ((i % 4) * 8)) & 0xFF;
}
}