14190f1a0SAlex Hornung /*
24190f1a0SAlex Hornung * Copyright (c) 2011 Alex Hornung <alex@alexhornung.com>.
34190f1a0SAlex Hornung * All rights reserved.
44190f1a0SAlex Hornung *
54190f1a0SAlex Hornung * Redistribution and use in source and binary forms, with or without
64190f1a0SAlex Hornung * modification, are permitted provided that the following conditions
74190f1a0SAlex Hornung * are met:
84190f1a0SAlex Hornung *
94190f1a0SAlex Hornung * 1. Redistributions of source code must retain the above copyright
104190f1a0SAlex Hornung * notice, this list of conditions and the following disclaimer.
114190f1a0SAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright
124190f1a0SAlex Hornung * notice, this list of conditions and the following disclaimer in
134190f1a0SAlex Hornung * the documentation and/or other materials provided with the
144190f1a0SAlex Hornung * distribution.
154190f1a0SAlex Hornung *
164190f1a0SAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174190f1a0SAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184190f1a0SAlex Hornung * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
194190f1a0SAlex Hornung * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
204190f1a0SAlex Hornung * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
214190f1a0SAlex Hornung * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
224190f1a0SAlex Hornung * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
234190f1a0SAlex Hornung * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
244190f1a0SAlex Hornung * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
254190f1a0SAlex Hornung * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
264190f1a0SAlex Hornung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274190f1a0SAlex Hornung * SUCH DAMAGE.
284190f1a0SAlex Hornung */
294190f1a0SAlex Hornung #include <sys/param.h>
304190f1a0SAlex Hornung #include <sys/systm.h>
314190f1a0SAlex Hornung #include <sys/kernel.h>
324190f1a0SAlex Hornung #include <sys/module.h>
334190f1a0SAlex Hornung #include <sys/malloc.h>
344190f1a0SAlex Hornung #include <sys/libkern.h>
354190f1a0SAlex Hornung #include <sys/random.h>
364190f1a0SAlex Hornung
374190f1a0SAlex Hornung #include <dev/crypto/padlock/padlock.h>
384190f1a0SAlex Hornung
391f11d371SMatthew Dillon int padlock_rng(uint8_t *out, long limit);
404190f1a0SAlex Hornung
411f11d371SMatthew Dillon #define PADLOCK_RNDBYTES 32
424190f1a0SAlex Hornung
434190f1a0SAlex Hornung static void
padlock_rng_harvest(void * arg)444190f1a0SAlex Hornung padlock_rng_harvest(void *arg)
454190f1a0SAlex Hornung {
464190f1a0SAlex Hornung struct padlock_softc *sc = arg;
471f11d371SMatthew Dillon uint8_t randomness[PADLOCK_RNDBYTES + 32];
4824177c33SAlex Hornung uint8_t *arandomness; /* randomness aligned */
491f11d371SMatthew Dillon int cnt;
504190f1a0SAlex Hornung
514190f1a0SAlex Hornung arandomness = PADLOCK_ALIGN(randomness);
521f11d371SMatthew Dillon cnt = padlock_rng(arandomness, PADLOCK_RNDBYTES);
534190f1a0SAlex Hornung
54*790110cdSAlex Hornung add_buffer_randomness_src(arandomness, cnt, RAND_SRC_PADLOCK);
554190f1a0SAlex Hornung
564190f1a0SAlex Hornung callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks,
574190f1a0SAlex Hornung padlock_rng_harvest, sc);
584190f1a0SAlex Hornung }
594190f1a0SAlex Hornung
604190f1a0SAlex Hornung void
padlock_rng_init(struct padlock_softc * sc)614190f1a0SAlex Hornung padlock_rng_init(struct padlock_softc *sc)
624190f1a0SAlex Hornung {
634190f1a0SAlex Hornung if (hz > 100)
644190f1a0SAlex Hornung sc->sc_rng_ticks = hz/100;
654190f1a0SAlex Hornung else
664190f1a0SAlex Hornung sc->sc_rng_ticks = 1;
674190f1a0SAlex Hornung
684190f1a0SAlex Hornung callout_init_mp(&sc->sc_rng_co);
694190f1a0SAlex Hornung callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks,
704190f1a0SAlex Hornung padlock_rng_harvest, sc);
714190f1a0SAlex Hornung }
724190f1a0SAlex Hornung
734190f1a0SAlex Hornung void
padlock_rng_uninit(struct padlock_softc * sc)744190f1a0SAlex Hornung padlock_rng_uninit(struct padlock_softc *sc)
754190f1a0SAlex Hornung {
764190f1a0SAlex Hornung callout_stop(&sc->sc_rng_co);
774190f1a0SAlex Hornung }
784190f1a0SAlex Hornung
79