Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -5187,6 +5187,9 @@ int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
int res = 1;
curve25519_key privkey;
curve25519_key pubkey;
#ifdef WOLFSSL_CURVE25519_BLINDING
WC_RNG* rng = NULL;
#endif

WOLFSSL_ENTER("wolfSSL_EC25519_shared_key");

Expand All @@ -5206,8 +5209,13 @@ int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
}
if (res) {
#ifdef WOLFSSL_CURVE25519_BLINDING
/* An RNG is needed. */
if (wc_curve25519_set_rng(&privkey, wolfssl_make_global_rng()) != 0) {
/* An RNG is needed for blinding. */
rng = wolfssl_make_global_rng();
if (rng == NULL) {
WOLFSSL_MSG("Failed to use global RNG.");
res = 0;
}
else if (wc_curve25519_set_rng(&privkey, rng) != 0) {
res = 0;
}
else
Expand Down Expand Up @@ -5235,6 +5243,13 @@ int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
res = 0;
}
}
#ifdef WOLFSSL_CURVE25519_BLINDING
/* Global RNG is shared, lock it while blinding uses it. */
if (res && (wc_LockMutex(&globalRNGMutex) != 0)) {
WOLFSSL_MSG("Bad Lock Mutex rng");
res = 0;
}
#endif
if (res) {
/* Compute shared secret. */
ret = wc_curve25519_shared_secret_ex(&privkey, &pubkey, shared,
Expand All @@ -5243,6 +5258,9 @@ int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
res = 0;
}
#ifdef WOLFSSL_CURVE25519_BLINDING
wc_UnLockMutex(&globalRNGMutex);
#endif
}

wc_curve25519_free(&pubkey);
Expand Down
41 changes: 28 additions & 13 deletions src/pk_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -5489,6 +5489,7 @@ int wolfSSL_ECDH_compute_key(void *out, size_t outLen,
ecc_key* key = NULL;
#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) && \
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0))
WC_RNG* rng = NULL;
int setGlobalRNG = 0;
#endif

Expand Down Expand Up @@ -5524,30 +5525,44 @@ int wolfSSL_ECDH_compute_key(void *out, size_t outLen,

#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) && \
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0))
/* An RNG is needed. */
/* An RNG is needed. Hold the global lock across use of key->rng. */
if (key->rng == NULL) {
Comment thread
yosuke-wolfssl marked this conversation as resolved.
key->rng = wolfssl_make_global_rng();
/* RNG set and needs to be unset. */
setGlobalRNG = 1;
rng = wolfssl_make_global_rng();
if (rng == NULL) {
WOLFSSL_MSG("Failed to use global RNG.");
err = 1;
}
else if (wc_LockMutex(&globalRNGMutex) != 0) {
WOLFSSL_MSG("Bad Lock Mutex rng");
err = 1;
}
else {
key->rng = rng;
/* RNG set and needs to be unset. */
setGlobalRNG = 1;
}
}
#endif

PRIVATE_KEY_UNLOCK();
/* Create secret using wolfSSL. */
ret = wc_ecc_shared_secret_ex(key, (ecc_point*)pubKey->internal,
(byte *)out, &len);
PRIVATE_KEY_LOCK();
if (ret != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_shared_secret failed");
err = 1;
if (!err) {
PRIVATE_KEY_UNLOCK();
/* Create secret using wolfSSL. */
ret = wc_ecc_shared_secret_ex(key, (ecc_point*)pubKey->internal,
(byte *)out, &len);
PRIVATE_KEY_LOCK();
if (ret != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_shared_secret failed");
err = 1;
}
}
}

#if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) && \
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0))
/* Remove global from key. */
/* Remove global from key and release the lock taken when setting it. */
if (setGlobalRNG) {
key->rng = NULL;
wc_UnLockMutex(&globalRNGMutex);
}
#endif

Expand Down
15 changes: 12 additions & 3 deletions src/ssl_bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -2146,11 +2146,20 @@ int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
WOLFSSL_MSG("Failed to allocate buffer.");
ret = 0;
}
/* Generate bytes to cover bits. */
if ((ret == 1) && wc_RNG_GenerateBlock(rng, buff, len) != 0) {
WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
/* Global RNG is shared, lock it while generating. */
if ((ret == 1) && (wc_LockMutex(&globalRNGMutex) != 0)) {
WOLFSSL_MSG("Bad Lock Mutex rng");
ret = 0;
}

/* Generate bytes to cover bits. */
if (ret == 1) {
if (wc_RNG_GenerateBlock(rng, buff, len) != 0) {
WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
ret = 0;
}
wc_UnLockMutex(&globalRNGMutex);
}
/* Read bytes in to big number. */
if ((ret == 1) && mp_read_unsigned_bin((mp_int*)bn->internal, buff, len)
!= MP_OKAY) {
Expand Down
18 changes: 16 additions & 2 deletions src/ssl_sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -2162,15 +2162,29 @@ void AddSession(WOLFSSL* ssl)
* this point, it won't on resumption. */
if (idSz == 0 && ssl->options.side == WOLFSSL_CLIENT_END) {
WC_RNG* rng = NULL;
int genRet;
#if defined(HAVE_GLOBAL_RNG) && defined(OPENSSL_EXTRA)
int rngLocked = 0;
#endif
if (ssl->rng != NULL)
rng = ssl->rng;
#if defined(HAVE_GLOBAL_RNG) && defined(OPENSSL_EXTRA)
else if (initGlobalRNG == 1 || wolfSSL_RAND_Init() == WOLFSSL_SUCCESS) {
/* Global RNG is shared, lock it while generating. */
if (wc_LockMutex(&globalRNGMutex) != 0) {
WOLFSSL_MSG("Bad Lock Mutex rng");
return;
}
rng = &globalRNG;
rngLocked = 1;
}
#endif
if (wc_RNG_GenerateBlock(rng, ssl->session->altSessionID,
ID_LEN) != 0)
genRet = wc_RNG_GenerateBlock(rng, ssl->session->altSessionID, ID_LEN);
#if defined(HAVE_GLOBAL_RNG) && defined(OPENSSL_EXTRA)
if (rngLocked)
wc_UnLockMutex(&globalRNGMutex);
#endif
if (genRet != 0)
return;
ssl->session->haveAltSessionID = 1;
id = ssl->session->altSessionID;
Expand Down
Loading