2016-05-17 18:20:24 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:20:24 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/rand.h>
|
2017-04-18 18:50:00 +00:00
|
|
|
#include "testutil.h"
|
2002-11-28 18:54:30 +00:00
|
|
|
|
1998-12-21 10:52:47 +00:00
|
|
|
/* some FIPS 140-1 random number test */
|
|
|
|
/* some simple tests */
|
|
|
|
|
2017-04-18 18:50:00 +00:00
|
|
|
static int fips_random_tests(void)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
unsigned char buf[2500];
|
2017-04-18 18:50:00 +00:00
|
|
|
int i, j, k, s, sign, nsign, ret = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned long n1;
|
|
|
|
unsigned long n2[16];
|
|
|
|
unsigned long runs[2][34];
|
|
|
|
long d;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2017-04-18 18:50:00 +00:00
|
|
|
if (!TEST_int_ge(RAND_bytes(buf, sizeof(buf)), 0))
|
|
|
|
return 0;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
n1 = 0;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
n2[i] = 0;
|
|
|
|
for (i = 0; i < 34; i++)
|
|
|
|
runs[0][i] = runs[1][i] = 0;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* test 1 and 2 */
|
|
|
|
sign = 0;
|
|
|
|
nsign = 0;
|
|
|
|
for (i = 0; i < 2500; i++) {
|
|
|
|
j = buf[i];
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
n2[j & 0x0f]++;
|
|
|
|
n2[(j >> 4) & 0x0f]++;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
for (k = 0; k < 8; k++) {
|
|
|
|
s = (j & 0x01);
|
|
|
|
if (s == sign)
|
|
|
|
nsign++;
|
|
|
|
else {
|
|
|
|
if (nsign > 34)
|
|
|
|
nsign = 34;
|
|
|
|
if (nsign != 0) {
|
|
|
|
runs[sign][nsign - 1]++;
|
|
|
|
if (nsign > 6)
|
|
|
|
runs[sign][5]++;
|
|
|
|
}
|
|
|
|
sign = s;
|
|
|
|
nsign = 1;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (s)
|
|
|
|
n1++;
|
|
|
|
j >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nsign > 34)
|
|
|
|
nsign = 34;
|
|
|
|
if (nsign != 0)
|
|
|
|
runs[sign][nsign - 1]++;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* test 1 */
|
2017-04-18 18:50:00 +00:00
|
|
|
if (!TEST_true(9654 < n1 && n1 < 10346)) {
|
|
|
|
TEST_info("test 1 failed, X=%lu", n1);
|
|
|
|
ret = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* test 2 */
|
|
|
|
d = 0;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
d += n2[i] * n2[i];
|
|
|
|
d = (d * 8) / 25 - 500000;
|
2017-04-18 18:50:00 +00:00
|
|
|
if (!TEST_true(103 < d && d < 5740)) {
|
|
|
|
TEST_info("test 2 failed, X=%ld.%02ld", d / 100L, d % 100L);
|
|
|
|
ret = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test 3 */
|
|
|
|
for (i = 0; i < 2; i++) {
|
2017-04-18 18:50:00 +00:00
|
|
|
if (!TEST_true(2267 < runs[i][0] && runs[i][0] < 2733)
|
|
|
|
|| !TEST_true(1079 < runs[i][1] && runs[i][1] < 1421)
|
|
|
|
|| !TEST_true(502 < runs[i][2] && runs[i][2] < 748)
|
|
|
|
|| !TEST_true(223 < runs[i][3] && runs[i][3] < 402)
|
|
|
|
|| !TEST_true(90 < runs[i][4] && runs[i][4] < 223)
|
|
|
|
|| !TEST_true(90 < runs[i][5] && runs[i][5] < 223)) {
|
|
|
|
TEST_info("During run %d", i);
|
|
|
|
ret = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/* test 4 */
|
2017-04-18 18:50:00 +00:00
|
|
|
if (!TEST_int_eq(runs[0][33], 0)
|
|
|
|
|| !TEST_int_eq(runs[1][33], 0))
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_tests(void)
|
|
|
|
{
|
|
|
|
ADD_TEST(fips_random_tests);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|