xref: /titanic_50/usr/src/uts/common/crypto/io/swrand.c (revision a4faba164aa153855d621c694fc5aa75dd183b81)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Software based random number provider for the Kernel Cryptographic
28  * Framework (KCF). This provider periodically collects unpredictable input
29  * from external sources and processes it into a pool of entropy (randomness)
30  * in order to satisfy requests for random bits from kCF. It implements
31  * software-based mixing, extraction, and generation algorithms.
32  *
33  * A history note: The software-based algorithms in this file used to be
34  * part of the /dev/random driver.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/errno.h>
39 #include <sys/debug.h>
40 #include <vm/seg_kmem.h>
41 #include <vm/hat.h>
42 #include <sys/systm.h>
43 #include <sys/memlist.h>
44 #include <sys/cmn_err.h>
45 #include <sys/ksynch.h>
46 #include <sys/random.h>
47 #include <sys/ddi.h>
48 #include <sys/mman.h>
49 #include <sys/sysmacros.h>
50 #include <sys/mem_config.h>
51 #include <sys/time.h>
52 #include <sys/crypto/spi.h>
53 #include <sys/sha1.h>
54 #include <sys/sunddi.h>
55 #include <sys/modctl.h>
56 #include <sys/hold_page.h>
57 #include <rng/fips_random.h>
58 
59 #define	RNDPOOLSIZE		1024	/* Pool size in bytes */
60 #define	HASHBUFSIZE		64	/* Buffer size used for pool mixing */
61 #define	MAXMEMBLOCKS		16384	/* Number of memory blocks to scan */
62 #define	MEMBLOCKSIZE		4096	/* Size of memory block to read */
63 #define	MINEXTRACTBITS		160	/* Min entropy level for extraction */
64 #define	TIMEOUT_INTERVAL	5	/* Periodic mixing interval in secs */
65 
66 /* Hash-algo generic definitions. For now, they are SHA1's. */
67 #define	HASHSIZE		20
68 #define	HASH_CTX		SHA1_CTX
69 #define	HashInit(ctx)		SHA1Init((ctx))
70 #define	HashUpdate(ctx, p, s)	SHA1Update((ctx), (p), (s))
71 #define	HashFinal(d, ctx)	SHA1Final((d), (ctx))
72 
73 /* Physical memory entropy source */
74 typedef struct physmem_entsrc_s {
75 	uint8_t *parity;		/* parity bit vector */
76 	caddr_t pmbuf;			/* buffer for memory block */
77 	uint32_t nblocks;		/* number of  memory blocks */
78 	int entperblock;		/* entropy bits per block read */
79 	hrtime_t last_diff;		/* previous time to process a block */
80 	hrtime_t last_delta;		/* previous time delta */
81 	hrtime_t last_delta2;		/* previous 2nd order time delta */
82 } physmem_entsrc_t;
83 
84 static uint32_t srndpool[RNDPOOLSIZE/4];	/* Pool of random bits */
85 static uint32_t buffer[RNDPOOLSIZE/4];	/* entropy mixed in later */
86 static int buffer_bytes;		/* bytes written to buffer */
87 static uint32_t entropy_bits;		/* pool's current amount of entropy */
88 static kmutex_t srndpool_lock;		/* protects r/w accesses to the pool, */
89 					/* and the global variables */
90 static kmutex_t buffer_lock;		/* protects r/w accesses to buffer */
91 static kcondvar_t srndpool_read_cv;	/* serializes poll/read syscalls */
92 static int pindex;			/* Global index for adding/extracting */
93 					/* from the pool */
94 static int bstart, bindex;		/* Global vars for adding/extracting */
95 					/* from the buffer */
96 static uint8_t leftover[HASHSIZE];	/* leftover output */
97 static uint32_t	swrand_XKEY[6];		/* one extra word for getentropy */
98 static int leftover_bytes;		/* leftover length */
99 static uint32_t previous_bytes[HASHSIZE/BYTES_IN_WORD];	/* prev random bytes */
100 
101 static physmem_entsrc_t entsrc;		/* Physical mem as an entropy source */
102 static timeout_id_t rnd_timeout_id;
103 static int snum_waiters;
104 static crypto_kcf_provider_handle_t swrand_prov_handle = NULL;
105 swrand_stats_t swrand_stats;
106 
107 static int physmem_ent_init(physmem_entsrc_t *);
108 static void physmem_ent_fini(physmem_entsrc_t *);
109 static void physmem_ent_gen(physmem_entsrc_t *);
110 static int physmem_parity_update(uint8_t *, uint32_t, int);
111 static void physmem_count_blocks();
112 static void rnd_dr_callback_post_add(void *, pgcnt_t);
113 static int rnd_dr_callback_pre_del(void *, pgcnt_t);
114 static void rnd_dr_callback_post_del(void *, pgcnt_t, int);
115 static void rnd_handler(void *arg);
116 static void swrand_init();
117 static void swrand_schedule_timeout(void);
118 static int swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t);
119 static void swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est);
120 static void swrand_add_entropy_later(uint8_t *ptr, size_t len);
121 
122 /* Dynamic Reconfiguration related declarations */
123 kphysm_setup_vector_t rnd_dr_callback_vec = {
124 	KPHYSM_SETUP_VECTOR_VERSION,
125 	rnd_dr_callback_post_add,
126 	rnd_dr_callback_pre_del,
127 	rnd_dr_callback_post_del
128 };
129 
130 extern struct mod_ops mod_cryptoops;
131 
132 /*
133  * Module linkage information for the kernel.
134  */
135 static struct modlcrypto modlcrypto = {
136 	&mod_cryptoops,
137 	"Kernel Random number Provider"
138 };
139 
140 static struct modlinkage modlinkage = {
141 	MODREV_1,
142 	(void *)&modlcrypto,
143 	NULL
144 };
145 
146 /*
147  * CSPI information (entry points, provider info, etc.)
148  */
149 static void swrand_provider_status(crypto_provider_handle_t, uint_t *);
150 
151 static crypto_control_ops_t swrand_control_ops = {
152 	swrand_provider_status
153 };
154 
155 static int swrand_seed_random(crypto_provider_handle_t, crypto_session_id_t,
156     uchar_t *, size_t, uint_t, uint32_t, crypto_req_handle_t);
157 static int swrand_generate_random(crypto_provider_handle_t,
158     crypto_session_id_t, uchar_t *, size_t, crypto_req_handle_t);
159 
160 static crypto_random_number_ops_t swrand_random_number_ops = {
161 	swrand_seed_random,
162 	swrand_generate_random
163 };
164 
165 static crypto_ops_t swrand_crypto_ops = {
166 	&swrand_control_ops,
167 	NULL,
168 	NULL,
169 	NULL,
170 	NULL,
171 	NULL,
172 	NULL,
173 	NULL,
174 	&swrand_random_number_ops,
175 	NULL,
176 	NULL,
177 	NULL,
178 	NULL,
179 	NULL
180 };
181 
182 static crypto_provider_info_t swrand_prov_info = {
183 	CRYPTO_SPI_VERSION_1,
184 	"Kernel Random Number Provider",
185 	CRYPTO_SW_PROVIDER,
186 	{&modlinkage},
187 	NULL,
188 	&swrand_crypto_ops,
189 	0,
190 	NULL
191 };
192 
193 int
194 _init(void)
195 {
196 	int ret;
197 	hrtime_t ts;
198 	time_t now;
199 
200 	/*
201 	 * Register with KCF. If the registration fails, return error.
202 	 */
203 	if ((ret = crypto_register_provider(&swrand_prov_info,
204 	    &swrand_prov_handle)) != CRYPTO_SUCCESS) {
205 		cmn_err(CE_WARN, "swrand : Kernel Random Number Provider "
206 		    "disabled for /dev/random use");
207 		return (EACCES);
208 	}
209 
210 	mutex_init(&srndpool_lock, NULL, MUTEX_DEFAULT, NULL);
211 	mutex_init(&buffer_lock, NULL, MUTEX_DEFAULT, NULL);
212 	cv_init(&srndpool_read_cv, NULL, CV_DEFAULT, NULL);
213 	entropy_bits = 0;
214 	pindex = 0;
215 	bindex = 0;
216 	bstart = 0;
217 	snum_waiters = 0;
218 	leftover_bytes = 0;
219 	buffer_bytes = 0;
220 
221 	/*
222 	 * Initialize the pool using
223 	 * . 2 unpredictable times: high resolution time since the boot-time,
224 	 *   and the current time-of-the day.
225 	 * . The initial physical memory state.
226 	 */
227 	ts = gethrtime();
228 	swrand_add_entropy((uint8_t *)&ts, sizeof (ts), 0);
229 
230 	(void) drv_getparm(TIME, &now);
231 	swrand_add_entropy((uint8_t *)&now, sizeof (now), 0);
232 
233 	ret = kphysm_setup_func_register(&rnd_dr_callback_vec, NULL);
234 	ASSERT(ret == 0);
235 
236 	if (physmem_ent_init(&entsrc) != 0) {
237 		mutex_destroy(&srndpool_lock);
238 		mutex_destroy(&buffer_lock);
239 		cv_destroy(&srndpool_read_cv);
240 		(void) crypto_unregister_provider(swrand_prov_handle);
241 		return (ENOMEM);
242 	}
243 
244 	if ((ret = mod_install(&modlinkage)) != 0) {
245 		mutex_destroy(&srndpool_lock);
246 		mutex_destroy(&buffer_lock);
247 		cv_destroy(&srndpool_read_cv);
248 		physmem_ent_fini(&entsrc);
249 		(void) crypto_unregister_provider(swrand_prov_handle);
250 		return (ret);
251 	}
252 
253 	/* Schedule periodic mixing of the pool. */
254 	mutex_enter(&srndpool_lock);
255 	swrand_schedule_timeout();
256 	mutex_exit(&srndpool_lock);
257 	(void) swrand_get_entropy((uint8_t *)swrand_XKEY, HASHSIZE, B_TRUE);
258 	bcopy(swrand_XKEY, previous_bytes, HASHSIZE);
259 
260 	return (0);
261 }
262 
263 int
264 _info(struct modinfo *modinfop)
265 {
266 	return (mod_info(&modlinkage, modinfop));
267 }
268 
269 /*
270  * Control entry points.
271  */
272 /* ARGSUSED */
273 static void
274 swrand_provider_status(crypto_provider_handle_t provider, uint_t *status)
275 {
276 	*status = CRYPTO_PROVIDER_READY;
277 }
278 
279 /*
280  * Random number entry points.
281  */
282 /* ARGSUSED */
283 static int
284 swrand_seed_random(crypto_provider_handle_t provider, crypto_session_id_t sid,
285     uchar_t *buf, size_t len, uint_t entropy_est, uint32_t flags,
286     crypto_req_handle_t req)
287 {
288 	/* The entropy estimate is always 0 in this path */
289 	if (flags & CRYPTO_SEED_NOW)
290 		swrand_add_entropy(buf, len, 0);
291 	else
292 		swrand_add_entropy_later(buf, len);
293 	return (CRYPTO_SUCCESS);
294 }
295 
296 /* ARGSUSED */
297 static int
298 swrand_generate_random(crypto_provider_handle_t provider,
299     crypto_session_id_t sid, uchar_t *buf, size_t len, crypto_req_handle_t req)
300 {
301 	if (crypto_kmflag(req) == KM_NOSLEEP)
302 		(void) swrand_get_entropy(buf, len, B_TRUE);
303 	else
304 		(void) swrand_get_entropy(buf, len, B_FALSE);
305 
306 	return (CRYPTO_SUCCESS);
307 }
308 
309 /*
310  * Extraction of entropy from the pool.
311  *
312  * Returns "len" random bytes in *ptr.
313  * Try to gather some more entropy by calling physmem_ent_gen() when less than
314  * MINEXTRACTBITS are present in the pool.
315  * Will block if not enough entropy was available and the call is blocking.
316  */
317 static int
318 swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t nonblock)
319 {
320 	int i, bytes;
321 	HASH_CTX hashctx;
322 	uint8_t digest[HASHSIZE], *pool;
323 	uint32_t tempout[HASHSIZE/BYTES_IN_WORD];
324 	int size;
325 
326 	mutex_enter(&srndpool_lock);
327 	if (leftover_bytes > 0) {
328 		bytes = min(len, leftover_bytes);
329 		bcopy(leftover, ptr, bytes);
330 		len -= bytes;
331 		ptr += bytes;
332 		leftover_bytes -= bytes;
333 		if (leftover_bytes > 0)
334 			ovbcopy(leftover+bytes, leftover, leftover_bytes);
335 	}
336 
337 	while (len > 0) {
338 		/* Check if there is enough entropy */
339 		while (entropy_bits < MINEXTRACTBITS) {
340 
341 			physmem_ent_gen(&entsrc);
342 
343 			if (entropy_bits < MINEXTRACTBITS &&
344 			    nonblock == B_TRUE) {
345 				mutex_exit(&srndpool_lock);
346 				return (EAGAIN);
347 			}
348 
349 			if (entropy_bits < MINEXTRACTBITS) {
350 				ASSERT(nonblock == B_FALSE);
351 				snum_waiters++;
352 				if (cv_wait_sig(&srndpool_read_cv,
353 				    &srndpool_lock) == 0) {
354 					snum_waiters--;
355 					mutex_exit(&srndpool_lock);
356 					return (EINTR);
357 				}
358 				snum_waiters--;
359 			}
360 		}
361 
362 		/* Figure out how many bytes to extract */
363 		bytes = min(HASHSIZE, len);
364 		bytes = min(bytes, entropy_bits/8);
365 		entropy_bits -= bytes * 8;
366 		BUMP_SWRAND_STATS(ss_entOut, bytes * 8);
367 		swrand_stats.ss_entEst = entropy_bits;
368 
369 		/* Extract entropy by hashing pool content */
370 		HashInit(&hashctx);
371 		HashUpdate(&hashctx, (uint8_t *)srndpool, RNDPOOLSIZE);
372 		HashFinal(digest, &hashctx);
373 
374 		/*
375 		 * Feed the digest back into the pool so next
376 		 * extraction produces different result
377 		 */
378 		pool = (uint8_t *)srndpool;
379 		for (i = 0; i < HASHSIZE; i++) {
380 			pool[pindex++] ^= digest[i];
381 			/* pindex modulo RNDPOOLSIZE */
382 			pindex &= (RNDPOOLSIZE - 1);
383 		}
384 
385 		/* LINTED E_BAD_PTR_CAST_ALIGN */
386 		fips_random_inner(swrand_XKEY, tempout, (uint32_t *)digest);
387 
388 		if (len >= HASHSIZE) {
389 			size = HASHSIZE;
390 		} else {
391 			size = min(bytes, HASHSIZE);
392 		}
393 
394 		/*
395 		 * FIPS 140-2: Continuous RNG test - each generation
396 		 * of an n-bit block shall be compared with the previously
397 		 * generated block. Test shall fail if any two compared
398 		 * n-bit blocks are equal.
399 		 */
400 		for (i = 0; i < HASHSIZE/BYTES_IN_WORD; i++) {
401 			if (tempout[i] != previous_bytes[i])
402 				break;
403 		}
404 
405 		if (i == HASHSIZE/BYTES_IN_WORD) {
406 			cmn_err(CE_WARN, "swrand: The value of 160-bit block "
407 			    "random bytes are same as the previous one.\n");
408 			/* discard random bytes and return error */
409 			return (EIO);
410 		}
411 
412 		bcopy(tempout, previous_bytes, HASHSIZE);
413 
414 		bcopy(tempout, ptr, size);
415 		if (len < HASHSIZE) {
416 			leftover_bytes = HASHSIZE - bytes;
417 			bcopy((uint8_t *)tempout + bytes, leftover,
418 			    leftover_bytes);
419 		}
420 
421 		ptr += size;
422 		len -= size;
423 		BUMP_SWRAND_STATS(ss_bytesOut, size);
424 	}
425 
426 	/* Zero out sensitive information */
427 	bzero(digest, HASHSIZE);
428 	bzero(tempout, HASHSIZE);
429 	mutex_exit(&srndpool_lock);
430 	return (0);
431 }
432 
433 #define	SWRAND_ADD_BYTES(ptr, len, i, pool)		\
434 	ASSERT((ptr) != NULL && (len) > 0);		\
435 	BUMP_SWRAND_STATS(ss_bytesIn, (len));		\
436 	while ((len)--) {				\
437 		(pool)[(i)++] ^= *(ptr);		\
438 		(ptr)++;				\
439 		(i) &= (RNDPOOLSIZE - 1);		\
440 	}
441 
442 /* Write some more user-provided entropy to the pool */
443 static void
444 swrand_add_bytes(uint8_t *ptr, size_t len)
445 {
446 	uint8_t *pool = (uint8_t *)srndpool;
447 
448 	ASSERT(MUTEX_HELD(&srndpool_lock));
449 	SWRAND_ADD_BYTES(ptr, len, pindex, pool);
450 }
451 
452 /*
453  * Add bytes to buffer. Adding the buffer to the random pool
454  * is deferred until the random pool is mixed.
455  */
456 static void
457 swrand_add_bytes_later(uint8_t *ptr, size_t len)
458 {
459 	uint8_t *pool = (uint8_t *)buffer;
460 
461 	ASSERT(MUTEX_HELD(&buffer_lock));
462 	SWRAND_ADD_BYTES(ptr, len, bindex, pool);
463 	buffer_bytes += len;
464 }
465 
466 #undef SWRAND_ADD_BYTES
467 
468 /* Mix the pool */
469 static void
470 swrand_mix_pool(uint16_t entropy_est)
471 {
472 	int i, j, k, start;
473 	HASH_CTX hashctx;
474 	uint8_t digest[HASHSIZE];
475 	uint8_t *pool = (uint8_t *)srndpool;
476 	uint8_t *bp = (uint8_t *)buffer;
477 
478 	ASSERT(MUTEX_HELD(&srndpool_lock));
479 
480 	/* add deferred bytes */
481 	mutex_enter(&buffer_lock);
482 	if (buffer_bytes > 0) {
483 		if (buffer_bytes >= RNDPOOLSIZE) {
484 			for (i = 0; i < RNDPOOLSIZE/4; i++) {
485 				srndpool[i] ^= buffer[i];
486 				buffer[i] = 0;
487 			}
488 			bstart = bindex = 0;
489 		} else {
490 			for (i = 0; i < buffer_bytes; i++) {
491 				pool[pindex++] ^= bp[bstart];
492 				bp[bstart++] = 0;
493 				pindex &= (RNDPOOLSIZE - 1);
494 				bstart &= (RNDPOOLSIZE - 1);
495 			}
496 			ASSERT(bstart == bindex);
497 		}
498 		buffer_bytes = 0;
499 	}
500 	mutex_exit(&buffer_lock);
501 
502 	start = 0;
503 	for (i = 0; i < RNDPOOLSIZE/HASHSIZE + 1; i++) {
504 		HashInit(&hashctx);
505 
506 		/* Hash a buffer centered on a block in the pool */
507 		if (start + HASHBUFSIZE <= RNDPOOLSIZE)
508 			HashUpdate(&hashctx, &pool[start], HASHBUFSIZE);
509 		else {
510 			HashUpdate(&hashctx, &pool[start],
511 			    RNDPOOLSIZE - start);
512 			HashUpdate(&hashctx, pool,
513 			    HASHBUFSIZE - RNDPOOLSIZE + start);
514 		}
515 		HashFinal(digest, &hashctx);
516 
517 		/* XOR the hash result back into the block */
518 		k = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
519 		for (j = 0; j < HASHSIZE; j++) {
520 			pool[k++] ^= digest[j];
521 			k &= (RNDPOOLSIZE - 1);
522 		}
523 
524 		/* Slide the hash buffer and repeat with next block */
525 		start = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
526 	}
527 
528 	entropy_bits += entropy_est;
529 	if (entropy_bits > RNDPOOLSIZE * 8)
530 		entropy_bits = RNDPOOLSIZE * 8;
531 
532 	swrand_stats.ss_entEst = entropy_bits;
533 	BUMP_SWRAND_STATS(ss_entIn, entropy_est);
534 }
535 
536 static void
537 swrand_add_entropy_later(uint8_t *ptr, size_t len)
538 {
539 	mutex_enter(&buffer_lock);
540 	swrand_add_bytes_later(ptr, len);
541 	mutex_exit(&buffer_lock);
542 }
543 
544 static void
545 swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est)
546 {
547 	mutex_enter(&srndpool_lock);
548 	swrand_add_bytes(ptr, len);
549 	swrand_mix_pool(entropy_est);
550 	mutex_exit(&srndpool_lock);
551 }
552 
553 /*
554  * The physmem_* routines below generate entropy by reading blocks of
555  * physical memory.  Entropy is gathered in a couple of ways:
556  *
557  *  - By reading blocks of physical memory and detecting if changes
558  *    occurred in the blocks read.
559  *
560  *  - By measuring the time it takes to load and hash a block of memory
561  *    and computing the differences in the measured time.
562  *
563  * The first method was used in the CryptoRand implementation.  Physical
564  * memory is divided into blocks of fixed size.  A block of memory is
565  * chosen from the possible blocks and hashed to produce a digest.  This
566  * digest is then mixed into the pool.  A single bit from the digest is
567  * used as a parity bit or "checksum" and compared against the previous
568  * "checksum" computed for the block.  If the single-bit checksum has not
569  * changed, no entropy is credited to the pool.  If there is a change,
570  * then the assumption is that at least one bit in the block has changed.
571  * The possible locations within the memory block of where the bit change
572  * occurred is used as a measure of entropy.  For example, if a block
573  * size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of
574  * entropy is available.  Because the single-bit checksum will miss half
575  * of the changes, the amount of entropy credited to the pool is doubled
576  * when a change is detected.  With a 4096 byte block size, a block
577  * change will add a total of 30 bits of entropy to the pool.
578  *
579  * The second method measures the amount of time it takes to read and
580  * hash a physical memory block (as described above).  The time measured
581  * can vary depending on system load, scheduling and other factors.
582  * Differences between consecutive measurements are computed to come up
583  * with an entropy estimate.  The first, second, and third order delta is
584  * calculated to determine the minimum delta value.  The number of bits
585  * present in this minimum delta value is the entropy estimate.  This
586  * entropy estimation technique using time deltas is similar to that used
587  * in /dev/random implementations from Linux/BSD.
588  */
589 
590 static int
591 physmem_ent_init(physmem_entsrc_t *entsrc)
592 {
593 	uint8_t *ptr;
594 	int i;
595 
596 	bzero(entsrc, sizeof (*entsrc));
597 
598 	/*
599 	 * The maximum entropy amount in bits per block of memory read is
600 	 * log_2(MEMBLOCKSIZE * 8);
601 	 */
602 	i = MEMBLOCKSIZE << 3;
603 	while (i >>= 1)
604 		entsrc->entperblock++;
605 
606 	/* Initialize entsrc->nblocks */
607 	physmem_count_blocks();
608 
609 	if (entsrc->nblocks == 0) {
610 		cmn_err(CE_WARN, "no memory blocks to scan!");
611 		return (-1);
612 	}
613 
614 	/* Allocate space for the parity vector and memory page */
615 	entsrc->parity = kmem_alloc(howmany(entsrc->nblocks, 8),
616 	    KM_SLEEP);
617 	entsrc->pmbuf = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
618 
619 
620 	/* Initialize parity vector with bits from the pool */
621 	i = howmany(entsrc->nblocks, 8);
622 	ptr = entsrc->parity;
623 	while (i > 0) {
624 		if (i > RNDPOOLSIZE) {
625 			bcopy(srndpool, ptr, RNDPOOLSIZE);
626 			mutex_enter(&srndpool_lock);
627 			swrand_mix_pool(0);
628 			mutex_exit(&srndpool_lock);
629 			ptr += RNDPOOLSIZE;
630 			i -= RNDPOOLSIZE;
631 		} else {
632 			bcopy(srndpool, ptr, i);
633 			break;
634 		}
635 	}
636 
637 	/* Generate some entropy to further initialize the pool */
638 	mutex_enter(&srndpool_lock);
639 	physmem_ent_gen(entsrc);
640 	entropy_bits = 0;
641 	mutex_exit(&srndpool_lock);
642 
643 	return (0);
644 }
645 
646 static void
647 physmem_ent_fini(physmem_entsrc_t *entsrc)
648 {
649 	if (entsrc->pmbuf != NULL)
650 		vmem_free(heap_arena, entsrc->pmbuf, PAGESIZE);
651 	if (entsrc->parity != NULL)
652 		kmem_free(entsrc->parity, howmany(entsrc->nblocks, 8));
653 	bzero(entsrc, sizeof (*entsrc));
654 }
655 
656 static void
657 physmem_ent_gen(physmem_entsrc_t *entsrc)
658 {
659 	struct memlist *pmem;
660 	offset_t offset, poffset;
661 	pfn_t pfn;
662 	int i, nbytes, len, ent = 0;
663 	uint32_t block, oblock;
664 	hrtime_t ts1, ts2, diff, delta, delta2, delta3;
665 	uint8_t digest[HASHSIZE];
666 	HASH_CTX ctx;
667 	page_t *pp;
668 
669 	/*
670 	 * Use each 32-bit quantity in the pool to pick a memory
671 	 * block to read.
672 	 */
673 	for (i = 0; i < RNDPOOLSIZE/4; i++) {
674 
675 		/* If the pool is "full", stop after one block */
676 		if (entropy_bits + ent >= RNDPOOLSIZE * 8) {
677 			if (i > 0)
678 				break;
679 		}
680 
681 		/*
682 		 * This lock protects reading of phys_install.
683 		 * Any changes to this list, by DR, are done while
684 		 * holding this lock. So, holding this lock is sufficient
685 		 * to handle DR also.
686 		 */
687 		memlist_read_lock();
688 
689 		/* We're left with less than 4K of memory after DR */
690 		ASSERT(entsrc->nblocks > 0);
691 
692 		/* Pick a memory block to read */
693 		block = oblock = srndpool[i] % entsrc->nblocks;
694 
695 		for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
696 			if (block < pmem->size / MEMBLOCKSIZE)
697 				break;
698 			block -= pmem->size / MEMBLOCKSIZE;
699 		}
700 
701 		ASSERT(pmem != NULL);
702 
703 		offset = pmem->address + block * MEMBLOCKSIZE;
704 
705 		if (!address_in_memlist(phys_install, offset, MEMBLOCKSIZE)) {
706 			memlist_read_unlock();
707 			continue;
708 		}
709 
710 		/*
711 		 * Do an initial check to see if the address is safe
712 		 */
713 		if (plat_hold_page(offset >> PAGESHIFT, PLAT_HOLD_NO_LOCK, NULL)
714 		    == PLAT_HOLD_FAIL) {
715 			memlist_read_unlock();
716 			continue;
717 		}
718 
719 		/*
720 		 * Figure out which page to load to read the
721 		 * memory block.  Load the page and compute the
722 		 * hash of the memory block.
723 		 */
724 		len = MEMBLOCKSIZE;
725 		ts1 = gethrtime();
726 		HashInit(&ctx);
727 		while (len) {
728 			pfn = offset >> PAGESHIFT;
729 			poffset = offset & PAGEOFFSET;
730 			nbytes = PAGESIZE - poffset < len ?
731 			    PAGESIZE - poffset : len;
732 
733 			/*
734 			 * Re-check the offset, and lock the frame.  If the
735 			 * page was given away after the above check, we'll
736 			 * just bail out.
737 			 */
738 			if (plat_hold_page(pfn, PLAT_HOLD_LOCK, &pp) ==
739 			    PLAT_HOLD_FAIL)
740 				break;
741 
742 			hat_devload(kas.a_hat, entsrc->pmbuf,
743 			    PAGESIZE, pfn, PROT_READ,
744 			    HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
745 
746 			HashUpdate(&ctx, (uint8_t *)entsrc->pmbuf + poffset,
747 			    nbytes);
748 
749 			hat_unload(kas.a_hat, entsrc->pmbuf, PAGESIZE,
750 			    HAT_UNLOAD_UNLOCK);
751 
752 			plat_release_page(pp);
753 
754 			len -= nbytes;
755 			offset += nbytes;
756 		}
757 		/* We got our pages. Let the DR roll */
758 		memlist_read_unlock();
759 
760 		/* See if we had to bail out due to a page being given away */
761 		if (len)
762 			continue;
763 
764 		HashFinal(digest, &ctx);
765 		ts2 = gethrtime();
766 
767 		/*
768 		 * Compute the time it took to load and hash the
769 		 * block and compare it against the previous
770 		 * measurement. The delta of the time values
771 		 * provides a small amount of entropy.  The
772 		 * minimum of the first, second, and third order
773 		 * delta is used to estimate how much entropy
774 		 * is present.
775 		 */
776 		diff = ts2 - ts1;
777 		delta = diff - entsrc->last_diff;
778 		if (delta < 0)
779 			delta = -delta;
780 		delta2 = delta - entsrc->last_delta;
781 		if (delta2 < 0)
782 			delta2 = -delta2;
783 		delta3 = delta2 - entsrc->last_delta2;
784 		if (delta3 < 0)
785 			delta3 = -delta3;
786 		entsrc->last_diff = diff;
787 		entsrc->last_delta = delta;
788 		entsrc->last_delta2 = delta2;
789 
790 		if (delta > delta2)
791 			delta = delta2;
792 		if (delta > delta3)
793 			delta = delta3;
794 		delta2 = 0;
795 		while (delta >>= 1)
796 			delta2++;
797 		ent += delta2;
798 
799 		/*
800 		 * If the memory block has changed, credit the pool with
801 		 * the entropy estimate.  The entropy estimate is doubled
802 		 * because the single-bit checksum misses half the change
803 		 * on average.
804 		 */
805 		if (physmem_parity_update(entsrc->parity, oblock,
806 		    digest[0] & 1))
807 			ent += 2 * entsrc->entperblock;
808 
809 		/* Add the entropy bytes to the pool */
810 		swrand_add_bytes(digest, HASHSIZE);
811 		swrand_add_bytes((uint8_t *)&ts1, sizeof (ts1));
812 		swrand_add_bytes((uint8_t *)&ts2, sizeof (ts2));
813 	}
814 
815 	swrand_mix_pool(ent);
816 }
817 
818 static int
819 physmem_parity_update(uint8_t *parity_vec, uint32_t block, int parity)
820 {
821 	/* Test and set the parity bit, return 1 if changed */
822 	if (parity == ((parity_vec[block >> 3] >> (block & 7)) & 1))
823 		return (0);
824 	parity_vec[block >> 3] ^= 1 << (block & 7);
825 	return (1);
826 }
827 
828 /* Compute number of memory blocks available to scan */
829 static void
830 physmem_count_blocks()
831 {
832 	struct memlist *pmem;
833 
834 	memlist_read_lock();
835 	entsrc.nblocks = 0;
836 	for (pmem = phys_install; pmem != NULL; pmem = pmem->next) {
837 		entsrc.nblocks += pmem->size / MEMBLOCKSIZE;
838 		if (entsrc.nblocks > MAXMEMBLOCKS) {
839 			entsrc.nblocks = MAXMEMBLOCKS;
840 			break;
841 		}
842 	}
843 	memlist_read_unlock();
844 }
845 
846 /*
847  * Dynamic Reconfiguration call-back functions
848  */
849 
850 /* ARGSUSED */
851 static void
852 rnd_dr_callback_post_add(void *arg, pgcnt_t delta)
853 {
854 	/* More memory is available now, so update entsrc->nblocks. */
855 	physmem_count_blocks();
856 }
857 
858 /* Call-back routine invoked before the DR starts a memory removal. */
859 /* ARGSUSED */
860 static int
861 rnd_dr_callback_pre_del(void *arg, pgcnt_t delta)
862 {
863 	return (0);
864 }
865 
866 /* Call-back routine invoked after the DR starts a memory removal. */
867 /* ARGSUSED */
868 static void
869 rnd_dr_callback_post_del(void *arg, pgcnt_t delta, int cancelled)
870 {
871 	/* Memory has shrunk, so update entsrc->nblocks. */
872 	physmem_count_blocks();
873 }
874 
875 /* Timeout handling to gather entropy from physmem events */
876 static void
877 swrand_schedule_timeout(void)
878 {
879 	clock_t ut;	/* time in microseconds */
880 
881 	ASSERT(MUTEX_HELD(&srndpool_lock));
882 	/*
883 	 * The new timeout value is taken from the pool of random bits.
884 	 * We're merely reading the first 32 bits from the pool here, not
885 	 * consuming any entropy.
886 	 * This routine is usually called right after stirring the pool, so
887 	 * srndpool[0] will have a *fresh* random value each time.
888 	 * The timeout multiplier value is a random value between 0.7 sec and
889 	 * 1.748575 sec (0.7 sec + 0xFFFFF microseconds).
890 	 * The new timeout is TIMEOUT_INTERVAL times that multiplier.
891 	 */
892 	ut = 700000 + (clock_t)(srndpool[0] & 0xFFFFF);
893 	rnd_timeout_id = timeout(rnd_handler, NULL,
894 	    TIMEOUT_INTERVAL * drv_usectohz(ut));
895 }
896 
897 /*ARGSUSED*/
898 static void
899 rnd_handler(void *arg)
900 {
901 	mutex_enter(&srndpool_lock);
902 
903 	physmem_ent_gen(&entsrc);
904 	if (snum_waiters > 0)
905 		cv_broadcast(&srndpool_read_cv);
906 	swrand_schedule_timeout();
907 
908 	mutex_exit(&srndpool_lock);
909 }
910 
911 /*
912  * Swrand Power-Up Self-Test
913  */
914 void
915 swrand_POST(int *rc)
916 {
917 
918 	*rc = fips_rng_post();
919 
920 }
921