1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 #include <sys/random.h>
35 #include <sys/systm.h>
36
37 #include <dev/random/randomdev.h>
38 #include <dev/random/randomdev_soft.h>
39 #include <dev/random/random_adaptors.h>
40 #include <dev/random/live_entropy_sources.h>
41
42 static void live_random_example_init(void);
43 static void live_random_example_deinit(void);
44 static u_int live_random_example_read(void *, u_int);
45
46 struct random_adaptor live_random_example = {
47 .les_ident = "Example RNG",
48 .les_source = RANDOM_PURE_BOGUS, /* Make sure this is in
49 * sys/random.h and is unique */
50 .les_read = live_random_example_read,
51 };
52
53 /*
54 * Used under the license provided @ http://xkcd.com/221/
55 * http://creativecommons.org/licenses/by-nc/2.5/
56 */
57 static uint8_t
getRandomNumber(void)58 getRandomNumber(void)
59 {
60 return 4; /* chosen by fair dice roll, guaranteed to be random */
61 }
62
63 static void
live_random_example_init(void)64 live_random_example_init(void)
65 {
66
67 /* Do initialisation stuff here */
68 }
69
70 static void
live_random_example_deinit(void)71 live_random_example_deinit(void)
72 {
73
74 /* Do de-initialisation stuff here */
75 }
76
77 /* get <c> bytes of random stuff into <buf>. You may presume
78 * that <c> is a multiple of 2^n, with n>=3. A typical value
79 * is c=16.
80 */
81 static u_int
live_random_example_read(void * buf,u_int c)82 live_random_example_read(void *buf, u_int c)
83 {
84 uint8_t *b;
85 int count;
86
87 b = buf;
88
89 for (count = 0; count < c; count++)
90 b[count] = getRandomNumber();
91
92 /* printf("returning %d bytes of pure randomness\n", c); */
93 return (c);
94 }
95
96 /* ARGSUSED */
97 static int
live_random_example_modevent(module_t mod __unused,int type,void * unused __unused)98 live_random_example_modevent(module_t mod __unused, int type, void *unused __unused)
99 {
100 int error = 0;
101
102 switch (type) {
103 case MOD_LOAD:
104 live_entropy_source_register(&live_random_example);
105 break;
106
107 case MOD_UNLOAD:
108 live_entropy_source_deregister(&live_random_example);
109 break;
110
111 case MOD_SHUTDOWN:
112 break;
113
114 default:
115 error = EOPNOTSUPP;
116 break;
117 }
118
119 return (error);
120 }
121
122 DEV_MODULE(live_random_example, live_random_example_modevent, NULL);
123 MODULE_VERSION(live_random_example, 1);
124 MODULE_DEPEND(live_random_example, randomdev, 1, 1, 1);
125