Use build.info, not ifdef for crypto modules
[openssl.git] / crypto / ec / ec_backend.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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"
15
16 /*
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.
20  */
21
22 int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
23 {
24     const EC_GROUP *ecg = EC_KEY_get0_group(ec);
25     const BIGNUM *cofactor;
26     int mode;
27
28     if (!OSSL_PARAM_get_int(p, &mode))
29         return 0;
30
31     /*
32      * mode can be only 0 for disable, or 1 for enable here.
33      *
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".
37      */
38     if (mode < 0 || mode > 1)
39         return 0;
40
41     if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
42         return 0;
43
44     /* ECDH cofactor mode has no effect if cofactor is 1 */
45     if (BN_is_one(cofactor))
46         return 1;
47
48     if (mode == 1)
49         EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
50     else if (mode == 0)
51         EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
52
53     return 1;
54 }
55
56 /*
57  * Callers of ec_key_fromdata MUST make sure that ec_key_params_fromdata has
58  * been called before!
59  *
60  * This function only gets the bare keypair, domain parameters and other
61  * parameters are treated separately, and domain parameters are required to
62  * define a keypair.
63  */
64 int ec_key_fromdata(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
65 {
66     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key = NULL;
67     BN_CTX *ctx = NULL;
68     BIGNUM *priv_key = NULL;
69     unsigned char *pub_key = NULL;
70     size_t pub_key_len;
71     const EC_GROUP *ecg = NULL;
72     EC_POINT *pub_point = NULL;
73     int ok = 0;
74
75     ecg = EC_KEY_get0_group(ec);
76     if (ecg == NULL)
77         return 0;
78
79     param_pub_key =
80         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
81     if (include_private)
82         param_priv_key =
83             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
84
85     ctx = BN_CTX_new_ex(ec_key_get_libctx(ec));
86     if (ctx == NULL)
87         goto err;
88
89     /* OpenSSL decree: If there's a private key, there must be a public key */
90     if (param_priv_key != NULL && param_pub_key == NULL)
91         goto err;
92
93     if (param_pub_key != NULL)
94         if (!OSSL_PARAM_get_octet_string(param_pub_key,
95                                          (void **)&pub_key, 0, &pub_key_len)
96             || (pub_point = EC_POINT_new(ecg)) == NULL
97             || !EC_POINT_oct2point(ecg, pub_point, pub_key, pub_key_len, ctx))
98         goto err;
99
100     if (param_priv_key != NULL && include_private) {
101         int fixed_words;
102         const BIGNUM *order;
103
104         /*
105          * Key import/export should never leak the bit length of the secret
106          * scalar in the key.
107          *
108          * For this reason, on export we use padded BIGNUMs with fixed length.
109          *
110          * When importing we also should make sure that, even if short lived,
111          * the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
112          * soon as possible, so that any processing of this BIGNUM might opt for
113          * constant time implementations in the backend.
114          *
115          * Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
116          * to preallocate the BIGNUM internal buffer to a fixed public size big
117          * enough that operations performed during the processing never trigger
118          * a realloc which would leak the size of the scalar through memory
119          * accesses.
120          *
121          * Fixed Length
122          * ------------
123          *
124          * The order of the large prime subgroup of the curve is our choice for
125          * a fixed public size, as that is generally the upper bound for
126          * generating a private key in EC cryptosystems and should fit all valid
127          * secret scalars.
128          *
129          * For padding on export we just use the bit length of the order
130          * converted to bytes (rounding up).
131          *
132          * For preallocating the BIGNUM storage we look at the number of "words"
133          * required for the internal representation of the order, and we
134          * preallocate 2 extra "words" in case any of the subsequent processing
135          * might temporarily overflow the order length.
136          */
137         order = EC_GROUP_get0_order(ecg);
138         if (order == NULL || BN_is_zero(order))
139             goto err;
140
141         fixed_words = bn_get_top(order) + 2;
142
143         if ((priv_key = BN_secure_new()) == NULL)
144             goto err;
145         if (bn_wexpand(priv_key, fixed_words) == NULL)
146             goto err;
147         BN_set_flags(priv_key, BN_FLG_CONSTTIME);
148
149         if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
150             goto err;
151     }
152
153     if (priv_key != NULL
154         && !EC_KEY_set_private_key(ec, priv_key))
155         goto err;
156
157     if (pub_point != NULL
158         && !EC_KEY_set_public_key(ec, pub_point))
159         goto err;
160
161     ok = 1;
162
163  err:
164     BN_CTX_free(ctx);
165     BN_clear_free(priv_key);
166     OPENSSL_free(pub_key);
167     EC_POINT_free(pub_point);
168     return ok;
169 }
170
171 int ec_key_domparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
172 {
173     const OSSL_PARAM *param_ec_name;
174     EC_GROUP *ecg = NULL;
175     char *curve_name = NULL;
176     int ok = 0;
177
178     if (ec == NULL)
179         return 0;
180
181     param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
182     if (param_ec_name == NULL) {
183         /* explicit parameters */
184
185         /*
186          * TODO(3.0): should we support explicit parameters curves?
187          */
188         return 0;
189     } else {
190         /* named curve */
191         int curve_nid;
192
193         if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
194                 || curve_name == NULL
195                 || (curve_nid = ec_curve_name2nid(curve_name)) == NID_undef)
196             goto err;
197
198         if ((ecg = EC_GROUP_new_by_curve_name_ex(ec_key_get_libctx(ec),
199                                                  curve_nid)) == NULL)
200             goto err;
201     }
202
203     if (!EC_KEY_set_group(ec, ecg))
204         goto err;
205
206     /*
207      * TODO(3.0): if the group has changed, should we invalidate the private and
208      * public key?
209      */
210
211     ok = 1;
212
213  err:
214     OPENSSL_free(curve_name);
215     EC_GROUP_free(ecg);
216     return ok;
217 }
218
219 int ec_key_otherparams_fromdata(EC_KEY *ec, const OSSL_PARAM params[])
220 {
221     const OSSL_PARAM *p;
222
223     if (ec == NULL)
224         return 0;
225
226     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
227     if (p != NULL && !ec_set_param_ecdh_cofactor_mode(ec, p))
228         return 0;
229
230     return 1;
231 }