Commit 0bae965f authored by David Brown's avatar David Brown Committed by David Brown
Browse files

Zephyr: Use mbed TLS allocator

Instead of the overly-simplistic allocator in Zephyr, use the still
simplistic allocator in mbed TLS.

On K64f, this saves 848 bytes of text, 44 bytes of data, and 208 bytes
of bss.

Signed-off-by: default avatarDavid Brown <david.brown@linaro.org>
parent ce4fa440
Loading
Loading
Loading
Loading
+9 −17
Changes for boot/zephyr/os.c: 9 added lines, 17 removed lines.
Original line number Diff line number Diff line
@@ -24,29 +24,21 @@

#define MBEDTLS_CONFIG_FILE CONFIG_MBEDTLS_CFG_FILE
#include <mbedtls/platform.h>
#include <mbedtls/memory_buffer_alloc.h>

/* D(void *os_malloc(size_t size)) */
void *os_calloc(size_t nelem, size_t size)
{
    /* Note that this doesn't check for overflow.  Assume the
     * calls only come from within the app. */
    size_t total = nelem * size;
    void *buf = k_malloc(total);
    if (buf) {
        memset(buf, 0, total);
    }
    return buf;
}
/*
 * This is the heap for mbed TLS.  The value needed depends on the key
 * size and algorithm used.  For RSA-2048, 6144 bytes seems to be
 * enough.
 */
#define CRYPTO_HEAP_SIZE 6144

void os_free(void *ptr)
{
    k_free(ptr);
}
static unsigned char mempool[CRYPTO_HEAP_SIZE];

/*
 * Initialize mbedtls to be able to use the local heap.
 */
void os_heap_init(void)
{
    mbedtls_platform_set_calloc_free(os_calloc, os_free);
    mbedtls_memory_buffer_alloc_init(mempool, sizeof(mempool));
}
+2 −2
Changes for boot/zephyr/prj-p256.conf: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -11,8 +11,8 @@ CONFIG_MBEDTLS_CFG_FILE="config-asn1.h"
# CONFIG_TINYCRYPT_ECC_DSA is not set
# CONFIG_TINYCRYPT_SHA256 is not set

### mbedTLS wants a heap
CONFIG_HEAP_MEM_POOL_SIZE=16384
### mbedTLS has its own heap
# CONFIG_HEAP_MEM_POOL_SIZE is not set

CONFIG_FLASH=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
+2 −2
Changes for boot/zephyr/prj.conf: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -8,8 +8,8 @@ CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_CFG_FILE="config-boot.h"

### mbedTLS wants a heap
CONFIG_HEAP_MEM_POOL_SIZE=16384
### mbedTLS has its own heap
# CONFIG_HEAP_MEM_POOL_SIZE is not set

CONFIG_FLASH=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y