2 * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include <openssl/ec.h>
16 #include <openssl/evp.h>
17 #include "internal/evp_int.h"
19 #if !defined(OPENSSL_NO_SM2)
20 # include <openssl/sm2.h>
23 /* EC pkey context structure */
26 /* Key and paramgen group */
30 /* Duplicate key if custom cofactor needed */
33 signed char cofactor_mode;
34 /* KDF (if any) to use for ECDH */
36 /* Message digest to use for key derivation */
38 /* User key material */
39 unsigned char *kdf_ukm;
41 /* KDF output length */
45 static int pkey_ec_init(EVP_PKEY_CTX *ctx)
49 dctx = OPENSSL_zalloc(sizeof(*dctx));
53 dctx->cofactor_mode = -1;
54 dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
59 static int pkey_ec_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
61 EC_PKEY_CTX *dctx, *sctx;
62 if (!pkey_ec_init(dst))
66 if (sctx->gen_group) {
67 dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
74 dctx->co_key = EC_KEY_dup(sctx->co_key);
78 dctx->kdf_type = sctx->kdf_type;
79 dctx->kdf_md = sctx->kdf_md;
80 dctx->kdf_outlen = sctx->kdf_outlen;
82 dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
87 dctx->kdf_ukmlen = sctx->kdf_ukmlen;
91 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
93 EC_PKEY_CTX *dctx = ctx->data;
95 EC_GROUP_free(dctx->gen_group);
96 EC_KEY_free(dctx->co_key);
97 OPENSSL_free(dctx->kdf_ukm);
102 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
103 const unsigned char *tbs, size_t tbslen)
107 EC_PKEY_CTX *dctx = ctx->data;
108 EC_KEY *ec = ctx->pkey->pkey.ec;
109 const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
112 *siglen = ECDSA_size(ec);
114 } else if (*siglen < (size_t)ECDSA_size(ec)) {
115 ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
120 type = EVP_MD_type(dctx->md);
124 if (ec_nid == NID_sm2) {
125 #if defined(OPENSSL_NO_SM2)
128 ret = SM2_sign(type, tbs, tbslen, sig, &sltmp, ec);
131 ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
136 *siglen = (size_t)sltmp;
140 static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
141 const unsigned char *sig, size_t siglen,
142 const unsigned char *tbs, size_t tbslen)
145 EC_PKEY_CTX *dctx = ctx->data;
146 EC_KEY *ec = ctx->pkey->pkey.ec;
147 const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
150 type = EVP_MD_type(dctx->md);
154 if (ec_nid == NID_sm2) {
155 #if defined(OPENSSL_NO_SM2)
158 ret = SM2_verify(type, tbs, tbslen, sig, siglen, ec);
161 ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
167 #ifndef OPENSSL_NO_EC
168 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
172 const EC_POINT *pubkey = NULL;
174 EC_PKEY_CTX *dctx = ctx->data;
175 if (!ctx->pkey || !ctx->peerkey) {
176 ECerr(EC_F_PKEY_EC_DERIVE, EC_R_KEYS_NOT_SET);
180 eckey = dctx->co_key ? dctx->co_key : ctx->pkey->pkey.ec;
183 const EC_GROUP *group;
184 group = EC_KEY_get0_group(eckey);
185 *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
188 pubkey = EC_KEY_get0_public_key(ctx->peerkey->pkey.ec);
191 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
192 * an error, the result is truncated.
197 ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
204 static int pkey_ecies_encrypt(EVP_PKEY_CTX *ctx,
205 unsigned char *out, size_t *outlen,
206 const unsigned char *in, size_t inlen)
209 EC_KEY *ec = ctx->pkey->pkey.ec;
210 const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
212 if (ec_nid == NID_sm2) {
213 # if defined(OPENSSL_NO_SM2)
217 EC_PKEY_CTX *dctx = ctx->data;
220 md_type = EVP_MD_type(dctx->md);
221 else if (ec_nid == NID_sm2)
224 md_type = NID_sha256;
227 *outlen = SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type),
232 ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
233 in, inlen, out, outlen);
237 /* standard ECIES not implemented */
244 static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
245 unsigned char *out, size_t *outlen,
246 const unsigned char *in, size_t inlen)
249 EC_KEY *ec = ctx->pkey->pkey.ec;
250 const int ec_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
252 if (ec_nid == NID_sm2) {
253 # if defined(OPENSSL_NO_SM2)
257 EC_PKEY_CTX *dctx = ctx->data;
260 md_type = EVP_MD_type(dctx->md);
261 else if (ec_nid == NID_sm2)
264 md_type = NID_sha256;
267 *outlen = SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen);
271 ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
272 in, inlen, out, outlen);
276 /* standard ECIES not implemented */
283 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
284 unsigned char *key, size_t *keylen)
286 EC_PKEY_CTX *dctx = ctx->data;
287 unsigned char *ktmp = NULL;
290 if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
291 return pkey_ec_derive(ctx, key, keylen);
293 *keylen = dctx->kdf_outlen;
296 if (*keylen != dctx->kdf_outlen)
298 if (!pkey_ec_derive(ctx, NULL, &ktmplen))
300 ktmp = OPENSSL_malloc(ktmplen);
303 if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
306 if (!ECDH_KDF_X9_62(key, *keylen, ktmp, ktmplen,
307 dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md))
312 OPENSSL_clear_free(ktmp, ktmplen);
317 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
319 EC_PKEY_CTX *dctx = ctx->data;
322 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
323 group = EC_GROUP_new_by_curve_name(p1);
325 ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_CURVE);
328 EC_GROUP_free(dctx->gen_group);
329 dctx->gen_group = group;
332 case EVP_PKEY_CTRL_EC_PARAM_ENC:
333 if (!dctx->gen_group) {
334 ECerr(EC_F_PKEY_EC_CTRL, EC_R_NO_PARAMETERS_SET);
337 EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
340 #ifndef OPENSSL_NO_EC
341 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
343 if (dctx->cofactor_mode != -1)
344 return dctx->cofactor_mode;
346 EC_KEY *ec_key = ctx->pkey->pkey.ec;
347 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
349 } else if (p1 < -1 || p1 > 1)
351 dctx->cofactor_mode = p1;
353 EC_KEY *ec_key = ctx->pkey->pkey.ec;
356 /* If cofactor is 1 cofactor mode does nothing */
357 if (BN_is_one(ec_key->group->cofactor))
360 dctx->co_key = EC_KEY_dup(ec_key);
365 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
367 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
369 EC_KEY_free(dctx->co_key);
375 case EVP_PKEY_CTRL_EC_KDF_TYPE:
377 return dctx->kdf_type;
378 if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_62)
383 case EVP_PKEY_CTRL_EC_KDF_MD:
387 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
388 *(const EVP_MD **)p2 = dctx->kdf_md;
391 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
394 dctx->kdf_outlen = (size_t)p1;
397 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
398 *(int *)p2 = dctx->kdf_outlen;
401 case EVP_PKEY_CTRL_EC_KDF_UKM:
402 OPENSSL_free(dctx->kdf_ukm);
405 dctx->kdf_ukmlen = p1;
407 dctx->kdf_ukmlen = 0;
410 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
411 *(unsigned char **)p2 = dctx->kdf_ukm;
412 return dctx->kdf_ukmlen;
414 case EVP_PKEY_CTRL_MD:
415 if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
416 EVP_MD_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
417 EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
418 EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
419 EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
420 EVP_MD_type((const EVP_MD *)p2) != NID_sha512 &&
421 EVP_MD_type((const EVP_MD *)p2) != NID_sm3) {
422 ECerr(EC_F_PKEY_EC_CTRL, EC_R_INVALID_DIGEST_TYPE);
428 case EVP_PKEY_CTRL_GET_MD:
429 *(const EVP_MD **)p2 = dctx->md;
432 case EVP_PKEY_CTRL_PEER_KEY:
433 /* Default behaviour is OK */
434 case EVP_PKEY_CTRL_DIGESTINIT:
435 case EVP_PKEY_CTRL_PKCS7_SIGN:
436 case EVP_PKEY_CTRL_CMS_SIGN:
445 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
446 const char *type, const char *value)
448 if (strcmp(type, "ec_paramgen_curve") == 0) {
450 nid = EC_curve_nist2nid(value);
451 if (nid == NID_undef)
452 nid = OBJ_sn2nid(value);
453 if (nid == NID_undef)
454 nid = OBJ_ln2nid(value);
455 if (nid == NID_undef) {
456 ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_CURVE);
459 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
460 } else if (strcmp(type, "ec_param_enc") == 0) {
462 if (strcmp(value, "explicit") == 0)
464 else if (strcmp(value, "named_curve") == 0)
465 param_enc = OPENSSL_EC_NAMED_CURVE;
468 return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
469 } else if (strcmp(type, "ecdh_kdf_md") == 0) {
471 if ((md = EVP_get_digestbyname(value)) == NULL) {
472 ECerr(EC_F_PKEY_EC_CTRL_STR, EC_R_INVALID_DIGEST);
475 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
476 } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
478 co_mode = atoi(value);
479 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
485 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
488 EC_PKEY_CTX *dctx = ctx->data;
490 if (dctx->gen_group == NULL) {
491 ECerr(EC_F_PKEY_EC_PARAMGEN, EC_R_NO_PARAMETERS_SET);
497 ret = EC_KEY_set_group(ec, dctx->gen_group);
499 EVP_PKEY_assign_EC_KEY(pkey, ec);
505 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
508 EC_PKEY_CTX *dctx = ctx->data;
509 if (ctx->pkey == NULL && dctx->gen_group == NULL) {
510 ECerr(EC_F_PKEY_EC_KEYGEN, EC_R_NO_PARAMETERS_SET);
516 EVP_PKEY_assign_EC_KEY(pkey, ec);
518 /* Note: if error return, pkey is freed by parent routine */
519 if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
522 if (!EC_KEY_set_group(ec, dctx->gen_group))
525 return EC_KEY_generate_key(pkey->pkey.ec);
528 const EVP_PKEY_METHOD ec_pkey_meth = {
558 #ifndef OPENSSL_NO_EC