Skip to content

Commit 9ada2ac

Browse files
Merge pull request #553 from dgarske/stsafe-a120
Example for ST-SAFE A120
2 parents 4561614 + 22e2aa3 commit 9ada2ac

11 files changed

Lines changed: 4566 additions & 0 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,8 @@ kernel/bsdkm/bsd_example.ko
402402
kernel/bsdkm/machine
403403
kernel/bsdkm/opt_global.h
404404
kernel/bsdkm/x86
405+
406+
# STSAFE test executables
407+
stsafe/stsafe_test
408+
stsafe/wolfssl_stsafe_test
409+
stsafe/wolfssl_stsafe_full_test

stsafe/Makefile

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# STSAFE-A120 Test Makefile for Raspberry Pi
2+
#
3+
# Copyright 2025 wolfSSL Inc.
4+
5+
CC = gcc
6+
CFLAGS = -Wall -Wextra -O0 -g -Wno-strict-prototypes -DWOLFSSL_USER_SETTINGS
7+
8+
# wolfSSL configuration
9+
# Option 1: Use installed wolfSSL (after make install)
10+
#WOLFSSL_DIR = /usr/local
11+
# Option 2: Use local wolfSSL source
12+
WOLFSSL_DIR = $(HOME)/wolfssl
13+
14+
# STSELib paths
15+
STSELIB_DIR = $(HOME)/STSELib
16+
PLATFORM_DIR = ./platform
17+
18+
# Include paths
19+
# Note: Current directory (.) must come first so our user_settings.h is found
20+
INCLUDES = -I. \
21+
-I$(STSELIB_DIR) \
22+
-I$(PLATFORM_DIR) \
23+
-I$(WOLFSSL_DIR)
24+
25+
# Build directory for artifacts
26+
BUILD_DIR = build
27+
28+
# wolfSSL source files - build directly from source
29+
WOLFSSL_SRC = $(wildcard $(WOLFSSL_DIR)/src/*.c)
30+
WOLFSSL_CRYPT_SRC = $(wildcard $(WOLFSSL_DIR)/wolfcrypt/src/*.c)
31+
WOLFSSL_PORT_SRC = $(WOLFSSL_DIR)/wolfcrypt/src/port/st/stsafe.c
32+
WOLFSSL_ALL_SRC = $(WOLFSSL_SRC) $(WOLFSSL_CRYPT_SRC) $(WOLFSSL_PORT_SRC)
33+
34+
# wolfSSL static library
35+
WOLFSSL_LIB = $(BUILD_DIR)/libwolfssl.a
36+
37+
# Additional linker flags
38+
LDFLAGS = -L$(BUILD_DIR) -lwolfssl -lm
39+
40+
# STSELib source files
41+
STSELIB_CORE_SRC = $(STSELIB_DIR)/core/stse_device.c \
42+
$(STSELIB_DIR)/core/stse_frame.c \
43+
$(STSELIB_DIR)/core/stse_generic_typedef.c \
44+
$(STSELIB_DIR)/core/stse_platform.c \
45+
$(STSELIB_DIR)/core/stse_session.c
46+
47+
STSELIB_API_SRC = $(wildcard $(STSELIB_DIR)/api/*.c)
48+
STSELIB_SERVICES_SRC = $(wildcard $(STSELIB_DIR)/services/stsafea/*.c) \
49+
$(wildcard $(STSELIB_DIR)/services/stsafel/*.c)
50+
STSELIB_CERT_SRC = $(wildcard $(STSELIB_DIR)/certificate/*.c)
51+
52+
# Platform source files
53+
PLATFORM_SRC = $(PLATFORM_DIR)/stse_platform_linux.c
54+
55+
# Platform crypto source (wolfSSL implementation)
56+
# Uncomment when building with wolfSSL crypto support:
57+
PLATFORM_CRYPTO_SRC = $(PLATFORM_DIR)/stse_platform_crypto_wolfssl.c
58+
59+
# Targets
60+
STSAFE_TARGET = stsafe_test
61+
WOLFSSL_TARGET = wolfssl_stsafe_test
62+
WOLFSSL_FULL_TARGET = wolfssl_stsafe_full_test
63+
ALL_TARGETS = $(STSAFE_TARGET) $(WOLFSSL_TARGET) $(WOLFSSL_FULL_TARGET)
64+
65+
.PHONY: all clean test-all info
66+
67+
all: $(WOLFSSL_LIB) $(ALL_TARGETS)
68+
69+
# Create build directory
70+
$(BUILD_DIR):
71+
mkdir -p $(BUILD_DIR)
72+
73+
# Build wolfSSL static library
74+
$(WOLFSSL_LIB): $(WOLFSSL_ALL_SRC) | $(BUILD_DIR)
75+
@echo "Building wolfSSL library..."
76+
$(CC) $(CFLAGS) $(INCLUDES) -c $(WOLFSSL_ALL_SRC)
77+
mv *.o $(BUILD_DIR)/
78+
ar rcs $@ $(BUILD_DIR)/*.o
79+
@echo "wolfSSL library built successfully"
80+
81+
# Generic rule for all test targets - each target depends on its corresponding .c file
82+
$(ALL_TARGETS): %: %.c $(WOLFSSL_LIB)
83+
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $< \
84+
$(PLATFORM_SRC) $(PLATFORM_CRYPTO_SRC) \
85+
$(STSELIB_CORE_SRC) $(STSELIB_API_SRC) $(STSELIB_SERVICES_SRC) $(STSELIB_CERT_SRC) \
86+
$(LDFLAGS)
87+
88+
test-all: all
89+
@echo "=== Running stsafe_test ===" && ./$(STSAFE_TARGET)
90+
@echo ""
91+
@echo "=== Running wolfssl_stsafe_test ===" && ./$(WOLFSSL_TARGET)
92+
@echo ""
93+
@echo "=== Running wolfssl_stsafe_full_test ===" && ./$(WOLFSSL_FULL_TARGET)
94+
95+
clean:
96+
rm -rf $(BUILD_DIR) $(ALL_TARGETS)
97+
98+
# Show configuration
99+
info:
100+
@echo "STSELib directory: $(STSELIB_DIR)"
101+
@echo "wolfSSL directory: $(WOLFSSL_DIR)"
102+
@echo "Platform directory: $(PLATFORM_DIR)"
103+
@echo "Include paths: $(INCLUDES)"
104+
@echo "Linker flags: $(LDFLAGS)"
105+
@echo ""
106+
@echo "Build targets:"
107+
@echo " make - Build all test executables"
108+
@echo " make test-all - Build and run all tests"
109+
@echo " make clean - Clean build artifacts"
110+
@echo " make info - Show this configuration"

stsafe/README.md

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
# wolfSSL STSAFE-A120 Test Suite
2+
3+
Test harness for wolfSSL integration with ST STSAFE-A120 secure element on Raspberry Pi 5.
4+
5+
## Hardware Requirements
6+
7+
- Raspberry Pi 5 (or compatible Linux system with I2C)
8+
- STSAFE-A120 secure element connected via I2C
9+
- I2C enabled on the system
10+
11+
## Software Requirements
12+
13+
- wolfSSL library (compiled with ECC, CMAC, SHA-384 support)
14+
- STSELib (ST Secure Element Library)
15+
- GCC compiler
16+
- Linux I2C development headers (`libi2c-dev`)
17+
18+
## Quick Start
19+
20+
### 1. Enable I2C on Raspberry Pi
21+
22+
```bash
23+
sudo raspi-config
24+
# Navigate to: Interface Options -> I2C -> Enable
25+
```
26+
27+
Reboot if prompted, then verify the STSAFE device is detected:
28+
29+
```bash
30+
sudo i2cdetect -y 1
31+
# STSAFE should appear at address 0x20
32+
```
33+
34+
Optional: Allow non-root I2C access:
35+
```bash
36+
sudo usermod -a -G i2c $USER
37+
# Logout and login again
38+
```
39+
40+
### 2. Build wolfSSL
41+
42+
```bash
43+
cd ~/wolfssl
44+
./configure --enable-cryptocb --enable-ecc --enable-cmac --enable-sha384
45+
make
46+
```
47+
48+
### 3. Clone STSELib
49+
50+
```bash
51+
git clone https://github.com/STMicroelectronics/STSELib.git ~/STSELib
52+
```
53+
54+
### 4. Build and Run Tests
55+
56+
```bash
57+
cd ~/wolfssl-examples/stsafe
58+
59+
# Build all test executables
60+
make
61+
62+
# Run all tests
63+
make test-all
64+
```
65+
66+
## Test Suites
67+
68+
### Basic STSELib Tests (`stsafe_test`)
69+
70+
Tests core STSAFE-A120 functionality:
71+
- Echo command (I2C communication)
72+
- Random number generation
73+
- ECC P-256 key generation
74+
- ECDSA P-256 signing
75+
- ECC P-384 key generation
76+
77+
```bash
78+
make && ./stsafe_test
79+
```
80+
81+
### wolfSSL Crypto Callback Tests (`wolfssl_stsafe_test`)
82+
83+
Tests wolfSSL crypto callbacks with STSAFE:
84+
- RNG with STSAFE-A120
85+
- ECC P-256/P-384 key generation via crypto callback
86+
- ECDSA P-256/P-384 sign/verify
87+
- ECDHE P-256 ephemeral key generation
88+
- ECDHE P-256 shared secret computation
89+
90+
```bash
91+
make wolfssl && ./wolfssl_stsafe_test
92+
```
93+
94+
### Full Integration Tests (`wolfssl_stsafe_full_test`)
95+
96+
Comprehensive tests with benchmarks:
97+
- RNG benchmark
98+
- ECDSA P-256 benchmark (keygen, sign, verify timing)
99+
- ECDH P-256 key exchange (uses ECDHE ephemeral keys)
100+
- Multiple sequential operations
101+
102+
```bash
103+
make wolfssl-full && ./wolfssl_stsafe_full_test
104+
```
105+
106+
## Build Targets
107+
108+
| Target | Description |
109+
|--------|-------------|
110+
| `make` | Build all test executables |
111+
| `make test-all` | Build and run all tests |
112+
| `make basic` | Build without wolfSSL (basic I2C tests only) |
113+
| `make clean` | Clean build artifacts |
114+
| `make info` | Show configuration |
115+
116+
## Expected Output
117+
118+
```
119+
================================================
120+
STSAFE-A120 Test Suite for wolfSSL Integration
121+
================================================
122+
123+
Initializing STSAFE handler...
124+
STSAFE-A120 initialized successfully.
125+
126+
Test: Echo Command
127+
Echo response matches!
128+
[PASS] Echo command
129+
130+
Test: Random Number Generation
131+
Random data: A1 B2 C3 D4 ...
132+
[PASS] Random number generation
133+
134+
Test: ECC Key Generation (P-256)
135+
Public Key X: 12345678...
136+
Public Key Y: ABCDEF01...
137+
[PASS] ECC P-256 key generation
138+
139+
...
140+
141+
================================================
142+
Test Summary: 5 passed, 0 failed
143+
================================================
144+
```
145+
146+
## Performance Results (Raspberry Pi 5)
147+
148+
| Operation | Time | Throughput |
149+
|-----------|------|------------|
150+
| RNG (256 bytes) | <1 ms | ~9 MB/s |
151+
| ECC P-256 KeyGen | ~40 ms | 25 ops/sec |
152+
| ECDSA P-256 Sign | ~51 ms | 19.5 ops/sec |
153+
| ECDSA P-256 Verify | ~79 ms | 12.7 ops/sec |
154+
| ECDHE P-256 KeyGen | ~42 ms | ~24 ops/sec |
155+
| ECDHE P-256 Shared Secret | ~38 ms | ~26 ops/sec |
156+
157+
## Directory Structure
158+
159+
```
160+
stsafe/
161+
├── Makefile # Build configuration
162+
├── README.md # This file
163+
├── user_settings.h # wolfSSL configuration
164+
├── stsafe_test.c # STSELib basic tests
165+
├── wolfssl_stsafe_test.c # wolfSSL crypto callback tests
166+
├── wolfssl_stsafe_full_test.c # Full integration tests with benchmarks
167+
└── platform/
168+
├── stse_conf.h # STSELib configuration
169+
├── stse_platform_generic.h # Platform type definitions
170+
├── stse_platform_linux.c # Linux I2C platform implementation
171+
└── stse_platform_crypto_wolfssl.c # wolfSSL crypto for STSELib
172+
```
173+
174+
## Environment Variables
175+
176+
Default paths assume `~/wolfssl` and `~/STSELib`. Override if needed:
177+
178+
```bash
179+
export WOLFSSL_DIR=/path/to/wolfssl
180+
export STSELIB_DIR=/path/to/STSELib
181+
```
182+
183+
## Troubleshooting
184+
185+
### Error 0x0104 (STSE_PLATFORM_BUS_ERR)
186+
187+
I2C communication error. Check:
188+
1. I2C is enabled: `ls /dev/i2c*`
189+
2. Device detected: `sudo i2cdetect -y 1` (should show `20`)
190+
3. Wiring connections are secure
191+
192+
### Build errors
193+
194+
Ensure wolfSSL is built with required features:
195+
```bash
196+
./configure --enable-cryptocb --enable-ecc --enable-cmac --enable-sha384
197+
```
198+
199+
## References
200+
201+
- [wolfSSL Documentation](https://www.wolfssl.com/docs/)
202+
- [STSELib GitHub](https://github.com/STMicroelectronics/STSELib)
203+
- [STSAFE-A120 Datasheet](https://www.st.com/en/secure-mcus/stsafe-a120.html)
204+
- [Raspberry Pi I2C Documentation](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html)

0 commit comments

Comments
 (0)