Skip to content

Commit 7b805d7

Browse files
committed
Add lock around static ECC ecc_oid_cache
1 parent bf29b68 commit 7b805d7

3 files changed

Lines changed: 68 additions & 5 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,13 @@ size_t wc_ecc_get_sets_count(void) {
14261426
byte oid[ECC_MAX_OID_LEN];
14271427
} oid_cache_t;
14281428
static oid_cache_t ecc_oid_cache[ECC_SET_COUNT];
1429+
1430+
static wolfSSL_Mutex ecc_oid_cache_lock
1431+
WOLFSSL_MUTEX_INITIALIZER_CLAUSE(ecc_oid_cache_lock);
1432+
#ifndef WOLFSSL_MUTEX_INITIALIZER
1433+
static volatile int eccOidLockInit = 0;
14291434
#endif
1435+
#endif /* HAVE_OID_ENCODING */
14301436

14311437
/* Forward declarations */
14321438
#if defined(HAVE_COMP_KEY) && defined(HAVE_ECC_KEY_EXPORT)
@@ -15418,22 +15424,57 @@ static int wc_ecc_export_x963_compressed(ecc_key* key, byte* out, word32* outLen
1541815424
#endif /* HAVE_ECC_KEY_EXPORT */
1541915425
#endif /* HAVE_COMP_KEY */
1542015426

15427+
#ifdef HAVE_OID_ENCODING
15428+
int wc_ecc_oid_cache_init(void)
15429+
{
15430+
int ret = 0;
15431+
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER)
15432+
ret = wc_InitMutex(&ecc_oid_cache_lock);
15433+
#endif
15434+
return ret;
15435+
}
15436+
15437+
void wc_ecc_oid_cache_free(void)
15438+
{
15439+
#if !defined(SINGLE_THREADED) && !defined(WOLFSSL_MUTEX_INITIALIZER)
15440+
wc_FreeMutex(&ecc_oid_cache_lock);
15441+
#endif
15442+
}
15443+
#endif /* HAVE_OID_ENCODING */
1542115444

1542215445
int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz)
1542315446
{
1542415447
int x;
15448+
int ret = WC_NO_ERR_TRACE(NOT_COMPILED_IN);
15449+
#ifdef HAVE_OID_ENCODING
15450+
oid_cache_t* o = NULL;
15451+
#endif
1542515452

1542615453
if (oidSum == 0) {
1542715454
return BAD_FUNC_ARG;
1542815455
}
1542915456

15457+
#ifdef HAVE_OID_ENCODING
15458+
#ifndef WOLFSSL_MUTEX_INITIALIZER
15459+
/* extra sanity check if wolfCrypt_Init not called */
15460+
if (eccOidLockInit == 0) {
15461+
wc_InitMutex(&ecc_oid_cache_lock);
15462+
eccOidLockInit = 1;
15463+
}
15464+
#endif
15465+
15466+
if (wc_LockMutex(&ecc_oid_cache_lock) != 0) {
15467+
return BAD_MUTEX_E;
15468+
}
15469+
#endif
15470+
1543015471
/* find matching OID sum (based on encoded value) */
1543115472
for (x = 0; ecc_sets[x].size != 0; x++) {
1543215473
if (ecc_sets[x].oidSum == oidSum) {
1543315474
#ifdef HAVE_OID_ENCODING
15434-
int ret = 0;
1543515475
/* check cache */
15436-
oid_cache_t* o = &ecc_oid_cache[x];
15476+
ret = 0;
15477+
o = &ecc_oid_cache[x];
1543715478
if (o->oidSz == 0) {
1543815479
o->oidSz = sizeof(o->oid);
1543915480
ret = EncodeObjectId(ecc_sets[x].oid, ecc_sets[x].oidSz,
@@ -15445,24 +15486,30 @@ int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz)
1544515486
if (oid) {
1544615487
*oid = o->oid;
1544715488
}
15489+
1544815490
/* on success return curve id */
1544915491
if (ret == 0) {
1545015492
ret = ecc_sets[x].id;
1545115493
}
15452-
return ret;
15494+
break;
1545315495
#else
1545415496
if (oidSz) {
1545515497
*oidSz = ecc_sets[x].oidSz;
1545615498
}
1545715499
if (oid) {
1545815500
*oid = ecc_sets[x].oid;
1545915501
}
15460-
return ecc_sets[x].id;
15502+
ret = ecc_sets[x].id;
15503+
break;
1546115504
#endif
1546215505
}
1546315506
}
1546415507

15465-
return NOT_COMPILED_IN;
15508+
#ifdef HAVE_OID_ENCODING
15509+
wc_UnLockMutex(&ecc_oid_cache_lock);
15510+
#endif
15511+
15512+
return ret;
1546615513
}
1546715514

1546815515
#ifdef WOLFSSL_CUSTOM_CURVES

wolfcrypt/src/wc_port.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,13 @@ int wolfCrypt_Init(void)
365365
return ret;
366366
}
367367
#endif
368+
#if defined(HAVE_OID_ENCODING) && (!defined(HAVE_FIPS) || \
369+
(defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)))
370+
if ((ret = wc_ecc_oid_cache_init()) != 0) {
371+
WOLFSSL_MSG("Error creating ECC oid cache");
372+
return ret;
373+
}
374+
#endif
368375
#endif
369376

370377
#ifdef WOLFSSL_SCE
@@ -456,6 +463,10 @@ int wolfCrypt_Cleanup(void)
456463
#ifdef ECC_CACHE_CURVE
457464
wc_ecc_curve_cache_free();
458465
#endif
466+
#if defined(HAVE_OID_ENCODING) && (!defined(HAVE_FIPS) || \
467+
(defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0)))
468+
wc_ecc_oid_cache_free();
469+
#endif
459470
#endif /* HAVE_ECC */
460471

461472
#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)

wolfssl/wolfcrypt/ecc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,11 @@ WOLFSSL_API int wc_ecc_curve_cache_init(void);
10271027
WOLFSSL_API void wc_ecc_curve_cache_free(void);
10281028
#endif
10291029

1030+
#ifdef HAVE_OID_ENCODING
1031+
WOLFSSL_LOCAL int wc_ecc_oid_cache_init(void);
1032+
WOLFSSL_LOCAL void wc_ecc_oid_cache_free(void);
1033+
#endif
1034+
10301035
WOLFSSL_API
10311036
int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order);
10321037

0 commit comments

Comments
 (0)