Skip to content

Commit 9593d2a

Browse files
committed
Add export hooks for ecc
1 parent 6aee6af commit 9593d2a

3 files changed

Lines changed: 151 additions & 53 deletions

File tree

tests/api.c

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28346,37 +28346,47 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2834628346
break;
2834728347
}
2834828348

28349-
/* Export public key if available */
28350-
if (src->type != ECC_PRIVATEKEY_ONLY) {
28351-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28352-
if (ret != 0) {
28353-
WC_FREE_VAR(pubBuf, NULL);
28354-
WC_FREE_VAR(privBuf, NULL);
28355-
break;
28349+
/* Use software to export from src - prevent recursion */
28350+
{
28351+
int savedDevId = src->devId;
28352+
src->devId = INVALID_DEVID;
28353+
28354+
/* Export public key if available */
28355+
if (src->type != ECC_PRIVATEKEY_ONLY) {
28356+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28357+
if (ret != 0) {
28358+
src->devId = savedDevId;
28359+
WC_FREE_VAR(pubBuf, NULL);
28360+
WC_FREE_VAR(privBuf, NULL);
28361+
break;
28362+
}
28363+
pubPtr = pubBuf;
2835628364
}
28357-
pubPtr = pubBuf;
28358-
}
2835928365

28360-
/* Export private key if available */
28361-
if (src->type != ECC_PUBLICKEY) {
28362-
ret = wc_ecc_export_private_only(src, privBuf,
28363-
&privSz);
28364-
if (ret != 0) {
28365-
WC_FREE_VAR(pubBuf, NULL);
28366-
WC_FREE_VAR(privBuf, NULL);
28367-
break;
28366+
/* Export private key if available */
28367+
if (src->type != ECC_PUBLICKEY) {
28368+
ret = wc_ecc_export_private_only(src, privBuf,
28369+
&privSz);
28370+
if (ret != 0) {
28371+
src->devId = savedDevId;
28372+
WC_FREE_VAR(pubBuf, NULL);
28373+
WC_FREE_VAR(privBuf, NULL);
28374+
break;
28375+
}
28376+
28377+
curveId = wc_ecc_get_curve_id(src->idx);
28378+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28379+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28380+
dst, curveId);
28381+
}
28382+
else {
28383+
/* Public key only */
28384+
curveId = wc_ecc_get_curve_id(src->idx);
28385+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28386+
curveId, 0);
2836828387
}
2836928388

28370-
curveId = wc_ecc_get_curve_id(src->idx);
28371-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28372-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28373-
dst, curveId);
28374-
}
28375-
else {
28376-
/* Public key only */
28377-
curveId = wc_ecc_get_curve_id(src->idx);
28378-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28379-
curveId, 0);
28389+
src->devId = savedDevId;
2838028390
}
2838128391
WC_FREE_VAR(pubBuf, NULL);
2838228392
WC_FREE_VAR(privBuf, NULL);

wolfcrypt/src/ecc.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9870,6 +9870,9 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98709870
word32 numlen;
98719871
WC_DECLARE_VAR(buf, byte, ECC_BUFSIZE, 0);
98729872
word32 pubxlen, pubylen;
9873+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9874+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
9875+
#endif
98739876

98749877
/* return length needed only */
98759878
if (key != NULL && out == NULL && outLen != NULL) {
@@ -9885,6 +9888,41 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98859888
if (key->type == ECC_PRIVATEKEY_ONLY)
98869889
return ECC_PRIVATEONLY_E;
98879890

9891+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9892+
#ifndef WOLF_CRYPTO_CB_FIND
9893+
if (key->devId != INVALID_DEVID)
9894+
#endif
9895+
{
9896+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
9897+
if (!WC_VAR_OK(tmpKey)) {
9898+
return MEMORY_E;
9899+
}
9900+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
9901+
9902+
ret = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
9903+
if (ret != 0) {
9904+
WC_FREE_VAR(tmpKey, key->heap);
9905+
return ret;
9906+
}
9907+
9908+
ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
9909+
(void*)key, tmpKey);
9910+
if (ret == 0) {
9911+
/* Recursive call on software tmpKey (INVALID_DEVID) */
9912+
ret = wc_ecc_export_x963(tmpKey, out, outLen);
9913+
}
9914+
9915+
wc_ecc_free(tmpKey);
9916+
WC_FREE_VAR(tmpKey, key->heap);
9917+
9918+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
9919+
return ret;
9920+
}
9921+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
9922+
ret = MP_OKAY;
9923+
}
9924+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
9925+
98889926
#if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
98899927
/* check if public key in secure memory */
98909928
if (key->securePubKey > 0) {
@@ -11124,11 +11162,50 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
1112411162
{
1112511163
int err = 0;
1112611164
word32 keySz;
11165+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11166+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
11167+
#endif
1112711168

1112811169
if (key == NULL) {
1112911170
return BAD_FUNC_ARG;
1113011171
}
1113111172

11173+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11174+
#ifndef WOLF_CRYPTO_CB_FIND
11175+
if (key->devId != INVALID_DEVID)
11176+
#endif
11177+
{
11178+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
11179+
if (!WC_VAR_OK(tmpKey)) {
11180+
return MEMORY_E;
11181+
}
11182+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
11183+
11184+
err = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
11185+
if (err != 0) {
11186+
WC_FREE_VAR(tmpKey, key->heap);
11187+
return err;
11188+
}
11189+
11190+
err = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
11191+
(void*)key, tmpKey);
11192+
if (err == 0) {
11193+
/* Recursive call on software tmpKey (INVALID_DEVID) */
11194+
err = wc_ecc_export_ex(tmpKey, qx, qxLen, qy, qyLen, d, dLen,
11195+
encType);
11196+
}
11197+
11198+
wc_ecc_free(tmpKey);
11199+
WC_FREE_VAR(tmpKey, key->heap);
11200+
11201+
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
11202+
return err;
11203+
}
11204+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
11205+
err = 0;
11206+
}
11207+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
11208+
1113211209
if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
1113311210
return ECC_BAD_ARG_E;
1113411211
}

wolfcrypt/test/test.c

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66011,36 +66011,47 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
6601166011
break;
6601266012
}
6601366013

66014-
/* Export public key if available */
66015-
if (src->type != ECC_PRIVATEKEY_ONLY) {
66016-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66017-
if (ret != 0) {
66018-
WC_FREE_VAR(pubBuf, NULL);
66019-
WC_FREE_VAR(privBuf, NULL);
66020-
break;
66014+
/* Use software to export from src - prevent recursion */
66015+
{
66016+
int savedDevId = src->devId;
66017+
src->devId = INVALID_DEVID;
66018+
66019+
/* Export public key if available */
66020+
if (src->type != ECC_PRIVATEKEY_ONLY) {
66021+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66022+
if (ret != 0) {
66023+
src->devId = savedDevId;
66024+
WC_FREE_VAR(pubBuf, NULL);
66025+
WC_FREE_VAR(privBuf, NULL);
66026+
break;
66027+
}
66028+
pubPtr = pubBuf;
6602166029
}
66022-
pubPtr = pubBuf;
66023-
}
6602466030

66025-
/* Export private key if available */
66026-
if (src->type != ECC_PUBLICKEY) {
66027-
ret = wc_ecc_export_private_only(src, privBuf, &privSz);
66028-
if (ret != 0) {
66029-
WC_FREE_VAR(pubBuf, NULL);
66030-
WC_FREE_VAR(privBuf, NULL);
66031-
break;
66031+
/* Export private key if available */
66032+
if (src->type != ECC_PUBLICKEY) {
66033+
ret = wc_ecc_export_private_only(src, privBuf,
66034+
&privSz);
66035+
if (ret != 0) {
66036+
src->devId = savedDevId;
66037+
WC_FREE_VAR(pubBuf, NULL);
66038+
WC_FREE_VAR(privBuf, NULL);
66039+
break;
66040+
}
66041+
66042+
curveId = wc_ecc_get_curve_id(src->idx);
66043+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66044+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66045+
dst, curveId);
66046+
}
66047+
else {
66048+
/* Public key only */
66049+
curveId = wc_ecc_get_curve_id(src->idx);
66050+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66051+
curveId, 0);
6603266052
}
6603366053

66034-
curveId = wc_ecc_get_curve_id(src->idx);
66035-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66036-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66037-
dst, curveId);
66038-
}
66039-
else {
66040-
/* Public key only */
66041-
curveId = wc_ecc_get_curve_id(src->idx);
66042-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66043-
curveId, 0);
66054+
src->devId = savedDevId;
6604466055
}
6604566056
WC_FREE_VAR(pubBuf, NULL);
6604666057
WC_FREE_VAR(privBuf, NULL);

0 commit comments

Comments
 (0)