Skip to content

Commit

Permalink
Update esp-hal, esp-wifi, esp-idf and edge-net dependencies (#63)
Browse files Browse the repository at this point in the history
* Update esp-hal, esp-wifi, esp-idf and edge-net dependencies

* Polyfill the missing memchr impl in latest esp-hal

* fmt; increase sockets count for esp32s3

* Switch to 2024-12-01 nightly

* fmt

* Something (esp-hal?) needs more heap now

* Fix a potential issue when writing; increase memory for the edge_server example

* Address code review feedback

* Much cleaner way to polyfill missing libc functions

* Properly calculate necessary sockets

* Remove unnecessary call to mk_static

* Address a small typo

* Fix sha calc for sha-384 and sha-224; restore regular logging lvl for the async_client_mTLS example
  • Loading branch information
ivmarkov authored Jan 9, 2025
1 parent 1431835 commit 9cce6c2
Show file tree
Hide file tree
Showing 20 changed files with 429 additions and 307 deletions.
4 changes: 0 additions & 4 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ runner = "espflash flash --monitor --baud 921600"
rustflags = [
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-nostartfiles",
"-C", "link-arg=-Trom_functions.x",
]

[target.riscv32imc-unknown-none-elf]
runner = "espflash flash --monitor --baud 921600"

rustflags = [
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-Trom_functions.x",

# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
# NOTE: May negatively impact performance of produced code
Expand All @@ -39,7 +37,6 @@ runner = "espflash flash --monitor --baud 921600"
rustflags = [
#"-C", "linker=rust-lld",
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-Trom_functions.x",
]

[target.xtensa-esp32s3-none-elf]
Expand All @@ -48,7 +45,6 @@ runner = "espflash flash --monitor --baud 921600"
rustflags = [
"-C", "link-arg=-Tlinkall.x",
"-C", "link-arg=-nostartfiles",
"-C", "link-arg=-Trom_functions.x",
]

[target.'cfg(target_os = "espidf")']
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- uses: dtolnay/rust-toolchain@v1
with:
target: riscv32imc-unknown-none-elf
toolchain: nightly-2024-07-22
toolchain: nightly-2024-12-01
components: rust-src,rustfmt
- uses: esp-rs/[email protected]
with:
Expand Down
55 changes: 27 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,40 @@ opt-level = "z"
opt-level = 3

[dependencies]
esp-hal = { version = "0.21.0", optional = true }
esp-hal = { version = "0.22.0", optional = true }
esp-backtrace = { version = "0.14.0", optional = true, features = [
"panic-handler",
"println",
"exception-handler",
] }
esp-println = { version = "0.12.0", optional = true, features = ["log"] }
esp-hal-embassy = { version = "0.4.0", optional = true }
esp-hal-embassy = { version = "0.5.0", optional = true }

embassy-time = { version = "0.3.0", optional = true }
embassy-executor = { version = "=0.6.0", package = "embassy-executor", features = [
embassy-executor = { version = "0.6", package = "embassy-executor", features = [
"nightly",
"integrated-timers",
], optional = true }
embassy-net = { version = "0.4.0", features = [
embassy-net = { version = "0.5.0", features = [
"tcp",
"udp",
"dhcpv4",
"medium-ethernet",
], optional = true }

esp-wifi = { version = "0.10.1", optional = true, features = ["phy-enable-usb", "wifi-default", "sys-logs"] }
smoltcp = { version = "0.11.0", optional = true, default-features = false, features = [
"proto-igmp",
esp-wifi = { version = "0.11.0", optional = true, features = ["sys-logs", "utils", "wifi"] }
blocking-network-stack = { git = "https://github.com/bjoernQ/blocking-network-stack", rev = "1c581661d78e0cf0f17b936297179b993fb149d7" }
smoltcp11 = { package = "smoltcp", version = "0.11.0", optional = true, default-features = false, features = [
"proto-ipv4",
"socket-tcp",
"socket-icmp",
"socket-udp",
"medium-ethernet",
"proto-dhcpv4",
"socket-raw",
"socket-dhcpv4",
] }
smoltcp = { version = "0.12.0", optional = true, default-features = false, features = [
"proto-ipv4",
"socket-tcp",
"socket-icmp",
Expand All @@ -66,15 +76,17 @@ static_cell = { version = "2.1", features = ["nightly"] }

esp-mbedtls = { path = "./esp-mbedtls" }

edge-http = { version = "0.3.0", optional = true }
edge-nal = { version = "0.3.0", optional = true }
edge-nal-embassy = { version = "0.3.0", optional = true }
edge-http = { version = "0.4.0", optional = true }
edge-nal = { version = "0.4.0", optional = true }
edge-nal-embassy = { version = "0.4.0", optional = true }
cfg-if = "1.0.0"
esp-alloc = { version = "0.5.0", optional = true}
enumset = { version = "1", default-features = false }

tinyrlibc = { version = "0.5", optional = true, default-features = false }

[target.'cfg(target_os = "espidf")'.dependencies]
esp-idf-svc = { version = "0.49.1", features = ["binstart"] }
esp-idf-svc = { version = "0.50", features = ["binstart"] }

[[example]]
name = "crypto_self_test"
Expand Down Expand Up @@ -104,8 +116,8 @@ name = "edge_server"
required-features = ["examples-async", "edge-http"]

[features]
examples = ["esp-hal", "esp-backtrace", "esp-println", "esp-wifi", "smoltcp", "esp-alloc"]
examples-async = ["examples", "esp-hal-embassy", "embassy-time", "embassy-executor", "embassy-net", "edge-http", "edge-nal", "edge-nal-embassy", "esp-wifi/async", "esp-wifi/embassy-net", "esp-mbedtls/async", "esp-mbedtls/edge-nal"]
examples = ["esp-hal", "esp-backtrace", "esp-println", "esp-wifi", "smoltcp", "smoltcp11", "esp-alloc"]
examples-async = ["examples", "esp-hal-embassy", "embassy-time", "embassy-executor", "embassy-net", "edge-http", "edge-nal", "edge-nal-embassy", "esp-mbedtls/async", "esp-mbedtls/edge-nal"]
examples-std = ["critical-section/std"]

esp32 = [
Expand All @@ -123,6 +135,7 @@ esp32c3 = [
"esp-println?/esp32c3",
"esp-wifi?/esp32c3",
"esp-mbedtls/esp32c3",
"tinyrlibc/memchr",
]
esp32s2 = [
"esp-hal?/esp32s2",
Expand All @@ -142,18 +155,4 @@ esp32s3 = [
]

[build-dependencies]
embuild = { version = "0.32", features = ["espidf"] }

# Patch until new release
[patch.crates-io]
edge-http = { git = "https://github.com/ivmarkov/edge-net" }
edge-nal = { git = "https://github.com/ivmarkov/edge-net" }
edge-nal-embassy = { git = "https://github.com/ivmarkov/edge-net" }
esp-idf-svc = { git = "https://github.com/esp-rs/esp-idf-svc" }
esp-idf-hal = { git = "https://github.com/esp-rs/esp-idf-hal" }
esp-idf-sys = { git = "https://github.com/esp-rs/esp-idf-sys" }

# Patch before 0.6.0 got yanked
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "886580179ff250e15b0fad6448e8ebed6cdabf2b" }
embassy-time-driver = { git = "https://github.com/embassy-rs/embassy", rev = "886580179ff250e15b0fad6448e8ebed6cdabf2b" }
embassy-time-queue-driver = { git = "https://github.com/embassy-rs/embassy", rev = "886580179ff250e15b0fad6448e8ebed6cdabf2b" }
embuild = { version = "0.33", features = ["espidf"] }
8 changes: 4 additions & 4 deletions esp-mbedtls-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ rust-version = "1.82"

[build-dependencies]
anyhow = "1.0.68"
bindgen = "0.68.1"
bindgen = "0.71.1"
env_logger = "0.10.0"
log = "0.4.17"
cmake = "0.1.52"
fs_extra = "1.3.0"
embuild = "0.32"
embuild = "0.33"

[dependencies]
# For malloc/free
# TODO: Replace with `esp-alloc` once `esp-alloc` starts to provide `malloc` and `free` in future
# ... or switch to our own `mbedtls_malloc/free`
esp-wifi = { version = "0.10.1", default-features = false, optional = true }
esp-wifi = { version = "0.11.0", default-features = false, optional = true }

# ESP-IDF: The mbedtls lib distributed with ESP-IDF is used
[target.'cfg(target_os = "espidf")'.dependencies]
esp-idf-sys = { version = "0.35", default-features = false }
esp-idf-sys = { version = "0.36", default-features = false }

# All other platforms: mbedtls libs and bindings are created on the fly

Expand Down
6 changes: 3 additions & 3 deletions esp-mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ log = { version = "0.4.17", default-features = false }
enumset = { version = "1", default-features = false }
embedded-io = { version = "0.6.1" }
embedded-io-async = { version = "0.6.0", optional = true }
esp-hal = { version = "0.21.0", optional = true }
esp-hal = { version = "0.22.0", optional = true }
# For malloc/free
# TODO: Replace with `esp-alloc` once `esp-alloc` starts to provide `malloc` and `free` in future
# ... or switch to our own `mbedtls_malloc/free`
esp-wifi = { version = "0.10.1", default-features = false, optional = true }
esp-wifi = { version = "0.11.0", default-features = false, optional = true }
cfg-if = "1.0.0"
edge-nal = { version = "0.3.0", optional = true }
edge-nal = { version = "0.4.0", optional = true }
critical-section = "1.1.3"
crypto-bigint = { version = "0.5.3", optional = true, default-features = false, features = ["extra-sizes"] }

Expand Down
29 changes: 27 additions & 2 deletions esp-mbedtls/src/edge_nal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ where
T: edge_nal::TcpAccept,
{
type Error = TlsError;
type Socket<'a> = Session<'a, T::Socket<'a>> where Self: 'a;
type Socket<'a>
= Session<'a, T::Socket<'a>>
where
Self: 'a;

async fn accept(
&self,
Expand Down Expand Up @@ -115,7 +118,10 @@ where
{
type Error = TlsError;

type Socket<'a> = Session<'a, T::Socket<'a>> where Self: 'a;
type Socket<'a>
= Session<'a, T::Socket<'a>>
where
Self: 'a;

async fn connect(&self, remote: SocketAddr) -> Result<Self::Socket<'_>, Self::Error> {
let socket = self
Expand Down Expand Up @@ -151,6 +157,25 @@ where
}
}

impl<T> edge_nal::TcpSplit for Session<'_, T>
where
T: edge_nal::TcpSplit + embedded_io_async::Read + embedded_io_async::Write + edge_nal::Readable,
{
type Read<'a>
= Self
where
Self: 'a;

type Write<'a>
= Self
where
Self: 'a;

fn split(&mut self) -> (Self::Read<'_>, Self::Write<'_>) {
panic!("Splitting a TLS session is not supported yet");
}
}

impl<T> edge_nal::TcpShutdown for Session<'_, T>
where
T: embedded_io_async::Read + embedded_io_async::Write + edge_nal::TcpShutdown,
Expand Down
3 changes: 2 additions & 1 deletion esp-mbedtls/src/esp_hal/sha/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ pub unsafe extern "C" fn mbedtls_sha256_finish(
);
nb::block!(hasher.finish(&mut data)).unwrap();
nb::block!(hasher.save((*ctx).sha224_hasher.as_mut().unwrap())).unwrap();
core::ptr::copy_nonoverlapping(data.as_ptr(), output, 28);
} else {
let mut hasher = ShaDigest::restore(
sha.as_mut().unwrap(),
(*ctx).sha256_hasher.as_mut().unwrap(),
);
nb::block!(hasher.finish(&mut data)).unwrap();
nb::block!(hasher.save((*ctx).sha256_hasher.as_mut().unwrap())).unwrap();
core::ptr::copy_nonoverlapping(data.as_ptr(), output, 32);
}
});
core::ptr::copy_nonoverlapping(data.as_ptr(), output, data.len());
0
}
3 changes: 2 additions & 1 deletion esp-mbedtls/src/esp_hal/sha/sha512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ pub unsafe extern "C" fn mbedtls_sha512_finish(
);
nb::block!(hasher.finish(&mut data)).unwrap();
nb::block!(hasher.save((*ctx).sha384_hasher.as_mut().unwrap())).unwrap();
core::ptr::copy_nonoverlapping(data.as_ptr(), output, 48);
} else {
let mut hasher = ShaDigest::restore(
sha.as_mut().unwrap(),
(*ctx).sha512_hasher.as_mut().unwrap(),
);
nb::block!(hasher.finish(&mut data)).unwrap();
nb::block!(hasher.save((*ctx).sha512_hasher.as_mut().unwrap())).unwrap();
core::ptr::copy_nonoverlapping(data.as_ptr(), output, 64);
}
});
core::ptr::copy_nonoverlapping(data.as_ptr(), output, data.len());
0
}
30 changes: 25 additions & 5 deletions esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1176,16 +1176,26 @@ pub mod asynch {
F: FnMut(*mut mbedtls_ssl_context) -> i32,
{
loop {
// If there is an outstanding byte, we really need to push it down the wire first
// before we call `poll_fn` below. Otherwise, the bytes generated by `poll_fn` and emplaced
// in the socket write buffer would be send _before_ our outstanding byte
//
// It is another topic that this usually should not happen, as we are calling `flush_write` also
// _after_ the `poll_fn` call, but in the rare case where the user cancels this function (drops the future)
// and then re-tries the call, the outstanding byte will not be written so it needs to be re-tried here.
self.flush_write().await?;

let outcome =
core::future::poll_fn(|cx| PollCtx::poll(self, cx, |ssl| f(ssl))).await?;

self.flush_write().await?;

match outcome {
PollOutcome::Success(res) => break Ok(res),
PollOutcome::Success(res) => {
self.flush_write().await?;
break Ok(res);
}
PollOutcome::Retry => continue,
PollOutcome::WantRead => self.wait_read().await?,
PollOutcome::WantWrite => self.flush_write().await?,
PollOutcome::WantWrite => continue,
PollOutcome::Eof => {
self.state = SessionState::Eof;
break Ok(0);
Expand All @@ -1209,6 +1219,11 @@ pub mod asynch {
.map_err(|e| TlsError::Io(e.kind()))?;
if len > 0 {
self.read_byte = Some(buf[0]);
} else {
// len = 0 means the other party abruptly closed the socket
// For now, return an error as this is not the nice `MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY`
// case, but re-consider this in future
Err(TlsError::Eof)?;
}
}

Expand All @@ -1230,6 +1245,11 @@ pub mod asynch {
.map_err(|e| TlsError::Io(e.kind()))?;
if len > 0 {
self.write_byte.take();
} else {
// len = 0 means the other party abruptly closed the socket
// For now, return an error as this is not the nice `MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY`
// case, but re-consider this in future
Err(TlsError::Eof)?;
}
}

Expand Down Expand Up @@ -1395,7 +1415,7 @@ pub mod asynch {
::log::debug!("Send {}B", buf.len());

if buf.is_empty() {
// MbedTLS does not want us to read anything
// MbedTLS does not want us to write anything
return 0;
}

Expand Down
Loading

0 comments on commit 9cce6c2

Please sign in to comment.