2002-11-10 06:28:59 +03:00
|
|
|
/* $NetBSD: rndpool.c,v 1.17 2002/11/10 03:29:00 thorpej Exp $ */
|
1997-10-10 03:13:12 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1997 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
1997-10-13 23:59:26 +04:00
|
|
|
* by Michael Graff <explorer@flame.org>. This code uses ideas and
|
|
|
|
* algorithms from the Linux driver written by Ted Ts'o.
|
1997-10-10 03:13:12 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 08:32:49 +03:00
|
|
|
#include <sys/cdefs.h>
|
2002-11-10 06:28:59 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: rndpool.c,v 1.17 2002/11/10 03:29:00 thorpej Exp $");
|
2001-11-13 08:32:49 +03:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
1998-05-27 04:59:14 +04:00
|
|
|
#include <sys/sha1.h>
|
1997-10-10 03:13:12 +04:00
|
|
|
|
|
|
|
#include <sys/rnd.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The random pool "taps"
|
|
|
|
*/
|
2001-09-09 04:32:52 +04:00
|
|
|
#define TAP1 99
|
|
|
|
#define TAP2 59
|
|
|
|
#define TAP3 31
|
|
|
|
#define TAP4 9
|
|
|
|
#define TAP5 7
|
1997-10-10 03:13:12 +04:00
|
|
|
|
|
|
|
static inline void rndpool_add_one_word(rndpool_t *, u_int32_t);
|
|
|
|
|
|
|
|
void
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_init(rndpool_t *rp)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
1999-01-27 13:41:00 +03:00
|
|
|
|
2000-06-06 03:42:34 +04:00
|
|
|
rp->cursor = 0;
|
2002-10-09 18:48:58 +04:00
|
|
|
rp->rotate = 1;
|
2000-06-06 03:42:34 +04:00
|
|
|
|
|
|
|
memset(&rp->stats, 0, sizeof(rp->stats));
|
|
|
|
|
|
|
|
rp->stats.curentropy = 0;
|
|
|
|
rp->stats.poolsize = RND_POOLWORDS;
|
|
|
|
rp->stats.threshold = RND_ENTROPY_THRESHOLD;
|
|
|
|
rp->stats.maxentropy = RND_POOLBITS;
|
|
|
|
|
2002-10-08 16:12:56 +04:00
|
|
|
KASSERT(RND_ENTROPY_THRESHOLD*2 <= 20); /* XXX sha knowledge */
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_get_entropy_count(rndpool_t *rp)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
1999-01-27 13:41:00 +03:00
|
|
|
|
2001-09-09 04:32:52 +04:00
|
|
|
return (rp->stats.curentropy);
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
|
2001-09-09 04:32:52 +04:00
|
|
|
void rndpool_get_stats(rndpool_t *rp, void *rsp, int size)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
2001-09-09 04:32:52 +04:00
|
|
|
|
2000-06-06 03:42:34 +04:00
|
|
|
memcpy(rsp, &rp->stats, size);
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_increment_entropy_count(rndpool_t *rp, u_int32_t entropy)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
1999-01-27 13:41:00 +03:00
|
|
|
|
2000-06-06 03:42:34 +04:00
|
|
|
rp->stats.curentropy += entropy;
|
|
|
|
rp->stats.added += entropy;
|
|
|
|
if (rp->stats.curentropy > RND_POOLBITS) {
|
|
|
|
rp->stats.discarded += (rp->stats.curentropy - RND_POOLBITS);
|
|
|
|
rp->stats.curentropy = RND_POOLBITS;
|
|
|
|
}
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t *
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_get_pool(rndpool_t *rp)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
1999-01-27 13:41:00 +03:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
return (rp->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_int32_t
|
|
|
|
rndpool_get_poolsize(void)
|
|
|
|
{
|
1999-01-27 13:41:00 +03:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
return (RND_POOLWORDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-10-09 18:48:58 +04:00
|
|
|
* The input function treats the contents of the pool as an array of
|
|
|
|
* 32 LFSR's of length RND_POOLWORDS, one per bit-plane. The LFSR's
|
|
|
|
* are clocked once in parallel, using 32-bit xor operations, for each
|
|
|
|
* word to be added.
|
|
|
|
*
|
|
|
|
* Each word to be added is xor'd with the output word of the LFSR
|
|
|
|
* array (one tap at a time).
|
|
|
|
*
|
|
|
|
* In order to facilitate distribution of entropy between the
|
|
|
|
* bit-planes, a 32-bit rotate of this result is performed prior to
|
|
|
|
* feedback. The rotation distance is incremented every RND_POOLWORDS
|
|
|
|
* clocks, by a value that is relativly prime to the word size to try
|
|
|
|
* to spread the bits throughout the pool quickly when the pool is
|
|
|
|
* empty.
|
|
|
|
*
|
|
|
|
* Each LFSR thus takes its feedback from another LFSR, and is
|
|
|
|
* effectively re-keyed by both that LFSR and the new data. Feedback
|
|
|
|
* occurs with another XOR into the new LFSR, rather than assignment,
|
|
|
|
* to avoid destroying any entropy in the destination.
|
|
|
|
*
|
|
|
|
* Even with zeros as input, the LFSR output data are never visible;
|
|
|
|
* the contents of the pool are never divulged except via a hash of
|
|
|
|
* the entire pool, so there is no information for correlation
|
|
|
|
* attacks. With rotation-based rekeying, each LFSR runs at most a few
|
|
|
|
* cycles before being permuted. However, beware of initial
|
|
|
|
* conditions when no entropy has been added.
|
|
|
|
*
|
|
|
|
* The output function also stirs the generated hash back into the
|
|
|
|
* pool, further permuting the LFSRs and spreading entropy through the
|
|
|
|
* pool. Any unknown bits anywhere in the pool are thus reflected
|
|
|
|
* across all the LFSRs after output.
|
|
|
|
*
|
|
|
|
* (The final XOR assignment into the pool for feedback is equivalent
|
|
|
|
* to an additional LFSR tap of the MSB before shifting, in the case
|
|
|
|
* where no rotation is done, once every 32 cycles. This LFSR runs for
|
|
|
|
* at most one length.)
|
1997-10-10 03:13:12 +04:00
|
|
|
*/
|
|
|
|
static inline void
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_add_one_word(rndpool_t *rp, u_int32_t val)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
2002-10-09 18:48:58 +04:00
|
|
|
/*
|
|
|
|
* Shifting is implemented using a cursor and taps as offsets,
|
|
|
|
* added mod the size of the pool. For this reason,
|
|
|
|
* RND_POOLWORDS must be a power of two.
|
1997-10-10 03:13:12 +04:00
|
|
|
*/
|
|
|
|
val ^= rp->pool[(rp->cursor + TAP1) & (RND_POOLWORDS - 1)];
|
|
|
|
val ^= rp->pool[(rp->cursor + TAP2) & (RND_POOLWORDS - 1)];
|
|
|
|
val ^= rp->pool[(rp->cursor + TAP3) & (RND_POOLWORDS - 1)];
|
|
|
|
val ^= rp->pool[(rp->cursor + TAP4) & (RND_POOLWORDS - 1)];
|
1997-10-20 19:03:19 +04:00
|
|
|
val ^= rp->pool[(rp->cursor + TAP5) & (RND_POOLWORDS - 1)];
|
2000-06-10 21:01:15 +04:00
|
|
|
if (rp->rotate != 0)
|
|
|
|
val = ((val << rp->rotate) | (val >> (32 - rp->rotate)));
|
|
|
|
rp->pool[rp->cursor++] ^= val;
|
2001-09-09 04:32:52 +04:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
/*
|
|
|
|
* If we have looped around the pool, increment the rotate
|
2000-06-06 03:42:34 +04:00
|
|
|
* variable so the next value will get xored in rotated to
|
|
|
|
* a different position.
|
1997-10-10 03:13:12 +04:00
|
|
|
*/
|
|
|
|
if (rp->cursor == RND_POOLWORDS) {
|
|
|
|
rp->cursor = 0;
|
|
|
|
rp->rotate = (rp->rotate + 7) & 31;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-06 03:42:34 +04:00
|
|
|
#if 0
|
1997-10-10 03:13:12 +04:00
|
|
|
/*
|
2000-06-06 03:42:34 +04:00
|
|
|
* Stir a 32-bit value (with possibly less entropy than that) into the pool.
|
|
|
|
* Update entropy estimate.
|
1997-10-10 03:13:12 +04:00
|
|
|
*/
|
|
|
|
void
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_add_uint32(rndpool_t *rp, u_int32_t val, u_int32_t entropy)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
|
|
|
rndpool_add_one_word(rp, val);
|
2000-06-06 03:42:34 +04:00
|
|
|
|
|
|
|
rp->entropy += entropy;
|
|
|
|
rp->stats.added += entropy;
|
|
|
|
if (rp->entropy > RND_POOLBITS) {
|
|
|
|
rp->stats.discarded += (rp->entropy - RND_POOLBITS);
|
|
|
|
rp->entropy = RND_POOLBITS;
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
}
|
2000-06-06 03:42:34 +04:00
|
|
|
#endif
|
1997-10-10 03:13:12 +04:00
|
|
|
|
|
|
|
/*
|
2001-09-09 04:32:52 +04:00
|
|
|
* Add a buffer's worth of data to the pool.
|
1997-10-10 03:13:12 +04:00
|
|
|
*/
|
|
|
|
void
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_add_data(rndpool_t *rp, void *p, u_int32_t len, u_int32_t entropy)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
2001-09-09 04:32:52 +04:00
|
|
|
u_int32_t val;
|
|
|
|
u_int8_t *buf;
|
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
buf = p;
|
2001-09-09 04:32:52 +04:00
|
|
|
|
|
|
|
for (; len > 3; len -= 4) {
|
1997-10-10 03:13:12 +04:00
|
|
|
val = *((u_int32_t *)buf);
|
|
|
|
|
|
|
|
rndpool_add_one_word(rp, val);
|
|
|
|
buf += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len != 0) {
|
2000-06-06 03:42:34 +04:00
|
|
|
val = 0;
|
1997-10-10 03:13:12 +04:00
|
|
|
switch (len) {
|
|
|
|
case 3:
|
|
|
|
val = *buf++;
|
|
|
|
case 2:
|
|
|
|
val = val << 8 | *buf++;
|
|
|
|
case 1:
|
|
|
|
val = val << 8 | *buf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
rndpool_add_one_word(rp, val);
|
|
|
|
}
|
|
|
|
|
2000-06-06 03:42:34 +04:00
|
|
|
rp->stats.curentropy += entropy;
|
|
|
|
rp->stats.added += entropy;
|
|
|
|
|
|
|
|
if (rp->stats.curentropy > RND_POOLBITS) {
|
|
|
|
rp->stats.discarded += (rp->stats.curentropy - RND_POOLBITS);
|
|
|
|
rp->stats.curentropy = RND_POOLBITS;
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract some number of bytes from the random pool, decreasing the
|
|
|
|
* estimate of randomness as each byte is extracted.
|
|
|
|
*
|
2002-10-09 18:48:58 +04:00
|
|
|
* Do this by hashing the pool and returning a part of the hash as
|
|
|
|
* randomness. Stir the hash back into the pool. Note that no
|
|
|
|
* secrets going back into the pool are given away here since parts of
|
|
|
|
* the hash are xored together before being returned.
|
1997-10-10 03:13:12 +04:00
|
|
|
*
|
|
|
|
* Honor the request from the caller to only return good data, any data,
|
|
|
|
* etc. Note that we must have at least 64 bits of entropy in the pool
|
|
|
|
* before we return anything in the high-quality modes.
|
|
|
|
*/
|
2002-11-10 06:28:59 +03:00
|
|
|
u_int32_t
|
2001-09-09 04:32:52 +04:00
|
|
|
rndpool_extract_data(rndpool_t *rp, void *p, u_int32_t len, u_int32_t mode)
|
1997-10-10 03:13:12 +04:00
|
|
|
{
|
2001-09-09 04:32:52 +04:00
|
|
|
u_int i;
|
|
|
|
SHA1_CTX hash;
|
|
|
|
u_char digest[20]; /* XXX SHA knowledge */
|
|
|
|
u_int32_t remain, deltae, count;
|
|
|
|
u_int8_t *buf;
|
|
|
|
int good;
|
1997-10-10 03:13:12 +04:00
|
|
|
|
|
|
|
buf = p;
|
|
|
|
remain = len;
|
|
|
|
|
|
|
|
if (mode == RND_EXTRACT_ANY)
|
|
|
|
good = 1;
|
|
|
|
else
|
2000-06-06 03:42:34 +04:00
|
|
|
good = (rp->stats.curentropy >= (8 * RND_ENTROPY_THRESHOLD));
|
1997-10-10 03:13:12 +04:00
|
|
|
|
2002-10-08 16:12:56 +04:00
|
|
|
KASSERT(RND_ENTROPY_THRESHOLD*2 <= 20); /* XXX SHA knowledge */
|
2001-09-09 04:32:52 +04:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
while (good && (remain != 0)) {
|
2000-06-06 03:42:34 +04:00
|
|
|
/*
|
|
|
|
* While bytes are requested, compute the hash of the pool,
|
|
|
|
* and then "fold" the hash in half with XOR, keeping the
|
|
|
|
* exact hash value secret, as it will be stirred back into
|
|
|
|
* the pool.
|
|
|
|
*
|
|
|
|
* XXX this approach needs examination by competant
|
|
|
|
* cryptographers! It's rather expensive per bit but
|
|
|
|
* also involves every bit of the pool in the
|
|
|
|
* computation of every output bit..
|
|
|
|
*/
|
1998-05-27 04:59:14 +04:00
|
|
|
SHA1Init(&hash);
|
|
|
|
SHA1Update(&hash, (u_int8_t *)rp->pool, RND_POOLWORDS * 4);
|
2000-06-06 03:42:34 +04:00
|
|
|
SHA1Final(digest, &hash);
|
2001-09-09 04:32:52 +04:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
/*
|
2000-06-06 03:42:34 +04:00
|
|
|
* Stir the hash back into the pool. This guarantees
|
|
|
|
* that the next hash will generate a different value
|
|
|
|
* if no new values were added to the pool.
|
1997-10-10 03:13:12 +04:00
|
|
|
*/
|
2001-09-09 04:32:52 +04:00
|
|
|
for (i = 0; i < 5; i++) {
|
2000-06-06 03:42:34 +04:00
|
|
|
u_int32_t word;
|
2001-09-09 04:32:52 +04:00
|
|
|
memcpy(&word, &digest[i * 4], 4);
|
2000-06-06 03:42:34 +04:00
|
|
|
rndpool_add_one_word(rp, word);
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
|
|
|
|
2000-06-06 03:42:34 +04:00
|
|
|
count = min(remain, RND_ENTROPY_THRESHOLD);
|
|
|
|
|
2001-09-09 04:32:52 +04:00
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
buf[i] = digest[i] ^ digest[i + RND_ENTROPY_THRESHOLD];
|
2000-06-06 03:42:34 +04:00
|
|
|
|
|
|
|
buf += count;
|
|
|
|
deltae = count * 8;
|
|
|
|
remain -= count;
|
|
|
|
|
|
|
|
deltae = min(deltae, rp->stats.curentropy);
|
|
|
|
|
|
|
|
rp->stats.removed += deltae;
|
|
|
|
rp->stats.curentropy -= deltae;
|
|
|
|
|
|
|
|
if (rp->stats.curentropy == 0)
|
|
|
|
rp->stats.generated += (count * 8) - deltae;
|
2001-09-09 04:32:52 +04:00
|
|
|
|
1997-10-10 03:13:12 +04:00
|
|
|
if (mode == RND_EXTRACT_GOOD)
|
2000-06-06 03:42:34 +04:00
|
|
|
good = (rp->stats.curentropy >=
|
|
|
|
(8 * RND_ENTROPY_THRESHOLD));
|
1997-10-10 03:13:12 +04:00
|
|
|
}
|
2001-09-09 04:32:52 +04:00
|
|
|
|
2001-07-07 21:04:01 +04:00
|
|
|
memset(&hash, 0, sizeof(hash));
|
|
|
|
memset(digest, 0, sizeof(digest));
|
1997-10-10 03:13:12 +04:00
|
|
|
|
|
|
|
return (len - remain);
|
|
|
|
}
|