summaryrefslogtreecommitdiffstats
path: root/sys/dev/randomdev/yarrow.c
blob: 961143659198d45c17765bcc478104c2b02e9e9e (plain)
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
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
116
117
118
119
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*-
 * Copyright (c) 2000 Mark R V Murray
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer
 *    in this position and unchanged.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/* NOTE NOTE NOTE - This is not finished! It will supply numbers, but
 *                  it is not yet cryptographically secure!!
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/queue.h>
#include <sys/taskqueue.h>
#include <sys/linker.h>
#include <sys/libkern.h>
#include <sys/mbuf.h>
#include <sys/random.h>
#include <sys/time.h>
#include <sys/types.h>
#include <crypto/blowfish/blowfish.h>

#include <dev/randomdev/yarrow.h>

/* #define DEBUG */

static void generator_gate(void);
static void reseed(int);
static void random_harvest_internal(struct timespec *, void *, u_int, u_int, u_int, enum esource);

/* Structure holding the entropy state */
struct random_state random_state;

/* When enough entropy has been harvested, asynchronously "stir" it in.
 * The regate task is run at splsofttq()
 */
static struct task regate_task[2];

struct context {
	u_int pool;
} context[2] =	{
			{ 0 },
			{ 1 }
		};

static void
regate(void *context, int pending)
{
#ifdef DEBUG
	printf("Regate task\n");
#endif
	reseed(((struct context *)context)->pool);
}

void
random_init(void)
{
#ifdef DEBUG
	printf("Random init\n");
#endif
	random_state.gengateinterval = 10;
	random_state.bins = 10;
	random_state.pool[0].thresh = 100;
	random_state.pool[1].thresh = 160;
	random_state.slowoverthresh = 2;
	random_state.which = FAST;
	TASK_INIT(&regate_task[FAST], FAST, &regate, (void *)&context[FAST]);
	TASK_INIT(&regate_task[SLOW], SLOW, &regate, (void *)&context[SLOW]);
	random_init_harvester(random_harvest_internal);
}

void
random_deinit(void)
{
#ifdef DEBUG
	printf("Random deinit\n");
#endif
	random_deinit_harvester();
}

static void
reseed(int fastslow)
{
	/* Interrupt-context stack is a limited resource; make static
	 * large structures; XXX Revisit - needs to move to the large
	 * random_state structure.
	 */
	static unsigned char v[TIMEBIN][KEYSIZE];	/* v[i] */
	unsigned char hash[KEYSIZE];			/* h' */
	static BF_KEY hashkey;
	unsigned char ivec[8];
	unsigned char temp[KEYSIZE];
	struct entropy *bucket;
	int i, j;

#ifdef DEBUG
	printf("Reseed type %d\n", fastslow);
#endif

	/* 1. Hash the accumulated entropy into v[0] */

	bzero((void *)&v[0], KEYSIZE);
	if (fastslow == SLOW) {
		/* Feed a hash of the slow pool into the fast pool */
		for (i = 0; i < ENTROPYSOURCE; i++) {
			for (j = 0; j < ENTROPYBIN; j++) {
				bucket = &random_state.pool[SLOW].source[i].entropy[j];
				if(bucket->nanotime.tv_sec || bucket->nanotime.tv_nsec) {
					BF_set_key(&hashkey, sizeof(struct entropy),
						(void *)bucket);
					BF_cbc_encrypt(v[0], temp, KEYSIZE, &hashkey, ivec,
						BF_ENCRYPT);
					memcpy(&v[0], temp, KEYSIZE);
					bucket->nanotime.tv_sec = 0;
					bucket->nanotime.tv_nsec = 0;
				}
			}
		}
	}

	for (i = 0; i < ENTROPYSOURCE; i++) {
		for (j = 0; j < ENTROPYBIN; j++) {
			bucket = &random_state.pool[FAST].source[i].entropy[j];
			if(bucket->nanotime.tv_sec || bucket->nanotime.tv_nsec) {
				BF_set_key(&hashkey, sizeof(struct entropy), (void *)bucket);
				BF_cbc_encrypt(v[0], temp, KEYSIZE, &hashkey, ivec, BF_ENCRYPT);
				memcpy(&v[0], temp, KEYSIZE);
				bucket->nanotime.tv_sec = 0;
				bucket->nanotime.tv_nsec = 0;
			}
		}
	}

	/* 2. Compute hash values for all v. _Supposed_ to be computationally
	 *    intensive.
	 */

	if (random_state.bins > TIMEBIN)
		random_state.bins = TIMEBIN;
	for (i = 1; i < random_state.bins; i++) {
		bzero((void *)&v[i], KEYSIZE);
		/* v[i] #= h(v[i-1]) */
		BF_set_key(&hashkey, KEYSIZE, v[i - 1]);
		BF_cbc_encrypt(v[i], temp, KEYSIZE, &hashkey, ivec, BF_ENCRYPT);
		memcpy(&v[i], temp, KEYSIZE);
		/* v[i] #= h(v[0]) */
		BF_set_key(&hashkey, KEYSIZE, v[0]);
		BF_cbc_encrypt(v[i], temp, KEYSIZE, &hashkey, ivec, BF_ENCRYPT);
		memcpy(&v[i], temp, KEYSIZE);
		/* v[i] #= h(i) */
		BF_set_key(&hashkey, sizeof(int), (unsigned char *)&i);
		BF_cbc_encrypt(v[i], temp, KEYSIZE, &hashkey, ivec, BF_ENCRYPT);
		memcpy(&v[i], temp, KEYSIZE);
	}

	/* 3. Compute a new Key. */

	bzero((void *)hash, KEYSIZE);
	BF_set_key(&hashkey, KEYSIZE, (unsigned char *)&random_state.key);
	BF_cbc_encrypt(hash, temp, KEYSIZE, &hashkey, ivec, BF_ENCRYPT);
	memcpy(hash, temp, KEYSIZE);
	for (i = 1; i < random_state.bins; i++) {
		BF_set_key(&hashkey, KEYSIZE, v[i]);
		BF_cbc_encrypt(hash, temp, KEYSIZE, &hashkey, ivec, BF_ENCRYPT);
		memcpy(hash, temp, KEYSIZE);
	}
	BF_set_key(&random_state.key, KEYSIZE, hash);

	/* 4. Recompute the counter */

	random_state.counter = 0;
	BF_cbc_encrypt((unsigned char *)&random_state.counter, temp,
		sizeof(random_state.counter), &random_state.key,
		random_state.ivec, BF_ENCRYPT);
	memcpy(&random_state.counter, temp, random_state.counter);

	/* 5. Reset entropy estimate accumulators to zero */

	for (i = 0; i <= fastslow; i++) {
		for (j = 0; j < ENTROPYSOURCE; j++) {
			random_state.pool[i].source[j].bits = 0;
			random_state.pool[i].source[j].frac = 0;
		}
	}

	/* 6. Wipe memory of intermediate values */

	bzero((void *)v, sizeof(v));
	bzero((void *)temp, sizeof(temp));
	bzero((void *)hash, sizeof(hash));

	/* 7. Dump to seed file (done by external process) */

}

u_int
read_random(void *buf, u_int count)
{
	static u_int64_t genval;
	static int cur = 0;
	static int gate = 1;
	u_int i;
	u_int retval;
	intrmask_t mask;

	/* The reseed task must not be jumped on */
	mask = splsofttq();

	if (gate) {
		generator_gate();
		random_state.outputblocks = 0;
		gate = 0;
	}
	if (count >= sizeof(random_state.counter)) {
		retval = 0;
		for (i = 0; i < count; i += sizeof(random_state.counter)) {
			random_state.counter++;
			BF_cbc_encrypt((unsigned char *)&random_state.counter,
				(unsigned char *)&genval,
				sizeof(random_state.counter),
				&random_state.key, random_state.ivec, BF_ENCRYPT);
			memcpy((char *)buf + i, &genval,
				sizeof(random_state.counter));
			if (++random_state.outputblocks >= random_state.gengateinterval) {
				generator_gate();
				random_state.outputblocks = 0;
			}
			retval += sizeof(random_state.counter);
		}
	}
	else {
		if (!cur) {
			random_state.counter++;
			BF_cbc_encrypt((unsigned char *)&random_state.counter,
				(unsigned char *)&genval,
				sizeof(random_state.counter),
				&random_state.key, random_state.ivec,
				BF_ENCRYPT);
			memcpy(buf, &genval, count);
			cur = sizeof(random_state.counter) - count;
			if (++random_state.outputblocks >= random_state.gengateinterval) {
				generator_gate();
				random_state.outputblocks = 0;
			}
			retval = count;
		}
		else {
			retval = cur < count ? cur : count;
			memcpy(buf,
				(char *)&genval +
					(sizeof(random_state.counter) - cur),
				retval);
			cur -= retval;
		}
	}
	splx(mask);
	return retval;
}

void
write_random(void *buf, u_int count)
{
	u_int i;
	intrmask_t mask;
	struct timespec timebuf;

	/* The reseed task must not be jumped on */
	mask = splsofttq();
	/* arbitrarily break the input up into 8-byte chunks */
	for (i = 0; i < count; i += 8) {
		nanotime(&timebuf);
		random_harvest_internal(&timebuf, (char *)buf + i, 8, 0, 0,
			RANDOM_WRITE);
	}
	/* Maybe the loop iterated at least once */
	if (i > count)
		i -= 8;
	/* Get the last bytes even if the input length is not a multiple of 8 */
	count %= 8;
	if (count) {
		nanotime(&timebuf);
		random_harvest_internal(&timebuf, (char *)buf + i, count, 0, 0,
			RANDOM_WRITE);
	}
	reseed(FAST);
	splx(mask);
}

static void
generator_gate(void)
{
	int i;
	unsigned char temp[KEYSIZE];
	intrmask_t mask;

#ifdef DEBUG
	printf("Generator gate\n");
#endif

	/* The reseed task must not be jumped on */
	mask = splsofttq();

	for (i = 0; i < KEYSIZE; i += sizeof(random_state.counter)) {
		random_state.counter++;
		BF_cbc_encrypt((unsigned char *)&random_state.counter,
			&(temp[i]), sizeof(random_state.counter),
			&random_state.key, random_state.ivec, BF_ENCRYPT);
	}

	BF_set_key(&random_state.key, KEYSIZE, temp);
	bzero((void *)temp, KEYSIZE);

	splx(mask);
}

/* Entropy harvesting routine. This is supposed to be fast; do
 * not do anything slow in here!
 */

static void
random_harvest_internal(struct timespec *timep, void *entropy, u_int count,
	u_int bits, u_int frac, enum esource origin)
{
	u_int insert;
	int which;	/* fast or slow */
	struct entropy *bucket;
	struct source *source;
	struct pool *pool;
	intrmask_t mask;
	u_int64_t entropy_buf;

#ifdef DEBUG
	printf("Random harvest\n");
#endif
	if (origin < ENTROPYSOURCE) {

		/* Called inside irq handlers; protect from interference */
		mask = splhigh();

		which = random_state.which;
		pool = &random_state.pool[which];
		source = &pool->source[origin];

		insert = source->current + 1;
		if (insert >= ENTROPYBIN)
			insert = 0;

		bucket = &source->entropy[insert];

		if (!bucket->nanotime.tv_sec && !bucket->nanotime.tv_nsec) {

			/* nanotime provides clock jitter */
			bucket->nanotime = *timep;

			/* the harvested entropy */
			count = count > sizeof(entropy_buf)
				? sizeof(entropy_buf)
				: count;
			memcpy(&entropy_buf, entropy, count);
			/* XOR it in to really foul up the works */
			bucket->data ^= entropy_buf;

			/* update the estimates - including "fractional bits" */
			source->bits += bits;
			source->frac += frac;
			if (source->frac >= 1024) {
				source->bits += source->frac / 1024;
				source->frac %= 1024;
			}
			if (source->bits >= pool->thresh) {
				/* XXX Slowoverthresh needs to be considered */
				taskqueue_enqueue(taskqueue_swi, &regate_task[which]);
			}

			/* bump the insertion point */
			source->current = insert;

			/* toggle the pool for next insertion */
			random_state.which = !random_state.which;

		}
		splx(mask);
	}
}
OpenPOWER on IntegriCloud