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

Better random number generation from RSSI #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. **NB** - these are not cryptographically secure random numbers! Use with caution!

```
byte b = LoRa.random();
Expand Down
21 changes: 20 additions & 1 deletion src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/LoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down