1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Register the noise sources with RNG at setup time

This commit is contained in:
Rhys Weatherley
2015-03-25 19:35:44 +10:00
parent fd38b7e127
commit 067e8ac177
6 changed files with 52 additions and 23 deletions

View File

@@ -70,20 +70,20 @@
* // Stir in the Ethernet MAC address.
* RNG.stir(mac_address, sizeof(mac_address));
*
* // Add the noise source to the list of sources known to RNG.
* RNG.addNoiseSource(noise);
*
* // ...
* }
* \endcode
*
* The application should regularly call stir() to mix in new data from
* the noise source and also regularly call loop():
* The application should regularly call loop() to stir in new data
* from the registered noise sources and to periodically save the seed:
*
* \code
* void loop() {
* // ...
*
* // If the noise source has accumulated new entropy, then stir it in.
* RNG.stir(noise);
*
* // Perform regular housekeeping on the random number generator.
* RNG.loop();
*
@@ -170,6 +170,7 @@ RNGClass::RNGClass()
, firstSave(1)
, timer(0)
, timeout(3600000UL) // 1 hour in milliseconds
, count(0)
{
}
@@ -197,7 +198,7 @@ RNGClass::~RNGClass()
* additional entropy data from noise sources to initialize the random
* number generator properly.
*
* \sa stir(), save()
* \sa addNoiseSource(), stir(), save()
*/
void RNGClass::begin(const char *tag, int eepromAddress)
{
@@ -234,6 +235,25 @@ void RNGClass::begin(const char *tag, int eepromAddress)
save();
}
/**
* \brief Adds a noise source to the random number generator.
*
* \param source The noise source to add, which will be polled regularly
* by loop() to accumulate noise-based entropy from the source.
*
* RNG supports a maximum of four noise sources. If the application needs
* more than that then the application must poll the noise sources itself by
* calling NoiseSource::stir() directly.
*
* \sa loop(), begin()
*/
void RNGClass::addNoiseSource(NoiseSource &source)
{
#define MAX_NOISE_SOURCES (sizeof(noiseSources) / sizeof(noiseSources[0]))
if (count < MAX_NOISE_SOURCES)
noiseSources[count++] = &source;
}
/**
* \brief Sets the amount of time between automatic seed saves.
*
@@ -485,6 +505,10 @@ void RNGClass::save()
*/
void RNGClass::loop()
{
// Stir in the entropy from all registered noise sources.
for (uint8_t posn = 0; posn < count; ++posn)
noiseSources[posn]->stir();
// Save the seed if the auto-save timer has expired.
if ((millis() - timer) >= timeout)
save();

View File

@@ -35,6 +35,7 @@ public:
~RNGClass();
void begin(const char *tag, int eepromAddress);
void addNoiseSource(NoiseSource &source);
void setAutoSaveTime(uint16_t minutes);
@@ -60,6 +61,8 @@ private:
uint16_t firstSave : 1;
unsigned long timer;
unsigned long timeout;
NoiseSource *noiseSources[4];
uint8_t count;
void rekey();
};

View File

@@ -76,15 +76,15 @@
* // "MyApp 1.0" and load the previous seed from EEPROM address 500.
* RNG.begin("MyApp 1.0", 500);
*
* // Add the noise source to the list of sources known to RNG.
* RNG.addNoiseSource(noise);
*
* // ...
* }
*
* void loop() {
* // ...
*
* // If the noise source has accumulated new entropy, then stir it in.
* RNG.stir(noise);
*
* // Perform regular housekeeping on the random number generator.
* RNG.loop();
*

View File

@@ -56,15 +56,15 @@
* // "MyApp 1.0" and load the previous seed from EEPROM address 500.
* RNG.begin("MyApp 1.0", 500);
*
* // Add the noise source to the list of sources known to RNG.
* RNG.addNoiseSource(noise);
*
* // ...
* }
*
* void loop() {
* // ...
*
* // If the noise source has accumulated new entropy, then stir it in.
* RNG.stir(noise);
*
* // Perform regular housekeeping on the random number generator.
* RNG.loop();
*

View File

@@ -29,6 +29,9 @@ void setup() {
// Initialize the random number generator.
RNG.begin(RNG_APP_TAG, RNG_EEPROM_ADDRESS);
// Add the noise source to the list of sources known to RNG.
RNG.addNoiseSource(noise);
startTime = millis();
}
@@ -58,9 +61,6 @@ void loop() {
Serial.println("calibrating");
}
// If the noise source has accumulated new entropy, then stir it in.
RNG.stir(noise);
// Perform regular housekeeping on the random number generator.
RNG.loop();