Skip to content

Commit 3236df0

Browse files
committed
Fix the build artefacts for RiscV; implement alignment checker
1 parent 3186616 commit 3236df0

File tree

9 files changed

+23
-39
lines changed

9 files changed

+23
-39
lines changed

.cargo/config.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
# Build on target firmware with ESP IDF
55
#target = "xtensa-esp32s3-espidf"
66
# Build on target firmware with baremetal
7-
target = "xtensa-esp32s3-none-elf"
7+
#target = "xtensa-esp32s3-none-elf"
88
#target = "xtensa-esp32-none-elf"
9+
target = "riscv32imc-unknown-none-elf"
910

1011
[target.xtensa-esp32-none-elf]
1112
runner = "espflash flash --monitor --baud 921600"

esp-mbedtls-sys/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() -> Result<()> {
2929
let dirs = if esp32 {
3030
Some((bindings_dir.join("esp32.rs"), libs_dir.join("xtensa-esp32-none-elf")))
3131
} else if esp32c3 {
32-
Some((bindings_dir.join("esp32c3.rs"), libs_dir.join("xtensa-esp32c3-none-elf")))
32+
Some((bindings_dir.join("esp32c3.rs"), libs_dir.join("riscv32imc-unknown-none-elf")))
3333
} else if esp32s2 {
3434
Some((bindings_dir.join("esp32s2.rs"), libs_dir.join("xtensa-esp32s2-none-elf")))
3535
} else if esp32s3 {
Binary file not shown.
Binary file not shown.

esp-mbedtls-sys/src/include/esp32s3.rs

-25
Original file line numberDiff line numberDiff line change
@@ -19748,8 +19748,6 @@ pub struct mbedtls_ssl_config {
1974819748
pub private_encrypt_then_mac: u8,
1974919749
///< negotiate extended master secret?
1975019750
pub private_extended_ms: u8,
19751-
///< detect and prevent replay?
19752-
pub private_anti_replay: u8,
1975319751
///< disable renegotiation?
1975419752
pub private_disable_renegotiation: u8,
1975519753
///< use session tickets?
@@ -20065,10 +20063,6 @@ pub struct mbedtls_ssl_context {
2006520063
///< offset of the next record in datagram
2006620064
///(equal to in_left if none)
2006720065
pub private_next_record_offset: usize,
20068-
///< last validated record seq_num
20069-
pub private_in_window_top: u64,
20070-
///< bitmask for replay detection
20071-
pub private_in_window: u64,
2007220066
///< current handshake message length,
2007320067
///including the handshake header
2007420068
pub private_in_hslen: usize,
@@ -20924,25 +20918,6 @@ extern "C" {
2092420918
ilen: usize,
2092520919
) -> crate::c_types::c_int;
2092620920
}
20927-
extern "C" {
20928-
/// \brief Enable or disable anti-replay protection for DTLS.
20929-
/// (DTLS only, no effect on TLS.)
20930-
/// Default: enabled.
20931-
///
20932-
/// \param conf SSL configuration
20933-
/// \param mode MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
20934-
///
20935-
/// \warning Disabling this is a security risk unless the application
20936-
/// protocol handles duplicated packets in a safe way. You
20937-
/// should not disable this without careful consideration.
20938-
/// However, if your application already detects duplicated
20939-
/// packets and needs information about them to adjust its
20940-
/// transmission strategy, then you'll want to disable this.
20941-
pub fn mbedtls_ssl_conf_dtls_anti_replay(
20942-
conf: *mut mbedtls_ssl_config,
20943-
mode: crate::c_types::c_char,
20944-
);
20945-
}
2094620921
extern "C" {
2094720922
/// \brief Set a limit on the number of records with a bad MAC
2094820923
/// before terminating the connection.

esp-mbedtls/src/esp_hal/sha/sha1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct mbedtls_sha1_context {
1515
#[no_mangle]
1616
pub unsafe extern "C" fn mbedtls_sha1_init(ctx: *mut mbedtls_sha1_context) {
1717
let hasher_mem =
18-
crate::calloc(1, core::mem::size_of::<Context<Sha1>>()) as *mut Context<Sha1>;
18+
crate::aligned_calloc(core::mem::align_of::<Context<Sha1>>(), core::mem::size_of::<Context<Sha1>>()) as *mut Context<Sha1>;
1919
core::ptr::write(hasher_mem, Context::<Sha1>::new());
2020
(*ctx).hasher = hasher_mem;
2121
}

esp-mbedtls/src/esp_hal/sha/sha256.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub struct mbedtls_sha256_context {
1717
#[no_mangle]
1818
pub unsafe extern "C" fn mbedtls_sha256_init(ctx: *mut mbedtls_sha256_context) {
1919
let sha224_mem =
20-
crate::calloc(1, core::mem::size_of::<Context<Sha224>>()) as *mut Context<Sha224>;
20+
crate::aligned_calloc(core::mem::align_of::<Context<Sha224>>(), core::mem::size_of::<Context<Sha224>>()) as *mut Context<Sha224>;
2121
let sha256_mem =
22-
crate::calloc(1, core::mem::size_of::<Context<Sha256>>()) as *mut Context<Sha256>;
22+
crate::aligned_calloc(core::mem::align_of::<Context<Sha256>>(), core::mem::size_of::<Context<Sha256>>()) as *mut Context<Sha256>;
2323
(*ctx).sha224_hasher = sha224_mem;
2424
(*ctx).sha256_hasher = sha256_mem;
2525
}

esp-mbedtls/src/esp_hal/sha/sha512.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub struct mbedtls_sha512_context {
1717
#[no_mangle]
1818
pub unsafe extern "C" fn mbedtls_sha512_init(ctx: *mut mbedtls_sha512_context) {
1919
let sha384_mem =
20-
crate::calloc(1, core::mem::size_of::<Context<Sha384>>()) as *mut Context<Sha384>;
20+
crate::aligned_calloc(core::mem::align_of::<Context<Sha384>>(), core::mem::size_of::<Context<Sha384>>()) as *mut Context<Sha384>;
2121
let sha512_mem =
22-
crate::calloc(1, core::mem::size_of::<Context<Sha512>>()) as *mut Context<Sha512>;
22+
crate::aligned_calloc(core::mem::align_of::<Context<Sha512>>(), core::mem::size_of::<Context<Sha512>>()) as *mut Context<Sha512>;
2323
(*ctx).sha384_hasher = sha384_mem;
2424
(*ctx).sha512_hasher = sha512_mem;
2525
}

esp-mbedtls/src/lib.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ pub mod io {
3232
pub use embedded_io::*;
3333
}
3434

35+
unsafe fn aligned_calloc(align: usize, size: usize) -> *const c_void {
36+
// if align > 4 {
37+
// panic!("Cannot allocate with alignment > 4 bytes: {align}");
38+
// }
39+
40+
calloc(1, size)
41+
}
42+
3543
// Baremetal: these will come from `esp-wifi` (i.e. this can only be used together with esp-wifi)
3644
// STD: these will come from `libc` indirectly via the Rust standard library
3745
extern "C" {
@@ -323,28 +331,28 @@ impl<'a> Certificates<'a> {
323331
unsafe {
324332
error_checked!(psa_crypto_init())?;
325333

326-
let drbg_context = calloc(1, size_of::<mbedtls_ctr_drbg_context>())
334+
let drbg_context = aligned_calloc(align_of::<mbedtls_ctr_drbg_context>(), size_of::<mbedtls_ctr_drbg_context>())
327335
as *mut mbedtls_ctr_drbg_context;
328336
if drbg_context.is_null() {
329337
return Err(TlsError::OutOfMemory);
330338
}
331339

332340
let ssl_context =
333-
calloc(1, size_of::<mbedtls_ssl_context>()) as *mut mbedtls_ssl_context;
341+
aligned_calloc(align_of::<mbedtls_ssl_context>(), size_of::<mbedtls_ssl_context>()) as *mut mbedtls_ssl_context;
334342
if ssl_context.is_null() {
335343
free(drbg_context as *const _);
336344
return Err(TlsError::OutOfMemory);
337345
}
338346

339347
let ssl_config =
340-
calloc(1, size_of::<mbedtls_ssl_config>()) as *mut mbedtls_ssl_config;
348+
aligned_calloc(align_of::<mbedtls_ssl_config>(), size_of::<mbedtls_ssl_config>()) as *mut mbedtls_ssl_config;
341349
if ssl_config.is_null() {
342350
free(drbg_context as *const _);
343351
free(ssl_context as *const _);
344352
return Err(TlsError::OutOfMemory);
345353
}
346354

347-
let crt = calloc(1, size_of::<mbedtls_x509_crt>()) as *mut mbedtls_x509_crt;
355+
let crt = aligned_calloc(align_of::<mbedtls_x509_crt>(), size_of::<mbedtls_x509_crt>()) as *mut mbedtls_x509_crt;
348356
if crt.is_null() {
349357
free(drbg_context as *const _);
350358
free(ssl_context as *const _);
@@ -353,7 +361,7 @@ impl<'a> Certificates<'a> {
353361
}
354362

355363
let certificate =
356-
calloc(1, size_of::<mbedtls_x509_crt>()) as *mut mbedtls_x509_crt;
364+
aligned_calloc(align_of::<mbedtls_x509_crt>(), size_of::<mbedtls_x509_crt>()) as *mut mbedtls_x509_crt;
357365
if certificate.is_null() {
358366
free(drbg_context as *const _);
359367
free(ssl_context as *const _);
@@ -363,7 +371,7 @@ impl<'a> Certificates<'a> {
363371
}
364372

365373
let private_key =
366-
calloc(1, size_of::<mbedtls_pk_context>()) as *mut mbedtls_pk_context;
374+
aligned_calloc(align_of::<mbedtls_pk_context>(), size_of::<mbedtls_pk_context>()) as *mut mbedtls_pk_context;
367375
if private_key.is_null() {
368376
free(drbg_context as *const _);
369377
free(ssl_context as *const _);
@@ -930,7 +938,7 @@ pub mod asynch {
930938

931939
#[cfg(feature = "edge-nal")]
932940
pub use super::edge_nal::*;
933-
941+
934942
/// An async TLS session over a stream represented by `embedded-io-async`'s `Read` and `Write` traits.
935943
pub struct Session<'a, T> {
936944
pub(crate) stream: T,

0 commit comments

Comments
 (0)