Skip to content

Commit

Permalink
Update to esp-hal 0.18.0 (#30)
Browse files Browse the repository at this point in the history
* Update to esp-hal 0.18.0

* chore: Remove pub export of Rsa that is no longer needed

This revert the change made in #27 now that Session no longer take an instance of the driver.
  • Loading branch information
AnthonyGrondin committed Jul 15, 2024
1 parent b339fe8 commit c619fad
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 160 deletions.
18 changes: 9 additions & 9 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ xtask = "run --manifest-path ./xtask/Cargo.toml --"
# Alias' for quickly building for different chips or running examples
# By default we enable
# - `default` HAL features to set up basic chip specific settings
esp32 = "run --features esp32 --target xtensa-esp32-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
esp32s2 = "run --features esp32s2 --target xtensa-esp32s2-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
esp32s3 = "run --features esp32s3 --target xtensa-esp32s3-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
esp32c3 = "run --features esp32c3 --target riscv32imc-unknown-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"

besp32 = "build --features esp32 --target xtensa-esp32-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
besp32s2 = "build --features esp32s2 --target xtensa-esp32s2-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
besp32s3 = "build --features esp32s3 --target xtensa-esp32s3-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
besp32c3 = "build --features esp32c3 --target riscv32imc-unknown-none-elf --features esp-hal/default,esp-hal/embassy-time-timg0,esp-hal/embassy-executor-thread"
esp32 = "run --features esp32 --target xtensa-esp32-none-elf --features esp-hal/default"
esp32s2 = "run --features esp32s2 --target xtensa-esp32s2-none-elf --features esp-hal/default"
esp32s3 = "run --features esp32s3 --target xtensa-esp32s3-none-elf --features esp-hal/default"
esp32c3 = "run --features esp32c3 --target riscv32imc-unknown-none-elf --features esp-hal/default"

besp32 = "build --features esp32 --target xtensa-esp32-none-elf --features esp-hal/default"
besp32s2 = "build --features esp32s2 --target xtensa-esp32s2-none-elf --features esp-hal/default"
besp32s3 = "build --features esp32s3 --target xtensa-esp32s3-none-elf --features esp-hal/default"
besp32c3 = "build --features esp32c3 --target riscv32imc-unknown-none-elf --features esp-hal/default"
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
toolchain: nightly-2024-06-12
components: rust-src,rustfmt
- uses: esp-rs/xtensa-toolchain@v1.5
with:
Expand Down
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ lto = false
opt-level = 3

[dependencies]
esp-hal = { version = "0.16.1" }
esp-backtrace = { version = "0.11.0", features = [
esp-hal = { version = "0.18.0" }
esp-backtrace = { version = "0.12.1", features = [
"panic-handler",
"println",
"exception-handler",
] }
esp-println = { version = "0.9.0", features = ["log"] }
esp-hal-embassy = { version = "0.1.0", optional = true }

embassy-time = { version = "0.3.0", optional = true }
embassy-executor = { version = "0.5.0", package = "embassy-executor", features = [
"nightly",
"integrated-timers"
"integrated-timers",
], optional = true }
embassy-net = { version = "0.4.0", features = [
"tcp",
Expand All @@ -40,11 +41,12 @@ embassy-net = { version = "0.4.0", features = [
"medium-ethernet",
], optional = true }

esp-wifi = { git = "https://github.com/esp-rs/esp-wifi.git", features = [

esp-wifi = { version = "0.6.0", features = [
"phy-enable-usb",
"embedded-svc",
"wifi-default",
], version = "0.4.0" }
] }
smoltcp = { version = "0.11.0", default-features = false, features = [
"proto-igmp",
"proto-ipv4",
Expand Down Expand Up @@ -123,6 +125,6 @@ async = [
"embassy-time",
"dep:embedded-io-async",
"esp-mbedtls/async",
"esp-hal/embassy",
"esp-hal-embassy/time-timg0",
"esp-hal/async",
]
2 changes: 1 addition & 1 deletion cfg.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[esp-wifi]
heap_size = 112640 # use 110k by default
heap_size = 95000 # use 110k by default
# heap_size = 73728 # uncomment this to use 72k for esp32-s2/c2
2 changes: 1 addition & 1 deletion esp-mbedtls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ log = "0.4.17"
embedded-io = { version = "0.6.1" }
embedded-io-async = { version = "0.6.0", optional = true }
crypto-bigint = { version = "0.5.3", default-features = false, features = ["extra-sizes"] }
esp-hal = { version = "0.16.1" }
esp-hal = { version = "0.18.0" }
cfg-if = "1.0.0"

[features]
Expand Down
30 changes: 24 additions & 6 deletions esp-mbedtls/src/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ pub unsafe extern "C" fn mbedtls_mpi_exp_mod(
copy_bytes(Y.private_p, exponent.as_mut_ptr(), y_words);
copy_bytes(M.private_p, modulus.as_mut_ptr(), m_words);
copy_bytes(rinv.private_p, r.as_mut_ptr(), mpi_words(rinv));
let mut mod_exp = RsaModularExponentiation::<operand_sizes::Op256>::new(
let mut mod_exp = RsaModularExponentiation::<
operand_sizes::Op256,
esp_hal::Blocking,
>::new(
rsa,
&exponent, // exponent (Y) Y_MEM
&modulus, // modulus (M) M_MEM
Expand All @@ -214,7 +217,10 @@ pub unsafe extern "C" fn mbedtls_mpi_exp_mod(
copy_bytes(Y.private_p, exponent.as_mut_ptr(), y_words);
copy_bytes(M.private_p, modulus.as_mut_ptr(), m_words);
copy_bytes(rinv.private_p, r.as_mut_ptr(), mpi_words(rinv));
let mut mod_exp = RsaModularExponentiation::<operand_sizes::Op384>::new(
let mut mod_exp = RsaModularExponentiation::<
operand_sizes::Op384,
esp_hal::Blocking,
>::new(
rsa,
&exponent, // exponent (Y) Y_MEM
&modulus, // modulus (M) M_MEM
Expand All @@ -239,7 +245,10 @@ pub unsafe extern "C" fn mbedtls_mpi_exp_mod(
copy_bytes(Y.private_p, exponent.as_mut_ptr(), y_words);
copy_bytes(M.private_p, modulus.as_mut_ptr(), m_words);
copy_bytes(rinv.private_p, r.as_mut_ptr(), mpi_words(rinv));
let mut mod_exp = RsaModularExponentiation::<operand_sizes::Op512>::new(
let mut mod_exp = RsaModularExponentiation::<
operand_sizes::Op512,
esp_hal::Blocking,
>::new(
rsa,
&exponent, // exponent (Y) Y_MEM
&modulus, // modulus (M) M_MEM
Expand All @@ -264,7 +273,10 @@ pub unsafe extern "C" fn mbedtls_mpi_exp_mod(
copy_bytes(Y.private_p, exponent.as_mut_ptr(), y_words);
copy_bytes(M.private_p, modulus.as_mut_ptr(), m_words);
copy_bytes(rinv.private_p, r.as_mut_ptr(), mpi_words(rinv));
let mut mod_exp = RsaModularExponentiation::<operand_sizes::Op1024>::new(
let mut mod_exp = RsaModularExponentiation::<
operand_sizes::Op1024,
esp_hal::Blocking,
>::new(
rsa,
&exponent, // exponent (Y) Y_MEM
&modulus, // modulus (M) M_MEM
Expand All @@ -289,7 +301,10 @@ pub unsafe extern "C" fn mbedtls_mpi_exp_mod(
copy_bytes(Y.private_p, exponent.as_mut_ptr(), y_words);
copy_bytes(M.private_p, modulus.as_mut_ptr(), m_words);
copy_bytes(rinv.private_p, r.as_mut_ptr(), mpi_words(rinv));
let mut mod_exp = RsaModularExponentiation::<operand_sizes::Op2048>::new(
let mut mod_exp = RsaModularExponentiation::<
operand_sizes::Op2048,
esp_hal::Blocking,
>::new(
rsa,
&exponent, // exponent (Y) Y_MEM
&modulus, // modulus (M) M_MEM
Expand All @@ -315,7 +330,10 @@ pub unsafe extern "C" fn mbedtls_mpi_exp_mod(
copy_bytes(Y.private_p, exponent.as_mut_ptr(), y_words);
copy_bytes(M.private_p, modulus.as_mut_ptr(), m_words);
copy_bytes(rinv.private_p, r.as_mut_ptr(), mpi_words(rinv));
let mut mod_exp = RsaModularExponentiation::<operand_sizes::Op4096>::new(
let mut mod_exp = RsaModularExponentiation::<
operand_sizes::Op4096,
esp_hal::Blocking,
>::new(
rsa,
&exponent, // exponent (Y) Y_MEM
&modulus, // modulus (M) M_MEM
Expand Down
13 changes: 6 additions & 7 deletions esp-mbedtls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
use embedded_io::ErrorType;
#[doc(hidden)]
pub use esp_hal as hal;

pub use crate::hal::rsa::Rsa;
use hal::{peripheral::Peripheral, peripherals::RSA, rsa::Rsa};

mod compat;

Expand All @@ -30,7 +29,7 @@ pub use esp_mbedtls_sys::bindings::{
use esp_mbedtls_sys::c_types::*;

/// Hold the RSA peripheral for cryptographic operations.
static mut RSA_REF: Option<&mut Rsa> = None;
static mut RSA_REF: Option<Rsa<esp_hal::Blocking>> = None;

// these will come from esp-wifi (i.e. this can only be used together with esp-wifi)
extern "C" {
Expand Down Expand Up @@ -414,11 +413,11 @@ impl<T> Session<T> {
mode: Mode,
min_version: TlsVersion,
certificates: Certificates,
rsa: Option<&mut Rsa>,
rsa: Option<impl Peripheral<P = RSA>>,
) -> Result<Self, TlsError> {
let (ssl_context, ssl_config, crt, client_crt, private_key) =
certificates.init_ssl(servername, mode, min_version)?;
unsafe { RSA_REF = core::mem::transmute(rsa) }
unsafe { RSA_REF = core::mem::transmute(rsa.map(|inner| Rsa::new(inner, None))) }
return Ok(Self {
stream,
ssl_context,
Expand Down Expand Up @@ -640,11 +639,11 @@ pub mod asynch {
mode: Mode,
min_version: TlsVersion,
certificates: Certificates,
rsa: Option<&mut Rsa>,
rsa: Option<impl Peripheral<P = RSA>>,
) -> Result<Self, TlsError> {
let (ssl_context, ssl_config, crt, client_crt, private_key) =
certificates.init_ssl(servername, mode, min_version)?;
unsafe { RSA_REF = core::mem::transmute(rsa) }
unsafe { RSA_REF = core::mem::transmute(rsa.map(|inner| Rsa::new(inner, None))) }
return Ok(Self {
stream,
ssl_context,
Expand Down
27 changes: 13 additions & 14 deletions examples/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use esp_wifi::wifi::{
WifiState,
};
use esp_wifi::{initialize, EspWifiInitFor};
use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, rsa::Rsa, timer::TimerGroup};
use hal::{
clock::ClockControl, peripherals::Peripherals, prelude::*, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

const SSID: &str = env!("SSID");
Expand All @@ -35,18 +36,18 @@ async fn main(spawner: Spawner) -> ! {
init_logger(log::LevelFilter::Info);

let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::max(system.clock_control).freeze();

#[cfg(feature = "esp32c3")]
let timer = hal::systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0;
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
let timer = hal::timer::TimerGroup::new(peripherals.TIMG1, &clocks).timer0;
#[cfg(target_arch = "xtensa")]
let timer = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1, &clocks, None).timer0;
#[cfg(target_arch = "riscv32")]
let timer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0;
let init = initialize(
EspWifiInitFor::Wifi,
timer,
Rng::new(peripherals.RNG),
system.radio_clock_control,
peripherals.RADIO_CLK,
&clocks,
)
.unwrap();
Expand All @@ -55,8 +56,8 @@ async fn main(spawner: Spawner) -> ! {
let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap();

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0);
let timer_group0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
esp_hal_embassy::init(&clocks, timer_group0);

let config = Config::dhcpv4(Default::default());

Expand Down Expand Up @@ -106,8 +107,6 @@ async fn main(spawner: Spawner) -> ! {

set_debug(0);

let mut rsa = Rsa::new(peripherals.RSA);

let tls: Session<_, 4096> = Session::new(
&mut socket,
"www.google.com",
Expand All @@ -120,7 +119,7 @@ async fn main(spawner: Spawner) -> ! {
.ok(),
..Default::default()
},
Some(&mut rsa),
Some(peripherals.RSA),
)
.unwrap();

Expand Down
27 changes: 13 additions & 14 deletions examples/async_client_mTLS.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use esp_wifi::wifi::{
WifiState,
};
use esp_wifi::{initialize, EspWifiInitFor};
use hal::clock::ClockControl;
use hal::Rng;
use hal::{embassy, peripherals::Peripherals, prelude::*, rsa::Rsa, timer::TimerGroup};
use hal::{
clock::ClockControl, peripherals::Peripherals, prelude::*, rng::Rng, system::SystemControl,
timer::timg::TimerGroup,
};
use static_cell::make_static;

const SSID: &str = env!("SSID");
Expand All @@ -35,18 +36,18 @@ async fn main(spawner: Spawner) -> ! {
init_logger(log::LevelFilter::Info);

let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::max(system.clock_control).freeze();

#[cfg(feature = "esp32c3")]
let timer = hal::systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0;
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
let timer = hal::timer::TimerGroup::new(peripherals.TIMG1, &clocks).timer0;
#[cfg(target_arch = "xtensa")]
let timer = esp_hal::timer::timg::TimerGroup::new(peripherals.TIMG1, &clocks, None).timer0;
#[cfg(target_arch = "riscv32")]
let timer = esp_hal::timer::systimer::SystemTimer::new(peripherals.SYSTIMER).alarm0;
let init = initialize(
EspWifiInitFor::Wifi,
timer,
Rng::new(peripherals.RNG),
system.radio_clock_control,
peripherals.RADIO_CLK,
&clocks,
)
.unwrap();
Expand All @@ -55,8 +56,8 @@ async fn main(spawner: Spawner) -> ! {
let (wifi_interface, controller) =
esp_wifi::wifi::new_with_mode(&init, wifi, WifiStaDevice).unwrap();

let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0);
let timer_group0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
esp_hal_embassy::init(&clocks, timer_group0);

let config = Config::dhcpv4(Default::default());

Expand Down Expand Up @@ -106,8 +107,6 @@ async fn main(spawner: Spawner) -> ! {

set_debug(0);

let mut rsa = Rsa::new(peripherals.RSA);

let certificates = Certificates {
ca_chain: X509::pem(
concat!(include_str!("./certs/certauth.cryptomix.com.pem"), "\0").as_bytes(),
Expand All @@ -126,7 +125,7 @@ async fn main(spawner: Spawner) -> ! {
Mode::Client,
TlsVersion::Tls1_3,
certificates,
Some(&mut rsa),
Some(peripherals.RSA),
)
.unwrap();

Expand Down
Loading

0 comments on commit c619fad

Please sign in to comment.