Skip to content

Commit bc12b75

Browse files
committed
Peer review improvements
1 parent 2a18b7e commit bc12b75

File tree

8 files changed

+101
-56
lines changed

8 files changed

+101
-56
lines changed

doc/dox_comments/header_files/ecc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
20932093
\return 0 Returned upon successfully setting the callback context the input message
20942094
20952095
\param key pointer to the ecc_key object
2096-
\param ctx pointer to ecc_nb_ctx_t structure with stack data cache for SP
2096+
\param ctx pointer to ecc nb_ctx_t structure with stack data cache for SP
20972097
20982098
_Example_
20992099
\code

src/internal.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8191,8 +8191,8 @@ void FreeKey(WOLFSSL* ssl, int type, void** pKey)
81918191
case DYNAMIC_TYPE_CURVE25519:
81928192
#if defined(WC_X25519_NONBLOCK) && \
81938193
defined(WOLFSSL_ASYNC_CRYPT_SW)
8194-
if (((curve25519_key*)*pKey)->nbCtx != NULL) {
8195-
XFREE(((curve25519_key*)*pKey)->nbCtx, ssl->heap,
8194+
if (((curve25519_key*)*pKey)->nb_ctx != NULL) {
8195+
XFREE(((curve25519_key*)*pKey)->nb_ctx, ssl->heap,
81968196
DYNAMIC_TYPE_TMP_BUFFER);
81978197
}
81988198
#endif

src/tls.c

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8014,16 +8014,16 @@ static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
80148014
* INVALID_DEVID there is no async loop to retry on FP_WOULDBLOCK, so
80158015
* skip non-blocking setup and use blocking mode instead. */
80168016
if (ret == 0 && ssl->devId != INVALID_DEVID) {
8017-
x25519_nb_ctx_t* nbCtx = (x25519_nb_ctx_t*)XMALLOC(
8017+
x25519_nb_ctx_t* nb_ctx = (x25519_nb_ctx_t*)XMALLOC(
80188018
sizeof(x25519_nb_ctx_t), ssl->heap,
80198019
DYNAMIC_TYPE_TMP_BUFFER);
8020-
if (nbCtx == NULL) {
8020+
if (nb_ctx == NULL) {
80218021
ret = MEMORY_E;
80228022
}
80238023
else {
8024-
ret = wc_curve25519_set_nonblock(key, nbCtx);
8024+
ret = wc_curve25519_set_nonblock(key, nb_ctx);
80258025
if (ret != 0) {
8026-
XFREE(nbCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
8026+
XFREE(nb_ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
80278027
}
80288028
}
80298029
}
@@ -8088,8 +8088,8 @@ static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
80888088
kse->pubKey = NULL;
80898089
if (key != NULL) {
80908090
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
8091-
if (key->nbCtx != NULL) {
8092-
XFREE(key->nbCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
8091+
if (key->nb_ctx != NULL) {
8092+
XFREE(key->nb_ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
80938093
}
80948094
#endif
80958095
wc_curve25519_free(key);
@@ -8279,6 +8279,28 @@ static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
82798279
/* Initialize an ECC key struct for the ephemeral key */
82808280
ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
82818281

8282+
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8283+
defined(WC_ASYNC_ENABLE_ECC)
8284+
/* Only set non-blocking context when async device is active. With
8285+
* INVALID_DEVID there is no async loop to retry on FP_WOULDBLOCK, so
8286+
* skip non-blocking setup and use blocking mode instead. */
8287+
if (ret == 0 && ssl->devId != INVALID_DEVID) {
8288+
ecc_nb_ctx_t* eccNbCtx = (ecc_nb_ctx_t*)XMALLOC(
8289+
sizeof(ecc_nb_ctx_t), ssl->heap,
8290+
DYNAMIC_TYPE_TMP_BUFFER);
8291+
if (eccNbCtx == NULL) {
8292+
ret = MEMORY_E;
8293+
}
8294+
else {
8295+
ret = wc_ecc_set_nonblock((ecc_key*)kse->key, eccNbCtx);
8296+
if (ret != 0) {
8297+
XFREE(eccNbCtx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
8298+
}
8299+
}
8300+
}
8301+
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
8302+
WC_ASYNC_ENABLE_ECC */
8303+
82828304
if (ret == 0) {
82838305
kse->keyLen = keySize;
82848306
kse->pubKeyLen = keySize * 2 + 1;
@@ -8852,8 +8874,8 @@ static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
88528874
#ifdef HAVE_CURVE25519
88538875
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
88548876
if (current->key != NULL &&
8855-
((curve25519_key*)current->key)->nbCtx != NULL) {
8856-
XFREE(((curve25519_key*)current->key)->nbCtx, heap,
8877+
((curve25519_key*)current->key)->nb_ctx != NULL) {
8878+
XFREE(((curve25519_key*)current->key)->nb_ctx, heap,
88578879
DYNAMIC_TYPE_TMP_BUFFER);
88588880
}
88598881
#endif
@@ -8900,13 +8922,30 @@ static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
89008922
}
89018923
else {
89028924
#ifdef HAVE_ECC
8925+
#if defined(WC_ECC_NONBLOCK) && \
8926+
defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8927+
defined(WC_ASYNC_ENABLE_ECC)
8928+
if (current->key != NULL &&
8929+
((ecc_key*)current->key)->nb_ctx != NULL) {
8930+
XFREE(((ecc_key*)current->key)->nb_ctx, heap,
8931+
DYNAMIC_TYPE_TMP_BUFFER);
8932+
}
8933+
#endif
89038934
wc_ecc_free((ecc_key*)current->key);
89048935
#endif
89058936
}
89068937
}
89078938
#endif
89088939
else {
89098940
#ifdef HAVE_ECC
8941+
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
8942+
defined(WC_ASYNC_ENABLE_ECC)
8943+
if (current->key != NULL &&
8944+
((ecc_key*)current->key)->nb_ctx != NULL) {
8945+
XFREE(((ecc_key*)current->key)->nb_ctx, heap,
8946+
DYNAMIC_TYPE_TMP_BUFFER);
8947+
}
8948+
#endif
89108949
wc_ecc_free((ecc_key*)current->key);
89118950
#endif
89128951
}
@@ -9248,8 +9287,8 @@ static int TLSX_KeyShare_ProcessX25519_ex(WOLFSSL* ssl,
92489287
}
92499288
if (keyShareEntry->key != NULL) {
92509289
#if defined(WC_X25519_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW)
9251-
if (((curve25519_key*)keyShareEntry->key)->nbCtx != NULL) {
9252-
XFREE(((curve25519_key*)keyShareEntry->key)->nbCtx, ssl->heap,
9290+
if (((curve25519_key*)keyShareEntry->key)->nb_ctx != NULL) {
9291+
XFREE(((curve25519_key*)keyShareEntry->key)->nb_ctx, ssl->heap,
92539292
DYNAMIC_TYPE_TMP_BUFFER);
92549293
}
92559294
#endif

wolfcrypt/src/asn.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18456,7 +18456,7 @@ int ConfirmSignature(SignatureCtx* sigCtx,
1845618456
word32 idx = 0;
1845718457
#if defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_ASYNC_CRYPT_SW) && \
1845818458
defined(WC_ASYNC_ENABLE_ECC)
18459-
ecc_nb_ctx_t* nbCtx;
18459+
ecc_nb_ctx_t* nb_ctx;
1846018460
#endif /* WC_ECC_NONBLOCK && WOLFSSL_ASYNC_CRYPT_SW &&
1846118461
WC_ASYNC_ENABLE_ECC */
1846218462

@@ -18479,15 +18479,15 @@ int ConfirmSignature(SignatureCtx* sigCtx,
1847918479
* retry on FP_WOULDBLOCK, so let the WC_ECC_NONBLOCK_ONLY
1848018480
* blocking fallback handle it instead. */
1848118481
if (sigCtx->devId != INVALID_DEVID) {
18482-
nbCtx = (ecc_nb_ctx_t*)XMALLOC(sizeof(ecc_nb_ctx_t),
18482+
nb_ctx = (ecc_nb_ctx_t*)XMALLOC(sizeof(ecc_nb_ctx_t),
1848318483
sigCtx->heap, DYNAMIC_TYPE_TMP_BUFFER);
18484-
if (nbCtx == NULL) {
18484+
if (nb_ctx == NULL) {
1848518485
ERROR_OUT(MEMORY_E, exit_cs);
1848618486
}
1848718487

18488-
ret = wc_ecc_set_nonblock(sigCtx->key.ecc, nbCtx);
18488+
ret = wc_ecc_set_nonblock(sigCtx->key.ecc, nb_ctx);
1848918489
if (ret != 0) {
18490-
XFREE(nbCtx, sigCtx->heap,
18490+
XFREE(nb_ctx, sigCtx->heap,
1849118491
DYNAMIC_TYPE_TMP_BUFFER);
1849218492
goto exit_cs;
1849318493
}

wolfcrypt/src/curve25519.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,13 @@ static int wc_curve25519_make_pub_nb(curve25519_key* key)
459459
if (key == NULL) {
460460
ret = BAD_FUNC_ARG;
461461
}
462-
else if (key->nbCtx == NULL) {
462+
else if (key->nb_ctx == NULL) {
463463
WOLFSSL_MSG("wc_curve25519_make_pub_nb called with NULL non-blocking "
464464
"context.");
465465
ret = BAD_FUNC_ARG;
466466
}
467467

468-
if (ret == 0 && key->nbCtx->state == 0) {
468+
if (ret == 0 && key->nb_ctx->state == 0) {
469469
/* check clamping */
470470
ret = curve25519_priv_clamp_check(key->k);
471471
if (ret == 0) {
@@ -474,7 +474,7 @@ static int wc_curve25519_make_pub_nb(curve25519_key* key)
474474
}
475475
if (ret == 0) {
476476
ret = curve25519_nb(key->p.point, key->k, (byte*)kCurve25519BasePoint,
477-
key->nbCtx);
477+
key->nb_ctx);
478478
}
479479

480480
return ret;
@@ -488,13 +488,13 @@ static int wc_curve25519_make_key_nb(WC_RNG* rng, int keysize,
488488
if (key == NULL || rng == NULL) {
489489
ret = BAD_FUNC_ARG;
490490
}
491-
else if (key->nbCtx == NULL) {
491+
else if (key->nb_ctx == NULL) {
492492
WOLFSSL_MSG("wc_curve25519_make_key_nb called with NULL non-blocking "
493493
"context.");
494494
ret = BAD_FUNC_ARG;
495495
}
496496

497-
if (ret == 0 && key->nbCtx->state == 0) {
497+
if (ret == 0 && key->nb_ctx->state == 0) {
498498
ret = wc_curve25519_make_priv(rng, keysize, key->k);
499499
if (ret == 0) {
500500
key->privSet = 1;
@@ -512,19 +512,19 @@ static int wc_curve25519_make_key_nb(WC_RNG* rng, int keysize,
512512

513513
int wc_curve25519_set_nonblock(curve25519_key* key, x25519_nb_ctx_t* ctx)
514514
{
515-
int ret = 0;
516-
517-
if (key != NULL) {
518-
if (ctx != NULL) {
519-
XMEMSET(ctx, 0, sizeof(x25519_nb_ctx_t));
520-
}
521-
key->nbCtx = ctx;
515+
if (key == NULL) {
516+
return BAD_FUNC_ARG;
522517
}
523-
else {
524-
ret = BAD_FUNC_ARG;
518+
/* If a different context is already set, clear it before replacing.
519+
* The caller is responsible for freeing any heap-allocated context. */
520+
if (key->nb_ctx != NULL && key->nb_ctx != ctx) {
521+
XMEMSET(key->nb_ctx, 0, sizeof(x25519_nb_ctx_t));
525522
}
526-
527-
return ret;
523+
if (ctx != NULL) {
524+
XMEMSET(ctx, 0, sizeof(x25519_nb_ctx_t));
525+
}
526+
key->nb_ctx = ctx;
527+
return 0;
528528
}
529529

530530
#endif /* WC_X25519_NONBLOCK */
@@ -567,7 +567,7 @@ int wc_curve25519_make_key(WC_RNG* rng, int keysize, curve25519_key* key)
567567
#ifdef WOLFSSL_SE050
568568
ret = se050_curve25519_create_key(key, keysize);
569569
#elif defined(WC_X25519_NONBLOCK)
570-
if (key->nbCtx != NULL) {
570+
if (key->nb_ctx != NULL) {
571571
ret = wc_curve25519_make_key_nb(rng, keysize, key);
572572
}
573573
else
@@ -612,17 +612,17 @@ static int wc_curve25519_shared_secret_nb(curve25519_key* privKey,
612612
{
613613
int ret = FP_WOULDBLOCK;
614614

615-
switch (privKey->nbCtx->ssState) {
615+
switch (privKey->nb_ctx->ssState) {
616616
case 0:
617-
XMEMSET(&privKey->nbCtx->o, 0, sizeof(privKey->nbCtx->o));
618-
privKey->nbCtx->ssState = 1;
617+
XMEMSET(&privKey->nb_ctx->o, 0, sizeof(privKey->nb_ctx->o));
618+
privKey->nb_ctx->ssState = 1;
619619
break;
620620
case 1:
621-
ret = curve25519_nb(privKey->nbCtx->o.point, privKey->k,
622-
pubKey->p.point, privKey->nbCtx);
621+
ret = curve25519_nb(privKey->nb_ctx->o.point, privKey->k,
622+
pubKey->p.point, privKey->nb_ctx);
623623
if (ret == 0) {
624624
ret = FP_WOULDBLOCK;
625-
privKey->nbCtx->ssState = 2;
625+
privKey->nb_ctx->ssState = 2;
626626
}
627627
break;
628628
case 2:
@@ -632,15 +632,15 @@ static int wc_curve25519_shared_secret_nb(curve25519_key* privKey,
632632
byte t = 0;
633633

634634
for (i = 0; i < CURVE25519_KEYSIZE; i++) {
635-
t |= privKey->nbCtx->o.point[i];
635+
t |= privKey->nb_ctx->o.point[i];
636636
}
637637
if (t == 0) {
638638
ret = ECC_OUT_OF_RANGE_E;
639639
}
640640
else
641641
#endif /* WOLFSSL_ECDHX_SHARED_NOT_ZERO */
642642
{
643-
curve25519_copy_point(out, privKey->nbCtx->o.point, endian);
643+
curve25519_copy_point(out, privKey->nb_ctx->o.point, endian);
644644
*outlen = CURVE25519_KEYSIZE;
645645
ret = 0;
646646
}
@@ -651,7 +651,7 @@ static int wc_curve25519_shared_secret_nb(curve25519_key* privKey,
651651
}
652652

653653
if (ret != FP_WOULDBLOCK) {
654-
XMEMSET(privKey->nbCtx, 0, sizeof(x25519_nb_ctx_t));
654+
XMEMSET(privKey->nb_ctx, 0, sizeof(x25519_nb_ctx_t));
655655
}
656656

657657
return ret;
@@ -714,7 +714,7 @@ int wc_curve25519_shared_secret_ex(curve25519_key* private_key,
714714
#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_X25519 &&
715715
* WOLFSSL_ASYNC_CRYPT_SW */
716716

717-
if (private_key->nbCtx != NULL) {
717+
if (private_key->nb_ctx != NULL) {
718718
ret = wc_curve25519_shared_secret_nb(private_key, public_key, out,
719719
outlen, endian);
720720
}

wolfcrypt/src/ecc.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Possible ECC enable options:
5555
* WOLFSSL_ECC_CURVE_STATIC: default off (on for windows)
5656
* For the ECC curve parameters `ecc_set_type` use fixed
5757
* array for hex string
58-
* WC_ECC_NONBLOCK: Enable non-blocking support for sign/verify.
58+
* WC_ECC_NONBLOCK: Enable non-blocking support for sign/verify/keygen/secret.
5959
* Requires SP with WOLFSSL_SP_NONBLOCK
6060
* WC_ECC_NONBLOCK_ONLY Enable the non-blocking function only, no fall-back to
6161
* normal blocking API's
@@ -15627,12 +15627,18 @@ int wc_ecc_get_key_id(ecc_key* key, word32* keyId)
1562715627
/* Enable ECC support for non-blocking operations */
1562815628
int wc_ecc_set_nonblock(ecc_key *key, ecc_nb_ctx_t* ctx)
1562915629
{
15630-
if (key) {
15631-
if (ctx) {
15632-
XMEMSET(ctx, 0, sizeof(ecc_nb_ctx_t));
15633-
}
15634-
key->nb_ctx = ctx;
15630+
if (key == NULL) {
15631+
return BAD_FUNC_ARG;
15632+
}
15633+
/* If a different context is already set, clear it before replacing.
15634+
* The caller is responsible for freeing any heap-allocated context. */
15635+
if (key->nb_ctx != NULL && key->nb_ctx != ctx) {
15636+
XMEMSET(key->nb_ctx, 0, sizeof(ecc_nb_ctx_t));
15637+
}
15638+
if (ctx != NULL) {
15639+
XMEMSET(ctx, 0, sizeof(ecc_nb_ctx_t));
1563515640
}
15641+
key->nb_ctx = ctx;
1563615642
return 0;
1563715643
}
1563815644
#endif /* WC_ECC_NONBLOCK */

wolfcrypt/test/test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38594,7 +38594,7 @@ static wc_test_ret_t curve255519_der_test(void)
3859438594
static int x25519_nonblock_test(WC_RNG* rng)
3859538595
{
3859638596
int ret = 0;
38597-
x25519_nb_ctx_t nbCtx;
38597+
x25519_nb_ctx_t nb_ctx;
3859838598
curve25519_key userA;
3859938599
curve25519_key userB;
3860038600
#ifdef HAVE_CURVE25519_SHARED_SECRET
@@ -38605,14 +38605,14 @@ static int x25519_nonblock_test(WC_RNG* rng)
3860538605
#endif
3860638606
int count;
3860738607

38608-
XMEMSET(&nbCtx, 0, sizeof(nbCtx));
38608+
XMEMSET(&nb_ctx, 0, sizeof(nb_ctx));
3860938609

3861038610
ret = wc_curve25519_init(&userA);
3861138611
if (ret != 0) {
3861238612
printf("wc_curve25519_init 1 %d\n", ret);
3861338613
return -10722;
3861438614
}
38615-
ret = wc_curve25519_set_nonblock(&userA, &nbCtx);
38615+
ret = wc_curve25519_set_nonblock(&userA, &nb_ctx);
3861638616
if (ret != 0) {
3861738617
printf("wc_curve25519_set_nonblock 1 %d\n", ret);
3861838618
wc_curve25519_free(&userA);
@@ -38639,7 +38639,7 @@ static int x25519_nonblock_test(WC_RNG* rng)
3863938639
wc_curve25519_free(&userA);
3864038640
return -10724;
3864138641
}
38642-
ret = wc_curve25519_set_nonblock(&userB, &nbCtx);
38642+
ret = wc_curve25519_set_nonblock(&userB, &nb_ctx);
3864338643
if (ret != 0) {
3864438644
printf("wc_curve25519_set_nonblock 2 %d\n", ret);
3864538645
wc_curve25519_free(&userA);

wolfssl/wolfcrypt/curve25519.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ struct curve25519_key {
141141
#endif
142142

143143
#ifdef WC_X25519_NONBLOCK
144-
x25519_nb_ctx_t* nbCtx;
144+
x25519_nb_ctx_t* nb_ctx;
145145
#endif /* WC_X25519_NONBLOCK */
146146

147147
/* bit fields */

0 commit comments

Comments
 (0)