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 5*02f574f0Skais * Common Development and Distribution License (the "License"). 6*02f574f0Skais * 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 /* 22fa626f0cSkrishna * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * This file implements the interfaces that the /dev/random 307c478bd9Sstevel@tonic-gate * driver uses for read(2), write(2) and poll(2) on /dev/random or 317c478bd9Sstevel@tonic-gate * /dev/urandom. It also implements the kernel API - random_add_entropy(), 327c478bd9Sstevel@tonic-gate * random_get_pseudo_bytes() and random_get_bytes(). 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * We periodically collect random bits from providers which are registered 357c478bd9Sstevel@tonic-gate * with the Kernel Cryptographic Framework (kCF) as capable of random 367c478bd9Sstevel@tonic-gate * number generation. The random bits are maintained in a cache and 377c478bd9Sstevel@tonic-gate * it is used for high quality random numbers (/dev/random) requests. 387c478bd9Sstevel@tonic-gate * We pick a provider and call its SPI routine, if the cache does not have 397c478bd9Sstevel@tonic-gate * enough bytes to satisfy a request. 407c478bd9Sstevel@tonic-gate * 417c478bd9Sstevel@tonic-gate * /dev/urandom requests use a software-based generator algorithm that uses the 427c478bd9Sstevel@tonic-gate * random bits in the cache as a seed. We create one pseudo-random generator 437c478bd9Sstevel@tonic-gate * (for /dev/urandom) per possible CPU on the system, and use it, 447c478bd9Sstevel@tonic-gate * kmem-magazine-style, to avoid cache line contention. 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * LOCKING HIERARCHY: 477c478bd9Sstevel@tonic-gate * 1) rmp->rm_lock protects the per-cpu pseudo-random generators. 487c478bd9Sstevel@tonic-gate * 2) rndpool_lock protects the high-quality randomness pool. 497c478bd9Sstevel@tonic-gate * It may be locked while a rmp->rm_lock is held. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * A history note: The kernel API and the software-based algorithms in this 527c478bd9Sstevel@tonic-gate * file used to be part of the /dev/random driver. 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #include <sys/types.h> 567c478bd9Sstevel@tonic-gate #include <sys/conf.h> 577c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 587c478bd9Sstevel@tonic-gate #include <sys/disp.h> 597c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 607c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 617c478bd9Sstevel@tonic-gate #include <sys/crypto/common.h> 627c478bd9Sstevel@tonic-gate #include <sys/crypto/api.h> 637c478bd9Sstevel@tonic-gate #include <sys/crypto/impl.h> 647c478bd9Sstevel@tonic-gate #include <sys/crypto/sched_impl.h> 657c478bd9Sstevel@tonic-gate #include <sys/random.h> 667c478bd9Sstevel@tonic-gate #include <sys/sha1.h> 677c478bd9Sstevel@tonic-gate #include <sys/time.h> 687c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 697c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 707c478bd9Sstevel@tonic-gate #include <sys/taskq.h> 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define RNDPOOLSIZE 1024 /* Pool size in bytes */ 737c478bd9Sstevel@tonic-gate #define MINEXTRACTBYTES 20 747c478bd9Sstevel@tonic-gate #define MAXEXTRACTBYTES 1024 757c478bd9Sstevel@tonic-gate #define PRNG_MAXOBLOCKS 1310720 /* Max output block per prng key */ 767c478bd9Sstevel@tonic-gate #define TIMEOUT_INTERVAL 5 /* Periodic mixing interval in secs */ 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate typedef enum extract_type { 797c478bd9Sstevel@tonic-gate NONBLOCK_EXTRACT, 807c478bd9Sstevel@tonic-gate BLOCKING_EXTRACT, 817c478bd9Sstevel@tonic-gate ALWAYS_EXTRACT 827c478bd9Sstevel@tonic-gate } extract_type_t; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate /* 857c478bd9Sstevel@tonic-gate * Hash-algo generic definitions. For now, they are SHA1's. We use SHA1 867c478bd9Sstevel@tonic-gate * routines directly instead of using k-API because we can't return any 877c478bd9Sstevel@tonic-gate * error code in /dev/urandom case and we can get an error using k-API 887c478bd9Sstevel@tonic-gate * if a mechanism is disabled. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate #define HASHSIZE 20 917c478bd9Sstevel@tonic-gate #define HASH_CTX SHA1_CTX 927c478bd9Sstevel@tonic-gate #define HashInit(ctx) SHA1Init((ctx)) 937c478bd9Sstevel@tonic-gate #define HashUpdate(ctx, p, s) SHA1Update((ctx), (p), (s)) 947c478bd9Sstevel@tonic-gate #define HashFinal(d, ctx) SHA1Final((d), (ctx)) 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* HMAC-SHA1 */ 977c478bd9Sstevel@tonic-gate #define HMAC_KEYSIZE 20 987c478bd9Sstevel@tonic-gate #define HMAC_BLOCK_SIZE 64 997c478bd9Sstevel@tonic-gate #define HMAC_KEYSCHED sha1keysched_t 1007c478bd9Sstevel@tonic-gate #define SET_ENCRYPT_KEY(k, s, ks) hmac_key((k), (s), (ks)) 1017c478bd9Sstevel@tonic-gate #define HMAC_ENCRYPT(ks, p, s, d) hmac_encr((ks), (uint8_t *)(p), s, d) 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* HMAC-SHA1 "keyschedule" */ 1047c478bd9Sstevel@tonic-gate typedef struct sha1keysched_s { 1057c478bd9Sstevel@tonic-gate SHA1_CTX ictx; 1067c478bd9Sstevel@tonic-gate SHA1_CTX octx; 1077c478bd9Sstevel@tonic-gate } sha1keysched_t; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * Cache of random bytes implemented as a circular buffer. findex and rindex 1117c478bd9Sstevel@tonic-gate * track the front and back of the circular buffer. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate uint8_t rndpool[RNDPOOLSIZE]; 1147c478bd9Sstevel@tonic-gate static int findex, rindex; 1157c478bd9Sstevel@tonic-gate static int rnbyte_cnt; /* Number of bytes in the cache */ 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static kmutex_t rndpool_lock; /* protects r/w accesses to the cache, */ 1187c478bd9Sstevel@tonic-gate /* and the global variables */ 1197c478bd9Sstevel@tonic-gate static kcondvar_t rndpool_read_cv; /* serializes poll/read syscalls */ 1207c478bd9Sstevel@tonic-gate static int num_waiters; /* #threads waiting to read from /dev/random */ 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static struct pollhead rnd_pollhead; 1237c478bd9Sstevel@tonic-gate static timeout_id_t kcf_rndtimeout_id; 1247c478bd9Sstevel@tonic-gate static crypto_mech_type_t rngmech_type = CRYPTO_MECH_INVALID; 1257c478bd9Sstevel@tonic-gate rnd_stats_t rnd_stats; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate static void rndc_addbytes(uint8_t *, size_t); 1287c478bd9Sstevel@tonic-gate static void rndc_getbytes(uint8_t *ptr, size_t len); 1297c478bd9Sstevel@tonic-gate static void rnd_handler(void *); 1307c478bd9Sstevel@tonic-gate static void rnd_alloc_magazines(); 1317c478bd9Sstevel@tonic-gate static void hmac_key(uint8_t *, size_t, void *); 1327c478bd9Sstevel@tonic-gate static void hmac_encr(void *, uint8_t *, size_t, uint8_t *); 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate void 1367c478bd9Sstevel@tonic-gate kcf_rnd_init() 1377c478bd9Sstevel@tonic-gate { 1387c478bd9Sstevel@tonic-gate hrtime_t ts; 1397c478bd9Sstevel@tonic-gate time_t now; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate mutex_init(&rndpool_lock, NULL, MUTEX_DEFAULT, NULL); 1427c478bd9Sstevel@tonic-gate cv_init(&rndpool_read_cv, NULL, CV_DEFAULT, NULL); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * Add bytes to the cache using 1467c478bd9Sstevel@tonic-gate * . 2 unpredictable times: high resolution time since the boot-time, 1477c478bd9Sstevel@tonic-gate * and the current time-of-the day. 1487c478bd9Sstevel@tonic-gate * This is used only to make the timeout value in the timer 1497c478bd9Sstevel@tonic-gate * unpredictable. 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate ts = gethrtime(); 1527c478bd9Sstevel@tonic-gate rndc_addbytes((uint8_t *)&ts, sizeof (ts)); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate (void) drv_getparm(TIME, &now); 1557c478bd9Sstevel@tonic-gate rndc_addbytes((uint8_t *)&now, sizeof (now)); 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate rnbyte_cnt = 0; 1587c478bd9Sstevel@tonic-gate findex = rindex = 0; 1597c478bd9Sstevel@tonic-gate num_waiters = 0; 1607c478bd9Sstevel@tonic-gate rngmech_type = KCF_MECHID(KCF_MISC_CLASS, 0); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate rnd_alloc_magazines(); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * Return TRUE if at least one provider exists that can 1677c478bd9Sstevel@tonic-gate * supply random numbers. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate boolean_t 1707c478bd9Sstevel@tonic-gate kcf_rngprov_check(void) 1717c478bd9Sstevel@tonic-gate { 1727c478bd9Sstevel@tonic-gate int rv; 1737c478bd9Sstevel@tonic-gate kcf_provider_desc_t *pd; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if ((pd = kcf_get_mech_provider(rngmech_type, NULL, &rv, 1767c478bd9Sstevel@tonic-gate NULL, CRYPTO_FG_RANDOM, B_FALSE, 0)) != NULL) { 1777c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 1787c478bd9Sstevel@tonic-gate return (B_TRUE); 1797c478bd9Sstevel@tonic-gate } else 1807c478bd9Sstevel@tonic-gate return (B_FALSE); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * Pick a software-based provider and submit a request to seed 1857c478bd9Sstevel@tonic-gate * its random number generator. 1867c478bd9Sstevel@tonic-gate */ 1877c478bd9Sstevel@tonic-gate static void 1887c478bd9Sstevel@tonic-gate rngprov_seed(uint8_t *buf, int len) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate kcf_provider_desc_t *pd = NULL; 1917c478bd9Sstevel@tonic-gate kcf_req_params_t params; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (kcf_get_sw_prov(rngmech_type, &pd, B_FALSE) == CRYPTO_SUCCESS) { 1947c478bd9Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_SEED, 1957c478bd9Sstevel@tonic-gate pd->pd_sid, buf, len); 1967c478bd9Sstevel@tonic-gate (void) kcf_submit_request(pd, NULL, NULL, ¶ms, B_FALSE); 1977c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate /* Boot-time tunable for experimentation. */ 2027c478bd9Sstevel@tonic-gate int kcf_limit_hwrng = 1; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * This routine is called for blocking reads. 2077c478bd9Sstevel@tonic-gate * 2087c478bd9Sstevel@tonic-gate * The argument from_user_api indicates whether the caller is 2097c478bd9Sstevel@tonic-gate * from userland coming via the /dev/random driver. 2107c478bd9Sstevel@tonic-gate * 2117c478bd9Sstevel@tonic-gate * The argument is_taskq_thr indicates whether the caller is 2127c478bd9Sstevel@tonic-gate * the taskq thread dispatched by the timeout handler routine. 2137c478bd9Sstevel@tonic-gate * In this case, we cycle through all the providers 2147c478bd9Sstevel@tonic-gate * submitting a request to each provider to generate random numbers. 2157c478bd9Sstevel@tonic-gate * 2167c478bd9Sstevel@tonic-gate * For other cases, we pick a provider and submit a request to generate 2177c478bd9Sstevel@tonic-gate * random numbers. We retry using another provider if we get an error. 2187c478bd9Sstevel@tonic-gate * 2197c478bd9Sstevel@tonic-gate * Returns the number of bytes that are written to 'ptr'. Returns -1 2207c478bd9Sstevel@tonic-gate * if no provider is found. ptr and need are unchanged. 2217c478bd9Sstevel@tonic-gate */ 2227c478bd9Sstevel@tonic-gate static int 2237c478bd9Sstevel@tonic-gate rngprov_getbytes(uint8_t *ptr, size_t need, boolean_t from_user_api, 2247c478bd9Sstevel@tonic-gate boolean_t is_taskq_thr) 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate int rv; 2277c478bd9Sstevel@tonic-gate int prov_cnt = 0; 2287c478bd9Sstevel@tonic-gate int total_bytes = 0; 2297c478bd9Sstevel@tonic-gate kcf_provider_desc_t *pd; 2307c478bd9Sstevel@tonic-gate kcf_req_params_t params; 2317c478bd9Sstevel@tonic-gate kcf_prov_tried_t *list = NULL; 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate while ((pd = kcf_get_mech_provider(rngmech_type, NULL, &rv, 2347c478bd9Sstevel@tonic-gate list, CRYPTO_FG_RANDOM, B_FALSE, 0)) != NULL) { 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate prov_cnt++; 2377c478bd9Sstevel@tonic-gate /* 2387c478bd9Sstevel@tonic-gate * Typically a hardware RNG is a multi-purpose 2397c478bd9Sstevel@tonic-gate * crypto card and hence we do not want to overload the card 2407c478bd9Sstevel@tonic-gate * just for random numbers. The following check is to prevent 2417c478bd9Sstevel@tonic-gate * a user process from hogging the hardware RNG. Note that we 2427c478bd9Sstevel@tonic-gate * still use the hardware RNG from the periodically run 2437c478bd9Sstevel@tonic-gate * taskq thread. 2447c478bd9Sstevel@tonic-gate */ 2457c478bd9Sstevel@tonic-gate if (pd->pd_prov_type == CRYPTO_HW_PROVIDER && from_user_api && 2467c478bd9Sstevel@tonic-gate kcf_limit_hwrng == 1) { 2477c478bd9Sstevel@tonic-gate ASSERT(is_taskq_thr == B_FALSE); 2487c478bd9Sstevel@tonic-gate goto try_next; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, KCF_OP_RANDOM_GENERATE, 2527c478bd9Sstevel@tonic-gate pd->pd_sid, ptr, need); 2537c478bd9Sstevel@tonic-gate rv = kcf_submit_request(pd, NULL, NULL, ¶ms, B_FALSE); 2547c478bd9Sstevel@tonic-gate ASSERT(rv != CRYPTO_QUEUED); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 2577c478bd9Sstevel@tonic-gate total_bytes += need; 2587c478bd9Sstevel@tonic-gate if (is_taskq_thr) 2597c478bd9Sstevel@tonic-gate rndc_addbytes(ptr, need); 2607c478bd9Sstevel@tonic-gate else { 2617c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 2627c478bd9Sstevel@tonic-gate break; 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if (is_taskq_thr || rv != CRYPTO_SUCCESS) { 2677c478bd9Sstevel@tonic-gate try_next: 2687c478bd9Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */ 2697c478bd9Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KM_SLEEP) == NULL) { 2707c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 2717c478bd9Sstevel@tonic-gate break; 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (list != NULL) 2787c478bd9Sstevel@tonic-gate kcf_free_triedlist(list); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate if (prov_cnt == 0) { /* no provider could be found. */ 2817c478bd9Sstevel@tonic-gate return (-1); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate return (total_bytes); 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate static void 2887c478bd9Sstevel@tonic-gate notify_done(void *arg, int rv) 2897c478bd9Sstevel@tonic-gate { 2907c478bd9Sstevel@tonic-gate uchar_t *rndbuf = arg; 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) 2937c478bd9Sstevel@tonic-gate rndc_addbytes(rndbuf, MINEXTRACTBYTES); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate bzero(rndbuf, MINEXTRACTBYTES); 2967c478bd9Sstevel@tonic-gate kmem_free(rndbuf, MINEXTRACTBYTES); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate /* 3007c478bd9Sstevel@tonic-gate * Cycle through all the providers submitting a request to each provider 3017c478bd9Sstevel@tonic-gate * to generate random numbers. This is called for the modes - NONBLOCK_EXTRACT 3027c478bd9Sstevel@tonic-gate * and ALWAYS_EXTRACT. 3037c478bd9Sstevel@tonic-gate * 3047c478bd9Sstevel@tonic-gate * Returns the number of bytes that are written to 'ptr'. Returns -1 3057c478bd9Sstevel@tonic-gate * if no provider is found. ptr and len are unchanged. 3067c478bd9Sstevel@tonic-gate */ 3077c478bd9Sstevel@tonic-gate static int 3087c478bd9Sstevel@tonic-gate rngprov_getbytes_nblk(uint8_t *ptr, size_t len, boolean_t from_user_api) 3097c478bd9Sstevel@tonic-gate { 3107c478bd9Sstevel@tonic-gate int rv, blen, total_bytes; 3117c478bd9Sstevel@tonic-gate uchar_t *rndbuf; 3127c478bd9Sstevel@tonic-gate kcf_provider_desc_t *pd; 3137c478bd9Sstevel@tonic-gate kcf_req_params_t params; 3147c478bd9Sstevel@tonic-gate crypto_call_req_t req; 3157c478bd9Sstevel@tonic-gate kcf_prov_tried_t *list = NULL; 3167c478bd9Sstevel@tonic-gate int prov_cnt = 0; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate blen = 0; 3197c478bd9Sstevel@tonic-gate total_bytes = 0; 3207c478bd9Sstevel@tonic-gate req.cr_flag = CRYPTO_SKIP_REQID; 3217c478bd9Sstevel@tonic-gate req.cr_callback_func = notify_done; 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate while ((pd = kcf_get_mech_provider(rngmech_type, NULL, &rv, 3247c478bd9Sstevel@tonic-gate list, CRYPTO_FG_RANDOM, CHECK_RESTRICT(&req), 0)) != NULL) { 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate prov_cnt ++; 3277c478bd9Sstevel@tonic-gate switch (pd->pd_prov_type) { 3287c478bd9Sstevel@tonic-gate case CRYPTO_HW_PROVIDER: 3297c478bd9Sstevel@tonic-gate /* See comments in rngprov_getbytes() */ 3307c478bd9Sstevel@tonic-gate if (from_user_api && kcf_limit_hwrng == 1) 3317c478bd9Sstevel@tonic-gate goto try_next; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * We have to allocate a buffer here as we can not 3357c478bd9Sstevel@tonic-gate * assume that the input buffer will remain valid 3367c478bd9Sstevel@tonic-gate * when the callback comes. We use a fixed size buffer 3377c478bd9Sstevel@tonic-gate * to simplify the book keeping. 3387c478bd9Sstevel@tonic-gate */ 3397c478bd9Sstevel@tonic-gate rndbuf = kmem_alloc(MINEXTRACTBYTES, KM_NOSLEEP); 3407c478bd9Sstevel@tonic-gate if (rndbuf == NULL) { 3417c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 3427c478bd9Sstevel@tonic-gate if (list != NULL) 3437c478bd9Sstevel@tonic-gate kcf_free_triedlist(list); 3447c478bd9Sstevel@tonic-gate return (total_bytes); 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate req.cr_callback_arg = rndbuf; 3477c478bd9Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, 3487c478bd9Sstevel@tonic-gate KCF_OP_RANDOM_GENERATE, 3497c478bd9Sstevel@tonic-gate pd->pd_sid, rndbuf, MINEXTRACTBYTES); 3507c478bd9Sstevel@tonic-gate break; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate case CRYPTO_SW_PROVIDER: 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * We do not need to allocate a buffer in the software 3557c478bd9Sstevel@tonic-gate * provider case as there is no callback involved. We 3567c478bd9Sstevel@tonic-gate * avoid any extra data copy by directly passing 'ptr'. 3577c478bd9Sstevel@tonic-gate */ 3587c478bd9Sstevel@tonic-gate KCF_WRAP_RANDOM_OPS_PARAMS(¶ms, 3597c478bd9Sstevel@tonic-gate KCF_OP_RANDOM_GENERATE, 3607c478bd9Sstevel@tonic-gate pd->pd_sid, ptr, len); 3617c478bd9Sstevel@tonic-gate break; 3627c478bd9Sstevel@tonic-gate } 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate rv = kcf_submit_request(pd, NULL, &req, ¶ms, B_FALSE); 3657c478bd9Sstevel@tonic-gate if (rv == CRYPTO_SUCCESS) { 3667c478bd9Sstevel@tonic-gate switch (pd->pd_prov_type) { 3677c478bd9Sstevel@tonic-gate case CRYPTO_HW_PROVIDER: 3687c478bd9Sstevel@tonic-gate /* 3697c478bd9Sstevel@tonic-gate * Since we have the input buffer handy, 3707c478bd9Sstevel@tonic-gate * we directly copy to it rather than 3717c478bd9Sstevel@tonic-gate * adding to the pool. 3727c478bd9Sstevel@tonic-gate */ 3737c478bd9Sstevel@tonic-gate blen = min(MINEXTRACTBYTES, len); 3747c478bd9Sstevel@tonic-gate bcopy(rndbuf, ptr, blen); 3757c478bd9Sstevel@tonic-gate if (len < MINEXTRACTBYTES) 3767c478bd9Sstevel@tonic-gate rndc_addbytes(rndbuf + len, 3777c478bd9Sstevel@tonic-gate MINEXTRACTBYTES - len); 3787c478bd9Sstevel@tonic-gate ptr += blen; 3797c478bd9Sstevel@tonic-gate len -= blen; 3807c478bd9Sstevel@tonic-gate total_bytes += blen; 3817c478bd9Sstevel@tonic-gate break; 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate case CRYPTO_SW_PROVIDER: 3847c478bd9Sstevel@tonic-gate total_bytes += len; 3857c478bd9Sstevel@tonic-gate len = 0; 3867c478bd9Sstevel@tonic-gate break; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate /* 3917c478bd9Sstevel@tonic-gate * We free the buffer in the callback routine 3927c478bd9Sstevel@tonic-gate * for the CRYPTO_QUEUED case. 3937c478bd9Sstevel@tonic-gate */ 3947c478bd9Sstevel@tonic-gate if (pd->pd_prov_type == CRYPTO_HW_PROVIDER && 3957c478bd9Sstevel@tonic-gate rv != CRYPTO_QUEUED) { 3967c478bd9Sstevel@tonic-gate bzero(rndbuf, MINEXTRACTBYTES); 3977c478bd9Sstevel@tonic-gate kmem_free(rndbuf, MINEXTRACTBYTES); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate if (len == 0) { 4017c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 4027c478bd9Sstevel@tonic-gate break; 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate if (rv != CRYPTO_SUCCESS) { 4067c478bd9Sstevel@tonic-gate try_next: 4077c478bd9Sstevel@tonic-gate /* Add pd to the linked list of providers tried. */ 4087c478bd9Sstevel@tonic-gate if (kcf_insert_triedlist(&list, pd, KM_NOSLEEP) == 4097c478bd9Sstevel@tonic-gate NULL) { 4107c478bd9Sstevel@tonic-gate KCF_PROV_REFRELE(pd); 4117c478bd9Sstevel@tonic-gate break; 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate if (list != NULL) { 4177c478bd9Sstevel@tonic-gate kcf_free_triedlist(list); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate if (prov_cnt == 0) { /* no provider could be found. */ 4217c478bd9Sstevel@tonic-gate return (-1); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate return (total_bytes); 4257c478bd9Sstevel@tonic-gate } 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate static void 4287c478bd9Sstevel@tonic-gate rngprov_task(void *arg) 4297c478bd9Sstevel@tonic-gate { 4307c478bd9Sstevel@tonic-gate int len = (int)(uintptr_t)arg; 4317c478bd9Sstevel@tonic-gate uchar_t tbuf[MAXEXTRACTBYTES]; 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate ASSERT(len <= MAXEXTRACTBYTES); 4347c478bd9Sstevel@tonic-gate if (rngprov_getbytes(tbuf, len, B_FALSE, B_TRUE) == -1) { 4357c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "No randomness provider enabled for " 4367c478bd9Sstevel@tonic-gate "/dev/random. Use cryptoadm(1M) to enable a provider."); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate /* 4417c478bd9Sstevel@tonic-gate * Returns "len" random or pseudo-random bytes in *ptr. 4427c478bd9Sstevel@tonic-gate * Will block if not enough random bytes are available and the 4437c478bd9Sstevel@tonic-gate * call is blocking. 4447c478bd9Sstevel@tonic-gate * 4457c478bd9Sstevel@tonic-gate * Called with rndpool_lock held (allowing caller to do optimistic locking; 4467c478bd9Sstevel@tonic-gate * releases the lock before return). 4477c478bd9Sstevel@tonic-gate */ 4487c478bd9Sstevel@tonic-gate static int 4497c478bd9Sstevel@tonic-gate rnd_get_bytes(uint8_t *ptr, size_t len, extract_type_t how, 4507c478bd9Sstevel@tonic-gate boolean_t from_user_api) 4517c478bd9Sstevel@tonic-gate { 4527c478bd9Sstevel@tonic-gate int bytes; 4537c478bd9Sstevel@tonic-gate size_t got; 4547c478bd9Sstevel@tonic-gate 4557c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&rndpool_lock)); 4567c478bd9Sstevel@tonic-gate /* 4577c478bd9Sstevel@tonic-gate * Check if the request can be satisfied from the cache 4587c478bd9Sstevel@tonic-gate * of random bytes. 4597c478bd9Sstevel@tonic-gate */ 4607c478bd9Sstevel@tonic-gate if (len <= rnbyte_cnt) { 4617c478bd9Sstevel@tonic-gate rndc_getbytes(ptr, len); 4627c478bd9Sstevel@tonic-gate mutex_exit(&rndpool_lock); 4637c478bd9Sstevel@tonic-gate return (0); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate mutex_exit(&rndpool_lock); 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate switch (how) { 4687c478bd9Sstevel@tonic-gate case BLOCKING_EXTRACT: 4697c478bd9Sstevel@tonic-gate if ((got = rngprov_getbytes(ptr, len, from_user_api, 4707c478bd9Sstevel@tonic-gate B_FALSE)) == -1) 4717c478bd9Sstevel@tonic-gate break; /* No provider found */ 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate if (got == len) 4747c478bd9Sstevel@tonic-gate return (0); 4757c478bd9Sstevel@tonic-gate len -= got; 4767c478bd9Sstevel@tonic-gate ptr += got; 4777c478bd9Sstevel@tonic-gate break; 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate case NONBLOCK_EXTRACT: 4807c478bd9Sstevel@tonic-gate case ALWAYS_EXTRACT: 4817c478bd9Sstevel@tonic-gate if ((got = rngprov_getbytes_nblk(ptr, len, 4827c478bd9Sstevel@tonic-gate from_user_api)) == -1) { 4837c478bd9Sstevel@tonic-gate /* No provider found */ 4847c478bd9Sstevel@tonic-gate if (how == NONBLOCK_EXTRACT) { 4857c478bd9Sstevel@tonic-gate return (EAGAIN); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate } else { 4887c478bd9Sstevel@tonic-gate if (got == len) 4897c478bd9Sstevel@tonic-gate return (0); 4907c478bd9Sstevel@tonic-gate len -= got; 4917c478bd9Sstevel@tonic-gate ptr += got; 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate if (how == NONBLOCK_EXTRACT && (rnbyte_cnt < len)) 4947c478bd9Sstevel@tonic-gate return (EAGAIN); 4957c478bd9Sstevel@tonic-gate break; 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate mutex_enter(&rndpool_lock); 4997c478bd9Sstevel@tonic-gate while (len > 0) { 5007c478bd9Sstevel@tonic-gate if (how == BLOCKING_EXTRACT) { 5017c478bd9Sstevel@tonic-gate /* Check if there is enough */ 5027c478bd9Sstevel@tonic-gate while (rnbyte_cnt < MINEXTRACTBYTES) { 5037c478bd9Sstevel@tonic-gate num_waiters++; 5047c478bd9Sstevel@tonic-gate if (cv_wait_sig(&rndpool_read_cv, 5057c478bd9Sstevel@tonic-gate &rndpool_lock) == 0) { 5067c478bd9Sstevel@tonic-gate num_waiters--; 5077c478bd9Sstevel@tonic-gate mutex_exit(&rndpool_lock); 5087c478bd9Sstevel@tonic-gate return (EINTR); 5097c478bd9Sstevel@tonic-gate } 5107c478bd9Sstevel@tonic-gate num_waiters--; 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate /* Figure out how many bytes to extract */ 5157c478bd9Sstevel@tonic-gate bytes = min(len, rnbyte_cnt); 5167c478bd9Sstevel@tonic-gate rndc_getbytes(ptr, bytes); 5177c478bd9Sstevel@tonic-gate 5187c478bd9Sstevel@tonic-gate len -= bytes; 5197c478bd9Sstevel@tonic-gate ptr += bytes; 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate if (len > 0 && how == ALWAYS_EXTRACT) { 5227c478bd9Sstevel@tonic-gate /* 5237c478bd9Sstevel@tonic-gate * There are not enough bytes, but we can not block. 5247c478bd9Sstevel@tonic-gate * This only happens in the case of /dev/urandom which 5257c478bd9Sstevel@tonic-gate * runs an additional generation algorithm. So, there 5267c478bd9Sstevel@tonic-gate * is no problem. 5277c478bd9Sstevel@tonic-gate */ 5287c478bd9Sstevel@tonic-gate while (len > 0) { 5297c478bd9Sstevel@tonic-gate *ptr = rndpool[findex]; 5307c478bd9Sstevel@tonic-gate ptr++; len--; 5317c478bd9Sstevel@tonic-gate rindex = findex = (findex + 1) & 5327c478bd9Sstevel@tonic-gate (RNDPOOLSIZE - 1); 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate break; 5357c478bd9Sstevel@tonic-gate } 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate mutex_exit(&rndpool_lock); 5397c478bd9Sstevel@tonic-gate return (0); 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate int 5437c478bd9Sstevel@tonic-gate kcf_rnd_get_bytes(uint8_t *ptr, size_t len, boolean_t noblock, 5447c478bd9Sstevel@tonic-gate boolean_t from_user_api) 5457c478bd9Sstevel@tonic-gate { 5467c478bd9Sstevel@tonic-gate extract_type_t how; 5477c478bd9Sstevel@tonic-gate int error; 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate how = noblock ? NONBLOCK_EXTRACT : BLOCKING_EXTRACT; 5507c478bd9Sstevel@tonic-gate mutex_enter(&rndpool_lock); 5517c478bd9Sstevel@tonic-gate if ((error = rnd_get_bytes(ptr, len, how, from_user_api)) != 0) 5527c478bd9Sstevel@tonic-gate return (error); 5537c478bd9Sstevel@tonic-gate 5547c478bd9Sstevel@tonic-gate BUMP_RND_STATS(rs_rndOut, len); 5557c478bd9Sstevel@tonic-gate return (0); 5567c478bd9Sstevel@tonic-gate } 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate /* 5597c478bd9Sstevel@tonic-gate * Revisit this if the structs grow or we come up with a better way 5607c478bd9Sstevel@tonic-gate * of cache-line-padding structures. 5617c478bd9Sstevel@tonic-gate */ 5627c478bd9Sstevel@tonic-gate #define RND_CPU_CACHE_SIZE 64 5637c478bd9Sstevel@tonic-gate #define RND_CPU_PAD_SIZE RND_CPU_CACHE_SIZE*5 5647c478bd9Sstevel@tonic-gate #define RND_CPU_PAD (RND_CPU_PAD_SIZE - \ 5657c478bd9Sstevel@tonic-gate (sizeof (kmutex_t) + 3*sizeof (uint8_t *) + sizeof (HMAC_KEYSCHED) + \ 5667c478bd9Sstevel@tonic-gate sizeof (uint64_t) + 3*sizeof (uint32_t) + sizeof (rnd_stats_t))) 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate /* 5697c478bd9Sstevel@tonic-gate * Per-CPU random state. Somewhat like like kmem's magazines, this provides 5707c478bd9Sstevel@tonic-gate * a per-CPU instance of the pseudo-random generator. We have it much easier 5717c478bd9Sstevel@tonic-gate * than kmem, as we can afford to "leak" random bits if a CPU is DR'ed out. 5727c478bd9Sstevel@tonic-gate * 5737c478bd9Sstevel@tonic-gate * Note that this usage is preemption-safe; a thread 5747c478bd9Sstevel@tonic-gate * entering a critical section remembers which generator it locked 5757c478bd9Sstevel@tonic-gate * and unlocks the same one; should it be preempted and wind up running on 5767c478bd9Sstevel@tonic-gate * a different CPU, there will be a brief period of increased contention 5777c478bd9Sstevel@tonic-gate * before it exits the critical section but nothing will melt. 5787c478bd9Sstevel@tonic-gate */ 5797c478bd9Sstevel@tonic-gate typedef struct rndmag_s 5807c478bd9Sstevel@tonic-gate { 5817c478bd9Sstevel@tonic-gate kmutex_t rm_lock; 5827c478bd9Sstevel@tonic-gate uint8_t *rm_buffer; /* Start of buffer */ 5837c478bd9Sstevel@tonic-gate uint8_t *rm_eptr; /* End of buffer */ 5847c478bd9Sstevel@tonic-gate uint8_t *rm_rptr; /* Current read pointer */ 5857c478bd9Sstevel@tonic-gate HMAC_KEYSCHED rm_ks; /* seed */ 5867c478bd9Sstevel@tonic-gate uint64_t rm_counter; /* rotating counter for extracting */ 5877c478bd9Sstevel@tonic-gate uint32_t rm_oblocks; /* time to rekey? */ 5887c478bd9Sstevel@tonic-gate uint32_t rm_ofuzz; /* Rekey backoff state */ 5897c478bd9Sstevel@tonic-gate uint32_t rm_olimit; /* Hard rekey limit */ 5907c478bd9Sstevel@tonic-gate rnd_stats_t rm_stats; /* Per-CPU Statistics */ 5917c478bd9Sstevel@tonic-gate uint8_t rm_pad[RND_CPU_PAD]; 5927c478bd9Sstevel@tonic-gate } rndmag_t; 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate /* 5957c478bd9Sstevel@tonic-gate * Generate random bytes for /dev/urandom by encrypting a 5967c478bd9Sstevel@tonic-gate * rotating counter with a key created from bytes extracted 5977c478bd9Sstevel@tonic-gate * from the pool. A maximum of PRNG_MAXOBLOCKS output blocks 5987c478bd9Sstevel@tonic-gate * is generated before a new key is obtained. 5997c478bd9Sstevel@tonic-gate * 6007c478bd9Sstevel@tonic-gate * Note that callers to this routine are likely to assume it can't fail. 6017c478bd9Sstevel@tonic-gate * 6027c478bd9Sstevel@tonic-gate * Called with rmp locked; releases lock. 6037c478bd9Sstevel@tonic-gate */ 6047c478bd9Sstevel@tonic-gate static int 6057c478bd9Sstevel@tonic-gate rnd_generate_pseudo_bytes(rndmag_t *rmp, uint8_t *ptr, size_t len) 6067c478bd9Sstevel@tonic-gate { 6077c478bd9Sstevel@tonic-gate size_t bytes = len; 6087c478bd9Sstevel@tonic-gate int nblock, size; 6097c478bd9Sstevel@tonic-gate uint32_t oblocks; 6107c478bd9Sstevel@tonic-gate uint8_t digest[HASHSIZE]; 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate ASSERT(mutex_owned(&rmp->rm_lock)); 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* Nothing is being asked */ 6157c478bd9Sstevel@tonic-gate if (len == 0) { 6167c478bd9Sstevel@tonic-gate mutex_exit(&rmp->rm_lock); 6177c478bd9Sstevel@tonic-gate return (0); 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate nblock = howmany(len, HASHSIZE); 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate rmp->rm_oblocks += nblock; 6237c478bd9Sstevel@tonic-gate oblocks = rmp->rm_oblocks; 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate do { 6267c478bd9Sstevel@tonic-gate if (oblocks >= rmp->rm_olimit) { 6277c478bd9Sstevel@tonic-gate hrtime_t timestamp; 6287c478bd9Sstevel@tonic-gate uint8_t key[HMAC_KEYSIZE]; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate /* 6317c478bd9Sstevel@tonic-gate * Contention-avoiding rekey: see if 6327c478bd9Sstevel@tonic-gate * the pool is locked, and if so, wait a bit. 6337c478bd9Sstevel@tonic-gate * Do an 'exponential back-in' to ensure we don't 6347c478bd9Sstevel@tonic-gate * run too long without rekey. 6357c478bd9Sstevel@tonic-gate */ 6367c478bd9Sstevel@tonic-gate if (rmp->rm_ofuzz) { 6377c478bd9Sstevel@tonic-gate /* 6387c478bd9Sstevel@tonic-gate * Decaying exponential back-in for rekey. 6397c478bd9Sstevel@tonic-gate */ 6407c478bd9Sstevel@tonic-gate if ((rnbyte_cnt < MINEXTRACTBYTES) || 6417c478bd9Sstevel@tonic-gate (!mutex_tryenter(&rndpool_lock))) { 6427c478bd9Sstevel@tonic-gate rmp->rm_olimit += rmp->rm_ofuzz; 6437c478bd9Sstevel@tonic-gate rmp->rm_ofuzz >>= 1; 6447c478bd9Sstevel@tonic-gate goto punt; 6457c478bd9Sstevel@tonic-gate } 6467c478bd9Sstevel@tonic-gate } else { 6477c478bd9Sstevel@tonic-gate mutex_enter(&rndpool_lock); 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate /* Get a new chunk of entropy */ 6517c478bd9Sstevel@tonic-gate (void) rnd_get_bytes(key, HMAC_KEYSIZE, 6527c478bd9Sstevel@tonic-gate ALWAYS_EXTRACT, B_FALSE); 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate /* Set up key */ 6557c478bd9Sstevel@tonic-gate SET_ENCRYPT_KEY(key, HMAC_KEYSIZE, &rmp->rm_ks); 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate /* Get new counter value by encrypting timestamp */ 6587c478bd9Sstevel@tonic-gate timestamp = gethrtime(); 6597c478bd9Sstevel@tonic-gate HMAC_ENCRYPT(&rmp->rm_ks, ×tamp, 6607c478bd9Sstevel@tonic-gate sizeof (timestamp), digest); 6617c478bd9Sstevel@tonic-gate rmp->rm_olimit = PRNG_MAXOBLOCKS/2; 6627c478bd9Sstevel@tonic-gate rmp->rm_ofuzz = PRNG_MAXOBLOCKS/4; 6637c478bd9Sstevel@tonic-gate bcopy(digest, &rmp->rm_counter, sizeof (uint64_t)); 6647c478bd9Sstevel@tonic-gate oblocks = 0; 6657c478bd9Sstevel@tonic-gate rmp->rm_oblocks = nblock; 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate punt: 6687c478bd9Sstevel@tonic-gate /* Hash counter to produce prn stream */ 6697c478bd9Sstevel@tonic-gate if (bytes >= HASHSIZE) { 6707c478bd9Sstevel@tonic-gate size = HASHSIZE; 6717c478bd9Sstevel@tonic-gate HMAC_ENCRYPT(&rmp->rm_ks, &rmp->rm_counter, 6727c478bd9Sstevel@tonic-gate sizeof (rmp->rm_counter), ptr); 6737c478bd9Sstevel@tonic-gate } else { 6747c478bd9Sstevel@tonic-gate size = min(bytes, HASHSIZE); 6757c478bd9Sstevel@tonic-gate HMAC_ENCRYPT(&rmp->rm_ks, &rmp->rm_counter, 6767c478bd9Sstevel@tonic-gate sizeof (rmp->rm_counter), digest); 6777c478bd9Sstevel@tonic-gate bcopy(digest, ptr, size); 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate ptr += size; 6807c478bd9Sstevel@tonic-gate bytes -= size; 6817c478bd9Sstevel@tonic-gate rmp->rm_counter++; 6827c478bd9Sstevel@tonic-gate oblocks++; 6837c478bd9Sstevel@tonic-gate nblock--; 6847c478bd9Sstevel@tonic-gate } while (bytes > 0); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate mutex_exit(&rmp->rm_lock); 6877c478bd9Sstevel@tonic-gate return (0); 6887c478bd9Sstevel@tonic-gate } 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate /* 6917c478bd9Sstevel@tonic-gate * Per-CPU Random magazines. 6927c478bd9Sstevel@tonic-gate */ 6937c478bd9Sstevel@tonic-gate static rndmag_t *rndmag; 6947c478bd9Sstevel@tonic-gate static uint8_t *rndbuf; 6957c478bd9Sstevel@tonic-gate static size_t rndmag_total; 6967c478bd9Sstevel@tonic-gate /* 6977c478bd9Sstevel@tonic-gate * common/os/cpu.c says that platform support code can shrinkwrap 6987c478bd9Sstevel@tonic-gate * max_ncpus. On the off chance that we get loaded very early, we 6997c478bd9Sstevel@tonic-gate * read it exactly once, to copy it here. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate static uint32_t random_max_ncpus = 0; 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate /* 7047c478bd9Sstevel@tonic-gate * Boot-time tunables, for experimentation. 7057c478bd9Sstevel@tonic-gate */ 706*02f574f0Skais size_t rndmag_threshold = 2560; 707*02f574f0Skais size_t rndbuf_len = 5120; 708fa626f0cSkrishna size_t rndmag_size = 1280; 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate int 7127c478bd9Sstevel@tonic-gate kcf_rnd_get_pseudo_bytes(uint8_t *ptr, size_t len) 7137c478bd9Sstevel@tonic-gate { 7147c478bd9Sstevel@tonic-gate rndmag_t *rmp; 7157c478bd9Sstevel@tonic-gate uint8_t *cptr, *eptr; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate /* 7187c478bd9Sstevel@tonic-gate * Anyone who asks for zero bytes of randomness should get slapped. 7197c478bd9Sstevel@tonic-gate */ 7207c478bd9Sstevel@tonic-gate ASSERT(len > 0); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate /* 7237c478bd9Sstevel@tonic-gate * Fast path. 7247c478bd9Sstevel@tonic-gate */ 7257c478bd9Sstevel@tonic-gate for (;;) { 7267c478bd9Sstevel@tonic-gate rmp = &rndmag[CPU->cpu_seqid]; 7277c478bd9Sstevel@tonic-gate mutex_enter(&rmp->rm_lock); 7287c478bd9Sstevel@tonic-gate 7297c478bd9Sstevel@tonic-gate /* 7307c478bd9Sstevel@tonic-gate * Big requests bypass buffer and tail-call the 7317c478bd9Sstevel@tonic-gate * generate routine directly. 7327c478bd9Sstevel@tonic-gate */ 7337c478bd9Sstevel@tonic-gate if (len > rndmag_threshold) { 7347c478bd9Sstevel@tonic-gate BUMP_CPU_RND_STATS(rmp, rs_urndOut, len); 7357c478bd9Sstevel@tonic-gate return (rnd_generate_pseudo_bytes(rmp, ptr, len)); 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate cptr = rmp->rm_rptr; 7397c478bd9Sstevel@tonic-gate eptr = cptr + len; 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate if (eptr <= rmp->rm_eptr) { 7427c478bd9Sstevel@tonic-gate rmp->rm_rptr = eptr; 7437c478bd9Sstevel@tonic-gate bcopy(cptr, ptr, len); 7447c478bd9Sstevel@tonic-gate BUMP_CPU_RND_STATS(rmp, rs_urndOut, len); 7457c478bd9Sstevel@tonic-gate mutex_exit(&rmp->rm_lock); 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate return (0); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate /* 7507c478bd9Sstevel@tonic-gate * End fast path. 7517c478bd9Sstevel@tonic-gate */ 7527c478bd9Sstevel@tonic-gate rmp->rm_rptr = rmp->rm_buffer; 7537c478bd9Sstevel@tonic-gate /* 7547c478bd9Sstevel@tonic-gate * Note: We assume the generate routine always succeeds 7557c478bd9Sstevel@tonic-gate * in this case (because it does at present..) 7567c478bd9Sstevel@tonic-gate * It also always releases rm_lock. 7577c478bd9Sstevel@tonic-gate */ 7587c478bd9Sstevel@tonic-gate (void) rnd_generate_pseudo_bytes(rmp, rmp->rm_buffer, 7597c478bd9Sstevel@tonic-gate rndbuf_len); 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate } 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate /* 7647c478bd9Sstevel@tonic-gate * We set up (empty) magazines for all of max_ncpus, possibly wasting a 7657c478bd9Sstevel@tonic-gate * little memory on big systems that don't have the full set installed. 7667c478bd9Sstevel@tonic-gate * See above; "empty" means "rptr equal to eptr"; this will trigger the 7677c478bd9Sstevel@tonic-gate * refill path in rnd_get_pseudo_bytes above on the first call for each CPU. 7687c478bd9Sstevel@tonic-gate * 7697c478bd9Sstevel@tonic-gate * TODO: make rndmag_size tunable at run time! 7707c478bd9Sstevel@tonic-gate */ 7717c478bd9Sstevel@tonic-gate static void 7727c478bd9Sstevel@tonic-gate rnd_alloc_magazines() 7737c478bd9Sstevel@tonic-gate { 7747c478bd9Sstevel@tonic-gate rndmag_t *rmp; 7757c478bd9Sstevel@tonic-gate int i; 7767c478bd9Sstevel@tonic-gate 7777c478bd9Sstevel@tonic-gate rndbuf_len = roundup(rndbuf_len, HASHSIZE); 7787c478bd9Sstevel@tonic-gate if (rndmag_size < rndbuf_len) 7797c478bd9Sstevel@tonic-gate rndmag_size = rndbuf_len; 7807c478bd9Sstevel@tonic-gate rndmag_size = roundup(rndmag_size, RND_CPU_CACHE_SIZE); 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate random_max_ncpus = max_ncpus; 7837c478bd9Sstevel@tonic-gate rndmag_total = rndmag_size * random_max_ncpus; 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate rndbuf = kmem_alloc(rndmag_total, KM_SLEEP); 7867c478bd9Sstevel@tonic-gate rndmag = kmem_zalloc(sizeof (rndmag_t) * random_max_ncpus, KM_SLEEP); 7877c478bd9Sstevel@tonic-gate 7887c478bd9Sstevel@tonic-gate for (i = 0; i < random_max_ncpus; i++) { 7897c478bd9Sstevel@tonic-gate uint8_t *buf; 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate rmp = &rndmag[i]; 7927c478bd9Sstevel@tonic-gate mutex_init(&rmp->rm_lock, NULL, MUTEX_DRIVER, NULL); 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate buf = rndbuf + i * rndmag_size; 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate rmp->rm_buffer = buf; 7977c478bd9Sstevel@tonic-gate rmp->rm_eptr = buf + rndbuf_len; 7987c478bd9Sstevel@tonic-gate rmp->rm_rptr = buf + rndbuf_len; 7997c478bd9Sstevel@tonic-gate rmp->rm_oblocks = 1; 8007c478bd9Sstevel@tonic-gate } 8017c478bd9Sstevel@tonic-gate } 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate void 8047c478bd9Sstevel@tonic-gate kcf_rnd_schedule_timeout(boolean_t do_mech2id) 8057c478bd9Sstevel@tonic-gate { 8067c478bd9Sstevel@tonic-gate clock_t ut; /* time in microseconds */ 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate if (do_mech2id) 8097c478bd9Sstevel@tonic-gate rngmech_type = crypto_mech2id(SUN_RANDOM); 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate /* 8127c478bd9Sstevel@tonic-gate * The new timeout value is taken from the buffer of random bytes. 8137c478bd9Sstevel@tonic-gate * We're merely reading the first 32 bits from the buffer here, not 8147c478bd9Sstevel@tonic-gate * consuming any random bytes. 8157c478bd9Sstevel@tonic-gate * The timeout multiplier value is a random value between 0.5 sec and 8167c478bd9Sstevel@tonic-gate * 1.544480 sec (0.5 sec + 0xFF000 microseconds). 8177c478bd9Sstevel@tonic-gate * The new timeout is TIMEOUT_INTERVAL times that multiplier. 8187c478bd9Sstevel@tonic-gate */ 8197c478bd9Sstevel@tonic-gate ut = 500000 + (clock_t)((((uint32_t)rndpool[findex]) << 12) & 0xFF000); 8207c478bd9Sstevel@tonic-gate kcf_rndtimeout_id = timeout(rnd_handler, NULL, 8217c478bd9Sstevel@tonic-gate TIMEOUT_INTERVAL * drv_usectohz(ut)); 8227c478bd9Sstevel@tonic-gate } 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate /* 8257c478bd9Sstevel@tonic-gate * &rnd_pollhead is passed in *phpp in order to indicate the calling thread 8267c478bd9Sstevel@tonic-gate * will block. When enough random bytes are available, later, the timeout 8277c478bd9Sstevel@tonic-gate * handler routine will issue the pollwakeup() calls. 8287c478bd9Sstevel@tonic-gate */ 8297c478bd9Sstevel@tonic-gate void 8307c478bd9Sstevel@tonic-gate kcf_rnd_chpoll(int anyyet, short *reventsp, struct pollhead **phpp) 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate /* 8337c478bd9Sstevel@tonic-gate * Sampling of rnbyte_cnt is an atomic 8347c478bd9Sstevel@tonic-gate * operation. Hence we do not need any locking. 8357c478bd9Sstevel@tonic-gate */ 8367c478bd9Sstevel@tonic-gate if (rnbyte_cnt >= MINEXTRACTBYTES) { 8377c478bd9Sstevel@tonic-gate *reventsp |= (POLLIN | POLLRDNORM); 8387c478bd9Sstevel@tonic-gate } else { 8397c478bd9Sstevel@tonic-gate *reventsp = 0; 8407c478bd9Sstevel@tonic-gate if (!anyyet) 8417c478bd9Sstevel@tonic-gate *phpp = &rnd_pollhead; 8427c478bd9Sstevel@tonic-gate } 8437c478bd9Sstevel@tonic-gate } 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8467c478bd9Sstevel@tonic-gate static void 8477c478bd9Sstevel@tonic-gate rnd_handler(void *arg) 8487c478bd9Sstevel@tonic-gate { 8497c478bd9Sstevel@tonic-gate int len = 0; 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if (num_waiters > 0) 8527c478bd9Sstevel@tonic-gate len = MAXEXTRACTBYTES; 8537c478bd9Sstevel@tonic-gate else if (rnbyte_cnt < RNDPOOLSIZE) 8547c478bd9Sstevel@tonic-gate len = MINEXTRACTBYTES; 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate if (len > 0) { 8577c478bd9Sstevel@tonic-gate (void) taskq_dispatch(system_taskq, rngprov_task, 8587c478bd9Sstevel@tonic-gate (void *)(uintptr_t)len, TQ_NOSLEEP); 8597c478bd9Sstevel@tonic-gate } else if (!kcf_rngprov_check()) { 8607c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "No randomness provider enabled for " 8617c478bd9Sstevel@tonic-gate "/dev/random. Use cryptoadm(1M) to enable a provider."); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate 8647c478bd9Sstevel@tonic-gate mutex_enter(&rndpool_lock); 8657c478bd9Sstevel@tonic-gate /* 8667c478bd9Sstevel@tonic-gate * Wake up threads waiting in poll() or for enough accumulated 8677c478bd9Sstevel@tonic-gate * random bytes to read from /dev/random. In case a poll() is 8687c478bd9Sstevel@tonic-gate * concurrent with a read(), the polling process may be woken up 8697c478bd9Sstevel@tonic-gate * indicating that enough randomness is now available for reading, 8707c478bd9Sstevel@tonic-gate * and another process *steals* the bits from the pool, causing the 8717c478bd9Sstevel@tonic-gate * subsequent read() from the first process to block. It is acceptable 8727c478bd9Sstevel@tonic-gate * since the blocking will eventually end, after the timeout 8737c478bd9Sstevel@tonic-gate * has expired enough times to honor the read. 8747c478bd9Sstevel@tonic-gate * 8757c478bd9Sstevel@tonic-gate * Note - Since we hold the rndpool_lock across the pollwakeup() call 8767c478bd9Sstevel@tonic-gate * we MUST NOT grab the rndpool_lock in kcf_rndchpoll(). 8777c478bd9Sstevel@tonic-gate */ 8787c478bd9Sstevel@tonic-gate if (rnbyte_cnt >= MINEXTRACTBYTES) 8797c478bd9Sstevel@tonic-gate pollwakeup(&rnd_pollhead, POLLIN | POLLRDNORM); 8807c478bd9Sstevel@tonic-gate 8817c478bd9Sstevel@tonic-gate if (num_waiters > 0) 8827c478bd9Sstevel@tonic-gate cv_broadcast(&rndpool_read_cv); 8837c478bd9Sstevel@tonic-gate mutex_exit(&rndpool_lock); 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate kcf_rnd_schedule_timeout(B_FALSE); 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate /* Hashing functions */ 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate static void 8917c478bd9Sstevel@tonic-gate hmac_key(uint8_t *key, size_t keylen, void *buf) 8927c478bd9Sstevel@tonic-gate { 8937c478bd9Sstevel@tonic-gate uint32_t *ip, *op; 8947c478bd9Sstevel@tonic-gate uint32_t ipad[HMAC_BLOCK_SIZE/sizeof (uint32_t)]; 8957c478bd9Sstevel@tonic-gate uint32_t opad[HMAC_BLOCK_SIZE/sizeof (uint32_t)]; 8967c478bd9Sstevel@tonic-gate HASH_CTX *icontext, *ocontext; 8977c478bd9Sstevel@tonic-gate int i; 8987c478bd9Sstevel@tonic-gate int nints; 8997c478bd9Sstevel@tonic-gate 9007c478bd9Sstevel@tonic-gate icontext = buf; 9017c478bd9Sstevel@tonic-gate ocontext = (SHA1_CTX *)((uint8_t *)buf + sizeof (HASH_CTX)); 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate bzero((uchar_t *)ipad, HMAC_BLOCK_SIZE); 9047c478bd9Sstevel@tonic-gate bzero((uchar_t *)opad, HMAC_BLOCK_SIZE); 9057c478bd9Sstevel@tonic-gate bcopy(key, (uchar_t *)ipad, keylen); 9067c478bd9Sstevel@tonic-gate bcopy(key, (uchar_t *)opad, keylen); 9077c478bd9Sstevel@tonic-gate 9087c478bd9Sstevel@tonic-gate /* 9097c478bd9Sstevel@tonic-gate * XOR key with ipad (0x36) and opad (0x5c) as defined 9107c478bd9Sstevel@tonic-gate * in RFC 2104. 9117c478bd9Sstevel@tonic-gate */ 9127c478bd9Sstevel@tonic-gate ip = ipad; 9137c478bd9Sstevel@tonic-gate op = opad; 9147c478bd9Sstevel@tonic-gate nints = HMAC_BLOCK_SIZE/sizeof (uint32_t); 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate for (i = 0; i < nints; i++) { 9177c478bd9Sstevel@tonic-gate ip[i] ^= 0x36363636; 9187c478bd9Sstevel@tonic-gate op[i] ^= 0x5c5c5c5c; 9197c478bd9Sstevel@tonic-gate } 9207c478bd9Sstevel@tonic-gate 9217c478bd9Sstevel@tonic-gate /* Perform hash with ipad */ 9227c478bd9Sstevel@tonic-gate HashInit(icontext); 9237c478bd9Sstevel@tonic-gate HashUpdate(icontext, (uchar_t *)ipad, HMAC_BLOCK_SIZE); 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate /* Perform hash with opad */ 9267c478bd9Sstevel@tonic-gate HashInit(ocontext); 9277c478bd9Sstevel@tonic-gate HashUpdate(ocontext, (uchar_t *)opad, HMAC_BLOCK_SIZE); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate static void 9317c478bd9Sstevel@tonic-gate hmac_encr(void *ctx, uint8_t *ptr, size_t len, uint8_t *digest) 9327c478bd9Sstevel@tonic-gate { 9337c478bd9Sstevel@tonic-gate HASH_CTX *saved_contexts; 9347c478bd9Sstevel@tonic-gate HASH_CTX icontext; 9357c478bd9Sstevel@tonic-gate HASH_CTX ocontext; 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate saved_contexts = (HASH_CTX *)ctx; 9387c478bd9Sstevel@tonic-gate icontext = saved_contexts[0]; 9397c478bd9Sstevel@tonic-gate ocontext = saved_contexts[1]; 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate HashUpdate(&icontext, ptr, len); 9427c478bd9Sstevel@tonic-gate HashFinal(digest, &icontext); 9437c478bd9Sstevel@tonic-gate 9447c478bd9Sstevel@tonic-gate /* 9457c478bd9Sstevel@tonic-gate * Perform Hash(K XOR OPAD, DIGEST), where DIGEST is the 9467c478bd9Sstevel@tonic-gate * Hash(K XOR IPAD, DATA). 9477c478bd9Sstevel@tonic-gate */ 9487c478bd9Sstevel@tonic-gate HashUpdate(&ocontext, digest, HASHSIZE); 9497c478bd9Sstevel@tonic-gate HashFinal(digest, &ocontext); 9507c478bd9Sstevel@tonic-gate } 9517c478bd9Sstevel@tonic-gate 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate static void 9547c478bd9Sstevel@tonic-gate rndc_addbytes(uint8_t *ptr, size_t len) 9557c478bd9Sstevel@tonic-gate { 9567c478bd9Sstevel@tonic-gate ASSERT(ptr != NULL && len > 0); 9577c478bd9Sstevel@tonic-gate ASSERT(rnbyte_cnt <= RNDPOOLSIZE); 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate mutex_enter(&rndpool_lock); 9607c478bd9Sstevel@tonic-gate while ((len > 0) && (rnbyte_cnt < RNDPOOLSIZE)) { 9617c478bd9Sstevel@tonic-gate rndpool[rindex] ^= *ptr; 9627c478bd9Sstevel@tonic-gate ptr++; len--; 9637c478bd9Sstevel@tonic-gate rindex = (rindex + 1) & (RNDPOOLSIZE - 1); 9647c478bd9Sstevel@tonic-gate rnbyte_cnt++; 9657c478bd9Sstevel@tonic-gate } 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate /* Handle buffer full case */ 9687c478bd9Sstevel@tonic-gate while (len > 0) { 9697c478bd9Sstevel@tonic-gate rndpool[rindex] ^= *ptr; 9707c478bd9Sstevel@tonic-gate ptr++; len--; 9717c478bd9Sstevel@tonic-gate findex = rindex = (rindex + 1) & (RNDPOOLSIZE - 1); 9727c478bd9Sstevel@tonic-gate } 9737c478bd9Sstevel@tonic-gate mutex_exit(&rndpool_lock); 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate /* 9777c478bd9Sstevel@tonic-gate * Caller should check len <= rnbyte_cnt under the 9787c478bd9Sstevel@tonic-gate * rndpool_lock before calling. 9797c478bd9Sstevel@tonic-gate */ 9807c478bd9Sstevel@tonic-gate static void 9817c478bd9Sstevel@tonic-gate rndc_getbytes(uint8_t *ptr, size_t len) 9827c478bd9Sstevel@tonic-gate { 9837c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&rndpool_lock)); 9847c478bd9Sstevel@tonic-gate ASSERT(len <= rnbyte_cnt && rnbyte_cnt <= RNDPOOLSIZE); 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate BUMP_RND_STATS(rs_rndcOut, len); 9877c478bd9Sstevel@tonic-gate 9887c478bd9Sstevel@tonic-gate while (len > 0) { 9897c478bd9Sstevel@tonic-gate *ptr = rndpool[findex]; 9907c478bd9Sstevel@tonic-gate ptr++; len--; 9917c478bd9Sstevel@tonic-gate findex = (findex + 1) & (RNDPOOLSIZE - 1); 9927c478bd9Sstevel@tonic-gate rnbyte_cnt--; 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate } 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate /* Random number exported entry points */ 9977c478bd9Sstevel@tonic-gate 9987c478bd9Sstevel@tonic-gate /* 9997c478bd9Sstevel@tonic-gate * Mix the supplied bytes into the entropy pool of a kCF 10007c478bd9Sstevel@tonic-gate * RNG provider. 10017c478bd9Sstevel@tonic-gate */ 10027c478bd9Sstevel@tonic-gate /* ARGSUSED */ 10037c478bd9Sstevel@tonic-gate int 10047c478bd9Sstevel@tonic-gate random_add_entropy(uint8_t *ptr, size_t len, uint16_t entropy_est) 10057c478bd9Sstevel@tonic-gate { 10067c478bd9Sstevel@tonic-gate if (len < 1) 10077c478bd9Sstevel@tonic-gate return (-1); 10087c478bd9Sstevel@tonic-gate 10097c478bd9Sstevel@tonic-gate rngprov_seed(ptr, len); 10107c478bd9Sstevel@tonic-gate 10117c478bd9Sstevel@tonic-gate return (0); 10127c478bd9Sstevel@tonic-gate } 10137c478bd9Sstevel@tonic-gate 10147c478bd9Sstevel@tonic-gate /* 10157c478bd9Sstevel@tonic-gate * Get bytes from the /dev/urandom generator. This function 10167c478bd9Sstevel@tonic-gate * always succeeds. Returns 0. 10177c478bd9Sstevel@tonic-gate */ 10187c478bd9Sstevel@tonic-gate int 10197c478bd9Sstevel@tonic-gate random_get_pseudo_bytes(uint8_t *ptr, size_t len) 10207c478bd9Sstevel@tonic-gate { 10217c478bd9Sstevel@tonic-gate ASSERT(!mutex_owned(&rndpool_lock)); 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate if (len < 1) 10247c478bd9Sstevel@tonic-gate return (0); 10257c478bd9Sstevel@tonic-gate return (kcf_rnd_get_pseudo_bytes(ptr, len)); 10267c478bd9Sstevel@tonic-gate } 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate /* 10297c478bd9Sstevel@tonic-gate * Get bytes from the /dev/random generator. Returns 0 10307c478bd9Sstevel@tonic-gate * on success. Returns EAGAIN if there is insufficient entropy. 10317c478bd9Sstevel@tonic-gate */ 10327c478bd9Sstevel@tonic-gate int 10337c478bd9Sstevel@tonic-gate random_get_bytes(uint8_t *ptr, size_t len) 10347c478bd9Sstevel@tonic-gate { 10357c478bd9Sstevel@tonic-gate ASSERT(!mutex_owned(&rndpool_lock)); 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate if (len < 1) 10387c478bd9Sstevel@tonic-gate return (0); 10397c478bd9Sstevel@tonic-gate return (kcf_rnd_get_bytes(ptr, len, B_TRUE, B_FALSE)); 10407c478bd9Sstevel@tonic-gate } 1041