Using the Hardware Random Number Generator

The STM32 microcontrollers have built-in hardware random number generators. Unlike software pseudo-random number generators, they produce truly random sequences of numbers. This is important e.g. when producing cryptographic keys. Bad random numbers result in weak encryption.

Starting with Mountaineer 4.3.1 Beta 2, the Mountaineer firmware supports the STM32 hardware random number generator.

We have used the following API, a small subset of the full .NET framework's System.Security.Cryptography namespace:

 

namespace System.Security.Cryptography

{

  public abstract class RandomNumberGenerator : IDisposable
  {

    public static RandomNumberGenerator Create();

    public abstract void GetBytes(byte[] data);

  }

}

Here is an example of how to use this random number generator:

 

using Microsoft.SPOT.Native;

using System.Security.Cryptography;

 

static class Program

{

  static voiMain()
  {

     RandomNumberGenerator rng = RandomNumberGenerator.Create();

     byte[] r = new byte[1];

     rng.GetBytes(r);

     Debug.Print("random byte: " + r[0]);

  }

}