harvest.c revision 63771
1/*-
2 * Copyright (c) 2000 Mark R V Murray
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/random/harvest.c 63771 2000-07-23 11:08:16Z markm $
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/types.h>
32#include <sys/queue.h>
33#include <sys/linker.h>
34#include <sys/libkern.h>
35#include <sys/mbuf.h>
36#include <sys/random.h>
37#include <sys/time.h>
38#include <crypto/blowfish/blowfish.h>
39
40#include <dev/randomdev/yarrow.h>
41
42/* hold the address of the routine which is actually called if
43 * the ramdomdev is loaded
44 */
45static void (*reap)(struct timespec *, u_int64_t, u_int, u_int, u_int) = NULL;
46
47/* Initialise the harvester at load time */
48void
49random_init_harvester(void (*reaper)(struct timespec *, u_int64_t, u_int, u_int, u_int))
50{
51	reap = reaper;
52}
53
54/* Deinitialise the harvester at unload time */
55void
56random_deinit_harvester(void)
57{
58	reap = NULL;
59}
60
61/* Entropy harvesting routine. This is supposed to be fast; do
62 * not do anything slow in here!
63 * Implemented as in indirect call to allow non-inclusion of
64 * the entropy device.
65 */
66void
67random_harvest(u_int64_t entropy, u_int bits, u_int frac, u_int origin)
68{
69	struct timespec timebuf;
70
71	if (reap) {
72		nanotime(&timebuf);
73		(*reap)(&timebuf, entropy, bits, frac, origin);
74	}
75}
76