netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: linux@horizon.com
To: linux@horizon.com, roland@topspin.com
Cc: ak@muc.de, arjan@infradead.org, bunk@stusta.de, chrisw@osdl.org,
	davem@redhat.com, hlein@progressive-comp.com,
	linux-kernel@vger.kernel.org, netdev@oss.sgi.com,
	shemminger@osdl.org, Valdis.Kletnieks@vt.edu
Subject: Re: [PATCH] OpenBSD Networking-related randomization port
Date: 13 Feb 2005 01:41:04 -0000	[thread overview]
Message-ID: <20050213014104.19282.qmail@science.horizon.com> (raw)
In-Reply-To: <527jld2tyx.fsf@topspin.com>

    linux> It's easy to make a smaller hash by just thowing bits away,
    linux> but a block cipher is a permutation, and has to be
    linux> invertible.

    linux> For example, if I take a k-bit counter and encrypt it with
    linux> a k-bit block cipher, the output is guaranteed not to
    linux> repeat in less than 2^k steps, but the value after a given
    linux> value is hard to predict.

> Huh?  What if my cipher consists of XOR-ing with a k-bit pattern?
> That's a permutation on the set of k-bit blocks but it happens to
> decompose as a product of (non-overlapping) swaps.
> 
> In general for more realistic block ciphers like DES it seems
> extremely unlikely that the cipher has only a single orbit when viewed
> as a permutation.  I would expect a real block cipher to behave more
> like a random permutation, which means that the expected number of
> orbits for a k-bit cipher should be about ln(2^k) or roughly .7 * k.

I think you misunderstand; your comments don't seem to make sense unless
I assume you're imagining output feedback mode:

x[0] = encrypt(IV)
x[1] = encrypt(x[0])
x[2] = encrypt(x[1])
etc.

Obviously, this pattern will repeat after some unpredictable interval.
(However, owing to the invertibility of encryption, looping can
be easily detected by noticing that x[i] = IV.)

But I was talking about counter mode:

x[0] = encrypt(0)
x[1] = encrypt(1)
x[2] = encrypt(2)
etc.

It should be obvious that this will not repeat until the counter
overflows k bits and you try to compute encrypt(2^k) = encrypt(0).

One easy way to generate unpredictable 16-bit port numbers that don't
repeat too fast is:

highbit = 0;
for (;;) {
	generate_random_encryption_key(key);
	for (i = 0; i < 20000; i++)
		use(highbit | encrypt15(i, key));
	highbit ^= 0x8000;
}

Note that this does NOT use all 32K values before switching to another
key; if that were the case, an attacker who kept a big bitmap of reviously
seen values could preduct the last few values based on knowing what
hadn't been seen already.


Of course, you can always wrap a layer of Knuth's Algorithm B
(randomization by shuffling) around anything:

#include "basic_rng.h"

#define SHUFFLE_SIZE 32	/* Power of 2 is more efficient */

struct better_rng_state {
	struct basic_rng_state basic;
	unsigned y;
	unsigned z[SHUFFLE_SIZE];
};

void
better_rng_seed(struct better_rng_state *state, unsigned seed)
{
	unsigned i;
	basic_rng_seed(&state->basic, seed);

	for (i = 0; i < SHUFFLE_SIZE; i++)
		state->z[i] = basic_rng(&state->basic);
	state->y = basic_rng(&state->basic) % SHUFFLE_SIZE;
}

unsigned
better_rng(struct better_rng_state *state)
{
	unsigned x = state->z[state->y];
	state->y = (state->z = basic_rng(&state->basic)) % SHUFFLE_SIZE;
	return x;
}

(You can reduce code size by reducing modulo SHUFFLE_SIZE when you use
state->y rather than when storing into it, but I have done it the other
way to make clear exactly how much "effective" state is stored.  You can
also just initialize state->y to a fixed value.)

  reply	other threads:[~2005-02-13  1:41 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1106932637.3778.92.camel@localhost.localdomain>
     [not found] ` <20050128174046.GR28047@stusta.de>
     [not found]   ` <1106934475.3778.98.camel@localhost.localdomain>
2005-01-28 18:18     ` [PATCH] OpenBSD Networking-related randomization port Stephen Hemminger
2005-01-28 18:54       ` Lorenzo Hernández García-Hierro
     [not found] ` <20050128100229.5c0e4ea1@dxpl.pdx.osdl.net>
2005-01-28 18:31   ` Lorenzo Hernández García-Hierro
2005-01-28 18:52     ` Stephen Hemminger
2005-01-28 18:58       ` Lorenzo Hernández García-Hierro
2005-01-28 20:34       ` Lorenzo Hernández García-Hierro
2005-01-28 20:45         ` David S. Miller
2005-01-28 21:34           ` Stephen Hemminger
2005-01-28 21:45             ` David S. Miller
2005-01-29  6:59             ` Andi Kleen
2005-01-28 20:47         ` Arjan van de Ven
2005-01-28 22:12           ` Lorenzo Hernández García-Hierro
2005-01-29  8:04             ` Arjan van de Ven
2005-01-29  8:05             ` Arjan van de Ven
2005-01-29  9:15           ` Valdis.Kletnieks
2005-01-31 16:50             ` Adrian Bunk
2005-01-31 17:23               ` Lorenzo Hernández García-Hierro
2005-01-31 20:11                 ` Ingo Molnar
2005-01-31 23:27                   ` linux
2005-02-12 22:29                     ` Andi Kleen
2005-02-12 23:25                       ` linux
2005-02-13  0:18                         ` Roland Dreier
2005-02-13  1:41                           ` linux [this message]
2005-02-02 17:17                   ` linux
2005-02-02 17:38                     ` Lorenzo Hernández García-Hierro
2005-02-03 19:51                       ` Stephen Hemminger
2005-02-03 20:14                         ` Lennert Buytenhek
2005-01-31 19:42               ` Valdis.Kletnieks
2005-01-31 20:03                 ` Lorenzo Hernández García-Hierro
2005-02-01 23:22                   ` Matt Mackall
     [not found] ` <1106935677.7776.29.camel@laptopd505.fenrus.org>
2005-01-28 18:36   ` Lorenzo Hernández García-Hierro

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=20050213014104.19282.qmail@science.horizon.com \
    --to=linux@horizon.com \
    --cc=Valdis.Kletnieks@vt.edu \
    --cc=ak@muc.de \
    --cc=arjan@infradead.org \
    --cc=bunk@stusta.de \
    --cc=chrisw@osdl.org \
    --cc=davem@redhat.com \
    --cc=hlein@progressive-comp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@oss.sgi.com \
    --cc=roland@topspin.com \
    --cc=shemminger@osdl.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).