linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matt Mackall <mpm@selenic.com>
To: Jamie Lokier <jamie@shareable.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Andrew Morton <akpm@osdl.org>,
	jmorris@intercode.com.au, davem@redhat.com
Subject: Re: [RFC][PATCH] Make cryptoapi non-optional?
Date: Sat, 9 Aug 2003 12:33:29 -0500	[thread overview]
Message-ID: <20030809173329.GU31810@waste.org> (raw)
In-Reply-To: <20030809171318.GD29647@mail.jlokier.co.uk>

On Sat, Aug 09, 2003 at 06:13:18PM +0100, Jamie Lokier wrote:
> Matt Mackall wrote:
> > This code is already at about 125% of baseline throughput, and can
> > probably reach 250% with some tweaking of cryptoapi's redundant
> > padding (in case anyone else cares about being able to get 120Mb/s
> > of cryptographically strong random data).
> 
> Why would the cryptoapi version be faster, I wondered?  So I had a look.
> No conclusions, just observations.
> 
>   - random.c defines a constant, SHA_CODE_SIZE.  This selects between
>     4 implementations, from smaller to (presumably) faster by
>     progressively unrolling more and more.
> 
>     This is set to SHA_CODE_SIZE == 0, for smallest code.
> 
>     In crypto/sha1.c, the code is roughly equivalent to SHA_CODE_SIZE == 3,
>     random.c's _largest_ implementation.
> 
>     So you seem to be replacing a small version with a large version,
>     albeit faster.

Yes. I've got another patch to include a non-unrolled version of SHA
in cryptolib but that depends on some config magic I haven't thought
out. Another thing I failed to mention at 3am is that most of the
speed increase actually comes from the following patch which wasn't in
baseline and isn't cryptoapi-specific. So I expect the cryptoapi
version is about 30% faster once you amortize the initialization
stuff.

>>>>
Remove folding step and double throughput.

If there was a recognizable output pattern, simply folding would make
things worse. To see why, assume bits x and y are correlated, but are
othewise random. Then x^y has a bias toward zero, meaning less entropy
than just x alone.

Best case, this is the moral equivalent of running every bit through
SHA four times rather than twice. Twice should be more than enough.

diff -urN -X dontdiff orig/drivers/char/random.c work/drivers/char/random.c
--- orig/drivers/char/random.c	2003-08-09 10:25:46.000000000 -0500
+++ work/drivers/char/random.c	2003-08-09 10:31:27.000000000 -0500
@@ -1013,20 +1013,8 @@
 			add_entropy_words(r, &tmp[x%HASH_BUFFER_SIZE], 1);
 		}
 		
-		/*
-		 * In case the hash function has some recognizable
-		 * output pattern, we fold it in half.
-		 */
-		for (i = 0; i <  HASH_BUFFER_SIZE/2; i++)
-			tmp[i] ^= tmp[i + (HASH_BUFFER_SIZE+1)/2];
-#if HASH_BUFFER_SIZE & 1	/* There's a middle word to deal with */
-		x = tmp[HASH_BUFFER_SIZE/2];
-		x ^= (x >> 16);		/* Fold it in half */
-		((__u16 *)tmp)[HASH_BUFFER_SIZE-1] = (__u16)x;
-#endif
-		
 		/* Copy data to destination buffer */
-		i = min(nbytes, HASH_BUFFER_SIZE*sizeof(__u32)/2);
+		i = min(nbytes, HASH_BUFFER_SIZE*sizeof(__u32));
 		if (flags & EXTRACT_ENTROPY_USER) {
 			i -= copy_to_user(buf, (__u8 const *)tmp, i);
 			if (!i) {

>>>>>

>   - One of the optimisations in crypto/sha1.c is:
> 
>       /* blk0() and blk() perform the initial expand. */
>       /* I got the idea of expanding during the round function from SSLeay */
> 
>     Yet, random.c has the opposite:
> 
>       /*
>        * Do the preliminary expansion of 16 to 80 words.  Doing it
>        * out-of-line line this is faster than doing it in-line on
>        * register-starved machines like the x86, and not really any
>        * slower on real processors.
>        */
> 
>     I wonder if the random.c method is faster on x86?

They're probably equal on modern machines. The random code is from 486 days.

>   - sha1_transform() in crypto/sha1.c ends with this misleading
>     comment.  Was the author trying to prevent data leakage?  If so,
>     he failed:
> 
> 	/* Wipe variables */
> 	a = b = c = d = e = 0;
> 	memset (block32, 0x00, sizeof block32);
> 
>     The second line will definitely be optimised away.  The third
>     could be, although GCC doesn't.

Preventing data leakage is the intent, though I'm not sure its worth
the trouble. If your attacker can leverage this sort of data leakage,
you probably have more important stuff to worry about.

-- 
Matt Mackall : http://www.selenic.com : of or relating to the moon

  reply	other threads:[~2003-08-09 17:33 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-08-09  7:44 [RFC][PATCH] Make cryptoapi non-optional? Matt Mackall
2003-08-09  8:04 ` David S. Miller
2003-08-09 14:05   ` Matt Mackall
2003-08-09 17:39     ` David S. Miller
2003-08-09 19:46       ` Matt Mackall
2003-08-09 20:17         ` David S. Miller
2003-08-10  8:15           ` Matt Mackall
2003-08-10  8:32             ` virt_to_offset() (Re: [RFC][PATCH] Make cryptoapi non-optional?) YOSHIFUJI Hideaki / 吉藤英明
2003-08-10  8:30               ` David S. Miller
2003-08-10  9:02                 ` virt_to_offset() YOSHIFUJI Hideaki / 吉藤英明
2003-08-11 18:21                   ` virt_to_offset() David Mosberger
2003-08-12  2:46                     ` virt_to_offset() David S. Miller
2003-08-10  9:05                 ` virt_to_offset() (Re: [RFC][PATCH] Make cryptoapi non-optional?) Matt Mackall
2003-08-10  9:04                   ` David S. Miller
2003-08-10 11:00                     ` [PATCH 1/9] introduce virt_to_pagoff() YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:00                     ` [PATCH 2/9] convert crypto to virt_to_pageoff() YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:05                     ` [PATCH 3/9] convert net " YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:07                     ` [PATCH 4/9] convert drivers/block " YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:09                     ` [PATCH 5/9] convert drivers/ide " YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:10                     ` [PATCH 6/9] convert drivers/net " YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:10                     ` [PATCH 7/9] convert drivers/scsi " YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:31                       ` Christoph Hellwig
2003-08-10 11:51                         ` David S. Miller
2003-08-10 12:03                           ` YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 14:54                             ` Christoph Hellwig
2003-08-10 14:51                           ` Christoph Hellwig
2003-08-10 13:54                         ` Russell King
2003-08-10 13:55                         ` Russell King
2003-08-10 14:55                           ` Christoph Hellwig
2003-08-11  5:21                           ` David S. Miller
2003-08-10 11:10                     ` [PATCH 8/9] convert drivers/usb " YOSHIFUJI Hideaki / 吉藤英明
2003-08-10 11:10                     ` [PATCH 9/9] convert fs/jbd " YOSHIFUJI Hideaki / 吉藤英明
2003-08-11  2:15             ` [RFC][PATCH] Make cryptoapi non-optional? Jamie Lokier
2003-08-11  2:38               ` Matt Mackall
2003-08-11  4:54               ` David S. Miller
2003-08-11  5:17                 ` Jamie Lokier
2003-08-13  5:01                 ` [Numbers][PATCH] " Matt Mackall
2003-08-10 14:46           ` [RFC][PATCH] " James Morris
2003-08-09 14:33 ` Matt Mackall
2003-08-09 17:13   ` Jamie Lokier
2003-08-09 17:33     ` Matt Mackall [this message]
2003-08-10 13:18       ` James Morris
2003-08-10 17:45         ` Matt Mackall
2003-08-11  2:09           ` Jamie Lokier
2003-08-11  2:35             ` Matt Mackall
2003-08-11  4:59               ` Jamie Lokier
2003-08-11  5:04                 ` Matt Mackall
2003-08-11  5:20                   ` Jamie Lokier
2003-08-11  5:54                     ` Matt Mackall
2003-08-11  6:24                       ` Jamie Lokier
2003-08-11  4:58             ` David Wagner
2003-08-11  5:36               ` Jamie Lokier
2003-08-11 19:21                 ` David Wagner
2003-08-13 19:37                   ` Jamie Lokier
2003-08-13  3:52             ` Theodore Ts'o
2003-08-13 15:44               ` i810_rng.o on various Dell models Jim Carter
2003-08-13 16:15                 ` Jeff Garzik
2003-08-13 18:43                   ` Jamie Lokier
2003-08-13 18:36               ` [RFC][PATCH] Make cryptoapi non-optional? Jamie Lokier
2003-08-15  0:16                 ` Network Card Entropy? was: " Mike Fedyk
2003-08-15  0:22                   ` Robert Love
2003-08-13  3:20           ` Theodore Ts'o
2003-08-13  4:06             ` Matt Mackall
2003-08-14 16:53               ` Val Henson
2003-08-14 19:40                 ` David Wagner
2003-08-14 20:07                   ` Chris Friesen
2003-08-14 21:36                   ` Jamie Lokier
2003-08-15  0:25                     ` Val Henson
2003-08-15 11:47                       ` Jamie Lokier
2003-08-15  0:17                   ` Val Henson
2003-08-15  1:45                     ` David Wagner
2003-08-15  2:21                       ` Matt Mackall
2003-08-15  7:30                     ` Andries Brouwer
2003-08-15  7:40                       ` David S. Miller
2003-08-15  7:55                         ` Andries Brouwer
2003-08-15  8:06                           ` Måns Rullgård
2003-08-15  8:11                             ` Nick Piggin
2003-08-15 15:11                             ` Matt Mackall
2003-08-15 22:16                               ` Jamie Lokier
2003-08-15 20:22                           ` Val Henson
2003-08-16  6:27                             ` David Wagner
2003-08-18  4:25                               ` Val Henson
2003-08-15  8:09                         ` Nick Piggin
2003-08-15 15:03                       ` Matt Mackall
2003-08-15 17:04                         ` Andries Brouwer
2003-08-15 22:05                           ` Jamie Lokier
2003-08-15 22:02                         ` Jamie Lokier
2003-08-15 12:48                     ` Jamie Lokier
2003-08-15 22:34                     ` Theodore Ts'o
2003-08-15 22:12               ` Theodore Ts'o
2003-08-15 23:35                 ` James Morris
2003-08-16 15:51                   ` Matt Mackall
2003-08-17 14:37                     ` James Morris
2003-08-17 15:30                       ` Matt Mackall
2003-08-15 23:55                 ` Matt Mackall
2003-08-16  0:05                   ` Andrew Morton
2003-08-16  0:58                     ` Jamie Lokier
2003-08-16  4:57                       ` Matt Mackall
2003-08-16  4:38                     ` Matt Mackall
2003-08-16  5:03                       ` Andrew Morton
2003-08-16  5:39                         ` Matt Mackall
2003-08-18  6:43                       ` Andreas Dilger
2003-08-18  6:55                         ` David Lang
2003-08-18 11:59                           ` Jamie Lokier
2003-08-18 12:11                             ` Måns Rullgård
2003-08-18 13:33                               ` Jamie Lokier
2003-08-18 17:03                             ` David Lang
2003-08-18 17:51                               ` Jamie Lokier
2003-08-22  4:28                           ` David Wagner
2003-08-25  4:29                             ` Jamie Lokier
2003-08-18 15:20                         ` Matt Mackall
2003-08-18  3:23                   ` Theodore Ts'o
2003-08-18 15:46                     ` Matt Mackall
2003-08-10  2:07   ` Robert Love
2003-08-10  3:14     ` Matt Mackall
2003-08-10  3:49       ` David S. Miller
2003-08-10  4:01         ` Robert Love
2003-08-10  4:07           ` Robert Love
2003-08-16 20:40 Adam J. Richter
2003-08-17  4:28 ` 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=20030809173329.GU31810@waste.org \
    --to=mpm@selenic.com \
    --cc=akpm@osdl.org \
    --cc=davem@redhat.com \
    --cc=jamie@shareable.org \
    --cc=jmorris@intercode.com.au \
    --cc=linux-kernel@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;
as well as URLs for NNTP newsgroup(s).