xref: /freebsd/sys/dev/random/other_algorithm.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1646041a8SMark Murray /*-
219fa89e9SMark 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,
3319fa89e9SMark 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
3819fa89e9SMark Murray  * supported branch (This is currently 12-CURRENT, and may be no
3919fa89e9SMark Murray  * older than 12-STABLE in the future).
40646041a8SMark Murray  */
41646041a8SMark Murray 
42646041a8SMark Murray #include <sys/cdefs.h>
4319fa89e9SMark Murray #include <sys/limits.h>
4419fa89e9SMark Murray 
4519fa89e9SMark Murray #ifdef _KERNEL
46646041a8SMark Murray #include <sys/param.h>
47646041a8SMark Murray #include <sys/kernel.h>
48646041a8SMark Murray #include <sys/lock.h>
49646041a8SMark Murray #include <sys/malloc.h>
50646041a8SMark Murray #include <sys/mutex.h>
51646041a8SMark Murray #include <sys/random.h>
52646041a8SMark Murray #include <sys/sysctl.h>
53646041a8SMark Murray #include <sys/systm.h>
54646041a8SMark Murray 
55646041a8SMark Murray #include <machine/cpu.h>
56646041a8SMark Murray 
57646041a8SMark Murray #include <crypto/rijndael/rijndael-api-fst.h>
587a3f5d11SAllan Jude #include <crypto/sha2/sha256.h>
59646041a8SMark Murray 
60646041a8SMark Murray #include <dev/random/hash.h>
61646041a8SMark Murray #include <dev/random/randomdev.h>
62646041a8SMark Murray #include <dev/random/random_harvestq.h>
63646041a8SMark Murray #include <dev/random/uint128.h>
64646041a8SMark Murray #include <dev/random/other_algorithm.h>
6519fa89e9SMark Murray #else /* !_KERNEL */
6619fa89e9SMark Murray #include <inttypes.h>
6719fa89e9SMark Murray #include <stdbool.h>
6819fa89e9SMark Murray #include <stdio.h>
6919fa89e9SMark Murray #include <stdlib.h>
7019fa89e9SMark Murray #include <string.h>
7119fa89e9SMark Murray #include <threads.h>
7219fa89e9SMark Murray 
7319fa89e9SMark Murray #include "unit_test.h"
7419fa89e9SMark Murray 
7519fa89e9SMark Murray #include <crypto/rijndael/rijndael-api-fst.h>
7619fa89e9SMark Murray #include <crypto/sha2/sha256.h>
7719fa89e9SMark Murray 
7819fa89e9SMark Murray #include <dev/random/hash.h>
7919fa89e9SMark Murray #include <dev/random/randomdev.h>
8019fa89e9SMark Murray #include <dev/random/uint128.h>
8119fa89e9SMark Murray #include <dev/random/other_algorithm.h>
8219fa89e9SMark Murray #endif /* _KERNEL */
83646041a8SMark Murray 
84646041a8SMark Murray static void random_other_pre_read(void);
85d0d71d81SConrad Meyer static void random_other_read(uint8_t *, size_t);
86646041a8SMark Murray static bool random_other_seeded(void);
87646041a8SMark Murray static void random_other_process_event(struct harvest_event *);
88646041a8SMark Murray 
89646041a8SMark Murray /*
90646041a8SMark Murray  * RANDOM_OTHER_NPOOLS is used when reading hardware random
91646041a8SMark Murray  * number sources to ensure that each pool gets one read sample
9219fa89e9SMark Murray  * per loop iteration. Fortuna has 32 (0-31).
93646041a8SMark Murray  */
94646041a8SMark Murray #define RANDOM_OTHER_NPOOLS 1
95646041a8SMark Murray 
96*3ee1d5bbSConrad Meyer #ifdef RANDOM_LOADABLE
97*3ee1d5bbSConrad Meyer static
98*3ee1d5bbSConrad Meyer #endif
99*3ee1d5bbSConrad Meyer const struct random_algorithm random_alg_context = {
100646041a8SMark Murray 	.ra_ident = "other",
101646041a8SMark Murray 	.ra_pre_read = random_other_pre_read,
102646041a8SMark Murray 	.ra_read = random_other_read,
103646041a8SMark Murray 	.ra_seeded = random_other_seeded,
104646041a8SMark Murray 	.ra_event_processor = random_other_process_event,
105646041a8SMark Murray 	.ra_poolcount = RANDOM_OTHER_NPOOLS,
106646041a8SMark Murray };
107646041a8SMark Murray 
108646041a8SMark Murray /* Use a mutex to protect your reseed variables? */
109646041a8SMark Murray static mtx_t other_mtx;
110646041a8SMark Murray 
111646041a8SMark Murray /*
112646041a8SMark Murray  * Do algorithm-specific initialisation here.
113646041a8SMark Murray  */
114*3ee1d5bbSConrad Meyer static void
random_other_init_alg(void * unused __unused)115646041a8SMark Murray random_other_init_alg(void *unused __unused)
116646041a8SMark Murray {
117646041a8SMark Murray 
118*3ee1d5bbSConrad Meyer #ifdef RANDOM_LOADABLE
119*3ee1d5bbSConrad Meyer 	p_random_alg_context = &random_alg_context;
120*3ee1d5bbSConrad Meyer #endif
121*3ee1d5bbSConrad Meyer 
122646041a8SMark Murray 	RANDOM_RESEED_INIT_LOCK();
123646041a8SMark Murray }
124*3ee1d5bbSConrad Meyer SYSINIT(random_alg, SI_SUB_RANDOM, SI_ORDER_SECOND, random_other_init_alg,
125*3ee1d5bbSConrad Meyer     NULL);
126646041a8SMark Murray 
127646041a8SMark Murray /*
128646041a8SMark Murray  * void random_other_pre_read(void)
129646041a8SMark Murray  *
130646041a8SMark Murray  * Do any pre-read preparation you need to. This will be called
131646041a8SMark Murray  * before >=1 calls to random_other_read() corresponding to one
132646041a8SMark Murray  * read(2).
133646041a8SMark Murray  *
134646041a8SMark Murray  * This routine will be called periodically while the generator is
135646041a8SMark Murray  * still blocked and a read is being attempted, giving you an
136646041a8SMark Murray  * opportunity to unblock.
137646041a8SMark Murray  */
138646041a8SMark Murray static void
random_other_pre_read(void)139646041a8SMark Murray random_other_pre_read(void)
140646041a8SMark Murray {
141646041a8SMark Murray 
142646041a8SMark Murray 	RANDOM_RESEED_LOCK();
143646041a8SMark Murray 	/*
144646041a8SMark Murray 	 * Do pre-read housekeeping work here!
145646041a8SMark Murray 	 * You may use this as a chance to unblock the generator.
146646041a8SMark Murray 	 */
147646041a8SMark Murray 	RANDOM_RESEED_UNLOCK();
148646041a8SMark Murray }
149646041a8SMark Murray 
150646041a8SMark Murray /*
151d0d71d81SConrad Meyer  * void random_other_read(uint8_t *buf, size_t count)
152646041a8SMark Murray  *
153646041a8SMark Murray  * Generate <count> bytes of output into <*buf>.
154d0d71d81SConrad Meyer  * You may NOT use the fact that <count> will be a multiple of
155646041a8SMark Murray  * RANDOM_BLOCKSIZE for optimization purposes.
156646041a8SMark Murray  *
157646041a8SMark Murray  * This function will always be called with your generator
158646041a8SMark Murray  * unblocked and ready. If you are not ready to generate
159646041a8SMark Murray  * output here, then feel free to KASSERT() or panic().
160646041a8SMark Murray  */
161646041a8SMark Murray static void
random_other_read(uint8_t * buf,size_t count)162d0d71d81SConrad Meyer random_other_read(uint8_t *buf, size_t count)
163646041a8SMark Murray {
164646041a8SMark Murray 
165646041a8SMark Murray 	RANDOM_RESEED_LOCK();
166646041a8SMark Murray 	/*
167646041a8SMark Murray 	 * Do random-number generation work here!
168646041a8SMark Murray 	 */
169646041a8SMark Murray 	RANDOM_RESEED_UNLOCK();
170646041a8SMark Murray }
171646041a8SMark Murray 
172646041a8SMark Murray /*
173646041a8SMark Murray  * bool random_other_seeded(void)
174646041a8SMark Murray  *
175646041a8SMark Murray  * Return true if your generator is ready to generate
176646041a8SMark Murray  * output, and false otherwise.
177646041a8SMark Murray  */
178646041a8SMark Murray static bool
random_other_seeded(void)179646041a8SMark Murray random_other_seeded(void)
180646041a8SMark Murray {
181646041a8SMark Murray 	bool seeded = false;
182646041a8SMark Murray 
183646041a8SMark Murray 	/*
184646041a8SMark Murray 	 * Find out if your generator is seeded here!
185646041a8SMark Murray 	 */
186646041a8SMark Murray 	return (seeded);
187646041a8SMark Murray }
188646041a8SMark Murray 
189646041a8SMark Murray /*
190646041a8SMark Murray  * void random_other_process_event(struct harvest_event *event)
191646041a8SMark Murray  *
192646041a8SMark Murray  * Process one stochastic event <*event> into your entropy
193646041a8SMark Murray  * processor.
194646041a8SMark Murray  *
195646041a8SMark Murray  * The structure of the event may change, so it is easier to
196646041a8SMark Murray  * just grab the whole thing into your accumulation system.
197646041a8SMark Murray  * You may pick-and-choose bits, but please don't complain
198646041a8SMark Murray  * when/if these change.
199646041a8SMark Murray  */
200646041a8SMark Murray static void
random_other_process_event(struct harvest_event * event)201646041a8SMark Murray random_other_process_event(struct harvest_event *event)
202646041a8SMark Murray {
203646041a8SMark Murray 
204646041a8SMark Murray 	RANDOM_RESEED_LOCK();
205646041a8SMark Murray 	/*
206646041a8SMark Murray 	 * Do entropy accumulation work here!
207646041a8SMark Murray 	 * You may use this as a chance to unblock the generator.
208646041a8SMark Murray 	 */
209646041a8SMark Murray 	RANDOM_RESEED_UNLOCK();
210646041a8SMark Murray }
211