BlinkenArea - GitList
Repositories
Blog
Wiki
flaneth
Code
Commits
Branches
Tags
Search
Tree:
e8658d5
Branches
Tags
master
flaneth
firmware.dartboard
random.c
initial commit after making CF identify work
Stefan Schuermans
commited
e8658d5
at 2012-04-15 19:57:57
random.c
Blame
History
Raw
/* flaneth - flash and ethernet - dartboard mod * version 0.1 date 2008-11-09 * Copyright (C) 2007-2008 Stefan Schuermans <stefan@schuermans.info> * Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html * a BlinkenArea project - http://www.blinkenarea.org/ */ #include <stdlib.h> #include "random.h" // number of complete bytes a call to random( ) returns #if RANDOM_MAX < 0xFF #error random number generator is not able to return a random byte #elif RANDOM_MAX < 0xFFFF #define RANDOM_BYTES 1 #elif RANDOM_MAX < 0xFFFFFF #define RANDOM_BYTES 2 #elif RANDOM_MAX < 0xFFFFFFFF #define RANDOM_BYTES 3 #else #define RANDOM_BYTES 4 #endif // entropy collected so far unsigned long RandomEntropy = 0; // the entropy itself unsigned char RandomEntropyCnt // number of times entropy was collected so far = sizeof( unsigned long ) * 8 - 5; // (use first entropy (few is better than none)) // provide some entropy void RandomProvideEntropy( unsigned char Entropy ) // (extern) { // collect entropy RandomEntropy = RandomEntropy << 1 ^ (unsigned int)Entropy; RandomEntropyCnt++; } // task function to do the work - call from main loop void RandomTask( void ) // (extern) { // enough entropy was collected if( RandomEntropyCnt >= sizeof( unsigned long ) * 8 ) // one time entropy for every bit of RandomEntropy { RandomEntropyCnt = 0; // re-seed random number generator srandom( RandomEntropy ); } } // get random data void RandomGetData( unsigned char * pData, unsigned char Length ) // (extern) { unsigned long r; unsigned char i; // return random data for( ; Length >= RANDOM_BYTES; Length -= RANDOM_BYTES ) { r = random( ); for( i = 0; i < RANDOM_BYTES; i++ ) { *pData = (unsigned char)r; pData++; r >>= 8; } } if( Length > 0 ) { r = random( ); for( i = 0; i < Length; i++ ) { *pData = (unsigned char)r; pData++; r >>= 8; } } }