uint32_t hw_rand(void) { uint32_t rng; #ifdef FREESCALE_KSDK_BM TRNG_GetRandomData(TRNG0, &rng, sizeof(rng)); #else while((RNG->SR & RNG_SR_OREG_LVL(0xF)) == 0) {}; /* Wait until FIFO has a value available */ rng = RNG->OR; /* Return next value in FIFO output register */ #endif return rng; }
ssize_t hwGetentropy(void *__buffer, const size_t __length) { SIM_SCGC6 |= SIM_SCGC6_RNGA; // enable RNG RNG_CR &= ~RNG_CR_SLP_MASK; RNG_CR |= RNG_CR_HA_MASK; //high assurance, not needed size_t pos = 0; while (pos < __length) { RNG_CR |= RNG_CR_GO_MASK; while (!(RNG_SR & RNG_SR_OREG_LVL(0xF))); const uint32_t rndVar = RNG_OR; const uint8_t bsize = (__length - pos) > sizeof(rndVar) ? sizeof(rndVar) : (__length - pos); (void)memcpy((uint8_t*)__buffer + pos, &rndVar, bsize); pos += bsize; } SIM_SCGC6 &= ~SIM_SCGC6_RNGA; // disable RNG return pos; }
uint32_t hw_rand(void) { while((RNG->SR & RNG_SR_OREG_LVL(0xF)) == 0) {}; /* Wait until FIFO has a value available */ return RNG->OR; /* Return next value in FIFO output register */ }