1 // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <sys/random.h>
16 #include <sys/param.h>
17 #include <assert.h>
18 #include <errno.h>
19 #include <string.h>
20 #include "esp_system.h"
21 #include "esp_log.h"
22
23 static const char *TAG = "RANDOM";
24
getrandom(void * buf,size_t buflen,unsigned int flags)25 ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
26 {
27 // Flags are ignored because:
28 // - esp_random is non-blocking so it works for both blocking and non-blocking calls,
29 // - don't have opportunity so set som other source of entropy.
30
31 ESP_LOGD(TAG, "getrandom(buf=0x%x, buflen=%d, flags=%u)", (int) buf, buflen, flags);
32
33 if (buf == NULL) {
34 errno = EFAULT;
35 ESP_LOGD(TAG, "getrandom returns -1 (EFAULT)");
36 return -1;
37 }
38
39 esp_fill_random(buf, buflen);
40
41 ESP_LOGD(TAG, "getrandom returns %d", buflen);
42 return buflen;
43 }
44