1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 #if defined(__ANDROID__) || defined(__linux__)
16 uint8_t GetRandomBitRaw() {
17 return analogRead((uint8_t)ANALOG_IN) & 0x1;
20 uint8_t GetRandomBitRaw2() {
23 a = GetRandomBitRaw() | (GetRandomBitRaw()<<1);
25 return 0; // 1 to 0 transition: log a zero bit
28 return 1;// 0 to 1 transition: log a one bit
30 // For other cases, try again.
34 uint8_t GetRandomBit() {
37 a = GetRandomBitRaw2() | (GetRandomBitRaw2()<<1);
39 return 0; // 1 to 0 transition: log a zero bit
42 return 1;// 0 to 1 transition: log a one bit
44 // For other cases, try again.
49 int8_t OCSeedRandom() {
50 #if defined(__ANDROID__) || defined(__linux__)
51 int32_t fd = open("/dev/urandom", O_RDONLY);
54 uint32_t totalRead = 0; //how many integers were read
55 int32_t currentRead = 0;
56 while (totalRead < sizeof(randomSeed)) {
57 currentRead = read(fd, (uint8_t*) &randomSeed + totalRead,
58 sizeof(randomSeed) - totalRead);
60 totalRead += currentRead;
73 result += result + GetRandomBit();
81 void OCFillRandomMem(uint8_t * location, uint16_t len) {
86 *location++ = OCGetRandomByte();
90 uint32_t OCGetRandom() {
92 OCFillRandomMem((uint8_t*) &result, 4);
96 uint8_t OCGetRandomByte(void) {
97 #if defined(__ANDROID__) || defined(__linux__)
98 return rand() & 0x00FF;
100 return random(256) & 0x00FF;