forked from siriobalmelli/nonlibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlc_urand_test.c
83 lines (58 loc) · 1.65 KB
/
nlc_urand_test.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* nlc_urand_test.c
Validate use of nonlibc_urand.
Show use of nlc_urand() to initialize an RNG for the purposes of writing
many random bytes to memory.
*/
#include <ndebug.h>
#include <nlc_urand.h>
#include <stdlib.h> /* malloc() */
/* test_IV()
Test using nlc_urand() to get an Initialization Vector.
The desired property is it just works and DOESN'T BLOCK; thank you very much.
*/
int test_IV()
{
int err_cnt = 0;
const int test_iter = 100000;
/* reasonably the LARGEST Initialization Vector I can imagine needing */
uint64_t words[32];
for (int i=0; i < test_iter; i++) {
NB_die_if(nlc_urand(words, sizeof(words)) != sizeof(words),
"we really expect this not to have blocked");
}
die:
return err_cnt;
}
#include <pcg_rand.h>
/* test_big_random()
Show use of nlc_urand() to initialize an RNG (pcg in this case; from this same library),
then generating many many bytes.
*/
int test_big_random()
{
int err_cnt = 0;
const size_t size = 20000000; /* 20MB */
void *mem_a = NULL, *mem_b = NULL;
NB_die_if(!( mem_a = calloc(1, size) ), "size %zu", size);
NB_die_if(!( mem_b = calloc(1, size) ), "size %zu", size);
uint64_t seeds[2];
/* get seeds and generate random memory */
NB_die_if(nlc_urand(seeds, sizeof(seeds)) != sizeof(seeds), "");
pcg_randset(mem_a, size, seeds[0], seeds[1]);
NB_die_if(nlc_urand(seeds, sizeof(seeds)) != sizeof(seeds), "");
pcg_randset(mem_b, size, seeds[0], seeds[1]);
NB_die_if(!memcmp(mem_a, mem_b, size), "these should NEVER be identical");
die:
free(mem_a);
free(mem_b);
return err_cnt;
}
/* main()
*/
int main()
{
int err_cnt = 0;
err_cnt += test_IV();
err_cnt += test_big_random();
return err_cnt;
}