From 6f2e1658f86672b42f7cf1411f5a02d269ab39fb Mon Sep 17 00:00:00 2001 From: Mark C Date: Mon, 28 May 2018 14:03:50 +0100 Subject: [PATCH 1/3] changed random() to getRSSI() and added a function to get a better random byte. --M. --- src/LoRa.cpp | 21 ++++++++++++++++++++- src/LoRa.h | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 4296e76..163e3c4 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -512,9 +512,28 @@ void LoRaClass::disableCrc() writeRegister(REG_MODEM_CONFIG_2, readRegister(REG_MODEM_CONFIG_2) & 0xfb); } +byte LoRaClass::getRSSI() +{ + return readRegister(REG_RSSI_WIDEBAND); +} + byte LoRaClass::random() { - return readRegister(REG_RSSI_WIDEBAND); + int n=0, bits=7; + while(bits--) { + n<<=1; + while(1){ + // implement a basic von Neumann Extractor + int a=(readRegister(REG_RSSI_WIDEBAND) & 1); + if(a != (readRegister(REG_RSSI_WIDEBAND) & 1)){ + // put random, whitened bit in n + n |= a; + break; + } + } + } + // return the random byte + return n; } void LoRaClass::setPins(int ss, int reset, int dio0) diff --git a/src/LoRa.h b/src/LoRa.h index 248a988..0c4831b 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -71,6 +71,7 @@ class LoRaClass : public Stream { void crc() { enableCrc(); } void noCrc() { disableCrc(); } + byte getRSSI(); byte random(); void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN); From ddbbbbf2c7d80c9b128c1e4e5bfda0da93e13355 Mon Sep 17 00:00:00 2001 From: Mark C Date: Mon, 28 May 2018 14:07:44 +0100 Subject: [PATCH 2/3] added documentation notes for previous commit. --- API.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/API.md b/API.md index a9a89b5..1775bf7 100644 --- a/API.md +++ b/API.md @@ -331,9 +331,19 @@ LoRa.disableCrc(); ## Other functions +### Get RSSI + +Gets the RSSI value from the radio. + +``` +byte b = LoRa.getRSSI(); +``` + +Returns RSSI value as a byte. + ### Random -Generate a random byte, based on the Wideband RSSI measurement. +Generate a random byte, based on the Wideband RSSI measurement run through a von Neumann Extractor. ``` byte b = LoRa.random(); From c34a7193548570df8bec6b92be7d4bb4f92d63b8 Mon Sep 17 00:00:00 2001 From: Mark C Date: Mon, 28 May 2018 14:15:11 +0100 Subject: [PATCH 3/3] Added required warning that the output of random() is not CS. --- API.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/API.md b/API.md index 1775bf7..d1c230c 100644 --- a/API.md +++ b/API.md @@ -343,7 +343,7 @@ Returns RSSI value as a byte. ### Random -Generate a random byte, based on the Wideband RSSI measurement run through a von Neumann Extractor. +Generate a random byte, based on the Wideband RSSI measurement run through a von Neumann Extractor. **NB** - these are not cryptographically secure random numbers! Use with caution! ``` byte b = LoRa.random();