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