Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: rndio.h,v 1.3 2023/07/16 10:36:21 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Michael Graff <explorer (at) flame.org>.  This code uses ideas and
      9  * algorithms from the Linux driver written by Ted 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #ifndef	_SYS_RNDIO_H
     34 #define	_SYS_RNDIO_H
     35 
     36 #include <sys/types.h>
     37 #include <sys/sha1.h>
     38 #include <sys/ioccom.h>
     39 
     40 /*
     41  * Exposed "size" of entropy pool, for convenience in load/save
     42  * from userspace.  Do not assume this is the same as the actual in-kernel
     43  * pool size!
     44  */
     45 #define RND_SAVEWORDS	128
     46 typedef struct {
     47 	uint32_t entropy;
     48 	uint8_t data[RND_SAVEWORDS * sizeof(uint32_t)];
     49 	uint8_t digest[SHA1_DIGEST_LENGTH];
     50 } rndsave_t;
     51 
     52 /* Statistics exposed by RNDGETPOOLSTAT */
     53 typedef struct {
     54 	uint32_t	poolsize;
     55 	uint32_t 	threshold;
     56 	uint32_t	maxentropy;
     57 
     58 	uint32_t	added;
     59 	uint32_t	curentropy;
     60 	uint32_t	removed;
     61 	uint32_t	discarded;
     62 	uint32_t	generated;
     63 } rndpoolstat_t;
     64 
     65 /* Sanitized random source view for userspace */
     66 typedef struct {
     67 	char		name[16];	/* device name */
     68 	uint32_t	total;		/* entropy from this source */
     69 	uint32_t	type;		/* type */
     70 	uint32_t	flags;		/* flags */
     71 } rndsource_t;
     72 
     73 typedef struct {
     74 	rndsource_t	rt;
     75 	uint32_t	dt_samples;	/* time-delta samples input */
     76 	uint32_t	dt_total;	/* time-delta entropy estimate */
     77 	uint32_t	dv_samples;	/* value-delta samples input */
     78 	uint32_t	dv_total;	/* value-delta entropy estimate */
     79 } rndsource_est_t;
     80 
     81 /*
     82  * Flags to control the source.  Low byte is type, upper bits are flags.
     83  */
     84 #define RND_FLAG_NO_ESTIMATE	0x00000100
     85 #define RND_FLAG_NO_COLLECT	0x00000200
     86 #define RND_FLAG_FAST		0x00000400	/* process samples in bulk */
     87 #define RND_FLAG_HASCB		0x00000800	/* has get callback */
     88 #define RND_FLAG_COLLECT_TIME	0x00001000	/* use timestamp as input */
     89 #define RND_FLAG_COLLECT_VALUE	0x00002000	/* use value as input */
     90 #define RND_FLAG_ESTIMATE_TIME	0x00004000	/* estimate entropy on time */
     91 #define RND_FLAG_ESTIMATE_VALUE	0x00008000	/* estimate entropy on value */
     92 #define	RND_FLAG_HASENABLE	0x00010000	/* has enable/disable fns */
     93 #define RND_FLAG_DEFAULT	(RND_FLAG_COLLECT_VALUE|RND_FLAG_COLLECT_TIME|\
     94 				 RND_FLAG_ESTIMATE_TIME)
     95 
     96 #define	RND_TYPE_UNKNOWN	0	/* unknown source */
     97 #define	RND_TYPE_DISK		1	/* source is physical disk */
     98 #define	RND_TYPE_NET		2	/* source is a network device */
     99 #define	RND_TYPE_TAPE		3	/* source is a tape drive */
    100 #define	RND_TYPE_TTY		4	/* source is a tty device */
    101 #define	RND_TYPE_RNG		5	/* source is a hardware RNG */
    102 #define RND_TYPE_SKEW		6	/* source is skew between clocks */
    103 #define RND_TYPE_ENV		7	/* source is temp or fan sensor */
    104 #define RND_TYPE_VM		8	/* source is VM system events */
    105 #define RND_TYPE_POWER		9	/* source is power events */
    106 #define	RND_TYPE_MAX		9	/* last type id used */
    107 
    108 #define	RND_MAXSTATCOUNT	10	/* 10 sources at once max */
    109 
    110 /*
    111  * return "count" random entries, starting at "start"
    112  */
    113 typedef struct {
    114 	uint32_t	start;
    115 	uint32_t	count;
    116 	rndsource_t	source[RND_MAXSTATCOUNT];
    117 } rndstat_t;
    118 
    119 /*
    120  * return "count" random entries with estimates, starting at "start"
    121  */
    122 typedef struct {
    123 	uint32_t	start;
    124 	uint32_t	count;
    125 	rndsource_est_t	source[RND_MAXSTATCOUNT];
    126 } rndstat_est_t;
    127 
    128 /*
    129  * return information on a specific source by name
    130  */
    131 typedef struct {
    132 	char		name[16];
    133 	rndsource_t	source;
    134 } rndstat_name_t;
    135 
    136 typedef struct {
    137 	char		name[16];
    138 	rndsource_est_t	source;
    139 } rndstat_est_name_t;
    140 
    141 
    142 /*
    143  * set/clear device flags.  If type is set to 0xff, the name is used
    144  * instead.  Otherwise, the flags set/cleared apply to all devices of
    145  * the specified type, and the name is ignored.
    146  */
    147 typedef struct {
    148 	char		name[16];	/* the name we are adjusting */
    149 	uint32_t	type;		/* the type of device we want */
    150 	uint32_t	flags;		/* flags to set or clear */
    151 	uint32_t	mask;		/* mask for the flags we are setting */
    152 } rndctl_t;
    153 
    154 /*
    155  * Add entropy to the pool.  len is the data length, in bytes.
    156  * entropy is the number of bits of estimated entropy in the data.
    157  */
    158 typedef struct {
    159 	uint32_t	len;
    160 	uint32_t	entropy;
    161 	u_char		data[RND_SAVEWORDS * sizeof(uint32_t)];
    162 } rnddata_t;
    163 
    164 #define	RNDGETENTCNT	_IOR('R',  101, uint32_t) /* get entropy count */
    165 #define	RNDGETSRCNUM	_IOWR('R', 102, rndstat_t) /* get rnd source info */
    166 #define	RNDGETSRCNAME	_IOWR('R', 103, rndstat_name_t) /* get src by name */
    167 #define	RNDCTL		_IOW('R',  104, rndctl_t)  /* set/clear source flags */
    168 #define	RNDADDDATA	_IOW('R',  105, rnddata_t) /* add data to the pool */
    169 #define	RNDGETPOOLSTAT	_IOR('R',  106, rndpoolstat_t) /* get statistics */
    170 #define	RNDGETESTNUM	_IOWR('R', 107, rndstat_est_t) /* get srcest */
    171 #define	RNDGETESTNAME	_IOWR('R', 108, rndstat_est_name_t) /* " by name */
    172 
    173 #endif	/* _SYS_RNDIO_H */
    174