Remove dynamic memory allocation (malloc/calloc/free)

- Add static memory pool implementation (se050_mem_pool.c/h)
- Replace all malloc/calloc with pool allocations
- Replace all free with pool deallocations
- Remove strdup usage (use fixed-size buffer instead)
- Update I2C HAL to use fixed-size dev_path array
- All 24 tests pass with static memory only

Suitable for embedded environments (u-boot, ESP32) without heap.
This commit is contained in:
km
2026-03-29 19:07:57 +09:00
parent 479fcd37c1
commit 11bcc5e0c3
11 changed files with 447 additions and 79 deletions
+5 -17
View File
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 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,