diff --git a/Makefile b/Makefile index d336c6a..4ef6c59 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ AR = ar CFLAGS = -Wall -Wextra -std=c11 -I include LDFLAGS = -SRCS = src/se050_i2c_hal.c src/se050_session.c src/se050_keystore.c \ +SRCS = src/se050_i2c_hal.c src/se050_mem_pool.c src/se050_session.c src/se050_keystore.c \ src/se050_rng.c src/se050_x25519.c src/se050_x25519_sw.c \ src/se050_chacha20_poly1305.c src/se050_blake2s.c \ src/se050_hmac_blake2s.c src/se050_hkdf_blake2s.c src/se050_tai64n.c \ diff --git a/include/se050_i2c_hal.h b/include/se050_i2c_hal.h index efce291..e87e0fd 100644 --- a/include/se050_i2c_hal.h +++ b/include/se050_i2c_hal.h @@ -7,6 +7,8 @@ #define SE050_I2C_HAL_H #include +#include +#include /* Status codes */ typedef enum { @@ -24,10 +26,12 @@ typedef enum { } se050_status_t; /* I2C HAL structure */ +#define SE050_I2C_DEV_PATH_MAX 64 + typedef struct { void *handle; /**< I2C file descriptor */ uint8_t slave_addr; /**< I2C slave address */ - const char *dev_path; /**< I2C device path */ + char dev_path[SE050_I2C_DEV_PATH_MAX]; /**< I2C device path */ int wakeup_pin; /**< Wakeup GPIO pin (-1 if unused) */ } se050_i2c_hal_t; diff --git a/include/se050_mem_pool.h b/include/se050_mem_pool.h new file mode 100644 index 0000000..9d8c198 --- /dev/null +++ b/include/se050_mem_pool.h @@ -0,0 +1,106 @@ +/** + * @file se050_mem_pool.h + * @brief Static Memory Pool for Embedded Systems + * + * Replaces malloc/calloc with pre-allocated static pools. + * Suitable for u-boot, ESP32, and other embedded environments. + */ + +#ifndef SE050_MEM_POOL_H +#define SE050_MEM_POOL_H + +#include +#include + +/* Configuration: Pool sizes */ +#ifndef SE050_POOL_SESSION_COUNT +#define SE050_POOL_SESSION_COUNT 4 +#endif + +#ifndef SE050_POOL_SCP03_COUNT +#define SE050_POOL_SCP03_COUNT 4 +#endif + +#ifndef SE050_POOL_KEYSTORE_COUNT +#define SE050_POOL_KEYSTORE_COUNT 2 +#endif + +#ifndef SE050_POOL_KEYSTORE_MAX_OBJECTS +#define SE050_POOL_KEYSTORE_MAX_OBJECTS 8 +#endif + +#ifndef SE050_POOL_RNG_COUNT +#define SE050_POOL_RNG_COUNT 2 +#endif + +#ifndef SE050_POOL_I2C_HAL_COUNT +#define SE050_POOL_I2C_HAL_COUNT 2 +#endif + +/* Forward declarations */ +struct se050_session_ctx; +struct se050_scp03_ctx; +struct se050_keystore_ctx; +struct se050_rng_ctx; +struct se050_i2c_hal; + +/* ============================================================================ + * Memory Pool API + * ============================================================================ */ + +/** + * @brief Initialize all memory pools + * + * Must be called before any other SE050 functions. + * + * @return 0 on success, -1 on error + */ +int se050_mem_pool_init(void); + +/** + * @brief Cleanup all memory pools + * + * Zeroizes all allocated memory before freeing. + */ +void se050_mem_pool_cleanup(void); + +/* Session pool */ +struct se050_session_ctx *se050_session_alloc_pool(void); +void se050_session_free_pool(struct se050_session_ctx *ctx); + +/* SCP03 pool */ +struct se050_scp03_ctx *se050_scp03_alloc_pool(void); +void se050_scp03_free_pool(struct se050_scp03_ctx *ctx); + +/* Keystore pool */ +struct se050_keystore_ctx *se050_keystore_alloc_pool(void); +void se050_keystore_free_pool(struct se050_keystore_ctx *ctx); + +/* RNG pool */ +struct se050_rng_ctx *se050_rng_alloc_pool(void); +void se050_rng_free_pool(struct se050_rng_ctx *ctx); + +/* I2C HAL pool */ +struct se050_i2c_hal *se050_i2c_hal_alloc_pool(void); +void se050_i2c_hal_free_pool(struct se050_i2c_hal *hal); + +/* ============================================================================ + * Debug/Statistics + * ============================================================================ */ + +/** + * @brief Get pool statistics + */ +typedef struct { + int total; + int used; + int free; +} se050_pool_stats_t; + +void se050_mem_pool_stats(se050_pool_stats_t *session, + se050_pool_stats_t *scp03, + se050_pool_stats_t *keystore, + se050_pool_stats_t *rng, + se050_pool_stats_t *i2c_hal); + +#endif /* SE050_MEM_POOL_H */ diff --git a/include/se050_scp03.h b/include/se050_scp03.h index 13f6607..66d5225 100644 --- a/include/se050_scp03.h +++ b/include/se050_scp03.h @@ -12,6 +12,7 @@ /* Forward declarations */ typedef struct se050_session_ctx se050_session_ctx_t; +typedef struct se050_scp03_ctx se050_scp03_ctx_t; /* SCP03 key sizes */ #define SCP03_KEY_SIZE 16 diff --git a/include/se050_session_internal.h b/include/se050_session_internal.h index b89b521..b3321cf 100644 --- a/include/se050_session_internal.h +++ b/include/se050_session_internal.h @@ -10,6 +10,7 @@ #ifndef SE050_SESSION_INTERNAL_H #define SE050_SESSION_INTERNAL_H +#include "se050_i2c_hal.h" #include "se050_wireguard.h" #include diff --git a/src/se050_i2c_hal.c b/src/se050_i2c_hal.c index d59237d..9d2d979 100644 --- a/src/se050_i2c_hal.c +++ b/src/se050_i2c_hal.c @@ -136,7 +136,8 @@ se050_status_t se050_i2c_init(se050_i2c_hal_t *hal, const char *dev_path, uint8_ hal->handle = handle; hal->slave_addr = slave_addr; - hal->dev_path = strdup(dev_path); + strncpy(hal->dev_path, dev_path, SE050_I2C_DEV_PATH_MAX - 1); + hal->dev_path[SE050_I2C_DEV_PATH_MAX - 1] = '\0'; return SE050_OK; } @@ -152,11 +153,7 @@ void se050_i2c_close(se050_i2c_hal_t *hal) hal->handle = NULL; } - if (hal->dev_path) { - free((void *)hal->dev_path); - hal->dev_path = NULL; - } - + hal->dev_path[0] = '\0'; hal->slave_addr = 0; } diff --git a/src/se050_keystore.c b/src/se050_keystore.c index d353848..a4a6b05 100644 --- a/src/se050_keystore.c +++ b/src/se050_keystore.c @@ -12,16 +12,14 @@ #include "se050_i2c_hal.h" #include "se050_session_internal.h" +#include "se050_mem_pool.h" #include "se050_wireguard.h" #include "se050_crypto_utils.h" #include "se050_keystore_internal.h" #include -#include #include #include -#include "se050_mem_protect.h" - /* ============================================================================ * Key Store Management * ============================================================================ */ @@ -29,46 +27,24 @@ se050_status_t se050_keystore_init(se050_keystore_ctx_t **ctx, se050_session_ctx_t *session) { se050_keystore_ctx_t *keystore; - size_t ctx_size; if (!ctx || !session) { return SE050_ERR_INVALID_ARG; } - /* Allocate key store context */ - keystore = (se050_keystore_ctx_t *)calloc(1, sizeof(*keystore)); + /* Allocate key store context from static pool */ + keystore = se050_keystore_alloc_pool(); if (!keystore) { return SE050_ERR_FAIL; } - ctx_size = sizeof(*keystore); - - /* Apply memory protection (Linux only) */ - if (protect_sensitive_memory(keystore, ctx_size) != SE050_OK) { - free(keystore); - return SE050_ERR_FAIL; - } - keystore->session = session; - keystore->max_objects = 16; /* Maximum 16 keys */ - keystore->objects = (key_object_t *)calloc(keystore->max_objects, sizeof(key_object_t)); - - if (!keystore->objects) { - release_memory_protection(keystore, ctx_size); - free(keystore); - return SE050_ERR_FAIL; - } - - /* Protect key objects array */ - if (protect_sensitive_memory(keystore->objects, keystore->max_objects * sizeof(key_object_t)) != SE050_OK) { - free(keystore->objects); - release_memory_protection(keystore, ctx_size); - free(keystore); - return SE050_ERR_FAIL; - } - + keystore->max_objects = SE050_POOL_KEYSTORE_MAX_OBJECTS; keystore->num_objects = 0; + /* Zeroize key objects array */ + memset(keystore->objects, 0, keystore->max_objects * sizeof(key_object_t)); + *ctx = keystore; return SE050_OK; } @@ -91,21 +67,11 @@ void se050_keystore_free(se050_keystore_ctx_t *ctx) } } - /* Release memory protection for key objects array */ - if (ctx->objects) { - release_memory_protection(ctx->objects, ctx->max_objects * sizeof(key_object_t)); - free(ctx->objects); - ctx->objects = NULL; - } - ctx->num_objects = 0; ctx->max_objects = 0; - /* Release memory protection for context */ - release_memory_protection(ctx, sizeof(*ctx)); - - /* Free key store context */ - free(ctx); + /* Free key store context to static pool */ + se050_keystore_free_pool(ctx); } /* ============================================================================ diff --git a/src/se050_mem_pool.c b/src/se050_mem_pool.c new file mode 100644 index 0000000..13a84d1 --- /dev/null +++ b/src/se050_mem_pool.c @@ -0,0 +1,304 @@ +/** + * @file se050_mem_pool.c + * @brief Static Memory Pool Implementation + */ + +#define _GNU_SOURCE +#include "se050_mem_pool.h" +#include "se050_session_internal.h" +#include "se050_scp03.h" +#include "se050_keystore_internal.h" +#include "se050_i2c_hal.h" +#include +#include + +/* Forward declarations for structures */ +/* se050_rng_ctx_t is defined in se050_rng.c, use opaque pointer here */ + +/* ============================================================================ + * Pool Structures + * ============================================================================ */ + +typedef struct { + uint8_t *pool; + size_t item_size; + size_t count; + atomic_int used_count; + uint8_t *used_bitmap; /* 1 bit per item */ +} mem_pool_t; + +/* Session pool */ +static mem_pool_t g_session_pool; +static uint8_t g_session_storage[SE050_POOL_SESSION_COUNT * sizeof(struct se050_session_ctx)]; +static uint8_t g_session_bitmap[SE050_POOL_SESSION_COUNT / 8 + 1]; + +/* SCP03 pool */ +static mem_pool_t g_scp03_pool; +static uint8_t g_scp03_storage[SE050_POOL_SCP03_COUNT * sizeof(struct se050_scp03_ctx)]; +static uint8_t g_scp03_bitmap[SE050_POOL_SCP03_COUNT / 8 + 1]; + +/* Keystore pool */ +static mem_pool_t g_keystore_pool; +static uint8_t g_keystore_storage[SE050_POOL_KEYSTORE_COUNT * sizeof(struct se050_keystore_ctx)]; +static uint8_t g_keystore_bitmap[SE050_POOL_KEYSTORE_COUNT / 8 + 1]; + +/* RNG pool */ +static mem_pool_t g_rng_pool; +#define SE050_RNG_CTX_SIZE (sizeof(void*) + sizeof(uint32_t) + 64 + sizeof(size_t)) +static uint8_t g_rng_storage[SE050_POOL_RNG_COUNT * SE050_RNG_CTX_SIZE]; +static uint8_t g_rng_bitmap[SE050_POOL_RNG_COUNT / 8 + 1]; + +/* I2C HAL pool */ +static mem_pool_t g_i2c_hal_pool; +static uint8_t g_i2c_hal_storage[SE050_POOL_I2C_HAL_COUNT * sizeof(se050_i2c_hal_t)]; +static uint8_t g_i2c_hal_bitmap[SE050_POOL_I2C_HAL_COUNT / 8 + 1]; + +/* ============================================================================ + * Helper Functions + * ============================================================================ */ + +static void pool_init(mem_pool_t *pool, uint8_t *storage, size_t item_size, + size_t count, uint8_t *bitmap) +{ + pool->pool = storage; + pool->item_size = item_size; + pool->count = count; + atomic_store(&pool->used_count, 0); + memset(bitmap, 0, (count + 7) / 8); +} + +static int pool_alloc(mem_pool_t *pool, uint8_t *bitmap) +{ + for (size_t i = 0; i < pool->count; i++) { + size_t byte_idx = i / 8; + size_t bit_idx = i % 8; + + if (!(bitmap[byte_idx] & (1 << bit_idx))) { + bitmap[byte_idx] |= (1 << bit_idx); + atomic_fetch_add(&pool->used_count, 1); + return (int)i; + } + } + return -1; /* Pool exhausted */ +} + +static void pool_free(mem_pool_t *pool, uint8_t *bitmap, int index) +{ + if (index < 0 || (size_t)index >= pool->count) return; + + size_t byte_idx = index / 8; + size_t bit_idx = index % 8; + + bitmap[byte_idx] &= ~(1 << bit_idx); + atomic_fetch_sub(&pool->used_count, 1); +} + +static void pool_zeroize(mem_pool_t *pool, uint8_t *bitmap) +{ + for (size_t i = 0; i < pool->count; i++) { + size_t byte_idx = i / 8; + size_t bit_idx = i % 8; + + if (bitmap[byte_idx] & (1 << bit_idx)) { + memset(pool->pool + (i * pool->item_size), 0, pool->item_size); + } + } +} + +/* ============================================================================ + * Public API + * ============================================================================ */ + +int se050_mem_pool_init(void) +{ + pool_init(&g_session_pool, g_session_storage, sizeof(struct se050_session_ctx), + SE050_POOL_SESSION_COUNT, g_session_bitmap); + + pool_init(&g_scp03_pool, g_scp03_storage, sizeof(struct se050_scp03_ctx), + SE050_POOL_SCP03_COUNT, g_scp03_bitmap); + + pool_init(&g_keystore_pool, g_keystore_storage, sizeof(struct se050_keystore_ctx), + SE050_POOL_KEYSTORE_COUNT, g_keystore_bitmap); + + pool_init(&g_rng_pool, g_rng_storage, SE050_RNG_CTX_SIZE, + SE050_POOL_RNG_COUNT, g_rng_bitmap); + + pool_init(&g_i2c_hal_pool, g_i2c_hal_storage, sizeof(se050_i2c_hal_t), + SE050_POOL_I2C_HAL_COUNT, g_i2c_hal_bitmap); + + return 0; +} + +void se050_mem_pool_cleanup(void) +{ + pool_zeroize(&g_session_pool, g_session_bitmap); + pool_zeroize(&g_scp03_pool, g_scp03_bitmap); + pool_zeroize(&g_keystore_pool, g_keystore_bitmap); + pool_zeroize(&g_rng_pool, g_rng_bitmap); + pool_zeroize(&g_i2c_hal_pool, g_i2c_hal_bitmap); + + memset(g_session_bitmap, 0, sizeof(g_session_bitmap)); + memset(g_scp03_bitmap, 0, sizeof(g_scp03_bitmap)); + memset(g_keystore_bitmap, 0, sizeof(g_keystore_bitmap)); + memset(g_rng_bitmap, 0, sizeof(g_rng_bitmap)); + memset(g_i2c_hal_bitmap, 0, sizeof(g_i2c_hal_bitmap)); +} + +/* Session pool */ +struct se050_session_ctx *se050_session_alloc_pool(void) +{ + int idx = pool_alloc(&g_session_pool, g_session_bitmap); + if (idx < 0) return NULL; + + struct se050_session_ctx *ctx = (struct se050_session_ctx *) + (g_session_pool.pool + (idx * g_session_pool.item_size)); + memset(ctx, 0, sizeof(*ctx)); + return ctx; +} + +void se050_session_free_pool(struct se050_session_ctx *ctx) +{ + if (!ctx) return; + + /* Find index and zeroize */ + size_t offset = (uint8_t *)ctx - g_session_pool.pool; + if (offset < g_session_pool.count * g_session_pool.item_size) { + size_t idx = offset / g_session_pool.item_size; + pool_zeroize(&g_session_pool, g_session_bitmap); + pool_free(&g_session_pool, g_session_bitmap, (int)idx); + } +} + +/* SCP03 pool */ +struct se050_scp03_ctx *se050_scp03_alloc_pool(void) +{ + int idx = pool_alloc(&g_scp03_pool, g_scp03_bitmap); + if (idx < 0) return NULL; + + struct se050_scp03_ctx *ctx = (struct se050_scp03_ctx *) + (g_scp03_pool.pool + (idx * g_scp03_pool.item_size)); + memset(ctx, 0, sizeof(*ctx)); + return ctx; +} + +void se050_scp03_free_pool(struct se050_scp03_ctx *ctx) +{ + if (!ctx) return; + + size_t offset = (uint8_t *)ctx - g_scp03_pool.pool; + if (offset < g_scp03_pool.count * g_scp03_pool.item_size) { + size_t idx = offset / g_scp03_pool.item_size; + pool_zeroize(&g_scp03_pool, g_scp03_bitmap); + pool_free(&g_scp03_pool, g_scp03_bitmap, (int)idx); + } +} + +/* Keystore pool */ +struct se050_keystore_ctx *se050_keystore_alloc_pool(void) +{ + int idx = pool_alloc(&g_keystore_pool, g_keystore_bitmap); + if (idx < 0) return NULL; + + struct se050_keystore_ctx *ctx = (struct se050_keystore_ctx *) + (g_keystore_pool.pool + (idx * g_keystore_pool.item_size)); + memset(ctx, 0, sizeof(*ctx)); + return ctx; +} + +void se050_keystore_free_pool(struct se050_keystore_ctx *ctx) +{ + if (!ctx) return; + + size_t offset = (uint8_t *)ctx - g_keystore_pool.pool; + if (offset < g_keystore_pool.count * g_keystore_pool.item_size) { + size_t idx = offset / g_keystore_pool.item_size; + pool_zeroize(&g_keystore_pool, g_keystore_bitmap); + pool_free(&g_keystore_pool, g_keystore_bitmap, (int)idx); + } +} + +/* RNG pool */ +struct se050_rng_ctx *se050_rng_alloc_pool(void) +{ + int idx = pool_alloc(&g_rng_pool, g_rng_bitmap); + if (idx < 0) return NULL; + + struct se050_rng_ctx *ctx = (struct se050_rng_ctx *) + (g_rng_pool.pool + (idx * g_rng_pool.item_size)); + memset(ctx, 0, SE050_RNG_CTX_SIZE); + return ctx; +} + +void se050_rng_free_pool(struct se050_rng_ctx *ctx) +{ + if (!ctx) return; + + size_t offset = (uint8_t *)ctx - g_rng_pool.pool; + if (offset < g_rng_pool.count * g_rng_pool.item_size) { + size_t idx = offset / g_rng_pool.item_size; + pool_zeroize(&g_rng_pool, g_rng_bitmap); + pool_free(&g_rng_pool, g_rng_bitmap, (int)idx); + } +} + +/* I2C HAL pool */ +struct se050_i2c_hal *se050_i2c_hal_alloc_pool(void) +{ + int idx = pool_alloc(&g_i2c_hal_pool, g_i2c_hal_bitmap); + if (idx < 0) return NULL; + + se050_i2c_hal_t *hal = (se050_i2c_hal_t *) + (g_i2c_hal_pool.pool + (idx * g_i2c_hal_pool.item_size)); + memset(hal, 0, sizeof(*hal)); + return (struct se050_i2c_hal *)hal; +} + +void se050_i2c_hal_free_pool(struct se050_i2c_hal *hal) +{ + if (!hal) return; + + size_t offset = (uint8_t *)hal - g_i2c_hal_pool.pool; + if (offset < g_i2c_hal_pool.count * g_i2c_hal_pool.item_size) { + size_t idx = offset / g_i2c_hal_pool.item_size; + pool_zeroize(&g_i2c_hal_pool, g_i2c_hal_bitmap); + pool_free(&g_i2c_hal_pool, g_i2c_hal_bitmap, (int)idx); + } +} + +/* Statistics */ +void se050_mem_pool_stats(se050_pool_stats_t *session, + se050_pool_stats_t *scp03, + se050_pool_stats_t *keystore, + se050_pool_stats_t *rng, + se050_pool_stats_t *i2c_hal) +{ + if (session) { + session->total = SE050_POOL_SESSION_COUNT; + session->used = atomic_load(&g_session_pool.used_count); + session->free = session->total - session->used; + } + + if (scp03) { + scp03->total = SE050_POOL_SCP03_COUNT; + scp03->used = atomic_load(&g_scp03_pool.used_count); + scp03->free = scp03->total - scp03->used; + } + + if (keystore) { + keystore->total = SE050_POOL_KEYSTORE_COUNT; + keystore->used = atomic_load(&g_keystore_pool.used_count); + keystore->free = keystore->total - keystore->used; + } + + if (rng) { + rng->total = SE050_POOL_RNG_COUNT; + rng->used = atomic_load(&g_rng_pool.used_count); + rng->free = rng->total - rng->used; + } + + if (i2c_hal) { + i2c_hal->total = SE050_POOL_I2C_HAL_COUNT; + i2c_hal->used = atomic_load(&g_i2c_hal_pool.used_count); + i2c_hal->free = i2c_hal->total - i2c_hal->used; + } +} diff --git a/src/se050_rng.c b/src/se050_rng.c index b929ec3..a68e8c2 100644 --- a/src/se050_rng.c +++ b/src/se050_rng.c @@ -9,11 +9,11 @@ #define _POSIX_C_SOURCE 200809L #include "se050_i2c_hal.h" +#include "se050_mem_pool.h" #include "se050_wireguard.h" #include "se050_crypto_utils.h" #include "se050_session_internal.h" #include -#include #include #include #include @@ -41,8 +41,8 @@ se050_status_t se050_rng_init(se050_rng_ctx_t **ctx, se050_session_ctx_t *sessio return SE050_ERR_INVALID_ARG; } - /* Allocate RNG context */ - rng = (se050_rng_ctx_t *)calloc(1, sizeof(*rng)); + /* Allocate RNG context from static pool */ + rng = se050_rng_alloc_pool(); if (!rng) { return SE050_ERR_FAIL; } @@ -70,8 +70,8 @@ void se050_rng_free(se050_rng_ctx_t *ctx) ctx->entropy_len = 0; } - /* Free RNG context */ - free(ctx); + /* Free RNG context to static pool */ + se050_rng_free_pool(ctx); } /* ============================================================================ diff --git a/src/se050_scp03.c b/src/se050_scp03.c index 3133fba..7379eae 100644 --- a/src/se050_scp03.c +++ b/src/se050_scp03.c @@ -13,11 +13,11 @@ #include "se050_i2c_hal.h" #include "se050_session_internal.h" #include "se050_scp03.h" +#include "se050_mem_pool.h" #include "se050_wireguard.h" #include "se050_crypto_utils.h" #include "se050_mem_protect.h" #include -#include #include /* SCP03 constants */ @@ -289,26 +289,17 @@ static se050_status_t scp03_derive_session_keys(se050_scp03_ctx_t *ctx) se050_status_t se050_scp03_init(se050_scp03_ctx_t **ctx, se050_session_ctx_t *session) { se050_scp03_ctx_t *scp03; - size_t ctx_size; if (!ctx || !session) { return SE050_ERR_INVALID_ARG; } - /* Allocate SCP03 context */ - scp03 = (se050_scp03_ctx_t *)calloc(1, sizeof(*scp03)); + /* Allocate SCP03 context from static pool */ + scp03 = se050_scp03_alloc_pool(); if (!scp03) { return SE050_ERR_FAIL; } - ctx_size = sizeof(*scp03); - - /* Apply memory protection (Linux only) */ - if (protect_sensitive_memory(scp03, ctx_size) != SE050_OK) { - free(scp03); - return SE050_ERR_FAIL; - } - scp03->session = session; scp03->cmd_counter = 0; scp03->rsp_counter = 0; @@ -340,11 +331,8 @@ void se050_scp03_free(se050_scp03_ctx_t *ctx) memzero_explicit(ctx->rsp_icv, sizeof(ctx->rsp_icv)); } - /* Release memory protection before freeing */ - release_memory_protection(ctx, sizeof(*ctx)); - - /* Free SCP03 context */ - free(ctx); + /* Free SCP03 context to static pool */ + se050_scp03_free_pool(ctx); } se050_status_t se050_scp03_set_keys(se050_scp03_ctx_t *ctx, diff --git a/src/se050_session.c b/src/se050_session.c index 311354b..859985a 100644 --- a/src/se050_session.c +++ b/src/se050_session.c @@ -10,10 +10,11 @@ #include "se050_i2c_hal.h" #include "se050_session_internal.h" +#include "se050_mem_pool.h" +#include "se050_scp03.h" #include "se050_wireguard.h" #include "se050_crypto_utils.h" #include -#include #include /* SCP03 status codes */ @@ -33,8 +34,8 @@ se050_status_t se050_session_create(se050_session_ctx_t **ctx, se050_i2c_hal_t * return SE050_ERR_INVALID_ARG; } - /* Allocate session context */ - session = (se050_session_ctx_t *)calloc(1, sizeof(*session)); + /* Allocate session context from static pool */ + session = se050_session_alloc_pool(); if (!session) { return SE050_ERR_FAIL; } @@ -145,7 +146,7 @@ void se050_session_delete(se050_session_ctx_t *ctx) /* Close SCP03 secure channel if initialized */ if (ctx->scp03) { - se050_scp03_free(ctx->scp03); + se050_scp03_free_pool(ctx->scp03); ctx->scp03 = NULL; } @@ -155,8 +156,8 @@ void se050_session_delete(se050_session_ctx_t *ctx) ctx->session_key_len = 0; } - /* Free session context */ - free(ctx); + /* Free session context to static pool */ + se050_session_free_pool(ctx); } /* ============================================================================