public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Karel Zak <kzak@redhat.com>
To: Davidlohr Bueso <dave@gnu.org>
Cc: util-linux <util-linux@vger.kernel.org>
Subject: Re: [PATCH 1/3] lib: random utilities
Date: Tue, 10 Apr 2012 12:38:38 +0200	[thread overview]
Message-ID: <20120410103838.GK19784@x2.net.home> (raw)
In-Reply-To: <1333662724.2696.4.camel@offbook>

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@redhat.com>
 http://karelzak.blogspot.com

  reply	other threads:[~2012-04-10 10:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-05 21:52 [PATCH 1/3] lib: random utilities Davidlohr Bueso
2012-04-10 10:38 ` Karel Zak [this message]
2012-04-10 11:23 ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120410103838.GK19784@x2.net.home \
    --to=kzak@redhat.com \
    --cc=dave@gnu.org \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox