From 142e8b70e28bd1a12a40b13cdbb9972dd73578a3 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 22 Jun 2023 10:22:00 +0200 Subject: [PATCH] renesas-ra: Add RNG driver. It needs to be enabled explicitly by a board. Signed-off-by: iabdalkader --- ports/renesas-ra/Makefile | 38 ++++++++++++++++++++++++++++ ports/renesas-ra/modos.c | 14 +++++++++++ ports/renesas-ra/rng.c | 52 +++++++++++++++++++++++++++++++++++++++ ports/renesas-ra/rng.h | 34 +++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 ports/renesas-ra/rng.c create mode 100644 ports/renesas-ra/rng.h diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile index 9accc760b5..50a7100538 100644 --- a/ports/renesas-ra/Makefile +++ b/ports/renesas-ra/Makefile @@ -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) diff --git a/ports/renesas-ra/modos.c b/ports/renesas-ra/modos.c index e6aadd9ebc..0884a0b2c0 100644 --- a/ports/renesas-ra/modos.c +++ b/ports/renesas-ra/modos.c @@ -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 diff --git a/ports/renesas-ra/rng.c b/ports/renesas-ra/rng.c new file mode 100644 index 0000000000..4efee2d21c --- /dev/null +++ b/ports/renesas-ra/rng.c @@ -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 +#include + +#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 diff --git a/ports/renesas-ra/rng.h b/ports/renesas-ra/rng.h new file mode 100644 index 0000000000..ad70a454ff --- /dev/null +++ b/ports/renesas-ra/rng.h @@ -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 + +uint32_t rng_read(void); + +#endif // MICROPY_INCLUDED_RENESAS_RA_RNG_H