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/cdefs.h> 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/lock.h> 34 #include <sys/module.h> 35 #include <sys/random.h> 36 #include <sys/systm.h> 37 38 #include <dev/random/randomdev.h> 39 #include <dev/random/randomdev_soft.h> 40 #include <dev/random/random_adaptors.h> 41 #include <dev/random/live_entropy_sources.h> 42 43 static void live_random_example_init(void); 44 static void live_random_example_deinit(void); 45 static u_int live_random_example_read(void *, u_int); 46 47 struct random_adaptor live_random_example = { 48 .les_ident = "Example RNG", 49 .les_source = RANDOM_PURE_BOGUS, /* Make sure this is in 50 * sys/random.h and is unique */ 51 .les_read = live_random_example_read, 52 }; 53 54 /* 55 * Used under the license provided @ http://xkcd.com/221/ 56 * http://creativecommons.org/licenses/by-nc/2.5/ 57 */ 58 static uint8_t 59 getRandomNumber(void) 60 { 61 return 4; /* chosen by fair dice roll, guaranteed to be random */ 62 } 63 64 static void 65 live_random_example_init(void) 66 { 67 68 /* Do initialisation stuff here */ 69 } 70 71 static void 72 live_random_example_deinit(void) 73 { 74 75 /* Do de-initialisation stuff here */ 76 } 77 78 /* get <c> bytes of random stuff into <buf>. You may presume 79 * that <c> is a multiple of 2^n, with n>=3. A typical value 80 * is c=16. 81 */ 82 static u_int 83 live_random_example_read(void *buf, u_int c) 84 { 85 uint8_t *b; 86 int count; 87 88 b = buf; 89 90 for (count = 0; count < c; count++) 91 b[count] = getRandomNumber(); 92 93 /* printf("returning %d bytes of pure randomness\n", c); */ 94 return (c); 95 } 96 97 /* ARGSUSED */ 98 static int 99 live_random_example_modevent(module_t mod __unused, int type, void *unused __unused) 100 { 101 int error = 0; 102 103 switch (type) { 104 case MOD_LOAD: 105 live_entropy_source_register(&live_random_example); 106 break; 107 108 case MOD_UNLOAD: 109 live_entropy_source_deregister(&live_random_example); 110 break; 111 112 case MOD_SHUTDOWN: 113 break; 114 115 default: 116 error = EOPNOTSUPP; 117 break; 118 } 119 120 return (error); 121 } 122 123 DEV_MODULE(live_random_example, live_random_example_modevent, NULL); 124 MODULE_VERSION(live_random_example, 1); 125 MODULE_DEPEND(live_random_example, randomdev, 1, 1, 1); 126