Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use analogRead or ECCX08 to generate random NTP port #481

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions src/utility/time/NTPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ unsigned long NTPUtils::getTime(UDP & udp)
udp.stop();
return 0;
}

uint8_t ntp_packet_buf[NTP_PACKET_SIZE];
udp.read(ntp_packet_buf, NTP_PACKET_SIZE);
udp.stop();
Expand All @@ -75,7 +75,7 @@ unsigned long NTPUtils::getTime(UDP & udp)
void NTPUtils::sendNTPpacket(UDP & udp)
{
uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0};

ntp_packet_buf[0] = 0b11100011;
ntp_packet_buf[1] = 0;
ntp_packet_buf[2] = 6;
Expand All @@ -84,23 +84,78 @@ void NTPUtils::sendNTPpacket(UDP & udp)
ntp_packet_buf[13] = 0x4E;
ntp_packet_buf[14] = 49;
ntp_packet_buf[15] = 52;

udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT);
udp.write(ntp_packet_buf, NTP_PACKET_SIZE);
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
Loading