File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / hping2 / random.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Feb 21 22:11:37 2012 UTC (12 years, 10 months ago) by misho
Branches: hping2, MAIN
CVS tags: v2_0_0rc3p7, v2_0_0rc3p5, v2_0_0rc3p4, v2_0_0rc3p0, v2_0_0rc3, HEAD
hping2

/* rc4-based pseudo-random number generator for hping.
 * Copyright (C) 2003 Salvatore Sanfilippo
 * This software is released under the GPL license
 * All rights reserved */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

u_int32_t hp_rand(void);

/* The rc4 sbox */
static unsigned char rc4_sbox[256];
/* This flags is used to initialize the sbox the first time,
 * without an explicit intialization step outside this file. */
static int rc4_seedflag = 0;

/* Initialize the sbox with pseudo random data */
static void hp_rand_init(void)
{
	int i, fd;

	/* Strong sbox initialization */
	fd = open("/dev/urandom", O_RDONLY);
	if (fd != -1) {
		read(fd, rc4_sbox, 256);
		close(fd);
	}
	/* Weaker sbox initialization */
	for (i = 0; i < 256; i++) {
		struct timeval tv;
		gettimeofday(&tv, NULL);
		if (i&1)
			rc4_sbox[i] ^= (tv.tv_usec >> (i&0xF)) & 0xFF;
		else
			rc4_sbox[i] ^= (tv.tv_sec >> (i&0xF)) & 0xFF;
	}
	rc4_seedflag = 1;
}

#if 0
/* Re-seed the generator with user-provided bytes. Not used for now. */
static void hp_rand_seed(void *seed, size_t len)
{
	int i;

	if (len > 256) len = 256;
	memcpy(rc4_sbox, seed, len);
	/* discard the first 256 bytes of output after the reseed */
	for (i = 0; i < 32; i++)
		(void) hp_rand();
}
#endif

/* Generates a 32bit random number using an RC4-like algorithm */
u_int32_t hp_rand(void)
{
	u_int32_t r = 0;
	unsigned char *rc = (unsigned char*) &r;
	static unsigned int i = 0, j = 0;
	unsigned int si, sj, x;

	/* initialization, only needed the first time */
	if (!rc4_seedflag)
		hp_rand_init();
	/* generates 4 bytes of pseudo-random data using RC4 */
	for (x = 0; x < 4; x++) {
		i = (i+1) & 0xff;
		si = rc4_sbox[i];
		j = (j + si) & 0xff;
		sj = rc4_sbox[j];
		rc4_sbox[i] = sj;
		rc4_sbox[j] = si;
		*rc++ = rc4_sbox[(si+sj)&0xff];
	}
	return r;
}


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>