yarrow256.c 7.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
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
/* yarrow256.c
 *
 * The yarrow pseudo-randomness generator.
 */

/* nettle, low-level cryptographics library
 *
 * Copyright (C) 2001 Niels Mller
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include "yarrow.h"

#include <assert.h>
#include <string.h>

/* Parameters */

/* An upper limit on the entropy (in bits) in one octet of sample
 * data. */
#define YARROW_MULTIPLIER 4

/* Generator gate threshold */
#define YARROW_GATE_THRESHOLD 10

40
41
42
43
44
45
46
47
48
/* Entropy threshold for reseeding from the fast pool */
#define YARROW_FAST_THRESHOLD 100

/* Entropy threshold for reseeding from the fast pool */
#define YARROW_SLOW_THRESHOLD 160

/* Number of sources that must exceed the threshold for slow reseed */
#define YARROW_SLOW_K 2

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* Entropy estimates sticks to this value, it is treated as infinity
 * in calculations. It should fit comfortably in an uint32_t, to avoid
 * overflows. */
#define YARROW_MAX_ENTROPY 0x100000

void
yarrow256_init(struct yarrow256_ctx *ctx,
	       int n,
	       struct yarrow_source *s)
{
  sha256_init(&ctx->pools[0]);
  sha256_init(&ctx->pools[1]);

  ctx->seeded = 0;

64
65
66
  /* Means that we have no buffered output */
  ctx->index = sizeof(ctx->buffer);
  
67
68
69
70
  ctx->nsources = n;
  ctx->sources = s;
}

71
72
73
74
75
76
77
78
79
80
81
/* NOTE: The SHA-256 digest size equals the AES key size, so we need
 * no "size adaptor". We also use P_t = 0, i.e. we don't currently try
 * to make reseeding computationally expensive. */

static void
yarrow_fast_reseed(struct yarrow256_ctx *ctx)
{
  uint8_t digest[SHA256_DIGEST_SIZE];
  
  unsigned i;

Niels Möller's avatar
Niels Möller committed
82
  /* FIXME: Mixin the current key! */
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
116
117
118
119
  sha256_final(&ctx->pools[YARROW_FAST]);
  sha256_digest(&ctx->pools[YARROW_FAST], sizeof(digest), digest);
  sha256_init(&ctx->pools[YARROW_FAST]);
  
  aes_set_key(&ctx->key, sizeof(digest), digest);

  /* Derive new counter value */
  memset(ctx->counter, 0, sizeof(ctx->counter));
  aes_encrypt(&ctx->key, sizeof(ctx->counter), ctx->counter, ctx->counter);
  
  /* Reset estimates. */
  for (i = 0; i<ctx->nsources; i++)
    ctx->sources[i].estimate[YARROW_FAST] = 0;
}

static void
yarrow_slow_reseed(struct yarrow256_ctx *ctx)
{
  uint8_t digest[SHA256_DIGEST_SIZE];
  unsigned i;

  /* Get digest of the slow pool*/
  
  sha256_final(&ctx->pools[YARROW_SLOW]);
  sha256_digest(&ctx->pools[YARROW_SLOW], sizeof(digest), digest);
  sha256_init(&ctx->pools[YARROW_SLOW]);

  /* Feed it into the fast pool */
  sha256_update(&ctx->pools[YARROW_SLOW], sizeof(digest), digest);

  yarrow_fast_reseed(ctx);
  
  /* Reset estimates. */
  for (i = 0; i<ctx->nsources; i++)
    ctx->sources[i].estimate[YARROW_SLOW] = 0;
}

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
168
169
170
void
yarrow256_update(struct yarrow256_ctx *ctx,
		 unsigned source_index, unsigned entropy,
		 unsigned length, const uint8_t *data)
{
  enum yarrow_pool_id current;
  struct yarrow_source *source;
  
  assert(source_index < ctx->nsources);

  if (!length)
    /* Nothing happens */
    return;

  source = &ctx->sources[source_index];
  
  if (!ctx->seeded)
    /* While seeding, use the slow pool */
    current = YARROW_SLOW;
  else
    {
      current = source->next;
      source->next = !source->next;
    }

  sha256_update(&ctx->pools[current], length, data);
 
  /* FIXME: Use different counters for fast and slow poll? Or a total
   * for fast poll, and individual for slow poll? */

  /* NOTE: We should be careful to avoid overflows in the estimates. */
  if (source->estimate[current] < YARROW_MAX_ENTROPY)
    {
      if (entropy > YARROW_MAX_ENTROPY)
	entropy = YARROW_MAX_ENTROPY;

      if ( (length < (YARROW_MAX_ENTROPY / YARROW_MULTIPLIER))
	   && (entropy > YARROW_MULTIPLIER * length) )
	entropy = YARROW_MULTIPLIER * length;

      /* FIXME: Calling a more sophisticated estimater should be done
       * here. */

      entropy += source->estimate[current];
      if (entropy > YARROW_MAX_ENTROPY)
	entropy = YARROW_MAX_ENTROPY;

      source->estimate[current] = entropy;
    }

  /* Check for seed/reseed */
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
  switch(current)
    {
    case YARROW_FAST:
      if (source->estimate[YARROW_FAST] >= YARROW_FAST_THRESHOLD)
	yarrow_fast_reseed(ctx);
      break;
    case YARROW_SLOW:
      {
	/* FIXME: This is somewhat inefficient. It would be better to
	 * either maintain the count, or do this loop only if the
	 * current source just crossed the threshold. */
	unsigned k, i;
	for (i = k = 0; i < ctx->nsources; i++)
	  if (ctx->sources[i].estimate[YARROW_SLOW] >= YARROW_SLOW_THRESHOLD)
	    k++;

	if (k >= YARROW_SLOW_K)
	  {
	    yarrow_slow_reseed(ctx);
	    ctx->seeded = 1;
	  }
      }
    }
194
195
196
197
198
199
200
201
}

static void
yarrow_generate_block(struct yarrow256_ctx *ctx,
		      uint8_t *block)
{
  unsigned i;
  
202
  aes_encrypt(&ctx->key, sizeof(ctx->counter), block, ctx->counter);
203

204
205
206
  /* Increment counter, treating it as a big-endian number. This is
   * machine independent, and follows appendix B of the NIST
   * specification of cipher modes of operation.
207
   *
208
   * We could keep a representation of thy counter as 4 32-bit values,
209
210
   * and write entire words (in big-endian byteorder) into the counter
   * block, whenever they change. */
211
  for (i = sizeof(ctx->counter); i--; )
212
213
214
215
216
217
    {
      if (++ctx->counter[i])
	break;
    }
}

Niels Möller's avatar
Niels Möller committed
218
219
/* FIXME: According to Niels Ferguson, it's better to gate after each
 * request for random data. */
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
static void
yarrow_generate_block_with_gate(struct yarrow256_ctx *ctx,
				uint8_t *block)
{
  if (ctx->block_count < YARROW_GATE_THRESHOLD)
    {
      yarrow_generate_block(ctx, block);
      ctx->block_count++;
    }
  else
    {
      uint8_t key[AES_MAX_KEY_SIZE];
      unsigned i;

      for (i = 0; i < sizeof(key); i+= AES_BLOCK_SIZE)
	yarrow_generate_block(ctx, key + i);

      aes_set_key(&ctx->key, sizeof(key), key);

      yarrow_generate_block(ctx, block);
      ctx->block_count = 1;
    }
}

void
yarrow256_random(struct yarrow256_ctx *ctx, unsigned length, uint8_t *dst)
{
  assert(ctx->seeded);

  if (ctx->index < AES_BLOCK_SIZE)
    {
      unsigned left = AES_BLOCK_SIZE - ctx->index;

      if (length <= left)
	{
	  memcpy(dst, ctx->buffer + ctx->index, length);
	  ctx->index += length;
	  return;
	}

      memcpy(dst, ctx->buffer + ctx->index, left);
      dst += left;
      length -= left;

      assert(length);
    }

  while (length > AES_BLOCK_SIZE)
    {
      yarrow_generate_block_with_gate(ctx, dst);
      dst += AES_BLOCK_SIZE;
      length -= AES_BLOCK_SIZE;
    }
  if (length)
    {
      assert(length < AES_BLOCK_SIZE);
      yarrow_generate_block_with_gate(ctx, ctx->buffer);
      memcpy(dst, ctx->buffer, length);
      ctx->index = length;
    }
}