linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matt Mackall <mpm@selenic.com>
To: Andrew Morton <akpm@osdl.org>, "Theodore Ts'o" <tytso@mit.edu>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 11/12] random pt3: Clean up hash buffering
Date: Wed, 19 Jan 2005 00:17:24 -0800	[thread overview]
Message-ID: <12.64403262@selenic.com> (raw)
In-Reply-To: <11.64403262@selenic.com>

Clean up buffer usage for SHA and reseed. This makes the code more
readable and reduces worst-case stack usage.

Signed-off-by: Matt Mackall <mpm@selenic.com>

Index: rnd/drivers/char/random.c
===================================================================
--- rnd.orig/drivers/char/random.c	2005-01-18 10:42:39.078612373 -0800
+++ rnd/drivers/char/random.c	2005-01-18 10:45:13.176966505 -0800
@@ -255,6 +255,7 @@
 #define INPUT_POOL_WORDS 128
 #define OUTPUT_POOL_WORDS 32
 #define BATCH_ENTROPY_SIZE 256
+#define SEC_XFER_SIZE 512
 
 /*
  * The minimum number of bits of entropy before we wake up a read on
@@ -813,6 +814,7 @@
  */
 
 #define HASH_BUFFER_SIZE 5
+#define EXTRACT_SIZE 10
 #define HASH_EXTRA_SIZE 80
 
 /* Various size/speed tradeoffs are available.  Choose 0..3. */
@@ -1048,9 +1050,6 @@
  *
  *********************************************************************/
 
-#define TMP_BUF_SIZE			(HASH_BUFFER_SIZE + HASH_EXTRA_SIZE)
-#define SEC_XFER_SIZE			(TMP_BUF_SIZE*4)
-
 static ssize_t extract_entropy(struct entropy_store *r, void * buf,
 			       size_t nbytes, int min, int rsvd);
 
@@ -1059,13 +1058,14 @@
  * from the primary pool to the secondary extraction pool. We make
  * sure we pull enough for a 'catastrophic reseed'.
  */
-static void xfer_secondary_pool(struct entropy_store *r,
-				       size_t nbytes, __u32 *tmp)
+static void xfer_secondary_pool(struct entropy_store *r, size_t nbytes)
 {
+	__u32 tmp[OUTPUT_POOL_WORDS];
+
 	if (r->pull && r->entropy_count < nbytes * 8 &&
 	    r->entropy_count < r->poolinfo->POOLBITS) {
 		int bytes = max_t(int, random_read_wakeup_thresh / 8,
-				min_t(int, nbytes, TMP_BUF_SIZE));
+				min_t(int, nbytes, sizeof(tmp)));
 		int rsvd = r->limit ? 0 : random_read_wakeup_thresh/4;
 
 		DEBUG_ENT("going to reseed %s with %d bits "
@@ -1129,10 +1129,10 @@
 	return nbytes;
 }
 
-static void extract_buf(struct entropy_store *r, __u32 *buf)
+static void extract_buf(struct entropy_store *r, __u8 *out)
 {
 	int i, x;
-	__u32 data[16];
+	__u32 data[16], buf[85];
 
 	/* Hash the pool to get the output */
 	buf[0] = 0x67452301;
@@ -1151,7 +1151,7 @@
 	 */
 	for (i = 0, x = 0; i < r->poolinfo->poolwords; i += 16, x+=2) {
 		sha_transform(buf, r->pool+i);
-		add_entropy_words(r, &buf[x%HASH_BUFFER_SIZE], 1);
+		add_entropy_words(r, &buf[x % 5], 1);
 	}
 
 	/*
@@ -1159,7 +1159,7 @@
 	 * portion of the pool while mixing, and hash one
 	 * final time.
 	 */
-	__add_entropy_words(r, &buf[x%HASH_BUFFER_SIZE], 1, data);
+	__add_entropy_words(r, &buf[x % 5], 1, data);
 	sha_transform(buf, data);
 
 	/*
@@ -1170,21 +1170,23 @@
 	buf[0] ^= buf[3];
 	buf[1] ^= buf[4];
 	buf[0] ^= rol32(buf[3], 16);
+	memcpy(out, buf, EXTRACT_SIZE);
+	memset(buf, 0, sizeof(buf));
 }
 
 static ssize_t extract_entropy(struct entropy_store *r, void * buf,
 			       size_t nbytes, int min, int reserved)
 {
 	ssize_t ret = 0, i;
-	__u32 tmp[TMP_BUF_SIZE];
+	__u8 tmp[EXTRACT_SIZE];
 
-	xfer_secondary_pool(r, nbytes, tmp);
+	xfer_secondary_pool(r, nbytes);
 	nbytes = account(r, nbytes, min, reserved);
 
 	while (nbytes) {
 		extract_buf(r, tmp);
-		i = min(nbytes, HASH_BUFFER_SIZE * sizeof(__u32) / 2);
-		memcpy(buf, (__u8 const *)tmp, i);
+		i = min_t(int, nbytes, EXTRACT_SIZE);
+		memcpy(buf, tmp, i);
 		nbytes -= i;
 		buf += i;
 		ret += i;
@@ -1200,9 +1202,9 @@
 				    size_t nbytes)
 {
 	ssize_t ret = 0, i;
-	__u32 tmp[TMP_BUF_SIZE];
+	__u8 tmp[EXTRACT_SIZE];
 
-	xfer_secondary_pool(r, nbytes, tmp);
+	xfer_secondary_pool(r, nbytes);
 	nbytes = account(r, nbytes, 0, 0);
 
 	while (nbytes) {
@@ -1216,7 +1218,7 @@
 		}
 
 		extract_buf(r, tmp);
-		i = min(nbytes, HASH_BUFFER_SIZE * sizeof(__u32) / 2);
+		i = min_t(int, nbytes, EXTRACT_SIZE);
 		if (copy_to_user(buf, tmp, i)) {
 			ret = -EFAULT;
 			break;

  reply	other threads:[~2005-01-19  8:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-19  8:17 [PATCH 0/12] random pt3: More core and accounting cleanups Matt Mackall
2005-01-19  8:17 ` [PATCH 1/12] random pt3: More meaningful pool names Matt Mackall
2005-01-19  8:17   ` [PATCH 2/12] random pt3: Static allocation of pools Matt Mackall
2005-01-19  8:17     ` [PATCH 3/12] random pt3: Static sysctl bits Matt Mackall
2005-01-19  8:17       ` [PATCH 4/12] random pt3: Catastrophic reseed checks Matt Mackall
2005-01-19  8:17         ` [PATCH 5/12] random pt3: Entropy reservation accounting Matt Mackall
2005-01-19  8:17           ` [PATCH 6/12] random pt3: Reservation flag in pool struct Matt Mackall
2005-01-19  8:17             ` [PATCH 7/12] random pt3: Reseed pointer " Matt Mackall
2005-01-19  8:17               ` [PATCH 8/12] random pt3: Break up extract_user Matt Mackall
2005-01-19  8:17                 ` [PATCH 9/12] random pt3: Remove dead MD5 copy Matt Mackall
2005-01-19  8:17                   ` [PATCH 10/12] random pt3: Simplify hash folding Matt Mackall
2005-01-19  8:17                     ` Matt Mackall [this message]
2005-01-19  8:17                       ` [PATCH 12/12] random pt3: Remove entropy batching Matt Mackall

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=12.64403262@selenic.com \
    --to=mpm@selenic.com \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tytso@mit.edu \
    /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;
as well as URLs for NNTP newsgroup(s).