Skip to content

Commit 70df138

Browse files
committed
Unify locking behavior using a single function
1 parent 46c8301 commit 70df138

8 files changed

Lines changed: 103 additions & 209 deletions

File tree

include/wolfprovider/alg_funcs.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,17 @@ typedef void (*DFUNC)(void);
175175
int wolfssl_prov_is_running(void);
176176
WC_RNG* wolfssl_prov_get_rng(WOLFPROV_CTX* provctx);
177177

178+
int wp_lock(wolfSSL_Mutex* mutex);
179+
int wp_unlock(wolfSSL_Mutex* mutex);
180+
178181
/* Internal RSA types and functions. */
179182
typedef struct wp_Rsa wp_Rsa;
180183

181184
int wp_rsa_up_ref(wp_Rsa* rsa);
182185
void wp_rsa_free(wp_Rsa* rsa);
183186
int wp_rsa_get_type(wp_Rsa* rsa);
184187
int wp_rsa_get_bits(wp_Rsa* rsa);
185-
int wp_rsa_lock(wp_Rsa* rsa);
186-
int wp_rsa_unlock(wp_Rsa* rsa);
188+
wolfSSL_Mutex* wp_rsa_get_mutex(wp_Rsa* rsa);
187189
RsaKey* wp_rsa_get_key(wp_Rsa* rsa);
188190
void wp_rsa_get_pss_mds(wp_Rsa* rsa, char** mdName, char** mgfMdName);
189191
int wp_rsa_get_pss_salt_len(wp_Rsa* rsa);
@@ -201,17 +203,15 @@ ecc_key* wp_ecc_get_key(wp_Ecc* ecc);
201203
WC_RNG* wp_ecc_get_rng(wp_Ecc* ecc);
202204
int wp_ecc_get_size(wp_Ecc* ecc);
203205
int wp_ecc_check_usage(wp_Ecc* ecc);
204-
int wp_ecc_lock(wp_Ecc* ecc);
205-
int wp_ecc_unlock(wp_Ecc* ecc);
206+
wolfSSL_Mutex* wp_ecc_get_mutex(wp_Ecc* ecc);
206207

207208
/* Internal ECX types and functions. */
208209
typedef struct wp_Ecx wp_Ecx;
209210

210211
int wp_ecx_up_ref(wp_Ecx* ecx);
211212
void wp_ecx_free(wp_Ecx* ecx);
212213
void* wp_ecx_get_key(wp_Ecx* ecx);
213-
int wp_ecx_lock(wp_Ecx* ecx);
214-
int wp_ecx_unlock(wp_Ecx* ecx);
214+
wolfSSL_Mutex* wp_ecx_get_mutex(wp_Ecx* ecx);
215215

216216
/* Internal DH types and functions. */
217217
typedef struct wp_Dh wp_Dh;

src/wp_ecc_kmgmt.c

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -300,73 +300,14 @@ int wp_ecc_get_size(wp_Ecc* ecc)
300300
}
301301

302302
/**
303-
* Lock the ECC key mutex.
304-
*
305-
* This function locks the mutex associated with the ECC key object.
306-
* Use wp_ecc_unlock() to unlock after operations are complete.
307-
*
308-
* @param [in] ecc ECC key object.
309-
* @return 1 on success.
310-
* @return 0 on failure or when single-threaded build.
311-
*/
312-
int wp_ecc_lock(wp_Ecc* ecc)
313-
{
314-
#ifndef WP_SINGLE_THREADED
315-
int ok = 1;
316-
int rc;
317-
318-
if (ecc == NULL) {
319-
ok = 0;
320-
}
321-
else {
322-
rc = wc_LockMutex(&ecc->mutex);
323-
if (rc < 0) {
324-
ok = 0;
325-
}
326-
}
327-
328-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
329-
return ok;
330-
#else
331-
(void)ecc;
332-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
333-
return 1;
334-
#endif
335-
}
336-
337-
/**
338-
* Unlock the ECC key mutex.
339-
*
340-
* This function unlocks the mutex associated with the ECC key object.
341-
* Should only be called after a successful wp_ecc_lock() call.
303+
* Get the mutex object from the ECC key object.
342304
*
343305
* @param [in] ecc ECC key object.
344-
* @return 1 on success.
345-
* @return 0 on failure or when single-threaded build.
306+
* @return Pointer to wolfSSL mutex object.
346307
*/
347-
int wp_ecc_unlock(wp_Ecc* ecc)
308+
wolfSSL_Mutex* wp_ecc_get_mutex(wp_Ecc* ecc)
348309
{
349-
#ifndef WP_SINGLE_THREADED
350-
int ok = 1;
351-
int rc;
352-
353-
if (ecc == NULL) {
354-
ok = 0;
355-
}
356-
else {
357-
rc = wc_UnLockMutex(&ecc->mutex);
358-
if (rc < 0) {
359-
ok = 0;
360-
}
361-
}
362-
363-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
364-
return ok;
365-
#else
366-
(void)ecc;
367-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
368-
return 1;
369-
#endif
310+
return &ecc->mutex;
370311
}
371312

372313
/**

src/wp_ecdsa_sig.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,15 +280,15 @@ static int wp_ecdsa_sign(wp_EcdsaSigCtx *ctx, unsigned char *sig,
280280
sigSize = *sigLen;
281281
}
282282
len = (word32)sigSize;
283-
if (wp_ecc_lock(ctx->ecc) != 1) {
283+
if (wp_lock(wp_ecc_get_mutex(ctx->ecc)) != 1) {
284284
ok = 0;
285285
}
286286
if (ok) {
287287
PRIVATE_KEY_UNLOCK();
288288
rc = wc_ecc_sign_hash(tbs, (word32)tbsLen, sig, &len,
289289
wp_ecc_get_rng(ctx->ecc), wp_ecc_get_key(ctx->ecc));
290290
PRIVATE_KEY_LOCK();
291-
wp_ecc_unlock(ctx->ecc);
291+
wp_unlock(wp_ecc_get_mutex(ctx->ecc));
292292
if (rc != 0) {
293293
ok = 0;
294294
}

src/wp_ecx_kmgmt.c

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -244,73 +244,14 @@ void* wp_ecx_get_key(wp_Ecx* ecx)
244244
}
245245

246246
/**
247-
* Lock the ECX key mutex.
248-
*
249-
* This function locks the mutex associated with the ECX key object.
250-
* Use wp_ecx_unlock() to unlock after operations are complete.
251-
*
252-
* @param [in] ecx ECX key object.
253-
* @return 1 on success.
254-
* @return 0 on failure or when single-threaded build.
255-
*/
256-
int wp_ecx_lock(wp_Ecx* ecx)
257-
{
258-
#ifndef WP_SINGLE_THREADED
259-
int ok = 1;
260-
int rc;
261-
262-
if (ecx == NULL) {
263-
ok = 0;
264-
}
265-
else {
266-
rc = wc_LockMutex(&ecx->mutex);
267-
if (rc < 0) {
268-
ok = 0;
269-
}
270-
}
271-
272-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
273-
return ok;
274-
#else
275-
(void)ecx;
276-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
277-
return 1;
278-
#endif
279-
}
280-
281-
/**
282-
* Unlock the ECX key mutex.
283-
*
284-
* This function unlocks the mutex associated with the ECX key object.
285-
* Should only be called after a successful wp_ecx_lock() call.
247+
* Get the mutex object from the ECX key object.
286248
*
287249
* @param [in] ecx ECX key object.
288-
* @return 1 on success.
289-
* @return 0 on failure or when single-threaded build.
250+
* @return Pointer to wolfSSL mutex object.
290251
*/
291-
int wp_ecx_unlock(wp_Ecx* ecx)
252+
wolfSSL_Mutex* wp_ecx_get_mutex(wp_Ecx* ecx)
292253
{
293-
#ifndef WP_SINGLE_THREADED
294-
int ok = 1;
295-
int rc;
296-
297-
if (ecx == NULL) {
298-
ok = 0;
299-
}
300-
else {
301-
rc = wc_UnLockMutex(&ecx->mutex);
302-
if (rc < 0) {
303-
ok = 0;
304-
}
305-
}
306-
307-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
308-
return ok;
309-
#else
310-
(void)ecx;
311-
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
312-
return 1;
313-
#endif
254+
return &ecx->mutex;
314255
}
315256

316257
/**

src/wp_ecx_sig.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ static int wp_ed25519_digest_sign(wp_EcxSigCtx *ctx, unsigned char *sig,
436436
}
437437
}
438438
if (ok) {
439-
if (wp_ecx_lock(ctx->ecx) != 1) {
439+
if (wp_lock(wp_ecx_get_mutex(ctx->ecx)) != 1) {
440440
ok = 0;
441441
}
442442
if (ok) {
443443
rc = wc_ed25519_sign_msg(tbs, (word32)tbsLen, sig, &len, ed25519);
444-
wp_ecx_unlock(ctx->ecx);
444+
wp_unlock(wp_ecx_get_mutex(ctx->ecx));
445445
if (rc != 0) {
446446
ok = 0;
447447
}
@@ -584,13 +584,13 @@ static int wp_ed448_digest_sign(wp_EcxSigCtx *ctx, unsigned char *sig,
584584
}
585585
}
586586
if (ok) {
587-
if (wp_ecx_lock(ctx->ecx) != 1) {
587+
if (wp_lock(wp_ecx_get_mutex(ctx->ecx)) != 1) {
588588
ok = 0;
589589
}
590590
if (ok) {
591591
rc = wc_ed448_sign_msg(tbs, (word32)tbsLen, sig, &len,
592592
(ed448_key*)wp_ecx_get_key(ctx->ecx), NULL, 0);
593-
wp_ecx_unlock(ctx->ecx);
593+
wp_unlock(wp_ecx_get_mutex(ctx->ecx));
594594
if (rc != 0) {
595595
ok = 0;
596596
}

src/wp_internal.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <wolfprovider/internal.h>
2626
#include <wolfprovider/wp_wolfprov.h>
2727
#include <wolfprovider/wp_logging.h>
28+
#include <wolfprovider/alg_funcs.h>
2829

2930
#include <wolfssl/wolfcrypt/rsa.h>
3031
#include <wolfssl/wolfcrypt/pwdbased.h>
@@ -73,6 +74,76 @@ void wp_provctx_unlock_rng(WOLFPROV_CTX* provCtx)
7374
}
7475
#endif
7576

77+
/**
78+
* Lock the mutex.
79+
*
80+
* This function locks the mutex and translates the return value.
81+
* Use wp_unlock() to unlock after operations are complete.
82+
*
83+
* @param [in] mutex Mutex object.
84+
* @return 1 on success.
85+
* @return 0 on failure or when single-threaded build.
86+
*/
87+
int wp_lock(wolfSSL_Mutex *mutex)
88+
{
89+
#ifndef WP_SINGLE_THREADED
90+
int ok = 1;
91+
int rc;
92+
93+
if (mutex == NULL) {
94+
ok = 0;
95+
}
96+
else {
97+
rc = wc_LockMutex(mutex);
98+
if (rc < 0) {
99+
ok = 0;
100+
}
101+
}
102+
103+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
104+
return ok;
105+
#else
106+
(void)mutex;
107+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
108+
return 1;
109+
#endif
110+
}
111+
112+
/**
113+
* Unlock the mutex.
114+
*
115+
* This function unlocks the mutex and translates the return value.
116+
* Should only be called after a successful wp_lock() call.
117+
*
118+
* @param [in] mutex Mutex object.
119+
* @return 1 on success.
120+
* @return 0 on failure or when single-threaded build.
121+
*/
122+
int wp_unlock(wolfSSL_Mutex* mutex)
123+
{
124+
#ifndef WP_SINGLE_THREADED
125+
int ok = 1;
126+
int rc;
127+
128+
if (mutex == NULL) {
129+
ok = 0;
130+
}
131+
else {
132+
rc = wc_UnLockMutex(mutex);
133+
if (rc < 0) {
134+
ok = 0;
135+
}
136+
}
137+
138+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
139+
return ok;
140+
#else
141+
(void)mutex;
142+
WOLFPROV_LEAVE(WP_LOG_KE, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
143+
return 1;
144+
#endif
145+
}
146+
76147

77148
/**
78149
* Convert the string name of an object to an OpenSSL Numeric ID (NID).

0 commit comments

Comments
 (0)