Skip to content

Commit 19d1e4a

Browse files
committed
Add export hooks for ecc
1 parent 8b092de commit 19d1e4a

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
@@ -28580,37 +28580,47 @@ static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
2858028580
break;
2858128581
}
2858228582

28583-
/* Export public key if available */
28584-
if (src->type != ECC_PRIVATEKEY_ONLY) {
28585-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28586-
if (ret != 0) {
28587-
WC_FREE_VAR(pubBuf, NULL);
28588-
WC_FREE_VAR(privBuf, NULL);
28589-
break;
28583+
/* Use software to export from src - prevent recursion */
28584+
{
28585+
int savedDevId = src->devId;
28586+
src->devId = INVALID_DEVID;
28587+
28588+
/* Export public key if available */
28589+
if (src->type != ECC_PRIVATEKEY_ONLY) {
28590+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
28591+
if (ret != 0) {
28592+
src->devId = savedDevId;
28593+
WC_FREE_VAR(pubBuf, NULL);
28594+
WC_FREE_VAR(privBuf, NULL);
28595+
break;
28596+
}
28597+
pubPtr = pubBuf;
2859028598
}
28591-
pubPtr = pubBuf;
28592-
}
2859328599

28594-
/* Export private key if available */
28595-
if (src->type != ECC_PUBLICKEY) {
28596-
ret = wc_ecc_export_private_only(src, privBuf,
28597-
&privSz);
28598-
if (ret != 0) {
28599-
WC_FREE_VAR(pubBuf, NULL);
28600-
WC_FREE_VAR(privBuf, NULL);
28601-
break;
28600+
/* Export private key if available */
28601+
if (src->type != ECC_PUBLICKEY) {
28602+
ret = wc_ecc_export_private_only(src, privBuf,
28603+
&privSz);
28604+
if (ret != 0) {
28605+
src->devId = savedDevId;
28606+
WC_FREE_VAR(pubBuf, NULL);
28607+
WC_FREE_VAR(privBuf, NULL);
28608+
break;
28609+
}
28610+
28611+
curveId = wc_ecc_get_curve_id(src->idx);
28612+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28613+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28614+
dst, curveId);
28615+
}
28616+
else {
28617+
/* Public key only */
28618+
curveId = wc_ecc_get_curve_id(src->idx);
28619+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28620+
curveId, 0);
2860228621
}
2860328622

28604-
curveId = wc_ecc_get_curve_id(src->idx);
28605-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
28606-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
28607-
dst, curveId);
28608-
}
28609-
else {
28610-
/* Public key only */
28611-
curveId = wc_ecc_get_curve_id(src->idx);
28612-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
28613-
curveId, 0);
28623+
src->devId = savedDevId;
2861428624
}
2861528625
WC_FREE_VAR(pubBuf, NULL);
2861628626
WC_FREE_VAR(privBuf, NULL);

wolfcrypt/src/ecc.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9864,6 +9864,9 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98649864
word32 numlen;
98659865
WC_DECLARE_VAR(buf, byte, ECC_BUFSIZE, 0);
98669866
word32 pubxlen, pubylen;
9867+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9868+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
9869+
#endif
98679870

98689871
/* return length needed only */
98699872
if (key != NULL && out == NULL && outLen != NULL) {
@@ -9879,6 +9882,41 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98799882
if (key->type == ECC_PRIVATEKEY_ONLY)
98809883
return ECC_PRIVATEONLY_E;
98819884

9885+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9886+
#ifndef WOLF_CRYPTO_CB_FIND
9887+
if (key->devId != INVALID_DEVID)
9888+
#endif
9889+
{
9890+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
9891+
if (!WC_VAR_OK(tmpKey)) {
9892+
return MEMORY_E;
9893+
}
9894+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
9895+
9896+
ret = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
9897+
if (ret != 0) {
9898+
WC_FREE_VAR(tmpKey, key->heap);
9899+
return ret;
9900+
}
9901+
9902+
ret = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
9903+
(void*)key, tmpKey);
9904+
if (ret == 0) {
9905+
/* Recursive call on software tmpKey (INVALID_DEVID) */
9906+
ret = wc_ecc_export_x963(tmpKey, out, outLen);
9907+
}
9908+
9909+
wc_ecc_free(tmpKey);
9910+
WC_FREE_VAR(tmpKey, key->heap);
9911+
9912+
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
9913+
return ret;
9914+
}
9915+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
9916+
ret = MP_OKAY;
9917+
}
9918+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
9919+
98829920
#if defined(WOLFSSL_QNX_CAAM) || defined(WOLFSSL_IMXRT1170_CAAM)
98839921
/* check if public key in secure memory */
98849922
if (key->securePubKey > 0) {
@@ -11146,11 +11184,50 @@ int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
1114611184
{
1114711185
int err = 0;
1114811186
word32 keySz;
11187+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11188+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
11189+
#endif
1114911190

1115011191
if (key == NULL) {
1115111192
return BAD_FUNC_ARG;
1115211193
}
1115311194

11195+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
11196+
#ifndef WOLF_CRYPTO_CB_FIND
11197+
if (key->devId != INVALID_DEVID)
11198+
#endif
11199+
{
11200+
WC_ALLOC_VAR(tmpKey, ecc_key, 1, key->heap);
11201+
if (!WC_VAR_OK(tmpKey)) {
11202+
return MEMORY_E;
11203+
}
11204+
XMEMSET(tmpKey, 0, sizeof(ecc_key));
11205+
11206+
err = wc_ecc_init_ex(tmpKey, key->heap, INVALID_DEVID);
11207+
if (err != 0) {
11208+
WC_FREE_VAR(tmpKey, key->heap);
11209+
return err;
11210+
}
11211+
11212+
err = wc_CryptoCb_ExportKey(key->devId, WC_PK_TYPE_ECDSA_SIGN,
11213+
(void*)key, tmpKey);
11214+
if (err == 0) {
11215+
/* Recursive call on software tmpKey (INVALID_DEVID) */
11216+
err = wc_ecc_export_ex(tmpKey, qx, qxLen, qy, qyLen, d, dLen,
11217+
encType);
11218+
}
11219+
11220+
wc_ecc_free(tmpKey);
11221+
WC_FREE_VAR(tmpKey, key->heap);
11222+
11223+
if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
11224+
return err;
11225+
}
11226+
/* CRYPTOCB_UNAVAILABLE: fall through to software export */
11227+
err = 0;
11228+
}
11229+
#endif /* WOLF_CRYPTO_CB && WOLF_CRYPTO_CB_EXPORT_KEY */
11230+
1115411231
if (wc_ecc_is_valid_idx(key->idx) == 0 || key->dp == NULL) {
1115511232
return ECC_BAD_ARG_E;
1115611233
}

wolfcrypt/test/test.c

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

66439-
/* Export public key if available */
66440-
if (src->type != ECC_PRIVATEKEY_ONLY) {
66441-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66442-
if (ret != 0) {
66443-
WC_FREE_VAR(pubBuf, NULL);
66444-
WC_FREE_VAR(privBuf, NULL);
66445-
break;
66439+
/* Use software to export from src - prevent recursion */
66440+
{
66441+
int savedDevId = src->devId;
66442+
src->devId = INVALID_DEVID;
66443+
66444+
/* Export public key if available */
66445+
if (src->type != ECC_PRIVATEKEY_ONLY) {
66446+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66447+
if (ret != 0) {
66448+
src->devId = savedDevId;
66449+
WC_FREE_VAR(pubBuf, NULL);
66450+
WC_FREE_VAR(privBuf, NULL);
66451+
break;
66452+
}
66453+
pubPtr = pubBuf;
6644666454
}
66447-
pubPtr = pubBuf;
66448-
}
6644966455

66450-
/* Export private key if available */
66451-
if (src->type != ECC_PUBLICKEY) {
66452-
ret = wc_ecc_export_private_only(src, privBuf, &privSz);
66453-
if (ret != 0) {
66454-
WC_FREE_VAR(pubBuf, NULL);
66455-
WC_FREE_VAR(privBuf, NULL);
66456-
break;
66456+
/* Export private key if available */
66457+
if (src->type != ECC_PUBLICKEY) {
66458+
ret = wc_ecc_export_private_only(src, privBuf,
66459+
&privSz);
66460+
if (ret != 0) {
66461+
src->devId = savedDevId;
66462+
WC_FREE_VAR(pubBuf, NULL);
66463+
WC_FREE_VAR(privBuf, NULL);
66464+
break;
66465+
}
66466+
66467+
curveId = wc_ecc_get_curve_id(src->idx);
66468+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66469+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66470+
dst, curveId);
66471+
}
66472+
else {
66473+
/* Public key only */
66474+
curveId = wc_ecc_get_curve_id(src->idx);
66475+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66476+
curveId, 0);
6645766477
}
6645866478

66459-
curveId = wc_ecc_get_curve_id(src->idx);
66460-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66461-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66462-
dst, curveId);
66463-
}
66464-
else {
66465-
/* Public key only */
66466-
curveId = wc_ecc_get_curve_id(src->idx);
66467-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66468-
curveId, 0);
66479+
src->devId = savedDevId;
6646966480
}
6647066481
WC_FREE_VAR(pubBuf, NULL);
6647166482
WC_FREE_VAR(privBuf, NULL);

0 commit comments

Comments
 (0)