Skip to content

Commit 182eaa0

Browse files
Frauschijulek-wolfssl
authored andcommitted
Zephyr: add support for RTC time
For ASN date validation, the actual wall clock time is needed from an RTC. This commit adds support to read the RTC time in case it is available in the Zephyr system. If the RTC is not available or an error occurs during the readout, we fallback to the old implementation which only supports relative time since boot. Signed-off-by: Tobias Frauenschläger <t.frauenschlaeger@me.com>
1 parent 4d8bbd7 commit 182eaa0

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

wolfcrypt/src/wc_port.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3164,6 +3164,30 @@ time_t z_time(time_t * timer)
31643164
{
31653165
struct timespec ts;
31663166

3167+
#if defined(CONFIG_RTC) && \
3168+
(defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC))
3169+
/* Try to obtain the actual time from an RTC */
3170+
static const struct device *rtc = DEVICE_DT_GET(DT_NODELABEL(rtc));
3171+
3172+
if (device_is_ready(rtc)) {
3173+
struct rtc_time rtc_time;
3174+
struct tm *tm_time = rtc_time_to_tm(&rtc_time);
3175+
3176+
int ret = rtc_get_time(rtc, &rtc_time);
3177+
3178+
if (ret == 0) {
3179+
time_t epochTime = mktime(tm_time);
3180+
3181+
if (timer != NULL)
3182+
*timer = epochTime;
3183+
3184+
return epochTime;
3185+
}
3186+
}
3187+
#endif
3188+
3189+
/* Fallback to uptime since boot. This works for relative times, but
3190+
* not for ASN.1 date validation */
31673191
if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
31683192
if (timer != NULL)
31693193
*timer = ts.tv_sec;

wolfssl/wolfcrypt/wc_port.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,14 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void);
985985
#include <time.h>
986986
#endif
987987

988+
#if defined(CONFIG_RTC)
989+
#if defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC)
990+
#include <zephyr/drivers/rtc.h>
991+
#else
992+
#warning "RTC support needs picolibc or newlib (nano)"
993+
#endif
994+
#endif
995+
988996
time_t z_time(time_t *timer);
989997

990998
#define XTIME(tl) z_time((tl))

0 commit comments

Comments
 (0)