2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * ECDH and ECDSA low level APIs are deprecated for public use, but still ok
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/ec.h>
22 #include <openssl/evp.h>
23 #include "crypto/evp.h"
25 /* EC pkey context structure */
28 /* Key and paramgen group */
32 /* Duplicate key if custom cofactor needed */
35 signed char cofactor_mode;
36 /* KDF (if any) to use for ECDH */
38 /* Message digest to use for key derivation */
40 /* User key material */
41 unsigned char *kdf_ukm;
43 /* KDF output length */
47 static int pkey_ec_init(EVP_PKEY_CTX *ctx)
51 if ((dctx = OPENSSL_zalloc(sizeof(*dctx))) == NULL)
54 dctx->cofactor_mode = -1;
55 dctx->kdf_type = EVP_PKEY_ECDH_KDF_NONE;
60 static int pkey_ec_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
62 EC_PKEY_CTX *dctx, *sctx;
63 if (!pkey_ec_init(dst))
67 if (sctx->gen_group) {
68 dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
75 dctx->co_key = EC_KEY_dup(sctx->co_key);
79 dctx->kdf_type = sctx->kdf_type;
80 dctx->kdf_md = sctx->kdf_md;
81 dctx->kdf_outlen = sctx->kdf_outlen;
83 dctx->kdf_ukm = OPENSSL_memdup(sctx->kdf_ukm, sctx->kdf_ukmlen);
88 dctx->kdf_ukmlen = sctx->kdf_ukmlen;
92 static void pkey_ec_cleanup(EVP_PKEY_CTX *ctx)
94 EC_PKEY_CTX *dctx = ctx->data;
96 EC_GROUP_free(dctx->gen_group);
97 EC_KEY_free(dctx->co_key);
98 OPENSSL_free(dctx->kdf_ukm);
104 static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
105 const unsigned char *tbs, size_t tbslen)
109 EC_PKEY_CTX *dctx = ctx->data;
111 * Discard const. Its marked as const because this may be a cached copy of
112 * the "real" key. These calls don't make any modifications that need to
113 * be reflected back in the "original" key.
115 EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
116 const int sig_sz = ECDSA_size(ec);
118 /* ensure cast to size_t is safe */
119 if (!ossl_assert(sig_sz > 0))
123 *siglen = (size_t)sig_sz;
127 if (*siglen < (size_t)sig_sz) {
128 ERR_raise(ERR_LIB_EC, EC_R_BUFFER_TOO_SMALL);
132 type = (dctx->md != NULL) ? EVP_MD_get_type(dctx->md) : NID_sha1;
134 ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);
138 *siglen = (size_t)sltmp;
142 static int pkey_ec_verify(EVP_PKEY_CTX *ctx,
143 const unsigned char *sig, size_t siglen,
144 const unsigned char *tbs, size_t tbslen)
147 EC_PKEY_CTX *dctx = ctx->data;
149 * Discard const. Its marked as const because this may be a cached copy of
150 * the "real" key. These calls don't make any modifications that need to
151 * be reflected back in the "original" key.
153 EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
156 type = EVP_MD_get_type(dctx->md);
160 ret = ECDSA_verify(type, tbs, tbslen, sig, siglen, ec);
165 #ifndef OPENSSL_NO_EC
166 static int pkey_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
170 const EC_POINT *pubkey = NULL;
172 const EC_KEY *eckeypub;
173 EC_PKEY_CTX *dctx = ctx->data;
175 if (ctx->pkey == NULL || ctx->peerkey == NULL) {
176 ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
179 eckeypub = EVP_PKEY_get0_EC_KEY(ctx->peerkey);
180 if (eckeypub == NULL) {
181 ERR_raise(ERR_LIB_EC, EC_R_KEYS_NOT_SET);
185 eckey = dctx->co_key ? dctx->co_key
186 : (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
189 const EC_GROUP *group;
190 group = EC_KEY_get0_group(eckey);
194 *keylen = (EC_GROUP_get_degree(group) + 7) / 8;
197 pubkey = EC_KEY_get0_public_key(eckeypub);
200 * NB: unlike PKCS#3 DH, if *outlen is less than maximum size this is not
201 * an error, the result is truncated.
206 ret = ECDH_compute_key(key, outlen, pubkey, eckey, 0);
213 static int pkey_ec_kdf_derive(EVP_PKEY_CTX *ctx,
214 unsigned char *key, size_t *keylen)
216 EC_PKEY_CTX *dctx = ctx->data;
217 unsigned char *ktmp = NULL;
220 if (dctx->kdf_type == EVP_PKEY_ECDH_KDF_NONE)
221 return pkey_ec_derive(ctx, key, keylen);
223 *keylen = dctx->kdf_outlen;
226 if (*keylen != dctx->kdf_outlen)
228 if (!pkey_ec_derive(ctx, NULL, &ktmplen))
230 if ((ktmp = OPENSSL_malloc(ktmplen)) == NULL)
232 if (!pkey_ec_derive(ctx, ktmp, &ktmplen))
235 if (!ossl_ecdh_kdf_X9_63(key, *keylen, ktmp, ktmplen,
236 dctx->kdf_ukm, dctx->kdf_ukmlen, dctx->kdf_md,
237 ctx->libctx, ctx->propquery))
242 OPENSSL_clear_free(ktmp, ktmplen);
247 static int pkey_ec_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
249 EC_PKEY_CTX *dctx = ctx->data;
252 case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
253 group = EC_GROUP_new_by_curve_name(p1);
255 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
258 EC_GROUP_free(dctx->gen_group);
259 dctx->gen_group = group;
262 case EVP_PKEY_CTRL_EC_PARAM_ENC:
263 if (!dctx->gen_group) {
264 ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
267 EC_GROUP_set_asn1_flag(dctx->gen_group, p1);
270 #ifndef OPENSSL_NO_EC
271 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
273 if (dctx->cofactor_mode != -1)
274 return dctx->cofactor_mode;
276 const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ctx->pkey);
277 return EC_KEY_get_flags(ec_key) & EC_FLAG_COFACTOR_ECDH ? 1 : 0;
279 } else if (p1 < -1 || p1 > 1)
281 dctx->cofactor_mode = p1;
283 EC_KEY *ec_key = (EC_KEY *)EVP_PKEY_get0_EC_KEY(ctx->pkey);
286 * We discarded the "const" above. This will only work if the key is
287 * a "real" legacy key, and not a cached copy of a provided key
289 if (evp_pkey_is_provided(ctx->pkey)) {
290 ERR_raise(ERR_LIB_EC, ERR_R_UNSUPPORTED);
295 /* If cofactor is 1 cofactor mode does nothing */
296 if (BN_is_one(ec_key->group->cofactor))
299 dctx->co_key = EC_KEY_dup(ec_key);
304 EC_KEY_set_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
306 EC_KEY_clear_flags(dctx->co_key, EC_FLAG_COFACTOR_ECDH);
308 EC_KEY_free(dctx->co_key);
314 case EVP_PKEY_CTRL_EC_KDF_TYPE:
316 return dctx->kdf_type;
317 if (p1 != EVP_PKEY_ECDH_KDF_NONE && p1 != EVP_PKEY_ECDH_KDF_X9_63)
322 case EVP_PKEY_CTRL_EC_KDF_MD:
326 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
327 *(const EVP_MD **)p2 = dctx->kdf_md;
330 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
333 dctx->kdf_outlen = (size_t)p1;
336 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
337 *(int *)p2 = dctx->kdf_outlen;
340 case EVP_PKEY_CTRL_EC_KDF_UKM:
341 OPENSSL_free(dctx->kdf_ukm);
344 dctx->kdf_ukmlen = p1;
346 dctx->kdf_ukmlen = 0;
349 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
350 *(unsigned char **)p2 = dctx->kdf_ukm;
351 return dctx->kdf_ukmlen;
353 case EVP_PKEY_CTRL_MD:
354 if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
355 EVP_MD_get_type((const EVP_MD *)p2) != NID_ecdsa_with_SHA1 &&
356 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
357 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
358 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
359 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
360 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
361 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
362 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
363 EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512 &&
364 EVP_MD_get_type((const EVP_MD *)p2) != NID_sm3) {
365 ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST_TYPE);
371 case EVP_PKEY_CTRL_GET_MD:
372 *(const EVP_MD **)p2 = dctx->md;
375 case EVP_PKEY_CTRL_PEER_KEY:
376 /* Default behaviour is OK */
377 case EVP_PKEY_CTRL_DIGESTINIT:
378 case EVP_PKEY_CTRL_PKCS7_SIGN:
379 case EVP_PKEY_CTRL_CMS_SIGN:
388 static int pkey_ec_ctrl_str(EVP_PKEY_CTX *ctx,
389 const char *type, const char *value)
391 if (strcmp(type, "ec_paramgen_curve") == 0) {
393 nid = EC_curve_nist2nid(value);
394 if (nid == NID_undef)
395 nid = OBJ_sn2nid(value);
396 if (nid == NID_undef)
397 nid = OBJ_ln2nid(value);
398 if (nid == NID_undef) {
399 ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);
402 return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
403 } else if (strcmp(type, "ec_param_enc") == 0) {
405 if (strcmp(value, "explicit") == 0)
407 else if (strcmp(value, "named_curve") == 0)
408 param_enc = OPENSSL_EC_NAMED_CURVE;
411 return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
412 } else if (strcmp(type, "ecdh_kdf_md") == 0) {
414 if ((md = EVP_get_digestbyname(value)) == NULL) {
415 ERR_raise(ERR_LIB_EC, EC_R_INVALID_DIGEST);
418 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md);
419 } else if (strcmp(type, "ecdh_cofactor_mode") == 0) {
421 co_mode = atoi(value);
422 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, co_mode);
428 static int pkey_ec_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
431 EC_PKEY_CTX *dctx = ctx->data;
434 if (dctx->gen_group == NULL) {
435 ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
441 if (!(ret = EC_KEY_set_group(ec, dctx->gen_group))
442 || !ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
447 static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
450 EC_PKEY_CTX *dctx = ctx->data;
453 if (ctx->pkey == NULL && dctx->gen_group == NULL) {
454 ERR_raise(ERR_LIB_EC, EC_R_NO_PARAMETERS_SET);
460 if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
464 /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
465 if (ctx->pkey != NULL)
466 ret = EVP_PKEY_copy_parameters(pkey, ctx->pkey);
468 ret = EC_KEY_set_group(ec, dctx->gen_group);
470 return ret ? EC_KEY_generate_key(ec) : 0;
473 static const EVP_PKEY_METHOD ec_pkey_meth = {
503 #ifndef OPENSSL_NO_EC
512 const EVP_PKEY_METHOD *ossl_ec_pkey_method(void)
514 return &ec_pkey_meth;