1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com> 3219820Sjeff * All rights reserved. 4219820Sjeff * 5219820Sjeff * Redistribution and use in source and binary forms, with or without 6219820Sjeff * modification, are permitted provided that the following conditions 7219820Sjeff * are met: 8219820Sjeff * 1. Redistributions of source code must retain the above copyright 9219820Sjeff * notice, this list of conditions and the following disclaimer 10219820Sjeff * in this position and unchanged. 11219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 12219820Sjeff * notice, this list of conditions and the following disclaimer in the 13219820Sjeff * documentation and/or other materials provided with the distribution. 14219820Sjeff * 15219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25219820Sjeff * 26219820Sjeff */ 27219820Sjeff 28219820Sjeff#include <sys/cdefs.h> 29219820Sjeff__FBSDID("$FreeBSD$"); 30219820Sjeff 31219820Sjeff#include <sys/param.h> 32219820Sjeff#include <sys/kernel.h> 33219820Sjeff#include <sys/lock.h> 34219820Sjeff#include <sys/module.h> 35219820Sjeff#include <sys/random.h> 36219820Sjeff#include <sys/systm.h> 37219820Sjeff 38219820Sjeff#include <dev/random/live_entropy_sources.h> 39219820Sjeff#include <dev/random/random_adaptors.h> 40219820Sjeff#include <dev/random/randomdev.h> 41219820Sjeff 42219820Sjeffstatic int random_example_read(void *, int); 43219820Sjeff 44219820Sjeffstruct random_adaptor random_example = { 45219820Sjeff .ident = "Example RNG", 46219820Sjeff .source = RANDOM_PURE_BOGUS, /* Make sure this is in 47219820Sjeff * sys/random.h and is unique */ 48219820Sjeff .read = random_example_read, 49219820Sjeff}; 50219820Sjeff 51219820Sjeff/* 52219820Sjeff * Used under the license provided @ http://xkcd.com/221/ 53219820Sjeff * http://creativecommons.org/licenses/by-nc/2.5/ 54219820Sjeff */ 55219820Sjeffstatic uint8_t 56219820SjeffgetRandomNumber(void) 57219820Sjeff{ 58219820Sjeff return 4; /* chosen by fair dice roll, guaranteed to be random */ 59219820Sjeff} 60219820Sjeff 61219820Sjeffstatic int 62219820Sjeffrandom_example_read(void *buf, int c) 63219820Sjeff{ 64219820Sjeff uint8_t *b; 65219820Sjeff int count; 66219820Sjeff 67219820Sjeff b = buf; 68219820Sjeff 69219820Sjeff for (count = 0; count < c; count++) 70219820Sjeff b[count] = getRandomNumber(); 71219820Sjeff 72219820Sjeff printf("returning %d bytes of pure randomness\n", c); 73219820Sjeff return (c); 74219820Sjeff} 75219820Sjeff 76219820Sjeffstatic int 77219820Sjeffrandom_example_modevent(module_t mod, int type, void *unused) 78219820Sjeff{ 79219820Sjeff int error = 0; 80219820Sjeff 81219820Sjeff switch (type) { 82219820Sjeff case MOD_LOAD: 83219820Sjeff live_entropy_source_register(&random_example); 84219820Sjeff break; 85219820Sjeff 86219820Sjeff case MOD_UNLOAD: 87219820Sjeff live_entropy_source_deregister(&random_example); 88219820Sjeff break; 89219820Sjeff 90219820Sjeff case MOD_SHUTDOWN: 91219820Sjeff break; 92219820Sjeff 93219820Sjeff default: 94219820Sjeff error = EOPNOTSUPP; 95219820Sjeff break; 96219820Sjeff } 97219820Sjeff 98219820Sjeff return (error); 99219820Sjeff} 100219820Sjeff 101219820SjeffLIVE_ENTROPY_SRC_MODULE(live_entropy_source_example, random_example_modevent, 1); 102219820Sjeff