This commit changes the way random numbers are available with MySensors. The AVR platform uses a 32 Bit random seed build from a floating analog pin. The way of the generation of this seed is FIPS 140-2 compliant. For support MCU with a fast way to generate random data, the hwGetentropy() command is now available to allow rapid generation of random data. The ESP8266 platform uses the internal random source for randomness for hwGetentropy() and Arduino's random() function. The NRF5 platform initializes the pseudo random number generator from internal hardware number generator. The internal AES-ECB unit generates random data after filling with HWRNG data by AES-CBC. On Linux getrandom() is implemented via syscall(). signerAtsha204SoftGetNonce speed: AVR = ~14000us (sha204Soft) NRF51 = ~152us (AES-CBC), 1739us (old sha204Soft) NRF52 = ~31us (AES-CBC), 183us (old sha204Soft) ESP8266 = ~20us (RANDOM_REG32), 380us (old sha204Soft) All Nonce results tested for FIPS 140-2 compliance successfully.
153 lines
4.3 KiB
C
153 lines
4.3 KiB
C
/*
|
|
* The MySensors Arduino library handles the wireless radio link and protocol
|
|
* between your home built sensors/actuators and HA controller of choice.
|
|
* The sensors forms a self healing radio network with optional repeaters. Each
|
|
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
|
|
* network topology allowing messages to be routed to nodes.
|
|
*
|
|
* Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
|
|
* Copyright (C) 2013-2017 Sensnology AB
|
|
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
|
|
*
|
|
* Documentation: http://www.mysensors.org
|
|
* Support Forum: http://forum.mysensors.org
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* version 2 as published by the Free Software Foundation.
|
|
*/
|
|
|
|
/**
|
|
* @file MyHw.h
|
|
*
|
|
* MySensors hardware abstraction layer
|
|
*/
|
|
|
|
#ifndef MyHw_h
|
|
#define MyHw_h
|
|
|
|
// Implement these as functions or macros
|
|
/*
|
|
#define hwInit() MY_SERIALDEVICE.begin(BAUD_RATE)
|
|
#define hwWatchdogReset() wdt_reset()
|
|
#define hwReboot() wdt_enable(WDTO_15MS); while (1)
|
|
#define hwMillis() millis()
|
|
|
|
#define hwDigitalWrite(__pin, __value)
|
|
#define hwDigitalRead(__pin)
|
|
#define hwPinMode(__pin, __value)
|
|
|
|
void hwReadConfigBlock(void* buf, void* adr, size_t length);
|
|
void hwWriteConfigBlock(void* buf, void* adr, size_t length);
|
|
void hwWriteConfig(int adr, uint8_t value);
|
|
uint8_t hwReadConfig(int adr);
|
|
*/
|
|
|
|
/**
|
|
* @def MY_HW_HAS_GETRANDOM
|
|
* @brief Define this, if hwGetentropy is implemented
|
|
*
|
|
* ssize_t hwGetentropy(void *__buffer, size_t __length);
|
|
*/
|
|
//#define MY_HW_HAS_GETRANDOM
|
|
|
|
/**
|
|
* Sleep for a defined time, using minimum power.
|
|
* @param ms Time to sleep, in [ms].
|
|
* @return Nonsense, please ignore.
|
|
*/
|
|
|
|
typedef uint8_t unique_id_t[16];
|
|
|
|
/**
|
|
* Sleep for a defined time, using minimum power.
|
|
* @param ms Time to sleep, in [ms].
|
|
* @return -1.
|
|
*/
|
|
int8_t hwSleep(unsigned long ms);
|
|
|
|
/**
|
|
* Sleep for a defined time, using minimum power, or until woken by interrupt.
|
|
* @param interrupt Interrupt number, which can wake the mcu from sleep.
|
|
* @param mode Interrupt mode, as passed to attachInterrupt.
|
|
* @param ms Time to sleep, in [ms].
|
|
* @return -1 when woken by timer, or interrupt number when woken by interrupt.
|
|
*/
|
|
int8_t hwSleep(uint8_t interrupt, uint8_t mode, unsigned long ms);
|
|
|
|
/**
|
|
* Sleep for a defined time, using minimum power, or until woken by one of the interrupts.
|
|
* @param interrupt1 Interrupt1 number, which can wake the mcu from sleep.
|
|
* @param mode1 Interrupt1 mode, as passed to attachInterrupt.
|
|
* @param interrupt2 Interrupt2 number, which can wake the mcu from sleep.
|
|
* @param mode2 Interrupt2 mode, as passed to attachInterrupt.
|
|
* @param ms Time to sleep, in [ms].
|
|
* @return -1 when woken by timer, or interrupt number when woken by interrupt.
|
|
*/
|
|
int8_t hwSleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
|
|
unsigned long ms);
|
|
|
|
/**
|
|
* Retrieve unique ID
|
|
* @param uniqueID uniqueID
|
|
* @return True if unique ID successfully retrieved
|
|
*/
|
|
bool hwUniqueID(unique_id_t* uniqueID);
|
|
|
|
/**
|
|
* CPU voltage
|
|
* @return CPU voltage in mV
|
|
*/
|
|
uint16_t hwCPUVoltage();
|
|
|
|
/**
|
|
* CPU frequency
|
|
* @return CPU frequency in 1/10Mhz
|
|
*/
|
|
uint16_t hwCPUFrequency();
|
|
|
|
/**
|
|
* Free memory
|
|
* @return free memory in bytes
|
|
*/
|
|
uint16_t hwFreeMem();
|
|
|
|
#if defined(DEBUG_OUTPUT_ENABLED)
|
|
void hwDebugPrint(const char *fmt, ... );
|
|
#endif
|
|
|
|
/**
|
|
* @def MY_CRITICAL_SECTION
|
|
* @brief Creates a block of code that is guaranteed to be executed atomically.
|
|
* Upon entering the block all interrupts are disabled, and re-enabled upon
|
|
* exiting the block from any exit path.
|
|
* A typical example that requires atomic access is a 16 (or more) bit variable
|
|
* that is shared between the main execution path and an ISR, on an 8-bit
|
|
* platform (e.g AVR):
|
|
* @code
|
|
* volatile uint16_t val = 0;
|
|
*
|
|
* void interrupHandler()
|
|
* {
|
|
* val = ~val;
|
|
* }
|
|
*
|
|
* void loop()
|
|
* {
|
|
* uint16_t copy_val;
|
|
* MY_CRITICAL_SECTION
|
|
* {
|
|
* copy_val = val;
|
|
* }
|
|
* }
|
|
* @endcode
|
|
* All code within the MY_CRITICAL_SECTION block will be protected from being
|
|
* interrupted during execution.
|
|
*/
|
|
#ifdef DOXYGEN
|
|
#define MY_CRITICAL_SECTION
|
|
#define MY_HW_HAS_GETRANDOM
|
|
#endif /* DOXYGEN */
|
|
|
|
#endif // #ifdef MyHw_h
|