Skip to content

Commit 7b0fbbb

Browse files
committed
Add export hooks for ecc
1 parent 4fe873f commit 7b0fbbb

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
@@ -28122,37 +28122,47 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2812228122
break;
2812328123
}
2812428124

28125-
/* Export public key if available */
28126-
if (src->type != ECC_PRIVATEKEY_ONLY) {
28127-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28128-
if (ret != 0) {
28129-
WC_FREE_VAR(pubBuf, NULL);
28130-
WC_FREE_VAR(privBuf, NULL);
28131-
break;
28125+
/* Use software to export from src - prevent recursion */
28126+
{
28127+
int savedDevId = src->devId;
28128+
src->devId = INVALID_DEVID;
28129+
28130+
/* Export public key if available */
28131+
if (src->type != ECC_PRIVATEKEY_ONLY) {
28132+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28133+
if (ret != 0) {
28134+
src->devId = savedDevId;
28135+
WC_FREE_VAR(pubBuf, NULL);
28136+
WC_FREE_VAR(privBuf, NULL);
28137+
break;
28138+
}
28139+
pubPtr = pubBuf;
2813228140
}
28133-
pubPtr = pubBuf;
28134-
}
2813528141

28136-
/* Export private key if available */
28137-
if (src->type != ECC_PUBLICKEY) {
28138-
ret = wc_ecc_export_private_only(src, privBuf,
28139-
&privSz);
28140-
if (ret != 0) {
28141-
WC_FREE_VAR(pubBuf, NULL);
28142-
WC_FREE_VAR(privBuf, NULL);
28143-
break;
28142+
/* Export private key if available */
28143+
if (src->type != ECC_PUBLICKEY) {
28144+
ret = wc_ecc_export_private_only(src, privBuf,
28145+
&privSz);
28146+
if (ret != 0) {
28147+
src->devId = savedDevId;
28148+
WC_FREE_VAR(pubBuf, NULL);
28149+
WC_FREE_VAR(privBuf, NULL);
28150+
break;
28151+
}
28152+
28153+
curveId = wc_ecc_get_curve_id(src->idx);
28154+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28155+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28156+
dst, curveId);
28157+
}
28158+
else {
28159+
/* Public key only */
28160+
curveId = wc_ecc_get_curve_id(src->idx);
28161+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28162+
curveId, 0);
2814428163
}
2814528164

28146-
curveId = wc_ecc_get_curve_id(src->idx);
28147-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28148-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28149-
dst, curveId);
28150-
}
28151-
else {
28152-
/* Public key only */
28153-
curveId = wc_ecc_get_curve_id(src->idx);
28154-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28155-
curveId, 0);
28165+
src->devId = savedDevId;
2815628166
}
2815728167
WC_FREE_VAR(pubBuf, NULL);
2815828168
WC_FREE_VAR(privBuf, NULL);

wolfcrypt/src/ecc.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9811,6 +9811,9 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98119811
word32 numlen;
98129812
WC_DECLARE_VAR(buf, byte, ECC_BUFSIZE, 0);
98139813
word32 pubxlen, pubylen;
9814+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9815+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
9816+
#endif
98149817

98159818
/* return length needed only */
98169819
if (key != NULL && out == NULL && outLen != NULL) {
@@ -9826,6 +9829,41 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98269829
if (key->type == ECC_PRIVATEKEY_ONLY)
98279830
return ECC_PRIVATEONLY_E;
98289831

9832+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9833+
#ifndef WOLF_CRYPTO_CB_FIND
9834+
if (key->devId != INVALID_DEVID)
9835+
#endif
9836+
{
9837+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
9838+
if (!WC_VAR_OK(tmpKey)) {
9839+
return MEMORY_E;
9840+
}
9841+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
9842+
9843+
ret = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
9844+
if (ret != 0) {
9845+
WC_FREE_VAR(tmpKey, key->heap);
9846+
return ret;
9847+
}
9848+
9849+
ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
9850+
(void*)key, tmpKey);
9851+
if (ret == 0) {
9852+
/* Recursive call on software tmpKey (INVALID_DEVID) */
9853+
ret = wc_ecc_export_x963(tmpKey, out, outLen);
9854+
}
9855+
9856+
wc_ecc_free(tmpKey);
9857+
WC_FREE_VAR(tmpKey, key->heap);
9858+
9859+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
9860+
return ret;
9861+
}
9862+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
9863+
ret = MP_OKAY;
9864+
}
9865+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
9866+
98299867
#if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
98309868
/* check if public key in secure memory */
98319869
if (key->securePubKey > 0) {
@@ -11065,11 +11103,50 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
1106511103
{
1106611104
int err = 0;
1106711105
word32 keySz;
11106+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11107+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
11108+
#endif
1106811109

1106911110
if (key == NULL) {
1107011111
return BAD_FUNC_ARG;
1107111112
}
1107211113

11114+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11115+
#ifndef WOLF_CRYPTO_CB_FIND
11116+
if (key->devId != INVALID_DEVID)
11117+
#endif
11118+
{
11119+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
11120+
if (!WC_VAR_OK(tmpKey)) {
11121+
return MEMORY_E;
11122+
}
11123+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
11124+
11125+
err = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
11126+
if (err != 0) {
11127+
WC_FREE_VAR(tmpKey, key->heap);
11128+
return err;
11129+
}
11130+
11131+
err = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
11132+
(void*)key, tmpKey);
11133+
if (err == 0) {
11134+
/* Recursive call on software tmpKey (INVALID_DEVID) */
11135+
err = wc_ecc_export_ex(tmpKey, qx, qxLen, qy, qyLen, d, dLen,
11136+
encType);
11137+
}
11138+
11139+
wc_ecc_free(tmpKey);
11140+
WC_FREE_VAR(tmpKey, key->heap);
11141+
11142+
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
11143+
return err;
11144+
}
11145+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
11146+
err = 0;
11147+
}
11148+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
11149+
1107311150
if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
1107411151
return ECC_BAD_ARG_E;
1107511152
}

wolfcrypt/test/test.c

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

65842-
/* Export public key if available */
65843-
if (src->type != ECC_PRIVATEKEY_ONLY) {
65844-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
65845-
if (ret != 0) {
65846-
WC_FREE_VAR(pubBuf, NULL);
65847-
WC_FREE_VAR(privBuf, NULL);
65848-
break;
65842+
/* Use software to export from src - prevent recursion */
65843+
{
65844+
int savedDevId = src->devId;
65845+
src->devId = INVALID_DEVID;
65846+
65847+
/* Export public key if available */
65848+
if (src->type != ECC_PRIVATEKEY_ONLY) {
65849+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
65850+
if (ret != 0) {
65851+
src->devId = savedDevId;
65852+
WC_FREE_VAR(pubBuf, NULL);
65853+
WC_FREE_VAR(privBuf, NULL);
65854+
break;
65855+
}
65856+
pubPtr = pubBuf;
6584965857
}
65850-
pubPtr = pubBuf;
65851-
}
6585265858

65853-
/* Export private key if available */
65854-
if (src->type != ECC_PUBLICKEY) {
65855-
ret = wc_ecc_export_private_only(src, privBuf, &privSz);
65856-
if (ret != 0) {
65857-
WC_FREE_VAR(pubBuf, NULL);
65858-
WC_FREE_VAR(privBuf, NULL);
65859-
break;
65859+
/* Export private key if available */
65860+
if (src->type != ECC_PUBLICKEY) {
65861+
ret = wc_ecc_export_private_only(src, privBuf,
65862+
&privSz);
65863+
if (ret != 0) {
65864+
src->devId = savedDevId;
65865+
WC_FREE_VAR(pubBuf, NULL);
65866+
WC_FREE_VAR(privBuf, NULL);
65867+
break;
65868+
}
65869+
65870+
curveId = wc_ecc_get_curve_id(src->idx);
65871+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
65872+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
65873+
dst, curveId);
65874+
}
65875+
else {
65876+
/* Public key only */
65877+
curveId = wc_ecc_get_curve_id(src->idx);
65878+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
65879+
curveId, 0);
6586065880
}
6586165881

65862-
curveId = wc_ecc_get_curve_id(src->idx);
65863-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
65864-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
65865-
dst, curveId);
65866-
}
65867-
else {
65868-
/* Public key only */
65869-
curveId = wc_ecc_get_curve_id(src->idx);
65870-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
65871-
curveId, 0);
65882+
src->devId = savedDevId;
6587265883
}
6587365884
WC_FREE_VAR(pubBuf, NULL);
6587465885
WC_FREE_VAR(privBuf, NULL);

0 commit comments

Comments
 (0)