Skip to content

Commit 89fba5d

Browse files
committed
wip: provide time support to mbedtls
1 parent 4329f17 commit 89fba5d

File tree

35 files changed

+1041
-40
lines changed

35 files changed

+1041
-40
lines changed

examples/esp/Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ opt-level = "z"
3434

3535
[features]
3636
default = ["esp32c6"]
37-
esp32 = ["esp-rtos/esp32", "esp-hal/esp32", "esp-backtrace/esp32", "esp-println/esp32", "esp-radio/esp32", "esp-bootloader-esp-idf/esp32", "esp-metadata-generated/esp32", "mbedtls-rs/accel-esp32"]
38-
esp32c2 = ["esp-rtos/esp32c2", "esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-println/esp32c2", "esp-radio/esp32c2", "esp-bootloader-esp-idf/esp32c2", "esp-metadata-generated/esp32c2", "mbedtls-rs/accel-esp32c2"]
39-
esp32c3 = ["esp-rtos/esp32c3", "esp-hal/esp32c3", "esp-backtrace/esp32c3", "esp-println/esp32c3", "esp-radio/esp32c3", "esp-bootloader-esp-idf/esp32c3", "esp-metadata-generated/esp32c3", "mbedtls-rs/accel-esp32c3"]
40-
esp32c6 = ["esp-rtos/esp32c6", "esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-println/esp32c6", "esp-radio/esp32c6", "esp-bootloader-esp-idf/esp32c6", "esp-metadata-generated/esp32c6", "mbedtls-rs/accel-esp32c6"]
41-
esp32h2 = ["esp-rtos/esp32h2", "esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-println/esp32h2", "esp-radio/esp32h2", "esp-bootloader-esp-idf/esp32h2", "esp-metadata-generated/esp32h2", "mbedtls-rs/accel-esp32h2"]
42-
esp32s2 = ["esp-rtos/esp32s2", "esp-hal/esp32s2", "esp-backtrace/esp32s2", "esp-println/esp32s2", "esp-radio/esp32s2", "esp-bootloader-esp-idf/esp32s2", "esp-metadata-generated/esp32s2", "mbedtls-rs/accel-esp32s2"]
43-
esp32s3 = ["esp-rtos/esp32s3", "esp-hal/esp32s3", "esp-backtrace/esp32s3", "esp-println/esp32s3", "esp-radio/esp32s3", "esp-bootloader-esp-idf/esp32s3", "esp-metadata-generated/esp32s3", "mbedtls-rs/accel-esp32s3"]
37+
esp32 = ["esp-rtos/esp32", "esp-hal/esp32", "esp-backtrace/esp32", "esp-println/esp32", "esp-radio/esp32", "esp-bootloader-esp-idf/esp32", "esp-metadata-generated/esp32", "mbedtls-rs/accel-esp32", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32"]
38+
esp32c2 = ["esp-rtos/esp32c2", "esp-hal/esp32c2", "esp-backtrace/esp32c2", "esp-println/esp32c2", "esp-radio/esp32c2", "esp-bootloader-esp-idf/esp32c2", "esp-metadata-generated/esp32c2", "mbedtls-rs/accel-esp32c2", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32c2"]
39+
esp32c3 = ["esp-rtos/esp32c3", "esp-hal/esp32c3", "esp-backtrace/esp32c3", "esp-println/esp32c3", "esp-radio/esp32c3", "esp-bootloader-esp-idf/esp32c3", "esp-metadata-generated/esp32c3", "mbedtls-rs/accel-esp32c3", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32c3"]
40+
esp32c6 = ["esp-rtos/esp32c6", "esp-hal/esp32c6", "esp-backtrace/esp32c6", "esp-println/esp32c6", "esp-radio/esp32c6", "esp-bootloader-esp-idf/esp32c6", "esp-metadata-generated/esp32c6", "mbedtls-rs/accel-esp32c6", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32c6"]
41+
esp32h2 = ["esp-rtos/esp32h2", "esp-hal/esp32h2", "esp-backtrace/esp32h2", "esp-println/esp32h2", "esp-radio/esp32h2", "esp-bootloader-esp-idf/esp32h2", "esp-metadata-generated/esp32h2", "mbedtls-rs/accel-esp32h2", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32h2"]
42+
esp32s2 = ["esp-rtos/esp32s2", "esp-hal/esp32s2", "esp-backtrace/esp32s2", "esp-println/esp32s2", "esp-radio/esp32s2", "esp-bootloader-esp-idf/esp32s2", "esp-metadata-generated/esp32s2", "mbedtls-rs/accel-esp32s2", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32s2"]
43+
esp32s3 = ["esp-rtos/esp32s3", "esp-hal/esp32s3", "esp-backtrace/esp32s3", "esp-println/esp32s3", "esp-radio/esp32s3", "esp-bootloader-esp-idf/esp32s3", "esp-metadata-generated/esp32s3", "mbedtls-rs/accel-esp32s3", "mbedtls-rs/timer-embassy", "mbedtls-rs/wall-clock-esp32s3"]
4444

4545
[dependencies]
4646
log = "0.4"

examples/esp/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::time::{SystemTime, UNIX_EPOCH};
2+
3+
fn main() {
4+
let current_time_ms = SystemTime::now()
5+
.duration_since(UNIX_EPOCH)
6+
.expect("Time went backwards")
7+
.as_millis();
8+
9+
println!("cargo:rustc-env=CURRENT_TIME_MS={}", current_time_ms);
10+
}

examples/esp/src/bootstrap.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use esp_hal::timer::timg::TimerGroup;
1818

1919
use mbedtls_rs::sys::accel::esp::EspAccel;
2020
use mbedtls_rs::Tls;
21+
use mbedtls_rs::sys::clock::esp::EspRtcWallClock;
22+
use mbedtls_rs::sys::timer::embassy::EmbassyTimer;
2123

2224
use esp_metadata_generated::memory_range;
2325

@@ -51,6 +53,7 @@ esp_bootloader_esp_idf::esp_app_desc!();
5153

5254
const WIFI_SSID: &str = env!("WIFI_SSID");
5355
const WIFI_PASS: &str = env!("WIFI_PASS");
56+
const CURRENT_TIME_MS: &str = env!("CURRENT_TIME_MS");
5457

5558
pub async fn bootstrap_stack<const SOCKETS: usize>(
5659
spawner: Spawner,
@@ -72,6 +75,35 @@ pub async fn bootstrap_stack<const SOCKETS: usize>(
7275
.software_interrupt0,
7376
);
7477

78+
// TODO feature gate?
79+
{
80+
let timer = mk_static!(EmbassyTimer, EmbassyTimer::default());
81+
unsafe {
82+
mbedtls_rs::sys::hook::timer::hook_timer(Some(timer));
83+
}
84+
}
85+
86+
// TODO feature gate?
87+
{
88+
let rtc = mk_static!(
89+
esp_hal::rtc_cntl::Rtc,
90+
esp_hal::rtc_cntl::Rtc::new(peripherals.LPWR)
91+
);
92+
93+
// In a real-life scenario NTP or equivalent should be used here to initialize the RTC
94+
rtc.set_current_time_us(
95+
CURRENT_TIME_MS
96+
.parse::<u64>()
97+
.expect("Failed to parse CURRENT_TIME_MS")
98+
* 1000, // Convert milliseconds to microseconds
99+
);
100+
101+
let clock = mk_static!(EspRtcWallClock, EspRtcWallClock::new(rtc));
102+
unsafe {
103+
mbedtls_rs::sys::hook::wall_clock::hook_wall_clock(Some(clock));
104+
}
105+
}
106+
75107
#[cfg(not(any(feature = "esp32", feature = "esp32c2")))]
76108
let accel = EspAccel::new(peripherals.SHA, peripherals.RSA);
77109

mbedtls-rs-sys/Cargo.toml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ use-gcc = []
2020
# https://github.com/riscv-collab/riscv-gnu-toolchain
2121
force-esp-riscv-gcc = ["use-gcc"]
2222
# Disable all or some hooks; this would preclude hooking custom HW-accelerated algos
23-
nohook = ["nohook-sha1", "nohook-sha256", "nohook-sha512", "nohook-exp-mod"]
23+
nohook = ["nohook-sha1", "nohook-sha256", "nohook-sha512", "nohook-exp-mod", "nohook-timer", "nohook-wall-clock"]
2424
nohook-sha1 = []
2525
nohook-sha256 = []
2626
nohook-sha512 = []
2727
nohook-exp-mod = []
28+
nohook-timer = []
29+
nohook-wall-clock = []
2830
# Enable HW acceleration drivers for the ESP series of MCUs via `esp-hal`
2931
accel-esp32 = ["esp-hal/esp32", "crypto-bigint"]
3032
accel-esp32c2 = ["esp-hal/esp32c2"]
@@ -34,6 +36,15 @@ accel-esp32h2 = ["esp-hal/esp32h2", "crypto-bigint"]
3436
accel-esp32s2 = ["esp-hal/esp32s2", "crypto-bigint"]
3537
accel-esp32s3 = ["esp-hal/esp32s3", "crypto-bigint"]
3638

39+
timer-embassy = ["embassy-time"]
40+
wall-clock-esp32 = ["esp-hal/esp32", "time"]
41+
wall-clock-esp32c2 = ["esp-hal/esp32c2", "time"]
42+
wall-clock-esp32c3 = ["esp-hal/esp32c3", "time"]
43+
wall-clock-esp32c6 = ["esp-hal/esp32c6", "time"]
44+
wall-clock-esp32h2 = ["esp-hal/esp32h2", "time"]
45+
wall-clock-esp32s2 = ["esp-hal/esp32s2", "time"]
46+
wall-clock-esp32s3 = ["esp-hal/esp32s3", "time"]
47+
3748
[dependencies]
3849
defmt = { version = "1", optional = true }
3950
log = { version = "0.4", optional = true }
@@ -43,12 +54,16 @@ digest = { version = "0.10", default-features = false }
4354
sha1 = { version = "0.10", default-features = false }
4455
# For some reason, `soft` results in enormous slowdowns on xtensa and riscv32
4556
sha2 = { version = "0.10", default-features = false, features = ["force-soft-compact"] }
57+
time = { version = "0.3", default-features = false, optional = true }
4658
critical-section = "1"
4759

4860
# For esp-hal accel
4961
esp-hal = { version = "1", features = ["unstable"], optional = true }
5062
crypto-bigint = { version = "0.5.3", default-features = false, features = ["extra-sizes"], optional = true }
5163

64+
# For embassy timer support
65+
embassy-time = {version = "0.5.0", optional = true}
66+
5267
# ESP-IDF: The mbedtls lib distributed with ESP-IDF is used
5368
# All other platforms: mbedtls libs and bindings are created on the fly
5469
[target.'cfg(target_os = "espidf")'.dependencies]

mbedtls-rs-sys/gen/builder.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ pub enum Hook {
1717
Sha512,
1818
/// MPI modular exponentiation
1919
ExpMod,
20+
/// Timer support
21+
Timer,
22+
/// Wall clock support
23+
WallClock,
2024
}
2125

2226
/// The MbedTLS builder
@@ -159,12 +163,14 @@ impl MbedtlsBuilder {
159163
}
160164

161165
for hook in self.hooks {
162-
let def = self.hook_def(hook);
166+
let defs = self.hook_defs(hook);
163167

164-
builder = builder.clang_arg(format!("-D{def}"));
168+
for def in defs {
169+
builder = builder.clang_arg(format!("-D{def}"));
165170

166-
if let Some(size_def) = self.hook_work_area_size_def(hook) {
167-
builder = builder.clang_arg(format!("-D{def}_WORK_AREA_SIZE={size_def}"));
171+
if let Some(size_def) = self.hook_work_area_size_def(hook) {
172+
builder = builder.clang_arg(format!("-D{def}_WORK_AREA_SIZE={size_def}"));
173+
}
168174
}
169175
}
170176

@@ -237,14 +243,16 @@ impl MbedtlsBuilder {
237243
.out_dir(&target_dir);
238244

239245
for hook in self.hooks {
240-
let def = self.hook_def(hook);
246+
let defs = self.hook_defs(hook);
241247

242-
config.cflag(format!("-D{def}")).cxxflag(format!("-D{def}"));
248+
for def in defs {
249+
config.cflag(format!("-D{def}")).cxxflag(format!("-D{def}"));
243250

244-
if let Some(size_def) = self.hook_work_area_size_def(hook) {
245-
config
246-
.cflag(format!("-D{def}_WORK_AREA_SIZE={size_def}"))
247-
.cxxflag(format!("-D{def}_WORK_AREA_SIZE={size_def}"));
251+
if let Some(size_def) = self.hook_work_area_size_def(hook) {
252+
config
253+
.cflag(format!("-D{def}_WORK_AREA_SIZE={size_def}"))
254+
.cxxflag(format!("-D{def}_WORK_AREA_SIZE={size_def}"));
255+
}
248256
}
249257
}
250258

@@ -259,12 +267,18 @@ impl MbedtlsBuilder {
259267
println!("cargo:rerun-if-changed={}", file_or_dir.display())
260268
}
261269

262-
fn hook_def(&self, hook: Hook) -> &'static str {
270+
fn hook_defs(&self, hook: Hook) -> &'static [&'static str] {
263271
match hook {
264-
Hook::Sha1 => "MBEDTLS_SHA1_ALT",
265-
Hook::Sha256 => "MBEDTLS_SHA256_ALT",
266-
Hook::Sha512 => "MBEDTLS_SHA512_ALT",
267-
Hook::ExpMod => "MBEDTLS_MPI_EXP_MOD_ALT_FALLBACK",
272+
Hook::Sha1 => &["MBEDTLS_SHA1_ALT"],
273+
Hook::Sha256 => &["MBEDTLS_SHA256_ALT"],
274+
Hook::Sha512 => &["MBEDTLS_SHA512_ALT"],
275+
Hook::ExpMod => &["MBEDTLS_MPI_EXP_MOD_ALT_FALLBACK"],
276+
Hook::Timer => &[
277+
"MBEDTLS_HAVE_TIME",
278+
"MBEDTLS_PLATFORM_TIME_ALT",
279+
"MBEDTLS_PLATFORM_MS_TIME_ALT",
280+
],
281+
Hook::WallClock => &["MBEDTLS_HAVE_TIME_DATE", "MBEDTLS_PLATFORM_GMTIME_R_ALT"],
268282
}
269283
}
270284

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef __TIME_H__
2+
#define __TIME_H__
3+
4+
#include <stdint.h>
5+
6+
typedef int64_t time_t;
7+
8+
struct tm {
9+
int tm_sec; // seconds after the minute - [0, 60]
10+
int tm_min; // minutes after the hour - [0, 59]
11+
int tm_hour; // hours since midnight - [0, 23]
12+
int tm_mday; // day of the month - [1, 31]
13+
int tm_mon; // months since January - [0, 11]
14+
int tm_year; // years since 1900
15+
int tm_wday; // days since Sunday - [0, 6]
16+
int tm_yday; // days since January 1 - [0, 365]
17+
int tm_isdst; // Daylight Saving Time flag
18+
};
19+
20+
time_t time(time_t* timer);
21+
22+
#endif
724 Bytes
Binary file not shown.
5.62 KB
Binary file not shown.
1.49 KB
Binary file not shown.
724 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)