Merge pull request #8782 from kaspar030/add_random_bytes
sys/random: add random_bytes()
This commit is contained in:
commit
d9cabdf73f
1
pkg/tweetnacl/Makefile.dep
Normal file
1
pkg/tweetnacl/Makefile.dep
Normal file
@ -0,0 +1 @@
|
|||||||
|
USEMODULE+=random
|
||||||
@ -15,15 +15,6 @@
|
|||||||
|
|
||||||
void randombytes(uint8_t *target, uint64_t n)
|
void randombytes(uint8_t *target, uint64_t n)
|
||||||
{
|
{
|
||||||
uint32_t random;
|
/* tweetnacl needs uint64_t as "n" parameter, random provides uint32 */
|
||||||
uint8_t *random_pos = (uint8_t*)&random;
|
random_bytes(target, n);
|
||||||
unsigned _n = 0;
|
|
||||||
|
|
||||||
while (n--) {
|
|
||||||
if (! (_n++ & 0x3)) {
|
|
||||||
random = random_uint32();
|
|
||||||
random_pos = (uint8_t *) &random;
|
|
||||||
}
|
|
||||||
*target++ = *random_pos++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
SRC := seed.c
|
SRC := random.c
|
||||||
|
|
||||||
BASE_MODULE := prng
|
BASE_MODULE := prng
|
||||||
SUBMODULES := 1
|
SUBMODULES := 1
|
||||||
|
|||||||
@ -39,3 +39,18 @@ void auto_init_random(void)
|
|||||||
DEBUG("random: using seed value %u\n", (unsigned)seed);
|
DEBUG("random: using seed value %u\n", (unsigned)seed);
|
||||||
random_init(seed);
|
random_init(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void random_bytes(uint8_t *target, size_t n)
|
||||||
|
{
|
||||||
|
uint32_t random;
|
||||||
|
uint8_t *random_pos = (uint8_t*)&random;
|
||||||
|
unsigned _n = 0;
|
||||||
|
|
||||||
|
while (n--) {
|
||||||
|
if (! (_n++ & 0x3)) {
|
||||||
|
random = random_uint32();
|
||||||
|
random_pos = (uint8_t *) &random;
|
||||||
|
}
|
||||||
|
*target++ = *random_pos++;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -44,36 +44,6 @@ uint32_t random_uint32(void)
|
|||||||
return tinymt32_generate_uint32(&_random);
|
return tinymt32_generate_uint32(&_random);
|
||||||
}
|
}
|
||||||
|
|
||||||
void random_bytes(uint8_t *buf, size_t size)
|
|
||||||
{
|
|
||||||
size_t iter = size;
|
|
||||||
size_t diff = _align(buf) - buf;
|
|
||||||
uint32_t tmp;
|
|
||||||
|
|
||||||
/* Fill first <4 unaligned bytes */
|
|
||||||
if (diff) {
|
|
||||||
tmp = tinymt32_generate_uint32(&_random);
|
|
||||||
if (diff > size) {
|
|
||||||
diff = size;
|
|
||||||
}
|
|
||||||
memcpy(buf, &tmp, diff);
|
|
||||||
iter -= diff;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fill aligned bytes */
|
|
||||||
while (iter >= sizeof(uint32_t)) {
|
|
||||||
*((uint32_t *) buf) = tinymt32_generate_uint32(&_random);
|
|
||||||
buf += sizeof(uint32_t);
|
|
||||||
iter -= sizeof(uint32_t);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fill last bytes */
|
|
||||||
if (iter) {
|
|
||||||
tmp = tinymt32_generate_uint32(&_random);
|
|
||||||
memcpy(buf, &tmp, iter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void random_init_by_array(uint32_t init_key[], int key_length)
|
void random_init_by_array(uint32_t init_key[], int key_length)
|
||||||
{
|
{
|
||||||
tinymt32_init_by_array(&_random, init_key, key_length);
|
tinymt32_init_by_array(&_random, init_key, key_length);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user