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> 3751a4a9a0SMatthew Dillon #include <sys/sysctl.h> 3883c37607SAlex Hornung 3983c37607SAlex Hornung #include <machine/specialreg.h> 4083c37607SAlex Hornung 4183c37607SAlex Hornung #define RDRAND_ALIGN(p) (void *)(roundup2((uintptr_t)(p), 16)) 4251a4a9a0SMatthew Dillon #define RDRAND_SIZE 512 4383c37607SAlex Hornung 4451a4a9a0SMatthew Dillon static int rdrand_debug; 4551a4a9a0SMatthew Dillon SYSCTL_INT(_debug, OID_AUTO, rdrand, CTLFLAG_RW, &rdrand_debug, 0, 4651a4a9a0SMatthew Dillon "Enable rdrand debugging"); 4783c37607SAlex Hornung 4883c37607SAlex Hornung struct rdrand_softc { 4983c37607SAlex Hornung struct callout sc_rng_co; 5083c37607SAlex Hornung int32_t sc_rng_ticks; 5183c37607SAlex Hornung }; 5283c37607SAlex Hornung 5383c37607SAlex Hornung 5483c37607SAlex Hornung static void rdrand_rng_harvest(void *); 5551a4a9a0SMatthew Dillon int rdrand_rng(uint8_t *out, long limit); 5683c37607SAlex Hornung 5783c37607SAlex Hornung 5883c37607SAlex Hornung static void 5983c37607SAlex Hornung rdrand_identify(driver_t *drv, device_t parent) 6083c37607SAlex Hornung { 6183c37607SAlex Hornung 6283c37607SAlex Hornung /* NB: order 10 is so we get attached after h/w devices */ 6383c37607SAlex Hornung if (device_find_child(parent, "rdrand", -1) == NULL && 6483c37607SAlex Hornung BUS_ADD_CHILD(parent, parent, 10, "rdrand", -1) == 0) 6583c37607SAlex Hornung panic("rdrand: could not attach"); 6683c37607SAlex Hornung } 6783c37607SAlex Hornung 6883c37607SAlex Hornung 6983c37607SAlex Hornung static int 7083c37607SAlex Hornung rdrand_probe(device_t dev) 7183c37607SAlex Hornung { 7283c37607SAlex Hornung 7383c37607SAlex Hornung if ((cpu_feature2 & CPUID2_RDRAND) == 0) { 7483c37607SAlex Hornung device_printf(dev, "No RdRand support.\n"); 7583c37607SAlex Hornung return (EINVAL); 7683c37607SAlex Hornung } 7783c37607SAlex Hornung 7883c37607SAlex Hornung device_set_desc(dev, "RdRand RNG"); 7983c37607SAlex Hornung return 0; 8083c37607SAlex Hornung } 8183c37607SAlex Hornung 8283c37607SAlex Hornung 8383c37607SAlex Hornung static int 8483c37607SAlex Hornung rdrand_attach(device_t dev) 8583c37607SAlex Hornung { 8683c37607SAlex Hornung struct rdrand_softc *sc; 8783c37607SAlex Hornung 8883c37607SAlex Hornung sc = device_get_softc(dev); 8983c37607SAlex Hornung 9051a4a9a0SMatthew Dillon if (hz > 10) 9151a4a9a0SMatthew Dillon sc->sc_rng_ticks = hz / 10; 9283c37607SAlex Hornung else 9383c37607SAlex Hornung sc->sc_rng_ticks = 1; 9483c37607SAlex Hornung 9583c37607SAlex Hornung callout_init_mp(&sc->sc_rng_co); 9683c37607SAlex Hornung callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks, 9783c37607SAlex Hornung rdrand_rng_harvest, sc); 9883c37607SAlex Hornung 9983c37607SAlex Hornung return 0; 10083c37607SAlex Hornung } 10183c37607SAlex Hornung 10283c37607SAlex Hornung 10383c37607SAlex Hornung static int 10483c37607SAlex Hornung rdrand_detach(device_t dev) 10583c37607SAlex Hornung { 10683c37607SAlex Hornung struct rdrand_softc *sc; 10783c37607SAlex Hornung 10883c37607SAlex Hornung sc = device_get_softc(dev); 10983c37607SAlex Hornung 11083c37607SAlex Hornung callout_stop_sync(&sc->sc_rng_co); 11183c37607SAlex Hornung 11283c37607SAlex Hornung return (0); 11383c37607SAlex Hornung } 11483c37607SAlex Hornung 11583c37607SAlex Hornung 11683c37607SAlex Hornung static void 11783c37607SAlex Hornung rdrand_rng_harvest(void *arg) 11883c37607SAlex Hornung { 11983c37607SAlex Hornung struct rdrand_softc *sc = arg; 12051a4a9a0SMatthew Dillon uint8_t randomness[RDRAND_SIZE + 32]; 12183c37607SAlex Hornung uint8_t *arandomness; /* randomness aligned */ 12251a4a9a0SMatthew Dillon int cnt; 12383c37607SAlex Hornung 12483c37607SAlex Hornung arandomness = RDRAND_ALIGN(randomness); 12583c37607SAlex Hornung 126*af8b80e4SMatthew Dillon cnt = rdrand_rng(arandomness, RDRAND_SIZE); 12751a4a9a0SMatthew Dillon if (cnt > 0 && cnt < sizeof(randomness)) { 12851a4a9a0SMatthew Dillon add_buffer_randomness(arandomness, cnt); 12951a4a9a0SMatthew Dillon 13051a4a9a0SMatthew Dillon if (rdrand_debug) { 13151a4a9a0SMatthew Dillon kprintf("rdrand(%d): %02x %02x %02x %02x...\n", 13251a4a9a0SMatthew Dillon cnt, 13351a4a9a0SMatthew Dillon arandomness[0], 13451a4a9a0SMatthew Dillon arandomness[1], 13551a4a9a0SMatthew Dillon arandomness[2], 13651a4a9a0SMatthew Dillon arandomness[3]); 13751a4a9a0SMatthew Dillon } 13851a4a9a0SMatthew Dillon } 13983c37607SAlex Hornung 14083c37607SAlex Hornung callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks, 14183c37607SAlex Hornung rdrand_rng_harvest, sc); 14283c37607SAlex Hornung } 14383c37607SAlex Hornung 14483c37607SAlex Hornung 14583c37607SAlex Hornung static device_method_t rdrand_methods[] = { 14683c37607SAlex Hornung DEVMETHOD(device_identify, rdrand_identify), 14783c37607SAlex Hornung DEVMETHOD(device_probe, rdrand_probe), 14883c37607SAlex Hornung DEVMETHOD(device_attach, rdrand_attach), 14983c37607SAlex Hornung DEVMETHOD(device_detach, rdrand_detach), 15083c37607SAlex Hornung 151d3c9c58eSSascha Wildner DEVMETHOD_END 15283c37607SAlex Hornung }; 15383c37607SAlex Hornung 15483c37607SAlex Hornung 15583c37607SAlex Hornung static driver_t rdrand_driver = { 15683c37607SAlex Hornung "rdrand", 15783c37607SAlex Hornung rdrand_methods, 15883c37607SAlex Hornung sizeof(struct rdrand_softc), 15983c37607SAlex Hornung }; 16083c37607SAlex Hornung 16183c37607SAlex Hornung static devclass_t rdrand_devclass; 16283c37607SAlex Hornung 16383c37607SAlex Hornung DRIVER_MODULE(rdrand, nexus, rdrand_driver, rdrand_devclass, NULL, NULL); 16483c37607SAlex Hornung MODULE_VERSION(rdrand, 1); 16583c37607SAlex Hornung MODULE_DEPEND(rdrand, crypto, 1, 1, 1); 166