1 /**	$MirOS: src/sys/dev/rndvar.h,v 1.31 2013/10/31 20:06:56 tg Exp $ */
2 /*	$OpenBSD: rndvar.h,v 1.19 2003/11/03 18:24:28 tedu Exp $	*/
3 
4 /*
5  * Copyright (c) 2004, 2005, 2006, 2008, 2009, 2010, 2013
6  *	Thorsten Glaser <tg@mirbsd.org>
7  * Copyright (c) 1996,2000 Michael Shalayeff.
8  *
9  * This software derived from one contributed by Theodore Ts'o.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef __RNDVAR_H__
34 #define __RNDVAR_H__
35 
36 #ifndef POOLWORDS
37 #define POOLWORDS	1024	/* power of 2 - note this is 32-bit words */
38 #endif
39 
40 #define RND_RND		0	/* real hw entropy like nuclear chips */
41 #define RND_SRND	1	/* strong random source */
42 #define RND_URND	2	/* less strong random source */
43 #define RND_PRND	3	/* pseudo random source */
44 #define RND_ARND	4	/* arcfour based stretching RNG */
45 #define RND_NODEV	5	/* first invalid minor device number */
46 
47 #define	RND_SRC_TRUE	0
48 #define	RND_SRC_TIMER	1
49 #define	RND_SRC_MOUSE	2
50 #define	RND_SRC_TTY	3
51 #define	RND_SRC_DISK	4
52 #define	RND_SRC_NET	5
53 #define	RND_SRC_AUVIS	6
54 #define	RND_SRC_IMACS	7
55 /* size of stats structure */
56 #define	RND_SRC_NUM	8
57 /* internal-use quirk */
58 #define	RND_SRC_LPC	24
59 
60 struct rndstats {
61 	quad_t rnd_total;	/* total bits of entropy generated */
62 	quad_t rnd_used;	/* strong data bits read so far */
63 	quad_t rnd_reads;	/* strong read calls */
64 	quad_t arc4_reads;	/* aRC4 data bytes read so far */
65 	quad_t arc4_nstirs;	/* arc4 pool stirs */
66 	quad_t arc4_stirs;	/* arc4 pool stirs (bytes used) */
67 	quad_t lopool_deq;	/* calls to lopool dequeue (128 bytes each) */
68 	quad_t lopool_enq;	/* calls to lopool enqueue */
69 	quad_t lopool_bytes;	/* bytes added during lopool enqueue */
70 
71 	quad_t rnd_pad_who_this_uses_is_stupid[2];
72 
73 	quad_t rnd_waits;	/* sleeps for data */
74 	quad_t rnd_enqs;	/* enqueue calls */
75 	quad_t rnd_deqs;	/* dequeue calls */
76 	quad_t rnd_drops;	/* queue-full drops */
77 	quad_t rnd_drople;	/* queue low watermark low entropy drops */
78 
79 	quad_t rnd_ed[32];	/* entropy feed distribution */
80 	quad_t rnd_sc[RND_SRC_NUM]; /* add* calls */
81 	quad_t rnd_sb[RND_SRC_NUM]; /* add* bits */
82 };
83 
84 #ifdef _KERNEL
85 
86 #include <sys/slibkern.h>
87 
88 extern struct rndstats rndstats;
89 
90 #define	add_true_randomness(d)	enqueue_randomness(RND_SRC_TRUE,  (int)(d))
91 #define	add_timer_randomness(d)	enqueue_randomness(RND_SRC_TIMER, (int)(d))
92 #define	add_mouse_randomness(d)	enqueue_randomness(RND_SRC_MOUSE, (int)(d))
93 #define	add_tty_randomness(d)	enqueue_randomness(RND_SRC_TTY,   (int)(d))
94 #define	add_disk_randomness(d)	enqueue_randomness(RND_SRC_DISK,  (int)(d))
95 #define	add_net_randomness(d)	enqueue_randomness(RND_SRC_NET,   (int)(d))
96 #define	add_auvis_randomness(d)	enqueue_randomness(RND_SRC_AUVIS, (int)(d))
97 #define	add_imacs_randomness(d)	enqueue_randomness(RND_SRC_IMACS, (int)(d))
98 /* compatibility */
99 #define	add_audio_randomness(d)	enqueue_randomness(RND_SRC_AUVIS, (int)(d))
100 #define	add_video_randomness(d)	enqueue_randomness(RND_SRC_AUVIS, (int)(d))
101 
102 void enqueue_randomness(int, int);
103 void get_random_bytes(void *, size_t)
104     __attribute__((__bounded__(__string__, 1, 2)));
105 void arc4random_buf(void *, size_t)
106     __attribute__((__bounded__(__string__, 1, 2)));
107 u_int32_t arc4random(void);
108 
109 void rnd_lopool_add(const void *, size_t)
110     __attribute__((__bounded__(__string__, 1, 2)));
111 void rnd_lopool_addh(const void *, size_t)
112     __attribute__((__bounded__(__string__, 1, 2)));
113 void rnd_lopool_addv(unsigned long);
114 
115 #endif /* _KERNEL */
116 
117 #endif /* __RNDVAR_H__ */
118