Skip to content

Commit 31ff3ff

Browse files
iabdalkaderdpgeorge
authored andcommitted
renesas-ra: Add mbedTLS support.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 62e650f commit 31ff3ff

4 files changed

Lines changed: 129 additions & 0 deletions

File tree

ports/renesas-ra/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ SRC_C += $(addprefix $(BOARD_DIR)/ra_gen/,\
344344
vector_data.c \
345345
)
346346

347+
ifeq ($(MICROPY_SSL_MBEDTLS),1)
348+
LIB_SRC_C += mbedtls/mbedtls_port.c
349+
endif
350+
347351
ifeq ($(MICROPY_PY_BLUETOOTH),1)
348352
SRC_C += mpbthciport.c
349353
endif
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2023 Damien P. George
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+
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
27+
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
28+
29+
// Enable mbedtls modules.
30+
#define MBEDTLS_HAVE_TIME
31+
#define MBEDTLS_HAVE_TIME_DATE
32+
33+
// Time hook.
34+
#include <time.h>
35+
extern time_t ra_rtctime_seconds(time_t *timer);
36+
#define MBEDTLS_PLATFORM_TIME_MACRO ra_rtctime_seconds
37+
38+
// Set MicroPython-specific options.
39+
#define MICROPY_MBEDTLS_CONFIG_BARE_METAL (1)
40+
41+
// Include common mbedtls configuration.
42+
#include "extmod/mbedtls/mbedtls_config_common.h"
43+
44+
#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Damien P. George
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 "rng.h"
28+
#include "mbedtls_config.h"
29+
30+
#if defined(MBEDTLS_HAVE_TIME) || defined(MBEDTLS_HAVE_TIME_DATE)
31+
#include "rtc.h"
32+
#include "shared/timeutils/timeutils.h"
33+
#endif
34+
35+
int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) {
36+
uint32_t val = 0;
37+
int n = 0;
38+
*olen = len;
39+
while (len--) {
40+
if (!n) {
41+
val = rng_read();
42+
n = 4;
43+
}
44+
*output++ = val;
45+
val >>= 8;
46+
--n;
47+
}
48+
return 0;
49+
}
50+
51+
#if defined(MBEDTLS_HAVE_TIME)
52+
time_t ra_rtctime_seconds(time_t *timer) {
53+
rtc_init_finalise();
54+
RTC_TimeTypeDef time;
55+
RTC_DateTypeDef date;
56+
rtc_get_time(&time);
57+
rtc_get_date(&date);
58+
return timeutils_seconds_since_epoch(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
59+
}
60+
#endif
61+
62+
#if defined(MBEDTLS_HAVE_TIME_DATE)
63+
struct tm *gmtime(const time_t *timep) {
64+
static struct tm tm;
65+
timeutils_struct_time_t tm_buf = {0};
66+
timeutils_seconds_since_epoch_to_struct_time(*timep, &tm_buf);
67+
68+
tm.tm_sec = tm_buf.tm_sec;
69+
tm.tm_min = tm_buf.tm_min;
70+
tm.tm_hour = tm_buf.tm_hour;
71+
tm.tm_mday = tm_buf.tm_mday;
72+
tm.tm_mon = tm_buf.tm_mon - 1;
73+
tm.tm_year = tm_buf.tm_year - 1900;
74+
tm.tm_wday = tm_buf.tm_wday;
75+
tm.tm_yday = tm_buf.tm_yday;
76+
tm.tm_isdst = -1;
77+
78+
return &tm;
79+
}
80+
#endif

ports/renesas-ra/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
#endif
9090

9191
// Python internal features
92+
#define MICROPY_TRACKED_ALLOC (MICROPY_SSL_MBEDTLS || MICROPY_BLUETOOTH_BTSTACK)
9293
#define MICROPY_READER_VFS (1)
9394
#define MICROPY_ENABLE_GC (1)
9495
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)

0 commit comments

Comments
 (0)