Skip to content

Commit 2fb19f8

Browse files
committed
Fixes for STSAFE-A120 ECDHE
1 parent 62ca344 commit 2fb19f8

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

wolfcrypt/src/port/st/README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,22 @@ wolfSTSAFE_CryptoCb_Ctx stsafeCtx;
160160
stsafeCtx.devId = WOLF_STSAFE_DEVID;
161161
wc_CryptoCb_RegisterDevice(WOLF_STSAFE_DEVID, wolfSSL_STSAFE_CryptoDevCb, &stsafeCtx);
162162
163-
/* Use with ECC operations */
163+
/* For ECDSA signing operations (uses persistent slot 1) */
164164
ecc_key key;
165165
wc_ecc_init_ex(&key, NULL, WOLF_STSAFE_DEVID);
166-
/* ECC operations will now use STSAFE hardware */
166+
wc_ecc_make_key_ex(&rng, 32, &key, ECC_SECP256R1);
167+
/* Sign operations will use STSAFE hardware */
168+
169+
/* For ECDH operations (uses ephemeral slot 0xFF) */
170+
ecc_key ecdh_key;
171+
wc_ecc_init_ex(&ecdh_key, NULL, WOLF_STSAFE_DEVID);
172+
ecdh_key.devCtx = (void*)(uintptr_t)STSAFE_KEY_SLOT_EPHEMERAL; /* Configure for ECDH */
173+
wc_ecc_make_key_ex(&rng, 32, &ecdh_key, ECC_SECP256R1);
174+
/* ECDH shared secret computation will use STSAFE hardware */
167175
```
168176

177+
**Note for STSAFE-A120**: ECDH operations require keys generated in the ephemeral slot (0xFF) which has key establishment enabled by default. Set `key.devCtx = (void*)(uintptr_t)STSAFE_KEY_SLOT_EPHEMERAL;` to configure keys for ECDH before generation. Persistent slots (0-4) require explicit configuration via `put_attribute` command to enable key establishment.
178+
169179
### Implementation Details
170180

171181
The STSAFE support is self-contained in `wolfcrypt/src/port/st/stsafe.c` with SDK-specific implementations selected at compile time:

wolfcrypt/src/port/st/stsafe.c

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,12 @@ int stsafe_interface_init(void)
273273

274274
/**
275275
* \brief Generate ECC key pair on STSAFE-A120
276-
* \details Uses dedicated key slot (slot 1) for persistent keys.
276+
* \details Uses dedicated key slot for persistent keys (typically slot 0 or 1).
277277
* For ephemeral ECDHE keys, use stsafe_create_ecdhe_key() instead.
278+
*
279+
* Note: For ECDH operations on persistent slots, the key must be generated
280+
* with appropriate usage settings. Per ST FAE: slot 0xFF with usage_limit=1
281+
* is recommended for ephemeral ECDH (key establishment mode).
278282
*/
279283
static int stsafe_create_key(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
280284
uint8_t* pPubKeyRaw)
@@ -288,7 +292,11 @@ static int stsafe_create_key(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
288292

289293
/* Generate key pair - public key is X||Y concatenated
290294
* Note: stse_generate_ecc_key_pair expects stse_ecc_key_type_t,
291-
* but stsafe_curve_id_t values match stse_ecc_key_type_t enum values */
295+
* but stsafe_curve_id_t values match stse_ecc_key_type_t enum values.
296+
*
297+
* For persistent keys: usage_limit=255 allows multiple operations (signing)
298+
* For ephemeral keys (slot 0xFF): usage_limit=1 for key establishment mode
299+
*/
292300
ret = stse_generate_ecc_key_pair(&g_stse_handler, slot,
293301
(stse_ecc_key_type_t)curve_id,
294302
STSAFE_PERSISTENT_KEY_USAGE_LIMIT,
@@ -303,10 +311,11 @@ static int stsafe_create_key(stsafe_slot_t slot, stsafe_curve_id_t curve_id,
303311

304312
/**
305313
* \brief Generate ECDHE ephemeral key pair on STSAFE-A120
306-
* \details Uses stse_generate_ECDHE_key_pair() which generates truly
307-
* ephemeral keys (not stored in slots). The private key remains
308-
* in STSE internal memory for use with shared secret computation.
309-
* Public key is returned in X||Y format (same as stse_generate_ecc_key_pair).
314+
* \details Uses stse_generate_ecc_key_pair() with slot 0xFF (ephemeral slot)
315+
* and usage_limit=1 for key establishment mode.
316+
* Per ST FAE recommendation: slot 0xFF must be used with mode of
317+
* operation = key establishment and usage limit = 1 for ECDH operations.
318+
* Public key is returned in X||Y format.
310319
*/
311320
static int stsafe_create_ecdhe_key(stsafe_curve_id_t curve_id,
312321
uint8_t* pPubKeyRaw)
@@ -318,11 +327,15 @@ static int stsafe_create_ecdhe_key(stsafe_curve_id_t curve_id,
318327
return BAD_FUNC_ARG;
319328
}
320329

321-
/* Generate ECDHE ephemeral key pair - public key returned as X||Y */
322-
ret = stse_generate_ECDHE_key_pair(&g_stse_handler,
323-
(stse_ecc_key_type_t)curve_id, pPubKeyRaw);
330+
/* Generate ephemeral key pair in slot 0xFF with usage_limit=1
331+
* This configures the key for key establishment mode */
332+
ret = stse_generate_ecc_key_pair(&g_stse_handler,
333+
STSAFE_KEY_SLOT_EPHEMERAL, /* slot 0xFF */
334+
(stse_ecc_key_type_t)curve_id,
335+
STSAFE_EPHEMERAL_KEY_USAGE_LIMIT, /* usage_limit = 1 */
336+
pPubKeyRaw);
324337
if (ret != STSE_OK) {
325-
STSAFE_INTERFACE_PRINTF("stse_generate_ECDHE_key_pair error: %d\n", ret);
338+
STSAFE_INTERFACE_PRINTF("stse_generate_ecc_key_pair (ephemeral) error: %d\n", ret);
326339
rc = (int)ret;
327340
}
328341

@@ -1535,22 +1548,27 @@ int wolfSSL_STSAFE_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
15351548
curve_id = stsafe_get_ecc_curve_id(ecc_curve);
15361549
key_sz = stsafe_get_key_size(curve_id);
15371550

1538-
/* For A120, generate keys in slot 1 (persistent slot) by default for ECDSA signing.
1539-
* For ECDH operations, the key slot from devCtx will be used directly.
1540-
* If ECDH is required, keys should be generated in the ephemeral slot from the start. */
1551+
/* For A120: Use ephemeral slot (0xFF) for ECDH/ECDHE operations.
1552+
* Use persistent slots (0-4) for ECDSA signing operations.
1553+
* Note: Persistent slots require key establishment to be enabled
1554+
* in their mode of operation flags via put_attribute command. */
15411555
#ifdef WOLFSSL_STSAFEA120
1542-
/* Retrieve slot from devCtx if available, otherwise use default */
1543-
slot = STSAFE_KEY_SLOT_1; /* Default fallback */
1556+
/* Check if this is for ECDH by looking at devCtx hint */
15441557
if (info->pk.eckg.key != NULL && info->pk.eckg.key->devCtx != NULL) {
15451558
slot = STSAFE_DEVCXT_TO_SLOT(info->pk.eckg.key->devCtx);
1559+
} else {
1560+
/* Default: Use slot 1 for ECDSA signing */
1561+
slot = STSAFE_KEY_SLOT_1;
15461562
}
15471563

15481564
STSAFE_INTERFACE_PRINTF("STSAFE: KeyGen slot %d, curve_id %d\n",
15491565
slot, curve_id);
15501566

1567+
/* Always use ephemeral slot (0xFF) for ECDH operations */
15511568
if (slot == STSAFE_KEY_SLOT_EPHEMERAL) {
15521569
rc = stsafe_create_ecdhe_key(curve_id, pubKeyRaw);
15531570
} else {
1571+
/* Persistent slot for signing */
15541572
rc = stsafe_create_key(slot, curve_id, pubKeyRaw);
15551573
}
15561574
if (rc != STSE_OK) {

0 commit comments

Comments
 (0)