Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
esp32/mbedtls: Added in/out buffer allocation strategy
Browse files Browse the repository at this point in the history
"Internal IRAM" strategy allocates in/out buffers in IRAM thus
saving memory in DRAM.

To select this strategy:

idf.py menuconfig -> Component config -> ESP32 Specific -> Enable IRAM as 8 bit accessible memory

idf.py menuconfig -> Component config -> mbedTLS -> Memory allocation strategy -> Internal IRAM
  • Loading branch information
sachin0x18 committed Apr 21, 2020
1 parent 4f3b7b5 commit 5776384
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions vendors/espressif/boards/esp32/components/mbedtls/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ choice MBEDTLS_MEM_ALLOC_MODE
behavior in ESP-IDF
- Custom allocation mode, by overwriting calloc()/free() using
mbedtls_platform_set_calloc_free() function
- Internal IRAM memory wherever applicable else internal DRAM

Recommended mode here is always internal, since that is most preferred
from security perspective. But if application requirement does not
Expand All @@ -32,6 +33,16 @@ config MBEDTLS_DEFAULT_MEM_ALLOC
config MBEDTLS_CUSTOM_MEM_ALLOC
bool "Custom alloc mode"

config MBEDTLS_IRAM_8BIT_MEM_ALLOC
bool "Internal IRAM"
depends on ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
help
Allows to use IRAM memory region as 8bit accessible region.

TLS input and output buffers will be allocated in IRAM section which is 32bit aligned
memory. Every unaligned (8bit or 16bit) access will result in an exception
and incur penalty of certain clock cycles per unaligned read/write.

endchoice #MBEDTLS_MEM_ALLOC_MODE

config MBEDTLS_SSL_MAX_CONTENT_LEN
Expand Down
10 changes: 10 additions & 0 deletions vendors/espressif/boards/esp32/components/mbedtls/port/esp_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ IRAM_ATTR void *esp_mbedtls_mem_calloc(size_t n, size_t size)
return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
#elif CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC
return heap_caps_calloc(n, size, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT);
#elif CONFIG_MBEDTLS_IRAM_8BIT_MEM_ALLOC
#ifdef CONFIG_MBEDTLS_ASYMMETRIC_CONTENT_LEN
if ((n*size) >= CONFIG_MBEDTLS_SSL_IN_CONTENT_LEN || (n*size) >= CONFIG_MBEDTLS_SSL_OUT_CONTENT_LEN) {
#else
if ((n*size) >= CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN) {
#endif
return heap_caps_calloc_prefer(n, size, 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
} else {
return heap_caps_calloc(n, size, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
}
#else
return calloc(n, size);
#endif
Expand Down

0 comments on commit 5776384

Please sign in to comment.