xref: /titanic_50/usr/src/uts/common/crypto/io/swrand.c (revision 6ea3c0609e50782557505b88bb391b786bca32c9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58047c9fbSmcpowers  * Common Development and Distribution License (the "License").
68047c9fbSmcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2295014fbbSDan OpenSolaris Anderson  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Software based random number provider for the Kernel Cryptographic
287c478bd9Sstevel@tonic-gate  * Framework (KCF). This provider periodically collects unpredictable input
297c478bd9Sstevel@tonic-gate  * from external sources and processes it into a pool of entropy (randomness)
307c478bd9Sstevel@tonic-gate  * in order to satisfy requests for random bits from kCF. It implements
317c478bd9Sstevel@tonic-gate  * software-based mixing, extraction, and generation algorithms.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * A history note: The software-based algorithms in this file used to be
347c478bd9Sstevel@tonic-gate  * part of the /dev/random driver.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/debug.h>
407c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
417c478bd9Sstevel@tonic-gate #include <vm/hat.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/memlist.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
467c478bd9Sstevel@tonic-gate #include <sys/random.h>
477c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
487c478bd9Sstevel@tonic-gate #include <sys/mman.h>
497c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
507c478bd9Sstevel@tonic-gate #include <sys/mem_config.h>
517c478bd9Sstevel@tonic-gate #include <sys/time.h>
527c478bd9Sstevel@tonic-gate #include <sys/crypto/spi.h>
537c478bd9Sstevel@tonic-gate #include <sys/sha1.h>
547c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
557c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
56ae115bc7Smrj #include <sys/hold_page.h>
57fe54a78eSHai-May Chao #include <rng/fips_random.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	RNDPOOLSIZE		1024	/* Pool size in bytes */
607c478bd9Sstevel@tonic-gate #define	HASHBUFSIZE		64	/* Buffer size used for pool mixing */
617c478bd9Sstevel@tonic-gate #define	MAXMEMBLOCKS		16384	/* Number of memory blocks to scan */
627c478bd9Sstevel@tonic-gate #define	MEMBLOCKSIZE		4096	/* Size of memory block to read */
637c478bd9Sstevel@tonic-gate #define	MINEXTRACTBITS		160	/* Min entropy level for extraction */
647c478bd9Sstevel@tonic-gate #define	TIMEOUT_INTERVAL	5	/* Periodic mixing interval in secs */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* Hash-algo generic definitions. For now, they are SHA1's. */
677c478bd9Sstevel@tonic-gate #define	HASHSIZE		20
687c478bd9Sstevel@tonic-gate #define	HASH_CTX		SHA1_CTX
697c478bd9Sstevel@tonic-gate #define	HashInit(ctx)		SHA1Init((ctx))
707c478bd9Sstevel@tonic-gate #define	HashUpdate(ctx, p, s)	SHA1Update((ctx), (p), (s))
717c478bd9Sstevel@tonic-gate #define	HashFinal(d, ctx)	SHA1Final((d), (ctx))
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* Physical memory entropy source */
747c478bd9Sstevel@tonic-gate typedef struct physmem_entsrc_s {
757c478bd9Sstevel@tonic-gate 	uint8_t *parity;		/* parity bit vector */
767c478bd9Sstevel@tonic-gate 	caddr_t pmbuf;			/* buffer for memory block */
777c478bd9Sstevel@tonic-gate 	uint32_t nblocks;		/* number of  memory blocks */
787c478bd9Sstevel@tonic-gate 	int entperblock;		/* entropy bits per block read */
797c478bd9Sstevel@tonic-gate 	hrtime_t last_diff;		/* previous time to process a block */
807c478bd9Sstevel@tonic-gate 	hrtime_t last_delta;		/* previous time delta */
817c478bd9Sstevel@tonic-gate 	hrtime_t last_delta2;		/* previous 2nd order time delta */
827c478bd9Sstevel@tonic-gate } physmem_entsrc_t;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static uint32_t srndpool[RNDPOOLSIZE/4];	/* Pool of random bits */
858047c9fbSmcpowers static uint32_t buffer[RNDPOOLSIZE/4];	/* entropy mixed in later */
868047c9fbSmcpowers static int buffer_bytes;		/* bytes written to buffer */
877c478bd9Sstevel@tonic-gate static uint32_t entropy_bits;		/* pool's current amount of entropy */
887c478bd9Sstevel@tonic-gate static kmutex_t srndpool_lock;		/* protects r/w accesses to the pool, */
897c478bd9Sstevel@tonic-gate 					/* and the global variables */
908047c9fbSmcpowers static kmutex_t buffer_lock;		/* protects r/w accesses to buffer */
917c478bd9Sstevel@tonic-gate static kcondvar_t srndpool_read_cv;	/* serializes poll/read syscalls */
927c478bd9Sstevel@tonic-gate static int pindex;			/* Global index for adding/extracting */
937c478bd9Sstevel@tonic-gate 					/* from the pool */
948047c9fbSmcpowers static int bstart, bindex;		/* Global vars for adding/extracting */
958047c9fbSmcpowers 					/* from the buffer */
967c478bd9Sstevel@tonic-gate static uint8_t leftover[HASHSIZE];	/* leftover output */
97fe54a78eSHai-May Chao static uint32_t	swrand_XKEY[6];		/* one extra word for getentropy */
987c478bd9Sstevel@tonic-gate static int leftover_bytes;		/* leftover length */
9956498af3SHai-May Chao static uint32_t previous_bytes[HASHSIZE/BYTES_IN_WORD];	/* prev random bytes */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static physmem_entsrc_t entsrc;		/* Physical mem as an entropy source */
1027c478bd9Sstevel@tonic-gate static timeout_id_t rnd_timeout_id;
1037c478bd9Sstevel@tonic-gate static int snum_waiters;
1047c478bd9Sstevel@tonic-gate static crypto_kcf_provider_handle_t swrand_prov_handle = NULL;
1057c478bd9Sstevel@tonic-gate swrand_stats_t swrand_stats;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int physmem_ent_init(physmem_entsrc_t *);
1087c478bd9Sstevel@tonic-gate static void physmem_ent_fini(physmem_entsrc_t *);
1097c478bd9Sstevel@tonic-gate static void physmem_ent_gen(physmem_entsrc_t *);
1107c478bd9Sstevel@tonic-gate static int physmem_parity_update(uint8_t *, uint32_t, int);
1117c478bd9Sstevel@tonic-gate static void physmem_count_blocks();
1127c478bd9Sstevel@tonic-gate static void rnd_dr_callback_post_add(void *, pgcnt_t);
1137c478bd9Sstevel@tonic-gate static int rnd_dr_callback_pre_del(void *, pgcnt_t);
1147c478bd9Sstevel@tonic-gate static void rnd_dr_callback_post_del(void *, pgcnt_t, int);
1157c478bd9Sstevel@tonic-gate static void rnd_handler(void *arg);
1167c478bd9Sstevel@tonic-gate static void swrand_init();
1177c478bd9Sstevel@tonic-gate static void swrand_schedule_timeout(void);
1187c478bd9Sstevel@tonic-gate static int swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t);
1197c478bd9Sstevel@tonic-gate static void swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est);
1208047c9fbSmcpowers static void swrand_add_entropy_later(uint8_t *ptr, size_t len);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* Dynamic Reconfiguration related declarations */
1237c478bd9Sstevel@tonic-gate kphysm_setup_vector_t rnd_dr_callback_vec = {
1247c478bd9Sstevel@tonic-gate 	KPHYSM_SETUP_VECTOR_VERSION,
1257c478bd9Sstevel@tonic-gate 	rnd_dr_callback_post_add,
1267c478bd9Sstevel@tonic-gate 	rnd_dr_callback_pre_del,
1277c478bd9Sstevel@tonic-gate 	rnd_dr_callback_post_del
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate extern struct mod_ops mod_cryptoops;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate static struct modlcrypto modlcrypto = {
1367c478bd9Sstevel@tonic-gate 	&mod_cryptoops,
137d2b32306Smcpowers 	"Kernel Random number Provider"
1387c478bd9Sstevel@tonic-gate };
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1417c478bd9Sstevel@tonic-gate 	MODREV_1,
1427c478bd9Sstevel@tonic-gate 	(void *)&modlcrypto,
1437c478bd9Sstevel@tonic-gate 	NULL
1447c478bd9Sstevel@tonic-gate };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * CSPI information (entry points, provider info, etc.)
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate static void swrand_provider_status(crypto_provider_handle_t, uint_t *);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static crypto_control_ops_t swrand_control_ops = {
1527c478bd9Sstevel@tonic-gate 	swrand_provider_status
1537c478bd9Sstevel@tonic-gate };
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static int swrand_seed_random(crypto_provider_handle_t, crypto_session_id_t,
1568047c9fbSmcpowers     uchar_t *, size_t, uint_t, uint32_t, crypto_req_handle_t);
1577c478bd9Sstevel@tonic-gate static int swrand_generate_random(crypto_provider_handle_t,
1587c478bd9Sstevel@tonic-gate     crypto_session_id_t, uchar_t *, size_t, crypto_req_handle_t);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static crypto_random_number_ops_t swrand_random_number_ops = {
1617c478bd9Sstevel@tonic-gate 	swrand_seed_random,
1627c478bd9Sstevel@tonic-gate 	swrand_generate_random
1637c478bd9Sstevel@tonic-gate };
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static crypto_ops_t swrand_crypto_ops = {
1667c478bd9Sstevel@tonic-gate 	&swrand_control_ops,
1677c478bd9Sstevel@tonic-gate 	NULL,
1687c478bd9Sstevel@tonic-gate 	NULL,
1697c478bd9Sstevel@tonic-gate 	NULL,
1707c478bd9Sstevel@tonic-gate 	NULL,
1717c478bd9Sstevel@tonic-gate 	NULL,
1727c478bd9Sstevel@tonic-gate 	NULL,
1737c478bd9Sstevel@tonic-gate 	NULL,
1747c478bd9Sstevel@tonic-gate 	&swrand_random_number_ops,
1757c478bd9Sstevel@tonic-gate 	NULL,
1767c478bd9Sstevel@tonic-gate 	NULL,
1777c478bd9Sstevel@tonic-gate 	NULL,
1787c478bd9Sstevel@tonic-gate 	NULL,
17973556491SAnthony Scarpino 	NULL,
18073556491SAnthony Scarpino 	NULL,
18173556491SAnthony Scarpino 	NULL,
182*6ea3c060SGarrett D'Amore 	NULL,
1837c478bd9Sstevel@tonic-gate };
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate static crypto_provider_info_t swrand_prov_info = {
18673556491SAnthony Scarpino 	CRYPTO_SPI_VERSION_4,
1877c478bd9Sstevel@tonic-gate 	"Kernel Random Number Provider",
1887c478bd9Sstevel@tonic-gate 	CRYPTO_SW_PROVIDER,
1897c478bd9Sstevel@tonic-gate 	{&modlinkage},
1907c478bd9Sstevel@tonic-gate 	NULL,
1917c478bd9Sstevel@tonic-gate 	&swrand_crypto_ops,
1927c478bd9Sstevel@tonic-gate 	0,
1937c478bd9Sstevel@tonic-gate 	NULL
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate int
_init(void)1977c478bd9Sstevel@tonic-gate _init(void)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate 	int ret;
2007c478bd9Sstevel@tonic-gate 	hrtime_t ts;
2017c478bd9Sstevel@tonic-gate 	time_t now;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	mutex_init(&srndpool_lock, NULL, MUTEX_DEFAULT, NULL);
2048047c9fbSmcpowers 	mutex_init(&buffer_lock, NULL, MUTEX_DEFAULT, NULL);
2057c478bd9Sstevel@tonic-gate 	cv_init(&srndpool_read_cv, NULL, CV_DEFAULT, NULL);
2067c478bd9Sstevel@tonic-gate 	entropy_bits = 0;
2077c478bd9Sstevel@tonic-gate 	pindex = 0;
2088047c9fbSmcpowers 	bindex = 0;
2098047c9fbSmcpowers 	bstart = 0;
2107c478bd9Sstevel@tonic-gate 	snum_waiters = 0;
2117c478bd9Sstevel@tonic-gate 	leftover_bytes = 0;
2128047c9fbSmcpowers 	buffer_bytes = 0;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Initialize the pool using
2167c478bd9Sstevel@tonic-gate 	 * . 2 unpredictable times: high resolution time since the boot-time,
2177c478bd9Sstevel@tonic-gate 	 *   and the current time-of-the day.
2187c478bd9Sstevel@tonic-gate 	 * . The initial physical memory state.
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 	ts = gethrtime();
2217c478bd9Sstevel@tonic-gate 	swrand_add_entropy((uint8_t *)&ts, sizeof (ts), 0);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	(void) drv_getparm(TIME, &now);
2247c478bd9Sstevel@tonic-gate 	swrand_add_entropy((uint8_t *)&now, sizeof (now), 0);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	ret = kphysm_setup_func_register(&rnd_dr_callback_vec, NULL);
2277c478bd9Sstevel@tonic-gate 	ASSERT(ret == 0);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (physmem_ent_init(&entsrc) != 0) {
230d3b2efc7SAnthony Scarpino 		ret = ENOMEM;
231d3b2efc7SAnthony Scarpino 		goto exit1;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
234d3b2efc7SAnthony Scarpino 	if ((ret = mod_install(&modlinkage)) != 0)
235d3b2efc7SAnthony Scarpino 		goto exit2;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* Schedule periodic mixing of the pool. */
2387c478bd9Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
2397c478bd9Sstevel@tonic-gate 	swrand_schedule_timeout();
2407c478bd9Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
241fe54a78eSHai-May Chao 	(void) swrand_get_entropy((uint8_t *)swrand_XKEY, HASHSIZE, B_TRUE);
242fe54a78eSHai-May Chao 	bcopy(swrand_XKEY, previous_bytes, HASHSIZE);
2437c478bd9Sstevel@tonic-gate 
244d3b2efc7SAnthony Scarpino 	/* Register with KCF. If the registration fails, return error. */
245d3b2efc7SAnthony Scarpino 	if (crypto_register_provider(&swrand_prov_info, &swrand_prov_handle)) {
246d3b2efc7SAnthony Scarpino 		(void) mod_remove(&modlinkage);
247d3b2efc7SAnthony Scarpino 		ret = EACCES;
248d3b2efc7SAnthony Scarpino 		goto exit2;
249d3b2efc7SAnthony Scarpino 	}
250d3b2efc7SAnthony Scarpino 
2517c478bd9Sstevel@tonic-gate 	return (0);
252d3b2efc7SAnthony Scarpino 
253d3b2efc7SAnthony Scarpino exit2:
254d3b2efc7SAnthony Scarpino 	physmem_ent_fini(&entsrc);
255d3b2efc7SAnthony Scarpino exit1:
256d3b2efc7SAnthony Scarpino 	mutex_destroy(&srndpool_lock);
257d3b2efc7SAnthony Scarpino 	mutex_destroy(&buffer_lock);
258d3b2efc7SAnthony Scarpino 	cv_destroy(&srndpool_read_cv);
259d3b2efc7SAnthony Scarpino 	return (ret);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2637c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * Control entry points.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate /* ARGSUSED */
2727c478bd9Sstevel@tonic-gate static void
swrand_provider_status(crypto_provider_handle_t provider,uint_t * status)2737c478bd9Sstevel@tonic-gate swrand_provider_status(crypto_provider_handle_t provider, uint_t *status)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	*status = CRYPTO_PROVIDER_READY;
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * Random number entry points.
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate /* ARGSUSED */
2827c478bd9Sstevel@tonic-gate static int
swrand_seed_random(crypto_provider_handle_t provider,crypto_session_id_t sid,uchar_t * buf,size_t len,uint_t entropy_est,uint32_t flags,crypto_req_handle_t req)2837c478bd9Sstevel@tonic-gate swrand_seed_random(crypto_provider_handle_t provider, crypto_session_id_t sid,
2848047c9fbSmcpowers     uchar_t *buf, size_t len, uint_t entropy_est, uint32_t flags,
2858047c9fbSmcpowers     crypto_req_handle_t req)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	/* The entropy estimate is always 0 in this path */
2888047c9fbSmcpowers 	if (flags & CRYPTO_SEED_NOW)
2897c478bd9Sstevel@tonic-gate 		swrand_add_entropy(buf, len, 0);
2908047c9fbSmcpowers 	else
2918047c9fbSmcpowers 		swrand_add_entropy_later(buf, len);
2927c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /* ARGSUSED */
2967c478bd9Sstevel@tonic-gate static int
swrand_generate_random(crypto_provider_handle_t provider,crypto_session_id_t sid,uchar_t * buf,size_t len,crypto_req_handle_t req)2977c478bd9Sstevel@tonic-gate swrand_generate_random(crypto_provider_handle_t provider,
2987c478bd9Sstevel@tonic-gate     crypto_session_id_t sid, uchar_t *buf, size_t len, crypto_req_handle_t req)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	if (crypto_kmflag(req) == KM_NOSLEEP)
3017c478bd9Sstevel@tonic-gate 		(void) swrand_get_entropy(buf, len, B_TRUE);
3027c478bd9Sstevel@tonic-gate 	else
3037c478bd9Sstevel@tonic-gate 		(void) swrand_get_entropy(buf, len, B_FALSE);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	return (CRYPTO_SUCCESS);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Extraction of entropy from the pool.
3107c478bd9Sstevel@tonic-gate  *
3117c478bd9Sstevel@tonic-gate  * Returns "len" random bytes in *ptr.
3127c478bd9Sstevel@tonic-gate  * Try to gather some more entropy by calling physmem_ent_gen() when less than
3137c478bd9Sstevel@tonic-gate  * MINEXTRACTBITS are present in the pool.
3147c478bd9Sstevel@tonic-gate  * Will block if not enough entropy was available and the call is blocking.
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate static int
swrand_get_entropy(uint8_t * ptr,size_t len,boolean_t nonblock)3177c478bd9Sstevel@tonic-gate swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t nonblock)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	int i, bytes;
3207c478bd9Sstevel@tonic-gate 	HASH_CTX hashctx;
3217c478bd9Sstevel@tonic-gate 	uint8_t digest[HASHSIZE], *pool;
32256498af3SHai-May Chao 	uint32_t tempout[HASHSIZE/BYTES_IN_WORD];
323fe54a78eSHai-May Chao 	int size;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
3267c478bd9Sstevel@tonic-gate 	if (leftover_bytes > 0) {
3277c478bd9Sstevel@tonic-gate 		bytes = min(len, leftover_bytes);
3287c478bd9Sstevel@tonic-gate 		bcopy(leftover, ptr, bytes);
3297c478bd9Sstevel@tonic-gate 		len -= bytes;
3307c478bd9Sstevel@tonic-gate 		ptr += bytes;
3317c478bd9Sstevel@tonic-gate 		leftover_bytes -= bytes;
3327c478bd9Sstevel@tonic-gate 		if (leftover_bytes > 0)
3337c478bd9Sstevel@tonic-gate 			ovbcopy(leftover+bytes, leftover, leftover_bytes);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	while (len > 0) {
3377c478bd9Sstevel@tonic-gate 		/* Check if there is enough entropy */
3387c478bd9Sstevel@tonic-gate 		while (entropy_bits < MINEXTRACTBITS) {
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 			physmem_ent_gen(&entsrc);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 			if (entropy_bits < MINEXTRACTBITS &&
3437c478bd9Sstevel@tonic-gate 			    nonblock == B_TRUE) {
3447c478bd9Sstevel@tonic-gate 				mutex_exit(&srndpool_lock);
3457c478bd9Sstevel@tonic-gate 				return (EAGAIN);
3467c478bd9Sstevel@tonic-gate 			}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 			if (entropy_bits < MINEXTRACTBITS) {
3497c478bd9Sstevel@tonic-gate 				ASSERT(nonblock == B_FALSE);
3507c478bd9Sstevel@tonic-gate 				snum_waiters++;
3517c478bd9Sstevel@tonic-gate 				if (cv_wait_sig(&srndpool_read_cv,
3527c478bd9Sstevel@tonic-gate 				    &srndpool_lock) == 0) {
3537c478bd9Sstevel@tonic-gate 					snum_waiters--;
3547c478bd9Sstevel@tonic-gate 					mutex_exit(&srndpool_lock);
3557c478bd9Sstevel@tonic-gate 					return (EINTR);
3567c478bd9Sstevel@tonic-gate 				}
3577c478bd9Sstevel@tonic-gate 				snum_waiters--;
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		/* Figure out how many bytes to extract */
3627c478bd9Sstevel@tonic-gate 		bytes = min(HASHSIZE, len);
36395014fbbSDan OpenSolaris Anderson 		bytes = min(bytes, CRYPTO_BITS2BYTES(entropy_bits));
36495014fbbSDan OpenSolaris Anderson 		entropy_bits -= CRYPTO_BYTES2BITS(bytes);
36595014fbbSDan OpenSolaris Anderson 		BUMP_SWRAND_STATS(ss_entOut, CRYPTO_BYTES2BITS(bytes));
3667c478bd9Sstevel@tonic-gate 		swrand_stats.ss_entEst = entropy_bits;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		/* Extract entropy by hashing pool content */
3697c478bd9Sstevel@tonic-gate 		HashInit(&hashctx);
3707c478bd9Sstevel@tonic-gate 		HashUpdate(&hashctx, (uint8_t *)srndpool, RNDPOOLSIZE);
3717c478bd9Sstevel@tonic-gate 		HashFinal(digest, &hashctx);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 		/*
3747c478bd9Sstevel@tonic-gate 		 * Feed the digest back into the pool so next
3757c478bd9Sstevel@tonic-gate 		 * extraction produces different result
3767c478bd9Sstevel@tonic-gate 		 */
3777c478bd9Sstevel@tonic-gate 		pool = (uint8_t *)srndpool;
3787c478bd9Sstevel@tonic-gate 		for (i = 0; i < HASHSIZE; i++) {
3797c478bd9Sstevel@tonic-gate 			pool[pindex++] ^= digest[i];
3807c478bd9Sstevel@tonic-gate 			/* pindex modulo RNDPOOLSIZE */
3817c478bd9Sstevel@tonic-gate 			pindex &= (RNDPOOLSIZE - 1);
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 
384fe54a78eSHai-May Chao 		/* LINTED E_BAD_PTR_CAST_ALIGN */
385fe54a78eSHai-May Chao 		fips_random_inner(swrand_XKEY, tempout, (uint32_t *)digest);
386fe54a78eSHai-May Chao 
387fe54a78eSHai-May Chao 		if (len >= HASHSIZE) {
388fe54a78eSHai-May Chao 			size = HASHSIZE;
389fe54a78eSHai-May Chao 		} else {
390fe54a78eSHai-May Chao 			size = min(bytes, HASHSIZE);
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 
393fe54a78eSHai-May Chao 		/*
394fe54a78eSHai-May Chao 		 * FIPS 140-2: Continuous RNG test - each generation
395fe54a78eSHai-May Chao 		 * of an n-bit block shall be compared with the previously
396fe54a78eSHai-May Chao 		 * generated block. Test shall fail if any two compared
397fe54a78eSHai-May Chao 		 * n-bit blocks are equal.
398fe54a78eSHai-May Chao 		 */
39956498af3SHai-May Chao 		for (i = 0; i < HASHSIZE/BYTES_IN_WORD; i++) {
400fe54a78eSHai-May Chao 			if (tempout[i] != previous_bytes[i])
401fe54a78eSHai-May Chao 				break;
4027c478bd9Sstevel@tonic-gate 		}
403b5a2d845SHai-May Chao 
404b5a2d845SHai-May Chao 		if (i == HASHSIZE/BYTES_IN_WORD) {
405fe54a78eSHai-May Chao 			cmn_err(CE_WARN, "swrand: The value of 160-bit block "
406fe54a78eSHai-May Chao 			    "random bytes are same as the previous one.\n");
407b5a2d845SHai-May Chao 			/* discard random bytes and return error */
408b5a2d845SHai-May Chao 			return (EIO);
409b5a2d845SHai-May Chao 		}
410fe54a78eSHai-May Chao 
411fe54a78eSHai-May Chao 		bcopy(tempout, previous_bytes, HASHSIZE);
412fe54a78eSHai-May Chao 
413fe54a78eSHai-May Chao 		bcopy(tempout, ptr, size);
414fe54a78eSHai-May Chao 		if (len < HASHSIZE) {
415fe54a78eSHai-May Chao 			leftover_bytes = HASHSIZE - bytes;
4169b6b215eSHai-May Chao 			bcopy((uint8_t *)tempout + bytes, leftover,
4179b6b215eSHai-May Chao 			    leftover_bytes);
418fe54a78eSHai-May Chao 		}
419fe54a78eSHai-May Chao 
420fe54a78eSHai-May Chao 		ptr += size;
421fe54a78eSHai-May Chao 		len -= size;
422fe54a78eSHai-May Chao 		BUMP_SWRAND_STATS(ss_bytesOut, size);
423fe54a78eSHai-May Chao 	}
424fe54a78eSHai-May Chao 
425fe54a78eSHai-May Chao 	/* Zero out sensitive information */
426fe54a78eSHai-May Chao 	bzero(digest, HASHSIZE);
427fe54a78eSHai-May Chao 	bzero(tempout, HASHSIZE);
4287c478bd9Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
4297c478bd9Sstevel@tonic-gate 	return (0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4328047c9fbSmcpowers #define	SWRAND_ADD_BYTES(ptr, len, i, pool)		\
4338047c9fbSmcpowers 	ASSERT((ptr) != NULL && (len) > 0);		\
4348047c9fbSmcpowers 	BUMP_SWRAND_STATS(ss_bytesIn, (len));		\
4358047c9fbSmcpowers 	while ((len)--) {				\
4368047c9fbSmcpowers 		(pool)[(i)++] ^= *(ptr);		\
4378047c9fbSmcpowers 		(ptr)++;				\
4388047c9fbSmcpowers 		(i) &= (RNDPOOLSIZE - 1);		\
4398047c9fbSmcpowers 	}
4408047c9fbSmcpowers 
4417c478bd9Sstevel@tonic-gate /* Write some more user-provided entropy to the pool */
4427c478bd9Sstevel@tonic-gate static void
swrand_add_bytes(uint8_t * ptr,size_t len)4437c478bd9Sstevel@tonic-gate swrand_add_bytes(uint8_t *ptr, size_t len)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	uint8_t *pool = (uint8_t *)srndpool;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&srndpool_lock));
4488047c9fbSmcpowers 	SWRAND_ADD_BYTES(ptr, len, pindex, pool);
4498047c9fbSmcpowers }
4507c478bd9Sstevel@tonic-gate 
4518047c9fbSmcpowers /*
4528047c9fbSmcpowers  * Add bytes to buffer. Adding the buffer to the random pool
4538047c9fbSmcpowers  * is deferred until the random pool is mixed.
4548047c9fbSmcpowers  */
4558047c9fbSmcpowers static void
swrand_add_bytes_later(uint8_t * ptr,size_t len)4568047c9fbSmcpowers swrand_add_bytes_later(uint8_t *ptr, size_t len)
4578047c9fbSmcpowers {
4588047c9fbSmcpowers 	uint8_t *pool = (uint8_t *)buffer;
4598047c9fbSmcpowers 
4608047c9fbSmcpowers 	ASSERT(MUTEX_HELD(&buffer_lock));
4618047c9fbSmcpowers 	SWRAND_ADD_BYTES(ptr, len, bindex, pool);
4628047c9fbSmcpowers 	buffer_bytes += len;
4637c478bd9Sstevel@tonic-gate }
4648047c9fbSmcpowers 
4658047c9fbSmcpowers #undef SWRAND_ADD_BYTES
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /* Mix the pool */
4687c478bd9Sstevel@tonic-gate static void
swrand_mix_pool(uint16_t entropy_est)4697c478bd9Sstevel@tonic-gate swrand_mix_pool(uint16_t entropy_est)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	int i, j, k, start;
4727c478bd9Sstevel@tonic-gate 	HASH_CTX hashctx;
4737c478bd9Sstevel@tonic-gate 	uint8_t digest[HASHSIZE];
4747c478bd9Sstevel@tonic-gate 	uint8_t *pool = (uint8_t *)srndpool;
4758047c9fbSmcpowers 	uint8_t *bp = (uint8_t *)buffer;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&srndpool_lock));
4787c478bd9Sstevel@tonic-gate 
4798047c9fbSmcpowers 	/* add deferred bytes */
4808047c9fbSmcpowers 	mutex_enter(&buffer_lock);
4818047c9fbSmcpowers 	if (buffer_bytes > 0) {
4828047c9fbSmcpowers 		if (buffer_bytes >= RNDPOOLSIZE) {
4838047c9fbSmcpowers 			for (i = 0; i < RNDPOOLSIZE/4; i++) {
4848047c9fbSmcpowers 				srndpool[i] ^= buffer[i];
4858047c9fbSmcpowers 				buffer[i] = 0;
4868047c9fbSmcpowers 			}
4878047c9fbSmcpowers 			bstart = bindex = 0;
4888047c9fbSmcpowers 		} else {
4898047c9fbSmcpowers 			for (i = 0; i < buffer_bytes; i++) {
4908047c9fbSmcpowers 				pool[pindex++] ^= bp[bstart];
4918047c9fbSmcpowers 				bp[bstart++] = 0;
4928047c9fbSmcpowers 				pindex &= (RNDPOOLSIZE - 1);
4938047c9fbSmcpowers 				bstart &= (RNDPOOLSIZE - 1);
4948047c9fbSmcpowers 			}
4958047c9fbSmcpowers 			ASSERT(bstart == bindex);
4968047c9fbSmcpowers 		}
4978047c9fbSmcpowers 		buffer_bytes = 0;
4988047c9fbSmcpowers 	}
4998047c9fbSmcpowers 	mutex_exit(&buffer_lock);
5008047c9fbSmcpowers 
5017c478bd9Sstevel@tonic-gate 	start = 0;
5027c478bd9Sstevel@tonic-gate 	for (i = 0; i < RNDPOOLSIZE/HASHSIZE + 1; i++) {
5037c478bd9Sstevel@tonic-gate 		HashInit(&hashctx);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		/* Hash a buffer centered on a block in the pool */
5067c478bd9Sstevel@tonic-gate 		if (start + HASHBUFSIZE <= RNDPOOLSIZE)
5077c478bd9Sstevel@tonic-gate 			HashUpdate(&hashctx, &pool[start], HASHBUFSIZE);
5087c478bd9Sstevel@tonic-gate 		else {
5097c478bd9Sstevel@tonic-gate 			HashUpdate(&hashctx, &pool[start],
5107c478bd9Sstevel@tonic-gate 			    RNDPOOLSIZE - start);
5117c478bd9Sstevel@tonic-gate 			HashUpdate(&hashctx, pool,
5127c478bd9Sstevel@tonic-gate 			    HASHBUFSIZE - RNDPOOLSIZE + start);
5137c478bd9Sstevel@tonic-gate 		}
5147c478bd9Sstevel@tonic-gate 		HashFinal(digest, &hashctx);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		/* XOR the hash result back into the block */
5177c478bd9Sstevel@tonic-gate 		k = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
5187c478bd9Sstevel@tonic-gate 		for (j = 0; j < HASHSIZE; j++) {
5197c478bd9Sstevel@tonic-gate 			pool[k++] ^= digest[j];
5207c478bd9Sstevel@tonic-gate 			k &= (RNDPOOLSIZE - 1);
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		/* Slide the hash buffer and repeat with next block */
5247c478bd9Sstevel@tonic-gate 		start = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	entropy_bits += entropy_est;
52895014fbbSDan OpenSolaris Anderson 	if (entropy_bits > CRYPTO_BYTES2BITS(RNDPOOLSIZE))
52995014fbbSDan OpenSolaris Anderson 		entropy_bits = CRYPTO_BYTES2BITS(RNDPOOLSIZE);
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	swrand_stats.ss_entEst = entropy_bits;
5327c478bd9Sstevel@tonic-gate 	BUMP_SWRAND_STATS(ss_entIn, entropy_est);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate static void
swrand_add_entropy_later(uint8_t * ptr,size_t len)5368047c9fbSmcpowers swrand_add_entropy_later(uint8_t *ptr, size_t len)
5378047c9fbSmcpowers {
5388047c9fbSmcpowers 	mutex_enter(&buffer_lock);
5398047c9fbSmcpowers 	swrand_add_bytes_later(ptr, len);
5408047c9fbSmcpowers 	mutex_exit(&buffer_lock);
5418047c9fbSmcpowers }
5428047c9fbSmcpowers 
5438047c9fbSmcpowers static void
swrand_add_entropy(uint8_t * ptr,size_t len,uint16_t entropy_est)5447c478bd9Sstevel@tonic-gate swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
5477c478bd9Sstevel@tonic-gate 	swrand_add_bytes(ptr, len);
5487c478bd9Sstevel@tonic-gate 	swrand_mix_pool(entropy_est);
5497c478bd9Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate  * The physmem_* routines below generate entropy by reading blocks of
5547c478bd9Sstevel@tonic-gate  * physical memory.  Entropy is gathered in a couple of ways:
5557c478bd9Sstevel@tonic-gate  *
5567c478bd9Sstevel@tonic-gate  *  - By reading blocks of physical memory and detecting if changes
5577c478bd9Sstevel@tonic-gate  *    occurred in the blocks read.
5587c478bd9Sstevel@tonic-gate  *
5597c478bd9Sstevel@tonic-gate  *  - By measuring the time it takes to load and hash a block of memory
5607c478bd9Sstevel@tonic-gate  *    and computing the differences in the measured time.
5617c478bd9Sstevel@tonic-gate  *
5627c478bd9Sstevel@tonic-gate  * The first method was used in the CryptoRand implementation.  Physical
5637c478bd9Sstevel@tonic-gate  * memory is divided into blocks of fixed size.  A block of memory is
5647c478bd9Sstevel@tonic-gate  * chosen from the possible blocks and hashed to produce a digest.  This
5657c478bd9Sstevel@tonic-gate  * digest is then mixed into the pool.  A single bit from the digest is
5667c478bd9Sstevel@tonic-gate  * used as a parity bit or "checksum" and compared against the previous
5677c478bd9Sstevel@tonic-gate  * "checksum" computed for the block.  If the single-bit checksum has not
5687c478bd9Sstevel@tonic-gate  * changed, no entropy is credited to the pool.  If there is a change,
5697c478bd9Sstevel@tonic-gate  * then the assumption is that at least one bit in the block has changed.
5707c478bd9Sstevel@tonic-gate  * The possible locations within the memory block of where the bit change
5717c478bd9Sstevel@tonic-gate  * occurred is used as a measure of entropy.  For example, if a block
5727c478bd9Sstevel@tonic-gate  * size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of
5737c478bd9Sstevel@tonic-gate  * entropy is available.  Because the single-bit checksum will miss half
5747c478bd9Sstevel@tonic-gate  * of the changes, the amount of entropy credited to the pool is doubled
5757c478bd9Sstevel@tonic-gate  * when a change is detected.  With a 4096 byte block size, a block
5767c478bd9Sstevel@tonic-gate  * change will add a total of 30 bits of entropy to the pool.
5777c478bd9Sstevel@tonic-gate  *
5787c478bd9Sstevel@tonic-gate  * The second method measures the amount of time it takes to read and
5797c478bd9Sstevel@tonic-gate  * hash a physical memory block (as described above).  The time measured
5807c478bd9Sstevel@tonic-gate  * can vary depending on system load, scheduling and other factors.
5817c478bd9Sstevel@tonic-gate  * Differences between consecutive measurements are computed to come up
5827c478bd9Sstevel@tonic-gate  * with an entropy estimate.  The first, second, and third order delta is
5837c478bd9Sstevel@tonic-gate  * calculated to determine the minimum delta value.  The number of bits
5847c478bd9Sstevel@tonic-gate  * present in this minimum delta value is the entropy estimate.  This
5857c478bd9Sstevel@tonic-gate  * entropy estimation technique using time deltas is similar to that used
5867c478bd9Sstevel@tonic-gate  * in /dev/random implementations from Linux/BSD.
5877c478bd9Sstevel@tonic-gate  */
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate static int
physmem_ent_init(physmem_entsrc_t * entsrc)5907c478bd9Sstevel@tonic-gate physmem_ent_init(physmem_entsrc_t *entsrc)
5917c478bd9Sstevel@tonic-gate {
5927c478bd9Sstevel@tonic-gate 	uint8_t *ptr;
5937c478bd9Sstevel@tonic-gate 	int i;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	bzero(entsrc, sizeof (*entsrc));
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	/*
5987c478bd9Sstevel@tonic-gate 	 * The maximum entropy amount in bits per block of memory read is
5997c478bd9Sstevel@tonic-gate 	 * log_2(MEMBLOCKSIZE * 8);
6007c478bd9Sstevel@tonic-gate 	 */
60195014fbbSDan OpenSolaris Anderson 	i = CRYPTO_BYTES2BITS(MEMBLOCKSIZE);
6027c478bd9Sstevel@tonic-gate 	while (i >>= 1)
6037c478bd9Sstevel@tonic-gate 		entsrc->entperblock++;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	/* Initialize entsrc->nblocks */
6067c478bd9Sstevel@tonic-gate 	physmem_count_blocks();
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (entsrc->nblocks == 0) {
6097c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "no memory blocks to scan!");
6107c478bd9Sstevel@tonic-gate 		return (-1);
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	/* Allocate space for the parity vector and memory page */
6147c478bd9Sstevel@tonic-gate 	entsrc->parity = kmem_alloc(howmany(entsrc->nblocks, 8),
6157c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
6167c478bd9Sstevel@tonic-gate 	entsrc->pmbuf = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/* Initialize parity vector with bits from the pool */
6207c478bd9Sstevel@tonic-gate 	i = howmany(entsrc->nblocks, 8);
6217c478bd9Sstevel@tonic-gate 	ptr = entsrc->parity;
6227c478bd9Sstevel@tonic-gate 	while (i > 0) {
6237c478bd9Sstevel@tonic-gate 		if (i > RNDPOOLSIZE) {
6247c478bd9Sstevel@tonic-gate 			bcopy(srndpool, ptr, RNDPOOLSIZE);
6257c478bd9Sstevel@tonic-gate 			mutex_enter(&srndpool_lock);
6267c478bd9Sstevel@tonic-gate 			swrand_mix_pool(0);
6277c478bd9Sstevel@tonic-gate 			mutex_exit(&srndpool_lock);
6287c478bd9Sstevel@tonic-gate 			ptr += RNDPOOLSIZE;
6297c478bd9Sstevel@tonic-gate 			i -= RNDPOOLSIZE;
6307c478bd9Sstevel@tonic-gate 		} else {
6317c478bd9Sstevel@tonic-gate 			bcopy(srndpool, ptr, i);
6327c478bd9Sstevel@tonic-gate 			break;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	/* Generate some entropy to further initialize the pool */
6377c478bd9Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
6387c478bd9Sstevel@tonic-gate 	physmem_ent_gen(entsrc);
6397c478bd9Sstevel@tonic-gate 	entropy_bits = 0;
6407c478bd9Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	return (0);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate static void
physmem_ent_fini(physmem_entsrc_t * entsrc)6467c478bd9Sstevel@tonic-gate physmem_ent_fini(physmem_entsrc_t *entsrc)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	if (entsrc->pmbuf != NULL)
6497c478bd9Sstevel@tonic-gate 		vmem_free(heap_arena, entsrc->pmbuf, PAGESIZE);
6507c478bd9Sstevel@tonic-gate 	if (entsrc->parity != NULL)
6517c478bd9Sstevel@tonic-gate 		kmem_free(entsrc->parity, howmany(entsrc->nblocks, 8));
6527c478bd9Sstevel@tonic-gate 	bzero(entsrc, sizeof (*entsrc));
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate static void
physmem_ent_gen(physmem_entsrc_t * entsrc)6567c478bd9Sstevel@tonic-gate physmem_ent_gen(physmem_entsrc_t *entsrc)
6577c478bd9Sstevel@tonic-gate {
6587c478bd9Sstevel@tonic-gate 	struct memlist *pmem;
6597c478bd9Sstevel@tonic-gate 	offset_t offset, poffset;
6607c478bd9Sstevel@tonic-gate 	pfn_t pfn;
6617c478bd9Sstevel@tonic-gate 	int i, nbytes, len, ent = 0;
6627c478bd9Sstevel@tonic-gate 	uint32_t block, oblock;
6637c478bd9Sstevel@tonic-gate 	hrtime_t ts1, ts2, diff, delta, delta2, delta3;
6647c478bd9Sstevel@tonic-gate 	uint8_t digest[HASHSIZE];
6657c478bd9Sstevel@tonic-gate 	HASH_CTX ctx;
666ae115bc7Smrj 	page_t *pp;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
6697c478bd9Sstevel@tonic-gate 	 * Use each 32-bit quantity in the pool to pick a memory
6707c478bd9Sstevel@tonic-gate 	 * block to read.
6717c478bd9Sstevel@tonic-gate 	 */
6727c478bd9Sstevel@tonic-gate 	for (i = 0; i < RNDPOOLSIZE/4; i++) {
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 		/* If the pool is "full", stop after one block */
67595014fbbSDan OpenSolaris Anderson 		if (entropy_bits + ent >= CRYPTO_BYTES2BITS(RNDPOOLSIZE)) {
6767c478bd9Sstevel@tonic-gate 			if (i > 0)
6777c478bd9Sstevel@tonic-gate 				break;
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		/*
6817c478bd9Sstevel@tonic-gate 		 * This lock protects reading of phys_install.
6827c478bd9Sstevel@tonic-gate 		 * Any changes to this list, by DR, are done while
6837c478bd9Sstevel@tonic-gate 		 * holding this lock. So, holding this lock is sufficient
6847c478bd9Sstevel@tonic-gate 		 * to handle DR also.
6857c478bd9Sstevel@tonic-gate 		 */
6867c478bd9Sstevel@tonic-gate 		memlist_read_lock();
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 		/* We're left with less than 4K of memory after DR */
6897c478bd9Sstevel@tonic-gate 		ASSERT(entsrc->nblocks > 0);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/* Pick a memory block to read */
6927c478bd9Sstevel@tonic-gate 		block = oblock = srndpool[i] % entsrc->nblocks;
6937c478bd9Sstevel@tonic-gate 
69456f33205SJonathan Adams 		for (pmem = phys_install; pmem != NULL; pmem = pmem->ml_next) {
69556f33205SJonathan Adams 			if (block < pmem->ml_size / MEMBLOCKSIZE)
6967c478bd9Sstevel@tonic-gate 				break;
69756f33205SJonathan Adams 			block -= pmem->ml_size / MEMBLOCKSIZE;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		ASSERT(pmem != NULL);
7017c478bd9Sstevel@tonic-gate 
70256f33205SJonathan Adams 		offset = pmem->ml_address + block * MEMBLOCKSIZE;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		if (!address_in_memlist(phys_install, offset, MEMBLOCKSIZE)) {
7057c478bd9Sstevel@tonic-gate 			memlist_read_unlock();
7067c478bd9Sstevel@tonic-gate 			continue;
7077c478bd9Sstevel@tonic-gate 		}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		/*
710ae115bc7Smrj 		 * Do an initial check to see if the address is safe
711ae115bc7Smrj 		 */
712ae115bc7Smrj 		if (plat_hold_page(offset >> PAGESHIFT, PLAT_HOLD_NO_LOCK, NULL)
713ae115bc7Smrj 		    == PLAT_HOLD_FAIL) {
714ae115bc7Smrj 			memlist_read_unlock();
715ae115bc7Smrj 			continue;
716ae115bc7Smrj 		}
717ae115bc7Smrj 
718ae115bc7Smrj 		/*
7197c478bd9Sstevel@tonic-gate 		 * Figure out which page to load to read the
7207c478bd9Sstevel@tonic-gate 		 * memory block.  Load the page and compute the
7217c478bd9Sstevel@tonic-gate 		 * hash of the memory block.
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		len = MEMBLOCKSIZE;
7247c478bd9Sstevel@tonic-gate 		ts1 = gethrtime();
7257c478bd9Sstevel@tonic-gate 		HashInit(&ctx);
7267c478bd9Sstevel@tonic-gate 		while (len) {
7277c478bd9Sstevel@tonic-gate 			pfn = offset >> PAGESHIFT;
7287c478bd9Sstevel@tonic-gate 			poffset = offset & PAGEOFFSET;
7297c478bd9Sstevel@tonic-gate 			nbytes = PAGESIZE - poffset < len ?
7307c478bd9Sstevel@tonic-gate 			    PAGESIZE - poffset : len;
7317c478bd9Sstevel@tonic-gate 
732ae115bc7Smrj 			/*
733ae115bc7Smrj 			 * Re-check the offset, and lock the frame.  If the
734ae115bc7Smrj 			 * page was given away after the above check, we'll
735ae115bc7Smrj 			 * just bail out.
736ae115bc7Smrj 			 */
737ae115bc7Smrj 			if (plat_hold_page(pfn, PLAT_HOLD_LOCK, &pp) ==
738ae115bc7Smrj 			    PLAT_HOLD_FAIL)
739ae115bc7Smrj 				break;
740ae115bc7Smrj 
7417c478bd9Sstevel@tonic-gate 			hat_devload(kas.a_hat, entsrc->pmbuf,
7427c478bd9Sstevel@tonic-gate 			    PAGESIZE, pfn, PROT_READ,
7437c478bd9Sstevel@tonic-gate 			    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 			HashUpdate(&ctx, (uint8_t *)entsrc->pmbuf + poffset,
7467c478bd9Sstevel@tonic-gate 			    nbytes);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 			hat_unload(kas.a_hat, entsrc->pmbuf, PAGESIZE,
7497c478bd9Sstevel@tonic-gate 			    HAT_UNLOAD_UNLOCK);
7507c478bd9Sstevel@tonic-gate 
751ae115bc7Smrj 			plat_release_page(pp);
752ae115bc7Smrj 
7537c478bd9Sstevel@tonic-gate 			len -= nbytes;
7547c478bd9Sstevel@tonic-gate 			offset += nbytes;
7557c478bd9Sstevel@tonic-gate 		}
7567c478bd9Sstevel@tonic-gate 		/* We got our pages. Let the DR roll */
7577c478bd9Sstevel@tonic-gate 		memlist_read_unlock();
7587c478bd9Sstevel@tonic-gate 
759ae115bc7Smrj 		/* See if we had to bail out due to a page being given away */
760ae115bc7Smrj 		if (len)
761ae115bc7Smrj 			continue;
762ae115bc7Smrj 
7637c478bd9Sstevel@tonic-gate 		HashFinal(digest, &ctx);
7647c478bd9Sstevel@tonic-gate 		ts2 = gethrtime();
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		/*
7677c478bd9Sstevel@tonic-gate 		 * Compute the time it took to load and hash the
7687c478bd9Sstevel@tonic-gate 		 * block and compare it against the previous
7697c478bd9Sstevel@tonic-gate 		 * measurement. The delta of the time values
7707c478bd9Sstevel@tonic-gate 		 * provides a small amount of entropy.  The
7717c478bd9Sstevel@tonic-gate 		 * minimum of the first, second, and third order
7727c478bd9Sstevel@tonic-gate 		 * delta is used to estimate how much entropy
7737c478bd9Sstevel@tonic-gate 		 * is present.
7747c478bd9Sstevel@tonic-gate 		 */
7757c478bd9Sstevel@tonic-gate 		diff = ts2 - ts1;
7767c478bd9Sstevel@tonic-gate 		delta = diff - entsrc->last_diff;
7777c478bd9Sstevel@tonic-gate 		if (delta < 0)
7787c478bd9Sstevel@tonic-gate 			delta = -delta;
7797c478bd9Sstevel@tonic-gate 		delta2 = delta - entsrc->last_delta;
7807c478bd9Sstevel@tonic-gate 		if (delta2 < 0)
7817c478bd9Sstevel@tonic-gate 			delta2 = -delta2;
7827c478bd9Sstevel@tonic-gate 		delta3 = delta2 - entsrc->last_delta2;
7837c478bd9Sstevel@tonic-gate 		if (delta3 < 0)
7847c478bd9Sstevel@tonic-gate 			delta3 = -delta3;
7857c478bd9Sstevel@tonic-gate 		entsrc->last_diff = diff;
7867c478bd9Sstevel@tonic-gate 		entsrc->last_delta = delta;
7877c478bd9Sstevel@tonic-gate 		entsrc->last_delta2 = delta2;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		if (delta > delta2)
7907c478bd9Sstevel@tonic-gate 			delta = delta2;
7917c478bd9Sstevel@tonic-gate 		if (delta > delta3)
7927c478bd9Sstevel@tonic-gate 			delta = delta3;
7937c478bd9Sstevel@tonic-gate 		delta2 = 0;
7947c478bd9Sstevel@tonic-gate 		while (delta >>= 1)
7957c478bd9Sstevel@tonic-gate 			delta2++;
7967c478bd9Sstevel@tonic-gate 		ent += delta2;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		/*
7997c478bd9Sstevel@tonic-gate 		 * If the memory block has changed, credit the pool with
8007c478bd9Sstevel@tonic-gate 		 * the entropy estimate.  The entropy estimate is doubled
8017c478bd9Sstevel@tonic-gate 		 * because the single-bit checksum misses half the change
8027c478bd9Sstevel@tonic-gate 		 * on average.
8037c478bd9Sstevel@tonic-gate 		 */
8047c478bd9Sstevel@tonic-gate 		if (physmem_parity_update(entsrc->parity, oblock,
8057c478bd9Sstevel@tonic-gate 		    digest[0] & 1))
8067c478bd9Sstevel@tonic-gate 			ent += 2 * entsrc->entperblock;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		/* Add the entropy bytes to the pool */
8097c478bd9Sstevel@tonic-gate 		swrand_add_bytes(digest, HASHSIZE);
8107c478bd9Sstevel@tonic-gate 		swrand_add_bytes((uint8_t *)&ts1, sizeof (ts1));
8117c478bd9Sstevel@tonic-gate 		swrand_add_bytes((uint8_t *)&ts2, sizeof (ts2));
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	swrand_mix_pool(ent);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate static int
physmem_parity_update(uint8_t * parity_vec,uint32_t block,int parity)8187c478bd9Sstevel@tonic-gate physmem_parity_update(uint8_t *parity_vec, uint32_t block, int parity)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	/* Test and set the parity bit, return 1 if changed */
8217c478bd9Sstevel@tonic-gate 	if (parity == ((parity_vec[block >> 3] >> (block & 7)) & 1))
8227c478bd9Sstevel@tonic-gate 		return (0);
8237c478bd9Sstevel@tonic-gate 	parity_vec[block >> 3] ^= 1 << (block & 7);
8247c478bd9Sstevel@tonic-gate 	return (1);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /* Compute number of memory blocks available to scan */
8287c478bd9Sstevel@tonic-gate static void
physmem_count_blocks()8297c478bd9Sstevel@tonic-gate physmem_count_blocks()
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	struct memlist *pmem;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	memlist_read_lock();
8347c478bd9Sstevel@tonic-gate 	entsrc.nblocks = 0;
83556f33205SJonathan Adams 	for (pmem = phys_install; pmem != NULL; pmem = pmem->ml_next) {
83656f33205SJonathan Adams 		entsrc.nblocks += pmem->ml_size / MEMBLOCKSIZE;
8377c478bd9Sstevel@tonic-gate 		if (entsrc.nblocks > MAXMEMBLOCKS) {
8387c478bd9Sstevel@tonic-gate 			entsrc.nblocks = MAXMEMBLOCKS;
8397c478bd9Sstevel@tonic-gate 			break;
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 	memlist_read_unlock();
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate /*
8467c478bd9Sstevel@tonic-gate  * Dynamic Reconfiguration call-back functions
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /* ARGSUSED */
8507c478bd9Sstevel@tonic-gate static void
rnd_dr_callback_post_add(void * arg,pgcnt_t delta)8517c478bd9Sstevel@tonic-gate rnd_dr_callback_post_add(void *arg, pgcnt_t delta)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	/* More memory is available now, so update entsrc->nblocks. */
8547c478bd9Sstevel@tonic-gate 	physmem_count_blocks();
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /* Call-back routine invoked before the DR starts a memory removal. */
8587c478bd9Sstevel@tonic-gate /* ARGSUSED */
8597c478bd9Sstevel@tonic-gate static int
rnd_dr_callback_pre_del(void * arg,pgcnt_t delta)8607c478bd9Sstevel@tonic-gate rnd_dr_callback_pre_del(void *arg, pgcnt_t delta)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	return (0);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate /* Call-back routine invoked after the DR starts a memory removal. */
8667c478bd9Sstevel@tonic-gate /* ARGSUSED */
8677c478bd9Sstevel@tonic-gate static void
rnd_dr_callback_post_del(void * arg,pgcnt_t delta,int cancelled)8687c478bd9Sstevel@tonic-gate rnd_dr_callback_post_del(void *arg, pgcnt_t delta, int cancelled)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	/* Memory has shrunk, so update entsrc->nblocks. */
8717c478bd9Sstevel@tonic-gate 	physmem_count_blocks();
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /* Timeout handling to gather entropy from physmem events */
8757c478bd9Sstevel@tonic-gate static void
swrand_schedule_timeout(void)8767c478bd9Sstevel@tonic-gate swrand_schedule_timeout(void)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	clock_t ut;	/* time in microseconds */
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&srndpool_lock));
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * The new timeout value is taken from the pool of random bits.
8837c478bd9Sstevel@tonic-gate 	 * We're merely reading the first 32 bits from the pool here, not
8847c478bd9Sstevel@tonic-gate 	 * consuming any entropy.
8857c478bd9Sstevel@tonic-gate 	 * This routine is usually called right after stirring the pool, so
8867c478bd9Sstevel@tonic-gate 	 * srndpool[0] will have a *fresh* random value each time.
8877c478bd9Sstevel@tonic-gate 	 * The timeout multiplier value is a random value between 0.7 sec and
8887c478bd9Sstevel@tonic-gate 	 * 1.748575 sec (0.7 sec + 0xFFFFF microseconds).
8897c478bd9Sstevel@tonic-gate 	 * The new timeout is TIMEOUT_INTERVAL times that multiplier.
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 	ut = 700000 + (clock_t)(srndpool[0] & 0xFFFFF);
8927c478bd9Sstevel@tonic-gate 	rnd_timeout_id = timeout(rnd_handler, NULL,
8937c478bd9Sstevel@tonic-gate 	    TIMEOUT_INTERVAL * drv_usectohz(ut));
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8977c478bd9Sstevel@tonic-gate static void
rnd_handler(void * arg)8987c478bd9Sstevel@tonic-gate rnd_handler(void *arg)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	mutex_enter(&srndpool_lock);
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	physmem_ent_gen(&entsrc);
9037c478bd9Sstevel@tonic-gate 	if (snum_waiters > 0)
9047c478bd9Sstevel@tonic-gate 		cv_broadcast(&srndpool_read_cv);
9057c478bd9Sstevel@tonic-gate 	swrand_schedule_timeout();
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	mutex_exit(&srndpool_lock);
9087c478bd9Sstevel@tonic-gate }
909