/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Software based random number provider for the Kernel Cryptographic
* Framework (KCF). This provider periodically collects unpredictable input
* from external sources and processes it into a pool of entropy (randomness)
* in order to satisfy requests for random bits from kCF. It implements
* software-based mixing, extraction, and generation algorithms.
*
* A history note: The software-based algorithms in this file used to be
*/
#include <vm/seg_kmem.h>
#include <sys/sysmacros.h>
#include <sys/mem_config.h>
#include <sys/hold_page.h>
#include <rng/fips_random.h>
/* Hash-algo generic definitions. For now, they are SHA1's. */
/* Physical memory entropy source */
typedef struct physmem_entsrc_s {
/* and the global variables */
/* from the pool */
/* from the buffer */
static int snum_waiters;
static int physmem_ent_init(physmem_entsrc_t *);
static void physmem_ent_fini(physmem_entsrc_t *);
static void physmem_ent_gen(physmem_entsrc_t *);
static void physmem_count_blocks();
static void rnd_dr_callback_post_add(void *, pgcnt_t);
static int rnd_dr_callback_pre_del(void *, pgcnt_t);
static void rnd_dr_callback_post_del(void *, pgcnt_t, int);
static void rnd_handler(void *arg);
static void swrand_init();
static void swrand_schedule_timeout(void);
/* Dynamic Reconfiguration related declarations */
};
extern struct mod_ops mod_cryptoops;
/*
* Module linkage information for the kernel.
*/
"Kernel Random number Provider"
};
(void *)&modlcrypto,
};
/*
* CSPI information (entry points, provider info, etc.)
*/
};
static int swrand_generate_random(crypto_provider_handle_t,
};
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};
"Kernel Random Number Provider",
{&modlinkage},
NULL,
0,
};
int
_init(void)
{
int ret;
entropy_bits = 0;
pindex = 0;
bindex = 0;
bstart = 0;
snum_waiters = 0;
leftover_bytes = 0;
buffer_bytes = 0;
/*
* Initialize the pool using
* . 2 unpredictable times: high resolution time since the boot-time,
* and the current time-of-the day.
* . The initial physical memory state.
*/
if (physmem_ent_init(&entsrc) != 0) {
goto exit1;
}
goto exit2;
/* Schedule periodic mixing of the pool. */
/* Register with KCF. If the registration fails, return error. */
(void) mod_remove(&modlinkage);
goto exit2;
}
return (0);
return (ret);
}
int
{
}
/*
* Control entry points.
*/
/* ARGSUSED */
static void
{
}
/*
* Random number entry points.
*/
/* ARGSUSED */
static int
{
/* The entropy estimate is always 0 in this path */
if (flags & CRYPTO_SEED_NOW)
else
return (CRYPTO_SUCCESS);
}
/* ARGSUSED */
static int
{
else
return (CRYPTO_SUCCESS);
}
/*
* Extraction of entropy from the pool.
*
* Returns "len" random bytes in *ptr.
* Try to gather some more entropy by calling physmem_ent_gen() when less than
* MINEXTRACTBITS are present in the pool.
* Will block if not enough entropy was available and the call is blocking.
*/
static int
{
int i, bytes;
int size;
if (leftover_bytes > 0) {
leftover_bytes -= bytes;
if (leftover_bytes > 0)
}
while (len > 0) {
/* Check if there is enough entropy */
while (entropy_bits < MINEXTRACTBITS) {
if (entropy_bits < MINEXTRACTBITS &&
return (EAGAIN);
}
if (entropy_bits < MINEXTRACTBITS) {
snum_waiters++;
if (cv_wait_sig(&srndpool_read_cv,
&srndpool_lock) == 0) {
snum_waiters--;
return (EINTR);
}
snum_waiters--;
}
}
/* Figure out how many bytes to extract */
/* Extract entropy by hashing pool content */
/*
* Feed the digest back into the pool so next
* extraction produces different result
*/
for (i = 0; i < HASHSIZE; i++) {
/* pindex modulo RNDPOOLSIZE */
}
/* LINTED E_BAD_PTR_CAST_ALIGN */
} else {
}
/*
* FIPS 140-2: Continuous RNG test - each generation
* of an n-bit block shall be compared with the previously
* generated block. Test shall fail if any two compared
* n-bit blocks are equal.
*/
for (i = 0; i < HASHSIZE/BYTES_IN_WORD; i++) {
if (tempout[i] != previous_bytes[i])
break;
}
if (i == HASHSIZE/BYTES_IN_WORD) {
"random bytes are same as the previous one.\n");
/* discard random bytes and return error */
return (EIO);
}
}
}
/* Zero out sensitive information */
return (0);
}
while ((len)--) { \
(ptr)++; \
(i) &= (RNDPOOLSIZE - 1); \
}
/* Write some more user-provided entropy to the pool */
static void
{
}
/*
* Add bytes to buffer. Adding the buffer to the random pool
* is deferred until the random pool is mixed.
*/
static void
{
buffer_bytes += len;
}
/* Mix the pool */
static void
{
int i, j, k, start;
/* add deferred bytes */
if (buffer_bytes > 0) {
if (buffer_bytes >= RNDPOOLSIZE) {
for (i = 0; i < RNDPOOLSIZE/4; i++) {
buffer[i] = 0;
}
} else {
for (i = 0; i < buffer_bytes; i++) {
}
}
buffer_bytes = 0;
}
start = 0;
/* Hash a buffer centered on a block in the pool */
else {
RNDPOOLSIZE - start);
}
/* XOR the hash result back into the block */
for (j = 0; j < HASHSIZE; j++) {
k &= (RNDPOOLSIZE - 1);
}
/* Slide the hash buffer and repeat with next block */
}
}
static void
{
}
static void
{
}
/*
* The physmem_* routines below generate entropy by reading blocks of
* physical memory. Entropy is gathered in a couple of ways:
*
* - By reading blocks of physical memory and detecting if changes
* occurred in the blocks read.
*
* - By measuring the time it takes to load and hash a block of memory
* and computing the differences in the measured time.
*
* The first method was used in the CryptoRand implementation. Physical
* memory is divided into blocks of fixed size. A block of memory is
* chosen from the possible blocks and hashed to produce a digest. This
* digest is then mixed into the pool. A single bit from the digest is
* used as a parity bit or "checksum" and compared against the previous
* "checksum" computed for the block. If the single-bit checksum has not
* changed, no entropy is credited to the pool. If there is a change,
* then the assumption is that at least one bit in the block has changed.
* The possible locations within the memory block of where the bit change
* occurred is used as a measure of entropy. For example, if a block
* size of 4096 bytes is used, about log_2(4096*8)=15 bits worth of
* entropy is available. Because the single-bit checksum will miss half
* of the changes, the amount of entropy credited to the pool is doubled
* when a change is detected. With a 4096 byte block size, a block
* change will add a total of 30 bits of entropy to the pool.
*
* The second method measures the amount of time it takes to read and
* hash a physical memory block (as described above). The time measured
* can vary depending on system load, scheduling and other factors.
* Differences between consecutive measurements are computed to come up
* with an entropy estimate. The first, second, and third order delta is
* calculated to determine the minimum delta value. The number of bits
* present in this minimum delta value is the entropy estimate. This
* entropy estimation technique using time deltas is similar to that used
*/
static int
{
int i;
/*
* The maximum entropy amount in bits per block of memory read is
* log_2(MEMBLOCKSIZE * 8);
*/
while (i >>= 1)
entsrc->entperblock++;
/* Initialize entsrc->nblocks */
return (-1);
}
/* Allocate space for the parity vector and memory page */
KM_SLEEP);
/* Initialize parity vector with bits from the pool */
while (i > 0) {
if (i > RNDPOOLSIZE) {
swrand_mix_pool(0);
ptr += RNDPOOLSIZE;
i -= RNDPOOLSIZE;
} else {
break;
}
}
/* Generate some entropy to further initialize the pool */
entropy_bits = 0;
return (0);
}
static void
{
}
static void
{
/*
* Use each 32-bit quantity in the pool to pick a memory
* block to read.
*/
for (i = 0; i < RNDPOOLSIZE/4; i++) {
/* If the pool is "full", stop after one block */
if (i > 0)
break;
}
/*
* This lock protects reading of phys_install.
* Any changes to this list, by DR, are done while
* holding this lock. So, holding this lock is sufficient
* to handle DR also.
*/
/* We're left with less than 4K of memory after DR */
/* Pick a memory block to read */
break;
}
continue;
}
/*
* Do an initial check to see if the address is safe
*/
== PLAT_HOLD_FAIL) {
continue;
}
/*
* Figure out which page to load to read the
* memory block. Load the page and compute the
* hash of the memory block.
*/
len = MEMBLOCKSIZE;
while (len) {
/*
* Re-check the offset, and lock the frame. If the
* page was given away after the above check, we'll
* just bail out.
*/
break;
nbytes);
}
/* We got our pages. Let the DR roll */
/* See if we had to bail out due to a page being given away */
if (len)
continue;
/*
* Compute the time it took to load and hash the
* block and compare it against the previous
* measurement. The delta of the time values
* provides a small amount of entropy. The
* minimum of the first, second, and third order
* delta is used to estimate how much entropy
* is present.
*/
if (delta < 0)
if (delta2 < 0)
if (delta3 < 0)
delta2 = 0;
while (delta >>= 1)
delta2++;
/*
* If the memory block has changed, credit the pool with
* the entropy estimate. The entropy estimate is doubled
* because the single-bit checksum misses half the change
* on average.
*/
digest[0] & 1))
/* Add the entropy bytes to the pool */
}
}
static int
{
/* Test and set the parity bit, return 1 if changed */
return (0);
return (1);
}
/* Compute number of memory blocks available to scan */
static void
{
break;
}
}
}
/*
* Dynamic Reconfiguration call-back functions
*/
/* ARGSUSED */
static void
{
/* More memory is available now, so update entsrc->nblocks. */
}
/* Call-back routine invoked before the DR starts a memory removal. */
/* ARGSUSED */
static int
{
return (0);
}
/* Call-back routine invoked after the DR starts a memory removal. */
/* ARGSUSED */
static void
{
/* Memory has shrunk, so update entsrc->nblocks. */
}
/* Timeout handling to gather entropy from physmem events */
static void
swrand_schedule_timeout(void)
{
/*
* The new timeout value is taken from the pool of random bits.
* We're merely reading the first 32 bits from the pool here, not
* consuming any entropy.
* This routine is usually called right after stirring the pool, so
* srndpool[0] will have a *fresh* random value each time.
* The timeout multiplier value is a random value between 0.7 sec and
* 1.748575 sec (0.7 sec + 0xFFFFF microseconds).
* The new timeout is TIMEOUT_INTERVAL times that multiplier.
*/
}
/*ARGSUSED*/
static void
{
if (snum_waiters > 0)
}