‹ BackHN Continuity

Thread

AMD's random number generator can't generate a 0?

288 points · 220 comments · BruceEel

  1. strenholme · · focus · HN ↗
    This is why I use, in security critical contents of my software (where the numbers have to be computationally infeasible to produce), a type of random number generator called an XOF (extendable-output function).

    It takes entropy from multiple different sources, makes it all input to the XOF, then the XOF uses cryptography to output a stream that has as much entropy as the combined entropy of all of its sources of randomness. So if an XOF, for example, takes 100 runs of rdrand16, along with the system time in microseconds and the number of milliseconds between receiving 100 packets over the network, the XOF will output a completely random stream without artifacts like never returning 0x0000, even if rdrand16 never outputs 0x0000.

    1. Taek · · focus · HN ↗
      You can effectively achieve the same result with this simple operation:

        hash = sha256(current_time());
        for i := 0; i < n; i++ {
            hash = sha256(hash.append(current_time()))
        }
      
      
      This is because the number of nanoseconds between hashes is actually itself variable, and this is true for physics reasons that are basically beyond the control of any attacker trying to manipulate your entropy. If your time() function has a resolution of nanoseconds, you only need your loop to iterate about 50 times to get a cryptographically secure amount of entropy. If your time() function has a resolution of milliseconds, you need to let this run for more like 20 milliseconds, and if your time() function has a resolution of seconds you need to let it run for more like 5 seconds.

      The reason I like doing it this way is that it happens entirely in userspace, it's genuinely a secure method of generating entropy, and it has no dependencies on potentially buggy firmware or microcode outside of the time() call, which is both fairly narrow, fairly heavily used (meaning a bug is likely to be discovered during testing, as the implementation is likely heavily scrutinized), and also fairly easy to test independently - just look at the number of nanoseconds that elapse at each consecutive call to sha256(current_time()) and verify that there's some statistical variance. The above suggestions are assuming about 2.5 bits of variance between calls, meaning there should be a range of at least 20 nanoseconds between your slowest and fastest hash call. This has been true on every CPU I've ever measured, including microcontrollers.

      1. sltkr · · focus · HN ↗
        This comment demonstrates everything that's wrong with people trying to be clever and rolling their own crypto.

        The security of your system depends on time() providing enough entropy, even though that's not what it's designed to do. It's built on top of the wrong primitive from the start.

        > The reason I like doing it this way is that it happens entirely in userspace

        On Linux this is often true, but there is no portable way to get the current time that is _guaranteed_ not to do any system calls.

        > If your time() function has a resolution of nanoseconds, you only need your loop to iterate about 50 times to get a cryptographically secure amount of entropy.

        You haven't proven that at all. It's easy to imagine that on a CPU running at a fixed frequency the interval between reads is constant, so if anyone knows (or can guess) the start time the resulting seed is entirely predictable.

        This is completely independent of timer resolution. You seem to realize that as you were writing that:

        > just look at the number of nanoseconds that elapse at each consecutive call to sha256(current_time()) and verify that there's some statistical variance

        Oh yes, because evaluating the quality of a random number generator is such a trivial thing to do, it's not like there is decades of research behind it or anything.

        And assuming you are able to verify the statistical variance: are you going to put that logic in the loop, making it significantly more complex?

        Or are you going to do this test on your machine and then ship your code on the assumption that if it works on your machine, it will work everywhere else, too?

        > if your time() function has a resolution of seconds you need to let it run for more like 5 seconds.

        So not only is it insecure, it's agonizingly slow by design. Why do a system call that takes milliseconds at best, when we can run a loop in userspace for 5 seconds?

        All this just so you can avoid writing the obviously correct oneliner:

            if (getentropy(&seed, sizeof(seed)) != 0) abort();
        1. sltkr · · focus · HN ↗
          And to show my objections are not just theoretical I wrote a little program to check:

              #include <time.h>
              #include <stdio.h>
              
              static int estimate_entropy(long l) {
                  int bits = 1; /* for the sign bit */
                  if (l < 0) l = -l;
                  while (l > 0) {
                      ++bits;
                      l >>= 1;
                  }
                  return bits;
              }
              
              int main() {
                  struct timespec ts;
                  if (clock_getres(CLOCK_REALTIME, &ts) != 0) {
                      perror("clock_getres");
                      return 1;
                  }
                  printf("Clock resolution: %ld.%09ld\n", (long) ts.tv_sec, (long) ts.tv_nsec);
                  
                  #define N 50  /* number of samples */
                  struct timespec samples[N];
                  for (int i = 0; i < N; ++i) {
                      clock_gettime(CLOCK_REALTIME, &samples[i]);
                  }
              
                  printf("Deltas (ns):");
                  long deltas[N - 1];
                  for (int i = 0; i < N - 1; ++i) {
                      deltas[i] = 
                          (samples[i + 1].tv_sec - samples[i].tv_sec)*1000000000L
                          + (samples[i + 1].tv_nsec - samples[i].tv_nsec);
                      printf(" %4ld", deltas[i]);
                  }
                  printf("\n");
                  long entropy = 0;
                  printf("Deltas of deltas: ");
                  for (int i = 0; i < N - 2; ++i) {
                      long dd = deltas[i + 1] - deltas[i];
                      printf(" %4ld", dd);
                      entropy += estimate_entropy(dd);
                  }
                  printf("\n");
                  printf("Maximum entropy: %lld\n", entropy);
              }
          
          On my system this prints:

              Clock resolution: 0.000000001
              Deltas (ns):   55   51   23   23   25   24   24   24   24   24   25   25   24   24   24   24   24   25   24   24   24   25   25   24   24   23   25   24   24   25   24   23   25   25   26   23   25   24   24   25   26   24   23   25   25   26   24   25   24
              Deltas of deltas:    -4  -28    0    2   -1    0    0    0    0    1    0   -1    0    0    0    0    1   -1    0    0    1    0   -1    0   -1    2   -1    0    1   -1   -1    2    0    1   -3    2   -1    0    1    1   -2   -1    2    0    1   -2    1   -1
              Maximum entropy: 92
          
          So no, 50 iterations of that loop does not provide 256 bits of entropy due to random fluctuations in nanontime between calls.
          1. strenholme · · focus · HN ↗
            Thanks for writing that code!

            The point is this: Getting micro-timing won’t give us as much entropy as we want, but it will still give us entropy. So it’s a perfectly good yet-another-source of entropy to feed in to an entropy pool (such as the input to a XOF).

            If those Coldcard devices had used this code as one source of entropy, and this source of entropy was the only entropy still working, they never would had been compromised.

            (I won’t update my 18-year-old PRNG to use this code, of course, since that code is now 18 years old and there are no known weaknesses in said code)

            1. Taek · · focus · HN ↗
              Actually, it gives you as much entropy as you need, just increase the iterations. That guy's output is shockingly consistent, so to be conservative maybe we say 0.2 bits of entropy per iteration. So just do 1000 iterations. That's still only going to take a few milliseconds even on embedded hardware.

              EDIT: I reviewed his code, and he's not hashing between calls to check the clock; the hash call itself causes the CPU to heat up in arbitrary ways which changes the timing between hashes and introduces more entropy; removing that call basically entirely defeats the idea behind the technique, these results are fully invalid.

          2. Taek · · focus · HN ↗
            You don't need 256 bits of entropy, you only need 128.

            I have tested this method on over 100 different CPUs and I have never seen such consistent output. I'm genuinely surprised to see that you only hit 92 bits of entropy, but that can trivially be fixed by doing 10x the iterations. 500 iterations is still going to put you under a millisecond of cost.

            And, for what it's worth, code I've actually shipped has combined the above technique with Fortuna, and has typically targeted 2000 bits of entropy rather than 128 (for security buffer).

            EDIT: I reviewed his code, and he's not hashing between calls to check the clock; the hash call itself causes the CPU to heat up in arbitrary ways which changes the timing between hashes and introduces more entropy; removing that call basically entirely defeats the idea behind the technique, these results are fully invalid.

            ---

            I updated the code to insert the hash call, this is what I got for his original code on my machine, and the updated code with hashing on my machine (and the difference is cryptographically meaningful):

              === Original C — no hashing ===
              Clock resolution: 0.000000001
              Deltas (ns):   50   34   19   19   13   13   13   13   13   14   13   13   13   13   13   14   13   13   14   12   13   14   13   13   13   14   13   13   14   12   13   14   13   13   14   12   13   14   13   14   13   12   13   14   14   13   13   13   13
              Deltas of deltas:   -16  -15    0   -6    0    0    0    0    1   -1    0    0    0    0    1   -1    0    1   -2    1    1   -1    0    0    1   -1    0    1   -2    1    1   -1    0    1   -2    1    1   -1    1   -1   -1    1    1    0   -1    0    0    0
              Maximum entropy: 90
            
              === C with SHA-256 between clock reads ===
              Clock resolution: 0.000000001
              Deltas (ns): 756852 1287  542  470  472  445  442  436  434  439  488  435  433  434  440  439  439  435  432  433  435  432  433  433  429  433  453  441  437  437  431  433  432  430  431  438  436  434  431  433  435  436  435  433  430  436  435  437  428
              Deltas of deltas:  -755565 -745  -72    2  -27   -3   -6   -2    5   49  -53   -2    1    6   -1    0   -4   -3    1    2   -3    1    0   -4    4   20  -12   -4    0   -6    2   -1   -2    1    7   -2   -2   -3    2    2    1   -1   -2   -3    6   -1    2   -9
              Maximum entropy: 188
            1. sltkr · · focus · HN ↗
              The increase in calculated entropy comes from the first iteration being slower than the rest, but that's a bit misleading, because the first call is always going to be slower.

              Can you run the program 10 times and show me how much variance there actually is in the first column? Because if all the values lie between (say) 756000 and 757000 that's actually just 10 bits of entropy, not 19.5, and if the same applies to the other values, you're much closer to the original 90 bits.

              1. Taek · · focus · HN ↗
                I ran it 500,000 times, discarding the 10% most entropic results ... in the hopes of arriving at a relatively conservative estimate for the amount of entropy you actually get from each iteration. Here&#x27;s the prompt I used to generate the code: <a href="https:&#x2F;&#x2F;chatgpt.com&#x2F;share&#x2F;6ab2df4a-7f94-83ea-aecf-1bb57c4838b9" rel="nofollow">https:&#x2F;&#x2F;chatgpt.com&#x2F;share&#x2F;6ab2df4a-7f94-83ea-aecf-1bb57c4838...

                And here are the results of running that code:

                  === No hashing ===
                  Clock resolution: 0.000000001 seconds
                  Clock reads:                       500,000
                  Second-difference outcomes:        499,998
                  Retained outcomes:                 449,998 (90.000%)
                  Average Shannon information:       1.755579 bits&#x2F;retained outcome
                  Marginal min-entropy estimate:      1.339460 bits&#x2F;retained outcome
                  Lag-1 conditional min-entropy:      0.960079 bits&#x2F;retained adjacent outcome
                  Conservative descriptive proxy:    0.960079 bits&#x2F;retained outcome
                  Proxy scaled per clock iteration:  0.864067 bits&#x2F;iteration
                  These are empirical timing statistics, not a proven entropy rate.
                
                  === One SHA-256 between clock reads ===
                  Clock resolution: 0.000000001 seconds
                  Clock reads:                       500,000
                  Second-difference outcomes:        499,998
                  Retained outcomes:                 449,998 (90.000%)
                  Average Shannon information:       4.205076 bits&#x2F;retained outcome
                  Marginal min-entropy estimate:      3.610848 bits&#x2F;retained outcome
                  Lag-1 conditional min-entropy:      3.351217 bits&#x2F;retained adjacent outcome
                  Conservative descriptive proxy:    3.351217 bits&#x2F;retained outcome
                  Proxy scaled per clock iteration:  3.016082 bits&#x2F;iteration
                  These are empirical timing statistics, not a proven entropy rate.
                
                ------------

                As GPT helpfully points out, this isn&#x27;t a proven guarantee, but a reasonable estimate is somewhere between 3 and 4 bits of entropy per hash. That means 50 is actually enough, though if you want to be conservative I don&#x27;t think there&#x27;s any harm in doing 500 or even 5,000 instead of 50. And, if you are going to be using this in a hostile environment, it doesn&#x27;t hurt to also add a fortuna-like accumulator that resets your entropy every once in a while.

                I said this in another reply as well, but the reason that you get 3-4 bits of entropy per hash is because of the fundamental nature of CPUs. In addition to having considerable professional experience with cryptography, I also have considerable professional experience with hardware; hardware is fickle as hell, especially when your transistors are tens of nanometers large. Every time you flip a bit, you expend some energy, which heats up the chip, and the heat changes the timing of the next clock cycle. Chips are composed of literally billions of transistors, and each one is going to have a different temperature, because clock cycles last less than a nanosecond (well, embedded hardware is slower but the same idea still applies reliably) and that&#x27;s not enough time for temperature deltas to dissipate across the chip.

                Hashing is particularly chaotic because it lights up a different set of transistors on each clock cycle, which means the hotspots on the chip are being jerked around. Some transistors are going to light up 5-10 times in a row, and others are going to be idle 5-10 times in a row, and then randomly that changes. And all of this changes the number of picoseconds that it takes for a clock cycle to complete, which means that each clock cycle is genuinely going to take a different amount of time to complete, and stuff like temperature throttling is completely not at play whatsoever, because we&#x27;re not talking about chip-wide temperatures, we&#x27;re literally talking about temperature deltas between transistor a and transistor b.

                That makes it a really wonderful source of entropy for cryptographic applications, because the CPU clock is so critical that it&#x27;s almost never buggy (especially relative to other components that provide entropy), it&#x27;s also almost impossible to manipulate reliably by an attacker (unless the attacker has an exploit that allows them to set the value of the clock directly - which is possible, but it&#x27;s a very narrow surface area relative to other entropy sources), and you can completely take advantage of this entropy entirely in userspace, which once again heavily minimizes attack surface area and exposure to bugs.

                1. strenholme · · focus · HN ↗
                  I’m getting similar findings:

                    #include &lt;time.h&gt;
                    #include &lt;stdio.h&gt;
                    #include &lt;stdint.h&gt;
                  
                    int main() {
                          struct timespec foo;
                          int z;
                          uint8_t buffer[512];
                  
                          for(z=0;z&lt;128;z++) {
                                  clock_gettime(CLOCK_REALTIME,&amp;foo);
                                  buffer[z * 4] = (foo.tv_nsec &gt;&gt; 24) &amp; 0xff;
                                  buffer[z * 4 + 1] = (foo.tv_nsec &gt;&gt; 16) &amp; 0xff;
                                  buffer[z * 4 + 2] = (foo.tv_nsec &gt;&gt; 8) &amp; 0xff;
                                  buffer[z * 4 + 3] = (foo.tv_nsec) &amp; 0xff;
                          }
                          for(z=0;z&lt;512;z++) {
                                  printf(&quot;%02x &quot;,buffer[z]);
                                  if(z % 16 == 15) {puts(&quot;&quot;);}
                          }
                          return 0;
                    }
                  
                  (code is public domain)

                  Here, we see, running it on Windows, at least 1 but of entropy per clock_gettime() call. For people who argue kernel entropy is somehow more secure, perhaps they should become familiar with how kernels before Linux 5.6 or so on some devices had issues where (u)random wouldn’t provide enough entropy to be really secure (people would use haveged to make sure they had enough entropy).

          3. Taek · · focus · HN ↗
            Hold on I have to go edit the rest of my responses because I just assumed you wrote the code correctly; you did not.

            You are not hashing between calls to the timer. The sha256 hash itself is responsible for doing physical things to the chip (heating up some parts unevenly during the hashing computation) which introduces meaningful entropy between calls to the current time.

            You can&#x27;t just do calls to clock_gettime(), you have do an actual sequential sha256() call between them. Please run this code again and tell me what results you get.

            1. sltkr · · focus · HN ↗
              You&#x27;re missing the point, which is that although timings may vary on the system you are testing on, there is no system guarantee from hardware _or_ software that this always happens.

              Case in point:

              &gt; The sha256 hash itself is responsible for doing physical things to the chip (heating up some parts unevenly during the hashing computation)

              Some CPUs do thermal throttling, others run at a fixed frequency or are so underclocked that thermal throttling doesn&#x27;t kick in during your 50 iterations. This is exactly the source of randomness that is just not guaranteed to exist across systems.

              -----

              &gt; You can&#x27;t just do calls to clock_gettime(), you have do an actual sequential sha256() call between them. Please run this code again and tell me what results you get.

              OK, I&#x27;ll humor you, but to reiterate: it isn&#x27;t really my point.

              After adding hashing in the loop:

                  Clock resolution: 0.000000001
                  Hash: a8531a79fc350a3b35b3e82e33b759f6caa97a12efd16a715acb99065b6f3e89
                  Deltas (ns): 21662  452  335  297  290  288  288  291  289  293  290  289  290  284  287  297  289  289  295  288  287  286  292  291  287  287  301  289  299  290  292  288  291  292  296  294  295  293  290  287  297  292  292  292  288  295  291  289  296
                  Deltas of deltas:  -21210 -117  -38   -7   -2    0    3   -2    4   -3   -1    1   -6    3   10   -8    0    6   -7   -1   -1    6   -1   -4    0   14  -12   10   -9    2   -4    3    1    4   -2    1   -2   -3   -3   10   -5    0    0   -4    7   -4   -2    7
                  Maximum entropy: 177
              
              Here it&#x27;s mostly the first few iterations that are slow, the remaining ones are both fast and surprisingly consistent (the value 289 appears six times for example).

              It&#x27;s more obvious if you run it a few times in a row:

                  Deltas (ns): 21662  452  335  297  290  288  288  291  289  293  290  289  290  284  287  297  289  289  295  288  287  286  292  291  287  287  301  289  299  290  292  288  291  292  296  294  295  293  290  287  297  292  292  292  288  295  291  289  296
                  Deltas (ns): 22213  486  361  318  290  290  290  289  289  291  289  291  287  289  285  289  294  289  289  287  294  292  293  292  295  295  286  298  288  291  292  295  291  292  291  292  297  294  293  297  289  288  299  288  299  295  292  291  293
                  Deltas (ns): 23042  475  312  309  290  292  294  291  291  289  290  293  287  291  290  297  299  288  289  294  289  289  297  294  295  295  288  295  291  287  290  287  300  293  289  290  292  287  293  295  292  291  289  292  288  294  290  287  290
                  Deltas (ns): 22209  478  360  301  295  293  290  291  290  290  293  284  291  290  289  290  294  289  294  293  290  301  288  298  287  295  300  295  292  300  293  296  295  294  294  293  291  289  295  293  291  299  292  299  292  291  295  298  292
              
              The loop timings are quite consistent at least on a single system. That&#x27;s a problem if an attacker is able to run the same program on the same system to establish baseline timings.

              If I estimate the entropy as the logarithm of the difference between maximum and minimum I get only 146 bits of entropy in this case. Technically above your standard of 128 bit, but my point was: nothing guarantees you get even this much entropy on a less noisy system.

              This also shows the problem with your &quot;just run more iterations&quot; advice: in the above sample, the first five columns provide 24 bit of entropy per column, and the remaing 45 columns only 2.6 bits. So adding more iterations at the tail end wouldn&#x27;t double the entropy obtained.

              The code I used is here: <a href="https:&#x2F;&#x2F;pastebin.com&#x2F;ZrL1UDEg" rel="nofollow">https:&#x2F;&#x2F;pastebin.com&#x2F;ZrL1UDEg

              1. Taek · · focus · HN ↗
                The reason that you get 3-4 bits of entropy per hash is because of the fundamental nature of CPUs. In addition to having considerable professional experience with cryptography, I also have considerable professional experience with hardware; hardware is fickle as hell, especially when your transistors are tens of nanometers large. Every time you flip a bit, you expend some energy, which heats up the chip, and the heat changes the timing of the next clock cycle. Chips are composed of literally billions of transistors, and each one is going to have a different temperature, because clock cycles last less than a nanosecond (well, embedded hardware is slower but the same idea still applies reliably) and that&#x27;s not enough time for temperature deltas to dissipate across the chip.

                Hashing is particularly chaotic because it lights up a different set of transistors on each clock cycle, which means the hotspots on the chip are being jerked around. Some transistors are going to light up 5-10 times in a row, and others are going to be idle 5-10 times in a row, and then randomly that changes. And all of this changes the number of picoseconds that it takes for a clock cycle to complete, which means that each clock cycle is genuinely going to take a different amount of time to complete, and stuff like temperature throttling is completely not at play whatsoever, because we&#x27;re not talking about chip-wide temperatures, we&#x27;re literally talking about temperature deltas between transistor a and transistor b.

                That makes it a really wonderful source of entropy for cryptographic applications, because the CPU clock is so critical that it&#x27;s almost never buggy (especially relative to other components that provide entropy), it&#x27;s also almost impossible to manipulate reliably by an attacker (unless the attacker has an exploit that allows them to set the value of the clock directly - which is possible, but it&#x27;s a very narrow surface area relative to other entropy sources), and you can completely take advantage of this entropy entirely in userspace, which once again heavily minimizes attack surface area and exposure to bugs.

                I have searched far and wide for a CPU that does not reliably generate entropy using the iterated-hashing-against-the-clock method, and I have not found a single example of a CPU that consistently takes the same amount of time to complete a hash. And the reason isn&#x27;t implementation, the physics of CPUs simply insist on introducing entropy when trying to repeatedly hash something quickly.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.