Skip to content

Commit 120c011

Browse files
committed
Add export hooks for ecc
1 parent 88a49d5 commit 120c011

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
@@ -28085,37 +28085,47 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2808528085
break;
2808628086
}
2808728087

28088-
/* Export public key if available */
28089-
if (src->type != ECC_PRIVATEKEY_ONLY) {
28090-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28091-
if (ret != 0) {
28092-
WC_FREE_VAR(pubBuf, NULL);
28093-
WC_FREE_VAR(privBuf, NULL);
28094-
break;
28088+
/* Use software to export from src - prevent recursion */
28089+
{
28090+
int savedDevId = src->devId;
28091+
src->devId = INVALID_DEVID;
28092+
28093+
/* Export public key if available */
28094+
if (src->type != ECC_PRIVATEKEY_ONLY) {
28095+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28096+
if (ret != 0) {
28097+
src->devId = savedDevId;
28098+
WC_FREE_VAR(pubBuf, NULL);
28099+
WC_FREE_VAR(privBuf, NULL);
28100+
break;
28101+
}
28102+
pubPtr = pubBuf;
2809528103
}
28096-
pubPtr = pubBuf;
28097-
}
2809828104

28099-
/* Export private key if available */
28100-
if (src->type != ECC_PUBLICKEY) {
28101-
ret = wc_ecc_export_private_only(src, privBuf,
28102-
&privSz);
28103-
if (ret != 0) {
28104-
WC_FREE_VAR(pubBuf, NULL);
28105-
WC_FREE_VAR(privBuf, NULL);
28106-
break;
28105+
/* Export private key if available */
28106+
if (src->type != ECC_PUBLICKEY) {
28107+
ret = wc_ecc_export_private_only(src, privBuf,
28108+
&privSz);
28109+
if (ret != 0) {
28110+
src->devId = savedDevId;
28111+
WC_FREE_VAR(pubBuf, NULL);
28112+
WC_FREE_VAR(privBuf, NULL);
28113+
break;
28114+
}
28115+
28116+
curveId = wc_ecc_get_curve_id(src->idx);
28117+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28118+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28119+
dst, curveId);
28120+
}
28121+
else {
28122+
/* Public key only */
28123+
curveId = wc_ecc_get_curve_id(src->idx);
28124+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28125+
curveId, 0);
2810728126
}
2810828127

28109-
curveId = wc_ecc_get_curve_id(src->idx);
28110-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28111-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28112-
dst, curveId);
28113-
}
28114-
else {
28115-
/* Public key only */
28116-
curveId = wc_ecc_get_curve_id(src->idx);
28117-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28118-
curveId, 0);
28128+
src->devId = savedDevId;
2811928129
}
2812028130
WC_FREE_VAR(pubBuf, NULL);
2812128131
WC_FREE_VAR(privBuf, NULL);

wolfcrypt/src/ecc.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9804,6 +9804,9 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98049804
word32 numlen;
98059805
WC_DECLARE_VAR(buf, byte, ECC_BUFSIZE, 0);
98069806
word32 pubxlen, pubylen;
9807+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9808+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
9809+
#endif
98079810

98089811
/* return length needed only */
98099812
if (key != NULL && out == NULL && outLen != NULL) {
@@ -9819,6 +9822,41 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98199822
if (key->type == ECC_PRIVATEKEY_ONLY)
98209823
return ECC_PRIVATEONLY_E;
98219824

9825+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9826+
#ifndef WOLF_CRYPTO_CB_FIND
9827+
if (key->devId != INVALID_DEVID)
9828+
#endif
9829+
{
9830+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
9831+
if (!WC_VAR_OK(tmpKey)) {
9832+
return MEMORY_E;
9833+
}
9834+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
9835+
9836+
ret = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
9837+
if (ret != 0) {
9838+
WC_FREE_VAR(tmpKey, key->heap);
9839+
return ret;
9840+
}
9841+
9842+
ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
9843+
(void*)key, tmpKey);
9844+
if (ret == 0) {
9845+
/* Recursive call on software tmpKey (INVALID_DEVID) */
9846+
ret = wc_ecc_export_x963(tmpKey, out, outLen);
9847+
}
9848+
9849+
wc_ecc_free(tmpKey);
9850+
WC_FREE_VAR(tmpKey, key->heap);
9851+
9852+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
9853+
return ret;
9854+
}
9855+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
9856+
ret = MP_OKAY;
9857+
}
9858+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
9859+
98229860
#if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
98239861
/* check if public key in secure memory */
98249862
if (key->securePubKey > 0) {
@@ -11055,11 +11093,50 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
1105511093
{
1105611094
int err = 0;
1105711095
word32 keySz;
11096+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11097+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
11098+
#endif
1105811099

1105911100
if (key == NULL) {
1106011101
return BAD_FUNC_ARG;
1106111102
}
1106211103

11104+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11105+
#ifndef WOLF_CRYPTO_CB_FIND
11106+
if (key->devId != INVALID_DEVID)
11107+
#endif
11108+
{
11109+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
11110+
if (!WC_VAR_OK(tmpKey)) {
11111+
return MEMORY_E;
11112+
}
11113+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
11114+
11115+
err = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
11116+
if (err != 0) {
11117+
WC_FREE_VAR(tmpKey, key->heap);
11118+
return err;
11119+
}
11120+
11121+
err = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
11122+
(void*)key, tmpKey);
11123+
if (err == 0) {
11124+
/* Recursive call on software tmpKey (INVALID_DEVID) */
11125+
err = wc_ecc_export_ex(tmpKey, qx, qxLen, qy, qyLen, d, dLen,
11126+
encType);
11127+
}
11128+
11129+
wc_ecc_free(tmpKey);
11130+
WC_FREE_VAR(tmpKey, key->heap);
11131+
11132+
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
11133+
return err;
11134+
}
11135+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
11136+
err = 0;
11137+
}
11138+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
11139+
1106311140
if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
1106411141
return ECC_BAD_ARG_E;
1106511142
}

wolfcrypt/test/test.c

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

65772-
/* Export public key if available */
65773-
if (src->type != ECC_PRIVATEKEY_ONLY) {
65774-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
65775-
if (ret != 0) {
65776-
WC_FREE_VAR(pubBuf, NULL);
65777-
WC_FREE_VAR(privBuf, NULL);
65778-
break;
65772+
/* Use software to export from src - prevent recursion */
65773+
{
65774+
int savedDevId = src->devId;
65775+
src->devId = INVALID_DEVID;
65776+
65777+
/* Export public key if available */
65778+
if (src->type != ECC_PRIVATEKEY_ONLY) {
65779+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
65780+
if (ret != 0) {
65781+
src->devId = savedDevId;
65782+
WC_FREE_VAR(pubBuf, NULL);
65783+
WC_FREE_VAR(privBuf, NULL);
65784+
break;
65785+
}
65786+
pubPtr = pubBuf;
6577965787
}
65780-
pubPtr = pubBuf;
65781-
}
6578265788

65783-
/* Export private key if available */
65784-
if (src->type != ECC_PUBLICKEY) {
65785-
ret = wc_ecc_export_private_only(src, privBuf, &privSz);
65786-
if (ret != 0) {
65787-
WC_FREE_VAR(pubBuf, NULL);
65788-
WC_FREE_VAR(privBuf, NULL);
65789-
break;
65789+
/* Export private key if available */
65790+
if (src->type != ECC_PUBLICKEY) {
65791+
ret = wc_ecc_export_private_only(src, privBuf,
65792+
&privSz);
65793+
if (ret != 0) {
65794+
src->devId = savedDevId;
65795+
WC_FREE_VAR(pubBuf, NULL);
65796+
WC_FREE_VAR(privBuf, NULL);
65797+
break;
65798+
}
65799+
65800+
curveId = wc_ecc_get_curve_id(src->idx);
65801+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
65802+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
65803+
dst, curveId);
65804+
}
65805+
else {
65806+
/* Public key only */
65807+
curveId = wc_ecc_get_curve_id(src->idx);
65808+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
65809+
curveId, 0);
6579065810
}
6579165811

65792-
curveId = wc_ecc_get_curve_id(src->idx);
65793-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
65794-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
65795-
dst, curveId);
65796-
}
65797-
else {
65798-
/* Public key only */
65799-
curveId = wc_ecc_get_curve_id(src->idx);
65800-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
65801-
curveId, 0);
65812+
src->devId = savedDevId;
6580265813
}
6580365814
WC_FREE_VAR(pubBuf, NULL);
6580465815
WC_FREE_VAR(privBuf, NULL);

0 commit comments

Comments
 (0)