1128059Smarkm/*-
2255362Smarkm * Copyright (c) 2000-2013 Mark R V Murray
3128059Smarkm * All rights reserved.
4128059Smarkm *
5128059Smarkm * Redistribution and use in source and binary forms, with or without
6128059Smarkm * modification, are permitted provided that the following conditions
7128059Smarkm * are met:
8128059Smarkm * 1. Redistributions of source code must retain the above copyright
9128059Smarkm *    notice, this list of conditions and the following disclaimer
10128059Smarkm *    in this position and unchanged.
11128059Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12128059Smarkm *    notice, this list of conditions and the following disclaimer in the
13128059Smarkm *    documentation and/or other materials provided with the distribution.
14128059Smarkm *
15128059Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16128059Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17128059Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18128059Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19128059Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20128059Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21128059Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22128059Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23128059Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24128059Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25128059Smarkm *
26128059Smarkm * $FreeBSD$
27128059Smarkm */
28128059Smarkm
29256381Smarkm#ifndef SYS_DEV_RANDOM_RANDOMDEV_SOFT_H_INCLUDED
30256381Smarkm#define SYS_DEV_RANDOM_RANDOMDEV_SOFT_H_INCLUDED
31256381Smarkm
32128059Smarkm/* This header contains only those definitions that are global
33128059Smarkm * and harvester-specific for the entropy processor
34128059Smarkm */
35128059Smarkm
36128059Smarkm/* #define ENTROPYSOURCE nn	   entropy sources (actually classes)
37128059Smarkm *					This is properly defined in
38128059Smarkm *					an enum in sys/random.h
39128059Smarkm */
40128059Smarkm
41128059Smarkm/* The ring size _MUST_ be a power of 2 */
42128059Smarkm#define HARVEST_RING_SIZE	1024	/* harvest ring buffer size */
43128059Smarkm#define HARVEST_RING_MASK	(HARVEST_RING_SIZE - 1)
44128059Smarkm
45128059Smarkm#define HARVESTSIZE	16	/* max size of each harvested entropy unit */
46128059Smarkm
47128059Smarkm/* These are used to queue harvested packets of entropy. The entropy
48128059Smarkm * buffer size is pretty arbitrary.
49128059Smarkm */
50128059Smarkmstruct harvest {
51128059Smarkm	uintmax_t somecounter;		/* fast counter for clock jitter */
52255362Smarkm	uint8_t entropy[HARVESTSIZE];	/* the harvested entropy */
53256381Smarkm	u_int size, bits;		/* stats about the entropy */
54256381Smarkm	enum esource source;		/* origin of the entropy */
55128059Smarkm	STAILQ_ENTRY(harvest) next;	/* next item on the list */
56128059Smarkm};
57128059Smarkm
58255362Smarkmvoid randomdev_init(void);
59255362Smarkmvoid randomdev_deinit(void);
60128059Smarkm
61255362Smarkmvoid randomdev_init_harvester(void (*)(u_int64_t, const void *, u_int,
62256381Smarkm	u_int, enum esource), int (*)(void *, int));
63255362Smarkmvoid randomdev_deinit_harvester(void);
64128059Smarkm
65128059Smarkmvoid random_set_wakeup_exit(void *);
66128059Smarkmvoid random_process_event(struct harvest *event);
67255362Smarkmvoid randomdev_unblock(void);
68128059Smarkm
69153575Spsextern struct mtx random_reseed_mtx;
70128059Smarkm
71255362Smarkm/* If this was C++, the macro below would be a template */
72128059Smarkm#define RANDOM_CHECK_UINT(name, min, max)				\
73128059Smarkmstatic int								\
74128059Smarkmrandom_check_uint_##name(SYSCTL_HANDLER_ARGS)				\
75128059Smarkm{									\
76128059Smarkm	if (oidp->oid_arg1 != NULL) {					\
77128059Smarkm		 if (*(u_int *)(oidp->oid_arg1) <= (min))		\
78128059Smarkm			*(u_int *)(oidp->oid_arg1) = (min);		\
79128059Smarkm		 else if (*(u_int *)(oidp->oid_arg1) > (max))		\
80128059Smarkm			*(u_int *)(oidp->oid_arg1) = (max);		\
81128059Smarkm	}								\
82256381Smarkm        return (sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,	\
83256381Smarkm		req));							\
84128059Smarkm}
85256381Smarkm
86256381Smarkm#endif
87