esp32: Add esp_fill_random() function

Convenience function to fill a buffer with random bytes.

Add some unit tests (only sanity checks, really.)
This commit is contained in:
Angus Gratton
2018-08-15 18:20:16 +10:00
committed by bot
parent 767ec27350
commit 83a179abb0
12 changed files with 124 additions and 71 deletions

View File

@@ -16,6 +16,7 @@
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <sys/param.h>
#include "esp_attr.h"
#include "esp_clk.h"
#include "soc/wdev_reg.h"
@@ -54,3 +55,16 @@ uint32_t IRAM_ATTR esp_random(void)
last_ccount = ccount;
return result ^ REG_READ(WDEV_RND_REG);
}
void esp_fill_random(void *buf, size_t len)
{
assert(buf != NULL);
uint8_t *buf_bytes = (uint8_t *)buf;
while (len > 0) {
uint32_t word = esp_random();
uint32_t to_copy = MIN(sizeof(word), len);
memcpy(buf_bytes, &word, to_copy);
buf_bytes += to_copy;
len -= to_copy;
}
}