1646041a8SMark Murray /*-
2*19fa89e9SMark Murray  * Copyright (c) 2015-2018 Mark R V Murray
3646041a8SMark Murray  * All rights reserved.
4646041a8SMark Murray  *
5646041a8SMark Murray  * Redistribution and use in source and binary forms, with or without
6646041a8SMark Murray  * modification, are permitted provided that the following conditions
7646041a8SMark Murray  * are met:
8646041a8SMark Murray  * 1. Redistributions of source code must retain the above copyright
9646041a8SMark Murray  *    notice, this list of conditions and the following disclaimer
10646041a8SMark Murray  *    in this position and unchanged.
11646041a8SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
12646041a8SMark Murray  *    notice, this list of conditions and the following disclaimer in the
13646041a8SMark Murray  *    documentation and/or other materials provided with the distribution.
14646041a8SMark Murray  *
15646041a8SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16646041a8SMark Murray  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17646041a8SMark Murray  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18646041a8SMark Murray  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19646041a8SMark Murray  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20646041a8SMark Murray  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21646041a8SMark Murray  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22646041a8SMark Murray  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23646041a8SMark Murray  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24646041a8SMark Murray  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25646041a8SMark Murray  *
26646041a8SMark Murray  */
27646041a8SMark Murray 
28646041a8SMark Murray /*-
29646041a8SMark Murray  * This is a skeleton for folks who wish to build a loadable module
30646041a8SMark Murray  * containing an alternative entropy-processing algorithm for random(4).
31646041a8SMark Murray  *
32646041a8SMark Murray  * The functions below should be completed with the appropriate code,
33*19fa89e9SMark Murray  * and the nearby fortuna.c may be consulted for examples of working code.
34646041a8SMark Murray  *
35646041a8SMark Murray  * The author is willing to provide reasonable help to those wishing to
36646041a8SMark Murray  * write such a module for themselves. Please use the markm@ FreeBSD
37646041a8SMark Murray  * email address, and ensure that you are developing this on a suitably
38*19fa89e9SMark Murray  * supported branch (This is currently 12-CURRENT, and may be no
39*19fa89e9SMark Murray  * older than 12-STABLE in the future).
40646041a8SMark Murray  */
41646041a8SMark Murray 
42646041a8SMark Murray #include <sys/cdefs.h>
43646041a8SMark Murray __FBSDID("$FreeBSD$");
44646041a8SMark Murray 
45*19fa89e9SMark Murray #include <sys/limits.h>
46*19fa89e9SMark Murray 
47*19fa89e9SMark Murray #ifdef _KERNEL
48646041a8SMark Murray #include <sys/param.h>
49646041a8SMark Murray #include <sys/kernel.h>
50646041a8SMark Murray #include <sys/lock.h>
51646041a8SMark Murray #include <sys/malloc.h>
52646041a8SMark Murray #include <sys/mutex.h>
53646041a8SMark Murray #include <sys/random.h>
54646041a8SMark Murray #include <sys/sysctl.h>
55646041a8SMark Murray #include <sys/systm.h>
56646041a8SMark Murray 
57646041a8SMark Murray #include <machine/cpu.h>
58646041a8SMark Murray 
59646041a8SMark Murray #include <crypto/rijndael/rijndael-api-fst.h>
607a3f5d11SAllan Jude #include <crypto/sha2/sha256.h>
61646041a8SMark Murray 
62646041a8SMark Murray #include <dev/random/hash.h>
63646041a8SMark Murray #include <dev/random/randomdev.h>
64646041a8SMark Murray #include <dev/random/random_harvestq.h>
65646041a8SMark Murray #include <dev/random/uint128.h>
66646041a8SMark Murray #include <dev/random/other_algorithm.h>
67*19fa89e9SMark Murray #else /* !_KERNEL */
68*19fa89e9SMark Murray #include <inttypes.h>
69*19fa89e9SMark Murray #include <stdbool.h>
70*19fa89e9SMark Murray #include <stdio.h>
71*19fa89e9SMark Murray #include <stdlib.h>
72*19fa89e9SMark Murray #include <string.h>
73*19fa89e9SMark Murray #include <threads.h>
74*19fa89e9SMark Murray 
75*19fa89e9SMark Murray #include "unit_test.h"
76*19fa89e9SMark Murray 
77*19fa89e9SMark Murray #include <crypto/rijndael/rijndael-api-fst.h>
78*19fa89e9SMark Murray #include <crypto/sha2/sha256.h>
79*19fa89e9SMark Murray 
80*19fa89e9SMark Murray #include <dev/random/hash.h>
81*19fa89e9SMark Murray #include <dev/random/randomdev.h>
82*19fa89e9SMark Murray #include <dev/random/uint128.h>
83*19fa89e9SMark Murray #include <dev/random/other_algorithm.h>
84*19fa89e9SMark Murray #endif /* _KERNEL */
85646041a8SMark Murray 
86646041a8SMark Murray static void random_other_pre_read(void);
87646041a8SMark Murray static void random_other_read(uint8_t *, u_int);
88646041a8SMark Murray static bool random_other_seeded(void);
89646041a8SMark Murray static void random_other_process_event(struct harvest_event *);
90646041a8SMark Murray static void random_other_init_alg(void *);
91646041a8SMark Murray static void random_other_deinit_alg(void *);
92646041a8SMark Murray 
93646041a8SMark Murray /*
94646041a8SMark Murray  * RANDOM_OTHER_NPOOLS is used when reading hardware random
95646041a8SMark Murray  * number sources to ensure that each pool gets one read sample
96*19fa89e9SMark Murray  * per loop iteration. Fortuna has 32 (0-31).
97646041a8SMark Murray  */
98646041a8SMark Murray #define RANDOM_OTHER_NPOOLS 1
99646041a8SMark Murray 
100646041a8SMark Murray struct random_algorithm random_alg_context = {
101646041a8SMark Murray 	.ra_ident = "other",
102646041a8SMark Murray 	.ra_init_alg = random_other_init_alg,
103646041a8SMark Murray 	.ra_deinit_alg = random_other_deinit_alg,
104646041a8SMark Murray 	.ra_pre_read = random_other_pre_read,
105646041a8SMark Murray 	.ra_read = random_other_read,
106646041a8SMark Murray 	.ra_seeded = random_other_seeded,
107646041a8SMark Murray 	.ra_event_processor = random_other_process_event,
108646041a8SMark Murray 	.ra_poolcount = RANDOM_OTHER_NPOOLS,
109646041a8SMark Murray };
110646041a8SMark Murray 
111646041a8SMark Murray /* Use a mutex to protect your reseed variables? */
112646041a8SMark Murray static mtx_t other_mtx;
113646041a8SMark Murray 
114646041a8SMark Murray /*
115646041a8SMark Murray  * void random_other_init_alg(void *unused __unused)
116646041a8SMark Murray  *
117646041a8SMark Murray  * Do algorithm-specific initialisation here.
118646041a8SMark Murray  */
119646041a8SMark Murray void
random_other_init_alg(void * unused __unused)120646041a8SMark Murray random_other_init_alg(void *unused __unused)
121646041a8SMark Murray {
122646041a8SMark Murray 
123646041a8SMark Murray 	RANDOM_RESEED_INIT_LOCK();
124646041a8SMark Murray 	/*
125646041a8SMark Murray 	 * Do set-up work here!
126646041a8SMark Murray 	 */
127646041a8SMark Murray }
128646041a8SMark Murray 
129646041a8SMark Murray /*
130646041a8SMark Murray  * void random_other_deinit_alg(void *unused __unused)
131646041a8SMark Murray  *
132646041a8SMark Murray  * Do algorithm-specific deinitialisation here.
133646041a8SMark Murray  */
134646041a8SMark Murray static void
random_other_deinit_alg(void * unused __unused)135646041a8SMark Murray random_other_deinit_alg(void *unused __unused)
136646041a8SMark Murray {
137646041a8SMark Murray 
138646041a8SMark Murray 	/*
139646041a8SMark Murray 	 * Do tear-down work here!
140646041a8SMark Murray 	 */
141646041a8SMark Murray 	RANDOM_RESEED_DEINIT_LOCK();
142646041a8SMark Murray }
143646041a8SMark Murray 
144646041a8SMark Murray /*
145646041a8SMark Murray  * void random_other_pre_read(void)
146646041a8SMark Murray  *
147646041a8SMark Murray  * Do any pre-read preparation you need to. This will be called
148646041a8SMark Murray  * before >=1 calls to random_other_read() corresponding to one
149646041a8SMark Murray  * read(2).
150646041a8SMark Murray  *
151646041a8SMark Murray  * This routine will be called periodically while the generator is
152646041a8SMark Murray  * still blocked and a read is being attempted, giving you an
153646041a8SMark Murray  * opportunity to unblock.
154646041a8SMark Murray  */
155646041a8SMark Murray static void
random_other_pre_read(void)156646041a8SMark Murray random_other_pre_read(void)
157646041a8SMark Murray {
158646041a8SMark Murray 
159646041a8SMark Murray 	RANDOM_RESEED_LOCK();
160646041a8SMark Murray 	/*
161646041a8SMark Murray 	 * Do pre-read housekeeping work here!
162646041a8SMark Murray 	 * You may use this as a chance to unblock the generator.
163646041a8SMark Murray 	 */
164646041a8SMark Murray 	RANDOM_RESEED_UNLOCK();
165646041a8SMark Murray }
166646041a8SMark Murray 
167646041a8SMark Murray /*
168646041a8SMark Murray  * void random_other_read(uint8_t *buf, u_int count)
169646041a8SMark Murray  *
170646041a8SMark Murray  * Generate <count> bytes of output into <*buf>.
171646041a8SMark Murray  * You may use the fact that <count> will be a multiple of
172646041a8SMark Murray  * RANDOM_BLOCKSIZE for optimization purposes.
173646041a8SMark Murray  *
174646041a8SMark Murray  * This function will always be called with your generator
175646041a8SMark Murray  * unblocked and ready. If you are not ready to generate
176646041a8SMark Murray  * output here, then feel free to KASSERT() or panic().
177646041a8SMark Murray  */
178646041a8SMark Murray static void
random_other_read(uint8_t * buf,u_int count)179646041a8SMark Murray random_other_read(uint8_t *buf, u_int count)
180646041a8SMark Murray {
181646041a8SMark Murray 
182646041a8SMark Murray 	RANDOM_RESEED_LOCK();
183646041a8SMark Murray 	/*
184646041a8SMark Murray 	 * Do random-number generation work here!
185646041a8SMark Murray 	 */
186646041a8SMark Murray 	RANDOM_RESEED_UNLOCK();
187646041a8SMark Murray }
188646041a8SMark Murray 
189646041a8SMark Murray /*
190646041a8SMark Murray  * bool random_other_seeded(void)
191646041a8SMark Murray  *
192646041a8SMark Murray  * Return true if your generator is ready to generate
193646041a8SMark Murray  * output, and false otherwise.
194646041a8SMark Murray  */
195646041a8SMark Murray static bool
random_other_seeded(void)196646041a8SMark Murray random_other_seeded(void)
197646041a8SMark Murray {
198646041a8SMark Murray 	bool seeded = false;
199646041a8SMark Murray 
200646041a8SMark Murray 	/*
201646041a8SMark Murray 	 * Find out if your generator is seeded here!
202646041a8SMark Murray 	 */
203646041a8SMark Murray 	return (seeded);
204646041a8SMark Murray }
205646041a8SMark Murray 
206646041a8SMark Murray /*
207646041a8SMark Murray  * void random_other_process_event(struct harvest_event *event)
208646041a8SMark Murray  *
209646041a8SMark Murray  * Process one stochastic event <*event> into your entropy
210646041a8SMark Murray  * processor.
211646041a8SMark Murray  *
212646041a8SMark Murray  * The structure of the event may change, so it is easier to
213646041a8SMark Murray  * just grab the whole thing into your accumulation system.
214646041a8SMark Murray  * You may pick-and-choose bits, but please don't complain
215646041a8SMark Murray  * when/if these change.
216646041a8SMark Murray  */
217646041a8SMark Murray static void
random_other_process_event(struct harvest_event * event)218646041a8SMark Murray random_other_process_event(struct harvest_event *event)
219646041a8SMark Murray {
220646041a8SMark Murray 
221646041a8SMark Murray 	RANDOM_RESEED_LOCK();
222646041a8SMark Murray 	/*
223646041a8SMark Murray 	 * Do entropy accumulation work here!
224646041a8SMark Murray 	 * You may use this as a chance to unblock the generator.
225646041a8SMark Murray 	 */
226646041a8SMark Murray 	RANDOM_RESEED_UNLOCK();
227646041a8SMark Murray }
228