mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Merge branch 'feature/expat_getrandom' into 'master'
expat: add source of entropy See merge request idf/esp-idf!2912
This commit is contained in:
commit
146e87fed5
@ -5,7 +5,7 @@ COMPONENT_ADD_INCLUDEDIRS := expat/expat/lib port/include
|
|||||||
|
|
||||||
COMPONENT_SRCDIRS := expat/expat/lib port
|
COMPONENT_SRCDIRS := expat/expat/lib port
|
||||||
|
|
||||||
CFLAGS += -DHAVE_EXPAT_CONFIG_H -DXML_POOR_ENTROPY
|
CFLAGS += -DHAVE_EXPAT_CONFIG_H -DHAVE_GETRANDOM
|
||||||
# Temporary suppress "fallthrough" warnings until they are fixed in expat repo
|
# Temporary suppress "fallthrough" warnings until they are fixed in expat repo
|
||||||
CFLAGS += -Wno-implicit-fallthrough
|
CFLAGS += -Wno-implicit-fallthrough
|
||||||
|
|
||||||
|
30
components/newlib/platform_include/sys/random.h
Normal file
30
components/newlib/platform_include/sys/random.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#ifndef __SYS_RANDOM__
|
||||||
|
#define __SYS_RANDOM__
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif //__SYS_RANDOM__
|
52
components/newlib/random.c
Normal file
52
components/newlib/random.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
#include <sys/random.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
|
||||||
|
static const char *TAG = "RANDOM";
|
||||||
|
|
||||||
|
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
|
||||||
|
{
|
||||||
|
// Flags are ignored because:
|
||||||
|
// - esp_random is non-blocking so it works for both blocking and non-blocking calls,
|
||||||
|
// - don't have opportunity so set som other source of entropy.
|
||||||
|
|
||||||
|
ESP_LOGD(TAG, "getrandom(buf=0x%x, buflen=%d, flags=%u)", (int) buf, buflen, flags);
|
||||||
|
|
||||||
|
if (buf == NULL) {
|
||||||
|
errno = EFAULT;
|
||||||
|
ESP_LOGD(TAG, "getrandom returns -1 (EFAULT)");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *dst = (uint8_t *) buf;
|
||||||
|
ssize_t ret = 0;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user