Skip to content

Commit 142e8b7

Browse files
iabdalkaderdpgeorge
authored andcommitted
renesas-ra: Add RNG driver.
It needs to be enabled explicitly by a board. Signed-off-by: iabdalkader <[email protected]>
1 parent 5b77451 commit 142e8b7

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

ports/renesas-ra/Makefile

+38
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ SRC_C += \
303303
main.c \
304304
ra_hal.c \
305305
ra_it.c \
306+
rng.c \
306307
mphalport.c \
307308
mpthreadport.c \
308309
irq.c \
@@ -429,6 +430,43 @@ HAL_SRC_C += $(addprefix ra/,\
429430

430431
endif
431432

433+
ifeq ($(MICROPY_HW_ENABLE_RNG),1)
434+
CRYPTO_DIR = ra/fsp/src/r_sce_protected/crypto_procedures_protected/src/sce9/
435+
436+
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/inc
437+
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/inc/api
438+
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/private/inc
439+
INC += -I$(TOP)/$(HAL_DIR)/$(CRYPTO_DIR)/inc/instances
440+
441+
# The following FSP files are required for the random number generator.
442+
HAL_SRC_C += $(addprefix $(HAL_DIR)/$(CRYPTO_DIR)/,\
443+
public/r_sce.c \
444+
public/r_sce_ecc.c \
445+
public/r_sce_sha.c \
446+
public/r_sce_aes.c \
447+
private/r_sce_private.c \
448+
primitive/r_sce_p00.c \
449+
primitive/r_sce_p20.c \
450+
primitive/r_sce_p26.c \
451+
primitive/r_sce_p81.c \
452+
primitive/r_sce_p82.c \
453+
primitive/r_sce_p92.c \
454+
primitive/r_sce_p40.c \
455+
primitive/r_sce_func050.c \
456+
primitive/r_sce_func051.c \
457+
primitive/r_sce_func052.c \
458+
primitive/r_sce_func053.c \
459+
primitive/r_sce_func054.c \
460+
primitive/r_sce_func100.c \
461+
primitive/r_sce_func101.c \
462+
primitive/r_sce_func040.c \
463+
primitive/r_sce_func048.c \
464+
primitive/r_sce_func102.c \
465+
primitive/r_sce_func103.c \
466+
primitive/r_sce_subprc01.c\
467+
)
468+
endif
469+
432470
OBJ += $(PY_O)
433471
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
434472
OBJ += $(LIBM_O)

ports/renesas-ra/modos.c

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "py/runtime.h"
2929
#include "uart.h"
30+
#include "rng.h"
3031

3132
bool mp_os_dupterm_is_builtin_stream(mp_const_obj_t stream) {
3233
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
4243
uart_attach_to_repl(MP_OBJ_TO_PTR(stream_attached), true);
4344
}
4445
}
46+
47+
#if MICROPY_PY_OS_URANDOM
48+
STATIC mp_obj_t mp_os_urandom(mp_obj_t num) {
49+
mp_int_t n = mp_obj_get_int(num);
50+
vstr_t vstr;
51+
vstr_init_len(&vstr, n);
52+
for (int i = 0; i < n; i++) {
53+
vstr.buf[i] = rng_read();
54+
}
55+
return mp_obj_new_bytes_from_vstr(&vstr);
56+
}
57+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom);
58+
#endif

ports/renesas-ra/rng.c

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Arduino SA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdint.h>
28+
#include <stdbool.h>
29+
30+
#include "py/runtime.h"
31+
#include "py/mphal.h"
32+
33+
#if MICROPY_HW_ENABLE_RNG
34+
35+
#include "rng.h"
36+
#include "r_sce.h"
37+
38+
uint32_t rng_read(void) {
39+
uint32_t random_data[4];
40+
static bool initialized = false;
41+
42+
if (initialized == false) {
43+
initialized = true;
44+
R_SCE_Open(&sce_ctrl, &sce_cfg);
45+
}
46+
47+
R_SCE_RandomNumberGenerate(random_data);
48+
49+
return random_data[0];
50+
}
51+
52+
#endif

ports/renesas-ra/rng.h

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

0 commit comments

Comments
 (0)