Skip to contentSkip to conversation

random: Add support for architectural random hooks

PATCH George Spelvin 4 participants 9 messages Jul 30, 2011 – Jul 31, 2011

lore
Share & tools
lore
#0 lore raw
Quoted 7 lines

The fact is, even if you worry about some back door for the NSA, or some theoretical lack of perfect 32-bit randomness, we can pretty much depend on it. We still do our own hashing on top of whatever entropy we get out of rdrand, and we would still have all our other stuff. Plus the instruction is public and testable - if Intel did something wrong, they'll be *very* embarrassed.

Quoted 5 lines

In other words, there's absolutely no reason not to use it, and allow us to get away from /dev/random running out of entropy. We absolutely should use it for bootup randomness (where we currently are somewhat weak), and I absolutely disagree that it should be made into more of a driver abstraction.

I agree with your second paragraph, but disagree violently with your
first: Intel provide no way to test their RNG, and the AES-based whitener makes it completely private and UNtestable.

Intel go into details about the hardware's boot-up self-test, but then provide no way to access that self-test information, much less check up on it.

I can feed all zeros into the described whitener and get output that is not possible to distinguish from random, without access to the secret initial state. That's the whole point of a cipher, and AES is a very good cipher!

The only technically difficult thing about implementing a back door is ensuring that the output sequence doesn't repeat each power cycle. You need just enough entropy or nonce to get that.

As long as you can achieve that, getting caught is actually quite unlikely.

Consider a sample implementation: implement it exactly as described, except:
- Make the hardware known answer test (or, more specifically, the state of the DRBG after the KAT) the backdoor secret, and
- Suppress all but the first 40 bits of entropy from the hardware entropy source, replacing it with a fixed pattern (e.g. all zero).

An attacker who knows the secret needs to brute-force the 40 bits plus the number of RDRND reads to be able to predict the RDRAND outout.

Someone who doesn't can ony detect the fiddle by power-cycling the hardware a million times and looking for duplicate outputs. (Birthday attack on the amount of seed entropy.)

Even a small amount of persistent storage makes things more difficult. For example, an 8-bit counter that usually survives power cycles replaces the 40-bit birthday attack with 256 32-bit birthday attacks, requiring 256 * 2^16 = 2^24 power-cycles to find a collision. While making an attacker's job easier after they've brute-forced 40+ bits once.

I don't think it's *likely* that Intel have done this, but given the about of National Security bullshit the U.S. government is throwing around these days, I'm less than 10 bits certain that they haven't.

A much more verifiable way would be to provide access to the raw hardware generator, and enough hardware documentation to predict how its output varies with environmental conditions (temperature, supply voltage, clock speed, x-rays, ...) and lot-to-lot. It would be extremely difficult to produce a deterministic (i.e. back-door-able) generator with the same sensitivities.

#1 lore raw
Quoted 4 lines · George Spelvin

I agree with your second paragraph, but disagree violently with your
first: Intel provide no way to test their RNG, and the AES-based whitener makes it completely private and UNtestable.

Umm.

Guys, if your argument is that you cannot possibly distinguish the Intel implementation from "true" randomness, then WHAT THE HELL are you complaining about?

We don't even care. "True randomness" and "something we cannot possibly even test and distinguish from true randomess" are 100% equivalent. Stop with the idiotic "we cannot test it" crap. If it really is indistinguishable from true randomness, nobody will ever care.

It's that simple. Really.

And if somebody ever figures out that Intel flubbed, and you actually *can* tell it from true randomness, even then, what's the downside? It will be practically random anyway, and we'll be mixing it up some more.

Seriously. This whole discussion just makes me convinced that security people are so far removed from reality that it's not even relevant any more.

Linus

#2 lore raw
Quoted 9 lines

Guys, if your argument is that you cannot possibly distinguish the Intel implementation from "true" randomness, then WHAT THE HELL are you complaining about?

We don't even care. "True randomness" and "something we cannot possibly even test and distinguish from true randomess" are 100% equivalent. Stop with the idiotic "we cannot test it" crap. If it really is indistinguishable from true randomness, nobody will ever care.

Did you READ what I wrote beyond the first paragraph?

Because the LAST paragraph included a description of a testable and verifiable hardware RNG. To repeat:

Quoted 6 lines
Quoted 6 lines

A much more verifiable way would be to provide access to the raw hardware generator, and enough hardware documentation to predict how its output varies with environmental conditions (temperature, supply voltage, clock speed, x-rays, ...) and lot-to-lot. It would be extremely difficult to produce a deterministic (i.e. back-door-able) generator with the same sensitivities.

(Apologies for the bad grammar of "much more verifiable".)

The problem is not that's it's theoretically impossible to distinguish them, but that it's PRACTICALLY impossible to distinguish true randomness from a good counterfeiting effort. As long as you're limited to black-box testing.

That's counterfeiting, not incompetence. The Intel RNG, if implemented as described in their documentation, is quite well engineered. (The basic architecture is essentially the same as /dev/urandom.)

I was just saying that it would be very easy to add a deliberate back door, and the interface they provide fails to include any way to detect such a thing.

Quoted 3 lines

Seriously. This whole discussion just makes me convinced that security people are so far removed from reality that it's not even relevant any more.

The real world matters enormously. Because that's where security exploits happen, not in some mathematical abstraction that can be proved secure.

I can build an RNG that nobody else can distinguish from truly random until I start forging your signature on kernel releases.

Isn't that a bit LATE to be noticing the problem?

#3 lore raw
In reply to George Spelvin
Quoted 13 lines · George Spelvin
Quoted 7 lines

The fact is, even if you worry about some back door for the NSA, or some theoretical lack of perfect 32-bit randomness, we can pretty much depend on it. We still do our own hashing on top of whatever entropy we get out of rdrand, and we would still have all our other stuff. Plus the instruction is public and testable - if Intel did something wrong, they'll be *very* embarrassed.

Quoted 5 lines

In other words, there's absolutely no reason not to use it, and allow us to get away from /dev/random running out of entropy. We absolutely should use it for bootup randomness (where we currently are somewhat weak), and I absolutely disagree that it should be made into more of a driver abstraction.

Quoted 3 lines

I agree with your second paragraph, but disagree violently with your
first: Intel provide no way to test their RNG, and the AES-based whitener makes it completely private and UNtestable.

[snip paranoia]

That's fine, but it's also not for the kernel to judge. Certainly there should be an option to allow the user to disable the use of RDRAND, if they're so inclined. But those who trust Intel's HWRNG should be able to use it as efficiently as possible.

That said, I'm not sure I like Linus's proposed API. As I understand it, he proposes something like:

int arch_get_random_word(unsigned long *pRandom);

Where arch_get_random_word returns 0 if not supported.

If we have that interface, then the urandom code implementing it might look something _vaguely_ like this (ignoring the possibility of nbytes % sizeof(unsigned long) != 0):

void __user *bufend = buf + nbytes;
while (buf < bufend) {
unsigned long tmp;

  if (arch_get_random_word(&tmp)) {
    if (copy_to_user(buf, &tmp, sizeof(tmp))) {
      ret = -EFAULT;
      break;
    }
  } else {
    return urandom_fallback(buf, bufend-buf);

}
buf += sizeof(tmp);
}

The problem, as you can see here, is we need to test for the existence of an architecture-specific HWRNG once for _every word processed_. And since we don't know the return value of arch_get_random_word until we probe the CPU's capability set at runtime, this isn't going to be eliminated statically.

The alternative is to simply have a single hook-based API, passing in a buffer and length, as in the HWRNG API, as well as this patch series. You can then test whether there is a hook once, and then go straight into a tight loop to generate your random data. This does add a _tiny_ amount of overhead to the case where the user wants a single word of random data, due to the indirect function call. However, in that case the overhead is nothing compared to the system call entry overhead. For larger buffers, the savings from avoiding a branch every loop is likely to be far larger than your function pointer invocation.

Now, of course, you could do something like this:

if (arch_has_get_random_word) {

  while (buf < bufend) {
    unsigned long tmp = arch_get_random_word();
    copy_to_user(buf, &tmp, sizeof(tmp));
    buf += sizeof(tmp);

}
}

I think it should be obvious that this a pretty terrible API, but as far as I can see if you're steadfast on avoiding the overhead of a hook then it's the best you can do.

In short, I have to agree with George Spelvin's suggestion of using the hwrng API - just add a hook to allow urandom to be directed to a hwrng device, and appropriate initialization code to do this at startup if RDRAND is available. Heck, you might not even have to do that - just configure udev to point /dev/urandom at the hwrng device node, and now the kernel doesn't have to make any policy decisions on what RNG to use at all.

#4 lore raw
Quoted 3 lines · Bryan Donlan

The problem, as you can see here, is we need to test for the existence of an architecture-specific HWRNG once for _every word processed_.

See the patch I sent out. Not only doesn't it check an architecture-specific flag on x86, IT COSTS NOTHING AT ALL on other architectures. Not per byte, not per call, not at all.

On x86, it does have the cost of:

- basically one conditional for the whole loop (branching out if
there is no rdrand support at all). You cannot do much better than that, except of course if you statically say "compile for an x86 that doesn't have rdrand")

- a per-word cost of checking CF - which you are supposed to do
anyway (ie the rdrand hardware will return with CF clear if the data isn't reliable).

So at least on x86, I think you can get pretty close to "optimal behavior" with my approach.

Linus

#5 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Bryan Donlan

The problem, as you can see here, is we need to test for the existence of an architecture-specific HWRNG once for _every word processed_.

See the patch I sent out. Not only doesn't it check an architecture-specific flag on x86, IT COSTS NOTHING AT ALL on other architectures. Not per byte, not per call, not at all.

Certainly, on architectures which never support fast hwrngs, you can avoid the overhead. I'm concerned about whether you're actually coming out ahead in the case where there _is_ an arch hwrng.

Quoted 7 lines

On x86, it does have the cost of:

 - basically one conditional for the whole loop (branching out if there is no rdrand support at all). You cannot do much better than that, except of course if you statically say "compile for an x86 that doesn't have rdrand")

Quoted 6 lines

 - a per-word cost of checking CF - which you are supposed to do anyway (ie the rdrand hardware will return with CF clear if the data isn't reliable).

So at least on x86, I think you can get pretty close to "optimal behavior" with my approach.

Ah, I see - I wasn't aware RDRAND could fail. In that case, it's not so bad, although it would be nicer to directly branch on the carry flag, rather than emitting code that will, eventually, do a setcc -> and -> test -> branch.

In any case though, what about my suggestion - don't mess with urandom at all, and let udev replace the /dev/urandom device node with one directed to a fast hwrng device if one is detected? This has zero extra overhead - you have to pass through the character device layer no matter what, and you can go straight from there to either the standard hwrng wrapper code, or to your own special extra-fast implementation of dev_read. It should also satisfy Mr. Spelvin's paranoia - he can just instruct his udev to not replace /dev/urandom. And so we don't need to introduce any new APIs to the kernel at all, nor do we need to touch any core code.

The downside, of course, is that in-kernel users of entropy won't have this fast API - do we have the kind of high-volume non-blocking entropy consumers in the kernel that might care about this?

#6 lore raw
Quoted 3 lines · Bryan Donlan

In any case though, what about my suggestion - don't mess with urandom at all, and let udev replace the /dev/urandom device node with one directed to a fast hwrng device if one is detected? This has zero

Now, THAT would be unsafe, and a nasty way to go about it.

If you really don't want to touch /dev/.random, make it simple: either export the embedded RNG as high-bandwidth /dev/hwrandom and let userspace postprocess it and reroute it back to /dev/random as needed, or do the same with a kernel thread.

Signature · 4 lines
  "One disk to rule them all, One disk to find them. One disk to bring
  them all and in the darkness grind them. In the Land of Redmond
  where the shadows lie." -- The Silicon Valley Tarot
  Henrique Holschuh
#7 lore raw
Quoted 6 lines · Henrique de Moraes Holschuh
Quoted 3 lines · Bryan Donlan

In any case though, what about my suggestion - don't mess with urandom at all, and let udev replace the /dev/urandom device node with one directed to a fast hwrng device if one is detected? This has zero

Now, THAT would be unsafe, and a nasty way to go about it.

I don't see why it's unsafe. It's leaving the policy decision of what RNG to use up to userspace, without needing to introduce any new API or parameter to do so. As long as the replacement device exposes the same interface as /dev/urandom, no problem, right? Userspace programs shouldn't notice the difference unless they stat() /dev/urandom and look at the device numbers.

I do realize it's a bit unorthodox to replace what has, traditionally, been a fixed device node with a configurable symlink, but there's no technical reason (as far as I can see) why it shouldn't work.

Quoted 4 lines

If you really don't want to touch /dev/.random, make it simple: either export the embedded RNG as high-bandwidth /dev/hwrandom and let userspace postprocess it and reroute it back to /dev/random as needed, or do the same with a kernel thread.

This approach is identical to mine as far as the kernel is concerned :) You'd set up a daemon to pump entropy into /dev/random. Users who want /dev/urandom to be even faster/more secure(?) can further symlink /dev/urandom to /dev/hwrandom. But there's no need to change /dev/urandom's implementation in the kernel to redirect it to the hwrng when userspace can do that just as well on its own, is my point.

#8 lore raw
In reply to Bryan Donlan
Quoted 4 lines · Bryan Donlan

In any case though, what about my suggestion - don't mess with urandom at all, and let udev replace the /dev/urandom device node with one directed to a fast hwrng device if one is detected?

Umm, user space is encouraged to do exactly that.

However, at no point does that mean "the kernel shouldn't do the best it can". So it's a totally independent issue.

The kernel will want to use rdrand regardless of what user space does. Both for legacy applications that don't know about rdrand, and for scripts that don't want to bother. And for just its own use.

Linus