Skip to content

Commit

Permalink
x86, kaslr: Add a circular multiply for better bit diffusion
Browse files Browse the repository at this point in the history
If we don't have RDRAND (in which case nothing else *should* matter),
most sources have a highly biased entropy distribution.  Use a
circular multiply to diffuse the entropic bits.  A circular multiply
is a good operation for this: it is cheap on standard hardware and
because it is symmetric (unlike an ordinary multiply) it doesn't
introduce its own bias.

Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Link: http://lkml.kernel.org/r/20131111222839.GA28616@www.outflux.net
  • Loading branch information
H. Peter Anvin committed Nov 12, 2013
1 parent a653f35 commit e8236c4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions arch/x86/boot/compressed/aslr.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ static unsigned long get_random_boot(void)

static unsigned long get_random_long(void)
{
#ifdef CONFIG_X86_64
const unsigned long mix_const = 0x5d6008cbf3848dd3UL;
#else
const unsigned long mix_const = 0x3f39e593UL;
#endif
unsigned long raw, random = get_random_boot();
bool use_i8254 = true;

Expand All @@ -90,6 +95,12 @@ static unsigned long get_random_long(void)
random ^= i8254();
}

/* Circular multiply for better bit diffusion */
asm("mul %3"
: "=a" (random), "=d" (raw)
: "a" (random), "rm" (mix_const));
random += raw;

debug_putstr("...\n");

return random;
Expand Down

0 comments on commit e8236c4

Please sign in to comment.