@@ -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
513513int 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 }
0 commit comments