yarrow-test.c 6.65 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
2
3
#include "yarrow.h"

#include "macros.h"
4
#include "testutils.h"
Niels Möller's avatar
Niels Möller committed
5
6

#include <assert.h>
7
#include <errno.h>
Niels Möller's avatar
Niels Möller committed
8
9
#include <stdio.h>
#include <stdlib.h>
10
#include <string.h>
Niels Möller's avatar
Niels Möller committed
11

12
13
static int verbose = 0;

Niels Möller's avatar
Niels Möller committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Lagged fibonacci sequence as described in Knuth 3.6 */

#define KK 100
#define LL 37
#define MM (1UL << 30)
#define TT 70

uint32_t ran_x[KK];
unsigned ran_index;

static void
ran_init(uint32_t seed)
{
  uint32_t t,j;
  uint32_t x[2*KK - 1];
  uint32_t ss = (seed + 2) & (MM-2);

  for (j = 0; j<KK; j++)
    {
      x[j] = ss;
      ss <<= 1;  if (ss >= MM) ss -= (MM-2);
    }
  for (;j< 2*KK-1; j++)
    x[j] = 0;

  x[1]++;

  ss = seed & (MM-1);
  for (t = TT-1; t; )
    {
      for (j = KK-1; j>0; j--)
        x[j+j] = x[j];
      for (j = 2*KK-2; j > KK-LL; j-= 2)
        x[2*KK-1-j] = x[j] & ~1;
      for (j = 2*KK-2; j>=KK; j--)
        if (x[j] & 1)
          {
            x[j-(KK-LL)] = (x[j - (KK-LL)] - x[j]) & (MM-1);
            x[j-KK] = (x[j-KK] - x[j]) & (MM-1);
          }
      if (ss & 1)
        {
          for (j=KK; j>0; j--)
            x[j] = x[j-1];
          x[0] = x[KK];
          if (x[KK] & 1)
            x[LL] = (x[LL] - x[KK]) & (MM-1);
        }
      if (ss)
        ss >>= 1;
      else
        t--;
    }
  for (j=0; j<LL; j++)
    ran_x[j+KK-LL] = x[j];
  for (; j<KK; j++)
    ran_x[j-LL] = x[j];

  ran_index = 0;
}

static uint32_t
ran_get(void)
{
  uint32_t value;
  assert(ran_index < KK);
  
  value = ran_x[ran_index];
  ran_x[ran_index] -= ran_x[(ran_index + KK - LL) % KK];
  ran_x[ran_index] &= (MM-1);
  
  ran_index = (ran_index + 1) % KK;

  return value;
}

static void
ran_array(uint32_t *a, unsigned n)
{
  unsigned i;
  
  for (i = 0; i<n; i++)
    a[i] = ran_get();
}

static void
ran_test(void)
{
  uint32_t a[2009];
  uint32_t x;
  
  unsigned m;
  
  ran_init(310952);
  for (m = 0; m<2009; m++)
    ran_array(a, 1009);

  x = ran_get();
  assert(x == 461390032);
}

static int
116
get_event(FILE *f, struct sha256_ctx *hash,
117
          unsigned *key, unsigned *time)
Niels Möller's avatar
Niels Möller committed
118
119
{
  static int t = 0;
120
  uint8_t buf[1];
Niels Möller's avatar
Niels Möller committed
121
  
122
  int c = getc(f);
Niels Möller's avatar
Niels Möller committed
123
124
125
  if (c == EOF)
    return 0;

126
  buf[0] = c;
127
  sha256_update(hash, sizeof(buf), buf);
128
    
Niels Möller's avatar
Niels Möller committed
129
130
131
132
133
134
135
136
  *key = c;

  t += (ran_get() % 10000);
  *time = t;

  return 1;
}

137
static void
138
print_hex(unsigned length, uint8_t *digest)
139
140
141
{
  unsigned i;
  
142
  for (i = 0; i < length; i++)
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    {
      if (! (i % 8))
        fprintf(stderr, " ");
      fprintf(stderr, "%02x", digest[i]);
    }
}

static FILE *
open_file(const char *name)
{
  /* Tries opening the file in $srcdir, if set, otherwise the current
   * working directory */

  const char *srcdir = getenv("srcdir");
  if (srcdir && srcdir[0])
    {
      char *buf = alloca(strlen(name) + strlen(srcdir) + 10);
      sprintf(buf, "%s/%s", srcdir, name);
      name = buf;
    }

  /* Opens the file in text mode. */
  return fopen(name, "r");
}

Niels Möller's avatar
Niels Möller committed
168
169
170
int
main(int argc, char **argv)
{
171
172
  FILE *input;
  
Niels Möller's avatar
Niels Möller committed
173
174
175
176
177
  struct yarrow256_ctx yarrow;
  struct yarrow_key_event_ctx estimator;

  struct yarrow_source sources[2];

178
179
180
  struct sha256_ctx output_hash;
  struct sha256_ctx input_hash;
  uint8_t digest[SHA256_DIGEST_SIZE];
181
182

  const uint8_t *expected_output
183
184
    = decode_hex_dup("8df8796bd937ad3d ea9a0a5ff284c275"
		     "cd8e4fafe21fe114 19ba154dfbde2ec4");
185
186

  const uint8_t *expected_input
187
188
    = decode_hex_dup("fec4c0767434a8a3 22d6d5d0c9f49c42"
		     "988ce8c159b1a806 29d51aa40c2e99aa");
189
190

  const uint8_t *expected_seed_file
191
192
    = decode_hex_dup("f473011d57f4a76b 8eeaff5edbd59224"
		     "c942cdce44a6e3ec 39b9a771b6944a60");
193
  
Niels Möller's avatar
Niels Möller committed
194
195
  unsigned c; unsigned t;

196
197
198
199
200
  unsigned processed = 0;
  unsigned output = 0;

  unsigned i;
  
Niels Möller's avatar
Niels Möller committed
201
  static const char zeroes[100];
202
203
204
205
206

  if ((argc == 2)
      && (argv[1][0] == '-')
      && (argv[1][1] == 'v'))
    verbose = 1;
Niels Möller's avatar
Niels Möller committed
207
208
  
  yarrow256_init(&yarrow, 2, sources);
209
210
  memset(&yarrow.seed_file, 0, sizeof(yarrow.seed_file));
  
Niels Möller's avatar
Niels Möller committed
211
  yarrow_key_event_init(&estimator);
212
213
  sha256_init(&input_hash);
  sha256_init(&output_hash);
Niels Möller's avatar
Niels Möller committed
214
215
216
217
218
219
220
221

  ran_test();

  ran_init(31416);

  /* Fake input to source 0 */
  yarrow256_update(&yarrow, 0, 200, sizeof(zeroes), zeroes);

222
223
224
225
  if (verbose)
    fprintf(stderr, "source 0 entropy: %d\n",
	    sources[0].estimate[YARROW_SLOW]);
  
226
  assert(!yarrow256_is_seeded(&yarrow));
227
228
229
230
231
232
233
234
235

  input = open_file("rfc1750.txt");

  if (!input)
    {
      fprintf(stderr, "Couldn't open `rfc1750.txt', errno = %d\n",
              errno);
      return EXIT_FAILURE;
    }
Niels Möller's avatar
Niels Möller committed
236
  
237
  while (get_event(input, &input_hash, &c, &t))
Niels Möller's avatar
Niels Möller committed
238
239
240
241
242
243
244
245
246
247
248
    {
      uint8_t buf[8];

      processed++;
      
      WRITE_UINT32(buf, c);
      WRITE_UINT32(buf + 4, t);
      yarrow256_update(&yarrow, 1,
                       yarrow_key_event_estimate(&estimator, c, t),
                       sizeof(buf), buf);

249
      if (yarrow256_is_seeded(&yarrow))
Niels Möller's avatar
Niels Möller committed
250
251
252
253
254
        {
          static const unsigned sizes[4] = { 1, 16, 500, 37 };
          unsigned size = sizes[processed % 4];
          
          uint8_t buf[500];
255

256
          if (verbose && !output)
257
258
259
            fprintf(stderr, "Generator was seeded after %d events\n",
                    processed);
          
Niels Möller's avatar
Niels Möller committed
260
261
          yarrow256_random(&yarrow, size, buf);

262
          sha256_update(&output_hash, size, buf);
263

264
265
266
267
268
269
	  if (verbose)
	    {
	      fprintf(stderr, "%02x ", buf[0]);
	      if (! (processed % 16))
		fprintf(stderr, "\n");
	    }
270
          output += size;
Niels Möller's avatar
Niels Möller committed
271
272
273
        }
    }

274
275
  if (verbose)
    {
276
277
      fprintf(stderr, "\n");
      
278
279
280
281
282
283
284
      for (i = 0; i<2; i++)
	fprintf(stderr, "source %d, (fast, slow) entropy: (%d, %d)\n",
		i,
		sources[i].estimate[YARROW_FAST],
		sources[i].estimate[YARROW_SLOW]); 

      fprintf(stderr, "Processed input: %d octets\n", processed);
285
      fprintf(stderr, "         sha256:");
286
    }
287
288
  sha256_final(&input_hash);
  sha256_digest(&input_hash, sizeof(digest), digest);
289

290
291
  if (verbose)
    {
292
      print_hex(sizeof(digest), digest);
293
294
295
      fprintf(stderr, "\n");
    }
  
296
297
298
299
300
  if (memcmp(digest, expected_input, sizeof(digest)))
    {
      fprintf(stderr, "Failed.\n");
      return EXIT_FAILURE;
    }
301

302
303
304
305
306
307
308
309
310
311
312
313
314
  if (verbose)
    {
      fprintf(stderr, "New seed file: ");
      print_hex(sizeof(yarrow.seed_file), yarrow.seed_file);
      fprintf(stderr, "\n");
    }

  if (memcmp(yarrow.seed_file, expected_seed_file, sizeof(yarrow.seed_file)))
    {
      fprintf(stderr, "Failed.\n");
      return EXIT_FAILURE;
    }
  
315
316
317
  if (verbose)
    {
      fprintf(stderr, "Generated output: %d octets\n", output);
318
      fprintf(stderr, "          sha256:");
319
    }
Niels Möller's avatar
Niels Möller committed
320
  
321
322
  sha256_final(&output_hash);
  sha256_digest(&output_hash, sizeof(digest), digest);
323

324
325
  if (verbose)
    {
326
      print_hex(sizeof(digest), digest);
327
328
329
      fprintf(stderr, "\n");
    }
  
330
331
332
333
334
335
  if (memcmp(digest, expected_output, sizeof(digest)))
    {
      fprintf(stderr, "Failed.\n");
      return EXIT_FAILURE;
    }
  
Niels Möller's avatar
Niels Möller committed
336
337
  return EXIT_SUCCESS;
}