diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index aeba28eb7..d03e500b4 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -4790,10 +4790,38 @@ int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen, #endif /* HAVE_ECC_KEY_EXPORT */ #ifdef HAVE_ECC_KEY_IMPORT -int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, const byte* pub, - word32 pubSz, ecc_key* key, int curve_id) +/* import private key, public part optional if (pub) passed as NULL */ +int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, + const byte* pub, word32 pubSz, ecc_key* key, + int curve_id) { - int ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id); + int ret; + void* heap; + + /* public optional, NULL if only importing private */ + if (pub != NULL) { + + ret = wc_ecc_import_x963_ex(pub, pubSz, key, curve_id); + + } else { + + if (key == NULL || priv == NULL) + return BAD_FUNC_ARG; + + /* init key */ + heap = key->heap; + ret = wc_ecc_init_ex(key, NULL, INVALID_DEVID); + key->heap = heap; + + key->state = ECC_STATE_NONE; + + if (ret != 0) + return ret; + + /* set key size */ + ret = wc_ecc_set_curve(key, privSz-1, curve_id); + } + if (ret != 0) return ret; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 3c87ed0a7..6c8d43a31 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -10261,6 +10261,7 @@ static int ecc_sig_test(WC_RNG* rng, ecc_key* key) static int ecc_exp_imp_test(ecc_key* key) { int ret; + int curve_id; ecc_key keyImp; byte priv[32]; word32 privLen; @@ -10302,6 +10303,19 @@ static int ecc_exp_imp_test(ecc_key* key) goto done; } + wc_ecc_free(&keyImp); + wc_ecc_init(&keyImp); + + curve_id = wc_ecc_get_curve_id(key->idx); + if (curve_id < 0) + return -1074; + + /* test import private only */ + ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, &keyImp, + curve_id); + if (ret != 0) + return -1075; + done: wc_ecc_free(&keyImp); return ret;