This commit is contained in:
Zhi Guan
2017-04-14 15:31:35 +08:00
parent 7fa961cd6f
commit eb21e9d572
76 changed files with 3249 additions and 2961 deletions

View File

@@ -41,13 +41,9 @@ typedef struct {
size_t kdf_outlen;
#ifndef OPENSSL_NO_SM2
int sign_type;
int exch_type;
int enc_type;
int dh_type;
union {
void *ptr;
ECIES_PARAMS *ecies;
SM2_ENC_PARAMS *sm2;
} enc_param;
int enc_param;
#endif
} EC_PKEY_CTX;
@@ -63,9 +59,9 @@ static int pkey_ec_init(EVP_PKEY_CTX *ctx)
dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
#ifndef OPENSSL_NO_SM2
dctx->sign_type = NID_secg_scheme;
dctx->exch_type = NID_secg_scheme;
dctx->enc_type = NID_secg_scheme;
dctx->dh_type = NID_secg_scheme;
dctx->enc_param.ptr = NULL;
dctx->enc_param = NID_undef;
#endif
ctx->data = dctx;
return 1;
@@ -102,23 +98,9 @@ static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
dctx->kdf_ukmlen = sctx->kdf_ukmlen;
#ifndef OPENSSL_NO_SM2
dctx->sign_type = sctx->sign_type;
dctx->exch_type = sctx->exch_type;
dctx->enc_type = sctx->enc_type;
dctx->dh_type = sctx->dh_type;
if (sctx->enc_param.ptr) {
if (sctx->enc_type == NID_secg_scheme) {
dctx->enc_param.ecies = ECIES_PARAMS_dup(sctx->enc_param.ecies);
if (!dctx->enc_param.ecies) {
return 0;
}
} else if (sctx->enc_type == NID_sm_scheme) {
dctx->enc_param.sm2 = SM2_ENC_PARAMS_dup(sctx->enc_param.sm2);
if (!dctx->enc_param.sm2) {
return 0;
}
} else {
return 0;
}
}
dctx->enc_param = sctx->enc_param;
#endif
return 1;
}
@@ -130,18 +112,6 @@ static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
EC_GROUP_free(dctx->gen_group);
EC_KEY_free(dctx->co_key);
OPENSSL_free(dctx->kdf_ukm);
#ifndef OPENSSL_NO_SM2
if (dctx->enc_param.ptr) {
if (dctx->enc_type == NID_secg_scheme) {
ECIES_PARAMS_free(dctx->enc_param.ecies);
} else if (dctx->enc_type == NID_sm_scheme) {
SM2_ENC_PARAMS_free(dctx->enc_param.sm2);
} else {
/* this should not happen */
OPENSSL_free(dctx->enc_param.ptr);
}
}
#endif
OPENSSL_free(dctx);
}
}
@@ -214,33 +184,17 @@ static int pkey_ec_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen
switch (dctx->enc_type) {
case NID_sm_scheme:
if (dctx->enc_param.sm2) {
if (!SM2_encrypt(dctx->enc_param.sm2, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_SM2_ENCRYPT_FAILED);
return 0;
}
} else {
if (!SM2_encrypt_with_recommended(in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_SM2_ENCRYPT_WITH_RECOMMENDED_FAILED);
return 0;
}
if (!SM2_encrypt(dctx->enc_param, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_SM2_ENCRYPT_FAILED);
return 0;
}
break;
case NID_secg_scheme:
if (dctx->enc_param.ecies) {
if (!ECIES_encrypt(dctx->enc_param.ecies, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_ECIES_ENCRYPT_FAILED);
return 0;
}
} else {
if (!ECIES_encrypt_with_recommended(in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_ECIES_ENCRYPT_WITH_RECOMMENDED_FAILED);
return 0;
}
if (!ECIES_encrypt(dctx->enc_param, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_ECIES_ENCRYPT_FAILED);
return 0;
}
break;
default:
ECerr(EC_F_PKEY_EC_ENCRYPT, EC_R_INVALID_ENC_TYPE);
return 0;
@@ -257,30 +211,15 @@ static int pkey_ec_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen
switch (dctx->enc_type) {
case NID_sm_scheme:
if (dctx->enc_param.sm2) {
if (!SM2_decrypt(dctx->enc_param.sm2, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_DECRYPT, EC_R_SM2_DECRYPT_FAILED);
return 0;
}
} else {
if (!SM2_decrypt_with_recommended(in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_DECRYPT, EC_R_SM2_DECRYPT_WITH_RECOMMENDED_FAILED);
return 0;
}
if (!SM2_decrypt(dctx->enc_param, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_DECRYPT, EC_R_SM2_DECRYPT_FAILED);
return 0;
}
break;
case NID_secg_scheme:
if (dctx->enc_param.ecies) {
if (!ECIES_decrypt(dctx->enc_param.ecies, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_DECRYPT, EC_R_ECIES_DECRYPT_FAILED);
return 0;
}
} else {
if (!ECIES_decrypt_with_recommended(in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_DECRYPT, EC_R_ECIES_DECRYPT_WITH_RECOMMENDED_FAILED);
return 0;
}
if (!ECIES_decrypt(dctx->enc_param, in, inlen, out, outlen, ec_key)) {
ECerr(EC_F_PKEY_EC_DECRYPT, EC_R_ECIES_DECRYPT_FAILED);
return 0;
}
break;
@@ -325,7 +264,7 @@ static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
outlen = *keylen;
#ifndef OPENSSL_NO_SM2
if (dctx->dh_type == NID_sm_scheme)
if (dctx->exch_type == NID_sm_scheme)
ret = SM2_compute_key(key, outlen, pubkey, eckey, 0);
else
#endif
@@ -465,14 +404,14 @@ static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
case EVP_PKEY_CTRL_EC_DH_TYPE:
if (p1 == -2)
return dctx->dh_type;
return dctx->exch_type;
if (p1 != NID_secg_scheme && p1 != NID_sm_scheme)
return -2;
dctx->dh_type = p1;
dctx->exch_type = p1;
return 1;
case EVP_PKEY_CTRL_GET_EC_DH_TYPE:
*(int *)p2 = dctx->dh_type;
*(int *)p2 = dctx->exch_type;
return 1;
#endif
@@ -556,6 +495,7 @@ static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
}
return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
#ifndef OPENSSL_NO_SM2
} else if (!strcmp(type, "signer")) {
} else if (!strcmp(type, "ec_sign_algor")) {
int sign_type;
if (!strcmp(value, "ecdsa"))