]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/randtest.c
Ignore -named_curve auto value to improve backwards compatibility
[thirdparty/openssl.git] / test / randtest.c
1 /*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/rand.h>
11 #include "testutil.h"
12
13 /* some FIPS 140-1 random number test */
14 /* some simple tests */
15
16 static int fips_random_tests(void)
17 {
18 unsigned char buf[2500];
19 int i, j, k, s, sign, nsign, ret = 1;
20 unsigned long n1;
21 unsigned long n2[16];
22 unsigned long runs[2][34];
23 long d;
24
25 if (!TEST_int_ge(RAND_bytes(buf, sizeof(buf)), 0))
26 return 0;
27
28 n1 = 0;
29 for (i = 0; i < 16; i++)
30 n2[i] = 0;
31 for (i = 0; i < 34; i++)
32 runs[0][i] = runs[1][i] = 0;
33
34 /* test 1 and 2 */
35 sign = 0;
36 nsign = 0;
37 for (i = 0; i < 2500; i++) {
38 j = buf[i];
39
40 n2[j & 0x0f]++;
41 n2[(j >> 4) & 0x0f]++;
42
43 for (k = 0; k < 8; k++) {
44 s = (j & 0x01);
45 if (s == sign)
46 nsign++;
47 else {
48 if (nsign > 34)
49 nsign = 34;
50 if (nsign != 0) {
51 runs[sign][nsign - 1]++;
52 if (nsign > 6)
53 runs[sign][5]++;
54 }
55 sign = s;
56 nsign = 1;
57 }
58
59 if (s)
60 n1++;
61 j >>= 1;
62 }
63 }
64 if (nsign > 34)
65 nsign = 34;
66 if (nsign != 0)
67 runs[sign][nsign - 1]++;
68
69 /* test 1 */
70 if (!TEST_true(9654 < n1 && n1 < 10346)) {
71 TEST_info("test 1 failed, X=%lu", n1);
72 ret = 0;
73 }
74
75 /* test 2 */
76 d = 0;
77 for (i = 0; i < 16; i++)
78 d += n2[i] * n2[i];
79 d = (d * 8) / 25 - 500000;
80 if (!TEST_true(103 < d && d < 5740)) {
81 TEST_info("test 2 failed, X=%ld.%02ld", d / 100L, d % 100L);
82 ret = 0;
83 }
84
85 /* test 3 */
86 for (i = 0; i < 2; i++) {
87 if (!TEST_true(2267 < runs[i][0] && runs[i][0] < 2733)
88 || !TEST_true(1079 < runs[i][1] && runs[i][1] < 1421)
89 || !TEST_true(502 < runs[i][2] && runs[i][2] < 748)
90 || !TEST_true(223 < runs[i][3] && runs[i][3] < 402)
91 || !TEST_true(90 < runs[i][4] && runs[i][4] < 223)
92 || !TEST_true(90 < runs[i][5] && runs[i][5] < 223)) {
93 TEST_info("During run %d", i);
94 ret = 0;
95 }
96 }
97
98 /* test 4 */
99 if (!TEST_int_eq(runs[0][33], 0)
100 || !TEST_int_eq(runs[1][33], 0))
101 ret = 0;
102
103 return ret;
104 }
105
106 void register_tests(void)
107 {
108 ADD_TEST(fips_random_tests);
109 }