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>
43646041a8SMark Murray __FBSDID("$FreeBSD$");
44646041a8SMark Murray
4519fa89e9SMark Murray #include <sys/limits.h>
4619fa89e9SMark Murray
4719fa89e9SMark 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>
6719fa89e9SMark Murray #else /* !_KERNEL */
6819fa89e9SMark Murray #include <inttypes.h>
6919fa89e9SMark Murray #include <stdbool.h>
7019fa89e9SMark Murray #include <stdio.h>
7119fa89e9SMark Murray #include <stdlib.h>
7219fa89e9SMark Murray #include <string.h>
7319fa89e9SMark Murray #include <threads.h>
7419fa89e9SMark Murray
7519fa89e9SMark Murray #include "unit_test.h"
7619fa89e9SMark Murray
7719fa89e9SMark Murray #include <crypto/rijndael/rijndael-api-fst.h>
7819fa89e9SMark Murray #include <crypto/sha2/sha256.h>
7919fa89e9SMark Murray
8019fa89e9SMark Murray #include <dev/random/hash.h>
8119fa89e9SMark Murray #include <dev/random/randomdev.h>
8219fa89e9SMark Murray #include <dev/random/uint128.h>
8319fa89e9SMark Murray #include <dev/random/other_algorithm.h>
8419fa89e9SMark Murray #endif /* _KERNEL */
85646041a8SMark Murray
86646041a8SMark Murray static void random_other_pre_read(void);
87d0d71d81SConrad Meyer static void random_other_read(uint8_t *, size_t);
88646041a8SMark Murray static bool random_other_seeded(void);
89646041a8SMark Murray static void random_other_process_event(struct harvest_event *);
90646041a8SMark Murray
91646041a8SMark Murray /*
92646041a8SMark Murray * RANDOM_OTHER_NPOOLS is used when reading hardware random
93646041a8SMark Murray * number sources to ensure that each pool gets one read sample
9419fa89e9SMark Murray * per loop iteration. Fortuna has 32 (0-31).
95646041a8SMark Murray */
96646041a8SMark Murray #define RANDOM_OTHER_NPOOLS 1
97646041a8SMark Murray
98*3ee1d5bbSConrad Meyer #ifdef RANDOM_LOADABLE
99*3ee1d5bbSConrad Meyer static
100*3ee1d5bbSConrad Meyer #endif
101*3ee1d5bbSConrad Meyer const struct random_algorithm random_alg_context = {
102646041a8SMark Murray .ra_ident = "other",
103646041a8SMark Murray .ra_pre_read = random_other_pre_read,
104646041a8SMark Murray .ra_read = random_other_read,
105646041a8SMark Murray .ra_seeded = random_other_seeded,
106646041a8SMark Murray .ra_event_processor = random_other_process_event,
107646041a8SMark Murray .ra_poolcount = RANDOM_OTHER_NPOOLS,
108646041a8SMark Murray };
109646041a8SMark Murray
110646041a8SMark Murray /* Use a mutex to protect your reseed variables? */
111646041a8SMark Murray static mtx_t other_mtx;
112646041a8SMark Murray
113646041a8SMark Murray /*
114646041a8SMark Murray * Do algorithm-specific initialisation here.
115646041a8SMark Murray */
116*3ee1d5bbSConrad Meyer static void
random_other_init_alg(void * unused __unused)117646041a8SMark Murray random_other_init_alg(void *unused __unused)
118646041a8SMark Murray {
119646041a8SMark Murray
120*3ee1d5bbSConrad Meyer #ifdef RANDOM_LOADABLE
121*3ee1d5bbSConrad Meyer p_random_alg_context = &random_alg_context;
122*3ee1d5bbSConrad Meyer #endif
123*3ee1d5bbSConrad Meyer
124646041a8SMark Murray RANDOM_RESEED_INIT_LOCK();
125646041a8SMark Murray }
126*3ee1d5bbSConrad Meyer SYSINIT(random_alg, SI_SUB_RANDOM, SI_ORDER_SECOND, random_other_init_alg,
127*3ee1d5bbSConrad Meyer NULL);
128646041a8SMark Murray
129646041a8SMark Murray /*
130646041a8SMark Murray * void random_other_pre_read(void)
131646041a8SMark Murray *
132646041a8SMark Murray * Do any pre-read preparation you need to. This will be called
133646041a8SMark Murray * before >=1 calls to random_other_read() corresponding to one
134646041a8SMark Murray * read(2).
135646041a8SMark Murray *
136646041a8SMark Murray * This routine will be called periodically while the generator is
137646041a8SMark Murray * still blocked and a read is being attempted, giving you an
138646041a8SMark Murray * opportunity to unblock.
139646041a8SMark Murray */
140646041a8SMark Murray static void
random_other_pre_read(void)141646041a8SMark Murray random_other_pre_read(void)
142646041a8SMark Murray {
143646041a8SMark Murray
144646041a8SMark Murray RANDOM_RESEED_LOCK();
145646041a8SMark Murray /*
146646041a8SMark Murray * Do pre-read housekeeping work here!
147646041a8SMark Murray * You may use this as a chance to unblock the generator.
148646041a8SMark Murray */
149646041a8SMark Murray RANDOM_RESEED_UNLOCK();
150646041a8SMark Murray }
151646041a8SMark Murray
152646041a8SMark Murray /*
153d0d71d81SConrad Meyer * void random_other_read(uint8_t *buf, size_t count)
154646041a8SMark Murray *
155646041a8SMark Murray * Generate <count> bytes of output into <*buf>.
156d0d71d81SConrad Meyer * You may NOT use the fact that <count> will be a multiple of
157646041a8SMark Murray * RANDOM_BLOCKSIZE for optimization purposes.
158646041a8SMark Murray *
159646041a8SMark Murray * This function will always be called with your generator
160646041a8SMark Murray * unblocked and ready. If you are not ready to generate
161646041a8SMark Murray * output here, then feel free to KASSERT() or panic().
162646041a8SMark Murray */
163646041a8SMark Murray static void
random_other_read(uint8_t * buf,size_t count)164d0d71d81SConrad Meyer random_other_read(uint8_t *buf, size_t count)
165646041a8SMark Murray {
166646041a8SMark Murray
167646041a8SMark Murray RANDOM_RESEED_LOCK();
168646041a8SMark Murray /*
169646041a8SMark Murray * Do random-number generation work here!
170646041a8SMark Murray */
171646041a8SMark Murray RANDOM_RESEED_UNLOCK();
172646041a8SMark Murray }
173646041a8SMark Murray
174646041a8SMark Murray /*
175646041a8SMark Murray * bool random_other_seeded(void)
176646041a8SMark Murray *
177646041a8SMark Murray * Return true if your generator is ready to generate
178646041a8SMark Murray * output, and false otherwise.
179646041a8SMark Murray */
180646041a8SMark Murray static bool
random_other_seeded(void)181646041a8SMark Murray random_other_seeded(void)
182646041a8SMark Murray {
183646041a8SMark Murray bool seeded = false;
184646041a8SMark Murray
185646041a8SMark Murray /*
186646041a8SMark Murray * Find out if your generator is seeded here!
187646041a8SMark Murray */
188646041a8SMark Murray return (seeded);
189646041a8SMark Murray }
190646041a8SMark Murray
191646041a8SMark Murray /*
192646041a8SMark Murray * void random_other_process_event(struct harvest_event *event)
193646041a8SMark Murray *
194646041a8SMark Murray * Process one stochastic event <*event> into your entropy
195646041a8SMark Murray * processor.
196646041a8SMark Murray *
197646041a8SMark Murray * The structure of the event may change, so it is easier to
198646041a8SMark Murray * just grab the whole thing into your accumulation system.
199646041a8SMark Murray * You may pick-and-choose bits, but please don't complain
200646041a8SMark Murray * when/if these change.
201646041a8SMark Murray */
202646041a8SMark Murray static void
random_other_process_event(struct harvest_event * event)203646041a8SMark Murray random_other_process_event(struct harvest_event *event)
204646041a8SMark Murray {
205646041a8SMark Murray
206646041a8SMark Murray RANDOM_RESEED_LOCK();
207646041a8SMark Murray /*
208646041a8SMark Murray * Do entropy accumulation work here!
209646041a8SMark Murray * You may use this as a chance to unblock the generator.
210646041a8SMark Murray */
211646041a8SMark Murray RANDOM_RESEED_UNLOCK();
212646041a8SMark Murray }
213