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/x509.h>
19 #include <openssl/ec.h>
20 #include <openssl/bn.h>
21 #include <openssl/asn1t.h>
22 #include "crypto/asn1.h"
23 #include "crypto/evp.h"
24 #include "crypto/x509.h"
25 #include <openssl/core_names.h>
26 #include <openssl/param_build.h>
29 static int eckey_param2type(int *pptype, void **ppval, const EC_KEY *ec_key)
31 const EC_GROUP *group;
34 if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
35 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
38 if (EC_GROUP_get_asn1_flag(group)
39 && (nid = EC_GROUP_get_curve_name(group)))
40 /* we have a 'named curve' => just set the OID */
42 ASN1_OBJECT *asn1obj = OBJ_nid2obj(nid);
44 if (asn1obj == NULL || OBJ_length(asn1obj) == 0) {
45 ASN1_OBJECT_free(asn1obj);
46 ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID);
50 *pptype = V_ASN1_OBJECT;
51 } else { /* explicit parameters */
53 ASN1_STRING *pstr = NULL;
54 pstr = ASN1_STRING_new();
57 pstr->length = i2d_ECParameters(ec_key, &pstr->data);
58 if (pstr->length <= 0) {
59 ASN1_STRING_free(pstr);
60 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
64 *pptype = V_ASN1_SEQUENCE;
69 static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
71 const EC_KEY *ec_key = pkey->pkey.ec;
74 unsigned char *penc = NULL, *p;
77 if (!eckey_param2type(&ptype, &pval, ec_key)) {
78 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
81 penclen = i2o_ECPublicKey(ec_key, NULL);
84 penc = OPENSSL_malloc(penclen);
88 penclen = i2o_ECPublicKey(ec_key, &p);
91 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC),
92 ptype, pval, penc, penclen))
95 if (ptype == V_ASN1_OBJECT)
96 ASN1_OBJECT_free(pval);
98 ASN1_STRING_free(pval);
103 static int eckey_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
105 const unsigned char *p = NULL;
107 EC_KEY *eckey = NULL;
109 OSSL_LIB_CTX *libctx = NULL;
110 const char *propq = NULL;
112 if (!ossl_x509_PUBKEY_get0_libctx(&libctx, &propq, pubkey)
113 || !X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
115 eckey = ossl_ec_key_param_from_x509_algor(palg, libctx, propq);
120 /* We have parameters now set public key */
121 if (!o2i_ECPublicKey(&eckey, &p, pklen)) {
122 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
126 EVP_PKEY_assign_EC_KEY(pkey, eckey);
134 static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
137 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
138 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
139 *pb = EC_KEY_get0_public_key(b->pkey.ec);
141 if (group == NULL || pa == NULL || pb == NULL)
143 r = EC_POINT_cmp(group, pa, pb, NULL);
151 static int eckey_priv_decode_ex(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8,
152 OSSL_LIB_CTX *libctx, const char *propq)
155 EC_KEY *eckey = ossl_ec_key_from_pkcs8(p8, libctx, propq);
159 EVP_PKEY_assign_EC_KEY(pkey, eckey);
165 static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
167 EC_KEY ec_key = *(pkey->pkey.ec);
168 unsigned char *ep = NULL;
171 unsigned int old_flags;
173 if (!eckey_param2type(&ptype, &pval, &ec_key)) {
174 ERR_raise(ERR_LIB_EC, EC_R_DECODE_ERROR);
178 /* set the private key */
181 * do not include the parameters in the SEC1 private key see PKCS#11
184 old_flags = EC_KEY_get_enc_flags(&ec_key);
185 EC_KEY_set_enc_flags(&ec_key, old_flags | EC_PKEY_NO_PARAMETERS);
187 eplen = i2d_ECPrivateKey(&ec_key, &ep);
189 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
190 ASN1_STRING_free(pval);
194 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0,
195 ptype, pval, ep, eplen)) {
196 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
197 ASN1_STRING_free(pval);
198 OPENSSL_clear_free(ep, eplen);
205 static int int_ec_size(const EVP_PKEY *pkey)
207 return ECDSA_size(pkey->pkey.ec);
210 static int ec_bits(const EVP_PKEY *pkey)
212 return EC_GROUP_order_bits(EC_KEY_get0_group(pkey->pkey.ec));
215 static int ec_security_bits(const EVP_PKEY *pkey)
217 int ecbits = ec_bits(pkey);
232 static int ec_missing_parameters(const EVP_PKEY *pkey)
234 if (pkey->pkey.ec == NULL || EC_KEY_get0_group(pkey->pkey.ec) == NULL)
239 static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
241 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
245 if (to->pkey.ec == NULL) {
246 to->pkey.ec = EC_KEY_new();
247 if (to->pkey.ec == NULL)
250 if (EC_KEY_set_group(to->pkey.ec, group) == 0)
252 EC_GROUP_free(group);
255 EC_GROUP_free(group);
259 static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
261 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
262 *group_b = EC_KEY_get0_group(b->pkey.ec);
264 if (group_a == NULL || group_b == NULL)
266 if (EC_GROUP_cmp(group_a, group_b, NULL))
272 static void int_ec_free(EVP_PKEY *pkey)
274 EC_KEY_free(pkey->pkey.ec);
278 EC_KEY_PRINT_PRIVATE,
283 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, ec_print_t ktype)
286 unsigned char *priv = NULL, *pub = NULL;
287 size_t privlen = 0, publen = 0;
289 const EC_GROUP *group;
291 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
292 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
296 if (ktype != EC_KEY_PRINT_PARAM && EC_KEY_get0_public_key(x) != NULL) {
297 publen = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
302 if (ktype == EC_KEY_PRINT_PRIVATE && EC_KEY_get0_private_key(x) != NULL) {
303 privlen = EC_KEY_priv2buf(x, &priv);
308 if (ktype == EC_KEY_PRINT_PRIVATE)
309 ecstr = "Private-Key";
310 else if (ktype == EC_KEY_PRINT_PUBLIC)
311 ecstr = "Public-Key";
313 ecstr = "ECDSA-Parameters";
315 if (!BIO_indent(bp, off, 128))
317 if (BIO_printf(bp, "%s: (%d bit)\n", ecstr,
318 EC_GROUP_order_bits(group)) <= 0)
322 if (BIO_printf(bp, "%*spriv:\n", off, "") <= 0)
324 if (ASN1_buf_print(bp, priv, privlen, off + 4) == 0)
329 if (BIO_printf(bp, "%*spub:\n", off, "") <= 0)
331 if (ASN1_buf_print(bp, pub, publen, off + 4) == 0)
335 if (!ECPKParameters_print(bp, group, off))
340 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
341 OPENSSL_clear_free(priv, privlen);
346 static int eckey_param_decode(EVP_PKEY *pkey,
347 const unsigned char **pder, int derlen)
351 if ((eckey = d2i_ECParameters(NULL, pder, derlen)) == NULL)
353 EVP_PKEY_assign_EC_KEY(pkey, eckey);
357 static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
359 return i2d_ECParameters(pkey->pkey.ec, pder);
362 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
365 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PARAM);
368 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
371 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PUBLIC);
374 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
377 return do_EC_KEY_print(bp, pkey->pkey.ec, indent, EC_KEY_PRINT_PRIVATE);
380 static int old_ec_priv_decode(EVP_PKEY *pkey,
381 const unsigned char **pder, int derlen)
385 if ((ec = d2i_ECPrivateKey(NULL, pder, derlen)) == NULL)
387 EVP_PKEY_assign_EC_KEY(pkey, ec);
391 static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
393 return i2d_ECPrivateKey(pkey->pkey.ec, pder);
396 static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
399 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
400 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_SM2) {
401 /* For SM2, the only valid digest-alg is SM3 */
402 *(int *)arg2 = NID_sm3;
403 return 2; /* Make it mandatory */
405 *(int *)arg2 = NID_sha256;
408 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
409 /* We should only be here if we have a legacy key */
410 if (!ossl_assert(evp_pkey_is_legacy(pkey)))
412 return EC_KEY_oct2key(evp_pkey_get0_EC_KEY_int(pkey), arg2, arg1, NULL);
414 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
415 return EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(pkey),
416 POINT_CONVERSION_UNCOMPRESSED, arg2, NULL);
423 static int ec_pkey_check(const EVP_PKEY *pkey)
425 EC_KEY *eckey = pkey->pkey.ec;
427 /* stay consistent to what EVP_PKEY_check demands */
428 if (eckey->priv_key == NULL) {
429 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
433 return EC_KEY_check_key(eckey);
436 static int ec_pkey_public_check(const EVP_PKEY *pkey)
438 EC_KEY *eckey = pkey->pkey.ec;
441 * Note: it unnecessary to check eckey->pub_key here since
442 * it will be checked in EC_KEY_check_key(). In fact, the
443 * EC_KEY_check_key() mainly checks the public key, and checks
444 * the private key optionally (only if there is one). So if
445 * someone passes a whole EC key (public + private), this
449 return EC_KEY_check_key(eckey);
452 static int ec_pkey_param_check(const EVP_PKEY *pkey)
454 EC_KEY *eckey = pkey->pkey.ec;
456 /* stay consistent to what EVP_PKEY_check demands */
457 if (eckey->group == NULL) {
458 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
462 return EC_GROUP_check(eckey->group, NULL);
466 size_t ec_pkey_dirty_cnt(const EVP_PKEY *pkey)
468 return pkey->pkey.ec->dirty_cnt;
472 int ec_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
473 OSSL_FUNC_keymgmt_import_fn *importer,
474 OSSL_LIB_CTX *libctx, const char *propq)
476 const EC_KEY *eckey = NULL;
477 const EC_GROUP *ecg = NULL;
478 unsigned char *pub_key_buf = NULL, *gen_buf = NULL;
479 size_t pub_key_buflen;
480 OSSL_PARAM_BLD *tmpl;
481 OSSL_PARAM *params = NULL;
482 const BIGNUM *priv_key = NULL;
483 const EC_POINT *pub_point = NULL;
486 BN_CTX *bnctx = NULL;
489 || (eckey = from->pkey.ec) == NULL
490 || (ecg = EC_KEY_get0_group(eckey)) == NULL)
493 tmpl = OSSL_PARAM_BLD_new();
498 * EC_POINT_point2buf() can generate random numbers in some
499 * implementations so we need to ensure we use the correct libctx.
501 bnctx = BN_CTX_new_ex(libctx);
506 /* export the domain parameters */
507 if (!ossl_ec_group_todata(ecg, tmpl, NULL, libctx, propq, bnctx, &gen_buf))
509 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
511 priv_key = EC_KEY_get0_private_key(eckey);
512 pub_point = EC_KEY_get0_public_key(eckey);
514 if (pub_point != NULL) {
515 /* convert pub_point to a octet string according to the SECG standard */
516 if ((pub_key_buflen = EC_POINT_point2buf(ecg, pub_point,
517 POINT_CONVERSION_COMPRESSED,
518 &pub_key_buf, bnctx)) == 0
519 || !OSSL_PARAM_BLD_push_octet_string(tmpl,
520 OSSL_PKEY_PARAM_PUB_KEY,
524 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
527 if (priv_key != NULL) {
530 int ecdh_cofactor_mode;
533 * Key import/export should never leak the bit length of the secret
536 * For this reason, on export we use padded BIGNUMs with fixed length.
538 * When importing we also should make sure that, even if short lived,
539 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
540 * soon as possible, so that any processing of this BIGNUM might opt for
541 * constant time implementations in the backend.
543 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
544 * to preallocate the BIGNUM internal buffer to a fixed public size big
545 * enough that operations performed during the processing never trigger
546 * a realloc which would leak the size of the scalar through memory
552 * The order of the large prime subgroup of the curve is our choice for
553 * a fixed public size, as that is generally the upper bound for
554 * generating a private key in EC cryptosystems and should fit all valid
557 * For padding on export we just use the bit length of the order
558 * converted to bytes (rounding up).
560 * For preallocating the BIGNUM storage we look at the number of "words"
561 * required for the internal representation of the order, and we
562 * preallocate 2 extra "words" in case any of the subsequent processing
563 * might temporarily overflow the order length.
565 ecbits = EC_GROUP_order_bits(ecg);
569 sz = (ecbits + 7) / 8;
570 if (!OSSL_PARAM_BLD_push_BN_pad(tmpl,
571 OSSL_PKEY_PARAM_PRIV_KEY,
574 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
577 * The ECDH Cofactor Mode is defined only if the EC_KEY actually
578 * contains a private key, so we check for the flag and export it only
582 (EC_KEY_get_flags(eckey) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
584 /* Export the ECDH_COFACTOR_MODE parameter */
585 if (!OSSL_PARAM_BLD_push_int(tmpl,
586 OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
589 selection |= OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS;
592 params = OSSL_PARAM_BLD_to_param(tmpl);
594 /* We export, the provider imports */
595 rv = importer(to_keydata, selection, params);
598 OSSL_PARAM_BLD_free(tmpl);
599 OSSL_PARAM_free(params);
600 OPENSSL_free(pub_key_buf);
601 OPENSSL_free(gen_buf);
607 static int ec_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
609 EVP_PKEY_CTX *pctx = vpctx;
610 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
611 EC_KEY *ec = EC_KEY_new_ex(pctx->libctx, pctx->propquery);
614 ERR_raise(ERR_LIB_DH, ERR_R_MALLOC_FAILURE);
618 if (!ossl_ec_group_fromdata(ec, params)
619 || !ossl_ec_key_otherparams_fromdata(ec, params)
620 || !ossl_ec_key_fromdata(ec, params, 1)
621 || !EVP_PKEY_assign_EC_KEY(pkey, ec)) {
628 static int ec_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
630 EC_KEY *eckey = from->pkey.ec;
631 EC_KEY *dupkey = NULL;
635 dupkey = EC_KEY_dup(eckey);
639 /* necessary to properly copy empty SM2 keys */
640 return EVP_PKEY_set_type(to, from->type);
643 ret = EVP_PKEY_assign_EC_KEY(to, dupkey);
649 const EVP_PKEY_ASN1_METHOD ossl_eckey_asn1_meth = {
654 "OpenSSL EC algorithm",
671 ec_missing_parameters,
685 ec_pkey_public_check,
688 0, /* set_priv_key */
690 0, /* get_priv_key */
700 #if !defined(OPENSSL_NO_SM2)
701 const EVP_PKEY_ASN1_METHOD ossl_sm2_asn1_meth = {
708 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
710 int private = EC_KEY_get0_private_key(x) != NULL;
712 return do_EC_KEY_print(bp, x, off,
713 private ? EC_KEY_PRINT_PRIVATE : EC_KEY_PRINT_PUBLIC);
716 int ECParameters_print(BIO *bp, const EC_KEY *x)
718 return do_EC_KEY_print(bp, x, 4, EC_KEY_PRINT_PARAM);