-
Notifications
You must be signed in to change notification settings - Fork 17
/
rlwe_rand_dev_urandom.c
51 lines (42 loc) · 1.09 KB
/
rlwe_rand_dev_urandom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#warning "Using /dev/urandom to generate all randomness for sampling will result in very slow performance. See README.md and the Makefile to switch to an AES-based PRNG seeded from OpenSSL's RAND_bytes."
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "rlwe_rand.h"
int RAND_CHOICE_init(int *rand_ctx) {
int fd = open("/dev/urandom", O_RDONLY);
if (!fd) {
return 0;
}
*rand_ctx = fd;
return 1;
}
void RAND_CHOICE_cleanup(int *rand_ctx) {
if (*rand_ctx) {
close(*rand_ctx);
}
}
static void mkrandom(int *rand_ctx, uint8_t *buf, size_t len) {
read(*rand_ctx, buf, len);
}
uint8_t RANDOM8(int *rand_ctx) {
uint8_t bit;
mkrandom(rand_ctx, &bit, 1);
return bit;
}
uint32_t RANDOM32(int *rand_ctx) {
uint32_t buf;
mkrandom(rand_ctx, (uint8_t *)&buf, 4);
return buf;
}
uint64_t RANDOM64(int *rand_ctx) {
uint64_t buf;
mkrandom(rand_ctx, (uint8_t *)&buf, 8);
return buf;
}
void RANDOM192(uint64_t r[3], int *rand_ctx) {
mkrandom(rand_ctx, (uint8_t *)r, 24);
}
void *(*volatile rlwe_memset_volatile)(void *, int, size_t) = memset;