add packaging
[platform/upstream/nettle.git] / testsuite / yarrow-test.c
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       FILE *f;
49       char *buf = xalloc(strlen(name) + strlen(srcdir) + 10);
50       sprintf(buf, "%s/%s", srcdir, name);
51
52       f = fopen(buf, "r");
53       free(buf);
54       return f;
55     }
56
57   /* Opens the file in text mode. */
58   return fopen(name, "r");
59 }
60
61 void
62 test_main(void)
63 {
64   FILE *input;
65   
66   struct yarrow256_ctx yarrow;
67   struct yarrow_key_event_ctx estimator;
68
69   struct yarrow_source sources[2];
70
71   struct sha256_ctx output_hash;
72   struct sha256_ctx input_hash;
73   uint8_t digest[SHA256_DIGEST_SIZE];
74
75   uint8_t seed_file[YARROW256_SEED_FILE_SIZE];
76
77   const uint8_t *expected_output
78     = H("dd304aacac3dc95e 70d684a642967c89"
79         "58501f7c8eb88b79 43b2ffccde6f0f79");
80
81   const uint8_t *expected_input
82     = H("e0596cf006025506 65d1195f32a87e4a"
83         "5c354910dfbd0a31 e2105b262f5ce3d8");
84
85   const uint8_t *expected_seed_file
86     = H("b03518f32b1084dd 983e6a445d47bb6f"
87         "13bb7b998740d570 503d6aaa62e28901");
88   
89   unsigned c; unsigned t;
90
91   unsigned processed = 0;
92   unsigned output = 0;
93
94   unsigned i;
95   
96   static const char zeroes[100];
97
98   yarrow256_init(&yarrow, 2, sources);
99   
100   yarrow_key_event_init(&estimator);
101   sha256_init(&input_hash);
102   sha256_init(&output_hash);
103
104   knuth_lfib_init(&lfib, 31416);
105
106   /* Fake input to source 0 */
107   yarrow256_update(&yarrow, 0, 200, sizeof(zeroes), zeroes);
108
109   if (verbose)
110     printf("source 0 entropy: %d\n",
111            sources[0].estimate[YARROW_SLOW]);
112   
113   ASSERT(!yarrow256_is_seeded(&yarrow));
114
115   input = open_file("gold-bug.txt");
116
117   if (!input)
118     {
119       fprintf(stderr, "Couldn't open `gold-bug.txt', errno = %d\n",
120               errno);
121       FAIL();
122     }
123   
124   while (get_event(input, &input_hash, &c, &t))
125     {
126       uint8_t buf[8];
127
128       processed++;
129       
130       WRITE_UINT32(buf, c);
131       WRITE_UINT32(buf + 4, t);
132       yarrow256_update(&yarrow, 1,
133                        yarrow_key_event_estimate(&estimator, c, t),
134                        sizeof(buf), buf);
135
136       if (yarrow256_is_seeded(&yarrow))
137         {
138           static const unsigned sizes[4] = { 1, 16, 500, 37 };
139           unsigned size = sizes[processed % 4];
140           
141           uint8_t buf[500];
142
143           if (verbose && !output)
144             printf("Generator was seeded after %d events\n",
145                    processed);
146           
147           yarrow256_random(&yarrow, size, buf);
148
149           sha256_update(&output_hash, size, buf);
150
151           if (verbose)
152             {
153               printf("%02x ", buf[0]);
154               if (! (processed % 16))
155                 printf("\n");
156             }
157           output += size;
158         }
159     }
160
161   fclose(input);
162
163   if (verbose)
164     {
165       printf("\n");
166       
167       for (i = 0; i<2; i++)
168         printf("source %d, (fast, slow) entropy: (%d, %d)\n",
169                i,
170                sources[i].estimate[YARROW_FAST],
171                sources[i].estimate[YARROW_SLOW]); 
172       
173       printf("Processed input: %d octets\n", processed);
174       printf("         sha256:");
175     }
176
177   sha256_digest(&input_hash, sizeof(digest), digest);
178
179   if (verbose)
180     {
181       print_hex(sizeof(digest), digest);
182       printf("\n");
183     }
184   
185   ASSERT (memcmp(digest, expected_input, sizeof(digest)) == 0);
186
187   yarrow256_random(&yarrow, sizeof(seed_file), seed_file);
188   if (verbose)
189     {
190       printf("New seed file: ");
191       print_hex(sizeof(seed_file), seed_file);
192       printf("\n");
193     }
194
195   ASSERT (memcmp(seed_file, expected_seed_file, sizeof(seed_file)) == 0);
196   
197   if (verbose)
198     {
199       printf("Generated output: %d octets\n", output);
200       printf("          sha256:");
201     }
202   
203   sha256_digest(&output_hash, sizeof(digest), digest);
204
205   if (verbose)
206     {
207       print_hex(sizeof(digest), digest);
208       printf("\n");
209     }
210   
211   ASSERT (memcmp(digest, expected_output, sizeof(digest)) == 0);
212 }