Skip to content

Instantly share code, notes, and snippets.

@bloc97
Last active December 29, 2017 19:49
Show Gist options
  • Save bloc97/209ad6ad53a6c0c9a64da0244b833b14 to your computer and use it in GitHub Desktop.
Save bloc97/209ad6ad53a6c0c9a64da0244b833b14 to your computer and use it in GitHub Desktop.
Two methods of harvesting entropy from an Arduino microcontroller board. Pseudo-Publication here: https://gist.github.com/bloc97/b55f684d17edd8f50df8e918cbc00f94
#include <MPU6050.h>
uint8_t randomByte(uint8_t analogPin) {
int firstValue = analogRead(analogPin);
const uint8_t minTemporalEntropyScale = 8; //Resolution in microseconds of the onboard micros() timer
int bitsCaptured = 0;
uint8_t randomBits = 0x00;
uint8_t lastRandomBit = 0x00;
while (bitsCaptured < 8) {
int newValue = analogRead(analogPin);
if (firstValue != newValue) {
firstValue = newValue;
uint8_t microsNoise = (micros() / minTemporalEntropyScale) & 1;
uint8_t previousNoise = lastRandomBit;
lastRandomBit = (microsNoise ^ previousNoise) & 1);
randomBits = randomBits | (lastRandomBit << bitsCaptured);
bitsCaptured++;
}
}
return randomBits;
}
uint8_t randomByteFromAccel(MPU6050 mpu) {
Vector rawAccel = mpu.readRawAccel();
const uint8_t minEntropyScale = 4; //Resolution of the MPU sensor, the one I used outputed integers that are multiples of 4.
int16_t xBits = rawAccel.XAxis;
int16_t yBits = rawAccel.YAxis;
int16_t zBits = rawAccel.ZAxis;
uint8_t real4xBits = (xBits / minEntropyScale) & 0xF;
uint8_t real4yBits = (yBits / minEntropyScale) & 0xF;
uint8_t real4zBits = (zBits / minEntropyScale) & 0xF;
uint8_t random8Bits = ((real4xBits & 0x3) << 6) ^ (real4zBits << 4) ^ (real4yBits << 2) ^ real4xBits ^ (real4zBits >> 2);
return random8Bits;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment