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