The capability of the ESP32 to generate truly random numbers is important for cryptographic operations. A random number generator generates a sequence of random numbers. A hardware random number generator is also known as a true random number generator. It generates random numbers from some physical process, not from code. A fundamental distinction is made between non-deterministic and deterministic random number generators. Since the implementation of a software procedure is usually deterministic, an external (e.g. physical) operation must be included to realize a non-deterministic random number generator. Pulse fluctuations of electronic circuits such as thermal noise of resistance or radioactive decay processes are exploited in the hardware random number generators. In general, all-natural sources based on physical effects that provide a fairly high quality can be used, but also other asynchronous sources. Some of the examples include atmospheric noise (such as analogue radio that is not tuned to a station), Photographic CCD sensor noise in a dark room, voltage fluctuations on a Z-diode. A hardware random number generator often has a transducer to convert the physical phenomena to an electrical signal. An amplifier increases the amplitude of the random fluctuations making it measurable. An analogue-to-digital converter can convert it into a binary digit.
In practice, software random number generators are often used in a mixed form with hardware random number generators. They are called hybrid random number generators and Linux or BSD are good examples of such deployment.
Table of Contents |
Uses of Hardware Random Number Generators
---
Dices have are mostly used in gambling to randomize the events in a game. One main use-case of the hardware random number generators is in data encryption, like for creating cryptographic keys to encrypt and sign. Hardware random number generators produce sequences of numbers technically difficult to be predictable provide greater security.
Unpredictable numbers were first used in gambling, slot machines and roulette wheels. In these use-cases, random number generation is regulated by governmental gaming commissions. Once, even the best online slots machines used to suffer from cheaters a lot before technology got more advanced, and now there are minimal ways in which a user can cheat the system. Good physical methods for generating random numbers are also the dice and drawing of lottery numbers with the typical machines. Random draws in relatively fast succession were realized in electromechanical gambling machines, based on cam discs with eccentric wheel and a switching time variator.
Drawbacks of the Hardware Random Number Generators
The physical random number generators are not fast since independence and equal distribution of the generated random numbers can only be achieved by sufficiently large distances in the observation of the physical processes or intercept methods. However, this is only a question of the technology used, because random processes such as thermal noise have limit frequencies of many terahertz.
There are other physical issues. Such as, Geiger-Müller counter tubes typically have a lifespan of one trillion pulses and are also dependent on temperature, magnetic fields and supply voltage. Also, for Geiger counters, the pulse rate must be significantly higher than the clock frequency at which the pulses are read. One solution to this problem is to use many random number generators in one unit. According to the central limit set of statistics, you get perfectly random bits even with bad random number generators (provided there are enough random number generators used).
Hardware Random Number Generator in ESP32
The ESP32 system-on-chip includes a True Random Number Generator as a peripheral. The random number generation is based on the noise of it’s WiFi or Bluetooth RF subsystem. This means, when the Bluetooth and WiFi are disabled, the ESP32 falls back to the software-based pseudorandom number generation. The Random Number Generator with the WiFi-enabled known to be passed the Dieharder Random Number Test suite. The communication between a CPU and internal peripherals is through the registers, which is nothing but specific memory addresses. The random number generator stores the produced number in a register named RNG_DATA_REG
, located at address 0x3FF75144
. The ESP32’s true random number generator can write a 32-bit value to the RNG_DATA_REG
register.
Using the Hardware Random Number Generator of ESP32 from Arduino IDE
Official Arduino software has a function named random(), which generates pseudo-numbers. This is a lower level function. The official Arduino library for ESP32 supports esp_random()
function which is the way to get the true random number from the RNG_DATA_REG
register. The associated file can be found here on GitHub (Line number 138).
The software and hardware of random number generation can be tested with a small sketch for the ESP32 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void setup() { Serial.begin(115200); } void loop() { Serial.println("-----------"); // Official ESP32 function Serial.println(esp_random()); // Official Arduino function Serial.println(random(1,100)); delay(1000); } |