randombytes.c revision 1.1 1 1.1 riastrad
2 1.1 riastrad #define TEST_NAME "randombytes"
3 1.1 riastrad #include "cmptest.h"
4 1.1 riastrad
5 1.1 riastrad static unsigned char x[65536];
6 1.1 riastrad static unsigned long long freq[256];
7 1.1 riastrad
8 1.1 riastrad static int
9 1.1 riastrad compat_tests(void)
10 1.1 riastrad {
11 1.1 riastrad size_t i;
12 1.1 riastrad
13 1.1 riastrad memset(x, 0, sizeof x);
14 1.1 riastrad randombytes(x, sizeof x);
15 1.1 riastrad for (i = 0; i < 256; ++i) {
16 1.1 riastrad freq[i] = 0;
17 1.1 riastrad }
18 1.1 riastrad for (i = 0; i < sizeof x; ++i) {
19 1.1 riastrad ++freq[255 & (int) x[i]];
20 1.1 riastrad }
21 1.1 riastrad for (i = 0; i < 256; ++i) {
22 1.1 riastrad if (!freq[i]) {
23 1.1 riastrad printf("nacl_tests failed\n");
24 1.1 riastrad }
25 1.1 riastrad }
26 1.1 riastrad return 0;
27 1.1 riastrad }
28 1.1 riastrad
29 1.1 riastrad static int
30 1.1 riastrad randombytes_tests(void)
31 1.1 riastrad {
32 1.1 riastrad static const unsigned char seed[randombytes_SEEDBYTES] = {
33 1.1 riastrad 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
34 1.1 riastrad 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
35 1.1 riastrad 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
36 1.1 riastrad };
37 1.1 riastrad unsigned char out[100];
38 1.1 riastrad unsigned int f = 0U;
39 1.1 riastrad unsigned int i;
40 1.1 riastrad uint32_t n;
41 1.1 riastrad
42 1.1 riastrad #ifndef BENCHMARKS
43 1.1 riastrad # ifdef __EMSCRIPTEN__
44 1.1 riastrad assert(strcmp(randombytes_implementation_name(), "js") == 0);
45 1.1 riastrad # elif defined(__native_client__)
46 1.1 riastrad assert(strcmp(randombytes_implementation_name(), "nativeclient") == 0);
47 1.1 riastrad # else
48 1.1 riastrad assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
49 1.1 riastrad # endif
50 1.1 riastrad #endif
51 1.1 riastrad randombytes(x, 1U);
52 1.1 riastrad do {
53 1.1 riastrad n = randombytes_random();
54 1.1 riastrad f |= ((n >> 24) > 1);
55 1.1 riastrad f |= ((n >> 16) > 1) << 1;
56 1.1 riastrad f |= ((n >> 8) > 1) << 2;
57 1.1 riastrad f |= ((n) > 1) << 3;
58 1.1 riastrad f |= (n > 0x7fffffff) << 4;
59 1.1 riastrad } while (f != 0x1f);
60 1.1 riastrad randombytes_close();
61 1.1 riastrad
62 1.1 riastrad for (i = 0; i < 256; ++i) {
63 1.1 riastrad freq[i] = 0;
64 1.1 riastrad }
65 1.1 riastrad for (i = 0; i < 65536; ++i) {
66 1.1 riastrad ++freq[randombytes_uniform(256)];
67 1.1 riastrad }
68 1.1 riastrad for (i = 0; i < 256; ++i) {
69 1.1 riastrad if (!freq[i]) {
70 1.1 riastrad printf("randombytes_uniform() test failed\n");
71 1.1 riastrad }
72 1.1 riastrad }
73 1.1 riastrad assert(randombytes_uniform(1U) == 0U);
74 1.1 riastrad randombytes_close();
75 1.1 riastrad #ifndef __EMSCRIPTEN__
76 1.1 riastrad randombytes_set_implementation(&randombytes_salsa20_implementation);
77 1.1 riastrad assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
78 1.1 riastrad #endif
79 1.1 riastrad randombytes_stir();
80 1.1 riastrad for (i = 0; i < 256; ++i) {
81 1.1 riastrad freq[i] = 0;
82 1.1 riastrad }
83 1.1 riastrad for (i = 0; i < 65536; ++i) {
84 1.1 riastrad ++freq[randombytes_uniform(256)];
85 1.1 riastrad }
86 1.1 riastrad for (i = 0; i < 256; ++i) {
87 1.1 riastrad if (!freq[i]) {
88 1.1 riastrad printf("randombytes_uniform() test failed\n");
89 1.1 riastrad }
90 1.1 riastrad }
91 1.1 riastrad memset(x, 0, sizeof x);
92 1.1 riastrad randombytes_buf(x, sizeof x);
93 1.1 riastrad for (i = 0; i < 256; ++i) {
94 1.1 riastrad freq[i] = 0;
95 1.1 riastrad }
96 1.1 riastrad for (i = 0; i < sizeof x; ++i) {
97 1.1 riastrad ++freq[255 & (int) x[i]];
98 1.1 riastrad }
99 1.1 riastrad for (i = 0; i < 256; ++i) {
100 1.1 riastrad if (!freq[i]) {
101 1.1 riastrad printf("randombytes_buf() test failed\n");
102 1.1 riastrad }
103 1.1 riastrad }
104 1.1 riastrad assert(randombytes_uniform(1U) == 0U);
105 1.1 riastrad
106 1.1 riastrad randombytes_buf_deterministic(out, sizeof out, seed);
107 1.1 riastrad for (i = 0; i < sizeof out; ++i) {
108 1.1 riastrad printf("%02x", out[i]);
109 1.1 riastrad }
110 1.1 riastrad printf(" (deterministic)\n");
111 1.1 riastrad
112 1.1 riastrad randombytes_close();
113 1.1 riastrad
114 1.1 riastrad randombytes(x, 1U);
115 1.1 riastrad randombytes_close();
116 1.1 riastrad
117 1.1 riastrad assert(randombytes_SEEDBYTES > 0);
118 1.1 riastrad assert(randombytes_seedbytes() == randombytes_SEEDBYTES);
119 1.1 riastrad
120 1.1 riastrad return 0;
121 1.1 riastrad }
122 1.1 riastrad
123 1.1 riastrad static uint32_t
124 1.1 riastrad randombytes_uniform_impl(const uint32_t upper_bound)
125 1.1 riastrad {
126 1.1 riastrad return upper_bound;
127 1.1 riastrad }
128 1.1 riastrad
129 1.1 riastrad static int
130 1.1 riastrad impl_tests(void)
131 1.1 riastrad {
132 1.1 riastrad #ifndef __native_client__
133 1.1 riastrad randombytes_implementation impl = randombytes_sysrandom_implementation;
134 1.1 riastrad #else
135 1.1 riastrad randombytes_implementation impl = randombytes_nativeclient_implementation;
136 1.1 riastrad #endif
137 1.1 riastrad uint32_t v = randombytes_random();
138 1.1 riastrad
139 1.1 riastrad impl.uniform = randombytes_uniform_impl;
140 1.1 riastrad randombytes_close();
141 1.1 riastrad randombytes_set_implementation(&impl);
142 1.1 riastrad assert(randombytes_uniform(1) == 1);
143 1.1 riastrad assert(randombytes_uniform(v) == v);
144 1.1 riastrad assert(randombytes_uniform(v) == v);
145 1.1 riastrad assert(randombytes_uniform(v) == v);
146 1.1 riastrad assert(randombytes_uniform(v) == v);
147 1.1 riastrad randombytes_close();
148 1.1 riastrad impl.close = NULL;
149 1.1 riastrad randombytes_close();
150 1.1 riastrad
151 1.1 riastrad return 0;
152 1.1 riastrad }
153 1.1 riastrad
154 1.1 riastrad int
155 1.1 riastrad main(void)
156 1.1 riastrad {
157 1.1 riastrad compat_tests();
158 1.1 riastrad randombytes_tests();
159 1.1 riastrad #ifndef __EMSCRIPTEN__
160 1.1 riastrad impl_tests();
161 1.1 riastrad #endif
162 1.1 riastrad printf("OK\n");
163 1.1 riastrad
164 1.1 riastrad randombytes_set_implementation(&randombytes_salsa20_implementation);
165 1.1 riastrad
166 1.1 riastrad return 0;
167 1.1 riastrad }
168