1254784Smarkm/*-
2254784Smarkm * Copyright (c) 2013 Arthur Mesh <arthurmesh@gmail.com>
3254784Smarkm * All rights reserved.
4254784Smarkm *
5254784Smarkm * Redistribution and use in source and binary forms, with or without
6254784Smarkm * modification, are permitted provided that the following conditions
7254784Smarkm * are met:
8254784Smarkm * 1. Redistributions of source code must retain the above copyright
9254784Smarkm *    notice, this list of conditions and the following disclaimer
10254784Smarkm *    in this position and unchanged.
11254784Smarkm * 2. Redistributions in binary form must reproduce the above copyright
12254784Smarkm *    notice, this list of conditions and the following disclaimer in the
13254784Smarkm *    documentation and/or other materials provided with the distribution.
14254784Smarkm *
15254784Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16254784Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17254784Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18254784Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19254784Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20254784Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21254784Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22254784Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23254784Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24254784Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25254784Smarkm *
26254784Smarkm */
27254784Smarkm
28254784Smarkm#include <sys/cdefs.h>
29254784Smarkm__FBSDID("$FreeBSD: releng/10.3/share/examples/kld/random_adaptor/random_adaptor_example.c 256381 2013-10-12 15:31:36Z markm $");
30254784Smarkm
31254784Smarkm#include <sys/param.h>
32254784Smarkm#include <sys/kernel.h>
33256381Smarkm#include <sys/lock.h>
34254784Smarkm#include <sys/module.h>
35256381Smarkm#include <sys/random.h>
36254784Smarkm#include <sys/systm.h>
37254784Smarkm
38256381Smarkm#include <dev/random/live_entropy_sources.h>
39254784Smarkm#include <dev/random/random_adaptors.h>
40254784Smarkm#include <dev/random/randomdev.h>
41254784Smarkm
42254784Smarkmstatic int random_example_read(void *, int);
43254784Smarkm
44254784Smarkmstruct random_adaptor random_example = {
45254784Smarkm	.ident = "Example RNG",
46256381Smarkm	.source = RANDOM_PURE_BOGUS,	/* Make sure this is in
47256381Smarkm					 * sys/random.h and is unique */
48254784Smarkm	.read = random_example_read,
49254784Smarkm};
50254784Smarkm
51254784Smarkm/*
52254784Smarkm * Used under the license provided @ http://xkcd.com/221/
53254784Smarkm * http://creativecommons.org/licenses/by-nc/2.5/
54254784Smarkm */
55256381Smarkmstatic uint8_t
56254784SmarkmgetRandomNumber(void)
57254784Smarkm{
58254784Smarkm	return 4;   /* chosen by fair dice roll, guaranteed to be random */
59254784Smarkm}
60254784Smarkm
61254784Smarkmstatic int
62254784Smarkmrandom_example_read(void *buf, int c)
63254784Smarkm{
64256381Smarkm	uint8_t *b;
65254784Smarkm	int count;
66254784Smarkm
67254784Smarkm	b = buf;
68254784Smarkm
69256381Smarkm	for (count = 0; count < c; count++)
70254784Smarkm		b[count] = getRandomNumber();
71254784Smarkm
72254784Smarkm	printf("returning %d bytes of pure randomness\n", c);
73254784Smarkm	return (c);
74254784Smarkm}
75254784Smarkm
76254784Smarkmstatic int
77254784Smarkmrandom_example_modevent(module_t mod, int type, void *unused)
78254784Smarkm{
79256381Smarkm	int error = 0;
80254784Smarkm
81254784Smarkm	switch (type) {
82254784Smarkm	case MOD_LOAD:
83256381Smarkm		live_entropy_source_register(&random_example);
84256381Smarkm		break;
85256381Smarkm
86256381Smarkm	case MOD_UNLOAD:
87256381Smarkm		live_entropy_source_deregister(&random_example);
88256381Smarkm		break;
89256381Smarkm
90256381Smarkm	case MOD_SHUTDOWN:
91256381Smarkm		break;
92256381Smarkm
93256381Smarkm	default:
94256381Smarkm		error = EOPNOTSUPP;
95256381Smarkm		break;
96254784Smarkm	}
97254784Smarkm
98256381Smarkm	return (error);
99254784Smarkm}
100254784Smarkm
101256381SmarkmLIVE_ENTROPY_SRC_MODULE(live_entropy_source_example, random_example_modevent, 1);
102