Hardware Random Number Generator Banana Pi

It turns out that the A20 has a PRNG capability and the driver maintainer has written the necessary code to expose it as /dev/hwrng.  However it is not clear it will ever make the kernel modules we get.  This is because there is very little Allwinner implementation documentation, randomness is very hard to prove and random numbers are used in encryption space which makes a unknown random number generator a potential security risk, could it’s sequence be predictable?

It has to have an advantage over the built in random number generator to be worth the bother.

A hardware PRNG has two possible advantages, it’s faster and consumes less resources.   I decided to set it up and run some tests.

I downloaded and built the 4.4.26 kernel with all it’s modules.  I then applied the patches necessary to enable the hwrng, changed config and selected the option to build the module.

Copied the modules to the modules directory and remade the modules map by running depmod -a

A reboot maybe necessary.

If it works a device file will appear /dev/.   Here is mine /dev/hwrng and it produces random numbers.

rng_device

However I can get perfectly good random numbers out of /dev/urandom.  Is this one better?

To test I used a simple c program which reads 2 million random numbers.  It purpose is to wrap the generator with as little code as possible to test the raw speed.   Here’s the source code.


#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>

main()
{
  int byte_count = 64;
  long int i;
  char data[64];
  FILE *fp;
  fp = fopen("/dev/hwrng", "r");
  for (i=1; i<=2000000; i++)
    {
     fread(&data, 1, byte_count, fp);
    }
  fclose(fp);
}

First test is run against /dev/urandom, second against the hardware random number generator.

rng_timing

It’s more than twice as fast, 24 seconds compared to 51 seconds.   Which makes it worth the investment.  However a random number generator has to produce good random numbers.  Being fast is no good if the sequence can be predicted.   As I mentioned earlier testing randomness is really hard and this why this code may never make it into the released kernels, without documentation from Allwinner the maintainer will always have doubts.

I decided to do some empirical testing.  First up rngtools test program.

rngtest

Looks good,  I ran this a few times and it often fails one or 2.

Next up a pretty picture!

random20161123194250

I lifted this from here, because I liked it, again that looks pretty random to me, no obvious patterns.

Finally I downloaded, built and ran dieharder against both /dev/urandom and /dev/hwrng.   This is as good a test a non-expert such as myself is going to be able to do.   I ran it on both /dev/urandom and the /dev/hwrng generators.  It takes a very long time, but it also brings home how much faster the hwrng is, that finished the test a great quicker than urandom did.

Results are here.

Summary it passes all tests albeit with three being described as weak.   I have decided for that it’s OK and will be using it going forward.

If anyones else want to try it out I have put the module HERE  The caveats are it is compiled for kernel 4.4.26 and with same vermagic as Bananian.   So, it works only with that config.   To set up.


Download
gunzip the module
Copy to /lib/modules/4.4.26-bananian/kernel/drivers/crypto/sunxi-ss
depmod -a
(Maybe reboot)