Skip to content

Commit e4cfe6b

Browse files
committed
Address items in code review
1 parent f45a489 commit e4cfe6b

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

.github/workflows/se050-sim.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ concurrency:
2020
# We patch it to COPY the PR checkout instead so CI reflects the PR's source.
2121

2222
env:
23-
# Pin the simulator to a known-good revision. Bump this deliberately after
24-
# validating upstream changes in a standalone PR.
2523
SE050SIM_REF: main
2624

2725
jobs:
@@ -48,7 +46,7 @@ jobs:
4846
working-directory: se050sim
4947
run: |
5048
sed -i 's|^RUN git clone --depth 1 https://github.com/wolfSSL/wolfssl.git /app/wolfssl$|COPY wolfssl /app/wolfssl|' Dockerfile.wolfcrypt
51-
# Fail fast if the pattern drifted upstream better a clear error
49+
# Fail fast if the pattern drifted upstream -- better a clear error
5250
# than a CI run that silently tests master.
5351
grep -q '^COPY wolfssl /app/wolfssl$' Dockerfile.wolfcrypt
5452
! grep -q 'git clone .*wolfssl\.git' Dockerfile.wolfcrypt

wolfcrypt/src/ed25519.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,15 @@ int wc_ed25519_import_public_ex(const byte* in, word32 inLen, ed25519_key* key,
11701170
return BAD_FUNC_ARG;
11711171

11721172
#ifdef WOLFSSL_SE050
1173-
/* Importing new key material invalidates any prior SE050 object binding. */
1174-
key->keyIdSet = 0;
1173+
/* Importing new key material invalidates any prior SE050 object binding;
1174+
* erase the old object (no-op when keyIdSet == 0) so the host and the
1175+
* secure element agree on what's bound. Clear the binding fields
1176+
* explicitly afterwards so a stale keyId never survives, even when
1177+
* se050_ed25519_free_key() returns early because the SE050 session isn't
1178+
* configured yet. */
1179+
se050_ed25519_free_key(key);
11751180
key->keyId = 0;
1181+
key->keyIdSet = 0;
11761182
#endif
11771183

11781184
/* compressed prefix according to draft
@@ -1262,9 +1268,15 @@ int wc_ed25519_import_private_only(const byte* priv, word32 privSz,
12621268
return BAD_FUNC_ARG;
12631269

12641270
#ifdef WOLFSSL_SE050
1265-
/* Importing new key material invalidates any prior SE050 object binding. */
1266-
key->keyIdSet = 0;
1271+
/* Importing new key material invalidates any prior SE050 object binding;
1272+
* erase the old object (no-op when keyIdSet == 0) so the host and the
1273+
* secure element agree on what's bound. Clear the binding fields
1274+
* explicitly afterwards so a stale keyId never survives, even when
1275+
* se050_ed25519_free_key() returns early because the SE050 session isn't
1276+
* configured yet. */
1277+
se050_ed25519_free_key(key);
12671278
key->keyId = 0;
1279+
key->keyIdSet = 0;
12681280
#endif
12691281

12701282
XMEMCPY(key->k, priv, ED25519_KEY_SIZE);
@@ -1324,9 +1336,18 @@ int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
13241336
}
13251337

13261338
#ifdef WOLFSSL_SE050
1327-
/* Importing new key material invalidates any prior SE050 object binding. */
1328-
key->keyIdSet = 0;
1339+
/* Importing new key material invalidates any prior SE050 object binding;
1340+
* erase the old object (no-op when keyIdSet == 0) so the host and the
1341+
* secure element agree on what's bound. key->k is overwritten before the
1342+
* wc_ed25519_import_public_ex() call below, so the binding must be
1343+
* dropped here first in case that function fails its own early-return
1344+
* argument checks before reaching its reset. Clear the binding fields
1345+
* explicitly afterwards so a stale keyId never survives, even when
1346+
* se050_ed25519_free_key() returns early because the SE050 session isn't
1347+
* configured yet. */
1348+
se050_ed25519_free_key(key);
13291349
key->keyId = 0;
1350+
key->keyIdSet = 0;
13301351
#endif
13311352

13321353
XMEMCPY(key->k, priv, ED25519_KEY_SIZE);

wolfcrypt/src/port/nxp/se050_port.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ int se050_rsa_verify(const byte* in, word32 inLen, byte* out, word32 outLen,
14831483
keyId = se050_allocate_key(SE050_RSA_KEY);
14841484
status = sss_key_object_allocate_handle(&newKey, keyId,
14851485
kSSS_KeyPart_Public, kSSS_CipherType_RSA, keySz,
1486-
kKeyObject_Mode_Persistent);
1486+
kKeyObject_Mode_Transient);
14871487
}
14881488
if (status == kStatus_SSS_Success) {
14891489
/* Try to delete existing key first, ignore return since will
@@ -1540,7 +1540,7 @@ int se050_rsa_verify(const byte* in, word32 inLen, byte* out, word32 outLen,
15401540
if (status == kStatus_SSS_Success) {
15411541
if (keyCreated) {
15421542
/* We uploaded only the public part of the key for this verify.
1543-
* Don't persist keyIdSet=1 a later sign on the same RsaKey
1543+
* Don't persist keyIdSet=1 -- a later sign on the same RsaKey
15441544
* would reuse this binding and fail because the SE050 object has
15451545
* no private material. Erase the transient object so the next
15461546
* SE050 op (sign or verify) re-uploads from whatever the host
@@ -3053,6 +3053,10 @@ int se050_ed25519_verify_msg(const byte* signature, word32 signatureLen,
30533053
key, signature, signatureLen, msg, msgLen);
30543054
#endif
30553055

3056+
if (signature == NULL || msg == NULL || key == NULL || res == NULL) {
3057+
return BAD_FUNC_ARG;
3058+
}
3059+
30563060
*res = 0;
30573061

30583062
if (cfg_se050_i2c_pi == NULL) {
@@ -3115,8 +3119,21 @@ int se050_ed25519_verify_msg(const byte* signature, word32 signatureLen,
31153119
}
31163120

31173121
if (status == kStatus_SSS_Success) {
3118-
key->keyId = keyId;
3119-
key->keyIdSet = 1;
3122+
if (keyCreated) {
3123+
/* We uploaded only the public part of the key for this verify.
3124+
* Don't persist keyIdSet=1 -- a later sign on the same ed25519_key
3125+
* would reuse this binding and fail because the SE050 object has
3126+
* no private material. Erase the transient object so the next
3127+
* SE050 op re-uploads. Mirrors the fix in se050_rsa_verify. */
3128+
sss_key_store_erase_key(&host_keystore, &newKey);
3129+
sss_key_object_free(&newKey);
3130+
}
3131+
else {
3132+
/* Pre-existing keyIdSet=1 binding (from prior sign that uploaded
3133+
* a keypair, or explicit caller setup). Preserve it. */
3134+
key->keyId = keyId;
3135+
key->keyIdSet = 1;
3136+
}
31203137
*res = 1;
31213138
ret = 0;
31223139
}

wolfcrypt/test/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41429,7 +41429,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
4142941429
/* These cases exercise host-side signature-encoding pre-validation (e.g.,
4143041430
* sig == curve order). The SE050 port delegates verify to the secure
4143141431
* element, which rejects all four inputs with WC_HW_E rather than the
41432-
* BAD_FUNC_ARG / SIG_VERIFY_E the host-side path produces so the
41432+
* BAD_FUNC_ARG / SIG_VERIFY_E the host-side path produces -- so the
4143341433
* expected error code differs below when built against an SE050. */
4143441434
{
4143541435
/* Run tests for some rare code paths */

0 commit comments

Comments
 (0)