Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /*
2 * stat-driver.c
3 *
4 * test driver for the stat_test functions
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
10 /*
11 *
12 * Copyright (c) 2001-2006, Cisco Systems, Inc.
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 *
19 * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials provided
25 * with the distribution.
26 *
27 * Neither the name of the Cisco Systems, Inc. nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42 * OF THE POSSIBILITY OF SUCH DAMAGE.
43 *
44 */
46 #include <stdio.h> /* for printf() */
48 #include "err.h"
49 #include "stat.h"
51 #include "cipher.h"
53 typedef struct {
54 void *state;
55 } random_source_t;
57 err_status_t
58 random_source_alloc(void);
60 void
61 err_check(err_status_t s) {
62 if (s) {
63 printf("error (code %d)\n", s);
64 exit(1);
65 }
66 }
68 int
69 main (int argc, char *argv[]) {
70 uint8_t buffer[2500];
71 unsigned int buf_len = 2500;
72 int i, j;
73 extern cipher_type_t aes_icm;
74 cipher_t *c;
75 uint8_t key[46] = {
76 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
77 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
78 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
79 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
80 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
81 0x00, 0x01, 0x02, 0x03, 0x04, 0x05
82 };
83 v128_t nonce;
84 int num_trials = 500;
85 int num_fail;
87 printf("statistical tests driver\n");
89 for (i=0; i < 2500; i++)
90 buffer[i] = 0;
92 /* run tests */
93 printf("running stat_tests on all-null buffer, expecting failure\n");
94 printf("monobit %d\n", stat_test_monobit(buffer));
95 printf("poker %d\n", stat_test_poker(buffer));
96 printf("runs %d\n", stat_test_runs(buffer));
98 for (i=0; i < 2500; i++)
99 buffer[i] = rand();
100 printf("running stat_tests on rand(), expecting success\n");
101 printf("monobit %d\n", stat_test_monobit(buffer));
102 printf("poker %d\n", stat_test_poker(buffer));
103 printf("runs %d\n", stat_test_runs(buffer));
105 printf("running stat_tests on AES-128-ICM, expecting success\n");
106 /* set buffer to cipher output */
107 for (i=0; i < 2500; i++)
108 buffer[i] = 0;
109 err_check(cipher_type_alloc(&aes_icm, &c, 30));
110 err_check(cipher_init(c, key, direction_encrypt));
111 err_check(cipher_set_iv(c, &nonce));
112 err_check(cipher_encrypt(c, buffer, &buf_len));
113 /* run tests on cipher outout */
114 printf("monobit %d\n", stat_test_monobit(buffer));
115 printf("poker %d\n", stat_test_poker(buffer));
116 printf("runs %d\n", stat_test_runs(buffer));
118 printf("runs test (please be patient): ");
119 fflush(stdout);
120 num_fail = 0;
121 v128_set_to_zero(&nonce);
122 for(j=0; j < num_trials; j++) {
123 for (i=0; i < 2500; i++)
124 buffer[i] = 0;
125 nonce.v32[3] = i;
126 err_check(cipher_set_iv(c, &nonce));
127 err_check(cipher_encrypt(c, buffer, &buf_len));
128 if (stat_test_runs(buffer)) {
129 num_fail++;
130 }
131 }
133 printf("%d failures in %d tests\n", num_fail, num_trials);
134 printf("(nota bene: a small fraction of stat_test failures does not \n"
135 "indicate that the random source is invalid)\n");
137 err_check(cipher_dealloc(c));
139 printf("running stat_tests on AES-256-ICM, expecting success\n");
140 /* set buffer to cipher output */
141 for (i=0; i < 2500; i++)
142 buffer[i] = 0;
143 err_check(cipher_type_alloc(&aes_icm, &c, 46));
144 err_check(cipher_init(c, key, direction_encrypt));
145 err_check(cipher_set_iv(c, &nonce));
146 err_check(cipher_encrypt(c, buffer, &buf_len));
147 /* run tests on cipher outout */
148 printf("monobit %d\n", stat_test_monobit(buffer));
149 printf("poker %d\n", stat_test_poker(buffer));
150 printf("runs %d\n", stat_test_runs(buffer));
152 printf("runs test (please be patient): ");
153 fflush(stdout);
154 num_fail = 0;
155 v128_set_to_zero(&nonce);
156 for(j=0; j < num_trials; j++) {
157 for (i=0; i < 2500; i++)
158 buffer[i] = 0;
159 nonce.v32[3] = i;
160 err_check(cipher_set_iv(c, &nonce));
161 err_check(cipher_encrypt(c, buffer, &buf_len));
162 if (stat_test_runs(buffer)) {
163 num_fail++;
164 }
165 }
167 printf("%d failures in %d tests\n", num_fail, num_trials);
168 printf("(nota bene: a small fraction of stat_test failures does not \n"
169 "indicate that the random source is invalid)\n");
171 err_check(cipher_dealloc(c));
173 return 0;
174 }