1 /*
2  * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <sys/random.h>
8 #include <sys/param.h>
9 #include <assert.h>
10 #include <errno.h>
11 #include <string.h>
12 #include "esp_random.h"
13 #include "esp_log.h"
14 
15 static const char *TAG = "RANDOM";
16 
getrandom(void * buf,size_t buflen,unsigned int flags)17 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
18 {
19     // Flags are ignored because:
20     // - esp_random is non-blocking so it works for both blocking and non-blocking calls,
21     // - don't have opportunity so set som other source of entropy.
22 
23     ESP_LOGD(TAG, "getrandom(buf=0x%x, buflen=%d, flags=%u)", (int) buf, buflen, flags);
24 
25     if (buf == NULL) {
26         errno = EFAULT;
27         ESP_LOGD(TAG, "getrandom returns -1 (EFAULT)");
28         return -1;
29     }
30 
31     esp_fill_random(buf, buflen);
32 
33     ESP_LOGD(TAG, "getrandom returns %d", buflen);
34     return buflen;
35 }
36