1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* lib/crypto/krb/t_fortuna.c - Fortuna test program */
4
* Copyright (c) 2007 Kungliga Tekniska Högskolan
5
* (Royal Institute of Technology, Stockholm, Sweden).
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
19
* 3. Neither the name of the Institute nor the names of its contributors
20
* may be used to endorse or promote products derived from this software
21
* without specific prior written permission.
23
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
* Copyright (C) 2011 by the Massachusetts Institute of Technology.
37
* All rights reserved.
39
* Export of this software from the United States of America may
40
* require a specific license from the United States Government.
41
* It is the responsibility of any person or organization contemplating
42
* export to obtain such a license before exporting.
44
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
45
* distribute this software and its documentation for any purpose and
46
* without fee is hereby granted, provided that the above copyright
47
* notice appear in all copies and that both that copyright notice and
48
* this permission notice appear in supporting documentation, and that
49
* the name of M.I.T. not be used in advertising or publicity pertaining
50
* to distribution of the software without specific, written prior
51
* permission. Furthermore if you modify this software you must label
52
* your software as modified software and not distribute it in such a
53
* fashion that it might be confused with the original M.I.T. software.
54
* M.I.T. makes no representations about the suitability of
55
* this software for any purpose. It is provided "as is" without express
56
* or implied warranty.
62
/* Include most of prng_fortuna.c so we can test the PRNG internals. */
64
#include "prng_fortuna.c"
67
display(const unsigned char *data, size_t len)
71
for (i = 0; i < len; i++)
72
printf("%02X", data[i]);
77
* Generate data from st with its current internal state and check for
78
* significant bias in each bit of the resulting bytes. This test would have a
79
* small chance of failure on random inputs, but we have a predictable state
80
* after all the other tests have been run, so it will never fail if the PRNG
81
* operates the way we expect.
84
head_tail_test(struct fortuna_state *st)
86
static unsigned char buffer[1024 * 1024];
88
size_t i, len = sizeof(buffer);
89
int bit, bits[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
92
memset(buffer, 0, len);
94
generator_output(st, buffer, len);
95
for (i = 0; i < len; i++) {
97
for (bit = 0; bit < 8 && c; bit++) {
104
for (bit = 0; bit < 8; bit++) {
105
res = ((double)abs(len - bits[bit] * 2)) / (double)len;
108
"Bit %d: %d zero, %d one exceeds 0.5%% variance (%f)\n",
109
bit, (int)len - bits[bit], bits[bit], res);
116
main(int argc, char **argv)
118
struct fortuna_state test_state;
119
struct fortuna_state *st = &test_state;
120
static unsigned char buf[2 * 1024 * 1024];
123
/* Seed the generator with a known state. */
124
init_state(&test_state);
125
generator_reseed(st, (unsigned char *)"test", 4);
127
/* Generate two pieces of output; key should change for each request. */
128
generator_output(st, buf, 32);
130
generator_output(st, buf, 32);
133
/* Generate a lot of output to test key changes during request. */
134
generator_output(st, buf, sizeof(buf));
136
display(buf + sizeof(buf) - 32, 32);
138
/* Reseed the generator and generate more output. */
139
generator_reseed(st, (unsigned char *)"retest", 6);
140
generator_output(st, buf, 32);
143
/* Add sample data to accumulator pools. */
144
for (i = 0; i < 44; i++) {
146
accumulator_add_event(st, buf, 4);
148
assert(st->pool_index == 12);
149
assert(st->pool0_bytes == 8);
151
/* Exercise accumulator reseeds. */
152
accumulator_reseed(st);
153
generator_output(st, buf, 32);
155
accumulator_reseed(st);
156
generator_output(st, buf, 32);
158
accumulator_reseed(st);
159
generator_output(st, buf, 32);
161
for (i = 0; i < 1000; i++)
162
accumulator_reseed(st);
163
assert(st->reseed_count == 1003);
164
generator_output(st, buf, 32);