2014-09-14 12:43:53 -07:00
|
|
|
|
|
|
|
#define TEST_NAME "randombytes"
|
|
|
|
#include "cmptest.h"
|
2013-01-20 15:03:05 -07:00
|
|
|
|
2017-02-23 03:21:30 -07:00
|
|
|
static unsigned char x[65536];
|
2015-11-23 08:07:13 -07:00
|
|
|
static unsigned long long freq[256];
|
2013-01-20 15:03:05 -07:00
|
|
|
|
2017-02-23 03:21:30 -07:00
|
|
|
static int
|
|
|
|
compat_tests(void)
|
2013-01-20 15:03:05 -07:00
|
|
|
{
|
2014-09-14 12:43:53 -07:00
|
|
|
size_t i;
|
2013-01-20 15:03:05 -07:00
|
|
|
|
2014-09-14 12:43:53 -07:00
|
|
|
memset(x, 0, sizeof x);
|
2014-09-14 11:32:55 -07:00
|
|
|
randombytes(x, sizeof x);
|
|
|
|
for (i = 0; i < 256; ++i) {
|
2014-09-14 10:34:16 -07:00
|
|
|
freq[i] = 0;
|
|
|
|
}
|
2014-09-14 11:32:55 -07:00
|
|
|
for (i = 0; i < sizeof x; ++i) {
|
2017-02-23 03:21:30 -07:00
|
|
|
++freq[255 & (int) x[i]];
|
2014-09-14 10:34:16 -07:00
|
|
|
}
|
2014-09-14 11:32:55 -07:00
|
|
|
for (i = 0; i < 256; ++i) {
|
2014-09-14 10:34:16 -07:00
|
|
|
if (!freq[i]) {
|
2014-09-14 12:43:53 -07:00
|
|
|
printf("nacl_tests failed\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:21:30 -07:00
|
|
|
static int
|
|
|
|
randombytes_tests(void)
|
2014-09-14 12:43:53 -07:00
|
|
|
{
|
2017-02-26 08:10:32 -07:00
|
|
|
static const unsigned char seed[randombytes_SEEDBYTES] = {
|
2017-02-23 03:21:30 -07:00
|
|
|
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
|
2017-02-19 10:40:29 -07:00
|
|
|
};
|
|
|
|
unsigned char out[100];
|
|
|
|
unsigned int f = 0U;
|
|
|
|
unsigned int i;
|
|
|
|
uint32_t n;
|
2014-09-14 12:43:53 -07:00
|
|
|
|
2017-08-04 14:01:16 -07:00
|
|
|
#ifndef BENCHMARKS
|
|
|
|
# ifdef __EMSCRIPTEN__
|
2016-04-11 16:23:14 -07:00
|
|
|
assert(strcmp(randombytes_implementation_name(), "js") == 0);
|
2017-08-04 14:01:16 -07:00
|
|
|
# else
|
2016-04-11 16:23:14 -07:00
|
|
|
assert(strcmp(randombytes_implementation_name(), "sysrandom") == 0);
|
2017-08-04 14:01:16 -07:00
|
|
|
# endif
|
2015-01-17 13:42:50 -07:00
|
|
|
#endif
|
2014-09-16 20:46:43 -07:00
|
|
|
randombytes(x, 1U);
|
2015-01-18 02:34:31 -07:00
|
|
|
do {
|
|
|
|
n = randombytes_random();
|
|
|
|
f |= ((n >> 24) > 1);
|
|
|
|
f |= ((n >> 16) > 1) << 1;
|
2017-02-23 03:21:30 -07:00
|
|
|
f |= ((n >> 8) > 1) << 2;
|
|
|
|
f |= ((n) > 1) << 3;
|
2015-01-18 02:34:31 -07:00
|
|
|
f |= (n > 0x7fffffff) << 4;
|
|
|
|
} while (f != 0x1f);
|
2014-09-16 20:46:43 -07:00
|
|
|
randombytes_close();
|
|
|
|
|
2014-09-14 12:43:53 -07:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2014-09-16 20:46:43 -07:00
|
|
|
assert(randombytes_uniform(1U) == 0U);
|
2014-09-14 12:43:53 -07:00
|
|
|
randombytes_close();
|
2015-01-17 13:42:50 -07:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2019-03-17 11:25:32 -07:00
|
|
|
assert(&randombytes_internal_implementation == &randombytes_salsa20_implementation);
|
|
|
|
randombytes_set_implementation(&randombytes_internal_implementation);
|
|
|
|
assert(strcmp(randombytes_implementation_name(), "internal") == 0);
|
2015-01-17 13:42:50 -07:00
|
|
|
#endif
|
2014-09-14 12:43:53 -07:00
|
|
|
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) {
|
2017-02-23 03:21:30 -07:00
|
|
|
++freq[255 & (int) x[i]];
|
2014-09-14 12:43:53 -07:00
|
|
|
}
|
|
|
|
for (i = 0; i < 256; ++i) {
|
|
|
|
if (!freq[i]) {
|
|
|
|
printf("randombytes_buf() test failed\n");
|
2014-09-14 10:34:16 -07:00
|
|
|
}
|
|
|
|
}
|
2014-09-16 20:46:43 -07:00
|
|
|
assert(randombytes_uniform(1U) == 0U);
|
2017-02-19 10:40:29 -07:00
|
|
|
|
|
|
|
randombytes_buf_deterministic(out, sizeof out, seed);
|
|
|
|
for (i = 0; i < sizeof out; ++i) {
|
|
|
|
printf("%02x", out[i]);
|
|
|
|
}
|
|
|
|
printf(" (deterministic)\n");
|
|
|
|
|
2014-09-16 20:46:43 -07:00
|
|
|
randombytes_close();
|
|
|
|
|
|
|
|
randombytes(x, 1U);
|
2014-09-14 12:43:53 -07:00
|
|
|
randombytes_close();
|
|
|
|
|
2017-02-19 11:10:08 -07:00
|
|
|
assert(randombytes_SEEDBYTES > 0);
|
|
|
|
assert(randombytes_seedbytes() == randombytes_SEEDBYTES);
|
|
|
|
|
2014-09-14 12:43:53 -07:00
|
|
|
return 0;
|
2014-09-14 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
2017-02-23 03:21:30 -07:00
|
|
|
static uint32_t
|
|
|
|
randombytes_uniform_impl(const uint32_t upper_bound)
|
2015-05-09 06:54:18 -07:00
|
|
|
{
|
|
|
|
return upper_bound;
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:21:30 -07:00
|
|
|
static int
|
|
|
|
impl_tests(void)
|
2015-05-09 06:54:18 -07:00
|
|
|
{
|
|
|
|
randombytes_implementation impl = randombytes_sysrandom_implementation;
|
|
|
|
uint32_t v = randombytes_random();
|
|
|
|
|
|
|
|
impl.uniform = randombytes_uniform_impl;
|
|
|
|
randombytes_close();
|
|
|
|
randombytes_set_implementation(&impl);
|
2017-08-04 14:13:29 -07:00
|
|
|
assert(randombytes_uniform(1) == 1);
|
2015-05-09 06:54:18 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-23 03:21:30 -07:00
|
|
|
int
|
|
|
|
main(void)
|
2014-09-14 11:32:55 -07:00
|
|
|
{
|
2014-09-14 12:43:53 -07:00
|
|
|
compat_tests();
|
|
|
|
randombytes_tests();
|
2015-05-09 06:54:18 -07:00
|
|
|
#ifndef __EMSCRIPTEN__
|
|
|
|
impl_tests();
|
|
|
|
#endif
|
2014-09-14 12:43:53 -07:00
|
|
|
printf("OK\n");
|
2014-09-14 11:32:55 -07:00
|
|
|
|
2017-12-20 16:32:42 -07:00
|
|
|
#ifndef __EMSCRIPTEN__
|
2017-08-04 14:01:16 -07:00
|
|
|
randombytes_set_implementation(&randombytes_salsa20_implementation);
|
2017-12-20 16:32:42 -07:00
|
|
|
#endif
|
2017-08-04 14:01:16 -07:00
|
|
|
|
2014-09-14 10:34:16 -07:00
|
|
|
return 0;
|
2013-01-20 15:03:05 -07:00
|
|
|
}
|