Changed to work with: http://redbearlab.com/blenano/

Dependents:   Garagem

Fork of RNG by Francois Berder

Committer:
dgomes
Date:
Sun Aug 30 01:06:04 2015 +0000
Revision:
3:75b4f86fb3aa
Parent:
2:2eb90943983b
Works on the http://redbearlab.com/blenano/;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:299f3795114b 1 #include "Random.h"
feb11 0:299f3795114b 2 #include "Crypto.h"
feb11 0:299f3795114b 3
feb11 2:2eb90943983b 4 static uint8_t pool[16];
dgomes 3:75b4f86fb3aa 5 static AnalogIn e(A3),f(A4);
feb11 2:2eb90943983b 6
feb11 2:2eb90943983b 7 void Random::init()
feb11 0:299f3795114b 8 {
dgomes 3:75b4f86fb3aa 9 AnalogIn a(A1), b(A0), c(A2), d(A5);
feb11 0:299f3795114b 10
feb11 0:299f3795114b 11 uint16_t tmp = a.read_u16();
feb11 0:299f3795114b 12 memcpy(pool, &tmp, 2);
feb11 0:299f3795114b 13 tmp = b.read_u16();
feb11 0:299f3795114b 14 memcpy(&pool[2], &tmp, 2);
feb11 0:299f3795114b 15 tmp = c.read_u16();
feb11 0:299f3795114b 16 memcpy(&pool[4], &tmp, 2);
feb11 0:299f3795114b 17 tmp = d.read_u16();
feb11 0:299f3795114b 18 memcpy(&pool[6], &tmp, 2);
feb11 0:299f3795114b 19 tmp = a.read_u16();
feb11 0:299f3795114b 20 memcpy(&pool[8], &tmp, 2);
feb11 0:299f3795114b 21 tmp = b.read_u16();
feb11 0:299f3795114b 22 memcpy(&pool[10], &tmp, 2);
feb11 0:299f3795114b 23 tmp = c.read_u16();
feb11 0:299f3795114b 24 memcpy(&pool[12], &tmp, 2);
feb11 0:299f3795114b 25 tmp = d.read_u16();
feb11 0:299f3795114b 26 memcpy(&pool[14], &tmp, 2);
feb11 0:299f3795114b 27 }
feb11 0:299f3795114b 28
feb11 0:299f3795114b 29 uint8_t Random::getByte()
feb11 0:299f3795114b 30 {
feb11 0:299f3795114b 31 uint8_t hash[16];
feb11 0:299f3795114b 32 MD5::computeHash(hash, pool, 16);
feb11 0:299f3795114b 33 uint8_t tmp = pool[hash[6]%16];
feb11 0:299f3795114b 34 memcpy(pool, hash, 16);
feb11 1:0536a4ca8d35 35 pool[0] ^= (e.read_u16() & 0xff);
feb11 0:299f3795114b 36 return tmp ^ (f.read_u16() & 0xff);
feb11 0:299f3795114b 37 }
feb11 0:299f3795114b 38
feb11 0:299f3795114b 39 void Random::getBytes(uint8_t *out, uint32_t length)
feb11 0:299f3795114b 40 {
feb11 0:299f3795114b 41 for(int i = 0; i < length; ++i)
feb11 0:299f3795114b 42 out[i] = getByte();
feb11 0:299f3795114b 43 }