~ubuntu-branches/ubuntu/quantal/nettle/quantal

« back to all changes in this revision

Viewing changes to testsuite/yarrow-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
ImportĀ upstreamĀ versionĀ 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "testutils.h"
 
2
#include "yarrow.h"
 
3
#include "knuth-lfib.h"
 
4
 
 
5
#include "macros.h"
 
6
 
 
7
#include <assert.h>
 
8
#include <errno.h>
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
 
 
13
/* Lagged fibonacci sequence as described in Knuth 3.6 */
 
14
 
 
15
struct knuth_lfib_ctx lfib;
 
16
 
 
17
static int
 
18
get_event(FILE *f, struct sha256_ctx *hash,
 
19
          unsigned *key, unsigned *time)
 
20
{
 
21
  static int t = 0;
 
22
  uint8_t buf[1];
 
23
  
 
24
  int c = getc(f);
 
25
  if (c == EOF)
 
26
    return 0;
 
27
 
 
28
  buf[0] = c;
 
29
  sha256_update(hash, sizeof(buf), buf);
 
30
    
 
31
  *key = c;
 
32
 
 
33
  t += (knuth_lfib_get(&lfib) % 10000);
 
34
  *time = t;
 
35
 
 
36
  return 1;
 
37
}
 
38
 
 
39
static FILE *
 
40
open_file(const char *name)
 
41
{
 
42
  /* Tries opening the file in $srcdir, if set, otherwise the current
 
43
   * working directory */
 
44
 
 
45
  const char *srcdir = getenv("srcdir");
 
46
  if (srcdir && srcdir[0])
 
47
    {
 
48
      /* Leaks this name, but that doesn't matter. */
 
49
      char *buf = xalloc(strlen(name) + strlen(srcdir) + 10);
 
50
      sprintf(buf, "%s/%s", srcdir, name);
 
51
      name = buf;
 
52
    }
 
53
 
 
54
  /* Opens the file in text mode. */
 
55
  return fopen(name, "r");
 
56
}
 
57
 
 
58
int
 
59
test_main(void)
 
60
{
 
61
  FILE *input;
 
62
  
 
63
  struct yarrow256_ctx yarrow;
 
64
  struct yarrow_key_event_ctx estimator;
 
65
 
 
66
  struct yarrow_source sources[2];
 
67
 
 
68
  struct sha256_ctx output_hash;
 
69
  struct sha256_ctx input_hash;
 
70
  uint8_t digest[SHA256_DIGEST_SIZE];
 
71
 
 
72
  const uint8_t *expected_output
 
73
    = decode_hex_dup("06ca66b204a92939 e75e09e11922153e"
 
74
                     "a2391000e0686da4 c7d27afb37a4630f");
 
75
 
 
76
  const uint8_t *expected_input
 
77
    = decode_hex_dup("fec4c0767434a8a3 22d6d5d0c9f49c42"
 
78
                     "988ce8c159b1a806 29d51aa40c2e99aa");
 
79
 
 
80
  const uint8_t *expected_seed_file
 
81
    = decode_hex_dup("87213a8a863a91f9 0e776c01e0d7c3a8"
 
82
                     "6b2ecf9977b06da5 34f3df8375918ac9");
 
83
  
 
84
  unsigned c; unsigned t;
 
85
 
 
86
  unsigned processed = 0;
 
87
  unsigned output = 0;
 
88
 
 
89
  unsigned i;
 
90
  
 
91
  static const char zeroes[100];
 
92
 
 
93
  yarrow256_init(&yarrow, 2, sources);
 
94
  memset(&yarrow.seed_file, 0, sizeof(yarrow.seed_file));
 
95
  
 
96
  yarrow_key_event_init(&estimator);
 
97
  sha256_init(&input_hash);
 
98
  sha256_init(&output_hash);
 
99
 
 
100
  knuth_lfib_init(&lfib, 31416);
 
101
 
 
102
  /* Fake input to source 0 */
 
103
  yarrow256_update(&yarrow, 0, 200, sizeof(zeroes), zeroes);
 
104
 
 
105
  if (verbose)
 
106
    printf("source 0 entropy: %d\n",
 
107
           sources[0].estimate[YARROW_SLOW]);
 
108
  
 
109
  assert(!yarrow256_is_seeded(&yarrow));
 
110
 
 
111
  input = open_file("rfc1750.txt");
 
112
 
 
113
  if (!input)
 
114
    {
 
115
      fprintf(stderr, "Couldn't open `rfc1750.txt', errno = %d\n",
 
116
              errno);
 
117
      return EXIT_FAILURE;
 
118
    }
 
119
  
 
120
  while (get_event(input, &input_hash, &c, &t))
 
121
    {
 
122
      uint8_t buf[8];
 
123
 
 
124
      processed++;
 
125
      
 
126
      WRITE_UINT32(buf, c);
 
127
      WRITE_UINT32(buf + 4, t);
 
128
      yarrow256_update(&yarrow, 1,
 
129
                       yarrow_key_event_estimate(&estimator, c, t),
 
130
                       sizeof(buf), buf);
 
131
 
 
132
      if (yarrow256_is_seeded(&yarrow))
 
133
        {
 
134
          static const unsigned sizes[4] = { 1, 16, 500, 37 };
 
135
          unsigned size = sizes[processed % 4];
 
136
          
 
137
          uint8_t buf[500];
 
138
 
 
139
          if (verbose && !output)
 
140
            printf("Generator was seeded after %d events\n",
 
141
                   processed);
 
142
          
 
143
          yarrow256_random(&yarrow, size, buf);
 
144
 
 
145
          sha256_update(&output_hash, size, buf);
 
146
 
 
147
          if (verbose)
 
148
            {
 
149
              printf("%02x ", buf[0]);
 
150
              if (! (processed % 16))
 
151
                printf("\n");
 
152
            }
 
153
          output += size;
 
154
        }
 
155
    }
 
156
 
 
157
  if (verbose)
 
158
    {
 
159
      printf("\n");
 
160
      
 
161
      for (i = 0; i<2; i++)
 
162
        printf("source %d, (fast, slow) entropy: (%d, %d)\n",
 
163
               i,
 
164
               sources[i].estimate[YARROW_FAST],
 
165
               sources[i].estimate[YARROW_SLOW]); 
 
166
      
 
167
      printf("Processed input: %d octets\n", processed);
 
168
      printf("         sha256:");
 
169
    }
 
170
 
 
171
  sha256_digest(&input_hash, sizeof(digest), digest);
 
172
 
 
173
  if (verbose)
 
174
    {
 
175
      print_hex(sizeof(digest), digest);
 
176
      printf("\n");
 
177
    }
 
178
  
 
179
  if (memcmp(digest, expected_input, sizeof(digest)))
 
180
    {
 
181
      fprintf(stderr, "Failed.\n");
 
182
      return EXIT_FAILURE;
 
183
    }
 
184
 
 
185
  if (verbose)
 
186
    {
 
187
      printf("New seed file: ");
 
188
      print_hex(sizeof(yarrow.seed_file), yarrow.seed_file);
 
189
      printf("\n");
 
190
    }
 
191
 
 
192
  if (memcmp(yarrow.seed_file, expected_seed_file, sizeof(yarrow.seed_file)))
 
193
    {
 
194
      fprintf(stderr, "Failed.\n");
 
195
      return EXIT_FAILURE;
 
196
    }
 
197
  
 
198
  if (verbose)
 
199
    {
 
200
      printf("Generated output: %d octets\n", output);
 
201
      printf("          sha256:");
 
202
    }
 
203
  
 
204
  sha256_digest(&output_hash, sizeof(digest), digest);
 
205
 
 
206
  if (verbose)
 
207
    {
 
208
      print_hex(sizeof(digest), digest);
 
209
      printf("\n");
 
210
    }
 
211
  
 
212
  if (memcmp(digest, expected_output, sizeof(digest)))
 
213
    {
 
214
      fprintf(stderr, "Failed.\n");
 
215
      return EXIT_FAILURE;
 
216
    }
 
217
  
 
218
  return EXIT_SUCCESS;
 
219
}