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 /*
2211413Sopensolaris@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
_init(void)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 mutex_init(&srndpool_lock, NULL, MUTEX_DEFAULT, NULL);
2101920Smcpowers mutex_init(&buffer_lock, NULL, MUTEX_DEFAULT, NULL);
2110Sstevel@tonic-gate cv_init(&srndpool_read_cv, NULL, CV_DEFAULT, NULL);
2120Sstevel@tonic-gate entropy_bits = 0;
2130Sstevel@tonic-gate pindex = 0;
2141920Smcpowers bindex = 0;
2151920Smcpowers bstart = 0;
2160Sstevel@tonic-gate snum_waiters = 0;
2170Sstevel@tonic-gate leftover_bytes = 0;
2181920Smcpowers buffer_bytes = 0;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate * Initialize the pool using
2220Sstevel@tonic-gate * . 2 unpredictable times: high resolution time since the boot-time,
2230Sstevel@tonic-gate * and the current time-of-the day.
2240Sstevel@tonic-gate * . The initial physical memory state.
2250Sstevel@tonic-gate */
2260Sstevel@tonic-gate ts = gethrtime();
2270Sstevel@tonic-gate swrand_add_entropy((uint8_t *)&ts, sizeof (ts), 0);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate (void) drv_getparm(TIME, &now);
2300Sstevel@tonic-gate swrand_add_entropy((uint8_t *)&now, sizeof (now), 0);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate ret = kphysm_setup_func_register(&rnd_dr_callback_vec, NULL);
2330Sstevel@tonic-gate ASSERT(ret == 0);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate if (physmem_ent_init(&entsrc) != 0) {
236*11751SAnthony.Scarpino@Sun.COM ret = ENOMEM;
237*11751SAnthony.Scarpino@Sun.COM goto exit1;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
240*11751SAnthony.Scarpino@Sun.COM if ((ret = mod_install(&modlinkage)) != 0)
241*11751SAnthony.Scarpino@Sun.COM goto exit2;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate /* Schedule periodic mixing of the pool. */
2440Sstevel@tonic-gate mutex_enter(&srndpool_lock);
2450Sstevel@tonic-gate swrand_schedule_timeout();
2460Sstevel@tonic-gate mutex_exit(&srndpool_lock);
2478029SHai-May.Chao@Sun.COM (void) swrand_get_entropy((uint8_t *)swrand_XKEY, HASHSIZE, B_TRUE);
2488029SHai-May.Chao@Sun.COM bcopy(swrand_XKEY, previous_bytes, HASHSIZE);
2490Sstevel@tonic-gate
250*11751SAnthony.Scarpino@Sun.COM /* Register with KCF. If the registration fails, return error. */
251*11751SAnthony.Scarpino@Sun.COM if (crypto_register_provider(&swrand_prov_info, &swrand_prov_handle)) {
252*11751SAnthony.Scarpino@Sun.COM (void) mod_remove(&modlinkage);
253*11751SAnthony.Scarpino@Sun.COM ret = EACCES;
254*11751SAnthony.Scarpino@Sun.COM goto exit2;
255*11751SAnthony.Scarpino@Sun.COM }
256*11751SAnthony.Scarpino@Sun.COM
2570Sstevel@tonic-gate return (0);
258*11751SAnthony.Scarpino@Sun.COM
259*11751SAnthony.Scarpino@Sun.COM exit2:
260*11751SAnthony.Scarpino@Sun.COM physmem_ent_fini(&entsrc);
261*11751SAnthony.Scarpino@Sun.COM exit1:
262*11751SAnthony.Scarpino@Sun.COM mutex_destroy(&srndpool_lock);
263*11751SAnthony.Scarpino@Sun.COM mutex_destroy(&buffer_lock);
264*11751SAnthony.Scarpino@Sun.COM cv_destroy(&srndpool_read_cv);
265*11751SAnthony.Scarpino@Sun.COM return (ret);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2690Sstevel@tonic-gate _info(struct modinfo *modinfop)
2700Sstevel@tonic-gate {
2710Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate * Control entry points.
2760Sstevel@tonic-gate */
2770Sstevel@tonic-gate /* ARGSUSED */
2780Sstevel@tonic-gate static void
swrand_provider_status(crypto_provider_handle_t provider,uint_t * status)2790Sstevel@tonic-gate swrand_provider_status(crypto_provider_handle_t provider, uint_t *status)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate *status = CRYPTO_PROVIDER_READY;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate /*
2850Sstevel@tonic-gate * Random number entry points.
2860Sstevel@tonic-gate */
2870Sstevel@tonic-gate /* ARGSUSED */
2880Sstevel@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)2890Sstevel@tonic-gate swrand_seed_random(crypto_provider_handle_t provider, crypto_session_id_t sid,
2901920Smcpowers uchar_t *buf, size_t len, uint_t entropy_est, uint32_t flags,
2911920Smcpowers crypto_req_handle_t req)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate /* The entropy estimate is always 0 in this path */
2941920Smcpowers if (flags & CRYPTO_SEED_NOW)
2951920Smcpowers swrand_add_entropy(buf, len, 0);
2961920Smcpowers else
2971920Smcpowers swrand_add_entropy_later(buf, len);
2980Sstevel@tonic-gate return (CRYPTO_SUCCESS);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /* ARGSUSED */
3020Sstevel@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)3030Sstevel@tonic-gate swrand_generate_random(crypto_provider_handle_t provider,
3040Sstevel@tonic-gate crypto_session_id_t sid, uchar_t *buf, size_t len, crypto_req_handle_t req)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate if (crypto_kmflag(req) == KM_NOSLEEP)
3070Sstevel@tonic-gate (void) swrand_get_entropy(buf, len, B_TRUE);
3080Sstevel@tonic-gate else
3090Sstevel@tonic-gate (void) swrand_get_entropy(buf, len, B_FALSE);
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate return (CRYPTO_SUCCESS);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate /*
3150Sstevel@tonic-gate * Extraction of entropy from the pool.
3160Sstevel@tonic-gate *
3170Sstevel@tonic-gate * Returns "len" random bytes in *ptr.
3180Sstevel@tonic-gate * Try to gather some more entropy by calling physmem_ent_gen() when less than
3190Sstevel@tonic-gate * MINEXTRACTBITS are present in the pool.
3200Sstevel@tonic-gate * Will block if not enough entropy was available and the call is blocking.
3210Sstevel@tonic-gate */
3220Sstevel@tonic-gate static int
swrand_get_entropy(uint8_t * ptr,size_t len,boolean_t nonblock)3230Sstevel@tonic-gate swrand_get_entropy(uint8_t *ptr, size_t len, boolean_t nonblock)
3240Sstevel@tonic-gate {
3250Sstevel@tonic-gate int i, bytes;
3260Sstevel@tonic-gate HASH_CTX hashctx;
3270Sstevel@tonic-gate uint8_t digest[HASHSIZE], *pool;
3288733SHai-May.Chao@Sun.COM uint32_t tempout[HASHSIZE/BYTES_IN_WORD];
3298029SHai-May.Chao@Sun.COM int size;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate mutex_enter(&srndpool_lock);
3320Sstevel@tonic-gate if (leftover_bytes > 0) {
3330Sstevel@tonic-gate bytes = min(len, leftover_bytes);
3340Sstevel@tonic-gate bcopy(leftover, ptr, bytes);
3350Sstevel@tonic-gate len -= bytes;
3360Sstevel@tonic-gate ptr += bytes;
3370Sstevel@tonic-gate leftover_bytes -= bytes;
3380Sstevel@tonic-gate if (leftover_bytes > 0)
3390Sstevel@tonic-gate ovbcopy(leftover+bytes, leftover, leftover_bytes);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate while (len > 0) {
3430Sstevel@tonic-gate /* Check if there is enough entropy */
3440Sstevel@tonic-gate while (entropy_bits < MINEXTRACTBITS) {
3450Sstevel@tonic-gate
3460Sstevel@tonic-gate physmem_ent_gen(&entsrc);
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate if (entropy_bits < MINEXTRACTBITS &&
3490Sstevel@tonic-gate nonblock == B_TRUE) {
3500Sstevel@tonic-gate mutex_exit(&srndpool_lock);
3510Sstevel@tonic-gate return (EAGAIN);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (entropy_bits < MINEXTRACTBITS) {
3550Sstevel@tonic-gate ASSERT(nonblock == B_FALSE);
3560Sstevel@tonic-gate snum_waiters++;
3570Sstevel@tonic-gate if (cv_wait_sig(&srndpool_read_cv,
3580Sstevel@tonic-gate &srndpool_lock) == 0) {
3590Sstevel@tonic-gate snum_waiters--;
3600Sstevel@tonic-gate mutex_exit(&srndpool_lock);
3610Sstevel@tonic-gate return (EINTR);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate snum_waiters--;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate
3670Sstevel@tonic-gate /* Figure out how many bytes to extract */
3680Sstevel@tonic-gate bytes = min(HASHSIZE, len);
36911413Sopensolaris@drydog.com bytes = min(bytes, CRYPTO_BITS2BYTES(entropy_bits));
37011413Sopensolaris@drydog.com entropy_bits -= CRYPTO_BYTES2BITS(bytes);
37111413Sopensolaris@drydog.com BUMP_SWRAND_STATS(ss_entOut, CRYPTO_BYTES2BITS(bytes));
3720Sstevel@tonic-gate swrand_stats.ss_entEst = entropy_bits;
3730Sstevel@tonic-gate
3740Sstevel@tonic-gate /* Extract entropy by hashing pool content */
3750Sstevel@tonic-gate HashInit(&hashctx);
3760Sstevel@tonic-gate HashUpdate(&hashctx, (uint8_t *)srndpool, RNDPOOLSIZE);
3770Sstevel@tonic-gate HashFinal(digest, &hashctx);
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate * Feed the digest back into the pool so next
3810Sstevel@tonic-gate * extraction produces different result
3820Sstevel@tonic-gate */
3830Sstevel@tonic-gate pool = (uint8_t *)srndpool;
3840Sstevel@tonic-gate for (i = 0; i < HASHSIZE; i++) {
3850Sstevel@tonic-gate pool[pindex++] ^= digest[i];
3860Sstevel@tonic-gate /* pindex modulo RNDPOOLSIZE */
3870Sstevel@tonic-gate pindex &= (RNDPOOLSIZE - 1);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate
3908029SHai-May.Chao@Sun.COM /* LINTED E_BAD_PTR_CAST_ALIGN */
3918029SHai-May.Chao@Sun.COM fips_random_inner(swrand_XKEY, tempout, (uint32_t *)digest);
3928029SHai-May.Chao@Sun.COM
3938029SHai-May.Chao@Sun.COM if (len >= HASHSIZE) {
3948029SHai-May.Chao@Sun.COM size = HASHSIZE;
3958029SHai-May.Chao@Sun.COM } else {
3968029SHai-May.Chao@Sun.COM size = min(bytes, HASHSIZE);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate
3998029SHai-May.Chao@Sun.COM /*
4008029SHai-May.Chao@Sun.COM * FIPS 140-2: Continuous RNG test - each generation
4018029SHai-May.Chao@Sun.COM * of an n-bit block shall be compared with the previously
4028029SHai-May.Chao@Sun.COM * generated block. Test shall fail if any two compared
4038029SHai-May.Chao@Sun.COM * n-bit blocks are equal.
4048029SHai-May.Chao@Sun.COM */
4058733SHai-May.Chao@Sun.COM for (i = 0; i < HASHSIZE/BYTES_IN_WORD; i++) {
4068029SHai-May.Chao@Sun.COM if (tempout[i] != previous_bytes[i])
4078029SHai-May.Chao@Sun.COM break;
4088029SHai-May.Chao@Sun.COM }
40910500SHai-May.Chao@Sun.COM
41010500SHai-May.Chao@Sun.COM if (i == HASHSIZE/BYTES_IN_WORD) {
4118029SHai-May.Chao@Sun.COM cmn_err(CE_WARN, "swrand: The value of 160-bit block "
4128029SHai-May.Chao@Sun.COM "random bytes are same as the previous one.\n");
41310500SHai-May.Chao@Sun.COM /* discard random bytes and return error */
41410500SHai-May.Chao@Sun.COM return (EIO);
41510500SHai-May.Chao@Sun.COM }
4168029SHai-May.Chao@Sun.COM
4178029SHai-May.Chao@Sun.COM bcopy(tempout, previous_bytes, HASHSIZE);
4188029SHai-May.Chao@Sun.COM
4198029SHai-May.Chao@Sun.COM bcopy(tempout, ptr, size);
4208029SHai-May.Chao@Sun.COM if (len < HASHSIZE) {
4218029SHai-May.Chao@Sun.COM leftover_bytes = HASHSIZE - bytes;
4229294SHai-May.Chao@Sun.COM bcopy((uint8_t *)tempout + bytes, leftover,
4239294SHai-May.Chao@Sun.COM leftover_bytes);
4248029SHai-May.Chao@Sun.COM }
4258029SHai-May.Chao@Sun.COM
4268029SHai-May.Chao@Sun.COM ptr += size;
4278029SHai-May.Chao@Sun.COM len -= size;
4288029SHai-May.Chao@Sun.COM BUMP_SWRAND_STATS(ss_bytesOut, size);
4290Sstevel@tonic-gate }
4308029SHai-May.Chao@Sun.COM
4318029SHai-May.Chao@Sun.COM /* Zero out sensitive information */
4328029SHai-May.Chao@Sun.COM bzero(digest, HASHSIZE);
4338029SHai-May.Chao@Sun.COM bzero(tempout, HASHSIZE);
4340Sstevel@tonic-gate mutex_exit(&srndpool_lock);
4350Sstevel@tonic-gate return (0);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4381920Smcpowers #define SWRAND_ADD_BYTES(ptr, len, i, pool) \
4391920Smcpowers ASSERT((ptr) != NULL && (len) > 0); \
4401920Smcpowers BUMP_SWRAND_STATS(ss_bytesIn, (len)); \
4411920Smcpowers while ((len)--) { \
4421920Smcpowers (pool)[(i)++] ^= *(ptr); \
4431920Smcpowers (ptr)++; \
4441920Smcpowers (i) &= (RNDPOOLSIZE - 1); \
4451920Smcpowers }
4461920Smcpowers
4470Sstevel@tonic-gate /* Write some more user-provided entropy to the pool */
4480Sstevel@tonic-gate static void
swrand_add_bytes(uint8_t * ptr,size_t len)4490Sstevel@tonic-gate swrand_add_bytes(uint8_t *ptr, size_t len)
4500Sstevel@tonic-gate {
4510Sstevel@tonic-gate uint8_t *pool = (uint8_t *)srndpool;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock));
4541920Smcpowers SWRAND_ADD_BYTES(ptr, len, pindex, pool);
4551920Smcpowers }
4560Sstevel@tonic-gate
4571920Smcpowers /*
4581920Smcpowers * Add bytes to buffer. Adding the buffer to the random pool
4591920Smcpowers * is deferred until the random pool is mixed.
4601920Smcpowers */
4611920Smcpowers static void
swrand_add_bytes_later(uint8_t * ptr,size_t len)4621920Smcpowers swrand_add_bytes_later(uint8_t *ptr, size_t len)
4631920Smcpowers {
4641920Smcpowers uint8_t *pool = (uint8_t *)buffer;
4651920Smcpowers
4661920Smcpowers ASSERT(MUTEX_HELD(&buffer_lock));
4671920Smcpowers SWRAND_ADD_BYTES(ptr, len, bindex, pool);
4681920Smcpowers buffer_bytes += len;
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4711920Smcpowers #undef SWRAND_ADD_BYTES
4721920Smcpowers
4730Sstevel@tonic-gate /* Mix the pool */
4740Sstevel@tonic-gate static void
swrand_mix_pool(uint16_t entropy_est)4750Sstevel@tonic-gate swrand_mix_pool(uint16_t entropy_est)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate int i, j, k, start;
4780Sstevel@tonic-gate HASH_CTX hashctx;
4790Sstevel@tonic-gate uint8_t digest[HASHSIZE];
4800Sstevel@tonic-gate uint8_t *pool = (uint8_t *)srndpool;
4811920Smcpowers uint8_t *bp = (uint8_t *)buffer;
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock));
4840Sstevel@tonic-gate
4851920Smcpowers /* add deferred bytes */
4861920Smcpowers mutex_enter(&buffer_lock);
4871920Smcpowers if (buffer_bytes > 0) {
4881920Smcpowers if (buffer_bytes >= RNDPOOLSIZE) {
4891920Smcpowers for (i = 0; i < RNDPOOLSIZE/4; i++) {
4901920Smcpowers srndpool[i] ^= buffer[i];
4911920Smcpowers buffer[i] = 0;
4921920Smcpowers }
4931920Smcpowers bstart = bindex = 0;
4941920Smcpowers } else {
4951920Smcpowers for (i = 0; i < buffer_bytes; i++) {
4961920Smcpowers pool[pindex++] ^= bp[bstart];
4971920Smcpowers bp[bstart++] = 0;
4981920Smcpowers pindex &= (RNDPOOLSIZE - 1);
4991920Smcpowers bstart &= (RNDPOOLSIZE - 1);
5001920Smcpowers }
5011920Smcpowers ASSERT(bstart == bindex);
5021920Smcpowers }
5031920Smcpowers buffer_bytes = 0;
5041920Smcpowers }
5051920Smcpowers mutex_exit(&buffer_lock);
5061920Smcpowers
5070Sstevel@tonic-gate start = 0;
5080Sstevel@tonic-gate for (i = 0; i < RNDPOOLSIZE/HASHSIZE + 1; i++) {
5090Sstevel@tonic-gate HashInit(&hashctx);
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate /* Hash a buffer centered on a block in the pool */
5120Sstevel@tonic-gate if (start + HASHBUFSIZE <= RNDPOOLSIZE)
5130Sstevel@tonic-gate HashUpdate(&hashctx, &pool[start], HASHBUFSIZE);
5140Sstevel@tonic-gate else {
5150Sstevel@tonic-gate HashUpdate(&hashctx, &pool[start],
5160Sstevel@tonic-gate RNDPOOLSIZE - start);
5170Sstevel@tonic-gate HashUpdate(&hashctx, pool,
5180Sstevel@tonic-gate HASHBUFSIZE - RNDPOOLSIZE + start);
5190Sstevel@tonic-gate }
5200Sstevel@tonic-gate HashFinal(digest, &hashctx);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate /* XOR the hash result back into the block */
5230Sstevel@tonic-gate k = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
5240Sstevel@tonic-gate for (j = 0; j < HASHSIZE; j++) {
5250Sstevel@tonic-gate pool[k++] ^= digest[j];
5260Sstevel@tonic-gate k &= (RNDPOOLSIZE - 1);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate /* Slide the hash buffer and repeat with next block */
5300Sstevel@tonic-gate start = (start + HASHSIZE) & (RNDPOOLSIZE - 1);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate entropy_bits += entropy_est;
53411413Sopensolaris@drydog.com if (entropy_bits > CRYPTO_BYTES2BITS(RNDPOOLSIZE))
53511413Sopensolaris@drydog.com entropy_bits = CRYPTO_BYTES2BITS(RNDPOOLSIZE);
5360Sstevel@tonic-gate
5370Sstevel@tonic-gate swrand_stats.ss_entEst = entropy_bits;
5380Sstevel@tonic-gate BUMP_SWRAND_STATS(ss_entIn, entropy_est);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate static void
swrand_add_entropy_later(uint8_t * ptr,size_t len)5421920Smcpowers swrand_add_entropy_later(uint8_t *ptr, size_t len)
5431920Smcpowers {
5441920Smcpowers mutex_enter(&buffer_lock);
5451920Smcpowers swrand_add_bytes_later(ptr, len);
5461920Smcpowers mutex_exit(&buffer_lock);
5471920Smcpowers }
5481920Smcpowers
5491920Smcpowers static void
swrand_add_entropy(uint8_t * ptr,size_t len,uint16_t entropy_est)5500Sstevel@tonic-gate swrand_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate mutex_enter(&srndpool_lock);
5530Sstevel@tonic-gate swrand_add_bytes(ptr, len);
5540Sstevel@tonic-gate swrand_mix_pool(entropy_est);
5550Sstevel@tonic-gate mutex_exit(&srndpool_lock);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate * The physmem_* routines below generate entropy by reading blocks of
5600Sstevel@tonic-gate * physical memory. Entropy is gathered in a couple of ways:
5610Sstevel@tonic-gate *
5620Sstevel@tonic-gate * - By reading blocks of physical memory and detecting if changes
5630Sstevel@tonic-gate * occurred in the blocks read.
5640Sstevel@tonic-gate *
5650Sstevel@tonic-gate * - By measuring the time it takes to load and hash a block of memory
5660Sstevel@tonic-gate * and computing the differences in the measured time.
5670Sstevel@tonic-gate *
5680Sstevel@tonic-gate * The first method was used in the CryptoRand implementation. Physical
5690Sstevel@tonic-gate * memory is divided into blocks of fixed size. A block of memory is
5700Sstevel@tonic-gate * chosen from the possible blocks and hashed to produce a digest. This
5710Sstevel@tonic-gate * digest is then mixed into the pool. A single bit from the digest is
5720Sstevel@tonic-gate * used as a parity bit or "checksum" and compared against the previous
5730Sstevel@tonic-gate * "checksum" computed for the block. If the single-bit checksum has not
5740Sstevel@tonic-gate * changed, no entropy is credited to the pool. If there is a change,
5750Sstevel@tonic-gate * then the assumption is that at least one bit in the block has changed.
5760Sstevel@tonic-gate * The possible locations within the memory block of where the bit change
5770Sstevel@tonic-gate * occurred is used as a measure of entropy. For example, if a block
5780Sstevel@tonic-gate * size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of
5790Sstevel@tonic-gate * entropy is available. Because the single-bit checksum will miss half
5800Sstevel@tonic-gate * of the changes, the amount of entropy credited to the pool is doubled
5810Sstevel@tonic-gate * when a change is detected. With a 4096 byte block size, a block
5820Sstevel@tonic-gate * change will add a total of 30 bits of entropy to the pool.
5830Sstevel@tonic-gate *
5840Sstevel@tonic-gate * The second method measures the amount of time it takes to read and
5850Sstevel@tonic-gate * hash a physical memory block (as described above). The time measured
5860Sstevel@tonic-gate * can vary depending on system load, scheduling and other factors.
5870Sstevel@tonic-gate * Differences between consecutive measurements are computed to come up
5880Sstevel@tonic-gate * with an entropy estimate. The first, second, and third order delta is
5890Sstevel@tonic-gate * calculated to determine the minimum delta value. The number of bits
5900Sstevel@tonic-gate * present in this minimum delta value is the entropy estimate. This
5910Sstevel@tonic-gate * entropy estimation technique using time deltas is similar to that used
5920Sstevel@tonic-gate * in /dev/random implementations from Linux/BSD.
5930Sstevel@tonic-gate */
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate static int
physmem_ent_init(physmem_entsrc_t * entsrc)5960Sstevel@tonic-gate physmem_ent_init(physmem_entsrc_t *entsrc)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate uint8_t *ptr;
5990Sstevel@tonic-gate int i;
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate bzero(entsrc, sizeof (*entsrc));
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate /*
6040Sstevel@tonic-gate * The maximum entropy amount in bits per block of memory read is
6050Sstevel@tonic-gate * log_2(MEMBLOCKSIZE * 8);
6060Sstevel@tonic-gate */
60711413Sopensolaris@drydog.com i = CRYPTO_BYTES2BITS(MEMBLOCKSIZE);
6080Sstevel@tonic-gate while (i >>= 1)
6090Sstevel@tonic-gate entsrc->entperblock++;
6100Sstevel@tonic-gate
6110Sstevel@tonic-gate /* Initialize entsrc->nblocks */
6120Sstevel@tonic-gate physmem_count_blocks();
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate if (entsrc->nblocks == 0) {
6150Sstevel@tonic-gate cmn_err(CE_WARN, "no memory blocks to scan!");
6160Sstevel@tonic-gate return (-1);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate /* Allocate space for the parity vector and memory page */
6200Sstevel@tonic-gate entsrc->parity = kmem_alloc(howmany(entsrc->nblocks, 8),
6210Sstevel@tonic-gate KM_SLEEP);
6220Sstevel@tonic-gate entsrc->pmbuf = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate /* Initialize parity vector with bits from the pool */
6260Sstevel@tonic-gate i = howmany(entsrc->nblocks, 8);
6270Sstevel@tonic-gate ptr = entsrc->parity;
6280Sstevel@tonic-gate while (i > 0) {
6290Sstevel@tonic-gate if (i > RNDPOOLSIZE) {
6300Sstevel@tonic-gate bcopy(srndpool, ptr, RNDPOOLSIZE);
6310Sstevel@tonic-gate mutex_enter(&srndpool_lock);
6320Sstevel@tonic-gate swrand_mix_pool(0);
6330Sstevel@tonic-gate mutex_exit(&srndpool_lock);
6340Sstevel@tonic-gate ptr += RNDPOOLSIZE;
6350Sstevel@tonic-gate i -= RNDPOOLSIZE;
6360Sstevel@tonic-gate } else {
6370Sstevel@tonic-gate bcopy(srndpool, ptr, i);
6380Sstevel@tonic-gate break;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate /* Generate some entropy to further initialize the pool */
6430Sstevel@tonic-gate mutex_enter(&srndpool_lock);
6440Sstevel@tonic-gate physmem_ent_gen(entsrc);
6450Sstevel@tonic-gate entropy_bits = 0;
6460Sstevel@tonic-gate mutex_exit(&srndpool_lock);
6470Sstevel@tonic-gate
6480Sstevel@tonic-gate return (0);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate static void
physmem_ent_fini(physmem_entsrc_t * entsrc)6520Sstevel@tonic-gate physmem_ent_fini(physmem_entsrc_t *entsrc)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate if (entsrc->pmbuf != NULL)
6550Sstevel@tonic-gate vmem_free(heap_arena, entsrc->pmbuf, PAGESIZE);
6560Sstevel@tonic-gate if (entsrc->parity != NULL)
6570Sstevel@tonic-gate kmem_free(entsrc->parity, howmany(entsrc->nblocks, 8));
6580Sstevel@tonic-gate bzero(entsrc, sizeof (*entsrc));
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate static void
physmem_ent_gen(physmem_entsrc_t * entsrc)6620Sstevel@tonic-gate physmem_ent_gen(physmem_entsrc_t *entsrc)
6630Sstevel@tonic-gate {
6640Sstevel@tonic-gate struct memlist *pmem;
6650Sstevel@tonic-gate offset_t offset, poffset;
6660Sstevel@tonic-gate pfn_t pfn;
6670Sstevel@tonic-gate int i, nbytes, len, ent = 0;
6680Sstevel@tonic-gate uint32_t block, oblock;
6690Sstevel@tonic-gate hrtime_t ts1, ts2, diff, delta, delta2, delta3;
6700Sstevel@tonic-gate uint8_t digest[HASHSIZE];
6710Sstevel@tonic-gate HASH_CTX ctx;
6723446Smrj page_t *pp;
6730Sstevel@tonic-gate
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate * Use each 32-bit quantity in the pool to pick a memory
6760Sstevel@tonic-gate * block to read.
6770Sstevel@tonic-gate */
6780Sstevel@tonic-gate for (i = 0; i < RNDPOOLSIZE/4; i++) {
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate /* If the pool is "full", stop after one block */
68111413Sopensolaris@drydog.com if (entropy_bits + ent >= CRYPTO_BYTES2BITS(RNDPOOLSIZE)) {
6820Sstevel@tonic-gate if (i > 0)
6830Sstevel@tonic-gate break;
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate /*
6870Sstevel@tonic-gate * This lock protects reading of phys_install.
6880Sstevel@tonic-gate * Any changes to this list, by DR, are done while
6890Sstevel@tonic-gate * holding this lock. So, holding this lock is sufficient
6900Sstevel@tonic-gate * to handle DR also.
6910Sstevel@tonic-gate */
6920Sstevel@tonic-gate memlist_read_lock();
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate /* We're left with less than 4K of memory after DR */
6950Sstevel@tonic-gate ASSERT(entsrc->nblocks > 0);
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate /* Pick a memory block to read */
6980Sstevel@tonic-gate block = oblock = srndpool[i] % entsrc->nblocks;
6990Sstevel@tonic-gate
70011474SJonathan.Adams@Sun.COM for (pmem = phys_install; pmem != NULL; pmem = pmem->ml_next) {
70111474SJonathan.Adams@Sun.COM if (block < pmem->ml_size / MEMBLOCKSIZE)
7020Sstevel@tonic-gate break;
70311474SJonathan.Adams@Sun.COM block -= pmem->ml_size / MEMBLOCKSIZE;
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7060Sstevel@tonic-gate ASSERT(pmem != NULL);
7070Sstevel@tonic-gate
70811474SJonathan.Adams@Sun.COM offset = pmem->ml_address + block * MEMBLOCKSIZE;
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate if (!address_in_memlist(phys_install, offset, MEMBLOCKSIZE)) {
7110Sstevel@tonic-gate memlist_read_unlock();
7120Sstevel@tonic-gate continue;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate /*
7163446Smrj * Do an initial check to see if the address is safe
7173446Smrj */
7183446Smrj if (plat_hold_page(offset >> PAGESHIFT, PLAT_HOLD_NO_LOCK, NULL)
7193446Smrj == PLAT_HOLD_FAIL) {
7203446Smrj memlist_read_unlock();
7213446Smrj continue;
7223446Smrj }
7233446Smrj
7243446Smrj /*
7250Sstevel@tonic-gate * Figure out which page to load to read the
7260Sstevel@tonic-gate * memory block. Load the page and compute the
7270Sstevel@tonic-gate * hash of the memory block.
7280Sstevel@tonic-gate */
7290Sstevel@tonic-gate len = MEMBLOCKSIZE;
7300Sstevel@tonic-gate ts1 = gethrtime();
7310Sstevel@tonic-gate HashInit(&ctx);
7320Sstevel@tonic-gate while (len) {
7330Sstevel@tonic-gate pfn = offset >> PAGESHIFT;
7340Sstevel@tonic-gate poffset = offset & PAGEOFFSET;
7350Sstevel@tonic-gate nbytes = PAGESIZE - poffset < len ?
7360Sstevel@tonic-gate PAGESIZE - poffset : len;
7370Sstevel@tonic-gate
7383446Smrj /*
7393446Smrj * Re-check the offset, and lock the frame. If the
7403446Smrj * page was given away after the above check, we'll
7413446Smrj * just bail out.
7423446Smrj */
7433446Smrj if (plat_hold_page(pfn, PLAT_HOLD_LOCK, &pp) ==
7443446Smrj PLAT_HOLD_FAIL)
7453446Smrj break;
7463446Smrj
7470Sstevel@tonic-gate hat_devload(kas.a_hat, entsrc->pmbuf,
7480Sstevel@tonic-gate PAGESIZE, pfn, PROT_READ,
7490Sstevel@tonic-gate HAT_LOAD_NOCONSIST | HAT_LOAD_LOCK);
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate HashUpdate(&ctx, (uint8_t *)entsrc->pmbuf + poffset,
7520Sstevel@tonic-gate nbytes);
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate hat_unload(kas.a_hat, entsrc->pmbuf, PAGESIZE,
7550Sstevel@tonic-gate HAT_UNLOAD_UNLOCK);
7560Sstevel@tonic-gate
7573446Smrj plat_release_page(pp);
7583446Smrj
7590Sstevel@tonic-gate len -= nbytes;
7600Sstevel@tonic-gate offset += nbytes;
7610Sstevel@tonic-gate }
7620Sstevel@tonic-gate /* We got our pages. Let the DR roll */
7630Sstevel@tonic-gate memlist_read_unlock();
7640Sstevel@tonic-gate
7653446Smrj /* See if we had to bail out due to a page being given away */
7663446Smrj if (len)
7673446Smrj continue;
7683446Smrj
7690Sstevel@tonic-gate HashFinal(digest, &ctx);
7700Sstevel@tonic-gate ts2 = gethrtime();
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate * Compute the time it took to load and hash the
7740Sstevel@tonic-gate * block and compare it against the previous
7750Sstevel@tonic-gate * measurement. The delta of the time values
7760Sstevel@tonic-gate * provides a small amount of entropy. The
7770Sstevel@tonic-gate * minimum of the first, second, and third order
7780Sstevel@tonic-gate * delta is used to estimate how much entropy
7790Sstevel@tonic-gate * is present.
7800Sstevel@tonic-gate */
7810Sstevel@tonic-gate diff = ts2 - ts1;
7820Sstevel@tonic-gate delta = diff - entsrc->last_diff;
7830Sstevel@tonic-gate if (delta < 0)
7840Sstevel@tonic-gate delta = -delta;
7850Sstevel@tonic-gate delta2 = delta - entsrc->last_delta;
7860Sstevel@tonic-gate if (delta2 < 0)
7870Sstevel@tonic-gate delta2 = -delta2;
7880Sstevel@tonic-gate delta3 = delta2 - entsrc->last_delta2;
7890Sstevel@tonic-gate if (delta3 < 0)
7900Sstevel@tonic-gate delta3 = -delta3;
7910Sstevel@tonic-gate entsrc->last_diff = diff;
7920Sstevel@tonic-gate entsrc->last_delta = delta;
7930Sstevel@tonic-gate entsrc->last_delta2 = delta2;
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate if (delta > delta2)
7960Sstevel@tonic-gate delta = delta2;
7970Sstevel@tonic-gate if (delta > delta3)
7980Sstevel@tonic-gate delta = delta3;
7990Sstevel@tonic-gate delta2 = 0;
8000Sstevel@tonic-gate while (delta >>= 1)
8010Sstevel@tonic-gate delta2++;
8020Sstevel@tonic-gate ent += delta2;
8030Sstevel@tonic-gate
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate * If the memory block has changed, credit the pool with
8060Sstevel@tonic-gate * the entropy estimate. The entropy estimate is doubled
8070Sstevel@tonic-gate * because the single-bit checksum misses half the change
8080Sstevel@tonic-gate * on average.
8090Sstevel@tonic-gate */
8100Sstevel@tonic-gate if (physmem_parity_update(entsrc->parity, oblock,
8110Sstevel@tonic-gate digest[0] & 1))
8120Sstevel@tonic-gate ent += 2 * entsrc->entperblock;
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate /* Add the entropy bytes to the pool */
8150Sstevel@tonic-gate swrand_add_bytes(digest, HASHSIZE);
8160Sstevel@tonic-gate swrand_add_bytes((uint8_t *)&ts1, sizeof (ts1));
8170Sstevel@tonic-gate swrand_add_bytes((uint8_t *)&ts2, sizeof (ts2));
8180Sstevel@tonic-gate }
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate swrand_mix_pool(ent);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate static int
physmem_parity_update(uint8_t * parity_vec,uint32_t block,int parity)8240Sstevel@tonic-gate physmem_parity_update(uint8_t *parity_vec, uint32_t block, int parity)
8250Sstevel@tonic-gate {
8260Sstevel@tonic-gate /* Test and set the parity bit, return 1 if changed */
8270Sstevel@tonic-gate if (parity == ((parity_vec[block >> 3] >> (block & 7)) & 1))
8280Sstevel@tonic-gate return (0);
8290Sstevel@tonic-gate parity_vec[block >> 3] ^= 1 << (block & 7);
8300Sstevel@tonic-gate return (1);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate /* Compute number of memory blocks available to scan */
8340Sstevel@tonic-gate static void
physmem_count_blocks()8350Sstevel@tonic-gate physmem_count_blocks()
8360Sstevel@tonic-gate {
8370Sstevel@tonic-gate struct memlist *pmem;
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate memlist_read_lock();
8400Sstevel@tonic-gate entsrc.nblocks = 0;
84111474SJonathan.Adams@Sun.COM for (pmem = phys_install; pmem != NULL; pmem = pmem->ml_next) {
84211474SJonathan.Adams@Sun.COM entsrc.nblocks += pmem->ml_size / MEMBLOCKSIZE;
8430Sstevel@tonic-gate if (entsrc.nblocks > MAXMEMBLOCKS) {
8440Sstevel@tonic-gate entsrc.nblocks = MAXMEMBLOCKS;
8450Sstevel@tonic-gate break;
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate memlist_read_unlock();
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate * Dynamic Reconfiguration call-back functions
8530Sstevel@tonic-gate */
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /* ARGSUSED */
8560Sstevel@tonic-gate static void
rnd_dr_callback_post_add(void * arg,pgcnt_t delta)8570Sstevel@tonic-gate rnd_dr_callback_post_add(void *arg, pgcnt_t delta)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate /* More memory is available now, so update entsrc->nblocks. */
8600Sstevel@tonic-gate physmem_count_blocks();
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate
8630Sstevel@tonic-gate /* Call-back routine invoked before the DR starts a memory removal. */
8640Sstevel@tonic-gate /* ARGSUSED */
8650Sstevel@tonic-gate static int
rnd_dr_callback_pre_del(void * arg,pgcnt_t delta)8660Sstevel@tonic-gate rnd_dr_callback_pre_del(void *arg, pgcnt_t delta)
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate return (0);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate /* Call-back routine invoked after the DR starts a memory removal. */
8720Sstevel@tonic-gate /* ARGSUSED */
8730Sstevel@tonic-gate static void
rnd_dr_callback_post_del(void * arg,pgcnt_t delta,int cancelled)8740Sstevel@tonic-gate rnd_dr_callback_post_del(void *arg, pgcnt_t delta, int cancelled)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate /* Memory has shrunk, so update entsrc->nblocks. */
8770Sstevel@tonic-gate physmem_count_blocks();
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate /* Timeout handling to gather entropy from physmem events */
8810Sstevel@tonic-gate static void
swrand_schedule_timeout(void)8820Sstevel@tonic-gate swrand_schedule_timeout(void)
8830Sstevel@tonic-gate {
8840Sstevel@tonic-gate clock_t ut; /* time in microseconds */
8850Sstevel@tonic-gate
8860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&srndpool_lock));
8870Sstevel@tonic-gate /*
8880Sstevel@tonic-gate * The new timeout value is taken from the pool of random bits.
8890Sstevel@tonic-gate * We're merely reading the first 32 bits from the pool here, not
8900Sstevel@tonic-gate * consuming any entropy.
8910Sstevel@tonic-gate * This routine is usually called right after stirring the pool, so
8920Sstevel@tonic-gate * srndpool[0] will have a *fresh* random value each time.
8930Sstevel@tonic-gate * The timeout multiplier value is a random value between 0.7 sec and
8940Sstevel@tonic-gate * 1.748575 sec (0.7 sec + 0xFFFFF microseconds).
8950Sstevel@tonic-gate * The new timeout is TIMEOUT_INTERVAL times that multiplier.
8960Sstevel@tonic-gate */
8970Sstevel@tonic-gate ut = 700000 + (clock_t)(srndpool[0] & 0xFFFFF);
8980Sstevel@tonic-gate rnd_timeout_id = timeout(rnd_handler, NULL,
8990Sstevel@tonic-gate TIMEOUT_INTERVAL * drv_usectohz(ut));
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate
9020Sstevel@tonic-gate /*ARGSUSED*/
9030Sstevel@tonic-gate static void
rnd_handler(void * arg)9040Sstevel@tonic-gate rnd_handler(void *arg)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate mutex_enter(&srndpool_lock);
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate physmem_ent_gen(&entsrc);
9090Sstevel@tonic-gate if (snum_waiters > 0)
9100Sstevel@tonic-gate cv_broadcast(&srndpool_read_cv);
9110Sstevel@tonic-gate swrand_schedule_timeout();
9120Sstevel@tonic-gate
9130Sstevel@tonic-gate mutex_exit(&srndpool_lock);
9140Sstevel@tonic-gate }
91510500SHai-May.Chao@Sun.COM
91610500SHai-May.Chao@Sun.COM /*
91710500SHai-May.Chao@Sun.COM * Swrand Power-Up Self-Test
91810500SHai-May.Chao@Sun.COM */
91910500SHai-May.Chao@Sun.COM void
swrand_POST(int * rc)92010500SHai-May.Chao@Sun.COM swrand_POST(int *rc)
92110500SHai-May.Chao@Sun.COM {
92210500SHai-May.Chao@Sun.COM
92310500SHai-May.Chao@Sun.COM *rc = fips_rng_post();
92410500SHai-May.Chao@Sun.COM
92510500SHai-May.Chao@Sun.COM }
926