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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#define TEST_NAME "randombytes"
#include "cmptest.h"
static unsigned char x[65536];
static unsigned long long freq[256];
static int
compat_tests(void)
{
size_t i;
memset(x, 0, sizeof x);
randombytes(x, sizeof x);
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < sizeof x; ++i) {
++freq[255 & (int) x[i]];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("nacl_tests failed\n");
}
}
return 0;
}
static int
randombytes_tests(void)
{
static const unsigned char seed[randombytes_SEEDBYTES] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
};
unsigned char out[100];
unsigned int f = 0U;
unsigned int i;
uint32_t n;
#ifndef BENCHMARKS
# ifdef __EMSCRIPTEN__
assert(strcmp(randombytes_implementation_name(), "js") == 0);
# elif defined(__native_client__)
assert(strcmp(randombytes_implementation_name(), "nativeclient") == 0);
# else
assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
# endif
#endif
randombytes(x, 1U);
do {
n = randombytes_random();
f |= ((n >> 24) > 1);
f |= ((n >> 16) > 1) << 1;
f |= ((n >> 8) > 1) << 2;
f |= ((n) > 1) << 3;
f |= (n > 0x7fffffff) << 4;
} while (f != 0x1f);
randombytes_close();
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < 65536; ++i) {
++freq[randombytes_uniform(256)];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("randombytes_uniform() test failed\n");
}
}
assert(randombytes_uniform(1U) == 0U);
randombytes_close();
#ifndef __EMSCRIPTEN__
randombytes_set_implementation(&randombytes_salsa20_implementation);
assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
#endif
randombytes_stir();
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < 65536; ++i) {
++freq[randombytes_uniform(256)];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("randombytes_uniform() test failed\n");
}
}
memset(x, 0, sizeof x);
randombytes_buf(x, sizeof x);
for (i = 0; i < 256; ++i) {
freq[i] = 0;
}
for (i = 0; i < sizeof x; ++i) {
++freq[255 & (int) x[i]];
}
for (i = 0; i < 256; ++i) {
if (!freq[i]) {
printf("randombytes_buf() test failed\n");
}
}
assert(randombytes_uniform(1U) == 0U);
randombytes_buf_deterministic(out, sizeof out, seed);
for (i = 0; i < sizeof out; ++i) {
printf("%02x", out[i]);
}
printf(" (deterministic)\n");
randombytes_close();
randombytes(x, 1U);
randombytes_close();
assert(randombytes_SEEDBYTES > 0);
assert(randombytes_seedbytes() == randombytes_SEEDBYTES);
return 0;
}
static uint32_t
randombytes_uniform_impl(const uint32_t upper_bound)
{
return upper_bound;
}
static int
impl_tests(void)
{
#ifndef __native_client__
randombytes_implementation impl = randombytes_sysrandom_implementation;
#else
randombytes_implementation impl = randombytes_nativeclient_implementation;
#endif
uint32_t v = randombytes_random();
impl.uniform = randombytes_uniform_impl;
randombytes_close();
randombytes_set_implementation(&impl);
assert(randombytes_uniform(1) == 1);
assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v);
assert(randombytes_uniform(v) == v);
randombytes_close();
impl.close = NULL;
randombytes_close();
return 0;
}
int
main(void)
{
compat_tests();
randombytes_tests();
#ifndef __EMSCRIPTEN__
impl_tests();
#endif
printf("OK\n");
#ifndef __EMSCRIPTEN__
randombytes_set_implementation(&randombytes_salsa20_implementation);
#endif
return 0;
}
|