Skip to content

Commit 1207849

Browse files
committed
Add export hooks for ecc
1 parent ea76f23 commit 1207849

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
@@ -28567,37 +28567,47 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2856728567
break;
2856828568
}
2856928569

28570-
/* Export public key if available */
28571-
if (src->type != ECC_PRIVATEKEY_ONLY) {
28572-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28573-
if (ret != 0) {
28574-
WC_FREE_VAR(pubBuf, NULL);
28575-
WC_FREE_VAR(privBuf, NULL);
28576-
break;
28570+
/* Use software to export from src - prevent recursion */
28571+
{
28572+
int savedDevId = src->devId;
28573+
src->devId = INVALID_DEVID;
28574+
28575+
/* Export public key if available */
28576+
if (src->type != ECC_PRIVATEKEY_ONLY) {
28577+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28578+
if (ret != 0) {
28579+
src->devId = savedDevId;
28580+
WC_FREE_VAR(pubBuf, NULL);
28581+
WC_FREE_VAR(privBuf, NULL);
28582+
break;
28583+
}
28584+
pubPtr = pubBuf;
2857728585
}
28578-
pubPtr = pubBuf;
28579-
}
2858028586

28581-
/* Export private key if available */
28582-
if (src->type != ECC_PUBLICKEY) {
28583-
ret = wc_ecc_export_private_only(src, privBuf,
28584-
&privSz);
28585-
if (ret != 0) {
28586-
WC_FREE_VAR(pubBuf, NULL);
28587-
WC_FREE_VAR(privBuf, NULL);
28588-
break;
28587+
/* Export private key if available */
28588+
if (src->type != ECC_PUBLICKEY) {
28589+
ret = wc_ecc_export_private_only(src, privBuf,
28590+
&privSz);
28591+
if (ret != 0) {
28592+
src->devId = savedDevId;
28593+
WC_FREE_VAR(pubBuf, NULL);
28594+
WC_FREE_VAR(privBuf, NULL);
28595+
break;
28596+
}
28597+
28598+
curveId = wc_ecc_get_curve_id(src->idx);
28599+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28600+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28601+
dst, curveId);
28602+
}
28603+
else {
28604+
/* Public key only */
28605+
curveId = wc_ecc_get_curve_id(src->idx);
28606+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28607+
curveId, 0);
2858928608
}
2859028609

28591-
curveId = wc_ecc_get_curve_id(src->idx);
28592-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28593-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28594-
dst, curveId);
28595-
}
28596-
else {
28597-
/* Public key only */
28598-
curveId = wc_ecc_get_curve_id(src->idx);
28599-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28600-
curveId, 0);
28610+
src->devId = savedDevId;
2860128611
}
2860228612
WC_FREE_VAR(pubBuf, NULL);
2860328613
WC_FREE_VAR(privBuf, NULL);

wolfcrypt/src/ecc.c

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

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

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

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

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

wolfcrypt/test/test.c

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

66122-
/* Export public key if available */
66123-
if (src->type != ECC_PRIVATEKEY_ONLY) {
66124-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66125-
if (ret != 0) {
66126-
WC_FREE_VAR(pubBuf, NULL);
66127-
WC_FREE_VAR(privBuf, NULL);
66128-
break;
66122+
/* Use software to export from src - prevent recursion */
66123+
{
66124+
int savedDevId = src->devId;
66125+
src->devId = INVALID_DEVID;
66126+
66127+
/* Export public key if available */
66128+
if (src->type != ECC_PRIVATEKEY_ONLY) {
66129+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66130+
if (ret != 0) {
66131+
src->devId = savedDevId;
66132+
WC_FREE_VAR(pubBuf, NULL);
66133+
WC_FREE_VAR(privBuf, NULL);
66134+
break;
66135+
}
66136+
pubPtr = pubBuf;
6612966137
}
66130-
pubPtr = pubBuf;
66131-
}
6613266138

66133-
/* Export private key if available */
66134-
if (src->type != ECC_PUBLICKEY) {
66135-
ret = wc_ecc_export_private_only(src, privBuf, &privSz);
66136-
if (ret != 0) {
66137-
WC_FREE_VAR(pubBuf, NULL);
66138-
WC_FREE_VAR(privBuf, NULL);
66139-
break;
66139+
/* Export private key if available */
66140+
if (src->type != ECC_PUBLICKEY) {
66141+
ret = wc_ecc_export_private_only(src, privBuf,
66142+
&privSz);
66143+
if (ret != 0) {
66144+
src->devId = savedDevId;
66145+
WC_FREE_VAR(pubBuf, NULL);
66146+
WC_FREE_VAR(privBuf, NULL);
66147+
break;
66148+
}
66149+
66150+
curveId = wc_ecc_get_curve_id(src->idx);
66151+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66152+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66153+
dst, curveId);
66154+
}
66155+
else {
66156+
/* Public key only */
66157+
curveId = wc_ecc_get_curve_id(src->idx);
66158+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66159+
curveId, 0);
6614066160
}
6614166161

66142-
curveId = wc_ecc_get_curve_id(src->idx);
66143-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66144-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66145-
dst, curveId);
66146-
}
66147-
else {
66148-
/* Public key only */
66149-
curveId = wc_ecc_get_curve_id(src->idx);
66150-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66151-
curveId, 0);
66162+
src->devId = savedDevId;
6615266163
}
6615366164
WC_FREE_VAR(pubBuf, NULL);
6615466165
WC_FREE_VAR(privBuf, NULL);

0 commit comments

Comments
 (0)