lfsr.c 3.65 KB
Newer Older
Michael Graff's avatar
Michael Graff committed
1
/*
Mark Andrews's avatar
Mark Andrews committed
2
 * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2002  Internet Software Consortium.
4
 *
Michael Graff's avatar
Michael Graff committed
5 6 7
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
Michael Graff's avatar
Michael Graff committed
16 17
 */

18
/* $Id: lfsr.c,v 1.17 2005/06/08 02:06:59 marka Exp $ */
19 20

/*! \file */
David Lawrence's avatar
David Lawrence committed
21

22 23
#include <config.h>

24
#include <stddef.h>
Michael Graff's avatar
Michael Graff committed
25 26 27 28
#include <stdlib.h>

#include <isc/assertions.h>
#include <isc/lfsr.h>
Bob Halley's avatar
Bob Halley committed
29
#include <isc/util.h>
Michael Graff's avatar
Michael Graff committed
30 31 32

#define VALID_LFSR(x)	(x != NULL)

33 34
void
isc_lfsr_init(isc_lfsr_t *lfsr, isc_uint32_t state, unsigned int bits,
35 36
	      isc_uint32_t tap, unsigned int count,
	      isc_lfsrreseed_t reseed, void *arg)
37 38 39 40 41 42 43 44
{
	REQUIRE(VALID_LFSR(lfsr));
	REQUIRE(8 <= bits && bits <= 32);
	REQUIRE(tap != 0);

	lfsr->state = state;
	lfsr->bits = bits;
	lfsr->tap = tap;
45 46 47 48 49 50 51 52
	lfsr->count = count;
	lfsr->reseed = reseed;
	lfsr->arg = arg;

	if (count == 0 && reseed != NULL)
		reseed(lfsr, arg);
	if (lfsr->state == 0)
		lfsr->state = 0xffffffffU >> (32 - lfsr->bits);
53 54
}

55
/*!
Michael Graff's avatar
Michael Graff committed
56 57 58 59 60
 * Return the next state of the lfsr.
 */
static inline isc_uint32_t
lfsr_generate(isc_lfsr_t *lfsr)
{
61
	unsigned int highbit;
Michael Graff's avatar
Michael Graff committed
62

63
	highbit = 1 << (lfsr->bits - 1);
Michael Graff's avatar
Michael Graff committed
64 65 66 67

	/*
	 * If the previous state is zero, we must fill it with something
	 * here, or we will begin to generate an extremely predictable output.
68 69 70
	 *
	 * First, give the reseed function a crack at it.  If the state is
	 * still 0, set it to all ones.
Michael Graff's avatar
Michael Graff committed
71
	 */
72 73 74 75 76 77 78 79
	if (lfsr->state == 0) {
		if (lfsr->reseed != NULL)
			lfsr->reseed(lfsr, lfsr->arg);
		if (lfsr->state == 0)
			lfsr->state = 0xffffffffU >> (32 - lfsr->bits);
	}

	if (lfsr->state & 0x01) {
80
		lfsr->state = (lfsr->state >> 1) ^ lfsr->tap;
81 82
		return (1);
	} else {
Michael Graff's avatar
Michael Graff committed
83
		lfsr->state >>= 1;
84 85
		return (0);
	}
Michael Graff's avatar
Michael Graff committed
86 87
}

88 89
void
isc_lfsr_generate(isc_lfsr_t *lfsr, void *data, unsigned int count)
Michael Graff's avatar
Michael Graff committed
90
{
91 92 93
	unsigned char *p;
	unsigned int bit;
	unsigned int byte;
Michael Graff's avatar
Michael Graff committed
94

95 96 97 98 99 100 101 102 103
	REQUIRE(VALID_LFSR(lfsr));
	REQUIRE(data != NULL);
	REQUIRE(count > 0);

	p = data;
	byte = count;

	while (byte--) {
		*p = 0;
104
		for (bit = 0; bit < 7; bit++) {
105 106 107 108 109 110 111 112 113 114 115 116 117
			*p |= lfsr_generate(lfsr);
			*p <<= 1;
		}
		*p |= lfsr_generate(lfsr);
		p++;
	}

	if (lfsr->count != 0 && lfsr->reseed != NULL) {
		if (lfsr->count <= count * 8)
			lfsr->reseed(lfsr, lfsr->arg);
		else
			lfsr->count -= (count * 8);
	}
Michael Graff's avatar
Michael Graff committed
118 119 120 121 122 123 124 125
}

static inline isc_uint32_t
lfsr_skipgenerate(isc_lfsr_t *lfsr, unsigned int skip)
{
	while (skip--)
		(void)lfsr_generate(lfsr);

126 127 128
	(void)lfsr_generate(lfsr);

	return (lfsr->state);
Michael Graff's avatar
Michael Graff committed
129 130 131
}

/*
132
 * Skip "skip" states in "lfsr".
Michael Graff's avatar
Michael Graff committed
133
 */
134 135
void
isc_lfsr_skip(isc_lfsr_t *lfsr, unsigned int skip)
Michael Graff's avatar
Michael Graff committed
136 137 138
{
	REQUIRE(VALID_LFSR(lfsr));

139 140
	while (skip--)
		(void)lfsr_generate(lfsr);
Michael Graff's avatar
Michael Graff committed
141 142 143 144 145 146 147
}

/*
 * Skip states in lfsr1 and lfsr2 using the other's current state.
 * Return the final state of lfsr1 ^ lfsr2.
 */
isc_uint32_t
148
isc_lfsr_generate32(isc_lfsr_t *lfsr1, isc_lfsr_t *lfsr2)
Michael Graff's avatar
Michael Graff committed
149 150 151 152 153 154 155
{
	isc_uint32_t state1, state2;
	isc_uint32_t skip1, skip2;

	REQUIRE(VALID_LFSR(lfsr1));
	REQUIRE(VALID_LFSR(lfsr2));

156 157
	skip1 = lfsr1->state & 0x01;
	skip2 = lfsr2->state & 0x01;
Michael Graff's avatar
Michael Graff committed
158 159 160 161 162 163 164

	/* cross-skip. */
	state1 = lfsr_skipgenerate(lfsr1, skip2);
	state2 = lfsr_skipgenerate(lfsr2, skip1);

	return (state1 ^ state2);
}