@@ -33196,6 +33196,52 @@ static void FreeSckeArgs(WOLFSSL* ssl, void* pArgs)
3319633196 XFREE(args->input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
3319733197 args->input = NULL;
3319833198}
33199+ #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
33200+ static int EcExportHsKey(WOLFSSL* ssl, byte* out, word32* len)
33201+ {
33202+ int ret = 0;
33203+ #ifdef HAVE_CURVE25519
33204+ if (ssl->ecdhCurveOID == ECC_X25519_OID) {
33205+ #ifdef HAVE_PK_CALLBACKS
33206+ /* if callback then use it for shared secret */
33207+ if (ssl->ctx->X25519SharedSecretCb != NULL)
33208+ return 0;
33209+ #endif
33210+ if (wc_curve25519_export_public_ex((curve25519_key*)ssl->hsKey,
33211+ out + OPAQUE8_LEN, len, EC25519_LITTLE_ENDIAN))
33212+ ret = ECC_EXPORT_ERROR;
33213+ } else
33214+ #endif
33215+ #ifdef HAVE_CURVE448
33216+ if (ssl->ecdhCurveOID == ECC_X448_OID) {
33217+ #ifdef HAVE_PK_CALLBACKS
33218+ /* if callback then use it for shared secret */
33219+ if (ssl->ctx->X448SharedSecretCb != NULL)
33220+ return 0;
33221+ #endif
33222+ if (wc_curve448_export_public_ex((curve448_key*)ssl->hsKey,
33223+ out + OPAQUE8_LEN, len, EC448_LITTLE_ENDIAN))
33224+ ret = ECC_EXPORT_ERROR;
33225+ } else
33226+ #endif
33227+ {
33228+ #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
33229+ #ifdef HAVE_PK_CALLBACKS
33230+ /* if callback then use it for shared secret */
33231+ if (ssl->ctx->EccSharedSecretCb != NULL)
33232+ return 0;
33233+ #endif
33234+ /* Place ECC key in output buffer, leaving room for size */
33235+ PRIVATE_KEY_UNLOCK();
33236+ ret = wc_ecc_export_x963((ecc_key*)ssl->hsKey, out + OPAQUE8_LEN, len);
33237+ PRIVATE_KEY_LOCK();
33238+ if (ret != 0)
33239+ ret = ECC_EXPORT_ERROR;
33240+ #endif
33241+ }
33242+ return ret;
33243+ }
33244+ #endif /*HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448*/
3319933245
3320033246#ifndef NO_PSK
3320133247static int AddPSKtoPreMasterSecret(WOLFSSL* ssl)
@@ -33820,63 +33866,7 @@ int SendClientKeyExchange(WOLFSSL* ssl)
3382033866 /* Create shared ECC key leaving room at the beginning
3382133867 * of buffer for size of shared key. */
3382233868 ssl->arrays->preMasterSz = ENCRYPT_LEN - OPAQUE16_LEN;
33823-
33824- #ifdef HAVE_CURVE25519
33825- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
33826- #ifdef HAVE_PK_CALLBACKS
33827- /* if callback then use it for shared secret */
33828- if (ssl->ctx->X25519SharedSecretCb != NULL) {
33829- break;
33830- }
33831- #endif
33832-
33833- ret = wc_curve25519_export_public_ex(
33834- (curve25519_key*)ssl->hsKey,
33835- args->output + OPAQUE8_LEN, &args->length,
33836- EC25519_LITTLE_ENDIAN);
33837- if (ret != 0) {
33838- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
33839- }
33840-
33841- break;
33842- }
33843- #endif
33844- #ifdef HAVE_CURVE448
33845- if (ssl->ecdhCurveOID == ECC_X448_OID) {
33846- #ifdef HAVE_PK_CALLBACKS
33847- /* if callback then use it for shared secret */
33848- if (ssl->ctx->X448SharedSecretCb != NULL) {
33849- break;
33850- }
33851- #endif
33852-
33853- ret = wc_curve448_export_public_ex(
33854- (curve448_key*)ssl->hsKey,
33855- args->output + OPAQUE8_LEN, &args->length,
33856- EC448_LITTLE_ENDIAN);
33857- if (ret != 0) {
33858- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
33859- }
33860-
33861- break;
33862- }
33863- #endif
33864- #ifdef HAVE_PK_CALLBACKS
33865- /* if callback then use it for shared secret */
33866- if (ssl->ctx->EccSharedSecretCb != NULL) {
33867- break;
33868- }
33869- #endif
33870-
33871- /* Place ECC key in output buffer, leaving room for size */
33872- PRIVATE_KEY_UNLOCK();
33873- ret = wc_ecc_export_x963((ecc_key*)ssl->hsKey,
33874- args->output + OPAQUE8_LEN, &args->length);
33875- PRIVATE_KEY_LOCK();
33876- if (ret != 0) {
33877- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
33878- }
33879-
33869+ ret = EcExportHsKey(ssl, args->output, &args->length);
3388033870 break;
3388133871 }
3388233872 #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) && !NO_PSK */
@@ -33885,64 +33875,7 @@ int SendClientKeyExchange(WOLFSSL* ssl)
3388533875 case ecc_diffie_hellman_kea:
3388633876 {
3388733877 ssl->arrays->preMasterSz = ENCRYPT_LEN;
33888-
33889- #ifdef HAVE_CURVE25519
33890- if (ssl->hsType == DYNAMIC_TYPE_CURVE25519) {
33891- #ifdef HAVE_PK_CALLBACKS
33892- /* if callback then use it for shared secret */
33893- if (ssl->ctx->X25519SharedSecretCb != NULL) {
33894- break;
33895- }
33896- #endif
33897-
33898- ret = wc_curve25519_export_public_ex(
33899- (curve25519_key*)ssl->hsKey,
33900- args->encSecret + OPAQUE8_LEN, &args->encSz,
33901- EC25519_LITTLE_ENDIAN);
33902- if (ret != 0) {
33903- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
33904- }
33905-
33906- break;
33907- }
33908- #endif
33909- #ifdef HAVE_CURVE448
33910- if (ssl->hsType == DYNAMIC_TYPE_CURVE448) {
33911- #ifdef HAVE_PK_CALLBACKS
33912- /* if callback then use it for shared secret */
33913- if (ssl->ctx->X448SharedSecretCb != NULL) {
33914- break;
33915- }
33916- #endif
33917-
33918- ret = wc_curve448_export_public_ex(
33919- (curve448_key*)ssl->hsKey,
33920- args->encSecret + OPAQUE8_LEN, &args->encSz,
33921- EC448_LITTLE_ENDIAN);
33922- if (ret != 0) {
33923- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
33924- }
33925-
33926- break;
33927- }
33928- #endif
33929- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
33930- #ifdef HAVE_PK_CALLBACKS
33931- /* if callback then use it for shared secret */
33932- if (ssl->ctx->EccSharedSecretCb != NULL) {
33933- break;
33934- }
33935- #endif
33936-
33937- /* Place ECC key in buffer, leaving room for size */
33938- PRIVATE_KEY_UNLOCK();
33939- ret = wc_ecc_export_x963((ecc_key*)ssl->hsKey,
33940- args->encSecret + OPAQUE8_LEN, &args->encSz);
33941- PRIVATE_KEY_LOCK();
33942- if (ret != 0) {
33943- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
33944- }
33945- #endif /* HAVE_ECC */
33878+ ret = EcExportHsKey(ssl, args->encSecret, &args->encSz);
3394633879 break;
3394733880 }
3394833881 #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
0 commit comments