renesas-ra: Add RNG driver.

It needs to be enabled explicitly by a board.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This commit is contained in:
iabdalkader 2023-06-22 10:22:00 +02:00 committed by Damien George
parent 5b774517dc
commit 142e8b70e2
4 changed files with 138 additions and 0 deletions

View File

@ -303,6 +303,7 @@ SRC_C += \
main.c \
ra_hal.c \
ra_it.c \
rng.c \
mphalport.c \
mpthreadport.c \
irq.c \
@ -429,6 +430,43 @@ HAL_SRC_C += $(addprefix ra/,\
endif
ifeq ($(MICROPY_HW_ENABLE_RNG),1)
CRYPTO_DIR = ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/inc
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/inc/api
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/private/inc
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/inc/instances
# The following FSP files are required for the random number generator.
HAL_SRC_C += $(addprefix $(HAL_DIR)/$(CRYPTO_DIR)/,\
public/r_sce.c \
public/r_sce_ecc.c \
public/r_sce_sha.c \
public/r_sce_aes.c \
private/r_sce_private.c \
primitive/r_sce_p00.c \
primitive/r_sce_p20.c \
primitive/r_sce_p26.c \
primitive/r_sce_p81.c \
primitive/r_sce_p82.c \
primitive/r_sce_p92.c \
primitive/r_sce_p40.c \
primitive/r_sce_func050.c \
primitive/r_sce_func051.c \
primitive/r_sce_func052.c \
primitive/r_sce_func053.c \
primitive/r_sce_func054.c \
primitive/r_sce_func100.c \
primitive/r_sce_func101.c \
primitive/r_sce_func040.c \
primitive/r_sce_func048.c \
primitive/r_sce_func102.c \
primitive/r_sce_func103.c \
primitive/r_sce_subprc01.c\
)
endif
OBJ += $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
OBJ += $(LIBM_O)

View File

@ -27,6 +27,7 @@
#include "py/runtime.h"
#include "uart.h"
#include "rng.h"
bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) {
const mp_obj_type_t *type = mp_obj_get_type(stream);
@ -42,3 +43,16 @@ void mp_os_dupterm_stream_detached_attached(mp_obj_t stream_detached, mp_obj_t s
uart_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true);
}
}
#if MICROPY_PY_OS_URANDOM
STATIC mp_obj_t mp_os_urandom(mp_obj_t num) {
mp_int_t n = mp_obj_get_int(num);
vstr_t vstr;
vstr_init_len(&vstr, n);
for (int i = 0; i < n; i++) {
vstr.buf[i] = rng_read();
}
return mp_obj_new_bytes_from_vstr(&vstr);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom);
#endif

52
ports/renesas-ra/rng.c Normal file
View File

@ -0,0 +1,52 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2023 Arduino SA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include <stdbool.h>
#include "py/runtime.h"
#include "py/mphal.h"
#if MICROPY_HW_ENABLE_RNG
#include "rng.h"
#include "r_sce.h"
uint32_t rng_read(void) {
uint32_t random_data[4];
static bool initialized = false;
if (initialized == false) {
initialized = true;
R_SCE_Open(&sce_ctrl, &sce_cfg);
}
R_SCE_RandomNumberGenerate(random_data);
return random_data[0];
}
#endif

34
ports/renesas-ra/rng.h Normal file
View File

@ -0,0 +1,34 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2023 Arduino SA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_RENESAS_RA_RNG_H
#define MICROPY_INCLUDED_RENESAS_RA_RNG_H
#include <stdint.h>
uint32_t rng_read(void);
#endif // MICROPY_INCLUDED_RENESAS_RA_RNG_H