From d50a7fed2da9bff996930d9394c72892f5c63145 Mon Sep 17 00:00:00 2001 From: Rhys Weatherley Date: Thu, 2 Apr 2015 10:18:35 +1000 Subject: [PATCH] Noise source initialization that is post-RNG.begin() --- libraries/Crypto/NoiseSource.cpp | 15 +++++++++++++++ libraries/Crypto/NoiseSource.h | 2 ++ libraries/Crypto/RNG.cpp | 4 +++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/libraries/Crypto/NoiseSource.cpp b/libraries/Crypto/NoiseSource.cpp index e11b6b11..588e1aba 100644 --- a/libraries/Crypto/NoiseSource.cpp +++ b/libraries/Crypto/NoiseSource.cpp @@ -82,6 +82,21 @@ NoiseSource::~NoiseSource() * \sa calibrating(), output() */ +/** + * \brief Called when the noise source is added to RNG with + * \link RNGClass::addNoiseSource() RNG.addNoiseSource()\endlink. + * + * This function is intended for noise source initialization tasks that + * must be performed after \link RNGClass::begin() RNG.begin()\endlink + * has been called to initialize the global random number pool. + * For example, if the noise source has a unique identifier or serial + * number then this function can stir it into the pool at startup time. + */ +void NoiseSource::added() +{ + // Nothing to do here. +} + /** * \brief Called from subclasses to output noise to the global random * number pool. diff --git a/libraries/Crypto/NoiseSource.h b/libraries/Crypto/NoiseSource.h index c16a4122..7baf2430 100644 --- a/libraries/Crypto/NoiseSource.h +++ b/libraries/Crypto/NoiseSource.h @@ -35,6 +35,8 @@ public: virtual bool calibrating() const = 0; virtual void stir() = 0; + virtual void added(); + protected: virtual void output(const uint8_t *data, size_t len, unsigned int credit); }; diff --git a/libraries/Crypto/RNG.cpp b/libraries/Crypto/RNG.cpp index b0268ad4..bc392236 100644 --- a/libraries/Crypto/RNG.cpp +++ b/libraries/Crypto/RNG.cpp @@ -249,8 +249,10 @@ void RNGClass::begin(const char *tag, int eepromAddress) void RNGClass::addNoiseSource(NoiseSource &source) { #define MAX_NOISE_SOURCES (sizeof(noiseSources) / sizeof(noiseSources[0])) - if (count < MAX_NOISE_SOURCES) + if (count < MAX_NOISE_SOURCES) { noiseSources[count++] = &source; + source.added(); + } } /**