|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +wolfHSM is a portable client-server framework for hardware cryptography, non-volatile memory (NVM), and isolated secure processing. The server runs in a trusted environment; clients communicate via a message-based protocol. wolfCrypt APIs are automatically offloaded to the server as remote procedure calls. Targeted at automotive HSM-enabled microcontrollers but runs on any platform with a secure execution environment. |
| 8 | + |
| 9 | +## Build Commands |
| 10 | + |
| 11 | +All builds use Make. wolfSSL must be available at `../../wolfssl` relative to this repo (override with `WOLFSSL_DIR=`). |
| 12 | + |
| 13 | +```bash |
| 14 | +# Build and run tests (from repo root or test/) |
| 15 | +cd test && make -j && make run |
| 16 | + |
| 17 | +# Common build flags (combine as needed) |
| 18 | +make DEBUG=1 # Basic debug output |
| 19 | +make DEBUG_VERBOSE=1 # Verbose debug (implies DEBUG) |
| 20 | +make ASAN=1 # Address Sanitizer |
| 21 | +make TSAN=1 # Thread Sanitizer (requires THREADSAFE=1) |
| 22 | +make THREADSAFE=1 # Thread-safe mode with locking |
| 23 | +make DMA=1 # DMA support |
| 24 | +make SHE=1 # AUTOSAR SHE extensions |
| 25 | +make TLS=1 # TLS transport |
| 26 | +make COVERAGE=1 # Code coverage instrumentation |
| 27 | +make TESTWOLFCRYPT=1 # Run wolfCrypt test suite as client |
| 28 | +make CLIENT_ONLY=1 # Client-only (connects to remote server) |
| 29 | +make STRESS=1 # Stress testing (requires THREADSAFE=1) |
| 30 | + |
| 31 | +# Coverage report |
| 32 | +cd test && make coverage # Builds, runs, generates ../coverage/index.html |
| 33 | + |
| 34 | +# Static analysis |
| 35 | +make scan # scan-build (excludes wolfSSL/wolfCrypt) |
| 36 | + |
| 37 | +# Benchmarks |
| 38 | +cd benchmark && make -j && make run |
| 39 | + |
| 40 | +# Examples |
| 41 | +cd examples/posix/wh_posix_server && make |
| 42 | +cd examples/posix/wh_posix_client && make |
| 43 | + |
| 44 | +# NVM tool |
| 45 | +cd tools/whnvmtool && make && make test |
| 46 | +``` |
| 47 | + |
| 48 | +## Code Formatting |
| 49 | + |
| 50 | +Uses clang-format-15 (CI enforced). 4-space indent, 80-column limit, braces on new line after functions only, `else` on new line. Pointer alignment left (`int* p`). Run: `clang-format-15 -i <file>`. |
| 51 | + |
| 52 | +## Architecture |
| 53 | + |
| 54 | +### Client-Server Model |
| 55 | + |
| 56 | +- **Client** (`src/wh_client*.c`, `wolfhsm/wh_client*.h`): Sends requests to the server. Registers wolfCrypt crypto callbacks so wolfCrypt calls are transparently offloaded. Device IDs: `WH_DEV_ID` (0x5748534D) for standard ops, `WH_DEV_ID_DMA` (0x57444D41) for DMA ops. |
| 57 | +- **Server** (`src/wh_server*.c`, `wolfhsm/wh_server*.h`): Dispatches incoming requests to handlers for crypto, NVM, keystore, SHE, certificates, image management, counters, and custom callbacks. |
| 58 | +- **Communication** (`src/wh_comm.c`, `wolfhsm/wh_comm.h`): 8-byte header + variable payload (default 1280 bytes, configurable via `WOLFHSM_CFG_COMM_DATA_LEN`). Message groups: COMM, NVM, KEY, CRYPTO, IMAGE, PKCS11, SHE, COUNTER, CUSTOM, CRYPTO_DMA, CERT. |
| 59 | + |
| 60 | +### Transport Layer |
| 61 | + |
| 62 | +Pluggable transport backends behind a common interface: |
| 63 | +- `wh_transport_mem` — shared memory buffer (used in tests) |
| 64 | +- POSIX port: shared memory (pthread), TCP sockets, TLS over TCP |
| 65 | + |
| 66 | +### Non-Volatile Memory (NVM) |
| 67 | + |
| 68 | +Log-based flash storage with crash recovery (`wh_nvm_flash_log.c`). Objects have metadata (ID, access, flags, label). RAM flash simulator (`wh_flash_ramsim.c`) used for testing. |
| 69 | + |
| 70 | +### Key Management |
| 71 | + |
| 72 | +RAM key cache + persistent NVM storage. Keys have access control (per-client), usage policies (encrypt/decrypt/sign/verify/wrap/derive), and flags (non-exportable, sensitive, non-modifiable, etc.). |
| 73 | + |
| 74 | +### Platform Ports |
| 75 | + |
| 76 | +`port/` contains platform-specific implementations. `port/posix/` is the reference port (flash sim, transport, locks, logging). `port/skeleton/` is a template for new ports. |
| 77 | + |
| 78 | +## Configuration |
| 79 | + |
| 80 | +Three-layer configuration (lowest to highest priority): |
| 81 | + |
| 82 | +1. **`wolfhsm/wh_settings.h`** — defaults for all `WOLFHSM_CFG_*` macros |
| 83 | +2. **`wolfhsm_cfg.h`** — per-project overrides (included when `WOLFHSM_CFG` is defined). Test version at `test/config/wolfhsm_cfg.h` |
| 84 | +3. **Compiler `-D` flags** — set via Makefile variables |
| 85 | + |
| 86 | +wolfCrypt is configured separately via `user_settings.h` (test version at `test/config/user_settings.h`). |
| 87 | + |
| 88 | +## Testing |
| 89 | + |
| 90 | +Tests are in `test/`. Each module has its own `wh_test_<module>.c` file with a corresponding header. The main driver is `wh_test.c`. Tests must be portable (no system dependencies) except POSIX-specific tests gated by `WOLFHSM_CFG_TEST_POSIX`. |
| 91 | + |
| 92 | +## Error Codes |
| 93 | + |
| 94 | +Defined in `wolfhsm/wh_error.h`: |
| 95 | +- General: -2000 to -2010 (BADARGS, NOTREADY, ABORTED, TIMEOUT, etc.) |
| 96 | +- NVM: -2100 to -2105 (LOCKED, ACCESS, NOTFOUND, NOSPACE, etc.) |
| 97 | +- SHE: -2200 to -2211 |
| 98 | + |
| 99 | +All wolfHSM functions return `int` where 0 is success (`WH_ERROR_OK`) and negative values are errors. |
0 commit comments