diff --git a/options.mk b/options.mk index 6fedd10d..52500449 100644 --- a/options.mk +++ b/options.mk @@ -33,7 +33,7 @@ ifeq ($(SIGN),ECC256) CFLAGS+=-D"WOLFBOOT_SIGN_ECC256" ifeq ($(WOLFBOOT_SMALL_STACK),1) STACK_USAGE?=4096 - OBJS+=./src/xmalloc_ecc.o + OBJS+=./src/xmalloc.o else ifeq ($(WOLFTPM),0) STACK_USAGE?=3888 else @@ -55,6 +55,9 @@ ifeq ($(SIGN),ED25519) ./lib/wolfssl/wolfcrypt/src/fe_low_mem.o PUBLIC_KEY_OBJS=./src/ed25519_pub_key.o CFLAGS+=-D"WOLFBOOT_SIGN_ED25519" + ifeq ($(WOLFBOOT_SMALL_STACK),1) + OBJS+=./src/xmalloc.o + endif STACK_USAGE?=1024 endif @@ -75,7 +78,7 @@ ifeq ($(SIGN),RSA2048) -D"IMAGE_HEADER_SIZE=512" ifeq ($(WOLFBOOT_SMALL_STACK),1) STACK_USAGE?=4096 - OBJS+=./src/xmalloc_rsa.o + OBJS+=./src/xmalloc.o else ifeq ($(WOLFTPM),0) STACK_USAGE?=12288 else @@ -100,7 +103,7 @@ ifeq ($(SIGN),RSA4096) -D"IMAGE_HEADER_SIZE=1024" ifeq ($(WOLFBOOT_SMALL_STACK),1) STACK_USAGE?=4096 - OBJS+=./src/xmalloc_rsa.o + OBJS+=./src/xmalloc.o else ifeq ($(WOLFTPM),0) STACK_USAGE?=18064 else diff --git a/src/xmalloc_ecc.c b/src/xmalloc.c similarity index 54% rename from src/xmalloc_ecc.c rename to src/xmalloc.c index 1cbe9945..72a73b0d 100644 --- a/src/xmalloc_ecc.c +++ b/src/xmalloc.c @@ -1,6 +1,7 @@ -/* xmalloc_ecc.c + +/* xmalloc.c * - * Implementations of minimal malloc/free + * Fixed-pool implementation of malloc/free for wolfBoot * * * Copyright (C) 2021 wolfSSL Inc. @@ -28,20 +29,27 @@ #include #include "target.h" -#define SP_CURVE_SPECS_SIZE (80) + +struct xmalloc_slot { + uint8_t *addr; + uint32_t size; + uint32_t in_use; +}; + #define SP_DIGIT_SIZE (4) #ifdef WOLFBOOT_HASH_SHA256 -#include +# include # define HASH_BLOCK_SIZE WC_SHA256_BLOCK_SIZE #elif defined WOLFBOOT_HASH_SHA3_384 -#include +# include # define HASH_BLOCK_SIZE WC_SHA3_384_BLOCK_SIZE #else # error "No hash mechanism selected." #endif - +#ifdef WOLFBOOT_SIGN_ECC256 +#define SP_CURVE_SPECS_SIZE (80) #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM #define SP_POINT_SIZE (196) #define SP_DIGITS_BUFFER_SIZE_0 (SP_DIGIT_SIZE * 16 * 8) @@ -52,15 +60,6 @@ #define SP_DIGITS_BUFFER_SIZE_1 (SP_DIGIT_SIZE * (3 * 10 + 1)) #define SP_DIGITS_BUFFER_SIZE_2 (SP_DIGIT_SIZE * (2 * 10 * 5)) #endif - - - -struct xmalloc_slot { - uint8_t *addr; - uint32_t size; - uint32_t in_use; -}; - static uint8_t sp_curve_specs[SP_CURVE_SPECS_SIZE]; static uint8_t sp_points_0[SP_POINT_SIZE * 2]; static uint8_t sp_points_1[SP_POINT_SIZE * 2]; @@ -76,7 +75,7 @@ static uint8_t sp_montgomery[sizeof(int64_t) * 2 * 8]; #endif static uint32_t sha_block[HASH_BLOCK_SIZE]; -static struct xmalloc_slot ecc_xmalloc_slots[] = { +static struct xmalloc_slot xmalloc_pool[] = { #ifdef WOLFBOOT_HASH_SHA256 { (uint8_t *)sha_block, HASH_BLOCK_SIZE * sizeof(uint32_t), 0 }, #endif @@ -98,15 +97,85 @@ static struct xmalloc_slot ecc_xmalloc_slots[] = { { NULL, 0, 0} }; +#elif defined WOLFBOOT_SIGN_ED25519 + +static uint32_t sha_block[HASH_BLOCK_SIZE]; +static uint32_t sha512_block[sizeof(word64) * 16]; + +static struct xmalloc_slot xmalloc_pool[] = { +#ifdef WOLFBOOT_HASH_SHA256 + { (uint8_t *)sha_block, HASH_BLOCK_SIZE * sizeof(uint32_t), 0 }, +#endif + { (uint8_t *)sha512_block, sizeof(word64) * 16, 0 }, + { NULL, 0, 0} +}; + +#elif defined WOLFBOOT_SIGN_RSA2048 + #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM + #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 64 * 5) + #else + #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 90 * 5) + #define SPDIGIT_BUF1_SIZE (SP_DIGIT_SIZE * (90 * 4 + 3)) + static uint8_t sp_digit_buf1[SPDIGIT_BUF1_SIZE]; + #endif + static uint32_t sha_block[HASH_BLOCK_SIZE]; + static uint8_t sp_digit_buf0[SPDIGIT_BUF0_SIZE]; + static struct xmalloc_slot xmalloc_pool[] = { + #ifdef WOLFBOOT_HASH_SHA256 + { (uint8_t *)sha_block, WC_SHA256_BLOCK_SIZE * sizeof(uint32_t), 0 }, + #endif + { sp_digit_buf0, SPDIGIT_BUF0_SIZE, 0}, + #ifndef WOLFSSL_SP_ARM_CORTEX_M_ASM + { sp_digit_buf1, SPDIGIT_BUF1_SIZE, 0}, + #endif + { NULL, 0, 0} + }; + +#elif defined WOLFBOOT_SIGN_RSA4096 + #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM + #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 128 * 5) + #else + #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 180 * 5) + #define SPDIGIT_BUF1_SIZE (SP_DIGIT_SIZE * (180 * 4 + 3)) + static uint8_t sp_digit_buf1[SPDIGIT_BUF1_SIZE]; + #endif + static uint32_t sha_block[HASH_BLOCK_SIZE]; + static uint8_t sp_digit_buf0[SPDIGIT_BUF0_SIZE]; + static struct xmalloc_slot xmalloc_pool[] = { + #ifdef WOLFBOOT_HASH_SHA256 + { (uint8_t *)sha_block, WC_SHA256_BLOCK_SIZE * sizeof(uint32_t), 0 }, + #endif + { sp_digit_buf0, SPDIGIT_BUF0_SIZE, 0}, + #ifndef WOLFSSL_SP_ARM_CORTEX_M_ASM + { sp_digit_buf1, SPDIGIT_BUF1_SIZE, 0}, + #endif + { NULL, 0, 0} + }; + + +#elif defined WOLFBOOT_SIGN_NONE +static struct xmalloc_slot xmalloc_pool[] = { + #ifdef WOLFBOOT_HASH_SHA256 + { (uint8_t *)sha_block, WC_SHA256_BLOCK_SIZE * sizeof(uint32_t), 0 }, + #endif + { NULL, 0, 0} + }; + +}; + +#else +# error "No cipher selected." +#endif + void* XMALLOC(size_t n, void* heap, int type) { int i = 0; - while (ecc_xmalloc_slots[i].addr) { - if ((n == ecc_xmalloc_slots[i].size) && - (ecc_xmalloc_slots[i].in_use == 0)) { - ecc_xmalloc_slots[i].in_use++; - return ecc_xmalloc_slots[i].addr; + while (xmalloc_pool[i].addr) { + if ((n == xmalloc_pool[i].size) && + (xmalloc_pool[i].in_use == 0)) { + xmalloc_pool[i].in_use++; + return xmalloc_pool[i].addr; } i++; } @@ -116,9 +185,9 @@ void* XMALLOC(size_t n, void* heap, int type) void XFREE(void *ptr, void *heap, int type) { int i = 0; - while (ecc_xmalloc_slots[i].addr) { - if ((ptr == (void *)(ecc_xmalloc_slots[i].addr)) && ecc_xmalloc_slots[i].in_use) { - ecc_xmalloc_slots[i].in_use = 0; + while (xmalloc_pool[i].addr) { + if ((ptr == (void *)(xmalloc_pool[i].addr)) && xmalloc_pool[i].in_use) { + xmalloc_pool[i].in_use = 0; return; } i++; diff --git a/src/xmalloc_rsa.c b/src/xmalloc_rsa.c deleted file mode 100644 index 3dc13612..00000000 --- a/src/xmalloc_rsa.c +++ /dev/null @@ -1,102 +0,0 @@ -/* xmalloc_ecc.c - * - * Implementations of minimal malloc/free - * - * - * Copyright (C) 2021 wolfSSL Inc. - * - * This file is part of wolfBoot. - * - * wolfBoot is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * wolfBoot is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA - */ - -#include -#include -#include -#include -#include - -#define SP_DIGIT_SIZE (4) - - - -#ifdef WOLFBOOT_SIGN_RSA2048 - #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM - #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 64 * 5) - #else - #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 90 * 5) - #define SPDIGIT_BUF1_SIZE (SP_DIGIT_SIZE * (90 * 4 + 3)) - static uint8_t sp_digit_buf1[SPDIGIT_BUF1_SIZE]; - #endif -#endif - -#ifdef WOLFBOOT_SIGN_RSA4096 - #ifdef WOLFSSL_SP_ARM_CORTEX_M_ASM - #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 128 * 5) - #else - #define SPDIGIT_BUF0_SIZE (SP_DIGIT_SIZE * 180 * 5) - #define SPDIGIT_BUF1_SIZE (SP_DIGIT_SIZE * (180 * 4 + 3)) - static uint8_t sp_digit_buf1[SPDIGIT_BUF1_SIZE]; - #endif -#endif - - -static uint8_t sp_digit_buf0[SPDIGIT_BUF0_SIZE]; - -struct xmalloc_slot { - uint8_t *addr; - uint32_t size; - uint32_t in_use; -}; - -static uint32_t sha_block[WC_SHA256_BLOCK_SIZE]; - -static struct xmalloc_slot rsa_xmalloc_slots[] = { -#ifdef WOLFBOOT_HASH_SHA256 - { (uint8_t *)sha_block, WC_SHA256_BLOCK_SIZE * sizeof(uint32_t), 0 }, -#endif - { sp_digit_buf0, SPDIGIT_BUF0_SIZE, 0}, -#ifndef WOLFSSL_SP_ARM_CORTEX_M_ASM - { sp_digit_buf1, SPDIGIT_BUF1_SIZE, 0}, -#endif - { NULL, 0, 0} -}; - -void* XMALLOC(size_t n, void* heap, int type) -{ - int i = 0; - - while (rsa_xmalloc_slots[i].addr) { - if ((n == rsa_xmalloc_slots[i].size) && - (rsa_xmalloc_slots[i].in_use == 0)) { - rsa_xmalloc_slots[i].in_use++; - return rsa_xmalloc_slots[i].addr; - } - i++; - } - return NULL; -} - -void XFREE(void *ptr, void *heap, int type) -{ - int i = 0; - while (rsa_xmalloc_slots[i].addr) { - if ((ptr == (void *)(rsa_xmalloc_slots[i].addr)) && rsa_xmalloc_slots[i].in_use) { - rsa_xmalloc_slots[i].in_use = 0; - return; - } - i++; - } -}