1 /* $NetBSD: rndsource.h,v 1.6 2018/04/19 21:19:07 christos 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@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_RNDSOURCE_H 34 #define _SYS_RNDSOURCE_H 35 36 #ifndef _KERNEL /* XXX */ 37 #error <sys/rndsource.h> is meant for kernel consumers only. 38 #endif 39 40 #include <sys/types.h> 41 #include <sys/null.h> 42 #include <sys/rndio.h> /* RND_TYPE_*, RND_FLAG_* */ 43 #include <sys/rngtest.h> 44 #include <sys/queue.h> 45 46 typedef struct rnd_delta_estimator { 47 uint64_t x; 48 uint64_t dx; 49 uint64_t d2x; 50 uint64_t insamples; 51 uint64_t outbits; 52 } rnd_delta_t; 53 54 typedef struct krndsource { 55 LIST_ENTRY(krndsource) list; /* the linked list */ 56 char name[16]; /* device name */ 57 rnd_delta_t time_delta; /* time delta estimator */ 58 rnd_delta_t value_delta; /* value delta estimator */ 59 uint32_t total; /* entropy from this source */ 60 uint32_t type; /* type */ 61 uint32_t flags; /* flags */ 62 void *state; /* state information */ 63 size_t test_cnt; /* how much test data accumulated? */ 64 void (*get)(size_t, void *); /* pool wants N bytes (badly) */ 65 void *getarg; /* argument to get-function */ 66 void (*enable)(struct krndsource *, bool); /* turn on/off */ 67 rngtest_t *test; /* test data for RNG type sources */ 68 unsigned refcnt; 69 } krndsource_t; 70 71 static __inline void 72 rndsource_setcb(struct krndsource *const rs, void (*const cb)(size_t, void *), 73 void *const arg) 74 { 75 rs->get = cb; 76 rs->getarg = arg; 77 } 78 79 static __inline void 80 rndsource_setenable(struct krndsource *const rs, void *const cb) 81 { 82 rs->enable = cb; 83 } 84 85 #define RND_ENABLED(rp) \ 86 (((rp)->flags & RND_FLAG_NO_COLLECT) == 0) 87 88 void _rnd_add_uint32(krndsource_t *, uint32_t); 89 void _rnd_add_uint64(krndsource_t *, uint64_t); 90 void rnd_add_data(krndsource_t *, const void *const, uint32_t, 91 uint32_t); 92 void rnd_add_data_sync(krndsource_t *, const void *, uint32_t, 93 uint32_t); 94 void rnd_attach_source(krndsource_t *, const char *, 95 uint32_t, uint32_t); 96 void rnd_detach_source(krndsource_t *); 97 98 static __inline void 99 rnd_add_uint32(krndsource_t *kr, uint32_t val) 100 { 101 if (__predict_true(kr)) { 102 if (RND_ENABLED(kr)) { 103 _rnd_add_uint32(kr, val); 104 } 105 } else { 106 rnd_add_data(NULL, &val, sizeof(val), 0); 107 } 108 } 109 110 static __inline void 111 rnd_add_uint64(krndsource_t *kr, uint64_t val) 112 { 113 if (__predict_true(kr)) { 114 if (RND_ENABLED(kr)) { 115 _rnd_add_uint64(kr, val); 116 } 117 } else { 118 rnd_add_data(NULL, &val, sizeof(val), 0); 119 } 120 } 121 122 #endif /* _SYS_RNDSOURCE_H */ 123