Skip to content

Commit 685bacc

Browse files
Merge pull request #9614 from dgarske/stsafe-a120
Add STSAFE-A120 Support
2 parents c8867d8 + 38b0fe1 commit 685bacc

File tree

6 files changed

+1865
-324
lines changed

6 files changed

+1865
-324
lines changed

.wolfssl_known_macro_extras

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ STM32WL55xx
545545
STM32_AESGCM_PARTIAL
546546
STM32_HW_CLOCK_AUTO
547547
STM32_NUTTX_RNG
548+
STSAFE_HOST_KEY_CIPHER
549+
STSAFE_HOST_KEY_MAC
550+
STSAFE_I2C_BUS
551+
STSE_CONF_ECC_BRAINPOOL_P_256
552+
STSE_CONF_ECC_BRAINPOOL_P_384
548553
TASK_EXTRA_STACK_SIZE
549554
TCP_NODELAY
550555
TFM_ALREADY_SET

wolfcrypt/src/port/st/README.md

Lines changed: 169 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ Support for the STM32 PKA on WB55, H7, MP13 and other devices with on-board
99
public-key acceleration:
1010
- ECC192/ECC224/ECC256/ECC384
1111

12-
Support for the STSAFE-A100 crypto hardware accelerator co-processor via I2C for ECC supporting NIST or Brainpool 256-bit and 384-bit curves. It requires the ST-Safe SDK including wolfSSL's `stsafe_interface.c/.h` files. Please contact us at support@wolfssl.com to get this code.
12+
Support for the STSAFE-A secure element family via I2C for ECC supporting NIST P-256/P-384 and Brainpool 256/384-bit curves:
13+
- **STSAFE-A100/A110**: Uses ST's proprietary STSAFE-A1xx middleware. Contact us at support@wolfssl.com for integration assistance.
14+
- **STSAFE-A120**: Uses ST's open-source [STSELib](https://github.com/STMicroelectronics/STSELib) (BSD-3 license).
1315

1416

1517
For details see our [wolfSSL ST](https://www.wolfssl.com/docs/stm32/) page.
@@ -65,50 +67,201 @@ To enable support define the following
6567

6668
When the support is enabled, the ECC operations will be accelerated using the PKA crypto co-processor.
6769

68-
## STSAFE-A100 ECC Acceleration
70+
## STSAFE-A ECC Acceleration
6971

70-
Using the wolfSSL PK callbacks and the reference ST Safe reference API's we support an ECC only cipher suite such as ECDHE-ECDSA-AES128-SHA256 for TLS client or server.
72+
Using the wolfSSL PK callbacks or Crypto callbacks with the ST-Safe reference API's we support ECC operations for TLS client/server:
73+
- **ECDSA Sign/Verify**: P-256 and P-384 (NIST and Brainpool curves)
74+
- **ECDH Key Agreement**: For TLS key exchange
75+
- **ECC Key Generation**: Ephemeral keys for TLS
7176

72-
At the wolfCrypt level we also support ECC native API's for `wc_ecc_*` using the ST-Safe.
77+
At the wolfCrypt level we also support ECC native API's for `wc_ecc_*` using the ST-Safe via Crypto Callbacks.
78+
79+
### Supported Hardware
80+
81+
| Model | Macro | SDK |
82+
|-------|-------|-----|
83+
| STSAFE-A100/A110 | `WOLFSSL_STSAFEA100` | ST STSAFE-A1xx Middleware (proprietary) |
84+
| STSAFE-A120 | `WOLFSSL_STSAFEA120` | [STSELib](https://github.com/STMicroelectronics/STSELib) (BSD-3, open source) |
7385

7486
### Building
7587

76-
`./configure --enable-pkcallbacks CFLAGS="-DWOLFSSL_STSAFEA100"`
88+
For STSAFE-A100/A110 (legacy):
7789

78-
or
90+
```
91+
./configure --enable-pkcallbacks CFLAGS="-DWOLFSSL_STSAFEA100"
92+
```
7993

80-
`#define HAVE_PK_CALLBACKS`
81-
`#define WOLFSSL_STSAFEA100`
94+
or in `user_settings.h`:
8295

96+
```c
97+
#define HAVE_PK_CALLBACKS
98+
#define WOLFSSL_STSAFEA100
99+
```
100+
101+
For STSAFE-A120 with STSELib:
102+
103+
```
104+
./configure --enable-pkcallbacks CFLAGS="-DWOLFSSL_STSAFEA120"
105+
```
106+
107+
or in `user_settings.h`:
108+
109+
```c
110+
#define HAVE_PK_CALLBACKS
111+
#define WOLFSSL_STSAFEA120
112+
```
113+
114+
To use Crypto Callbacks (recommended for wolfCrypt-level ECC operations):
115+
116+
```c
117+
#define WOLF_CRYPTO_CB
118+
#define WOLFSSL_STSAFEA120 /* or WOLFSSL_STSAFEA100 */
119+
```
83120
84121
### Coding
85122
123+
#### Using PK Callbacks (TLS)
124+
86125
Setup the PK callbacks for TLS using:
87126
88-
```
89-
/* Setup PK Callbacks for STSAFE-A100 */
127+
```c
128+
/* Setup PK Callbacks for STSAFE */
90129
WOLFSSL_CTX* ctx;
130+
SSL_STSAFE_SetupPkCallbacks(ctx);
131+
132+
/* Or manually: */
91133
wolfSSL_CTX_SetEccKeyGenCb(ctx, SSL_STSAFE_CreateKeyCb);
92134
wolfSSL_CTX_SetEccSignCb(ctx, SSL_STSAFE_SignCertificateCb);
93135
wolfSSL_CTX_SetEccVerifyCb(ctx, SSL_STSAFE_VerifyPeerCertCb);
94136
wolfSSL_CTX_SetEccSharedSecretCb(ctx, SSL_STSAFE_SharedSecretCb);
95137
wolfSSL_CTX_SetDevId(ctx, 0); /* enables wolfCrypt `wc_ecc_*` ST-Safe use */
96138
```
97139

98-
The reference STSAFE-A100 PK callback functions are located in the `wolfcrypt/src/port/st/stsafe.c` file.
140+
The reference STSAFE PK callback functions are located in the `wolfcrypt/src/port/st/stsafe.c` file.
99141

100142
Adding a custom context to the callbacks:
101143

102-
```
144+
```c
103145
/* Setup PK Callbacks context */
104146
WOLFSSL* ssl;
105147
void* myOwnCtx;
106-
wolfSSL_SetEccKeyGenCtx(ssl, myOwnCtx);
107-
wolfSSL_SetEccVerifyCtx(ssl, myOwnCtx);
108-
wolfSSL_SetEccSignCtx(ssl, myOwnCtx);
109-
wolfSSL_SetEccSharedSecretCtx(ssl, myOwnCtx);
148+
SSL_STSAFE_SetupPkCallbackCtx(ssl, myOwnCtx);
149+
```
150+
151+
#### Using Crypto Callbacks (wolfCrypt)
152+
153+
For direct wolfCrypt ECC operations using the hardware:
154+
155+
```c
156+
#include <wolfssl/wolfcrypt/port/st/stsafe.h>
157+
158+
/* Register the crypto callback */
159+
wolfSTSAFE_CryptoCb_Ctx stsafeCtx;
160+
stsafeCtx.devId = WOLF_STSAFE_DEVID;
161+
wc_CryptoCb_RegisterDevice(WOLF_STSAFE_DEVID, wolfSSL_STSAFE_CryptoDevCb, &stsafeCtx);
162+
163+
/* Use with ECC operations */
164+
ecc_key key;
165+
wc_ecc_init_ex(&key, NULL, WOLF_STSAFE_DEVID);
166+
/* ECC operations will now use STSAFE hardware */
110167
```
111168

169+
### Implementation Details
170+
171+
The STSAFE support is self-contained in `wolfcrypt/src/port/st/stsafe.c` with SDK-specific implementations selected at compile time:
172+
173+
| Macro | SDK | Description |
174+
|-------|-----|-------------|
175+
| `WOLFSSL_STSAFEA100` | STSAFE-A1xx Middleware | ST's proprietary SDK for A100/A110 |
176+
| `WOLFSSL_STSAFEA120` | [STSELib](https://github.com/STMicroelectronics/STSELib) | ST's open-source SDK for A120 (BSD-3) |
177+
178+
#### External Interface (Backwards Compatibility)
179+
180+
For customers with existing custom implementations, define `WOLFSSL_STSAFE_INTERFACE_EXTERNAL` to use an external `stsafe_interface.h` file instead of the built-in implementation:
181+
182+
```c
183+
#define WOLFSSL_STSAFEA100 /* or WOLFSSL_STSAFEA120 */
184+
#define WOLFSSL_STSAFE_INTERFACE_EXTERNAL
185+
```
186+
187+
When `WOLFSSL_STSAFE_INTERFACE_EXTERNAL` is defined, the customer must provide a `stsafe_interface.h` header that defines:
188+
189+
| Item | Type | Description |
190+
|------|------|-------------|
191+
| `stsafe_curve_id_t` | typedef | Curve identifier type |
192+
| `stsafe_slot_t` | typedef | Key slot identifier type |
193+
| `STSAFE_ECC_CURVE_P256` | macro | P-256 curve ID value |
194+
| `STSAFE_ECC_CURVE_P384` | macro | P-384 curve ID value |
195+
| `STSAFE_KEY_SLOT_0/1/EPHEMERAL` | macros | Key slot values |
196+
| `STSAFE_A_OK` | macro | Success return code |
197+
| `STSAFE_MAX_KEY_LEN` | macro | Max key size in bytes (48) |
198+
| `STSAFE_MAX_PUBKEY_RAW_LEN` | macro | Max public key size (96) |
199+
| `STSAFE_MAX_SIG_LEN` | macro | Max signature size (96) |
200+
201+
And provide implementations for these internal interface functions:
202+
- `int stsafe_interface_init(void)`
203+
- `int stsafe_create_key(stsafe_slot_t*, stsafe_curve_id_t, uint8_t*)`
204+
- `int stsafe_sign(stsafe_slot_t, stsafe_curve_id_t, uint8_t*, uint8_t*)`
205+
- `int stsafe_verify(stsafe_curve_id_t, uint8_t*, uint8_t*, uint8_t*, uint8_t*, int32_t*)`
206+
- `int stsafe_shared_secret(stsafe_slot_t, stsafe_curve_id_t, uint8_t*, uint8_t*, uint8_t*, int32_t*)`
207+
- `int stsafe_read_certificate(uint8_t**, uint32_t*)`
208+
- `int stsafe_get_random(uint8_t*, uint32_t)` (if `USE_STSAFE_RNG_SEED` defined)
209+
210+
When **NOT** defined (default behavior): All code is self-contained in `stsafe.c` using the appropriate SDK automatically.
211+
212+
The implementation provides these internal operations:
213+
214+
| Operation | Description |
215+
|-----------|-------------|
216+
| `stsafe_interface_init()` | Initialize the STSAFE device (called by `wolfCrypt_Init()`) |
217+
| `stsafe_sign()` | ECDSA signature generation (P-256/P-384) |
218+
| `stsafe_verify()` | ECDSA signature verification (P-256/P-384) |
219+
| `stsafe_create_key()` | Generate ECC key pair on device |
220+
| `stsafe_shared_secret()` | ECDH shared secret computation |
221+
| `stsafe_read_certificate()` | Read device certificate from secure storage |
222+
223+
### STSELib Setup (A120)
224+
225+
For STSAFE-A120, you need to include the STSELib library:
226+
227+
1. Clone STSELib as a submodule or add to your project:
228+
```bash
229+
git submodule add https://github.com/STMicroelectronics/STSELib.git lib/stselib
230+
```
231+
232+
2. Add STSELib headers to your include path
233+
234+
3. Implement the platform abstraction files required by STSELib:
235+
- `stse_conf.h` - Configuration (target device, features)
236+
- `stse_platform_generic.h` - Platform callbacks (I2C, timing)
237+
238+
4. See STSELib documentation for platform-specific integration details
239+
240+
### Raspberry Pi with STSAFE-A120
241+
242+
For testing on a Raspberry Pi with an STSAFE-A120 connected via I2C:
243+
244+
1. **Enable I2C** on the Raspberry Pi:
245+
```bash
246+
sudo raspi-config
247+
# Navigate to: Interface Options -> I2C -> Enable
248+
```
249+
250+
2. **Verify the STSAFE device is detected** (default I2C address is 0x20):
251+
```bash
252+
sudo i2cdetect -y 1
253+
```
254+
255+
3. **Build wolfSSL with STSAFE-A120 support**:
256+
```bash
257+
./configure --enable-pkcallbacks --enable-cryptocb \
258+
CFLAGS="-DWOLFSSL_STSAFEA120 -I/path/to/STSELib"
259+
make
260+
sudo make install
261+
```
262+
263+
4. **Platform abstraction**: Implement the STSELib I2C callbacks using the Linux I2C driver (`/dev/i2c-1`).
264+
112265
### Benchmarks and Memory Use
113266

114267
Software only implementation (STM32L4 120Mhz, Cortex-M4, Fast Math):

0 commit comments

Comments
 (0)