demo: read from urandom if someone requests real random data
This commit is contained in:
parent
ac4550ccbd
commit
cb085f06cf
28
demo/demo.c
28
demo/demo.c
@ -61,10 +61,23 @@ int lbit(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
|
||||||
|
static FILE* fd_urandom;
|
||||||
|
#endif
|
||||||
int myrng(unsigned char *dst, int len, void *dat)
|
int myrng(unsigned char *dst, int len, void *dat)
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
(void)dat;
|
(void)dat;
|
||||||
|
#if defined(LTM_DEMO_REAL_RAND)
|
||||||
|
if (!fd_urandom) {
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
fprintf(stderr, "\nno /dev/urandom\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return fread(dst, 1, len, fd_urandom);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
for (x = 0; x < len; x++)
|
for (x = 0; x < len; x++)
|
||||||
dst[x] = rand() & 0xFF;
|
dst[x] = rand() & 0xFF;
|
||||||
return len;
|
return len;
|
||||||
@ -84,6 +97,11 @@ static void _cleanup(void)
|
|||||||
{
|
{
|
||||||
mp_clear_multi(&a, &b, &c, &d, &e, &f, NULL);
|
mp_clear_multi(&a, &b, &c, &d, &e, &f, NULL);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
|
#ifdef LTM_DEMO_REAL_RAND
|
||||||
|
if(fd_urandom)
|
||||||
|
fclose(fd_urandom);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
char cmd[4096], buf[4096];
|
char cmd[4096], buf[4096];
|
||||||
@ -107,6 +125,16 @@ int main(void)
|
|||||||
|
|
||||||
atexit(_cleanup);
|
atexit(_cleanup);
|
||||||
|
|
||||||
|
#if defined(LTM_DEMO_REAL_RAND)
|
||||||
|
if (!fd_urandom) {
|
||||||
|
fd_urandom = fopen("/dev/urandom", "r");
|
||||||
|
if (!fd_urandom) {
|
||||||
|
#if !defined(_WIN32)
|
||||||
|
fprintf(stderr, "\ncould not open /dev/urandom\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
srand(LTM_DEMO_RAND_SEED);
|
srand(LTM_DEMO_RAND_SEED);
|
||||||
|
|
||||||
#ifdef MP_8BIT
|
#ifdef MP_8BIT
|
||||||
|
Loading…
Reference in New Issue
Block a user