Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion boards/stm32wb55xx_nucleo/Makefile.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
_BOARD_DIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))

PLATFORM = stm32wb
TESTS ?= clock gpio flash timer rng
TESTS ?= clock gpio flash timer rng crypto

GCC = $(GCC_PATH)arm-none-eabi-gcc
LD = $(GCC_PATH)arm-none-eabi-ld
Expand All @@ -26,5 +26,6 @@ BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/timer.c)
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/supply.c)
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/flash.c)
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/rng.c)
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/crypto.c)
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/stm32wb_*.c)
BOARD_SOURCE += $(wildcard $(WHAL_DIR)/src/*/systick.c)
32 changes: 32 additions & 0 deletions boards/stm32wb55xx_nucleo/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ whal_Rng g_whalRng = {
},
};

/* Crypto */
static const whal_Crypto_OpFunc cryptoOps[BOARD_CRYPTO_OP_COUNT] = {
[BOARD_CRYPTO_AES_ECB] = whal_Stm32wbAes_AesEcb,
[BOARD_CRYPTO_AES_CBC] = whal_Stm32wbAes_AesCbc,
[BOARD_CRYPTO_AES_CTR] = whal_Stm32wbAes_AesCtr,
[BOARD_CRYPTO_AES_GCM] = whal_Stm32wbAes_AesGcm,
[BOARD_CRYPTO_AES_GMAC] = whal_Stm32wbAes_AesGmac,
[BOARD_CRYPTO_AES_CCM] = whal_Stm32wbAes_AesCcm,
};

whal_Crypto g_whalCrypto = {
WHAL_STM32WB55_AES1_DEVICE,

.ops = cryptoOps,
.opsCount = BOARD_CRYPTO_OP_COUNT,

.cfg = &(whal_Stm32wbAes_Cfg) {
.clkCtrl = &g_whalClock,
.clk = &(whal_Stm32wbRcc_Clk) {WHAL_STM32WB55_AES1_CLOCK},
},
};

/* SysTick timing */
volatile size_t g_tick = 0;
volatile uint8_t g_waiting = 0;
Expand Down Expand Up @@ -191,6 +213,11 @@ whal_Error Board_Init(void)
return err;
}

err = whal_Crypto_Init(&g_whalCrypto);
if (err) {
return err;
}

err = whal_Timer_Init(&g_whalTimer);
if (err) {
return err;
Expand Down Expand Up @@ -218,6 +245,11 @@ whal_Error Board_Deinit(void)
return err;
}

err = whal_Crypto_Deinit(&g_whalCrypto);
if (err) {
return err;
}

err = whal_Rng_Deinit(&g_whalRng);
if (err) {
return err;
Expand Down
11 changes: 11 additions & 0 deletions boards/stm32wb55xx_nucleo/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ extern whal_Timer g_whalTimer;
extern whal_Uart g_whalUart;
extern whal_Flash g_whalFlash;
extern whal_Rng g_whalRng;
extern whal_Crypto g_whalCrypto;

extern volatile size_t g_tick;

#define BOARD_LED_PIN 0
#define BOARD_FLASH_TEST_ADDR 0x08080000
#define BOARD_FLASH_SECTOR_SZ 0x1000

enum {
BOARD_CRYPTO_AES_ECB,
BOARD_CRYPTO_AES_CBC,
BOARD_CRYPTO_AES_CTR,
BOARD_CRYPTO_AES_GCM,
BOARD_CRYPTO_AES_GMAC,
BOARD_CRYPTO_AES_CCM,
BOARD_CRYPTO_OP_COUNT,
};

whal_Error Board_Init(void);
whal_Error Board_Deinit(void);
void Board_WaitMs(size_t ms);
Expand Down
2 changes: 2 additions & 0 deletions docs/adding_a_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Tests use the macros defined in `tests/test.h`:
- `WHAL_ASSERT_NEQ(a, b)` — assert not equal
- `WHAL_ASSERT_MEM_EQ(a, b, len)` — assert memory regions are equal, reports
the byte offset of the first mismatch
- `WHAL_SKIP()` — skip the current test (marks it as SKIP instead of
PASS/FAIL and returns early)

Test functions take no arguments and return void. On assertion failure the
function returns early and the test is marked as failed.
Expand Down
69 changes: 69 additions & 0 deletions docs/writing_a_driver.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,75 @@ consumption and avoid unnecessary entropy source wear.

---

## Crypto

Header: `wolfHAL/crypto/crypto.h`

The crypto driver provides access to hardware cryptographic accelerators. Unlike
other device types, the crypto driver uses an **ops table** dispatch model
instead of a fixed vtable — each supported algorithm is a function pointer in a
board-defined ops table, indexed by a board-defined enum. This allows different
platforms to expose different subsets of algorithms without changing the generic
interface.

### Device Struct

The crypto device struct extends the standard model with an ops table:

```c
struct whal_Crypto {
const whal_Regmap regmap;
const whal_CryptoDriver *driver;
const whal_Crypto_OpFunc *ops;
size_t opsCount;
const void *cfg;
};
```

The `driver` vtable handles Init/Deinit. The `ops` table maps algorithm indices
to operation functions. The board defines the enum values and populates the ops
table.

### Init / Deinit

Init should enable the peripheral clock. Deinit should disable the AES
peripheral and its clock.

### Operations

Each operation function has the signature:

```c
whal_Error myOp(whal_Crypto *cryptoDev, void *opArgs);
```

The `opArgs` parameter is a pointer to an algorithm-specific argument struct
(e.g., `whal_Crypto_AesEcbArgs`, `whal_Crypto_AesGcmArgs`). The operation
function casts it to the correct type. See `wolfHAL/crypto/crypto.h` for the
full set of argument structs.

### Board Integration

The board defines an enum of supported operations and a corresponding ops table:

```c
enum {
BOARD_CRYPTO_AES_ECB,
BOARD_CRYPTO_AES_CBC,
BOARD_CRYPTO_OP_COUNT,
};

static const whal_Crypto_OpFunc cryptoOps[BOARD_CRYPTO_OP_COUNT] = {
[BOARD_CRYPTO_AES_ECB] = whal_Stm32wbAes_AesEcb,
[BOARD_CRYPTO_AES_CBC] = whal_Stm32wbAes_AesCbc,
};
```

Callers use `whal_Crypto_Op(&g_whalCrypto, BOARD_CRYPTO_AES_ECB, &args)` to
invoke an operation.

---

## Supply

Header: `wolfHAL/supply/supply.h`
Expand Down
33 changes: 33 additions & 0 deletions src/crypto/crypto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <wolfHAL/crypto/crypto.h>
#include <wolfHAL/error.h>

whal_Error whal_Crypto_Init(whal_Crypto *cryptoDev)
{
if (!cryptoDev || !cryptoDev->driver || !cryptoDev->driver->Init) {
return WHAL_EINVAL;
}

return cryptoDev->driver->Init(cryptoDev);
}

whal_Error whal_Crypto_Deinit(whal_Crypto *cryptoDev)
{
if (!cryptoDev || !cryptoDev->driver || !cryptoDev->driver->Deinit) {
return WHAL_EINVAL;
}

return cryptoDev->driver->Deinit(cryptoDev);
}

whal_Error whal_Crypto_Op(whal_Crypto *cryptoDev, size_t op, void *opArgs)
{
if (!cryptoDev || !cryptoDev->ops || !opArgs) {
return WHAL_EINVAL;
}

if (op >= cryptoDev->opsCount || !cryptoDev->ops[op]) {
return WHAL_EINVAL;
}

return cryptoDev->ops[op](cryptoDev, opArgs);
}
Loading
Loading