2014-09-14 12:43:53 -07:00
|
|
|
|
|
|
|
#define TEST_NAME "randombytes"
|
|
|
|
#include "cmptest.h"
|
2013-01-20 15:03:05 -07:00
|
|
|
|
|
|
|
unsigned char x[65536];
|
|
|
|
unsigned long long freq[256];
|
|
|
|
|
2014-09-14 12:43:53 -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) {
|
|
|
|
++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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int randombytes_tests(void)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2015-01-17 13:42:50 -07:00
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
assert(strcmp(randombytes_implementation_name(), "sysrandom"));
|
|
|
|
#else
|
|
|
|
assert(strcmp(randombytes_implementation_name(), "js"));
|
|
|
|
#endif
|
2014-09-16 20:46:43 -07:00
|
|
|
randombytes(x, 1U);
|
|
|
|
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__
|
2014-09-14 12:43:53 -07:00
|
|
|
randombytes_set_implementation(&randombytes_salsa20_implementation);
|
|
|
|
assert(strcmp(randombytes_implementation_name(), "salsa20") == 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) {
|
|
|
|
++freq[255 & (int)x[i]];
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
randombytes_close();
|
|
|
|
|
|
|
|
randombytes(x, 1U);
|
2014-09-14 12:43:53 -07:00
|
|
|
randombytes_close();
|
|
|
|
|
|
|
|
return 0;
|
2014-09-14 11:32:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2014-09-14 12:43:53 -07:00
|
|
|
compat_tests();
|
|
|
|
randombytes_tests();
|
|
|
|
printf("OK\n");
|
2014-09-14 11:32:55 -07:00
|
|
|
|
2014-09-14 10:34:16 -07:00
|
|
|
return 0;
|
2013-01-20 15:03:05 -07:00
|
|
|
}
|