Home | History | Annotate | Line # | Download | only in kern
subr_cprng.c revision 1.5.2.3
      1  1.5.2.3      jdc /*	$NetBSD: subr_cprng.c,v 1.5.2.3 2012/05/21 16:49:54 jdc Exp $ */
      2      1.1      tls 
      3      1.1      tls /*-
      4      1.1      tls  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5      1.1      tls  * All rights reserved.
      6      1.1      tls  *
      7      1.1      tls  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1      tls  * by Thor Lancelot Simon.
      9      1.1      tls  *
     10      1.1      tls  * Redistribution and use in source and binary forms, with or without
     11      1.1      tls  * modification, are permitted provided that the following conditions
     12      1.1      tls  * are met:
     13      1.1      tls  * 1. Redistributions of source code must retain the above copyright
     14      1.1      tls  *    notice, this list of conditions and the following disclaimer.
     15      1.1      tls  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1      tls  *    notice, this list of conditions and the following disclaimer in the
     17      1.1      tls  *    documentation and/or other materials provided with the distribution.
     18      1.1      tls  *
     19      1.1      tls  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1      tls  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1      tls  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1      tls  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1      tls  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1      tls  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1      tls  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1      tls  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1      tls  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1      tls  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1      tls  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1      tls  */
     31      1.1      tls 
     32      1.1      tls #include <sys/types.h>
     33      1.1      tls #include <sys/time.h>
     34      1.1      tls #include <sys/param.h>
     35      1.1      tls #include <sys/kernel.h>
     36      1.1      tls #include <sys/systm.h>
     37      1.1      tls #include <sys/kmem.h>
     38      1.1      tls #include <sys/mutex.h>
     39      1.1      tls #include <sys/rngtest.h>
     40      1.1      tls #include <sys/rnd.h>
     41      1.3      tls #include <dev/rnd_private.h>
     42      1.1      tls 
     43      1.2  tsutsui #if defined(__HAVE_CPU_COUNTER)
     44      1.1      tls #include <machine/cpu_counter.h>
     45      1.2  tsutsui #endif
     46      1.1      tls 
     47      1.1      tls #include <sys/cprng.h>
     48      1.1      tls 
     49  1.5.2.3      jdc __KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.5.2.3 2012/05/21 16:49:54 jdc Exp $");
     50      1.1      tls 
     51      1.1      tls void
     52      1.1      tls cprng_init(void)
     53      1.1      tls {
     54      1.1      tls 	nist_ctr_initialize();
     55      1.1      tls }
     56      1.1      tls 
     57      1.1      tls static inline uint32_t
     58      1.1      tls cprng_counter(void)
     59      1.1      tls {
     60      1.1      tls 	struct timeval tv;
     61      1.1      tls 
     62      1.1      tls #if defined(__HAVE_CPU_COUNTER)
     63      1.1      tls 	if (cpu_hascounter())
     64      1.1      tls 		return cpu_counter32();
     65      1.1      tls #endif
     66      1.1      tls 	if (__predict_false(cold)) {
     67      1.1      tls 		/* microtime unsafe if clock not running yet */
     68      1.1      tls 		return 0;
     69      1.1      tls 	}
     70      1.1      tls 	microtime(&tv);
     71      1.1      tls 	return (tv.tv_sec * 1000000 + tv.tv_usec);
     72      1.1      tls }
     73      1.1      tls 
     74      1.1      tls static void
     75  1.5.2.2      riz cprng_strong_doreseed(cprng_strong_t *const c)
     76  1.5.2.2      riz {
     77  1.5.2.2      riz 	uint32_t cc = cprng_counter();
     78  1.5.2.2      riz 
     79  1.5.2.2      riz 	KASSERT(mutex_owned(&c->mtx));
     80  1.5.2.2      riz 	KASSERT(mutex_owned(&c->reseed.mtx));
     81  1.5.2.2      riz 	KASSERT(c->reseed.len == NIST_BLOCK_KEYLEN_BYTES);
     82  1.5.2.2      riz 
     83  1.5.2.2      riz 	if (nist_ctr_drbg_reseed(&c->drbg, c->reseed.data, c->reseed.len,
     84  1.5.2.2      riz 				 &cc, sizeof(cc))) {
     85  1.5.2.2      riz 		panic("cprng %s: nist_ctr_drbg_reseed failed.", c->name);
     86  1.5.2.2      riz 	}
     87  1.5.2.2      riz #ifdef RND_VERBOSE
     88  1.5.2.2      riz 	printf("cprng %s: reseeded with rnd_filled = %d\n", c->name,
     89  1.5.2.2      riz 							    rnd_filled);
     90  1.5.2.2      riz #endif
     91  1.5.2.2      riz 	c->entropy_serial = rnd_filled;
     92  1.5.2.2      riz 	c->reseed.state = RSTATE_IDLE;
     93  1.5.2.2      riz 	if (c->flags & CPRNG_USE_CV) {
     94  1.5.2.2      riz 		cv_broadcast(&c->cv);
     95  1.5.2.2      riz 	}
     96  1.5.2.2      riz 	selnotify(&c->selq, 0, 0);
     97  1.5.2.2      riz }
     98  1.5.2.2      riz 
     99  1.5.2.2      riz static void
    100      1.5      tls cprng_strong_sched_reseed(cprng_strong_t *const c)
    101      1.5      tls {
    102      1.5      tls 	KASSERT(mutex_owned(&c->mtx));
    103  1.5.2.2      riz 	if (mutex_tryenter(&c->reseed.mtx)) {
    104  1.5.2.2      riz 		switch (c->reseed.state) {
    105  1.5.2.2      riz 		    case RSTATE_IDLE:
    106  1.5.2.2      riz 			c->reseed.state = RSTATE_PENDING;
    107  1.5.2.2      riz 			c->reseed.len = NIST_BLOCK_KEYLEN_BYTES;
    108  1.5.2.2      riz 			rndsink_attach(&c->reseed);
    109  1.5.2.2      riz 			break;
    110  1.5.2.2      riz 		    case RSTATE_HASBITS:
    111  1.5.2.2      riz 			/* Just rekey the underlying generator now. */
    112  1.5.2.2      riz 			cprng_strong_doreseed(c);
    113  1.5.2.2      riz 			break;
    114  1.5.2.2      riz 		    case RSTATE_PENDING:
    115  1.5.2.2      riz 			if (c->entropy_serial != rnd_filled) {
    116  1.5.2.2      riz 				rndsink_detach(&c->reseed);
    117  1.5.2.2      riz 				rndsink_attach(&c->reseed);
    118  1.5.2.2      riz 			}
    119  1.5.2.2      riz 			break;
    120  1.5.2.2      riz 		    default:
    121  1.5.2.2      riz 			panic("cprng %s: bad reseed state %d",
    122  1.5.2.2      riz 			      c->name, c->reseed.state);
    123  1.5.2.2      riz 			break;
    124  1.5.2.2      riz 		}
    125  1.5.2.1      riz 		mutex_spin_exit(&c->reseed.mtx);
    126      1.5      tls 	}
    127  1.5.2.2      riz #ifdef RND_VERBOSE
    128  1.5.2.2      riz 	else {
    129  1.5.2.2      riz 		printf("cprng %s: skipping sched_reseed, sink busy\n",
    130  1.5.2.2      riz 		       c->name);
    131  1.5.2.2      riz 	}
    132  1.5.2.2      riz #endif
    133      1.5      tls }
    134      1.5      tls 
    135      1.5      tls static void
    136      1.1      tls cprng_strong_reseed(void *const arg)
    137      1.1      tls {
    138      1.1      tls 	cprng_strong_t *c = arg;
    139  1.5.2.2      riz 
    140  1.5.2.2      riz 	KASSERT(mutex_owned(&c->reseed.mtx));
    141  1.5.2.2      riz 	KASSERT(RSTATE_HASBITS == c->reseed.state);
    142      1.1      tls 
    143  1.5.2.1      riz 	if (!mutex_tryenter(&c->mtx)) {
    144  1.5.2.2      riz #ifdef RND_VERBOSE
    145  1.5.2.2      riz 	    printf("cprng: sink %s cprng busy, no reseed\n", c->reseed.name);
    146  1.5.2.2      riz #endif
    147  1.5.2.3      jdc 	    if (c->flags & CPRNG_USE_CV) {	/* XXX if flags change? */
    148  1.5.2.3      jdc         	cv_broadcast(&c->cv);
    149  1.5.2.3      jdc 	    }
    150  1.5.2.1      riz 	    return;
    151  1.5.2.1      riz 	}
    152  1.5.2.1      riz 
    153  1.5.2.2      riz 	cprng_strong_doreseed(c);
    154      1.1      tls 	mutex_exit(&c->mtx);
    155      1.1      tls }
    156      1.1      tls 
    157      1.1      tls cprng_strong_t *
    158      1.1      tls cprng_strong_create(const char *const name, int ipl, int flags)
    159      1.1      tls {
    160      1.1      tls 	cprng_strong_t *c;
    161      1.1      tls 	uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
    162      1.5      tls 	int r, getmore = 0, hard = 0;
    163      1.1      tls 	uint32_t cc;
    164      1.1      tls 
    165      1.1      tls 	c = kmem_alloc(sizeof(*c), KM_NOSLEEP);
    166      1.1      tls 	if (c == NULL) {
    167      1.1      tls 		return NULL;
    168      1.1      tls 	}
    169      1.1      tls 	c->flags = flags;
    170      1.1      tls 	strlcpy(c->name, name, sizeof(c->name));
    171  1.5.2.2      riz 	c->reseed.state = RSTATE_IDLE;
    172      1.1      tls 	c->reseed.cb = cprng_strong_reseed;
    173      1.1      tls 	c->reseed.arg = c;
    174  1.5.2.2      riz 	c->entropy_serial = rnd_filled;
    175  1.5.2.1      riz 	mutex_init(&c->reseed.mtx, MUTEX_DEFAULT, IPL_VM);
    176      1.1      tls 	strlcpy(c->reseed.name, name, sizeof(c->reseed.name));
    177      1.1      tls 
    178      1.1      tls 	mutex_init(&c->mtx, MUTEX_DEFAULT, ipl);
    179      1.1      tls 
    180      1.1      tls 	if (c->flags & CPRNG_USE_CV) {
    181      1.1      tls 		cv_init(&c->cv, name);
    182      1.1      tls 	}
    183      1.1      tls 
    184      1.5      tls 	selinit(&c->selq);
    185      1.5      tls 
    186      1.1      tls 	r = rnd_extract_data(key, sizeof(key), RND_EXTRACT_GOOD);
    187      1.1      tls 	if (r != sizeof(key)) {
    188      1.1      tls 		if (c->flags & CPRNG_INIT_ANY) {
    189      1.5      tls #ifdef DEBUG
    190      1.1      tls 			printf("cprng %s: WARNING insufficient "
    191      1.1      tls 			       "entropy at creation.\n", name);
    192      1.5      tls #endif
    193      1.1      tls 			rnd_extract_data(key + r, sizeof(key - r),
    194      1.1      tls 					 RND_EXTRACT_ANY);
    195      1.1      tls 		} else {
    196      1.5      tls 			hard++;
    197      1.1      tls 		}
    198      1.1      tls 		getmore++;
    199      1.1      tls 	}
    200      1.1      tls 
    201      1.1      tls 	if (nist_ctr_drbg_instantiate(&c->drbg, key, sizeof(key),
    202      1.1      tls 				      &cc, sizeof(cc), name, strlen(name))) {
    203      1.1      tls 		panic("cprng %s: instantiation failed.", name);
    204      1.1      tls 	}
    205      1.1      tls 
    206      1.1      tls 	if (getmore) {
    207      1.5      tls 		/* Cause readers to wait for rekeying. */
    208      1.5      tls 		if (hard) {
    209      1.5      tls 			c->drbg.reseed_counter =
    210      1.5      tls 			    NIST_CTR_DRBG_RESEED_INTERVAL + 1;
    211      1.5      tls 		} else {
    212      1.5      tls 			c->drbg.reseed_counter =
    213      1.5      tls 			    (NIST_CTR_DRBG_RESEED_INTERVAL / 2) + 1;
    214      1.1      tls 		}
    215      1.1      tls 	}
    216      1.1      tls 	return c;
    217      1.1      tls }
    218      1.1      tls 
    219      1.1      tls size_t
    220      1.5      tls cprng_strong(cprng_strong_t *const c, void *const p, size_t len, int flags)
    221      1.1      tls {
    222      1.1      tls 	uint32_t cc = cprng_counter();
    223      1.5      tls #ifdef DEBUG
    224      1.5      tls 	int testfail = 0;
    225      1.5      tls #endif
    226      1.1      tls 	if (len > CPRNG_MAX_LEN) {	/* XXX should we loop? */
    227      1.1      tls 		len = CPRNG_MAX_LEN;	/* let the caller loop if desired */
    228      1.1      tls 	}
    229      1.5      tls 	mutex_enter(&c->mtx);
    230      1.1      tls 
    231      1.1      tls 	if (nist_ctr_drbg_generate(&c->drbg, p, len, &cc, sizeof(cc))) {
    232      1.1      tls 		/* A generator failure really means we hit the hard limit. */
    233      1.1      tls 		if (c->flags & CPRNG_REKEY_ANY) {
    234      1.1      tls 			uint8_t key[NIST_BLOCK_KEYLEN_BYTES];
    235      1.1      tls 
    236      1.1      tls  			printf("cprng %s: WARNING pseudorandom rekeying.\n",
    237      1.1      tls 			       c->name);
    238      1.1      tls                         rnd_extract_data(key, sizeof(key), RND_EXTRACT_ANY);
    239      1.1      tls 			cc = cprng_counter();
    240      1.1      tls 			if (nist_ctr_drbg_reseed(&c->drbg, key, sizeof(key),
    241      1.1      tls 			    &cc, sizeof(cc))) {
    242      1.1      tls 				panic("cprng %s: nist_ctr_drbg_reseed "
    243      1.1      tls 				      "failed.", c->name);
    244      1.1      tls 			}
    245      1.1      tls 		} else {
    246  1.5.2.3      jdc 			int wr;
    247      1.1      tls 
    248  1.5.2.3      jdc 			do {
    249      1.5      tls 				cprng_strong_sched_reseed(c);
    250  1.5.2.3      jdc 				if ((flags & FNONBLOCK) ||
    251  1.5.2.3      jdc 				    !(c->flags & CPRNG_USE_CV)) {
    252  1.5.2.3      jdc 					len = 0;
    253  1.5.2.3      jdc 					break;
    254  1.5.2.3      jdc 				}
    255  1.5.2.3      jdc 			/*
    256  1.5.2.3      jdc 			 * XXX There's a race with the cv_broadcast
    257  1.5.2.3      jdc 			 * XXX in cprng_strong_sched_reseed, because
    258  1.5.2.3      jdc 			 * XXX of the use of tryenter in that function.
    259  1.5.2.3      jdc 			 * XXX This "timedwait" hack works around it,
    260  1.5.2.3      jdc 			 * XXX at the expense of occasionaly polling
    261  1.5.2.3      jdc 			 * XXX for success on a /dev/random rekey.
    262  1.5.2.3      jdc 			 */
    263  1.5.2.3      jdc 				wr = cv_timedwait_sig(&c->cv, &c->mtx,
    264  1.5.2.3      jdc 						      mstohz(100));
    265  1.5.2.3      jdc 				if (wr == ERESTART) {
    266  1.5.2.3      jdc 					mutex_exit(&c->mtx);
    267  1.5.2.3      jdc 					return 0;
    268  1.5.2.3      jdc 				}
    269  1.5.2.3      jdc 			} while (nist_ctr_drbg_generate(&c->drbg, p,
    270  1.5.2.3      jdc 							len, &cc,
    271  1.5.2.3      jdc 							sizeof(cc)));
    272      1.1      tls 		}
    273      1.1      tls 	}
    274      1.1      tls 
    275      1.5      tls #ifdef DEBUG
    276      1.1      tls 	/*
    277      1.1      tls 	 * If the generator has just been keyed, perform
    278      1.1      tls 	 * the statistical RNG test.
    279      1.1      tls 	 */
    280      1.1      tls 	if (__predict_false(c->drbg.reseed_counter == 1)) {
    281      1.5      tls 		rngtest_t *rt = kmem_alloc(sizeof(*rt), KM_NOSLEEP);
    282      1.1      tls 
    283      1.5      tls 		if (rt) {
    284      1.1      tls 
    285      1.5      tls 			strncpy(rt->rt_name, c->name, sizeof(rt->rt_name));
    286      1.5      tls 
    287      1.5      tls 			if (nist_ctr_drbg_generate(&c->drbg, rt->rt_b,
    288      1.5      tls 		  	    sizeof(rt->rt_b), NULL, 0)) {
    289      1.5      tls 				panic("cprng %s: nist_ctr_drbg_generate "
    290      1.5      tls 				      "failed!", c->name);
    291      1.1      tls 
    292      1.5      tls 			}
    293      1.5      tls 			testfail = rngtest(rt);
    294      1.5      tls 
    295      1.5      tls 			if (testfail) {
    296      1.5      tls 				printf("cprng %s: failed statistical RNG "
    297      1.5      tls 				       "test.\n", c->name);
    298      1.5      tls 				c->drbg.reseed_counter =
    299      1.5      tls 				    NIST_CTR_DRBG_RESEED_INTERVAL + 1;
    300      1.5      tls 				len = 0;
    301      1.5      tls 			}
    302      1.5      tls 			memset(rt, 0, sizeof(*rt));
    303      1.5      tls 			kmem_free(rt, sizeof(*rt));
    304      1.1      tls 		}
    305      1.1      tls 	}
    306      1.1      tls #endif
    307      1.1      tls 	if (__predict_false(c->drbg.reseed_counter >
    308      1.5      tls 			     (NIST_CTR_DRBG_RESEED_INTERVAL / 2))) {
    309      1.5      tls 		cprng_strong_sched_reseed(c);
    310  1.5.2.2      riz 	} else if (rnd_full) {
    311  1.5.2.2      riz 		if (c->entropy_serial != rnd_filled) {
    312  1.5.2.2      riz #ifdef RND_VERBOSE
    313  1.5.2.2      riz 			printf("cprng %s: reseeding from full pool "
    314  1.5.2.2      riz 			       "(serial %d vs pool %d)\n", c->name,
    315  1.5.2.2      riz 			       c->entropy_serial, rnd_filled);
    316  1.5.2.2      riz #endif
    317      1.5      tls 			cprng_strong_sched_reseed(c);
    318      1.1      tls 		}
    319      1.1      tls 	}
    320      1.1      tls 
    321      1.1      tls 	mutex_exit(&c->mtx);
    322      1.1      tls 	return len;
    323      1.1      tls }
    324      1.1      tls 
    325      1.1      tls void
    326      1.1      tls cprng_strong_destroy(cprng_strong_t *c)
    327      1.1      tls {
    328      1.5      tls 	mutex_enter(&c->mtx);
    329  1.5.2.1      riz 	mutex_spin_enter(&c->reseed.mtx);
    330      1.5      tls 
    331      1.1      tls 	if (c->flags & CPRNG_USE_CV) {
    332      1.1      tls 		KASSERT(!cv_has_waiters(&c->cv));
    333      1.1      tls 		cv_destroy(&c->cv);
    334      1.1      tls 	}
    335      1.5      tls 	seldestroy(&c->selq);
    336      1.1      tls 
    337  1.5.2.2      riz 	if (RSTATE_PENDING == c->reseed.state) {
    338      1.1      tls 		rndsink_detach(&c->reseed);
    339      1.1      tls 	}
    340  1.5.2.1      riz 	mutex_spin_exit(&c->reseed.mtx);
    341  1.5.2.1      riz 	mutex_destroy(&c->reseed.mtx);
    342  1.5.2.1      riz 
    343      1.1      tls 	nist_ctr_drbg_destroy(&c->drbg);
    344      1.5      tls 
    345      1.5      tls 	mutex_exit(&c->mtx);
    346      1.5      tls 	mutex_destroy(&c->mtx);
    347      1.5      tls 
    348      1.1      tls 	memset(c, 0, sizeof(*c));
    349      1.1      tls 	kmem_free(c, sizeof(*c));
    350      1.1      tls }
    351      1.1      tls 
    352      1.1      tls int
    353      1.1      tls cprng_strong_getflags(cprng_strong_t *const c)
    354      1.1      tls {
    355      1.1      tls 	KASSERT(mutex_owned(&c->mtx));
    356      1.1      tls 	return c->flags;
    357      1.1      tls }
    358      1.1      tls 
    359      1.1      tls void
    360      1.1      tls cprng_strong_setflags(cprng_strong_t *const c, int flags)
    361      1.1      tls {
    362      1.1      tls 	KASSERT(mutex_owned(&c->mtx));
    363      1.1      tls 	if (flags & CPRNG_USE_CV) {
    364      1.1      tls 		if (!(c->flags & CPRNG_USE_CV)) {
    365      1.1      tls 			cv_init(&c->cv, (const char *)c->name);
    366      1.1      tls 		}
    367      1.1      tls 	} else {
    368      1.1      tls 		if (c->flags & CPRNG_USE_CV) {
    369      1.1      tls 			KASSERT(!cv_has_waiters(&c->cv));
    370      1.1      tls 			cv_destroy(&c->cv);
    371      1.1      tls 		}
    372      1.1      tls 	}
    373      1.1      tls 	if (flags & CPRNG_REKEY_ANY) {
    374      1.1      tls 		if (!(c->flags & CPRNG_REKEY_ANY)) {
    375      1.1      tls 			if (c->flags & CPRNG_USE_CV) {
    376      1.1      tls 				cv_broadcast(&c->cv);
    377      1.1      tls 			}
    378      1.5      tls 			selnotify(&c->selq, 0, 0);
    379      1.1      tls 		}
    380      1.1      tls 	}
    381      1.1      tls 	c->flags = flags;
    382      1.1      tls }
    383