xref: /onnv-gate/usr/src/uts/common/crypto/io/swrand.c (revision 11413:21d4b1442799)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51920Smcpowers  * Common Development and Distribution License (the "License").
61920Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11413Sopensolaris@drydog.com  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Software based random number provider for the Kernel Cryptographic
280Sstevel@tonic-gate  * Framework (KCF). This provider periodically collects unpredictable input
290Sstevel@tonic-gate  * from external sources and processes it into a pool of entropy (randomness)
300Sstevel@tonic-gate  * in order to satisfy requests for random bits from kCF. It implements
310Sstevel@tonic-gate  * software-based mixing, extraction, and generation algorithms.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * A history note: The software-based algorithms in this file used to be
340Sstevel@tonic-gate  * part of the /dev/random driver.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <vm/seg_kmem.h>
410Sstevel@tonic-gate #include <vm/hat.h>
420Sstevel@tonic-gate #include <sys/systm.h>
430Sstevel@tonic-gate #include <sys/memlist.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate #include <sys/ksynch.h>
460Sstevel@tonic-gate #include <sys/random.h>
470Sstevel@tonic-gate #include <sys/ddi.h>
480Sstevel@tonic-gate #include <sys/mman.h>
490Sstevel@tonic-gate #include <sys/sysmacros.h>
500Sstevel@tonic-gate #include <sys/mem_config.h>
510Sstevel@tonic-gate #include <sys/time.h>
520Sstevel@tonic-gate #include <sys/crypto/spi.h>
530Sstevel@tonic-gate #include <sys/sha1.h>
540Sstevel@tonic-gate #include <sys/sunddi.h>
550Sstevel@tonic-gate #include <sys/modctl.h>
563446Smrj #include <sys/hold_page.h>
578029SHai-May.Chao@Sun.COM #include <rng/fips_random.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	RNDPOOLSIZE		1024	/* Pool size in bytes */
600Sstevel@tonic-gate #define	HASHBUFSIZE		64	/* Buffer size used for pool mixing */
610Sstevel@tonic-gate #define	MAXMEMBLOCKS		16384	/* Number of memory blocks to scan */
620Sstevel@tonic-gate #define	MEMBLOCKSIZE		4096	/* Size of memory block to read */
630Sstevel@tonic-gate #define	MINEXTRACTBITS		160	/* Min entropy level for extraction */
640Sstevel@tonic-gate #define	TIMEOUT_INTERVAL	5	/* Periodic mixing interval in secs */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* Hash-algo generic definitions. For now, they are SHA1's. */
670Sstevel@tonic-gate #define	HASHSIZE		20
680Sstevel@tonic-gate #define	HASH_CTX		SHA1_CTX
690Sstevel@tonic-gate #define	HashInit(ctx)		SHA1Init((ctx))
700Sstevel@tonic-gate #define	HashUpdate(ctx, p, s)	SHA1Update((ctx), (p), (s))
710Sstevel@tonic-gate #define	HashFinal(d, ctx)	SHA1Final((d), (ctx))
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /* Physical memory entropy source */
740Sstevel@tonic-gate typedef struct physmem_entsrc_s {
750Sstevel@tonic-gate 	uint8_t *parity;		/* parity bit vector */
760Sstevel@tonic-gate 	caddr_t pmbuf;			/* buffer for memory block */
770Sstevel@tonic-gate 	uint32_t nblocks;		/* number of  memory blocks */
780Sstevel@tonic-gate 	int entperblock;		/* entropy bits per block read */
790Sstevel@tonic-gate 	hrtime_t last_diff;		/* previous time to process a block */
800Sstevel@tonic-gate 	hrtime_t last_delta;		/* previous time delta */
810Sstevel@tonic-gate 	hrtime_t last_delta2;		/* previous 2nd order time delta */
820Sstevel@tonic-gate } physmem_entsrc_t;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static uint32_t srndpool[RNDPOOLSIZE/4];	/* Pool of random bits */
851920Smcpowers static uint32_t buffer[RNDPOOLSIZE/4];	/* entropy mixed in later */
861920Smcpowers static int buffer_bytes;		/* bytes written to buffer */
870Sstevel@tonic-gate static uint32_t entropy_bits;		/* pool's current amount of entropy */
880Sstevel@tonic-gate static kmutex_t srndpool_lock;		/* protects r/w accesses to the pool, */
890Sstevel@tonic-gate 					/* and the global variables */
901920Smcpowers static kmutex_t buffer_lock;		/* protects r/w accesses to buffer */
910Sstevel@tonic-gate static kcondvar_t srndpool_read_cv;	/* serializes poll/read syscalls */
920Sstevel@tonic-gate static int pindex;			/* Global index for adding/extracting */
930Sstevel@tonic-gate 					/* from the pool */
941920Smcpowers static int bstart, bindex;		/* Global vars for adding/extracting */
951920Smcpowers 					/* from the buffer */
960Sstevel@tonic-gate static uint8_t leftover[HASHSIZE];	/* leftover output */
978029SHai-May.Chao@Sun.COM static uint32_t	swrand_XKEY[6];		/* one extra word for getentropy */
980Sstevel@tonic-gate static int leftover_bytes;		/* leftover length */
998733SHai-May.Chao@Sun.COM static uint32_t previous_bytes[HASHSIZE/BYTES_IN_WORD];	/* prev random bytes */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate static physmem_entsrc_t entsrc;		/* Physical mem as an entropy source */
1020Sstevel@tonic-gate static timeout_id_t rnd_timeout_id;
1030Sstevel@tonic-gate static int snum_waiters;
1040Sstevel@tonic-gate static crypto_kcf_provider_handle_t swrand_prov_handle = NULL;
1050Sstevel@tonic-gate swrand_stats_t swrand_stats;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static int physmem_ent_init(physmem_entsrc_t *);
1080Sstevel@tonic-gate static void physmem_ent_fini(physmem_entsrc_t *);
1090Sstevel@tonic-gate static void physmem_ent_gen(physmem_entsrc_t *);
1100Sstevel@tonic-gate static int physmem_parity_update(uint8_t *, uint32_t, int);
1110Sstevel@tonic-gate static void physmem_count_blocks();
1120Sstevel@tonic-gate static void rnd_dr_callback_post_add(void *, pgcnt_t);
1130Sstevel@tonic-gate static int rnd_dr_callback_pre_del(void *, pgcnt_t);
1140Sstevel@tonic-gate static void rnd_dr_callback_post_del(void *, pgcnt_t, int);
1150Sstevel@tonic-gate static void rnd_handler(void *arg);
1160Sstevel@tonic-gate static void swrand_init();
1170Sstevel@tonic-gate static void swrand_schedule_timeout(void);
1180Sstevel@tonic-gate static int swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t);
1190Sstevel@tonic-gate static void swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est);
1201920Smcpowers static void swrand_add_entropy_later(uint8_t *ptr, size_t len);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* Dynamic Reconfiguration related declarations */
1230Sstevel@tonic-gate kphysm_setup_vector_t rnd_dr_callback_vec = {
1240Sstevel@tonic-gate 	KPHYSM_SETUP_VECTOR_VERSION,
1250Sstevel@tonic-gate 	rnd_dr_callback_post_add,
1260Sstevel@tonic-gate 	rnd_dr_callback_pre_del,
1270Sstevel@tonic-gate 	rnd_dr_callback_post_del
1280Sstevel@tonic-gate };
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate extern struct mod_ops mod_cryptoops;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate  * Module linkage information for the kernel.
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
1360Sstevel@tonic-gate 	&mod_cryptoops,
1375072Smcpowers 	"Kernel Random number Provider"
1380Sstevel@tonic-gate };
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static struct modlinkage modlinkage = {
1410Sstevel@tonic-gate 	MODREV_1,
1420Sstevel@tonic-gate 	(void *)&modlcrypto,
1430Sstevel@tonic-gate 	NULL
1440Sstevel@tonic-gate };
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate  * CSPI information (entry points, provider info, etc.)
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate static void swrand_provider_status(crypto_provider_handle_t, uint_t *);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static crypto_control_ops_t swrand_control_ops = {
1520Sstevel@tonic-gate 	swrand_provider_status
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate static int swrand_seed_random(crypto_provider_handle_t, crypto_session_id_t,
1561920Smcpowers     uchar_t *, size_t, uint_t, uint32_t, crypto_req_handle_t);
1570Sstevel@tonic-gate static int swrand_generate_random(crypto_provider_handle_t,
1580Sstevel@tonic-gate     crypto_session_id_t, uchar_t *, size_t, crypto_req_handle_t);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate static crypto_random_number_ops_t swrand_random_number_ops = {
1610Sstevel@tonic-gate 	swrand_seed_random,
1620Sstevel@tonic-gate 	swrand_generate_random
1630Sstevel@tonic-gate };
1640Sstevel@tonic-gate 
16510732SAnthony.Scarpino@Sun.COM static void swrand_POST(int *);
16610732SAnthony.Scarpino@Sun.COM 
16710732SAnthony.Scarpino@Sun.COM static crypto_fips140_ops_t swrand_fips140_ops = {
16810732SAnthony.Scarpino@Sun.COM 	swrand_POST
16910732SAnthony.Scarpino@Sun.COM };
17010732SAnthony.Scarpino@Sun.COM 
1710Sstevel@tonic-gate static crypto_ops_t swrand_crypto_ops = {
1720Sstevel@tonic-gate 	&swrand_control_ops,
1730Sstevel@tonic-gate 	NULL,
1740Sstevel@tonic-gate 	NULL,
1750Sstevel@tonic-gate 	NULL,
1760Sstevel@tonic-gate 	NULL,
1770Sstevel@tonic-gate 	NULL,
1780Sstevel@tonic-gate 	NULL,
1790Sstevel@tonic-gate 	NULL,
1800Sstevel@tonic-gate 	&swrand_random_number_ops,
1810Sstevel@tonic-gate 	NULL,
1820Sstevel@tonic-gate 	NULL,
1830Sstevel@tonic-gate 	NULL,
1840Sstevel@tonic-gate 	NULL,
18510732SAnthony.Scarpino@Sun.COM 	NULL,
18610732SAnthony.Scarpino@Sun.COM 	NULL,
18710732SAnthony.Scarpino@Sun.COM 	NULL,
18810732SAnthony.Scarpino@Sun.COM 	&swrand_fips140_ops
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate static crypto_provider_info_t swrand_prov_info = {
19210732SAnthony.Scarpino@Sun.COM 	CRYPTO_SPI_VERSION_4,
1930Sstevel@tonic-gate 	"Kernel Random Number Provider",
1940Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
1950Sstevel@tonic-gate 	{&modlinkage},
1960Sstevel@tonic-gate 	NULL,
1970Sstevel@tonic-gate 	&swrand_crypto_ops,
1980Sstevel@tonic-gate 	0,
1990Sstevel@tonic-gate 	NULL
2000Sstevel@tonic-gate };
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate int
2030Sstevel@tonic-gate _init(void)
2040Sstevel@tonic-gate {
2050Sstevel@tonic-gate 	int ret;
2060Sstevel@tonic-gate 	hrtime_t ts;
2070Sstevel@tonic-gate 	time_t now;
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * Register with KCF. If the registration fails, return error.
2110Sstevel@tonic-gate 	 */
2120Sstevel@tonic-gate 	if ((ret = crypto_register_provider(&swrand_prov_info,
2130Sstevel@tonic-gate 	    &swrand_prov_handle)) != CRYPTO_SUCCESS) {
2140Sstevel@tonic-gate 		cmn_err(CE_WARN, "swrand : Kernel Random Number Provider "
2150Sstevel@tonic-gate 		    "disabled for /dev/random use");
2160Sstevel@tonic-gate 		return (EACCES);
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	mutex_init(&srndpool_lock, NULL, MUTEX_DEFAULT, NULL);
2201920Smcpowers 	mutex_init(&buffer_lock, NULL, MUTEX_DEFAULT, NULL);
2210Sstevel@tonic-gate 	cv_init(&srndpool_read_cv, NULL, CV_DEFAULT, NULL);
2220Sstevel@tonic-gate 	entropy_bits = 0;
2230Sstevel@tonic-gate 	pindex = 0;
2241920Smcpowers 	bindex = 0;
2251920Smcpowers 	bstart = 0;
2260Sstevel@tonic-gate 	snum_waiters = 0;
2270Sstevel@tonic-gate 	leftover_bytes = 0;
2281920Smcpowers 	buffer_bytes = 0;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * Initialize the pool using
2320Sstevel@tonic-gate 	 * . 2 unpredictable times: high resolution time since the boot-time,
2330Sstevel@tonic-gate 	 *   and the current time-of-the day.
2340Sstevel@tonic-gate 	 * . The initial physical memory state.
2350Sstevel@tonic-gate 	 */
2360Sstevel@tonic-gate 	ts = gethrtime();
2370Sstevel@tonic-gate 	swrand_add_entropy((uint8_t *)&ts, sizeof (ts), 0);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	(void) drv_getparm(TIME, &now);
2400Sstevel@tonic-gate 	swrand_add_entropy((uint8_t *)&now, sizeof (now), 0);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	ret = kphysm_setup_func_register(&rnd_dr_callback_vec, NULL);
2430Sstevel@tonic-gate 	ASSERT(ret == 0);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (physmem_ent_init(&entsrc) != 0) {
2460Sstevel@tonic-gate 		mutex_destroy(&srndpool_lock);
2471920Smcpowers 		mutex_destroy(&buffer_lock);
2480Sstevel@tonic-gate 		cv_destroy(&srndpool_read_cv);
2490Sstevel@tonic-gate 		(void) crypto_unregister_provider(swrand_prov_handle);
2500Sstevel@tonic-gate 		return (ENOMEM);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	if ((ret = mod_install(&modlinkage)) != 0) {
2540Sstevel@tonic-gate 		mutex_destroy(&srndpool_lock);
2551920Smcpowers 		mutex_destroy(&buffer_lock);
2560Sstevel@tonic-gate 		cv_destroy(&srndpool_read_cv);
2570Sstevel@tonic-gate 		physmem_ent_fini(&entsrc);
2580Sstevel@tonic-gate 		(void) crypto_unregister_provider(swrand_prov_handle);
2590Sstevel@tonic-gate 		return (ret);
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	/* Schedule periodic mixing of the pool. */
2630Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
2640Sstevel@tonic-gate 	swrand_schedule_timeout();
2650Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
2668029SHai-May.Chao@Sun.COM 	(void) swrand_get_entropy((uint8_t *)swrand_XKEY, HASHSIZE, B_TRUE);
2678029SHai-May.Chao@Sun.COM 	bcopy(swrand_XKEY, previous_bytes, HASHSIZE);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	return (0);
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate int
2730Sstevel@tonic-gate _info(struct modinfo *modinfop)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Control entry points.
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate /* ARGSUSED */
2820Sstevel@tonic-gate static void
2830Sstevel@tonic-gate swrand_provider_status(crypto_provider_handle_t provider, uint_t *status)
2840Sstevel@tonic-gate {
2850Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * Random number entry points.
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate /* ARGSUSED */
2920Sstevel@tonic-gate static int
2930Sstevel@tonic-gate swrand_seed_random(crypto_provider_handle_t provider, crypto_session_id_t sid,
2941920Smcpowers     uchar_t *buf, size_t len, uint_t entropy_est, uint32_t flags,
2951920Smcpowers     crypto_req_handle_t req)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	/* The entropy estimate is always 0 in this path */
2981920Smcpowers 	if (flags & CRYPTO_SEED_NOW)
2991920Smcpowers 		swrand_add_entropy(buf, len, 0);
3001920Smcpowers 	else
3011920Smcpowers 		swrand_add_entropy_later(buf, len);
3020Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /* ARGSUSED */
3060Sstevel@tonic-gate static int
3070Sstevel@tonic-gate swrand_generate_random(crypto_provider_handle_t provider,
3080Sstevel@tonic-gate     crypto_session_id_t sid, uchar_t *buf, size_t len, crypto_req_handle_t req)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	if (crypto_kmflag(req) == KM_NOSLEEP)
3110Sstevel@tonic-gate 		(void) swrand_get_entropy(buf, len, B_TRUE);
3120Sstevel@tonic-gate 	else
3130Sstevel@tonic-gate 		(void) swrand_get_entropy(buf, len, B_FALSE);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate /*
3190Sstevel@tonic-gate  * Extraction of entropy from the pool.
3200Sstevel@tonic-gate  *
3210Sstevel@tonic-gate  * Returns "len" random bytes in *ptr.
3220Sstevel@tonic-gate  * Try to gather some more entropy by calling physmem_ent_gen() when less than
3230Sstevel@tonic-gate  * MINEXTRACTBITS are present in the pool.
3240Sstevel@tonic-gate  * Will block if not enough entropy was available and the call is blocking.
3250Sstevel@tonic-gate  */
3260Sstevel@tonic-gate static int
3270Sstevel@tonic-gate swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t nonblock)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	int i, bytes;
3300Sstevel@tonic-gate 	HASH_CTX hashctx;
3310Sstevel@tonic-gate 	uint8_t digest[HASHSIZE], *pool;
3328733SHai-May.Chao@Sun.COM 	uint32_t tempout[HASHSIZE/BYTES_IN_WORD];
3338029SHai-May.Chao@Sun.COM 	int size;
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
3360Sstevel@tonic-gate 	if (leftover_bytes > 0) {
3370Sstevel@tonic-gate 		bytes = min(len, leftover_bytes);
3380Sstevel@tonic-gate 		bcopy(leftover, ptr, bytes);
3390Sstevel@tonic-gate 		len -= bytes;
3400Sstevel@tonic-gate 		ptr += bytes;
3410Sstevel@tonic-gate 		leftover_bytes -= bytes;
3420Sstevel@tonic-gate 		if (leftover_bytes > 0)
3430Sstevel@tonic-gate 			ovbcopy(leftover+bytes, leftover, leftover_bytes);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	while (len > 0) {
3470Sstevel@tonic-gate 		/* Check if there is enough entropy */
3480Sstevel@tonic-gate 		while (entropy_bits < MINEXTRACTBITS) {
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 			physmem_ent_gen(&entsrc);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 			if (entropy_bits < MINEXTRACTBITS &&
3530Sstevel@tonic-gate 			    nonblock == B_TRUE) {
3540Sstevel@tonic-gate 				mutex_exit(&srndpool_lock);
3550Sstevel@tonic-gate 				return (EAGAIN);
3560Sstevel@tonic-gate 			}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 			if (entropy_bits < MINEXTRACTBITS) {
3590Sstevel@tonic-gate 				ASSERT(nonblock == B_FALSE);
3600Sstevel@tonic-gate 				snum_waiters++;
3610Sstevel@tonic-gate 				if (cv_wait_sig(&srndpool_read_cv,
3620Sstevel@tonic-gate 				    &srndpool_lock) == 0) {
3630Sstevel@tonic-gate 					snum_waiters--;
3640Sstevel@tonic-gate 					mutex_exit(&srndpool_lock);
3650Sstevel@tonic-gate 					return (EINTR);
3660Sstevel@tonic-gate 				}
3670Sstevel@tonic-gate 				snum_waiters--;
3680Sstevel@tonic-gate 			}
3690Sstevel@tonic-gate 		}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		/* Figure out how many bytes to extract */
3720Sstevel@tonic-gate 		bytes = min(HASHSIZE, len);
373*11413Sopensolaris@drydog.com 		bytes = min(bytes, CRYPTO_BITS2BYTES(entropy_bits));
374*11413Sopensolaris@drydog.com 		entropy_bits -= CRYPTO_BYTES2BITS(bytes);
375*11413Sopensolaris@drydog.com 		BUMP_SWRAND_STATS(ss_entOut, CRYPTO_BYTES2BITS(bytes));
3760Sstevel@tonic-gate 		swrand_stats.ss_entEst = entropy_bits;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		/* Extract entropy by hashing pool content */
3790Sstevel@tonic-gate 		HashInit(&hashctx);
3800Sstevel@tonic-gate 		HashUpdate(&hashctx, (uint8_t *)srndpool, RNDPOOLSIZE);
3810Sstevel@tonic-gate 		HashFinal(digest, &hashctx);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		/*
3840Sstevel@tonic-gate 		 * Feed the digest back into the pool so next
3850Sstevel@tonic-gate 		 * extraction produces different result
3860Sstevel@tonic-gate 		 */
3870Sstevel@tonic-gate 		pool = (uint8_t *)srndpool;
3880Sstevel@tonic-gate 		for (i = 0; i < HASHSIZE; i++) {
3890Sstevel@tonic-gate 			pool[pindex++] ^= digest[i];
3900Sstevel@tonic-gate 			/* pindex modulo RNDPOOLSIZE */
3910Sstevel@tonic-gate 			pindex &= (RNDPOOLSIZE - 1);
3920Sstevel@tonic-gate 		}
3930Sstevel@tonic-gate 
3948029SHai-May.Chao@Sun.COM 		/* LINTED E_BAD_PTR_CAST_ALIGN */
3958029SHai-May.Chao@Sun.COM 		fips_random_inner(swrand_XKEY, tempout, (uint32_t *)digest);
3968029SHai-May.Chao@Sun.COM 
3978029SHai-May.Chao@Sun.COM 		if (len >= HASHSIZE) {
3988029SHai-May.Chao@Sun.COM 			size = HASHSIZE;
3998029SHai-May.Chao@Sun.COM 		} else {
4008029SHai-May.Chao@Sun.COM 			size = min(bytes, HASHSIZE);
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 
4038029SHai-May.Chao@Sun.COM 		/*
4048029SHai-May.Chao@Sun.COM 		 * FIPS 140-2: Continuous RNG test - each generation
4058029SHai-May.Chao@Sun.COM 		 * of an n-bit block shall be compared with the previously
4068029SHai-May.Chao@Sun.COM 		 * generated block. Test shall fail if any two compared
4078029SHai-May.Chao@Sun.COM 		 * n-bit blocks are equal.
4088029SHai-May.Chao@Sun.COM 		 */
4098733SHai-May.Chao@Sun.COM 		for (i = 0; i < HASHSIZE/BYTES_IN_WORD; i++) {
4108029SHai-May.Chao@Sun.COM 			if (tempout[i] != previous_bytes[i])
4118029SHai-May.Chao@Sun.COM 				break;
4128029SHai-May.Chao@Sun.COM 		}
41310500SHai-May.Chao@Sun.COM 
41410500SHai-May.Chao@Sun.COM 		if (i == HASHSIZE/BYTES_IN_WORD) {
4158029SHai-May.Chao@Sun.COM 			cmn_err(CE_WARN, "swrand: The value of 160-bit block "
4168029SHai-May.Chao@Sun.COM 			    "random bytes are same as the previous one.\n");
41710500SHai-May.Chao@Sun.COM 			/* discard random bytes and return error */
41810500SHai-May.Chao@Sun.COM 			return (EIO);
41910500SHai-May.Chao@Sun.COM 		}
4208029SHai-May.Chao@Sun.COM 
4218029SHai-May.Chao@Sun.COM 		bcopy(tempout, previous_bytes, HASHSIZE);
4228029SHai-May.Chao@Sun.COM 
4238029SHai-May.Chao@Sun.COM 		bcopy(tempout, ptr, size);
4248029SHai-May.Chao@Sun.COM 		if (len < HASHSIZE) {
4258029SHai-May.Chao@Sun.COM 			leftover_bytes = HASHSIZE - bytes;
4269294SHai-May.Chao@Sun.COM 			bcopy((uint8_t *)tempout + bytes, leftover,
4279294SHai-May.Chao@Sun.COM 			    leftover_bytes);
4288029SHai-May.Chao@Sun.COM 		}
4298029SHai-May.Chao@Sun.COM 
4308029SHai-May.Chao@Sun.COM 		ptr += size;
4318029SHai-May.Chao@Sun.COM 		len -= size;
4328029SHai-May.Chao@Sun.COM 		BUMP_SWRAND_STATS(ss_bytesOut, size);
4330Sstevel@tonic-gate 	}
4348029SHai-May.Chao@Sun.COM 
4358029SHai-May.Chao@Sun.COM 	/* Zero out sensitive information */
4368029SHai-May.Chao@Sun.COM 	bzero(digest, HASHSIZE);
4378029SHai-May.Chao@Sun.COM 	bzero(tempout, HASHSIZE);
4380Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
4390Sstevel@tonic-gate 	return (0);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4421920Smcpowers #define	SWRAND_ADD_BYTES(ptr, len, i, pool)		\
4431920Smcpowers 	ASSERT((ptr) != NULL && (len) > 0);		\
4441920Smcpowers 	BUMP_SWRAND_STATS(ss_bytesIn, (len));		\
4451920Smcpowers 	while ((len)--) {				\
4461920Smcpowers 		(pool)[(i)++] ^= *(ptr);		\
4471920Smcpowers 		(ptr)++;				\
4481920Smcpowers 		(i) &= (RNDPOOLSIZE - 1);		\
4491920Smcpowers 	}
4501920Smcpowers 
4510Sstevel@tonic-gate /* Write some more user-provided entropy to the pool */
4520Sstevel@tonic-gate static void
4530Sstevel@tonic-gate swrand_add_bytes(uint8_t *ptr, size_t len)
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	uint8_t *pool = (uint8_t *)srndpool;
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&srndpool_lock));
4581920Smcpowers 	SWRAND_ADD_BYTES(ptr, len, pindex, pool);
4591920Smcpowers }
4600Sstevel@tonic-gate 
4611920Smcpowers /*
4621920Smcpowers  * Add bytes to buffer. Adding the buffer to the random pool
4631920Smcpowers  * is deferred until the random pool is mixed.
4641920Smcpowers  */
4651920Smcpowers static void
4661920Smcpowers swrand_add_bytes_later(uint8_t *ptr, size_t len)
4671920Smcpowers {
4681920Smcpowers 	uint8_t *pool = (uint8_t *)buffer;
4691920Smcpowers 
4701920Smcpowers 	ASSERT(MUTEX_HELD(&buffer_lock));
4711920Smcpowers 	SWRAND_ADD_BYTES(ptr, len, bindex, pool);
4721920Smcpowers 	buffer_bytes += len;
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4751920Smcpowers #undef SWRAND_ADD_BYTES
4761920Smcpowers 
4770Sstevel@tonic-gate /* Mix the pool */
4780Sstevel@tonic-gate static void
4790Sstevel@tonic-gate swrand_mix_pool(uint16_t entropy_est)
4800Sstevel@tonic-gate {
4810Sstevel@tonic-gate 	int i, j, k, start;
4820Sstevel@tonic-gate 	HASH_CTX hashctx;
4830Sstevel@tonic-gate 	uint8_t digest[HASHSIZE];
4840Sstevel@tonic-gate 	uint8_t *pool = (uint8_t *)srndpool;
4851920Smcpowers 	uint8_t *bp = (uint8_t *)buffer;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&srndpool_lock));
4880Sstevel@tonic-gate 
4891920Smcpowers 	/* add deferred bytes */
4901920Smcpowers 	mutex_enter(&buffer_lock);
4911920Smcpowers 	if (buffer_bytes > 0) {
4921920Smcpowers 		if (buffer_bytes >= RNDPOOLSIZE) {
4931920Smcpowers 			for (i = 0; i < RNDPOOLSIZE/4; i++) {
4941920Smcpowers 				srndpool[i] ^= buffer[i];
4951920Smcpowers 				buffer[i] = 0;
4961920Smcpowers 			}
4971920Smcpowers 			bstart = bindex = 0;
4981920Smcpowers 		} else {
4991920Smcpowers 			for (i = 0; i < buffer_bytes; i++) {
5001920Smcpowers 				pool[pindex++] ^= bp[bstart];
5011920Smcpowers 				bp[bstart++] = 0;
5021920Smcpowers 				pindex &= (RNDPOOLSIZE - 1);
5031920Smcpowers 				bstart &= (RNDPOOLSIZE - 1);
5041920Smcpowers 			}
5051920Smcpowers 			ASSERT(bstart == bindex);
5061920Smcpowers 		}
5071920Smcpowers 		buffer_bytes = 0;
5081920Smcpowers 	}
5091920Smcpowers 	mutex_exit(&buffer_lock);
5101920Smcpowers 
5110Sstevel@tonic-gate 	start = 0;
5120Sstevel@tonic-gate 	for (i = 0; i < RNDPOOLSIZE/HASHSIZE + 1; i++) {
5130Sstevel@tonic-gate 		HashInit(&hashctx);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 		/* Hash a buffer centered on a block in the pool */
5160Sstevel@tonic-gate 		if (start + HASHBUFSIZE <= RNDPOOLSIZE)
5170Sstevel@tonic-gate 			HashUpdate(&hashctx, &pool[start], HASHBUFSIZE);
5180Sstevel@tonic-gate 		else {
5190Sstevel@tonic-gate 			HashUpdate(&hashctx, &pool[start],
5200Sstevel@tonic-gate 			    RNDPOOLSIZE - start);
5210Sstevel@tonic-gate 			HashUpdate(&hashctx, pool,
5220Sstevel@tonic-gate 			    HASHBUFSIZE - RNDPOOLSIZE + start);
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 		HashFinal(digest, &hashctx);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 		/* XOR the hash result back into the block */
5270Sstevel@tonic-gate 		k = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
5280Sstevel@tonic-gate 		for (j = 0; j < HASHSIZE; j++) {
5290Sstevel@tonic-gate 			pool[k++] ^= digest[j];
5300Sstevel@tonic-gate 			k &= (RNDPOOLSIZE - 1);
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 		/* Slide the hash buffer and repeat with next block */
5340Sstevel@tonic-gate 		start = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	entropy_bits += entropy_est;
538*11413Sopensolaris@drydog.com 	if (entropy_bits > CRYPTO_BYTES2BITS(RNDPOOLSIZE))
539*11413Sopensolaris@drydog.com 		entropy_bits = CRYPTO_BYTES2BITS(RNDPOOLSIZE);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	swrand_stats.ss_entEst = entropy_bits;
5420Sstevel@tonic-gate 	BUMP_SWRAND_STATS(ss_entIn, entropy_est);
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate static void
5461920Smcpowers swrand_add_entropy_later(uint8_t *ptr, size_t len)
5471920Smcpowers {
5481920Smcpowers 	mutex_enter(&buffer_lock);
5491920Smcpowers 	swrand_add_bytes_later(ptr, len);
5501920Smcpowers 	mutex_exit(&buffer_lock);
5511920Smcpowers }
5521920Smcpowers 
5531920Smcpowers static void
5540Sstevel@tonic-gate swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
5570Sstevel@tonic-gate 	swrand_add_bytes(ptr, len);
5580Sstevel@tonic-gate 	swrand_mix_pool(entropy_est);
5590Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate  * The physmem_* routines below generate entropy by reading blocks of
5640Sstevel@tonic-gate  * physical memory.  Entropy is gathered in a couple of ways:
5650Sstevel@tonic-gate  *
5660Sstevel@tonic-gate  *  - By reading blocks of physical memory and detecting if changes
5670Sstevel@tonic-gate  *    occurred in the blocks read.
5680Sstevel@tonic-gate  *
5690Sstevel@tonic-gate  *  - By measuring the time it takes to load and hash a block of memory
5700Sstevel@tonic-gate  *    and computing the differences in the measured time.
5710Sstevel@tonic-gate  *
5720Sstevel@tonic-gate  * The first method was used in the CryptoRand implementation.  Physical
5730Sstevel@tonic-gate  * memory is divided into blocks of fixed size.  A block of memory is
5740Sstevel@tonic-gate  * chosen from the possible blocks and hashed to produce a digest.  This
5750Sstevel@tonic-gate  * digest is then mixed into the pool.  A single bit from the digest is
5760Sstevel@tonic-gate  * used as a parity bit or "checksum" and compared against the previous
5770Sstevel@tonic-gate  * "checksum" computed for the block.  If the single-bit checksum has not
5780Sstevel@tonic-gate  * changed, no entropy is credited to the pool.  If there is a change,
5790Sstevel@tonic-gate  * then the assumption is that at least one bit in the block has changed.
5800Sstevel@tonic-gate  * The possible locations within the memory block of where the bit change
5810Sstevel@tonic-gate  * occurred is used as a measure of entropy.  For example, if a block
5820Sstevel@tonic-gate  * size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of
5830Sstevel@tonic-gate  * entropy is available.  Because the single-bit checksum will miss half
5840Sstevel@tonic-gate  * of the changes, the amount of entropy credited to the pool is doubled
5850Sstevel@tonic-gate  * when a change is detected.  With a 4096 byte block size, a block
5860Sstevel@tonic-gate  * change will add a total of 30 bits of entropy to the pool.
5870Sstevel@tonic-gate  *
5880Sstevel@tonic-gate  * The second method measures the amount of time it takes to read and
5890Sstevel@tonic-gate  * hash a physical memory block (as described above).  The time measured
5900Sstevel@tonic-gate  * can vary depending on system load, scheduling and other factors.
5910Sstevel@tonic-gate  * Differences between consecutive measurements are computed to come up
5920Sstevel@tonic-gate  * with an entropy estimate.  The first, second, and third order delta is
5930Sstevel@tonic-gate  * calculated to determine the minimum delta value.  The number of bits
5940Sstevel@tonic-gate  * present in this minimum delta value is the entropy estimate.  This
5950Sstevel@tonic-gate  * entropy estimation technique using time deltas is similar to that used
5960Sstevel@tonic-gate  * in /dev/random implementations from Linux/BSD.
5970Sstevel@tonic-gate  */
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate static int
6000Sstevel@tonic-gate physmem_ent_init(physmem_entsrc_t *entsrc)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	uint8_t *ptr;
6030Sstevel@tonic-gate 	int i;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	bzero(entsrc, sizeof (*entsrc));
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/*
6080Sstevel@tonic-gate 	 * The maximum entropy amount in bits per block of memory read is
6090Sstevel@tonic-gate 	 * log_2(MEMBLOCKSIZE * 8);
6100Sstevel@tonic-gate 	 */
611*11413Sopensolaris@drydog.com 	i = CRYPTO_BYTES2BITS(MEMBLOCKSIZE);
6120Sstevel@tonic-gate 	while (i >>= 1)
6130Sstevel@tonic-gate 		entsrc->entperblock++;
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* Initialize entsrc->nblocks */
6160Sstevel@tonic-gate 	physmem_count_blocks();
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	if (entsrc->nblocks == 0) {
6190Sstevel@tonic-gate 		cmn_err(CE_WARN, "no memory blocks to scan!");
6200Sstevel@tonic-gate 		return (-1);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/* Allocate space for the parity vector and memory page */
6240Sstevel@tonic-gate 	entsrc->parity = kmem_alloc(howmany(entsrc->nblocks, 8),
6250Sstevel@tonic-gate 	    KM_SLEEP);
6260Sstevel@tonic-gate 	entsrc->pmbuf = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	/* Initialize parity vector with bits from the pool */
6300Sstevel@tonic-gate 	i = howmany(entsrc->nblocks, 8);
6310Sstevel@tonic-gate 	ptr = entsrc->parity;
6320Sstevel@tonic-gate 	while (i > 0) {
6330Sstevel@tonic-gate 		if (i > RNDPOOLSIZE) {
6340Sstevel@tonic-gate 			bcopy(srndpool, ptr, RNDPOOLSIZE);
6350Sstevel@tonic-gate 			mutex_enter(&srndpool_lock);
6360Sstevel@tonic-gate 			swrand_mix_pool(0);
6370Sstevel@tonic-gate 			mutex_exit(&srndpool_lock);
6380Sstevel@tonic-gate 			ptr += RNDPOOLSIZE;
6390Sstevel@tonic-gate 			i -= RNDPOOLSIZE;
6400Sstevel@tonic-gate 		} else {
6410Sstevel@tonic-gate 			bcopy(srndpool, ptr, i);
6420Sstevel@tonic-gate 			break;
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	/* Generate some entropy to further initialize the pool */
6470Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
6480Sstevel@tonic-gate 	physmem_ent_gen(entsrc);
6490Sstevel@tonic-gate 	entropy_bits = 0;
6500Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	return (0);
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate static void
6560Sstevel@tonic-gate physmem_ent_fini(physmem_entsrc_t *entsrc)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	if (entsrc->pmbuf != NULL)
6590Sstevel@tonic-gate 		vmem_free(heap_arena, entsrc->pmbuf, PAGESIZE);
6600Sstevel@tonic-gate 	if (entsrc->parity != NULL)
6610Sstevel@tonic-gate 		kmem_free(entsrc->parity, howmany(entsrc->nblocks, 8));
6620Sstevel@tonic-gate 	bzero(entsrc, sizeof (*entsrc));
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate static void
6660Sstevel@tonic-gate physmem_ent_gen(physmem_entsrc_t *entsrc)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	struct memlist *pmem;
6690Sstevel@tonic-gate 	offset_t offset, poffset;
6700Sstevel@tonic-gate 	pfn_t pfn;
6710Sstevel@tonic-gate 	int i, nbytes, len, ent = 0;
6720Sstevel@tonic-gate 	uint32_t block, oblock;
6730Sstevel@tonic-gate 	hrtime_t ts1, ts2, diff, delta, delta2, delta3;
6740Sstevel@tonic-gate 	uint8_t digest[HASHSIZE];
6750Sstevel@tonic-gate 	HASH_CTX ctx;
6763446Smrj 	page_t *pp;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/*
6790Sstevel@tonic-gate 	 * Use each 32-bit quantity in the pool to pick a memory
6800Sstevel@tonic-gate 	 * block to read.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	for (i = 0; i < RNDPOOLSIZE/4; i++) {
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 		/* If the pool is "full", stop after one block */
685*11413Sopensolaris@drydog.com 		if (entropy_bits + ent >= CRYPTO_BYTES2BITS(RNDPOOLSIZE)) {
6860Sstevel@tonic-gate 			if (i > 0)
6870Sstevel@tonic-gate 				break;
6880Sstevel@tonic-gate 		}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 		/*
6910Sstevel@tonic-gate 		 * This lock protects reading of phys_install.
6920Sstevel@tonic-gate 		 * Any changes to this list, by DR, are done while
6930Sstevel@tonic-gate 		 * holding this lock. So, holding this lock is sufficient
6940Sstevel@tonic-gate 		 * to handle DR also.
6950Sstevel@tonic-gate 		 */
6960Sstevel@tonic-gate 		memlist_read_lock();
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 		/* We're left with less than 4K of memory after DR */
6990Sstevel@tonic-gate 		ASSERT(entsrc->nblocks > 0);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 		/* Pick a memory block to read */
7020Sstevel@tonic-gate 		block = oblock = srndpool[i] % entsrc->nblocks;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 		for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
7050Sstevel@tonic-gate 			if (block < pmem->size / MEMBLOCKSIZE)
7060Sstevel@tonic-gate 				break;
7070Sstevel@tonic-gate 			block -= pmem->size / MEMBLOCKSIZE;
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		ASSERT(pmem != NULL);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		offset = pmem->address + block * MEMBLOCKSIZE;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		if (!address_in_memlist(phys_install, offset, MEMBLOCKSIZE)) {
7150Sstevel@tonic-gate 			memlist_read_unlock();
7160Sstevel@tonic-gate 			continue;
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		/*
7203446Smrj 		 * Do an initial check to see if the address is safe
7213446Smrj 		 */
7223446Smrj 		if (plat_hold_page(offset >> PAGESHIFT, PLAT_HOLD_NO_LOCK, NULL)
7233446Smrj 		    == PLAT_HOLD_FAIL) {
7243446Smrj 			memlist_read_unlock();
7253446Smrj 			continue;
7263446Smrj 		}
7273446Smrj 
7283446Smrj 		/*
7290Sstevel@tonic-gate 		 * Figure out which page to load to read the
7300Sstevel@tonic-gate 		 * memory block.  Load the page and compute the
7310Sstevel@tonic-gate 		 * hash of the memory block.
7320Sstevel@tonic-gate 		 */
7330Sstevel@tonic-gate 		len = MEMBLOCKSIZE;
7340Sstevel@tonic-gate 		ts1 = gethrtime();
7350Sstevel@tonic-gate 		HashInit(&ctx);
7360Sstevel@tonic-gate 		while (len) {
7370Sstevel@tonic-gate 			pfn = offset >> PAGESHIFT;
7380Sstevel@tonic-gate 			poffset = offset & PAGEOFFSET;
7390Sstevel@tonic-gate 			nbytes = PAGESIZE - poffset < len ?
7400Sstevel@tonic-gate 			    PAGESIZE - poffset : len;
7410Sstevel@tonic-gate 
7423446Smrj 			/*
7433446Smrj 			 * Re-check the offset, and lock the frame.  If the
7443446Smrj 			 * page was given away after the above check, we'll
7453446Smrj 			 * just bail out.
7463446Smrj 			 */
7473446Smrj 			if (plat_hold_page(pfn, PLAT_HOLD_LOCK, &pp) ==
7483446Smrj 			    PLAT_HOLD_FAIL)
7493446Smrj 				break;
7503446Smrj 
7510Sstevel@tonic-gate 			hat_devload(kas.a_hat, entsrc->pmbuf,
7520Sstevel@tonic-gate 			    PAGESIZE, pfn, PROT_READ,
7530Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 			HashUpdate(&ctx, (uint8_t *)entsrc->pmbuf + poffset,
7560Sstevel@tonic-gate 			    nbytes);
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 			hat_unload(kas.a_hat, entsrc->pmbuf, PAGESIZE,
7590Sstevel@tonic-gate 			    HAT_UNLOAD_UNLOCK);
7600Sstevel@tonic-gate 
7613446Smrj 			plat_release_page(pp);
7623446Smrj 
7630Sstevel@tonic-gate 			len -= nbytes;
7640Sstevel@tonic-gate 			offset += nbytes;
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 		/* We got our pages. Let the DR roll */
7670Sstevel@tonic-gate 		memlist_read_unlock();
7680Sstevel@tonic-gate 
7693446Smrj 		/* See if we had to bail out due to a page being given away */
7703446Smrj 		if (len)
7713446Smrj 			continue;
7723446Smrj 
7730Sstevel@tonic-gate 		HashFinal(digest, &ctx);
7740Sstevel@tonic-gate 		ts2 = gethrtime();
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 		/*
7770Sstevel@tonic-gate 		 * Compute the time it took to load and hash the
7780Sstevel@tonic-gate 		 * block and compare it against the previous
7790Sstevel@tonic-gate 		 * measurement. The delta of the time values
7800Sstevel@tonic-gate 		 * provides a small amount of entropy.  The
7810Sstevel@tonic-gate 		 * minimum of the first, second, and third order
7820Sstevel@tonic-gate 		 * delta is used to estimate how much entropy
7830Sstevel@tonic-gate 		 * is present.
7840Sstevel@tonic-gate 		 */
7850Sstevel@tonic-gate 		diff = ts2 - ts1;
7860Sstevel@tonic-gate 		delta = diff - entsrc->last_diff;
7870Sstevel@tonic-gate 		if (delta < 0)
7880Sstevel@tonic-gate 			delta = -delta;
7890Sstevel@tonic-gate 		delta2 = delta - entsrc->last_delta;
7900Sstevel@tonic-gate 		if (delta2 < 0)
7910Sstevel@tonic-gate 			delta2 = -delta2;
7920Sstevel@tonic-gate 		delta3 = delta2 - entsrc->last_delta2;
7930Sstevel@tonic-gate 		if (delta3 < 0)
7940Sstevel@tonic-gate 			delta3 = -delta3;
7950Sstevel@tonic-gate 		entsrc->last_diff = diff;
7960Sstevel@tonic-gate 		entsrc->last_delta = delta;
7970Sstevel@tonic-gate 		entsrc->last_delta2 = delta2;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 		if (delta > delta2)
8000Sstevel@tonic-gate 			delta = delta2;
8010Sstevel@tonic-gate 		if (delta > delta3)
8020Sstevel@tonic-gate 			delta = delta3;
8030Sstevel@tonic-gate 		delta2 = 0;
8040Sstevel@tonic-gate 		while (delta >>= 1)
8050Sstevel@tonic-gate 			delta2++;
8060Sstevel@tonic-gate 		ent += delta2;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 		/*
8090Sstevel@tonic-gate 		 * If the memory block has changed, credit the pool with
8100Sstevel@tonic-gate 		 * the entropy estimate.  The entropy estimate is doubled
8110Sstevel@tonic-gate 		 * because the single-bit checksum misses half the change
8120Sstevel@tonic-gate 		 * on average.
8130Sstevel@tonic-gate 		 */
8140Sstevel@tonic-gate 		if (physmem_parity_update(entsrc->parity, oblock,
8150Sstevel@tonic-gate 		    digest[0] & 1))
8160Sstevel@tonic-gate 			ent += 2 * entsrc->entperblock;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 		/* Add the entropy bytes to the pool */
8190Sstevel@tonic-gate 		swrand_add_bytes(digest, HASHSIZE);
8200Sstevel@tonic-gate 		swrand_add_bytes((uint8_t *)&ts1, sizeof (ts1));
8210Sstevel@tonic-gate 		swrand_add_bytes((uint8_t *)&ts2, sizeof (ts2));
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	swrand_mix_pool(ent);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate static int
8280Sstevel@tonic-gate physmem_parity_update(uint8_t *parity_vec, uint32_t block, int parity)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	/* Test and set the parity bit, return 1 if changed */
8310Sstevel@tonic-gate 	if (parity == ((parity_vec[block >> 3] >> (block & 7)) & 1))
8320Sstevel@tonic-gate 		return (0);
8330Sstevel@tonic-gate 	parity_vec[block >> 3] ^= 1 << (block & 7);
8340Sstevel@tonic-gate 	return (1);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate /* Compute number of memory blocks available to scan */
8380Sstevel@tonic-gate static void
8390Sstevel@tonic-gate physmem_count_blocks()
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	struct memlist *pmem;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	memlist_read_lock();
8440Sstevel@tonic-gate 	entsrc.nblocks = 0;
8450Sstevel@tonic-gate 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
8460Sstevel@tonic-gate 		entsrc.nblocks += pmem->size / MEMBLOCKSIZE;
8470Sstevel@tonic-gate 		if (entsrc.nblocks > MAXMEMBLOCKS) {
8480Sstevel@tonic-gate 			entsrc.nblocks = MAXMEMBLOCKS;
8490Sstevel@tonic-gate 			break;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 	memlist_read_unlock();
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate /*
8560Sstevel@tonic-gate  * Dynamic Reconfiguration call-back functions
8570Sstevel@tonic-gate  */
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate /* ARGSUSED */
8600Sstevel@tonic-gate static void
8610Sstevel@tonic-gate rnd_dr_callback_post_add(void *arg, pgcnt_t delta)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate 	/* More memory is available now, so update entsrc->nblocks. */
8640Sstevel@tonic-gate 	physmem_count_blocks();
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate /* Call-back routine invoked before the DR starts a memory removal. */
8680Sstevel@tonic-gate /* ARGSUSED */
8690Sstevel@tonic-gate static int
8700Sstevel@tonic-gate rnd_dr_callback_pre_del(void *arg, pgcnt_t delta)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	return (0);
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate /* Call-back routine invoked after the DR starts a memory removal. */
8760Sstevel@tonic-gate /* ARGSUSED */
8770Sstevel@tonic-gate static void
8780Sstevel@tonic-gate rnd_dr_callback_post_del(void *arg, pgcnt_t delta, int cancelled)
8790Sstevel@tonic-gate {
8800Sstevel@tonic-gate 	/* Memory has shrunk, so update entsrc->nblocks. */
8810Sstevel@tonic-gate 	physmem_count_blocks();
8820Sstevel@tonic-gate }
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate /* Timeout handling to gather entropy from physmem events */
8850Sstevel@tonic-gate static void
8860Sstevel@tonic-gate swrand_schedule_timeout(void)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	clock_t ut;	/* time in microseconds */
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&srndpool_lock));
8910Sstevel@tonic-gate 	/*
8920Sstevel@tonic-gate 	 * The new timeout value is taken from the pool of random bits.
8930Sstevel@tonic-gate 	 * We're merely reading the first 32 bits from the pool here, not
8940Sstevel@tonic-gate 	 * consuming any entropy.
8950Sstevel@tonic-gate 	 * This routine is usually called right after stirring the pool, so
8960Sstevel@tonic-gate 	 * srndpool[0] will have a *fresh* random value each time.
8970Sstevel@tonic-gate 	 * The timeout multiplier value is a random value between 0.7 sec and
8980Sstevel@tonic-gate 	 * 1.748575 sec (0.7 sec + 0xFFFFF microseconds).
8990Sstevel@tonic-gate 	 * The new timeout is TIMEOUT_INTERVAL times that multiplier.
9000Sstevel@tonic-gate 	 */
9010Sstevel@tonic-gate 	ut = 700000 + (clock_t)(srndpool[0] & 0xFFFFF);
9020Sstevel@tonic-gate 	rnd_timeout_id = timeout(rnd_handler, NULL,
9030Sstevel@tonic-gate 	    TIMEOUT_INTERVAL * drv_usectohz(ut));
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate /*ARGSUSED*/
9070Sstevel@tonic-gate static void
9080Sstevel@tonic-gate rnd_handler(void *arg)
9090Sstevel@tonic-gate {
9100Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	physmem_ent_gen(&entsrc);
9130Sstevel@tonic-gate 	if (snum_waiters > 0)
9140Sstevel@tonic-gate 		cv_broadcast(&srndpool_read_cv);
9150Sstevel@tonic-gate 	swrand_schedule_timeout();
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
9180Sstevel@tonic-gate }
91910500SHai-May.Chao@Sun.COM 
92010500SHai-May.Chao@Sun.COM /*
92110500SHai-May.Chao@Sun.COM  * Swrand Power-Up Self-Test
92210500SHai-May.Chao@Sun.COM  */
92310500SHai-May.Chao@Sun.COM void
92410500SHai-May.Chao@Sun.COM swrand_POST(int *rc)
92510500SHai-May.Chao@Sun.COM {
92610500SHai-May.Chao@Sun.COM 
92710500SHai-May.Chao@Sun.COM 	*rc = fips_rng_post();
92810500SHai-May.Chao@Sun.COM 
92910500SHai-May.Chao@Sun.COM }
930