~ubuntu-branches/ubuntu/precise/krb5/precise-updates

« back to all changes in this revision

Viewing changes to src/lib/crypto/krb/t_fortuna.c

  • Committer: Package Import Robot
  • Author(s): Sam Hartman
  • Date: 2011-12-01 19:34:41 UTC
  • mfrom: (28.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20111201193441-9tipg3aru1jsidyv
Tags: 1.10+dfsg~alpha1-6
* Fix segfault with unknown hostnames in krb5_sname_to_principal,
  Closes: #650671
* Indicate that this library breaks libsmbclient versions that depend on
  krb5_locate_kdc, Closes: #650603, #650611

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
 
2
/* lib/crypto/krb/t_fortuna.c - Fortuna test program */
 
3
/*
 
4
 * Copyright (c) 2007 Kungliga Tekniska Högskolan
 
5
 * (Royal Institute of Technology, Stockholm, Sweden).
 
6
 * All rights reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
11
 *
 
12
 * 1. Redistributions of source code must retain the above copyright
 
13
 *    notice, this list of conditions and the following disclaimer.
 
14
 *
 
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.
 
18
 *
 
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.
 
22
 *
 
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
 
33
 * SUCH DAMAGE.
 
34
 */
 
35
/*
 
36
 * Copyright (C) 2011 by the Massachusetts Institute of Technology.
 
37
 * All rights reserved.
 
38
 *
 
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.
 
43
 *
 
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.
 
57
 */
 
58
 
 
59
#include "k5-int.h"
 
60
#ifdef FORTUNA
 
61
 
 
62
/* Include most of prng_fortuna.c so we can test the PRNG internals. */
 
63
#define TEST
 
64
#include "prng_fortuna.c"
 
65
 
 
66
static void
 
67
display(const unsigned char *data, size_t len)
 
68
{
 
69
    size_t i;
 
70
 
 
71
    for (i = 0; i < len; i++)
 
72
        printf("%02X", data[i]);
 
73
    printf("\n");
 
74
}
 
75
 
 
76
/*
 
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.
 
82
 */
 
83
static void
 
84
head_tail_test(struct fortuna_state *st)
 
85
{
 
86
    static unsigned char buffer[1024 * 1024];
 
87
    unsigned char c;
 
88
    size_t i, len = sizeof(buffer);
 
89
    int bit, bits[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 
90
    double res;
 
91
 
 
92
    memset(buffer, 0, len);
 
93
 
 
94
    generator_output(st, buffer, len);
 
95
    for (i = 0; i < len; i++) {
 
96
        c = buffer[i];
 
97
        for (bit = 0; bit < 8 && c; bit++) {
 
98
            if (c & 1)
 
99
                bits[bit]++;
 
100
            c = c >> 1;
 
101
        }
 
102
    }
 
103
 
 
104
    for (bit = 0; bit < 8; bit++) {
 
105
        res = ((double)abs(len - bits[bit] * 2)) / (double)len;
 
106
        if (res > 0.005){
 
107
            fprintf(stderr,
 
108
                    "Bit %d: %d zero, %d one exceeds 0.5%% variance (%f)\n",
 
109
                    bit, (int)len - bits[bit], bits[bit], res);
 
110
            exit(1);
 
111
        }
 
112
    }
 
113
}
 
114
 
 
115
int
 
116
main(int argc, char **argv)
 
117
{
 
118
    struct fortuna_state test_state;
 
119
    struct fortuna_state *st = &test_state;
 
120
    static unsigned char buf[2 * 1024 * 1024];
 
121
    unsigned int i;
 
122
 
 
123
    /* Seed the generator with a known state. */
 
124
    init_state(&test_state);
 
125
    generator_reseed(st, (unsigned char *)"test", 4);
 
126
 
 
127
    /* Generate two pieces of output; key should change for each request. */
 
128
    generator_output(st, buf, 32);
 
129
    display(buf, 32);
 
130
    generator_output(st, buf, 32);
 
131
    display(buf, 32);
 
132
 
 
133
    /* Generate a lot of output to test key changes during request. */
 
134
    generator_output(st, buf, sizeof(buf));
 
135
    display(buf, 32);
 
136
    display(buf + sizeof(buf) - 32, 32);
 
137
 
 
138
    /* Reseed the generator and generate more output. */
 
139
    generator_reseed(st, (unsigned char *)"retest", 6);
 
140
    generator_output(st, buf, 32);
 
141
    display(buf, 32);
 
142
 
 
143
    /* Add sample data to accumulator pools. */
 
144
    for (i = 0; i < 44; i++) {
 
145
        store_32_be(i, buf);
 
146
        accumulator_add_event(st, buf, 4);
 
147
    }
 
148
    assert(st->pool_index == 12);
 
149
    assert(st->pool0_bytes == 8);
 
150
 
 
151
    /* Exercise accumulator reseeds. */
 
152
    accumulator_reseed(st);
 
153
    generator_output(st, buf, 32);
 
154
    display(buf, 32);
 
155
    accumulator_reseed(st);
 
156
    generator_output(st, buf, 32);
 
157
    display(buf, 32);
 
158
    accumulator_reseed(st);
 
159
    generator_output(st, buf, 32);
 
160
    display(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);
 
165
    display(buf, 32);
 
166
 
 
167
    head_tail_test(st);
 
168
    return 0;
 
169
}
 
170
 
 
171
#else /* FORTUNA */
 
172
 
 
173
int
 
174
main()
 
175
{
 
176
    return 0;
 
177
}
 
178
 
 
179
#endif /* FORTUNA */