Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 99 additions & 7 deletions src/we_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ typedef struct we_Ecc
#endif
} we_Ecc;

static void we_ec_cleanup(EVP_PKEY_CTX *ctx);

/**
* Initialize and set the data required to complete an EC operation.
*
Expand All @@ -332,6 +334,9 @@ static int we_ec_init(EVP_PKEY_CTX *ctx)
int ret, rc;
we_Ecc *ecc;
int keyInited = 0;
#ifndef WE_ECC_USE_GLOBAL_RNG
int rngInited = 0;
#endif

WOLFENGINE_ENTER(WE_LOG_PK, "we_ec_init");
WOLFENGINE_MSG_VERBOSE(WE_LOG_PK, "ARGS [ctx = %p]", ctx);
Expand All @@ -358,7 +363,10 @@ static int we_ec_init(EVP_PKEY_CTX *ctx)
#ifndef WE_ECC_USE_GLOBAL_RNG
if (ret == 1) {
rc = wc_InitRng(&ecc->rng);
if (rc != 0) {
if (rc == 0) {
rngInited = 1;
}
else {
ret = 0;
}
}
Expand All @@ -385,7 +393,9 @@ static int we_ec_init(EVP_PKEY_CTX *ctx)

if (ret == 0 && ecc != NULL) {
#ifndef WE_ECC_USE_GLOBAL_RNG
wc_FreeRng(&ecc->rng);
if (rngInited) {
wc_FreeRng(&ecc->rng);
}
#endif
/* Make sure wolfSSL EC key is freed if initialized. */
if (keyInited) {
Expand Down Expand Up @@ -430,6 +440,7 @@ static int we_ec_p192_init(EVP_PKEY_CTX *ctx)
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name",
ecc->group);
ret = 0;
we_ec_cleanup(ctx);
}
}

Expand Down Expand Up @@ -466,6 +477,7 @@ static int we_ec_p224_init(EVP_PKEY_CTX *ctx)
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name",
ecc->group);
ret = 0;
we_ec_cleanup(ctx);
}
}

Expand Down Expand Up @@ -503,6 +515,7 @@ static int we_ec_p256_init(EVP_PKEY_CTX *ctx)
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name",
ecc->group);
ret = 0;
we_ec_cleanup(ctx);
}
}

Expand Down Expand Up @@ -540,6 +553,7 @@ static int we_ec_p384_init(EVP_PKEY_CTX *ctx)
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name",
ecc->group);
ret = 0;
we_ec_cleanup(ctx);
}
}

Expand Down Expand Up @@ -578,6 +592,7 @@ static int we_ec_p521_init(EVP_PKEY_CTX *ctx)
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_new_by_curve_name",
ecc->group);
ret = 0;
we_ec_cleanup(ctx);
}
}

Expand All @@ -602,15 +617,88 @@ static int we_ec_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
static int we_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
#endif
{
int ret = 1;
we_Ecc *srcEcc;
we_Ecc *dstEcc = NULL;

WOLFENGINE_ENTER(WE_LOG_PK, "we_ec_copy");

/* Nothing to copy as src is empty. */
(void)src;
(void)dst;
srcEcc = (we_Ecc *)EVP_PKEY_CTX_get_data(src);
EVP_PKEY_CTX_set_data(dst, NULL);

if (srcEcc != NULL) {
ret = we_ec_init(dst);
}
if (ret == 1 && srcEcc != NULL) {
dstEcc = (we_Ecc *)EVP_PKEY_CTX_get_data(dst);
dstEcc->curveId = srcEcc->curveId;
dstEcc->curveName = srcEcc->curveName;
#ifdef WE_HAVE_ECDSA
dstEcc->md = srcEcc->md;
#endif
#ifdef WE_HAVE_ECKEYGEN
if (srcEcc->group != NULL) {
dstEcc->group = EC_GROUP_dup(srcEcc->group);
if (dstEcc->group == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "EC_GROUP_dup",
dstEcc->group);
ret = 0;
}
}
#endif
#ifdef WE_HAVE_ECDH
dstEcc->coFactor = srcEcc->coFactor;
dstEcc->kdfType = srcEcc->kdfType;
dstEcc->kdfMd = srcEcc->kdfMd;
dstEcc->kdfOutLen = srcEcc->kdfOutLen;
if (ret == 1 && srcEcc->peerKey != NULL) {
if (srcEcc->peerKeyLen <= 0) {
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Invalid peer key length");
ret = 0;
}
else {
dstEcc->peerKey = OPENSSL_malloc(srcEcc->peerKeyLen);
if (dstEcc->peerKey == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "OPENSSL_malloc",
dstEcc->peerKey);
ret = 0;
}
else {
XMEMCPY(dstEcc->peerKey, srcEcc->peerKey,
srcEcc->peerKeyLen);
dstEcc->peerKeyLen = srcEcc->peerKeyLen;
}
}
}
if (ret == 1 && srcEcc->kdfUkm != NULL) {
if (srcEcc->kdfUkmLen <= 0) {
WOLFENGINE_ERROR_MSG(WE_LOG_PK, "Invalid KDF UKM length");
ret = 0;
}
else {
dstEcc->kdfUkm = OPENSSL_malloc(srcEcc->kdfUkmLen);
if (dstEcc->kdfUkm == NULL) {
WOLFENGINE_ERROR_FUNC_NULL(WE_LOG_PK, "OPENSSL_malloc",
dstEcc->kdfUkm);
ret = 0;
}
else {
XMEMCPY(dstEcc->kdfUkm, srcEcc->kdfUkm,
srcEcc->kdfUkmLen);
dstEcc->kdfUkmLen = srcEcc->kdfUkmLen;
}
}
}
#endif
}

if (ret == 0 && dstEcc != NULL) {
we_ec_cleanup(dst);
}

WOLFENGINE_LEAVE(WE_LOG_PK, "we_ec_copy", 1);
WOLFENGINE_LEAVE(WE_LOG_PK, "we_ec_copy", ret);

return 1;
return ret;
}

/**
Expand Down Expand Up @@ -638,6 +726,10 @@ static void we_ec_cleanup(EVP_PKEY_CTX *ctx)
OPENSSL_free(ecc->peerKey);
ecc->peerKey = NULL;
}
if (ecc->kdfUkm != NULL) {
OPENSSL_free(ecc->kdfUkm);
ecc->kdfUkm = NULL;
}
#endif
#ifndef WE_ECC_USE_GLOBAL_RNG
wc_FreeRng(&ecc->rng);
Expand Down
59 changes: 59 additions & 0 deletions test/test_ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,60 @@ int test_eckeygen_p224(ENGINE *e, void *data)
#endif /* WE_HAVE_EC_P224 */

#ifdef WE_HAVE_EC_P256
#if defined(WE_HAVE_EC_P384) && OPENSSL_VERSION_NUMBER < 0x30000000L
static int test_eckeygen_copy(ENGINE *e)
{
int err;
int shared = 0;
EVP_PKEY_CTX *src = NULL;
EVP_PKEY_CTX *dst = NULL;
EVP_PKEY *key = NULL;
EC_KEY *ecKey = NULL;
const EC_GROUP *group = NULL;

err = (src = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, e)) == NULL;
if (err == 0) {
err = EVP_PKEY_keygen_init(src) != 1;
}
if (err == 0) {
err = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(src,
NID_X9_62_prime256v1) != 1;
}
if (err == 0) {
dst = EVP_PKEY_CTX_dup(src);
err = dst == NULL;
}
if (err == 0) {
shared = EVP_PKEY_CTX_get_data(src) == EVP_PKEY_CTX_get_data(dst);
err = EVP_PKEY_CTX_set_ec_paramgen_curve_nid(src, NID_secp384r1) != 1;
}
if (err == 0) {
err = EVP_PKEY_keygen(dst, &key) != 1;
}
if (err == 0) {
ecKey = EVP_PKEY_get1_EC_KEY(key);
err = ecKey == NULL;
}
if (err == 0) {
group = EC_KEY_get0_group(ecKey);
err = group == NULL;
}
if (err == 0) {
err = EC_GROUP_get_curve_name(group) != NID_X9_62_prime256v1;
}

if (shared) {
EVP_PKEY_CTX_set_data(dst, NULL);
}
EC_KEY_free(ecKey);
EVP_PKEY_free(key);
EVP_PKEY_CTX_free(dst);
EVP_PKEY_CTX_free(src);

return err;
}
#endif

int test_eckeygen_p256_by_nid(ENGINE *e, void *data)
{
int err;
Expand All @@ -420,6 +474,11 @@ int test_eckeygen_p256_by_nid(ENGINE *e, void *data)
PRINT_MSG("Generate key");
err = EVP_PKEY_keygen(ctx, &key) != 1;
}
#if defined(WE_HAVE_EC_P384) && OPENSSL_VERSION_NUMBER < 0x30000000L
if (err == 0) {
err = test_eckeygen_copy(e);
}
#endif

EVP_PKEY_free(key);
EVP_PKEY_CTX_free(ctx);
Expand Down