Re: [PATCH 1/3] lib: random utilities

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Apr 05, 2012 at 11:52:04PM +0200, Davidlohr Bueso wrote:
v> +/*
> + * Generate a stream of random nbytes into buf.
> + * Use /dev/urandom if possible, and if not,
> + * use glibc pseudo-random functions.
> + */
> +void random_get_bytes(void *buf, int nbytes)
> +{
> +	int i, n = nbytes, fd = random_get_fd();
> +	int lose_counter = 0;
> +	unsigned char *cp = (unsigned char *) buf;
> +
> +	if (fd >= 0) {
> +		while (n > 0) {
> +			i = read(fd, cp, n);
> +			if (i <= 0) {
> +				if (lose_counter++ > 16)
> +					break;
> +				continue;
> +			}
> +			n -= i;
> +			cp += i;
> +			lose_counter = 0;
> +		}
> +	}
> +
> +	/*
> +	 * We do this all the time, but this is the only source of
> +	 * randomness if /dev/random/urandom is out to lunch.
> +	 */
> +	for (cp = buf, i = 0; i < nbytes; i++)
> +		*cp++ ^= (rand() >> 7) & 0xFF;
> +
> +#ifdef DO_JRAND_MIX
> +	{
> +		unsigned short tmp_seed[3];
> +
> +		memcpy(tmp_seed, jrand_seed, sizeof(tmp_seed));
> +		jrand_seed[2] = jrand_seed[2] ^ syscall(__NR_gettid);
> +		for (cp = buf, i = 0; i < nbytes; i++)
> +			*cp++ ^= (jrand48(tmp_seed) >> 7) & 0xFF;
> +		memcpy(jrand_seed, tmp_seed,
> +		       sizeof(jrand_seed)-sizeof(unsigned short));
> +	}
> +#endif

    close(fd);
> +
> +	return;
> +}

    Karel

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux