2022-08-14 16:19:40 +00:00
|
|
|
/************************************************************//**
|
|
|
|
*
|
|
|
|
* @file: unix_rng.c
|
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 06/03/2020
|
|
|
|
* @revision:
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
|
|
|
|
|
|
|
#include<stdio.h>
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
2023-04-18 16:05:54 +00:00
|
|
|
#include"platform_log.h"
|
2022-08-14 16:19:40 +00:00
|
|
|
#include"typedefs.h"
|
|
|
|
|
|
|
|
int RandomSeedFromDevice()
|
|
|
|
{
|
|
|
|
FILE* urandom = fopen("/dev/urandom", "r");
|
|
|
|
if(!urandom)
|
|
|
|
{
|
2023-04-18 16:05:54 +00:00
|
|
|
log_error("can't open /dev/urandom\n");
|
2022-08-14 16:19:40 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
u32 u;
|
|
|
|
char buff[4];
|
|
|
|
} seed;
|
|
|
|
|
|
|
|
int size = fread(seed.buff, 1, 4, urandom);
|
|
|
|
if(size != 4)
|
|
|
|
{
|
2023-04-18 16:05:54 +00:00
|
|
|
log_error("couldn't read from /dev/urandom\n");
|
2022-08-14 16:19:40 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(urandom);
|
|
|
|
srandom(seed.u);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 RandomU32()
|
|
|
|
{
|
|
|
|
u32 u1 = (u32)random();
|
|
|
|
u32 u2 = (u32)random();
|
|
|
|
return((u1<<1) | (u2 & 0x01));
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 RandomU64()
|
|
|
|
{
|
|
|
|
u64 u1 = (u64)random();
|
|
|
|
u64 u2 = (u64)random();
|
|
|
|
u64 u3 = (u64)random();
|
|
|
|
return((u1<<33) | (u2<<2) | (u3 & 0x03));
|
|
|
|
}
|