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

@@ -36,17 +36,8 @@ ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
return -1;
}
uint8_t *dst = (uint8_t *) buf;
ssize_t ret = 0;
esp_fill_random(buf, buflen);
while (ret < buflen) {
const uint32_t random = esp_random();
const int needed = buflen - ret;
const int copy_len = MIN(sizeof(random), needed);
memcpy(dst + ret, &random, copy_len);
ret += copy_len;
}
ESP_LOGD(TAG, "getrandom returns %d", ret);
return ret;
ESP_LOGD(TAG, "getrandom returns %d", buflen);
return buflen;
}