xref: /dflybsd-src/sys/dev/crypto/rdrand/rdrand.c (revision 1cb34a03363bd4f58f6d6756fdb48f44c09d23a4)
183c37607SAlex Hornung /*
283c37607SAlex Hornung  * Copyright (c) 2012 Alex Hornung <alex@alexhornung.com>.
383c37607SAlex Hornung  * All rights reserved.
483c37607SAlex Hornung  *
583c37607SAlex Hornung  * Redistribution and use in source and binary forms, with or without
683c37607SAlex Hornung  * modification, are permitted provided that the following conditions
783c37607SAlex Hornung  * are met:
883c37607SAlex Hornung  *
983c37607SAlex Hornung  * 1. Redistributions of source code must retain the above copyright
1083c37607SAlex Hornung  *    notice, this list of conditions and the following disclaimer.
1183c37607SAlex Hornung  * 2. Redistributions in binary form must reproduce the above copyright
1283c37607SAlex Hornung  *    notice, this list of conditions and the following disclaimer in
1383c37607SAlex Hornung  *    the documentation and/or other materials provided with the
1483c37607SAlex Hornung  *    distribution.
1583c37607SAlex Hornung  *
1683c37607SAlex Hornung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1783c37607SAlex Hornung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1883c37607SAlex Hornung  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1983c37607SAlex Hornung  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
2083c37607SAlex Hornung  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2183c37607SAlex Hornung  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2283c37607SAlex Hornung  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2383c37607SAlex Hornung  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2483c37607SAlex Hornung  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2583c37607SAlex Hornung  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2683c37607SAlex Hornung  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2783c37607SAlex Hornung  * SUCH DAMAGE.
2883c37607SAlex Hornung  */
2983c37607SAlex Hornung #include <sys/param.h>
3083c37607SAlex Hornung #include <sys/systm.h>
3183c37607SAlex Hornung #include <sys/kernel.h>
3283c37607SAlex Hornung #include <sys/kobj.h>
3383c37607SAlex Hornung #include <sys/libkern.h>
3483c37607SAlex Hornung #include <sys/module.h>
3583c37607SAlex Hornung #include <sys/bus.h>
3683c37607SAlex Hornung #include <sys/random.h>
37*1cb34a03SMatthew Dillon #include <sys/malloc.h>
3851a4a9a0SMatthew Dillon #include <sys/sysctl.h>
3983c37607SAlex Hornung 
4083c37607SAlex Hornung #include <machine/specialreg.h>
4183c37607SAlex Hornung 
4283c37607SAlex Hornung #define	RDRAND_ALIGN(p)	(void *)(roundup2((uintptr_t)(p), 16))
4351a4a9a0SMatthew Dillon #define RDRAND_SIZE	512
4483c37607SAlex Hornung 
4551a4a9a0SMatthew Dillon static int rdrand_debug;
4651a4a9a0SMatthew Dillon SYSCTL_INT(_debug, OID_AUTO, rdrand, CTLFLAG_RW, &rdrand_debug, 0,
4751a4a9a0SMatthew Dillon 	   "Enable rdrand debugging");
4883c37607SAlex Hornung 
4983c37607SAlex Hornung struct rdrand_softc {
50*1cb34a03SMatthew Dillon 	struct callout	*sc_rng_co;
5183c37607SAlex Hornung 	int32_t		sc_rng_ticks;
5283c37607SAlex Hornung };
5383c37607SAlex Hornung 
5483c37607SAlex Hornung 
5583c37607SAlex Hornung static void rdrand_rng_harvest(void *);
5651a4a9a0SMatthew Dillon int rdrand_rng(uint8_t *out, long limit);
5783c37607SAlex Hornung 
5883c37607SAlex Hornung 
5983c37607SAlex Hornung static void
6083c37607SAlex Hornung rdrand_identify(driver_t *drv, device_t parent)
6183c37607SAlex Hornung {
6283c37607SAlex Hornung 
6383c37607SAlex Hornung 	/* NB: order 10 is so we get attached after h/w devices */
6483c37607SAlex Hornung 	if (device_find_child(parent, "rdrand", -1) == NULL &&
6583c37607SAlex Hornung 	    BUS_ADD_CHILD(parent, parent, 10, "rdrand", -1) == 0)
6683c37607SAlex Hornung 		panic("rdrand: could not attach");
6783c37607SAlex Hornung }
6883c37607SAlex Hornung 
6983c37607SAlex Hornung 
7083c37607SAlex Hornung static int
7183c37607SAlex Hornung rdrand_probe(device_t dev)
7283c37607SAlex Hornung {
7383c37607SAlex Hornung 
7483c37607SAlex Hornung 	if ((cpu_feature2 & CPUID2_RDRAND) == 0) {
7583c37607SAlex Hornung 		device_printf(dev, "No RdRand support.\n");
7683c37607SAlex Hornung 		return (EINVAL);
7783c37607SAlex Hornung 	}
7883c37607SAlex Hornung 
7983c37607SAlex Hornung 	device_set_desc(dev, "RdRand RNG");
8083c37607SAlex Hornung 	return 0;
8183c37607SAlex Hornung }
8283c37607SAlex Hornung 
8383c37607SAlex Hornung 
8483c37607SAlex Hornung static int
8583c37607SAlex Hornung rdrand_attach(device_t dev)
8683c37607SAlex Hornung {
8783c37607SAlex Hornung 	struct rdrand_softc *sc;
88*1cb34a03SMatthew Dillon 	int i;
8983c37607SAlex Hornung 
9083c37607SAlex Hornung 	sc = device_get_softc(dev);
9183c37607SAlex Hornung 
9251a4a9a0SMatthew Dillon 	if (hz > 10)
9351a4a9a0SMatthew Dillon 		sc->sc_rng_ticks = hz / 10;
9483c37607SAlex Hornung 	else
9583c37607SAlex Hornung 		sc->sc_rng_ticks = 1;
9683c37607SAlex Hornung 
97*1cb34a03SMatthew Dillon 	sc->sc_rng_co = kmalloc(ncpus * sizeof(*sc->sc_rng_co),
98*1cb34a03SMatthew Dillon 				M_TEMP, M_WAITOK | M_ZERO);
99*1cb34a03SMatthew Dillon 
100*1cb34a03SMatthew Dillon 	for (i = 0; i < ncpus; ++i) {
101*1cb34a03SMatthew Dillon 		callout_init_mp(&sc->sc_rng_co[i]);
102*1cb34a03SMatthew Dillon 		callout_reset_bycpu(&sc->sc_rng_co[i], sc->sc_rng_ticks,
103*1cb34a03SMatthew Dillon 				    rdrand_rng_harvest, sc, i);
104*1cb34a03SMatthew Dillon 	}
10583c37607SAlex Hornung 
10683c37607SAlex Hornung 	return 0;
10783c37607SAlex Hornung }
10883c37607SAlex Hornung 
10983c37607SAlex Hornung 
11083c37607SAlex Hornung static int
11183c37607SAlex Hornung rdrand_detach(device_t dev)
11283c37607SAlex Hornung {
11383c37607SAlex Hornung 	struct rdrand_softc *sc;
114*1cb34a03SMatthew Dillon 	int i;
11583c37607SAlex Hornung 
11683c37607SAlex Hornung 	sc = device_get_softc(dev);
11783c37607SAlex Hornung 
118*1cb34a03SMatthew Dillon 	for (i = 0; i < ncpus; ++i) {
119*1cb34a03SMatthew Dillon 		callout_terminate(&sc->sc_rng_co[i]);
120*1cb34a03SMatthew Dillon 	}
12183c37607SAlex Hornung 
12283c37607SAlex Hornung 	return (0);
12383c37607SAlex Hornung }
12483c37607SAlex Hornung 
12583c37607SAlex Hornung 
12683c37607SAlex Hornung static void
12783c37607SAlex Hornung rdrand_rng_harvest(void *arg)
12883c37607SAlex Hornung {
12983c37607SAlex Hornung 	struct rdrand_softc *sc = arg;
13051a4a9a0SMatthew Dillon 	uint8_t randomness[RDRAND_SIZE + 32];
13183c37607SAlex Hornung 	uint8_t *arandomness; /* randomness aligned */
13251a4a9a0SMatthew Dillon 	int cnt;
13383c37607SAlex Hornung 
13483c37607SAlex Hornung 	arandomness = RDRAND_ALIGN(randomness);
13583c37607SAlex Hornung 
136af8b80e4SMatthew Dillon 	cnt = rdrand_rng(arandomness, RDRAND_SIZE);
13751a4a9a0SMatthew Dillon 	if (cnt > 0 && cnt < sizeof(randomness)) {
138*1cb34a03SMatthew Dillon 		add_buffer_randomness_src(arandomness, cnt,
139*1cb34a03SMatthew Dillon 					  RAND_SRC_RDRAND |
140*1cb34a03SMatthew Dillon 					  RAND_SRCF_PCPU);
14151a4a9a0SMatthew Dillon 
142*1cb34a03SMatthew Dillon 		if (rdrand_debug > 0) {
143*1cb34a03SMatthew Dillon 			--rdrand_debug;
144*1cb34a03SMatthew Dillon 			kprintf("rdrand(%d,cpu=%d): %02x %02x %02x %02x...\n",
145*1cb34a03SMatthew Dillon 				cnt, mycpu->gd_cpuid,
14651a4a9a0SMatthew Dillon 				arandomness[0],
14751a4a9a0SMatthew Dillon 				arandomness[1],
14851a4a9a0SMatthew Dillon 				arandomness[2],
14951a4a9a0SMatthew Dillon 				arandomness[3]);
15051a4a9a0SMatthew Dillon 		}
15151a4a9a0SMatthew Dillon 	}
15283c37607SAlex Hornung 
153*1cb34a03SMatthew Dillon 	callout_reset(&sc->sc_rng_co[mycpu->gd_cpuid], sc->sc_rng_ticks,
15483c37607SAlex Hornung 		      rdrand_rng_harvest, sc);
15583c37607SAlex Hornung }
15683c37607SAlex Hornung 
15783c37607SAlex Hornung 
15883c37607SAlex Hornung static device_method_t rdrand_methods[] = {
15983c37607SAlex Hornung 	DEVMETHOD(device_identify, rdrand_identify),
16083c37607SAlex Hornung 	DEVMETHOD(device_probe, rdrand_probe),
16183c37607SAlex Hornung 	DEVMETHOD(device_attach, rdrand_attach),
16283c37607SAlex Hornung 	DEVMETHOD(device_detach, rdrand_detach),
16383c37607SAlex Hornung 
164d3c9c58eSSascha Wildner 	DEVMETHOD_END
16583c37607SAlex Hornung };
16683c37607SAlex Hornung 
16783c37607SAlex Hornung 
16883c37607SAlex Hornung static driver_t rdrand_driver = {
16983c37607SAlex Hornung 	"rdrand",
17083c37607SAlex Hornung 	rdrand_methods,
17183c37607SAlex Hornung 	sizeof(struct rdrand_softc),
17283c37607SAlex Hornung };
17383c37607SAlex Hornung 
17483c37607SAlex Hornung static devclass_t rdrand_devclass;
17583c37607SAlex Hornung 
17683c37607SAlex Hornung DRIVER_MODULE(rdrand, nexus, rdrand_driver, rdrand_devclass, NULL, NULL);
17783c37607SAlex Hornung MODULE_VERSION(rdrand, 1);
17883c37607SAlex Hornung MODULE_DEPEND(rdrand, crypto, 1, 1, 1);
179