2 * Copyright 2020 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
10 #include <openssl/core_names.h>
11 #include <openssl/objects.h>
12 #include <openssl/params.h>
13 #include "crypto/bn.h"
14 #include "crypto/ec.h"
17 * The intention with the "backend" source file is to offer backend support
18 * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
19 * implementations alike.
22 int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
24 const EC_GROUP *ecg = EC_KEY_get0_group(ec);
25 const BIGNUM *cofactor;
28 if (!OSSL_PARAM_get_int(p, &mode))
32 * mode can be only 0 for disable, or 1 for enable here.
34 * This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
35 * also supports mode == -1 with the meaning of "reset to the default for
36 * the associated key".
38 if (mode < 0 || mode > 1)
41 if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
44 /* ECDH cofactor mode has no effect if cofactor is 1 */
45 if (BN_is_one(cofactor))
49 EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
51 EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
57 * Callers of ec_key_fromdata MUST make sure that ec_key_params_fromdata has
60 * This function only gets the bare keypair, domain parameters and other
61 * parameters are treated separately, and domain parameters are required to
64 int ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
66 const OSSL_PARAM *param_priv_key, *param_pub_key;
68 BIGNUM *priv_key = NULL;
69 unsigned char *pub_key = NULL;
71 const EC_GROUP *ecg = NULL;
72 EC_POINT *pub_point = NULL;
75 ecg = EC_KEY_get0_group(ec);
80 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
82 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
84 ctx = BN_CTX_new_ex(ec_key_get_libctx(ec));
88 * We want to have at least a public key either way, so we end up
89 * requiring it unconditionally.
91 if (param_pub_key == NULL
92 || !OSSL_PARAM_get_octet_string(param_pub_key,
93 (void **)&pub_key, 0, &pub_key_len)
94 || (pub_point = EC_POINT_new(ecg)) == NULL
95 || !EC_POINT_oct2point(ecg, pub_point,
96 pub_key, pub_key_len, ctx))
99 if (param_priv_key != NULL && include_private) {
104 * Key import/export should never leak the bit length of the secret
107 * For this reason, on export we use padded BIGNUMs with fixed length.
109 * When importing we also should make sure that, even if short lived,
110 * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
111 * soon as possible, so that any processing of this BIGNUM might opt for
112 * constant time implementations in the backend.
114 * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
115 * to preallocate the BIGNUM internal buffer to a fixed public size big
116 * enough that operations performed during the processing never trigger
117 * a realloc which would leak the size of the scalar through memory
123 * The order of the large prime subgroup of the curve is our choice for
124 * a fixed public size, as that is generally the upper bound for
125 * generating a private key in EC cryptosystems and should fit all valid
128 * For padding on export we just use the bit length of the order
129 * converted to bytes (rounding up).
131 * For preallocating the BIGNUM storage we look at the number of "words"
132 * required for the internal representation of the order, and we
133 * preallocate 2 extra "words" in case any of the subsequent processing
134 * might temporarily overflow the order length.
136 order = EC_GROUP_get0_order(ecg);
137 if (order == NULL || BN_is_zero(order))
140 fixed_words = bn_get_top(order) + 2;
142 if ((priv_key = BN_secure_new()) == NULL)
144 if (bn_wexpand(priv_key, fixed_words) == NULL)
146 BN_set_flags(priv_key, BN_FLG_CONSTTIME);
148 if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
153 && !EC_KEY_set_private_key(ec, priv_key))
156 if (!EC_KEY_set_public_key(ec, pub_point))
163 BN_clear_free(priv_key);
164 OPENSSL_free(pub_key);
165 EC_POINT_free(pub_point);
169 int ec_key_domparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
171 const OSSL_PARAM *param_ec_name;
172 EC_GROUP *ecg = NULL;
173 char *curve_name = NULL;
179 param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
180 if (param_ec_name == NULL) {
181 /* explicit parameters */
184 * TODO(3.0): should we support explicit parameters curves?
191 if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
192 || curve_name == NULL
193 || (curve_nid = ec_curve_name2nid(curve_name)) == NID_undef)
196 if ((ecg = EC_GROUP_new_by_curve_name_ex(ec_key_get_libctx(ec),
201 if (!EC_KEY_set_group(ec, ecg))
205 * TODO(3.0): if the group has changed, should we invalidate the private and
212 OPENSSL_free(curve_name);
217 int ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
224 p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
225 if (p != NULL && !ec_set_param_ecdh_cofactor_mode(ec, p))