From: "Jörn Engel" <joern@logfs.org>
To: "Theodore Ts'o" <tytso@mit.edu>
Cc: "H. Peter Anvin" <hpa@zytor.com>, lkml <linux-kernel@vger.kernel.org>
Subject: [PATCH] random: mix all saved registers into entropy pool
Date: Mon, 19 May 2014 17:17:19 -0400 [thread overview]
Message-ID: <20140519211719.GA14563@logfs.org> (raw)
The single biggest entropy source is a high-resolution timer running
asynchronous to the triggering event. That leaves systems without a
useful get_cycles() implementation with significantly less entropy.
Significant means orders of magnitude, not a few percent.
An alternate high-resolution timer is the register content at the time
of an interrupt. While not monotonic, it is guaranteed to change every
few clock cycles and very unlikely to repeat the same pattern. Not
useful for interpretation as time, but we only care about some bits
of the "clock" flipping in an unpredictable fashion.
Experimentation show this to be an excellent entropy source. Doing 1000
boottests with kvm and dumping a hash of the registers for the first
1024 interrupts each, >40% of all hashes were unique and >80% of all
hashes occurred less than once 1% of the time.
Even assuming that only unique register hashes yield any entropy at all
and only one bit each, we would gather >400 bits of entropy early in
boot and another 40 bits/s from the timer interrupt during runtime. I
would feel confident with this amount of entropy in the absence of any
other source.
Repeating the test on real hardware, albeit with fewer repetitions
yields roughly the same results, slightly above 40% unique hashes.
Signed-off-by: Joern Engel <joern@logfs.org>
---
drivers/char/random.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 62 insertions(+), 4 deletions(-)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 429b75bb60e8..b4a8968fe28d 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -553,6 +553,8 @@ static void mix_pool_bytes(struct entropy_store *r, const void *in,
struct fast_pool {
__u32 pool[4];
+ u32 last_shift;
+ u32 regs_count;
unsigned long last;
unsigned short count;
unsigned char rotate;
@@ -835,6 +837,61 @@ EXPORT_SYMBOL_GPL(add_input_randomness);
static DEFINE_PER_CPU(struct fast_pool, irq_randomness);
+/*
+ * Ratelimit to a steady state of about once per jiffy. A naïve approach
+ * would be to return 1 every time jiffies changes. But we want to avoid
+ * being closely coupled to the timer interrupt. So instead we increment
+ * a counter on every call and shift it right every time jiffies changes.
+ * If the counter is a power of two, return false;
+ *
+ * Effect is that some time after a jiffies change and cutting the counter
+ * in half we reach another power of two and return false. But the
+ * likelihood of this happening is about the same at any time within a
+ * jiffies interval.
+ */
+static inline int ratelimited(struct fast_pool *p)
+{
+ int ret = !(p->regs_count == 0 || is_power_of_2(p->regs_count));
+
+ p->regs_count++;
+ if (p->last_shift != (u32)jiffies) {
+ p->regs_count >>= min(31u, (u32)jiffies - p->last_shift);
+ p->last_shift = (u32)jiffies;
+ }
+ return ret;
+}
+
+#define BOOT_IRQS 1024
+
+/*
+ * The single biggest entropy source is a high-resolution timer running
+ * asynchronous to the triggering event. That leaves systems without a
+ * useful get_cycles() implementation with significantly less entropy.
+ * Significant means orders of magnitude, not a few percent.
+ *
+ * An alternate high-resolution timer is the register content at the time
+ * of an interrupt. While not monotonic, it is guaranteed to change every
+ * few clock cycles and very unlikely to repeat the same pattern. Not
+ * useful for interpretation as time, but we only care about some bits
+ * of the "clock" flipping in an unpredictable fashion.
+ */
+static void mix_regs(struct pt_regs *regs, struct fast_pool *fast_pool)
+{
+ struct entropy_store *r;
+ /* Two variables avoid decrementing by two without using atomics */
+ static int boot_count = BOOT_IRQS;
+ int in_boot = boot_count;
+
+ if (in_boot) {
+ boot_count = in_boot - 1;
+ } else if (ratelimited(fast_pool))
+ return;
+
+ /* During bootup we alternately feed both pools */
+ r = (in_boot & 1) ? &nonblocking_pool : &input_pool;
+ __mix_pool_bytes(r, regs, sizeof(*regs), NULL);
+}
+
void add_interrupt_randomness(int irq, int irq_flags)
{
struct entropy_store *r;
@@ -845,13 +902,14 @@ void add_interrupt_randomness(int irq, int irq_flags)
__u32 input[4], c_high, j_high;
__u64 ip;
+ mix_regs(regs, fast_pool);
c_high = (sizeof(cycles) > 4) ? cycles >> 32 : 0;
j_high = (sizeof(now) > 4) ? now >> 32 : 0;
- input[0] = cycles ^ j_high ^ irq;
- input[1] = now ^ c_high;
+ input[0] ^= cycles ^ j_high ^ irq;
+ input[1] ^= now ^ c_high;
ip = regs ? instruction_pointer(regs) : _RET_IP_;
- input[2] = ip;
- input[3] = ip >> 32;
+ input[2] ^= ip;
+ input[3] ^= ip >> 32;
fast_mix(fast_pool, input);
--
2.0.0.rc0.1.g7b2ba98
next reply other threads:[~2014-05-19 21:18 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-19 21:17 Jörn Engel [this message]
2014-05-19 21:23 ` [PATCH] random: mix all saved registers into entropy pool Jörn Engel
2014-05-19 22:18 ` H. Peter Anvin
2014-05-19 22:39 ` Jörn Engel
2014-05-19 23:05 ` H. Peter Anvin
2014-05-19 23:18 ` Jörn Engel
2014-05-20 12:12 ` Andi Kleen
2014-05-20 20:08 ` Jörn Engel
2014-05-21 19:39 ` Andi Kleen
2014-05-21 20:29 ` Jörn Engel
2014-05-21 20:38 ` Jörn Engel
2014-06-04 23:17 ` Jörn Engel
2014-06-10 16:14 ` Theodore Ts'o
2014-06-11 0:10 ` Jörn Engel
2014-06-11 15:27 ` Theodore Ts'o
2014-06-12 20:25 ` Jörn Engel
2014-06-12 20:05 ` Jörn Engel
-- strict thread matches above, loose matches on Subject: below --
2014-02-02 20:36 [PATCH,RFC] random: collect cpu randomness Jörn Engel
2014-02-03 15:50 ` Jörn Engel
2014-02-03 16:37 ` Theodore Ts'o
2014-02-03 18:48 ` Jörn Engel
2014-03-23 18:00 ` [PATCH] random: mix all saved registers into entropy pool Jörn Engel
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=20140519211719.GA14563@logfs.org \
--to=joern@logfs.org \
--cc=hpa@zytor.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.