Skip to content

Commit

Permalink
Do not use analogRead or ECCX08 to generate random NTP port
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Jun 20, 2024
1 parent b3f2282 commit 471296d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
67 changes: 61 additions & 6 deletions src/utility/time/NTPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,72 @@ void NTPUtils::sendNTPpacket(UDP & udp)
udp.endPacket();
}

#ifdef NTP_USE_RANDOM_PORT
int NTPUtils::getRandomPort(int const min_port, int const max_port)
{
#if defined (BOARD_HAS_ECCX08)
return ECCX08.random(min_port, max_port);
#elif defined (ARDUINO_ARCH_ESP8266) || (ARDUINO_ARCH_ESP32)
#if defined (ARDUINO_ARCH_ESP8266) || (ARDUINO_ARCH_ESP32) || \
(ARDUINO_ARCH_RENESAS) || (ARDUINO_ARCH_MBED)
/* Uses HW Random Number Generator */
return random(min_port, max_port);
#elif defined (ARDUINO_ARCH_SAMD)
/* Use ADC to generate a seed */
randomSeed(adcSeed());
#else
randomSeed(analogRead(0));
return random(min_port, max_port);
#endif
return random(min_port, max_port);
}

#endif /* #ifndef HAS_LORA */
#if defined (ARDUINO_ARCH_SAMD)
unsigned long NTPUtils::adcSeed()
{
uint32_t seed = 0;
uint32_t bitCount = 0;
uint16_t sampctlr = ADC->SAMPCTRL.reg;

// Use lowest sampling time
ADC->SAMPCTRL.reg = 0;
// Enable ADC
ADC->CTRLA.bit.ENABLE = 1;
while (ADC->STATUS.bit.SYNCBUSY == 1);
do {
uint16_t adcReading;
// Start ADC conversion
ADC->SWTRIG.bit.START = 1;
// Wait until ADC conversion is done
while (!(ADC->INTFLAG.bit.RESRDY));
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Get result
adcReading = ADC->RESULT.reg;
// Clear result ready flag
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Take least significant bit
uint8_t b0 = adcReading & 0x0001;
ADC->SWTRIG.bit.START = 1;
// Wait until ADC conversion is done
while (!(ADC->INTFLAG.bit.RESRDY));
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Get result
adcReading = ADC->RESULT.reg;
// Clear result ready flag
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Take least significant nibble
uint8_t b1 = adcReading & 0x0001;
if (b0 == b1) {
continue;
}
seed |= b0 << bitCount;
bitCount++;
} while(bitCount < 32);
// Disable ADC
ADC->CTRLA.bit.ENABLE = 0;
// restore original sampling time
ADC->SAMPCTRL.reg = sampctlr;
return seed;
}
#endif /* ARDUINO_ARCH_SAMD */

#endif /* NTP_USE_RANDOM_PORT */

#endif /* !HAS_LORA */
3 changes: 3 additions & 0 deletions src/utility/time/NTPUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ class NTPUtils
public:

static unsigned long getTime(UDP & udp);
#if NTP_USE_RANDOM_PORT
static int getRandomPort(int const min_port, int const max_port);
#endif

private:

Expand All @@ -57,6 +59,7 @@ class NTPUtils
static constexpr const char * NTP_TIME_SERVER = "time.arduino.cc";

static void sendNTPpacket(UDP & udp);
static unsigned long adcSeed();
};

#endif /* #ifndef HAS_LORA */
Expand Down

0 comments on commit 471296d

Please sign in to comment.