Skip to content

Commit 79a5c49

Browse files
lealem47Lealem Amedie
andauthored
Skip Async_DevCtxInit when using init rsa/ecc label/id api's (#6393)
* Skip Async_DevCtxInit when using init rsa/ecc label/id api's --------- Co-authored-by: Lealem Amedie <lealem47@github.com>
1 parent 56cd8c3 commit 79a5c49

4 files changed

Lines changed: 67 additions & 9 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5804,12 +5804,21 @@ int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key)
58045804
WOLFSSL_ABI
58055805
int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
58065806
{
5807-
int ret = 0;
5807+
int ret = 0;
5808+
#if defined(HAVE_PKCS11)
5809+
int isPkcs11 = 0;
5810+
#endif
58085811

58095812
if (key == NULL) {
58105813
return BAD_FUNC_ARG;
58115814
}
58125815

5816+
#if defined(HAVE_PKCS11)
5817+
if (key->isPkcs11) {
5818+
isPkcs11 = 1;
5819+
}
5820+
#endif
5821+
58135822
#ifdef ECC_DUMP_OID
58145823
wc_ecc_dump_oids();
58155824
#endif
@@ -5862,9 +5871,16 @@ int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
58625871
#endif
58635872

58645873
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
5865-
/* handle as async */
5866-
ret = wolfAsync_DevCtxInit(&key->asyncDev, WOLFSSL_ASYNC_MARKER_ECC,
5874+
#if defined(HAVE_PKCS11)
5875+
if (!isPkcs11)
5876+
#endif
5877+
{
5878+
/* handle as async */
5879+
ret = wolfAsync_DevCtxInit(&key->asyncDev, WOLFSSL_ASYNC_MARKER_ECC,
58675880
key->heap, devId);
5881+
}
5882+
#elif defined(HAVE_PKCS11)
5883+
(void)isPkcs11;
58685884
#endif
58695885

58705886
#if defined(WOLFSSL_DSP)
@@ -5917,6 +5933,11 @@ int wc_ecc_init_id(ecc_key* key, unsigned char* id, int len, void* heap,
59175933
if (ret == 0 && (len < 0 || len > ECC_MAX_ID_LEN))
59185934
ret = BUFFER_E;
59195935

5936+
#if defined(HAVE_PKCS11)
5937+
XMEMSET(key, 0, sizeof(ecc_key));
5938+
key->isPkcs11 = 1;
5939+
#endif
5940+
59205941
if (ret == 0)
59215942
ret = wc_ecc_init_ex(key, heap, devId);
59225943
if (ret == 0 && id != NULL && len != 0) {
@@ -5947,6 +5968,11 @@ int wc_ecc_init_label(ecc_key* key, const char* label, void* heap, int devId)
59475968
ret = BUFFER_E;
59485969
}
59495970

5971+
#if defined(HAVE_PKCS11)
5972+
XMEMSET(key, 0, sizeof(ecc_key));
5973+
key->isPkcs11 = 1;
5974+
#endif
5975+
59505976
if (ret == 0)
59515977
ret = wc_ecc_init_ex(key, heap, devId);
59525978
if (ret == 0) {

wolfcrypt/src/rsa.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,21 @@ static void wc_RsaCleanup(RsaKey* key)
267267

268268
int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
269269
{
270-
int ret = 0;
270+
int ret = 0;
271+
#if defined(HAVE_PKCS11)
272+
int isPkcs11 = 0;
273+
#endif
271274

272275
if (key == NULL) {
273276
return BAD_FUNC_ARG;
274277
}
275278

279+
#if defined(HAVE_PKCS11)
280+
if (key->isPkcs11) {
281+
isPkcs11 = 1;
282+
}
283+
#endif
284+
276285
XMEMSET(key, 0, sizeof(RsaKey));
277286

278287
key->type = RSA_TYPE_UNKNOWN;
@@ -299,12 +308,19 @@ int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId)
299308
#endif
300309

301310
#ifdef WC_ASYNC_ENABLE_RSA
302-
/* handle as async */
303-
ret = wolfAsync_DevCtxInit(&key->asyncDev, WOLFSSL_ASYNC_MARKER_RSA,
304-
key->heap, devId);
305-
if (ret != 0)
306-
return ret;
311+
#if defined(HAVE_PKCS11)
312+
if (!isPkcs11)
313+
#endif
314+
{
315+
/* handle as async */
316+
ret = wolfAsync_DevCtxInit(&key->asyncDev,
317+
WOLFSSL_ASYNC_MARKER_RSA, key->heap, devId);
318+
if (ret != 0)
319+
return ret;
320+
}
307321
#endif /* WC_ASYNC_ENABLE_RSA */
322+
#elif defined(HAVE_PKCS11)
323+
(void)isPkcs11;
308324
#endif /* WOLFSSL_ASYNC_CRYPT */
309325

310326
#ifndef WOLFSSL_RSA_PUBLIC_ONLY
@@ -370,6 +386,11 @@ int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len, void* heap,
370386
if (ret == 0 && (len < 0 || len > RSA_MAX_ID_LEN))
371387
ret = BUFFER_E;
372388

389+
#if defined(HAVE_PKCS11)
390+
XMEMSET(key, 0, sizeof(RsaKey));
391+
key->isPkcs11 = 1;
392+
#endif
393+
373394
if (ret == 0)
374395
ret = wc_InitRsaKey_ex(key, heap, devId);
375396
if (ret == 0 && id != NULL && len != 0) {
@@ -400,6 +421,11 @@ int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap, int devId)
400421
ret = BUFFER_E;
401422
}
402423

424+
#if defined(HAVE_PKCS11)
425+
XMEMSET(key, 0, sizeof(RsaKey));
426+
key->isPkcs11 = 1;
427+
#endif
428+
403429
if (ret == 0)
404430
ret = wc_InitRsaKey_ex(key, heap, devId);
405431
if (ret == 0) {

wolfssl/wolfcrypt/ecc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,9 @@ struct ecc_key {
504504
#if defined(PLUTON_CRYPTO_ECC) || defined(WOLF_CRYPTO_CB)
505505
int devId;
506506
#endif
507+
#if defined(HAVE_PKCS11)
508+
byte isPkcs11 : 1; /* indicate if PKCS11 is preferred */
509+
#endif
507510
#ifdef WOLFSSL_SILABS_SE_ACCEL
508511
sl_se_command_context_t cmd_ctx;
509512
sl_se_key_descriptor_t key;

wolfssl/wolfcrypt/rsa.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ struct RsaKey {
215215
#ifdef WOLF_CRYPTO_CB
216216
int devId;
217217
#endif
218+
#if defined(HAVE_PKCS11)
219+
byte isPkcs11 : 1; /* indicate if PKCS11 is preferred */
220+
#endif
218221
#ifdef WOLFSSL_ASYNC_CRYPT
219222
WC_ASYNC_DEV asyncDev;
220223
#ifdef WOLFSSL_CERT_GEN

0 commit comments

Comments
 (0)