Refactor Hardware RNG functions to esp/hwrand.h
As suggested by @foogod, thanks!
This commit is contained in:
parent
09405a9095
commit
87f77b1021
3 changed files with 51 additions and 7 deletions
27
core/esp_hwrand.c
Normal file
27
core/esp_hwrand.c
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* Hardware Random Number Generator Functions
|
||||
*
|
||||
* For documentation, see http://esp8266-re.foogod.com/wiki/Random_Number_Generator
|
||||
*
|
||||
* Part of esp-open-rtos
|
||||
* Copyright (C) 2015 Angus Gratton
|
||||
* BSD Licensed as described in the file LICENSE
|
||||
*/
|
||||
#include <esp/hwrand.h>
|
||||
#include <esp/wdev_regs.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Return a random 32-bit number */
|
||||
uint32_t hwrand(void)
|
||||
{
|
||||
return WDEV.HWRNG;
|
||||
}
|
||||
|
||||
/* Fill a variable size buffer with data from the Hardware RNG */
|
||||
void hwrand_fill(uint8_t *buf, size_t len)
|
||||
{
|
||||
for(size_t i = 0; i < len; i+=4) {
|
||||
uint32_t random = WDEV.HWRNG;
|
||||
/* using memcpy here in case 'buf' is unaligned */
|
||||
memcpy(buf + i, &random, (i+4 <= len) ? 4 : (len % 4));
|
||||
}
|
||||
}
|
22
core/include/esp/hwrand.h
Normal file
22
core/include/esp/hwrand.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
/** esp/hwrand.h
|
||||
*
|
||||
* Hardware Random Number Generator functions.
|
||||
*
|
||||
* For documentation, see http://esp8266-re.foogod.com/wiki/Random_Number_Generator
|
||||
*
|
||||
* Part of esp-open-rtos
|
||||
* Copyright (C) 2015 Angus Gratton
|
||||
* BSD Licensed as described in the file LICENSE
|
||||
*/
|
||||
#ifndef _ESP_RNG_H
|
||||
#define _ESP_RNG_H
|
||||
#include <stdint.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
/* Return a random 32-bit number */
|
||||
uint32_t hwrand(void);
|
||||
|
||||
/* Fill a variable size buffer with data from the Hardware RNG */
|
||||
void hwrand_fill(uint8_t *buf, size_t len);
|
||||
|
||||
#endif
|
|
@ -9,18 +9,13 @@
|
|||
* BSD Licensed as described in the file LICENSE
|
||||
*/
|
||||
#include <mbedtls/entropy_poll.h>
|
||||
#include <esp/wdev_regs.h>
|
||||
#include <string.h>
|
||||
#include <esp/hwrand.h>
|
||||
|
||||
int mbedtls_hardware_poll( void *data,
|
||||
unsigned char *output, size_t len, size_t *olen )
|
||||
{
|
||||
(void)(data);
|
||||
for(int i = 0; i < len; i+=4) {
|
||||
uint32_t random = WDEV.HWRNG;
|
||||
/* using memcpy here in case output is unaligned */
|
||||
memcpy(output + i, &random, (i+4 <= len) ? 4 : (len % 4));
|
||||
}
|
||||
hwrand_fill(output, len);
|
||||
if(olen)
|
||||
*olen = len;
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue