Skip to content

Commit 642a65a

Browse files
committed
Add export hooks for ecc
1 parent 79b0d9f commit 642a65a

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
@@ -9863,6 +9863,9 @@ int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen)
98639863
word32 numlen;
98649864
WC_DECLARE_VAR(buf, byte, ECC_BUFSIZE, 0);
98659865
word32 pubxlen, pubylen;
9866+
#if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_EXPORT_KEY)
9867+
WC_DECLARE_VAR(tmpKey, ecc_key, 1, NULL);
9868+
#endif
98669869

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

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

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

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

wolfcrypt/test/test.c

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

66938-
/* Export public key if available */
66939-
if (src->type != ECC_PRIVATEKEY_ONLY) {
66940-
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66941-
if (ret != 0) {
66942-
WC_FREE_VAR(pubBuf, NULL);
66943-
WC_FREE_VAR(privBuf, NULL);
66944-
break;
66938+
/* Use software to export from src - prevent recursion */
66939+
{
66940+
int savedDevId = src->devId;
66941+
src->devId = INVALID_DEVID;
66942+
66943+
/* Export public key if available */
66944+
if (src->type != ECC_PRIVATEKEY_ONLY) {
66945+
ret = wc_ecc_export_x963(src, pubBuf, &pubSz);
66946+
if (ret != 0) {
66947+
src->devId = savedDevId;
66948+
WC_FREE_VAR(pubBuf, NULL);
66949+
WC_FREE_VAR(privBuf, NULL);
66950+
break;
66951+
}
66952+
pubPtr = pubBuf;
6694566953
}
66946-
pubPtr = pubBuf;
66947-
}
6694866954

66949-
/* Export private key if available */
66950-
if (src->type != ECC_PUBLICKEY) {
66951-
ret = wc_ecc_export_private_only(src, privBuf, &privSz);
66952-
if (ret != 0) {
66953-
WC_FREE_VAR(pubBuf, NULL);
66954-
WC_FREE_VAR(privBuf, NULL);
66955-
break;
66955+
/* Export private key if available */
66956+
if (src->type != ECC_PUBLICKEY) {
66957+
ret = wc_ecc_export_private_only(src, privBuf,
66958+
&privSz);
66959+
if (ret != 0) {
66960+
src->devId = savedDevId;
66961+
WC_FREE_VAR(pubBuf, NULL);
66962+
WC_FREE_VAR(privBuf, NULL);
66963+
break;
66964+
}
66965+
66966+
curveId = wc_ecc_get_curve_id(src->idx);
66967+
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66968+
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66969+
dst, curveId);
66970+
}
66971+
else {
66972+
/* Public key only */
66973+
curveId = wc_ecc_get_curve_id(src->idx);
66974+
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66975+
curveId, 0);
6695666976
}
6695766977

66958-
curveId = wc_ecc_get_curve_id(src->idx);
66959-
ret = wc_ecc_import_private_key_ex(privBuf, privSz,
66960-
pubPtr, (pubPtr != NULL) ? pubSz : 0,
66961-
dst, curveId);
66962-
}
66963-
else {
66964-
/* Public key only */
66965-
curveId = wc_ecc_get_curve_id(src->idx);
66966-
ret = wc_ecc_import_x963_ex2(pubBuf, pubSz, dst,
66967-
curveId, 0);
66978+
src->devId = savedDevId;
6696866979
}
6696966980
WC_FREE_VAR(pubBuf, NULL);
6697066981
WC_FREE_VAR(privBuf, NULL);

0 commit comments

Comments
 (0)