Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
rng.c
Go to the documentation of this file.
1#include "rng.h"
2
3#ifndef USE_OPENSSL_RAND
4#include <stdlib.h>
5#include <unistd.h>
6#include <fcntl.h>
7
8#ifdef ENABLE_CT_TESTING
9#include <valgrind/memcheck.h>
10#endif
11
12#include <stdio.h>
13void randombytes(void *x, size_t l)
14{
15 static __thread int fd = -1;
16 ssize_t n;
17 if (fd < 0 && 0 > (fd = open("/dev/urandom", O_RDONLY)))
18 exit(1);
19 for (size_t i = 0; i < l; i += n)
20 if (0 >= (n = read(fd, (char *) x + i, l - i)))
21 exit(2);
22#ifdef ENABLE_CT_TESTING
23 VALGRIND_MAKE_MEM_UNDEFINED(x, l);
24#endif
25}
26#else
27
28#include <stdlib.h>
29#include <stdint.h>
30#include <limits.h>
31#include <stdio.h>
32
33static inline int randombytes_from_openssl(uint8_t *buf, int bytes);
34
35void randombytes(void *x, size_t l)
36{
37 int l_as_int;
38
39 if (l <= INT_MAX) {
40 l_as_int = (int) l;
41 } else {
42 fprintf(stderr, "Error: requested too many random bytes (%zu > %d)\n",
43 l, INT_MAX);
44 exit(EXIT_FAILURE);
45 }
46 if (!randombytes_from_openssl((uint8_t*)x, l_as_int))
47 exit(EXIT_FAILURE);
48}
49
50#include <openssl/rand.h>
51
52#define RAND_POLL_RETRY 3 // randomness could be temporarily unavailable, do not panic immediately
53static inline
54int randombytes_from_openssl(uint8_t *buf, int bytes)
55{
56 int i;
57 for (i=0; i < RAND_POLL_RETRY; i++) {
58 if (RAND_status() == 1)
59 break;
60 RAND_poll();
61 }
62 if (RAND_priv_bytes(buf, bytes) != 1) {
63 fprintf(stderr, "Failure retrieving randomness from OpenSSL\n");
64 return 0;
65 }
66 return 1;
67}
68#endif
void randombytes(void *x, size_t l)
Definition rng.c:8
for i