Skip to content

Commit 80a3b1e

Browse files
committed
Address items in code review
1 parent 37c70c4 commit 80a3b1e

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
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: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,9 +1170,10 @@ 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;
1175-
key->keyId = 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. */
1176+
se050_ed25519_free_key(key);
11761177
#endif
11771178

11781179
/* compressed prefix according to draft
@@ -1262,9 +1263,10 @@ int wc_ed25519_import_private_only(const byte* priv, word32 privSz,
12621263
return BAD_FUNC_ARG;
12631264

12641265
#ifdef WOLFSSL_SE050
1265-
/* Importing new key material invalidates any prior SE050 object binding. */
1266-
key->keyIdSet = 0;
1267-
key->keyId = 0;
1266+
/* Importing new key material invalidates any prior SE050 object binding;
1267+
* erase the old object (no-op when keyIdSet == 0) so the host and the
1268+
* secure element agree on what's bound. */
1269+
se050_ed25519_free_key(key);
12681270
#endif
12691271

12701272
XMEMCPY(key->k, priv, ED25519_KEY_SIZE);
@@ -1324,9 +1326,14 @@ int wc_ed25519_import_private_key_ex(const byte* priv, word32 privSz,
13241326
}
13251327

13261328
#ifdef WOLFSSL_SE050
1327-
/* Importing new key material invalidates any prior SE050 object binding. */
1328-
key->keyIdSet = 0;
1329-
key->keyId = 0;
1329+
/* Importing new key material invalidates any prior SE050 object binding;
1330+
* erase the old object (no-op when keyIdSet == 0) so the host and the
1331+
* secure element agree on what's bound. wc_ed25519_import_public_ex below
1332+
* does the same reset, but we also do it here explicitly: key->k is
1333+
* overwritten before that call, so the binding must be dropped first in
1334+
* case wc_ed25519_import_public_ex fails its own early-return argument
1335+
* checks before reaching its reset. */
1336+
se050_ed25519_free_key(key);
13301337
#endif
13311338

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

wolfcrypt/src/port/nxp/se050_port.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -41060,7 +41060,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ed25519_test(void)
4106041060
/* These cases exercise host-side signature-encoding pre-validation (e.g.,
4106141061
* sig == curve order). The SE050 port delegates verify to the secure
4106241062
* element, which rejects all four inputs with WC_HW_E rather than the
41063-
* BAD_FUNC_ARG / SIG_VERIFY_E the host-side path produces so the
41063+
* BAD_FUNC_ARG / SIG_VERIFY_E the host-side path produces -- so the
4106441064
* expected error code differs below when built against an SE050. */
4106541065
{
4106641066
/* Run tests for some rare code paths */

0 commit comments

Comments
 (0)