@@ -32,6 +32,14 @@ pub mod io {
32
32
pub use embedded_io:: * ;
33
33
}
34
34
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
+
35
43
// Baremetal: these will come from `esp-wifi` (i.e. this can only be used together with esp-wifi)
36
44
// STD: these will come from `libc` indirectly via the Rust standard library
37
45
extern "C" {
@@ -323,28 +331,28 @@ impl<'a> Certificates<'a> {
323
331
unsafe {
324
332
error_checked ! ( psa_crypto_init( ) ) ?;
325
333
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 > ( ) )
327
335
as * mut mbedtls_ctr_drbg_context ;
328
336
if drbg_context. is_null ( ) {
329
337
return Err ( TlsError :: OutOfMemory ) ;
330
338
}
331
339
332
340
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 ;
334
342
if ssl_context. is_null ( ) {
335
343
free ( drbg_context as * const _ ) ;
336
344
return Err ( TlsError :: OutOfMemory ) ;
337
345
}
338
346
339
347
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 ;
341
349
if ssl_config. is_null ( ) {
342
350
free ( drbg_context as * const _ ) ;
343
351
free ( ssl_context as * const _ ) ;
344
352
return Err ( TlsError :: OutOfMemory ) ;
345
353
}
346
354
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 ;
348
356
if crt. is_null ( ) {
349
357
free ( drbg_context as * const _ ) ;
350
358
free ( ssl_context as * const _ ) ;
@@ -353,7 +361,7 @@ impl<'a> Certificates<'a> {
353
361
}
354
362
355
363
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 ;
357
365
if certificate. is_null ( ) {
358
366
free ( drbg_context as * const _ ) ;
359
367
free ( ssl_context as * const _ ) ;
@@ -363,7 +371,7 @@ impl<'a> Certificates<'a> {
363
371
}
364
372
365
373
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 ;
367
375
if private_key. is_null ( ) {
368
376
free ( drbg_context as * const _ ) ;
369
377
free ( ssl_context as * const _ ) ;
@@ -930,7 +938,7 @@ pub mod asynch {
930
938
931
939
#[ cfg( feature = "edge-nal" ) ]
932
940
pub use super :: edge_nal:: * ;
933
-
941
+
934
942
/// An async TLS session over a stream represented by `embedded-io-async`'s `Read` and `Write` traits.
935
943
pub struct Session < ' a , T > {
936
944
pub ( crate ) stream : T ,
0 commit comments