BlinkenArea - GitList
Repositories
Blog
Wiki
mips_sys
Code
Commits
Branches
Tags
Search
Tree:
c2b0401
Branches
Tags
master
mips_sys
fw
random.c
added file headers
Stefan Schuermans
commited
c2b0401
at 2012-04-08 11:54:40
random.c
Blame
History
Raw
/* MIPS I system * Copyright 2011-2012 Stefan Schuermans <stefan@schuermans.info> * Copyleft GNU public license V2 or later * http://www.gnu.org/copyleft/gpl.html */ #include "random.h" // FIXME: this random number generator is most likely not very good /// state of the linear feedback shift register (LFSR) static unsigned long random_lfsr = 1; /// step LFSR static void random_step(void) { random_lfsr = (random_lfsr >> 1) ^ (-(random_lfsr & 1U) & 0xD0000001U); } /** * @brief provide entropy * @param[in] ptr pointer to buffer containing entropy * @param[in] sz size of buffer */ void random_provide_entropy(const void *ptr, unsigned int sz) { const unsigned char *src = ptr; unsigned char val; unsigned int bit; for ( ; sz > 0; --sz) { val = *src++; for (bit = 0; bit < 8; ++bit) { if (val & 1) random_step(); val >>= 1; } } } /** * @brief get random data * @param[in] ptr pointer to buffer to fill with random data * @param[in] sz size of buffer to fill */ void random_get_data(void *ptr, unsigned int sz) { unsigned char *dest = ptr; unsigned char val; unsigned int bit; for ( ; sz > 0; --sz) { val = 0; for (bit = 0; bit < 8; ++bit) { random_step(); val = val << 1 | (random_lfsr & 1); } *dest++ = val; } }