/src/openssl/crypto/ec/ec_lib.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2001-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved  | 
4  |  |  *  | 
5  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
6  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
7  |  |  * in the file LICENSE in the source distribution or at  | 
8  |  |  * https://www.openssl.org/source/license.html  | 
9  |  |  */  | 
10  |  |  | 
11  |  | /*  | 
12  |  |  * EC_GROUP low level APIs are deprecated for public use, but still ok for  | 
13  |  |  * internal use.  | 
14  |  |  */  | 
15  |  | #include "internal/deprecated.h"  | 
16  |  |  | 
17  |  | #include <string.h>  | 
18  |  | #include <openssl/params.h>  | 
19  |  | #include <openssl/core_names.h>  | 
20  |  | #include <openssl/err.h>  | 
21  |  | #include <openssl/opensslv.h>  | 
22  |  | #include <openssl/param_build.h>  | 
23  |  | #include "crypto/ec.h"  | 
24  |  | #include "crypto/bn.h"  | 
25  |  | #include "internal/nelem.h"  | 
26  |  | #include "ec_local.h"  | 
27  |  |  | 
28  |  | /* functions for EC_GROUP objects */  | 
29  |  |  | 
30  |  | EC_GROUP *ossl_ec_group_new_ex(OSSL_LIB_CTX *libctx, const char *propq,  | 
31  |  |                                const EC_METHOD *meth)  | 
32  | 0  | { | 
33  | 0  |     EC_GROUP *ret;  | 
34  |  | 
  | 
35  | 0  |     if (meth == NULL) { | 
36  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_SLOT_FULL);  | 
37  | 0  |         return NULL;  | 
38  | 0  |     }  | 
39  | 0  |     if (meth->group_init == 0) { | 
40  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
41  | 0  |         return NULL;  | 
42  | 0  |     }  | 
43  |  |  | 
44  | 0  |     ret = OPENSSL_zalloc(sizeof(*ret));  | 
45  | 0  |     if (ret == NULL)  | 
46  | 0  |         return NULL;  | 
47  |  |  | 
48  | 0  |     ret->libctx = libctx;  | 
49  | 0  |     if (propq != NULL) { | 
50  | 0  |         ret->propq = OPENSSL_strdup(propq);  | 
51  | 0  |         if (ret->propq == NULL)  | 
52  | 0  |             goto err;  | 
53  | 0  |     }  | 
54  | 0  |     ret->meth = meth;  | 
55  | 0  |     if ((ret->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { | 
56  | 0  |         ret->order = BN_new();  | 
57  | 0  |         if (ret->order == NULL)  | 
58  | 0  |             goto err;  | 
59  | 0  |         ret->cofactor = BN_new();  | 
60  | 0  |         if (ret->cofactor == NULL)  | 
61  | 0  |             goto err;  | 
62  | 0  |     }  | 
63  | 0  |     ret->asn1_flag = OPENSSL_EC_EXPLICIT_CURVE;  | 
64  | 0  |     ret->asn1_form = POINT_CONVERSION_UNCOMPRESSED;  | 
65  | 0  |     if (!meth->group_init(ret))  | 
66  | 0  |         goto err;  | 
67  | 0  |     return ret;  | 
68  |  |  | 
69  | 0  |  err:  | 
70  | 0  |     BN_free(ret->order);  | 
71  | 0  |     BN_free(ret->cofactor);  | 
72  | 0  |     OPENSSL_free(ret->propq);  | 
73  | 0  |     OPENSSL_free(ret);  | 
74  | 0  |     return NULL;  | 
75  | 0  | }  | 
76  |  |  | 
77  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
78  |  | # ifndef FIPS_MODULE  | 
79  |  | EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)  | 
80  | 0  | { | 
81  | 0  |     return ossl_ec_group_new_ex(NULL, NULL, meth);  | 
82  | 0  | }  | 
83  |  | # endif  | 
84  |  | #endif  | 
85  |  |  | 
86  |  | void EC_pre_comp_free(EC_GROUP *group)  | 
87  | 0  | { | 
88  | 0  |     switch (group->pre_comp_type) { | 
89  | 0  |     case PCT_none:  | 
90  | 0  |         break;  | 
91  | 0  |     case PCT_nistz256:  | 
92  | 0  | #ifdef ECP_NISTZ256_ASM  | 
93  | 0  |         EC_nistz256_pre_comp_free(group->pre_comp.nistz256);  | 
94  | 0  | #endif  | 
95  | 0  |         break;  | 
96  | 0  | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128  | 
97  | 0  |     case PCT_nistp224:  | 
98  | 0  |         EC_nistp224_pre_comp_free(group->pre_comp.nistp224);  | 
99  | 0  |         break;  | 
100  | 0  |     case PCT_nistp256:  | 
101  | 0  |         EC_nistp256_pre_comp_free(group->pre_comp.nistp256);  | 
102  | 0  |         break;  | 
103  | 0  |     case PCT_nistp384:  | 
104  | 0  |         ossl_ec_nistp384_pre_comp_free(group->pre_comp.nistp384);  | 
105  | 0  |         break;  | 
106  | 0  |     case PCT_nistp521:  | 
107  | 0  |         EC_nistp521_pre_comp_free(group->pre_comp.nistp521);  | 
108  | 0  |         break;  | 
109  |  | #else  | 
110  |  |     case PCT_nistp224:  | 
111  |  |     case PCT_nistp256:  | 
112  |  |     case PCT_nistp384:  | 
113  |  |     case PCT_nistp521:  | 
114  |  |         break;  | 
115  |  | #endif  | 
116  | 0  |     case PCT_ec:  | 
117  | 0  |         EC_ec_pre_comp_free(group->pre_comp.ec);  | 
118  | 0  |         break;  | 
119  | 0  |     }  | 
120  | 0  |     group->pre_comp.ec = NULL;  | 
121  | 0  | }  | 
122  |  |  | 
123  |  | void EC_GROUP_free(EC_GROUP *group)  | 
124  | 0  | { | 
125  | 0  |     if (!group)  | 
126  | 0  |         return;  | 
127  |  |  | 
128  | 0  |     if (group->meth->group_finish != 0)  | 
129  | 0  |         group->meth->group_finish(group);  | 
130  |  | 
  | 
131  | 0  |     EC_pre_comp_free(group);  | 
132  | 0  |     BN_MONT_CTX_free(group->mont_data);  | 
133  | 0  |     EC_POINT_free(group->generator);  | 
134  | 0  |     BN_free(group->order);  | 
135  | 0  |     BN_free(group->cofactor);  | 
136  | 0  |     OPENSSL_free(group->seed);  | 
137  | 0  |     OPENSSL_free(group->propq);  | 
138  | 0  |     OPENSSL_free(group);  | 
139  | 0  | }  | 
140  |  |  | 
141  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
142  |  | void EC_GROUP_clear_free(EC_GROUP *group)  | 
143  | 0  | { | 
144  | 0  |     if (!group)  | 
145  | 0  |         return;  | 
146  |  |  | 
147  | 0  |     if (group->meth->group_clear_finish != 0)  | 
148  | 0  |         group->meth->group_clear_finish(group);  | 
149  | 0  |     else if (group->meth->group_finish != 0)  | 
150  | 0  |         group->meth->group_finish(group);  | 
151  |  | 
  | 
152  | 0  |     EC_pre_comp_free(group);  | 
153  | 0  |     BN_MONT_CTX_free(group->mont_data);  | 
154  | 0  |     EC_POINT_clear_free(group->generator);  | 
155  | 0  |     BN_clear_free(group->order);  | 
156  | 0  |     BN_clear_free(group->cofactor);  | 
157  | 0  |     OPENSSL_clear_free(group->seed, group->seed_len);  | 
158  | 0  |     OPENSSL_clear_free(group, sizeof(*group));  | 
159  | 0  | }  | 
160  |  | #endif  | 
161  |  |  | 
162  |  | int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)  | 
163  | 0  | { | 
164  | 0  |     if (dest->meth->group_copy == 0) { | 
165  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
166  | 0  |         return 0;  | 
167  | 0  |     }  | 
168  | 0  |     if (dest->meth != src->meth) { | 
169  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
170  | 0  |         return 0;  | 
171  | 0  |     }  | 
172  | 0  |     if (dest == src)  | 
173  | 0  |         return 1;  | 
174  |  |  | 
175  | 0  |     dest->libctx = src->libctx;  | 
176  | 0  |     dest->curve_name = src->curve_name;  | 
177  |  |  | 
178  |  |     /* Copy precomputed */  | 
179  | 0  |     dest->pre_comp_type = src->pre_comp_type;  | 
180  | 0  |     switch (src->pre_comp_type) { | 
181  | 0  |     case PCT_none:  | 
182  | 0  |         dest->pre_comp.ec = NULL;  | 
183  | 0  |         break;  | 
184  | 0  |     case PCT_nistz256:  | 
185  | 0  | #ifdef ECP_NISTZ256_ASM  | 
186  | 0  |         dest->pre_comp.nistz256 = EC_nistz256_pre_comp_dup(src->pre_comp.nistz256);  | 
187  | 0  | #endif  | 
188  | 0  |         break;  | 
189  | 0  | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128  | 
190  | 0  |     case PCT_nistp224:  | 
191  | 0  |         dest->pre_comp.nistp224 = EC_nistp224_pre_comp_dup(src->pre_comp.nistp224);  | 
192  | 0  |         break;  | 
193  | 0  |     case PCT_nistp256:  | 
194  | 0  |         dest->pre_comp.nistp256 = EC_nistp256_pre_comp_dup(src->pre_comp.nistp256);  | 
195  | 0  |         break;  | 
196  | 0  |     case PCT_nistp384:  | 
197  | 0  |         dest->pre_comp.nistp384 = ossl_ec_nistp384_pre_comp_dup(src->pre_comp.nistp384);  | 
198  | 0  |         break;  | 
199  | 0  |     case PCT_nistp521:  | 
200  | 0  |         dest->pre_comp.nistp521 = EC_nistp521_pre_comp_dup(src->pre_comp.nistp521);  | 
201  | 0  |         break;  | 
202  |  | #else  | 
203  |  |     case PCT_nistp224:  | 
204  |  |     case PCT_nistp256:  | 
205  |  |     case PCT_nistp384:  | 
206  |  |     case PCT_nistp521:  | 
207  |  |         break;  | 
208  |  | #endif  | 
209  | 0  |     case PCT_ec:  | 
210  | 0  |         dest->pre_comp.ec = EC_ec_pre_comp_dup(src->pre_comp.ec);  | 
211  | 0  |         break;  | 
212  | 0  |     }  | 
213  |  |  | 
214  | 0  |     if (src->mont_data != NULL) { | 
215  | 0  |         if (dest->mont_data == NULL) { | 
216  | 0  |             dest->mont_data = BN_MONT_CTX_new();  | 
217  | 0  |             if (dest->mont_data == NULL)  | 
218  | 0  |                 return 0;  | 
219  | 0  |         }  | 
220  | 0  |         if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data))  | 
221  | 0  |             return 0;  | 
222  | 0  |     } else { | 
223  |  |         /* src->generator == NULL */  | 
224  | 0  |         BN_MONT_CTX_free(dest->mont_data);  | 
225  | 0  |         dest->mont_data = NULL;  | 
226  | 0  |     }  | 
227  |  |  | 
228  | 0  |     if (src->generator != NULL) { | 
229  | 0  |         if (dest->generator == NULL) { | 
230  | 0  |             dest->generator = EC_POINT_new(dest);  | 
231  | 0  |             if (dest->generator == NULL)  | 
232  | 0  |                 return 0;  | 
233  | 0  |         }  | 
234  | 0  |         if (!EC_POINT_copy(dest->generator, src->generator))  | 
235  | 0  |             return 0;  | 
236  | 0  |     } else { | 
237  |  |         /* src->generator == NULL */  | 
238  | 0  |         EC_POINT_clear_free(dest->generator);  | 
239  | 0  |         dest->generator = NULL;  | 
240  | 0  |     }  | 
241  |  |  | 
242  | 0  |     if ((src->meth->flags & EC_FLAGS_CUSTOM_CURVE) == 0) { | 
243  | 0  |         if (!BN_copy(dest->order, src->order))  | 
244  | 0  |             return 0;  | 
245  | 0  |         if (!BN_copy(dest->cofactor, src->cofactor))  | 
246  | 0  |             return 0;  | 
247  | 0  |     }  | 
248  |  |  | 
249  | 0  |     dest->asn1_flag = src->asn1_flag;  | 
250  | 0  |     dest->asn1_form = src->asn1_form;  | 
251  | 0  |     dest->decoded_from_explicit_params = src->decoded_from_explicit_params;  | 
252  |  | 
  | 
253  | 0  |     if (src->seed) { | 
254  | 0  |         OPENSSL_free(dest->seed);  | 
255  | 0  |         if ((dest->seed = OPENSSL_malloc(src->seed_len)) == NULL)  | 
256  | 0  |             return 0;  | 
257  | 0  |         if (!memcpy(dest->seed, src->seed, src->seed_len))  | 
258  | 0  |             return 0;  | 
259  | 0  |         dest->seed_len = src->seed_len;  | 
260  | 0  |     } else { | 
261  | 0  |         OPENSSL_free(dest->seed);  | 
262  | 0  |         dest->seed = NULL;  | 
263  | 0  |         dest->seed_len = 0;  | 
264  | 0  |     }  | 
265  |  |  | 
266  | 0  |     return dest->meth->group_copy(dest, src);  | 
267  | 0  | }  | 
268  |  |  | 
269  |  | EC_GROUP *EC_GROUP_dup(const EC_GROUP *a)  | 
270  | 0  | { | 
271  | 0  |     EC_GROUP *t = NULL;  | 
272  | 0  |     int ok = 0;  | 
273  |  | 
  | 
274  | 0  |     if (a == NULL)  | 
275  | 0  |         return NULL;  | 
276  |  |  | 
277  | 0  |     if ((t = ossl_ec_group_new_ex(a->libctx, a->propq, a->meth)) == NULL)  | 
278  | 0  |         return NULL;  | 
279  | 0  |     if (!EC_GROUP_copy(t, a))  | 
280  | 0  |         goto err;  | 
281  |  |  | 
282  | 0  |     ok = 1;  | 
283  |  | 
  | 
284  | 0  |  err:  | 
285  | 0  |     if (!ok) { | 
286  | 0  |         EC_GROUP_free(t);  | 
287  | 0  |         return NULL;  | 
288  | 0  |     }  | 
289  | 0  |         return t;  | 
290  | 0  | }  | 
291  |  |  | 
292  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
293  |  | const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group)  | 
294  | 0  | { | 
295  | 0  |     return group->meth;  | 
296  | 0  | }  | 
297  |  |  | 
298  |  | int EC_METHOD_get_field_type(const EC_METHOD *meth)  | 
299  | 0  | { | 
300  | 0  |     return meth->field_type;  | 
301  | 0  | }  | 
302  |  | #endif  | 
303  |  |  | 
304  |  | static int ec_precompute_mont_data(EC_GROUP *);  | 
305  |  |  | 
306  |  | /*-  | 
307  |  |  * Try computing cofactor from the generator order (n) and field cardinality (q).  | 
308  |  |  * This works for all curves of cryptographic interest.  | 
309  |  |  *  | 
310  |  |  * Hasse thm: q + 1 - 2*sqrt(q) <= n*h <= q + 1 + 2*sqrt(q)  | 
311  |  |  * h_min = (q + 1 - 2*sqrt(q))/n  | 
312  |  |  * h_max = (q + 1 + 2*sqrt(q))/n  | 
313  |  |  * h_max - h_min = 4*sqrt(q)/n  | 
314  |  |  * So if n > 4*sqrt(q) holds, there is only one possible value for h:  | 
315  |  |  * h = \lfloor (h_min + h_max)/2 \rceil = \lfloor (q + 1)/n \rceil  | 
316  |  |  *  | 
317  |  |  * Otherwise, zero cofactor and return success.  | 
318  |  |  */  | 
319  | 0  | static int ec_guess_cofactor(EC_GROUP *group) { | 
320  | 0  |     int ret = 0;  | 
321  | 0  |     BN_CTX *ctx = NULL;  | 
322  | 0  |     BIGNUM *q = NULL;  | 
323  |  |  | 
324  |  |     /*-  | 
325  |  |      * If the cofactor is too large, we cannot guess it.  | 
326  |  |      * The RHS of below is a strict overestimate of lg(4 * sqrt(q))  | 
327  |  |      */  | 
328  | 0  |     if (BN_num_bits(group->order) <= (BN_num_bits(group->field) + 1) / 2 + 3) { | 
329  |  |         /* default to 0 */  | 
330  | 0  |         BN_zero(group->cofactor);  | 
331  |  |         /* return success */  | 
332  | 0  |         return 1;  | 
333  | 0  |     }  | 
334  |  |  | 
335  | 0  |     if ((ctx = BN_CTX_new_ex(group->libctx)) == NULL)  | 
336  | 0  |         return 0;  | 
337  |  |  | 
338  | 0  |     BN_CTX_start(ctx);  | 
339  | 0  |     if ((q = BN_CTX_get(ctx)) == NULL)  | 
340  | 0  |         goto err;  | 
341  |  |  | 
342  |  |     /* set q = 2**m for binary fields; q = p otherwise */  | 
343  | 0  |     if (group->meth->field_type == NID_X9_62_characteristic_two_field) { | 
344  | 0  |         BN_zero(q);  | 
345  | 0  |         if (!BN_set_bit(q, BN_num_bits(group->field) - 1))  | 
346  | 0  |             goto err;  | 
347  | 0  |     } else { | 
348  | 0  |         if (!BN_copy(q, group->field))  | 
349  | 0  |             goto err;  | 
350  | 0  |     }  | 
351  |  |  | 
352  |  |     /* compute h = \lfloor (q + 1)/n \rceil = \lfloor (q + 1 + n/2)/n \rfloor */  | 
353  | 0  |     if (!BN_rshift1(group->cofactor, group->order) /* n/2 */  | 
354  | 0  |         || !BN_add(group->cofactor, group->cofactor, q) /* q + n/2 */  | 
355  |  |         /* q + 1 + n/2 */  | 
356  | 0  |         || !BN_add(group->cofactor, group->cofactor, BN_value_one())  | 
357  |  |         /* (q + 1 + n/2)/n */  | 
358  | 0  |         || !BN_div(group->cofactor, NULL, group->cofactor, group->order, ctx))  | 
359  | 0  |         goto err;  | 
360  | 0  |     ret = 1;  | 
361  | 0  |  err:  | 
362  | 0  |     BN_CTX_end(ctx);  | 
363  | 0  |     BN_CTX_free(ctx);  | 
364  | 0  |     return ret;  | 
365  | 0  | }  | 
366  |  |  | 
367  |  | int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,  | 
368  |  |                            const BIGNUM *order, const BIGNUM *cofactor)  | 
369  | 0  | { | 
370  | 0  |     if (generator == NULL) { | 
371  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);  | 
372  | 0  |         return 0;  | 
373  | 0  |     }  | 
374  |  |  | 
375  |  |     /* require group->field >= 1 */  | 
376  | 0  |     if (group->field == NULL || BN_is_zero(group->field)  | 
377  | 0  |         || BN_is_negative(group->field)) { | 
378  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);  | 
379  | 0  |         return 0;  | 
380  | 0  |     }  | 
381  |  |  | 
382  |  |     /*-  | 
383  |  |      * - require order >= 1  | 
384  |  |      * - enforce upper bound due to Hasse thm: order can be no more than one bit  | 
385  |  |      *   longer than field cardinality  | 
386  |  |      */  | 
387  | 0  |     if (order == NULL || BN_is_zero(order) || BN_is_negative(order)  | 
388  | 0  |         || BN_num_bits(order) > BN_num_bits(group->field) + 1) { | 
389  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);  | 
390  | 0  |         return 0;  | 
391  | 0  |     }  | 
392  |  |  | 
393  |  |     /*-  | 
394  |  |      * Unfortunately the cofactor is an optional field in many standards.  | 
395  |  |      * Internally, the lib uses 0 cofactor as a marker for "unknown cofactor".  | 
396  |  |      * So accept cofactor == NULL or cofactor >= 0.  | 
397  |  |      */  | 
398  | 0  |     if (cofactor != NULL && BN_is_negative(cofactor)) { | 
399  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_UNKNOWN_COFACTOR);  | 
400  | 0  |         return 0;  | 
401  | 0  |     }  | 
402  |  |  | 
403  | 0  |     if (group->generator == NULL) { | 
404  | 0  |         group->generator = EC_POINT_new(group);  | 
405  | 0  |         if (group->generator == NULL)  | 
406  | 0  |             return 0;  | 
407  | 0  |     }  | 
408  | 0  |     if (!EC_POINT_copy(group->generator, generator))  | 
409  | 0  |         return 0;  | 
410  |  |  | 
411  | 0  |     if (!BN_copy(group->order, order))  | 
412  | 0  |         return 0;  | 
413  |  |  | 
414  |  |     /* Either take the provided positive cofactor, or try to compute it */  | 
415  | 0  |     if (cofactor != NULL && !BN_is_zero(cofactor)) { | 
416  | 0  |         if (!BN_copy(group->cofactor, cofactor))  | 
417  | 0  |             return 0;  | 
418  | 0  |     } else if (!ec_guess_cofactor(group)) { | 
419  | 0  |         BN_zero(group->cofactor);  | 
420  | 0  |         return 0;  | 
421  | 0  |     }  | 
422  |  |  | 
423  |  |     /*  | 
424  |  |      * Some groups have an order with  | 
425  |  |      * factors of two, which makes the Montgomery setup fail.  | 
426  |  |      * |group->mont_data| will be NULL in this case.  | 
427  |  |      */  | 
428  | 0  |     if (BN_is_odd(group->order)) { | 
429  | 0  |         return ec_precompute_mont_data(group);  | 
430  | 0  |     }  | 
431  |  |  | 
432  | 0  |     BN_MONT_CTX_free(group->mont_data);  | 
433  | 0  |     group->mont_data = NULL;  | 
434  | 0  |     return 1;  | 
435  | 0  | }  | 
436  |  |  | 
437  |  | const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)  | 
438  | 0  | { | 
439  | 0  |     return group->generator;  | 
440  | 0  | }  | 
441  |  |  | 
442  |  | BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)  | 
443  | 0  | { | 
444  | 0  |     return group->mont_data;  | 
445  | 0  | }  | 
446  |  |  | 
447  |  | int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)  | 
448  | 0  | { | 
449  | 0  |     if (group->order == NULL)  | 
450  | 0  |         return 0;  | 
451  | 0  |     if (!BN_copy(order, group->order))  | 
452  | 0  |         return 0;  | 
453  |  |  | 
454  | 0  |     return !BN_is_zero(order);  | 
455  | 0  | }  | 
456  |  |  | 
457  |  | const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group)  | 
458  | 0  | { | 
459  | 0  |     return group->order;  | 
460  | 0  | }  | 
461  |  |  | 
462  |  | int EC_GROUP_order_bits(const EC_GROUP *group)  | 
463  | 0  | { | 
464  | 0  |     return group->meth->group_order_bits(group);  | 
465  | 0  | }  | 
466  |  |  | 
467  |  | int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,  | 
468  |  |                           BN_CTX *ctx)  | 
469  | 0  | { | 
470  |  | 
  | 
471  | 0  |     if (group->cofactor == NULL)  | 
472  | 0  |         return 0;  | 
473  | 0  |     if (!BN_copy(cofactor, group->cofactor))  | 
474  | 0  |         return 0;  | 
475  |  |  | 
476  | 0  |     return !BN_is_zero(group->cofactor);  | 
477  | 0  | }  | 
478  |  |  | 
479  |  | const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group)  | 
480  | 0  | { | 
481  | 0  |     return group->cofactor;  | 
482  | 0  | }  | 
483  |  |  | 
484  |  | void EC_GROUP_set_curve_name(EC_GROUP *group, int nid)  | 
485  | 0  | { | 
486  | 0  |     group->curve_name = nid;  | 
487  | 0  |     group->asn1_flag =  | 
488  | 0  |         (nid != NID_undef)  | 
489  | 0  |         ? OPENSSL_EC_NAMED_CURVE  | 
490  | 0  |         : OPENSSL_EC_EXPLICIT_CURVE;  | 
491  | 0  | }  | 
492  |  |  | 
493  |  | int EC_GROUP_get_curve_name(const EC_GROUP *group)  | 
494  | 0  | { | 
495  | 0  |     return group->curve_name;  | 
496  | 0  | }  | 
497  |  |  | 
498  |  | const BIGNUM *EC_GROUP_get0_field(const EC_GROUP *group)  | 
499  | 0  | { | 
500  | 0  |     return group->field;  | 
501  | 0  | }  | 
502  |  |  | 
503  |  | int EC_GROUP_get_field_type(const EC_GROUP *group)  | 
504  | 0  | { | 
505  | 0  |     return group->meth->field_type;  | 
506  | 0  | }  | 
507  |  |  | 
508  |  | void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag)  | 
509  | 0  | { | 
510  | 0  |     group->asn1_flag = flag;  | 
511  | 0  | }  | 
512  |  |  | 
513  |  | int EC_GROUP_get_asn1_flag(const EC_GROUP *group)  | 
514  | 0  | { | 
515  | 0  |     return group->asn1_flag;  | 
516  | 0  | }  | 
517  |  |  | 
518  |  | void EC_GROUP_set_point_conversion_form(EC_GROUP *group,  | 
519  |  |                                         point_conversion_form_t form)  | 
520  | 0  | { | 
521  | 0  |     group->asn1_form = form;  | 
522  | 0  | }  | 
523  |  |  | 
524  |  | point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP  | 
525  |  |                                                            *group)  | 
526  | 0  | { | 
527  | 0  |     return group->asn1_form;  | 
528  | 0  | }  | 
529  |  |  | 
530  |  | size_t EC_GROUP_set_seed(EC_GROUP *group, const unsigned char *p, size_t len)  | 
531  | 0  | { | 
532  | 0  |     OPENSSL_free(group->seed);  | 
533  | 0  |     group->seed = NULL;  | 
534  | 0  |     group->seed_len = 0;  | 
535  |  | 
  | 
536  | 0  |     if (!len || !p)  | 
537  | 0  |         return 1;  | 
538  |  |  | 
539  | 0  |     if ((group->seed = OPENSSL_malloc(len)) == NULL)  | 
540  | 0  |         return 0;  | 
541  | 0  |     memcpy(group->seed, p, len);  | 
542  | 0  |     group->seed_len = len;  | 
543  |  | 
  | 
544  | 0  |     return len;  | 
545  | 0  | }  | 
546  |  |  | 
547  |  | unsigned char *EC_GROUP_get0_seed(const EC_GROUP *group)  | 
548  | 0  | { | 
549  | 0  |     return group->seed;  | 
550  | 0  | }  | 
551  |  |  | 
552  |  | size_t EC_GROUP_get_seed_len(const EC_GROUP *group)  | 
553  | 0  | { | 
554  | 0  |     return group->seed_len;  | 
555  | 0  | }  | 
556  |  |  | 
557  |  | int EC_GROUP_set_curve(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,  | 
558  |  |                        const BIGNUM *b, BN_CTX *ctx)  | 
559  | 0  | { | 
560  | 0  |     if (group->meth->group_set_curve == 0) { | 
561  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
562  | 0  |         return 0;  | 
563  | 0  |     }  | 
564  | 0  |     return group->meth->group_set_curve(group, p, a, b, ctx);  | 
565  | 0  | }  | 
566  |  |  | 
567  |  | int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b,  | 
568  |  |                        BN_CTX *ctx)  | 
569  | 0  | { | 
570  | 0  |     if (group->meth->group_get_curve == NULL) { | 
571  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
572  | 0  |         return 0;  | 
573  | 0  |     }  | 
574  | 0  |     return group->meth->group_get_curve(group, p, a, b, ctx);  | 
575  | 0  | }  | 
576  |  |  | 
577  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
578  |  | int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,  | 
579  |  |                            const BIGNUM *b, BN_CTX *ctx)  | 
580  | 0  | { | 
581  | 0  |     return EC_GROUP_set_curve(group, p, a, b, ctx);  | 
582  | 0  | }  | 
583  |  |  | 
584  |  | int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,  | 
585  |  |                            BIGNUM *b, BN_CTX *ctx)  | 
586  | 0  | { | 
587  | 0  |     return EC_GROUP_get_curve(group, p, a, b, ctx);  | 
588  | 0  | }  | 
589  |  |  | 
590  |  | # ifndef OPENSSL_NO_EC2M  | 
591  |  | int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,  | 
592  |  |                             const BIGNUM *b, BN_CTX *ctx)  | 
593  | 0  | { | 
594  | 0  |     return EC_GROUP_set_curve(group, p, a, b, ctx);  | 
595  | 0  | }  | 
596  |  |  | 
597  |  | int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,  | 
598  |  |                             BIGNUM *b, BN_CTX *ctx)  | 
599  | 0  | { | 
600  | 0  |     return EC_GROUP_get_curve(group, p, a, b, ctx);  | 
601  | 0  | }  | 
602  |  | # endif  | 
603  |  | #endif  | 
604  |  |  | 
605  |  | int EC_GROUP_get_degree(const EC_GROUP *group)  | 
606  | 0  | { | 
607  | 0  |     if (group->meth->group_get_degree == 0) { | 
608  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
609  | 0  |         return 0;  | 
610  | 0  |     }  | 
611  | 0  |     return group->meth->group_get_degree(group);  | 
612  | 0  | }  | 
613  |  |  | 
614  |  | int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)  | 
615  | 0  | { | 
616  | 0  |     if (group->meth->group_check_discriminant == 0) { | 
617  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
618  | 0  |         return 0;  | 
619  | 0  |     }  | 
620  | 0  |     return group->meth->group_check_discriminant(group, ctx);  | 
621  | 0  | }  | 
622  |  |  | 
623  |  | int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx)  | 
624  | 0  | { | 
625  | 0  |     int r = 0;  | 
626  | 0  |     BIGNUM *a1, *a2, *a3, *b1, *b2, *b3;  | 
627  | 0  | #ifndef FIPS_MODULE  | 
628  | 0  |     BN_CTX *ctx_new = NULL;  | 
629  | 0  | #endif  | 
630  |  |  | 
631  |  |     /* compare the field types */  | 
632  | 0  |     if (EC_GROUP_get_field_type(a) != EC_GROUP_get_field_type(b))  | 
633  | 0  |         return 1;  | 
634  |  |     /* compare the curve name (if present in both) */  | 
635  | 0  |     if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) &&  | 
636  | 0  |         EC_GROUP_get_curve_name(a) != EC_GROUP_get_curve_name(b))  | 
637  | 0  |         return 1;  | 
638  | 0  |     if (a->meth->flags & EC_FLAGS_CUSTOM_CURVE)  | 
639  | 0  |         return 0;  | 
640  |  |  | 
641  | 0  | #ifndef FIPS_MODULE  | 
642  | 0  |     if (ctx == NULL)  | 
643  | 0  |         ctx_new = ctx = BN_CTX_new();  | 
644  | 0  | #endif  | 
645  | 0  |     if (ctx == NULL)  | 
646  | 0  |         return -1;  | 
647  |  |  | 
648  | 0  |     BN_CTX_start(ctx);  | 
649  | 0  |     a1 = BN_CTX_get(ctx);  | 
650  | 0  |     a2 = BN_CTX_get(ctx);  | 
651  | 0  |     a3 = BN_CTX_get(ctx);  | 
652  | 0  |     b1 = BN_CTX_get(ctx);  | 
653  | 0  |     b2 = BN_CTX_get(ctx);  | 
654  | 0  |     b3 = BN_CTX_get(ctx);  | 
655  | 0  |     if (b3 == NULL) { | 
656  | 0  |         BN_CTX_end(ctx);  | 
657  | 0  | #ifndef FIPS_MODULE  | 
658  | 0  |         BN_CTX_free(ctx_new);  | 
659  | 0  | #endif  | 
660  | 0  |         return -1;  | 
661  | 0  |     }  | 
662  |  |  | 
663  |  |     /*  | 
664  |  |      * XXX This approach assumes that the external representation of curves  | 
665  |  |      * over the same field type is the same.  | 
666  |  |      */  | 
667  | 0  |     if (!a->meth->group_get_curve(a, a1, a2, a3, ctx) ||  | 
668  | 0  |         !b->meth->group_get_curve(b, b1, b2, b3, ctx))  | 
669  | 0  |         r = 1;  | 
670  |  |  | 
671  |  |     /* return 1 if the curve parameters are different */  | 
672  | 0  |     if (r || BN_cmp(a1, b1) != 0 || BN_cmp(a2, b2) != 0 || BN_cmp(a3, b3) != 0)  | 
673  | 0  |         r = 1;  | 
674  |  |  | 
675  |  |     /* XXX EC_POINT_cmp() assumes that the methods are equal */  | 
676  |  |     /* return 1 if the generators are different */  | 
677  | 0  |     if (r || EC_POINT_cmp(a, EC_GROUP_get0_generator(a),  | 
678  | 0  |                           EC_GROUP_get0_generator(b), ctx) != 0)  | 
679  | 0  |         r = 1;  | 
680  |  | 
  | 
681  | 0  |     if (!r) { | 
682  | 0  |         const BIGNUM *ao, *bo, *ac, *bc;  | 
683  |  |         /* compare the orders */  | 
684  | 0  |         ao = EC_GROUP_get0_order(a);  | 
685  | 0  |         bo = EC_GROUP_get0_order(b);  | 
686  | 0  |         if (ao == NULL || bo == NULL) { | 
687  |  |             /* return an error if either order is NULL */  | 
688  | 0  |             r = -1;  | 
689  | 0  |             goto end;  | 
690  | 0  |         }  | 
691  | 0  |         if (BN_cmp(ao, bo) != 0) { | 
692  |  |             /* return 1 if orders are different */  | 
693  | 0  |             r = 1;  | 
694  | 0  |             goto end;  | 
695  | 0  |         }  | 
696  |  |         /*  | 
697  |  |          * It gets here if the curve parameters and generator matched.  | 
698  |  |          * Now check the optional cofactors (if both are present).  | 
699  |  |          */  | 
700  | 0  |         ac = EC_GROUP_get0_cofactor(a);  | 
701  | 0  |         bc = EC_GROUP_get0_cofactor(b);  | 
702  |  |         /* Returns 1 (mismatch) if both cofactors are specified and different */  | 
703  | 0  |         if (!BN_is_zero(ac) && !BN_is_zero(bc) && BN_cmp(ac, bc) != 0)  | 
704  | 0  |             r = 1;  | 
705  |  |         /* Returns 0 if the parameters matched */  | 
706  | 0  |     }  | 
707  | 0  | end:  | 
708  | 0  |     BN_CTX_end(ctx);  | 
709  | 0  | #ifndef FIPS_MODULE  | 
710  | 0  |     BN_CTX_free(ctx_new);  | 
711  | 0  | #endif  | 
712  | 0  |     return r;  | 
713  | 0  | }  | 
714  |  |  | 
715  |  | /* functions for EC_POINT objects */  | 
716  |  |  | 
717  |  | EC_POINT *EC_POINT_new(const EC_GROUP *group)  | 
718  | 0  | { | 
719  | 0  |     EC_POINT *ret;  | 
720  |  | 
  | 
721  | 0  |     if (group == NULL) { | 
722  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);  | 
723  | 0  |         return NULL;  | 
724  | 0  |     }  | 
725  | 0  |     if (group->meth->point_init == NULL) { | 
726  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
727  | 0  |         return NULL;  | 
728  | 0  |     }  | 
729  |  |  | 
730  | 0  |     ret = OPENSSL_zalloc(sizeof(*ret));  | 
731  | 0  |     if (ret == NULL)  | 
732  | 0  |         return NULL;  | 
733  |  |  | 
734  | 0  |     ret->meth = group->meth;  | 
735  | 0  |     ret->curve_name = group->curve_name;  | 
736  |  | 
  | 
737  | 0  |     if (!ret->meth->point_init(ret)) { | 
738  | 0  |         OPENSSL_free(ret);  | 
739  | 0  |         return NULL;  | 
740  | 0  |     }  | 
741  |  |  | 
742  | 0  |     return ret;  | 
743  | 0  | }  | 
744  |  |  | 
745  |  | void EC_POINT_free(EC_POINT *point)  | 
746  | 0  | { | 
747  | 0  |     if (point == NULL)  | 
748  | 0  |         return;  | 
749  |  |  | 
750  |  | #ifdef OPENSSL_PEDANTIC_ZEROIZATION  | 
751  |  |     EC_POINT_clear_free(point);  | 
752  |  | #else  | 
753  | 0  |     if (point->meth->point_finish != 0)  | 
754  | 0  |         point->meth->point_finish(point);  | 
755  | 0  |     OPENSSL_free(point);  | 
756  | 0  | #endif  | 
757  | 0  | }  | 
758  |  |  | 
759  |  | void EC_POINT_clear_free(EC_POINT *point)  | 
760  | 0  | { | 
761  | 0  |     if (point == NULL)  | 
762  | 0  |         return;  | 
763  |  |  | 
764  | 0  |     if (point->meth->point_clear_finish != 0)  | 
765  | 0  |         point->meth->point_clear_finish(point);  | 
766  | 0  |     else if (point->meth->point_finish != 0)  | 
767  | 0  |         point->meth->point_finish(point);  | 
768  | 0  |     OPENSSL_clear_free(point, sizeof(*point));  | 
769  | 0  | }  | 
770  |  |  | 
771  |  | int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src)  | 
772  | 0  | { | 
773  | 0  |     if (dest->meth->point_copy == 0) { | 
774  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
775  | 0  |         return 0;  | 
776  | 0  |     }  | 
777  | 0  |     if (dest->meth != src->meth  | 
778  | 0  |             || (dest->curve_name != src->curve_name  | 
779  | 0  |                  && dest->curve_name != 0  | 
780  | 0  |                  && src->curve_name != 0)) { | 
781  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
782  | 0  |         return 0;  | 
783  | 0  |     }  | 
784  | 0  |     if (dest == src)  | 
785  | 0  |         return 1;  | 
786  | 0  |     return dest->meth->point_copy(dest, src);  | 
787  | 0  | }  | 
788  |  |  | 
789  |  | EC_POINT *EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group)  | 
790  | 0  | { | 
791  | 0  |     EC_POINT *t;  | 
792  | 0  |     int r;  | 
793  |  | 
  | 
794  | 0  |     if (a == NULL)  | 
795  | 0  |         return NULL;  | 
796  |  |  | 
797  | 0  |     t = EC_POINT_new(group);  | 
798  | 0  |     if (t == NULL)  | 
799  | 0  |         return NULL;  | 
800  | 0  |     r = EC_POINT_copy(t, a);  | 
801  | 0  |     if (!r) { | 
802  | 0  |         EC_POINT_free(t);  | 
803  | 0  |         return NULL;  | 
804  | 0  |     }  | 
805  | 0  |     return t;  | 
806  | 0  | }  | 
807  |  |  | 
808  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
809  |  | const EC_METHOD *EC_POINT_method_of(const EC_POINT *point)  | 
810  | 0  | { | 
811  | 0  |     return point->meth;  | 
812  | 0  | }  | 
813  |  | #endif  | 
814  |  |  | 
815  |  | int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point)  | 
816  | 0  | { | 
817  | 0  |     if (group->meth->point_set_to_infinity == 0) { | 
818  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
819  | 0  |         return 0;  | 
820  | 0  |     }  | 
821  | 0  |     if (group->meth != point->meth) { | 
822  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
823  | 0  |         return 0;  | 
824  | 0  |     }  | 
825  | 0  |     return group->meth->point_set_to_infinity(group, point);  | 
826  | 0  | }  | 
827  |  |  | 
828  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
829  |  | int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,  | 
830  |  |                                              EC_POINT *point, const BIGNUM *x,  | 
831  |  |                                              const BIGNUM *y, const BIGNUM *z,  | 
832  |  |                                              BN_CTX *ctx)  | 
833  | 0  | { | 
834  | 0  |     if (group->meth->field_type != NID_X9_62_prime_field) { | 
835  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
836  | 0  |         return 0;  | 
837  | 0  |     }  | 
838  | 0  |     if (!ec_point_is_compat(point, group)) { | 
839  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
840  | 0  |         return 0;  | 
841  | 0  |     }  | 
842  | 0  |     return ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(group, point,  | 
843  | 0  |                                                               x, y, z, ctx);  | 
844  | 0  | }  | 
845  |  |  | 
846  |  | int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,  | 
847  |  |                                              const EC_POINT *point, BIGNUM *x,  | 
848  |  |                                              BIGNUM *y, BIGNUM *z,  | 
849  |  |                                              BN_CTX *ctx)  | 
850  | 0  | { | 
851  | 0  |     if (group->meth->field_type != NID_X9_62_prime_field) { | 
852  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
853  | 0  |         return 0;  | 
854  | 0  |     }  | 
855  | 0  |     if (!ec_point_is_compat(point, group)) { | 
856  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
857  | 0  |         return 0;  | 
858  | 0  |     }  | 
859  | 0  |     return ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(group, point,  | 
860  | 0  |                                                               x, y, z, ctx);  | 
861  | 0  | }  | 
862  |  | #endif  | 
863  |  |  | 
864  |  | int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,  | 
865  |  |                                     const BIGNUM *x, const BIGNUM *y,  | 
866  |  |                                     BN_CTX *ctx)  | 
867  | 0  | { | 
868  | 0  |     if (group->meth->point_set_affine_coordinates == NULL) { | 
869  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
870  | 0  |         return 0;  | 
871  | 0  |     }  | 
872  | 0  |     if (!ec_point_is_compat(point, group)) { | 
873  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
874  | 0  |         return 0;  | 
875  | 0  |     }  | 
876  | 0  |     if (!group->meth->point_set_affine_coordinates(group, point, x, y, ctx))  | 
877  | 0  |         return 0;  | 
878  |  |  | 
879  | 0  |     if (EC_POINT_is_on_curve(group, point, ctx) <= 0) { | 
880  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_POINT_IS_NOT_ON_CURVE);  | 
881  | 0  |         return 0;  | 
882  | 0  |     }  | 
883  | 0  |     return 1;  | 
884  | 0  | }  | 
885  |  |  | 
886  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
887  |  | int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,  | 
888  |  |                                         EC_POINT *point, const BIGNUM *x,  | 
889  |  |                                         const BIGNUM *y, BN_CTX *ctx)  | 
890  | 0  | { | 
891  | 0  |     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);  | 
892  | 0  | }  | 
893  |  |  | 
894  |  | # ifndef OPENSSL_NO_EC2M  | 
895  |  | int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group,  | 
896  |  |                                          EC_POINT *point, const BIGNUM *x,  | 
897  |  |                                          const BIGNUM *y, BN_CTX *ctx)  | 
898  | 0  | { | 
899  | 0  |     return EC_POINT_set_affine_coordinates(group, point, x, y, ctx);  | 
900  | 0  | }  | 
901  |  | # endif  | 
902  |  | #endif  | 
903  |  |  | 
904  |  | int EC_POINT_get_affine_coordinates(const EC_GROUP *group,  | 
905  |  |                                     const EC_POINT *point, BIGNUM *x, BIGNUM *y,  | 
906  |  |                                     BN_CTX *ctx)  | 
907  | 0  | { | 
908  | 0  |     if (group->meth->point_get_affine_coordinates == NULL) { | 
909  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
910  | 0  |         return 0;  | 
911  | 0  |     }  | 
912  | 0  |     if (!ec_point_is_compat(point, group)) { | 
913  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
914  | 0  |         return 0;  | 
915  | 0  |     }  | 
916  | 0  |     if (EC_POINT_is_at_infinity(group, point)) { | 
917  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY);  | 
918  | 0  |         return 0;  | 
919  | 0  |     }  | 
920  | 0  |     return group->meth->point_get_affine_coordinates(group, point, x, y, ctx);  | 
921  | 0  | }  | 
922  |  |  | 
923  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
924  |  | int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,  | 
925  |  |                                         const EC_POINT *point, BIGNUM *x,  | 
926  |  |                                         BIGNUM *y, BN_CTX *ctx)  | 
927  | 0  | { | 
928  | 0  |     return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);  | 
929  | 0  | }  | 
930  |  |  | 
931  |  | # ifndef OPENSSL_NO_EC2M  | 
932  |  | int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,  | 
933  |  |                                          const EC_POINT *point, BIGNUM *x,  | 
934  |  |                                          BIGNUM *y, BN_CTX *ctx)  | 
935  | 0  | { | 
936  | 0  |     return EC_POINT_get_affine_coordinates(group, point, x, y, ctx);  | 
937  | 0  | }  | 
938  |  | # endif  | 
939  |  | #endif  | 
940  |  |  | 
941  |  | int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,  | 
942  |  |                  const EC_POINT *b, BN_CTX *ctx)  | 
943  | 0  | { | 
944  | 0  |     if (group->meth->add == 0) { | 
945  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
946  | 0  |         return 0;  | 
947  | 0  |     }  | 
948  | 0  |     if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)  | 
949  | 0  |         || !ec_point_is_compat(b, group)) { | 
950  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
951  | 0  |         return 0;  | 
952  | 0  |     }  | 
953  | 0  |     return group->meth->add(group, r, a, b, ctx);  | 
954  | 0  | }  | 
955  |  |  | 
956  |  | int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,  | 
957  |  |                  BN_CTX *ctx)  | 
958  | 0  | { | 
959  | 0  |     if (group->meth->dbl == 0) { | 
960  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
961  | 0  |         return 0;  | 
962  | 0  |     }  | 
963  | 0  |     if (!ec_point_is_compat(r, group) || !ec_point_is_compat(a, group)) { | 
964  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
965  | 0  |         return 0;  | 
966  | 0  |     }  | 
967  | 0  |     return group->meth->dbl(group, r, a, ctx);  | 
968  | 0  | }  | 
969  |  |  | 
970  |  | int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)  | 
971  | 0  | { | 
972  | 0  |     if (group->meth->invert == 0) { | 
973  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
974  | 0  |         return 0;  | 
975  | 0  |     }  | 
976  | 0  |     if (!ec_point_is_compat(a, group)) { | 
977  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
978  | 0  |         return 0;  | 
979  | 0  |     }  | 
980  | 0  |     return group->meth->invert(group, a, ctx);  | 
981  | 0  | }  | 
982  |  |  | 
983  |  | int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)  | 
984  | 0  | { | 
985  | 0  |     if (group->meth->is_at_infinity == 0) { | 
986  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
987  | 0  |         return 0;  | 
988  | 0  |     }  | 
989  | 0  |     if (!ec_point_is_compat(point, group)) { | 
990  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
991  | 0  |         return 0;  | 
992  | 0  |     }  | 
993  | 0  |     return group->meth->is_at_infinity(group, point);  | 
994  | 0  | }  | 
995  |  |  | 
996  |  | /*  | 
997  |  |  * Check whether an EC_POINT is on the curve or not. Note that the return  | 
998  |  |  * value for this function should NOT be treated as a boolean. Return values:  | 
999  |  |  *  1: The point is on the curve  | 
1000  |  |  *  0: The point is not on the curve  | 
1001  |  |  * -1: An error occurred  | 
1002  |  |  */  | 
1003  |  | int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,  | 
1004  |  |                          BN_CTX *ctx)  | 
1005  | 0  | { | 
1006  | 0  |     if (group->meth->is_on_curve == 0) { | 
1007  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
1008  | 0  |         return 0;  | 
1009  | 0  |     }  | 
1010  | 0  |     if (!ec_point_is_compat(point, group)) { | 
1011  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1012  | 0  |         return 0;  | 
1013  | 0  |     }  | 
1014  | 0  |     return group->meth->is_on_curve(group, point, ctx);  | 
1015  | 0  | }  | 
1016  |  |  | 
1017  |  | int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,  | 
1018  |  |                  BN_CTX *ctx)  | 
1019  | 0  | { | 
1020  | 0  |     if (group->meth->point_cmp == 0) { | 
1021  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
1022  | 0  |         return -1;  | 
1023  | 0  |     }  | 
1024  | 0  |     if (!ec_point_is_compat(a, group) || !ec_point_is_compat(b, group)) { | 
1025  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1026  | 0  |         return -1;  | 
1027  | 0  |     }  | 
1028  | 0  |     return group->meth->point_cmp(group, a, b, ctx);  | 
1029  | 0  | }  | 
1030  |  |  | 
1031  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
1032  |  | int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)  | 
1033  | 0  | { | 
1034  | 0  |     if (group->meth->make_affine == 0) { | 
1035  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
1036  | 0  |         return 0;  | 
1037  | 0  |     }  | 
1038  | 0  |     if (!ec_point_is_compat(point, group)) { | 
1039  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1040  | 0  |         return 0;  | 
1041  | 0  |     }  | 
1042  | 0  |     return group->meth->make_affine(group, point, ctx);  | 
1043  | 0  | }  | 
1044  |  |  | 
1045  |  | int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,  | 
1046  |  |                           EC_POINT *points[], BN_CTX *ctx)  | 
1047  | 0  | { | 
1048  | 0  |     size_t i;  | 
1049  |  | 
  | 
1050  | 0  |     if (group->meth->points_make_affine == 0) { | 
1051  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
1052  | 0  |         return 0;  | 
1053  | 0  |     }  | 
1054  | 0  |     for (i = 0; i < num; i++) { | 
1055  | 0  |         if (!ec_point_is_compat(points[i], group)) { | 
1056  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1057  | 0  |             return 0;  | 
1058  | 0  |         }  | 
1059  | 0  |     }  | 
1060  | 0  |     return group->meth->points_make_affine(group, num, points, ctx);  | 
1061  | 0  | }  | 
1062  |  | #endif  | 
1063  |  |  | 
1064  |  | /*  | 
1065  |  |  * Functions for point multiplication. If group->meth->mul is 0, we use the  | 
1066  |  |  * wNAF-based implementations in ec_mult.c; otherwise we dispatch through  | 
1067  |  |  * methods.  | 
1068  |  |  */  | 
1069  |  |  | 
1070  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
1071  |  | int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,  | 
1072  |  |                   size_t num, const EC_POINT *points[],  | 
1073  |  |                   const BIGNUM *scalars[], BN_CTX *ctx)  | 
1074  | 0  | { | 
1075  | 0  |     int ret = 0;  | 
1076  | 0  |     size_t i = 0;  | 
1077  | 0  | #ifndef FIPS_MODULE  | 
1078  | 0  |     BN_CTX *new_ctx = NULL;  | 
1079  | 0  | #endif  | 
1080  |  | 
  | 
1081  | 0  |     if (!ec_point_is_compat(r, group)) { | 
1082  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1083  | 0  |         return 0;  | 
1084  | 0  |     }  | 
1085  |  |  | 
1086  | 0  |     if (scalar == NULL && num == 0)  | 
1087  | 0  |         return EC_POINT_set_to_infinity(group, r);  | 
1088  |  |  | 
1089  | 0  |     for (i = 0; i < num; i++) { | 
1090  | 0  |         if (!ec_point_is_compat(points[i], group)) { | 
1091  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1092  | 0  |             return 0;  | 
1093  | 0  |         }  | 
1094  | 0  |     }  | 
1095  |  |  | 
1096  | 0  | #ifndef FIPS_MODULE  | 
1097  | 0  |     if (ctx == NULL)  | 
1098  | 0  |         ctx = new_ctx = BN_CTX_secure_new();  | 
1099  | 0  | #endif  | 
1100  | 0  |     if (ctx == NULL) { | 
1101  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);  | 
1102  | 0  |         return 0;  | 
1103  | 0  |     }  | 
1104  |  |  | 
1105  | 0  |     if (group->meth->mul != NULL)  | 
1106  | 0  |         ret = group->meth->mul(group, r, scalar, num, points, scalars, ctx);  | 
1107  | 0  |     else  | 
1108  |  |         /* use default */  | 
1109  | 0  |         ret = ossl_ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);  | 
1110  |  | 
  | 
1111  | 0  | #ifndef FIPS_MODULE  | 
1112  | 0  |     BN_CTX_free(new_ctx);  | 
1113  | 0  | #endif  | 
1114  | 0  |     return ret;  | 
1115  | 0  | }  | 
1116  |  | #endif  | 
1117  |  |  | 
1118  |  | int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,  | 
1119  |  |                  const EC_POINT *point, const BIGNUM *p_scalar, BN_CTX *ctx)  | 
1120  | 0  | { | 
1121  | 0  |     int ret = 0;  | 
1122  | 0  |     size_t num;  | 
1123  | 0  | #ifndef FIPS_MODULE  | 
1124  | 0  |     BN_CTX *new_ctx = NULL;  | 
1125  | 0  | #endif  | 
1126  |  | 
  | 
1127  | 0  |     if (!ec_point_is_compat(r, group)  | 
1128  | 0  |         || (point != NULL && !ec_point_is_compat(point, group))) { | 
1129  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INCOMPATIBLE_OBJECTS);  | 
1130  | 0  |         return 0;  | 
1131  | 0  |     }  | 
1132  |  |  | 
1133  | 0  |     if (g_scalar == NULL && p_scalar == NULL)  | 
1134  | 0  |         return EC_POINT_set_to_infinity(group, r);  | 
1135  |  |  | 
1136  | 0  | #ifndef FIPS_MODULE  | 
1137  | 0  |     if (ctx == NULL)  | 
1138  | 0  |         ctx = new_ctx = BN_CTX_secure_new();  | 
1139  | 0  | #endif  | 
1140  | 0  |     if (ctx == NULL) { | 
1141  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR);  | 
1142  | 0  |         return 0;  | 
1143  | 0  |     }  | 
1144  |  |  | 
1145  | 0  |     num = (point != NULL && p_scalar != NULL) ? 1 : 0;  | 
1146  | 0  |     if (group->meth->mul != NULL)  | 
1147  | 0  |         ret = group->meth->mul(group, r, g_scalar, num, &point, &p_scalar, ctx);  | 
1148  | 0  |     else  | 
1149  |  |         /* use default */  | 
1150  | 0  |         ret = ossl_ec_wNAF_mul(group, r, g_scalar, num, &point, &p_scalar, ctx);  | 
1151  |  | 
  | 
1152  | 0  | #ifndef FIPS_MODULE  | 
1153  | 0  |     BN_CTX_free(new_ctx);  | 
1154  | 0  | #endif  | 
1155  | 0  |     return ret;  | 
1156  | 0  | }  | 
1157  |  |  | 
1158  |  | #ifndef OPENSSL_NO_DEPRECATED_3_0  | 
1159  |  | int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx)  | 
1160  | 0  | { | 
1161  | 0  |     if (group->meth->mul == 0)  | 
1162  |  |         /* use default */  | 
1163  | 0  |         return ossl_ec_wNAF_precompute_mult(group, ctx);  | 
1164  |  |  | 
1165  | 0  |     if (group->meth->precompute_mult != 0)  | 
1166  | 0  |         return group->meth->precompute_mult(group, ctx);  | 
1167  | 0  |     else  | 
1168  | 0  |         return 1;               /* nothing to do, so report success */  | 
1169  | 0  | }  | 
1170  |  |  | 
1171  |  | int EC_GROUP_have_precompute_mult(const EC_GROUP *group)  | 
1172  | 0  | { | 
1173  | 0  |     if (group->meth->mul == 0)  | 
1174  |  |         /* use default */  | 
1175  | 0  |         return ossl_ec_wNAF_have_precompute_mult(group);  | 
1176  |  |  | 
1177  | 0  |     if (group->meth->have_precompute_mult != 0)  | 
1178  | 0  |         return group->meth->have_precompute_mult(group);  | 
1179  | 0  |     else  | 
1180  | 0  |         return 0;               /* cannot tell whether precomputation has  | 
1181  |  |                                  * been performed */  | 
1182  | 0  | }  | 
1183  |  | #endif  | 
1184  |  |  | 
1185  |  | /*  | 
1186  |  |  * ec_precompute_mont_data sets |group->mont_data| from |group->order| and  | 
1187  |  |  * returns one on success. On error it returns zero.  | 
1188  |  |  */  | 
1189  |  | static int ec_precompute_mont_data(EC_GROUP *group)  | 
1190  | 0  | { | 
1191  | 0  |     BN_CTX *ctx = BN_CTX_new_ex(group->libctx);  | 
1192  | 0  |     int ret = 0;  | 
1193  |  | 
  | 
1194  | 0  |     BN_MONT_CTX_free(group->mont_data);  | 
1195  | 0  |     group->mont_data = NULL;  | 
1196  |  | 
  | 
1197  | 0  |     if (ctx == NULL)  | 
1198  | 0  |         goto err;  | 
1199  |  |  | 
1200  | 0  |     group->mont_data = BN_MONT_CTX_new();  | 
1201  | 0  |     if (group->mont_data == NULL)  | 
1202  | 0  |         goto err;  | 
1203  |  |  | 
1204  | 0  |     if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx)) { | 
1205  | 0  |         BN_MONT_CTX_free(group->mont_data);  | 
1206  | 0  |         group->mont_data = NULL;  | 
1207  | 0  |         goto err;  | 
1208  | 0  |     }  | 
1209  |  |  | 
1210  | 0  |     ret = 1;  | 
1211  |  | 
  | 
1212  | 0  |  err:  | 
1213  |  | 
  | 
1214  | 0  |     BN_CTX_free(ctx);  | 
1215  | 0  |     return ret;  | 
1216  | 0  | }  | 
1217  |  |  | 
1218  |  | #ifndef FIPS_MODULE  | 
1219  |  | int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg)  | 
1220  | 0  | { | 
1221  | 0  |     return CRYPTO_set_ex_data(&key->ex_data, idx, arg);  | 
1222  | 0  | }  | 
1223  |  |  | 
1224  |  | void *EC_KEY_get_ex_data(const EC_KEY *key, int idx)  | 
1225  | 0  | { | 
1226  | 0  |     return CRYPTO_get_ex_data(&key->ex_data, idx);  | 
1227  | 0  | }  | 
1228  |  | #endif  | 
1229  |  |  | 
1230  |  | int ossl_ec_group_simple_order_bits(const EC_GROUP *group)  | 
1231  | 0  | { | 
1232  | 0  |     if (group->order == NULL)  | 
1233  | 0  |         return 0;  | 
1234  | 0  |     return BN_num_bits(group->order);  | 
1235  | 0  | }  | 
1236  |  |  | 
1237  |  | static int ec_field_inverse_mod_ord(const EC_GROUP *group, BIGNUM *r,  | 
1238  |  |                                     const BIGNUM *x, BN_CTX *ctx)  | 
1239  | 0  | { | 
1240  | 0  |     BIGNUM *e = NULL;  | 
1241  | 0  |     int ret = 0;  | 
1242  | 0  | #ifndef FIPS_MODULE  | 
1243  | 0  |     BN_CTX *new_ctx = NULL;  | 
1244  | 0  | #endif  | 
1245  |  | 
  | 
1246  | 0  |     if (group->mont_data == NULL)  | 
1247  | 0  |         return 0;  | 
1248  |  |  | 
1249  | 0  | #ifndef FIPS_MODULE  | 
1250  | 0  |     if (ctx == NULL)  | 
1251  | 0  |         ctx = new_ctx = BN_CTX_secure_new();  | 
1252  | 0  | #endif  | 
1253  | 0  |     if (ctx == NULL)  | 
1254  | 0  |         return 0;  | 
1255  |  |  | 
1256  | 0  |     BN_CTX_start(ctx);  | 
1257  | 0  |     if ((e = BN_CTX_get(ctx)) == NULL)  | 
1258  | 0  |         goto err;  | 
1259  |  |  | 
1260  |  |     /*-  | 
1261  |  |      * We want inverse in constant time, therefore we utilize the fact  | 
1262  |  |      * order must be prime and use Fermats Little Theorem instead.  | 
1263  |  |      */  | 
1264  | 0  |     if (!BN_set_word(e, 2))  | 
1265  | 0  |         goto err;  | 
1266  | 0  |     if (!BN_sub(e, group->order, e))  | 
1267  | 0  |         goto err;  | 
1268  |  |     /*-  | 
1269  |  |      * Although the exponent is public we want the result to be  | 
1270  |  |      * fixed top.  | 
1271  |  |      */  | 
1272  | 0  |     if (!bn_mod_exp_mont_fixed_top(r, x, e, group->order, ctx, group->mont_data))  | 
1273  | 0  |         goto err;  | 
1274  |  |  | 
1275  | 0  |     ret = 1;  | 
1276  |  | 
  | 
1277  | 0  |  err:  | 
1278  | 0  |     BN_CTX_end(ctx);  | 
1279  | 0  | #ifndef FIPS_MODULE  | 
1280  | 0  |     BN_CTX_free(new_ctx);  | 
1281  | 0  | #endif  | 
1282  | 0  |     return ret;  | 
1283  | 0  | }  | 
1284  |  |  | 
1285  |  | /*-  | 
1286  |  |  * Default behavior, if group->meth->field_inverse_mod_ord is NULL:  | 
1287  |  |  * - When group->order is even, this function returns an error.  | 
1288  |  |  * - When group->order is otherwise composite, the correctness  | 
1289  |  |  *   of the output is not guaranteed.  | 
1290  |  |  * - When x is outside the range [1, group->order), the correctness  | 
1291  |  |  *   of the output is not guaranteed.  | 
1292  |  |  * - Otherwise, this function returns the multiplicative inverse in the  | 
1293  |  |  *   range [1, group->order).  | 
1294  |  |  *  | 
1295  |  |  * EC_METHODs must implement their own field_inverse_mod_ord for  | 
1296  |  |  * other functionality.  | 
1297  |  |  */  | 
1298  |  | int ossl_ec_group_do_inverse_ord(const EC_GROUP *group, BIGNUM *res,  | 
1299  |  |                                  const BIGNUM *x, BN_CTX *ctx)  | 
1300  | 0  | { | 
1301  | 0  |     if (group->meth->field_inverse_mod_ord != NULL)  | 
1302  | 0  |         return group->meth->field_inverse_mod_ord(group, res, x, ctx);  | 
1303  | 0  |     else  | 
1304  | 0  |         return ec_field_inverse_mod_ord(group, res, x, ctx);  | 
1305  | 0  | }  | 
1306  |  |  | 
1307  |  | /*-  | 
1308  |  |  * Coordinate blinding for EC_POINT.  | 
1309  |  |  *  | 
1310  |  |  * The underlying EC_METHOD can optionally implement this function:  | 
1311  |  |  * underlying implementations should return 0 on errors, or 1 on  | 
1312  |  |  * success.  | 
1313  |  |  *  | 
1314  |  |  * This wrapper returns 1 in case the underlying EC_METHOD does not  | 
1315  |  |  * support coordinate blinding.  | 
1316  |  |  */  | 
1317  |  | int ossl_ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p,  | 
1318  |  |                                     BN_CTX *ctx)  | 
1319  | 0  | { | 
1320  | 0  |     if (group->meth->blind_coordinates == NULL)  | 
1321  | 0  |         return 1; /* ignore if not implemented */  | 
1322  |  |  | 
1323  | 0  |     return group->meth->blind_coordinates(group, p, ctx);  | 
1324  | 0  | }  | 
1325  |  |  | 
1326  |  | int EC_GROUP_get_basis_type(const EC_GROUP *group)  | 
1327  | 0  | { | 
1328  | 0  |     int i;  | 
1329  |  | 
  | 
1330  | 0  |     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field)  | 
1331  |  |         /* everything else is currently not supported */  | 
1332  | 0  |         return 0;  | 
1333  |  |  | 
1334  |  |     /* Find the last non-zero element of group->poly[] */  | 
1335  | 0  |     for (i = 0;  | 
1336  | 0  |          i < (int)OSSL_NELEM(group->poly) && group->poly[i] != 0;  | 
1337  | 0  |          i++)  | 
1338  | 0  |         continue;  | 
1339  |  | 
  | 
1340  | 0  |     if (i == 4)  | 
1341  | 0  |         return NID_X9_62_ppBasis;  | 
1342  | 0  |     else if (i == 2)  | 
1343  | 0  |         return NID_X9_62_tpBasis;  | 
1344  | 0  |     else  | 
1345  |  |         /* everything else is currently not supported */  | 
1346  | 0  |         return 0;  | 
1347  | 0  | }  | 
1348  |  |  | 
1349  |  | #ifndef OPENSSL_NO_EC2M  | 
1350  |  | int EC_GROUP_get_trinomial_basis(const EC_GROUP *group, unsigned int *k)  | 
1351  | 0  | { | 
1352  | 0  |     if (group == NULL)  | 
1353  | 0  |         return 0;  | 
1354  |  |  | 
1355  | 0  |     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field  | 
1356  | 0  |         || !((group->poly[0] != 0) && (group->poly[1] != 0)  | 
1357  | 0  |              && (group->poly[2] == 0))) { | 
1358  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
1359  | 0  |         return 0;  | 
1360  | 0  |     }  | 
1361  |  |  | 
1362  | 0  |     if (k)  | 
1363  | 0  |         *k = group->poly[1];  | 
1364  |  | 
  | 
1365  | 0  |     return 1;  | 
1366  | 0  | }  | 
1367  |  |  | 
1368  |  | int EC_GROUP_get_pentanomial_basis(const EC_GROUP *group, unsigned int *k1,  | 
1369  |  |                                    unsigned int *k2, unsigned int *k3)  | 
1370  | 0  | { | 
1371  | 0  |     if (group == NULL)  | 
1372  | 0  |         return 0;  | 
1373  |  |  | 
1374  | 0  |     if (EC_GROUP_get_field_type(group) != NID_X9_62_characteristic_two_field  | 
1375  | 0  |         || !((group->poly[0] != 0) && (group->poly[1] != 0)  | 
1376  | 0  |              && (group->poly[2] != 0) && (group->poly[3] != 0)  | 
1377  | 0  |              && (group->poly[4] == 0))) { | 
1378  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);  | 
1379  | 0  |         return 0;  | 
1380  | 0  |     }  | 
1381  |  |  | 
1382  | 0  |     if (k1)  | 
1383  | 0  |         *k1 = group->poly[3];  | 
1384  | 0  |     if (k2)  | 
1385  | 0  |         *k2 = group->poly[2];  | 
1386  | 0  |     if (k3)  | 
1387  | 0  |         *k3 = group->poly[1];  | 
1388  |  | 
  | 
1389  | 0  |     return 1;  | 
1390  | 0  | }  | 
1391  |  | #endif  | 
1392  |  |  | 
1393  |  | #ifndef FIPS_MODULE  | 
1394  |  | /*  | 
1395  |  |  * Check if the explicit parameters group matches any built-in curves.  | 
1396  |  |  *  | 
1397  |  |  * We create a copy of the group just built, so that we can remove optional  | 
1398  |  |  * fields for the lookup: we do this to avoid the possibility that one of  | 
1399  |  |  * the optional parameters is used to force the library into using a less  | 
1400  |  |  * performant and less secure EC_METHOD instead of the specialized one.  | 
1401  |  |  * In any case, `seed` is not really used in any computation, while a  | 
1402  |  |  * cofactor different from the one in the built-in table is just  | 
1403  |  |  * mathematically wrong anyway and should not be used.  | 
1404  |  |  */  | 
1405  |  | static EC_GROUP *ec_group_explicit_to_named(const EC_GROUP *group,  | 
1406  |  |                                             OSSL_LIB_CTX *libctx,  | 
1407  |  |                                             const char *propq,  | 
1408  |  |                                             BN_CTX *ctx)  | 
1409  | 0  | { | 
1410  | 0  |     EC_GROUP *ret_group = NULL, *dup = NULL;  | 
1411  | 0  |     int curve_name_nid;  | 
1412  |  | 
  | 
1413  | 0  |     const EC_POINT *point = EC_GROUP_get0_generator(group);  | 
1414  | 0  |     const BIGNUM *order = EC_GROUP_get0_order(group);  | 
1415  | 0  |     int no_seed = (EC_GROUP_get0_seed(group) == NULL);  | 
1416  |  | 
  | 
1417  | 0  |     if ((dup = EC_GROUP_dup(group)) == NULL  | 
1418  | 0  |             || EC_GROUP_set_seed(dup, NULL, 0) != 1  | 
1419  | 0  |             || !EC_GROUP_set_generator(dup, point, order, NULL))  | 
1420  | 0  |         goto err;  | 
1421  | 0  |     if ((curve_name_nid = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) { | 
1422  |  |         /*  | 
1423  |  |          * The input explicit parameters successfully matched one of the  | 
1424  |  |          * built-in curves: often for built-in curves we have specialized  | 
1425  |  |          * methods with better performance and hardening.  | 
1426  |  |          *  | 
1427  |  |          * In this case we replace the `EC_GROUP` created through explicit  | 
1428  |  |          * parameters with one created from a named group.  | 
1429  |  |          */  | 
1430  |  | 
  | 
1431  | 0  | # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128  | 
1432  |  |         /*  | 
1433  |  |          * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for  | 
1434  |  |          * the same curve, we prefer the SECP nid when matching explicit  | 
1435  |  |          * parameters as that is associated with a specialized EC_METHOD.  | 
1436  |  |          */  | 
1437  | 0  |         if (curve_name_nid == NID_wap_wsg_idm_ecid_wtls12)  | 
1438  | 0  |             curve_name_nid = NID_secp224r1;  | 
1439  | 0  | # endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */  | 
1440  |  | 
  | 
1441  | 0  |         ret_group = EC_GROUP_new_by_curve_name_ex(libctx, propq, curve_name_nid);  | 
1442  | 0  |         if (ret_group == NULL)  | 
1443  | 0  |             goto err;  | 
1444  |  |  | 
1445  |  |         /*  | 
1446  |  |          * Set the flag so that EC_GROUPs created from explicit parameters are  | 
1447  |  |          * serialized using explicit parameters by default.  | 
1448  |  |          */  | 
1449  | 0  |         EC_GROUP_set_asn1_flag(ret_group, OPENSSL_EC_EXPLICIT_CURVE);  | 
1450  |  |  | 
1451  |  |         /*  | 
1452  |  |          * If the input params do not contain the optional seed field we make  | 
1453  |  |          * sure it is not added to the returned group.  | 
1454  |  |          *  | 
1455  |  |          * The seed field is not really used inside libcrypto anyway, and  | 
1456  |  |          * adding it to parsed explicit parameter keys would alter their DER  | 
1457  |  |          * encoding output (because of the extra field) which could impact  | 
1458  |  |          * applications fingerprinting keys by their DER encoding.  | 
1459  |  |          */  | 
1460  | 0  |         if (no_seed) { | 
1461  | 0  |             if (EC_GROUP_set_seed(ret_group, NULL, 0) != 1)  | 
1462  | 0  |                 goto err;  | 
1463  | 0  |         }  | 
1464  | 0  |     } else { | 
1465  | 0  |         ret_group = (EC_GROUP *)group;  | 
1466  | 0  |     }  | 
1467  | 0  |     EC_GROUP_free(dup);  | 
1468  | 0  |     return ret_group;  | 
1469  | 0  | err:  | 
1470  | 0  |     EC_GROUP_free(dup);  | 
1471  | 0  |     EC_GROUP_free(ret_group);  | 
1472  | 0  |     return NULL;  | 
1473  | 0  | }  | 
1474  |  | #endif /* FIPS_MODULE */  | 
1475  |  |  | 
1476  |  | static EC_GROUP *group_new_from_name(const OSSL_PARAM *p,  | 
1477  |  |                                      OSSL_LIB_CTX *libctx, const char *propq)  | 
1478  | 0  | { | 
1479  | 0  |     int ok = 0, nid;  | 
1480  | 0  |     const char *curve_name = NULL;  | 
1481  |  | 
  | 
1482  | 0  |     switch (p->data_type) { | 
1483  | 0  |     case OSSL_PARAM_UTF8_STRING:  | 
1484  |  |         /* The OSSL_PARAM functions have no support for this */  | 
1485  | 0  |         curve_name = p->data;  | 
1486  | 0  |         ok = (curve_name != NULL);  | 
1487  | 0  |         break;  | 
1488  | 0  |     case OSSL_PARAM_UTF8_PTR:  | 
1489  | 0  |         ok = OSSL_PARAM_get_utf8_ptr(p, &curve_name);  | 
1490  | 0  |         break;  | 
1491  | 0  |     }  | 
1492  |  |  | 
1493  | 0  |     if (ok) { | 
1494  | 0  |         nid = ossl_ec_curve_name2nid(curve_name);  | 
1495  | 0  |         if (nid == NID_undef) { | 
1496  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_CURVE);  | 
1497  | 0  |             return NULL;  | 
1498  | 0  |         } else { | 
1499  | 0  |             return EC_GROUP_new_by_curve_name_ex(libctx, propq, nid);  | 
1500  | 0  |         }  | 
1501  | 0  |     }  | 
1502  | 0  |     return NULL;  | 
1503  | 0  | }  | 
1504  |  |  | 
1505  |  | /* These parameters can be set directly into an EC_GROUP */  | 
1506  |  | int ossl_ec_group_set_params(EC_GROUP *group, const OSSL_PARAM params[])  | 
1507  | 0  | { | 
1508  | 0  |     int encoding_flag = -1, format = -1;  | 
1509  | 0  |     const OSSL_PARAM *p;  | 
1510  |  | 
  | 
1511  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT);  | 
1512  | 0  |     if (p != NULL) { | 
1513  | 0  |         if (!ossl_ec_pt_format_param2id(p, &format)) { | 
1514  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);  | 
1515  | 0  |             return 0;  | 
1516  | 0  |         }  | 
1517  | 0  |         EC_GROUP_set_point_conversion_form(group, format);  | 
1518  | 0  |     }  | 
1519  |  |  | 
1520  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);  | 
1521  | 0  |     if (p != NULL) { | 
1522  | 0  |         if (!ossl_ec_encoding_param2id(p, &encoding_flag)) { | 
1523  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_FORM);  | 
1524  | 0  |             return 0;  | 
1525  | 0  |         }  | 
1526  | 0  |         EC_GROUP_set_asn1_flag(group, encoding_flag);  | 
1527  | 0  |     }  | 
1528  |  |     /* Optional seed */  | 
1529  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);  | 
1530  | 0  |     if (p != NULL) { | 
1531  |  |         /* The seed is allowed to be NULL */  | 
1532  | 0  |         if (p->data_type != OSSL_PARAM_OCTET_STRING  | 
1533  | 0  |             || !EC_GROUP_set_seed(group, p->data, p->data_size)) { | 
1534  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);  | 
1535  | 0  |             return 0;  | 
1536  | 0  |         }  | 
1537  | 0  |     }  | 
1538  | 0  |     return 1;  | 
1539  | 0  | }  | 
1540  |  |  | 
1541  |  | EC_GROUP *EC_GROUP_new_from_params(const OSSL_PARAM params[],  | 
1542  |  |                                    OSSL_LIB_CTX *libctx, const char *propq)  | 
1543  | 0  | { | 
1544  | 0  |     const OSSL_PARAM *ptmp;  | 
1545  | 0  |     EC_GROUP *group = NULL;  | 
1546  |  | 
  | 
1547  | 0  | #ifndef FIPS_MODULE  | 
1548  | 0  |     const OSSL_PARAM *pa, *pb;  | 
1549  | 0  |     int ok = 0;  | 
1550  | 0  |     EC_GROUP *named_group = NULL;  | 
1551  | 0  |     BIGNUM *p = NULL, *a = NULL, *b = NULL, *order = NULL, *cofactor = NULL;  | 
1552  | 0  |     EC_POINT *point = NULL;  | 
1553  | 0  |     int field_bits = 0;  | 
1554  | 0  |     int is_prime_field = 1;  | 
1555  | 0  |     BN_CTX *bnctx = NULL;  | 
1556  | 0  |     const unsigned char *buf = NULL;  | 
1557  | 0  |     int encoding_flag = -1;  | 
1558  | 0  | #endif  | 
1559  |  |  | 
1560  |  |     /* This is the simple named group case */  | 
1561  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);  | 
1562  | 0  |     if (ptmp != NULL) { | 
1563  | 0  |         int decoded = 0;  | 
1564  |  | 
  | 
1565  | 0  |         if ((group = group_new_from_name(ptmp, libctx, propq)) == NULL)  | 
1566  | 0  |             return NULL;  | 
1567  | 0  |         if (!ossl_ec_group_set_params(group, params)) { | 
1568  | 0  |             EC_GROUP_free(group);  | 
1569  | 0  |             return NULL;  | 
1570  | 0  |         }  | 
1571  |  |  | 
1572  | 0  |         ptmp = OSSL_PARAM_locate_const(params,  | 
1573  | 0  |                                        OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS);  | 
1574  | 0  |         if (ptmp != NULL && !OSSL_PARAM_get_int(ptmp, &decoded)) { | 
1575  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_WRONG_CURVE_PARAMETERS);  | 
1576  | 0  |             EC_GROUP_free(group);  | 
1577  | 0  |             return NULL;  | 
1578  | 0  |         }  | 
1579  | 0  |         group->decoded_from_explicit_params = decoded > 0;  | 
1580  | 0  |         return group;  | 
1581  | 0  |     }  | 
1582  |  | #ifdef FIPS_MODULE  | 
1583  |  |     ERR_raise(ERR_LIB_EC, EC_R_EXPLICIT_PARAMS_NOT_SUPPORTED);  | 
1584  |  |     return NULL;  | 
1585  |  | #else  | 
1586  |  |     /* If it gets here then we are trying explicit parameters */  | 
1587  | 0  |     bnctx = BN_CTX_new_ex(libctx);  | 
1588  | 0  |     if (bnctx == NULL) { | 
1589  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);  | 
1590  | 0  |         return 0;  | 
1591  | 0  |     }  | 
1592  | 0  |     BN_CTX_start(bnctx);  | 
1593  |  | 
  | 
1594  | 0  |     p = BN_CTX_get(bnctx);  | 
1595  | 0  |     a = BN_CTX_get(bnctx);  | 
1596  | 0  |     b = BN_CTX_get(bnctx);  | 
1597  | 0  |     order = BN_CTX_get(bnctx);  | 
1598  | 0  |     if (order == NULL) { | 
1599  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);  | 
1600  | 0  |         goto err;  | 
1601  | 0  |     }  | 
1602  |  |  | 
1603  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_FIELD_TYPE);  | 
1604  | 0  |     if (ptmp == NULL || ptmp->data_type != OSSL_PARAM_UTF8_STRING) { | 
1605  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD);  | 
1606  | 0  |         goto err;  | 
1607  | 0  |     }  | 
1608  | 0  |     if (OPENSSL_strcasecmp(ptmp->data, SN_X9_62_prime_field) == 0) { | 
1609  | 0  |         is_prime_field = 1;  | 
1610  | 0  |     } else if (OPENSSL_strcasecmp(ptmp->data,  | 
1611  | 0  |                                   SN_X9_62_characteristic_two_field) == 0) { | 
1612  | 0  |         is_prime_field = 0;  | 
1613  | 0  |     } else { | 
1614  |  |         /* Invalid field */  | 
1615  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD);  | 
1616  | 0  |         goto err;  | 
1617  | 0  |     }  | 
1618  |  |  | 
1619  | 0  |     pa = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_A);  | 
1620  | 0  |     if (!OSSL_PARAM_get_BN(pa, &a)) { | 
1621  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_A);  | 
1622  | 0  |         goto err;  | 
1623  | 0  |     }  | 
1624  | 0  |     pb = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_B);  | 
1625  | 0  |     if (!OSSL_PARAM_get_BN(pb, &b)) { | 
1626  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_B);  | 
1627  | 0  |         goto err;  | 
1628  | 0  |     }  | 
1629  |  |  | 
1630  |  |     /* extract the prime number or irreducible polynomial */  | 
1631  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_P);  | 
1632  | 0  |     if (!OSSL_PARAM_get_BN(ptmp, &p)) { | 
1633  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);  | 
1634  | 0  |         goto err;  | 
1635  | 0  |     }  | 
1636  |  |  | 
1637  | 0  |     if (is_prime_field) { | 
1638  | 0  |         if (BN_is_negative(p) || BN_is_zero(p)) { | 
1639  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_P);  | 
1640  | 0  |             goto err;  | 
1641  | 0  |         }  | 
1642  | 0  |         field_bits = BN_num_bits(p);  | 
1643  | 0  |         if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | 
1644  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);  | 
1645  | 0  |             goto err;  | 
1646  | 0  |         }  | 
1647  |  |  | 
1648  |  |         /* create the EC_GROUP structure */  | 
1649  | 0  |         group = EC_GROUP_new_curve_GFp(p, a, b, bnctx);  | 
1650  | 0  |     } else { | 
1651  |  | # ifdef OPENSSL_NO_EC2M  | 
1652  |  |         ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED);  | 
1653  |  |         goto err;  | 
1654  |  | # else  | 
1655  |  |         /* create the EC_GROUP structure */  | 
1656  | 0  |         group = EC_GROUP_new_curve_GF2m(p, a, b, NULL);  | 
1657  | 0  |         if (group != NULL) { | 
1658  | 0  |             field_bits = EC_GROUP_get_degree(group);  | 
1659  | 0  |             if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | 
1660  | 0  |                 ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE);  | 
1661  | 0  |                 goto err;  | 
1662  | 0  |             }  | 
1663  | 0  |         }  | 
1664  | 0  | # endif /* OPENSSL_NO_EC2M */  | 
1665  | 0  |     }  | 
1666  |  |  | 
1667  | 0  |     if (group == NULL) { | 
1668  | 0  |         ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);  | 
1669  | 0  |         goto err;  | 
1670  | 0  |     }  | 
1671  |  |  | 
1672  |  |     /* Optional seed */  | 
1673  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_SEED);  | 
1674  | 0  |     if (ptmp != NULL) { | 
1675  | 0  |         if (ptmp->data_type != OSSL_PARAM_OCTET_STRING) { | 
1676  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_SEED);  | 
1677  | 0  |             goto err;  | 
1678  | 0  |         }  | 
1679  | 0  |         if (!EC_GROUP_set_seed(group, ptmp->data, ptmp->data_size))  | 
1680  | 0  |             goto err;  | 
1681  | 0  |     }  | 
1682  |  |  | 
1683  |  |     /* generator base point */  | 
1684  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_GENERATOR);  | 
1685  | 0  |     if (ptmp == NULL  | 
1686  | 0  |         || ptmp->data_type != OSSL_PARAM_OCTET_STRING) { | 
1687  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);  | 
1688  | 0  |         goto err;  | 
1689  | 0  |     }  | 
1690  | 0  |     buf = (const unsigned char *)(ptmp->data);  | 
1691  | 0  |     if ((point = EC_POINT_new(group)) == NULL)  | 
1692  | 0  |         goto err;  | 
1693  | 0  |     EC_GROUP_set_point_conversion_form(group,  | 
1694  | 0  |                                        (point_conversion_form_t)buf[0] & ~0x01);  | 
1695  | 0  |     if (!EC_POINT_oct2point(group, point, buf, ptmp->data_size, bnctx)) { | 
1696  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);  | 
1697  | 0  |         goto err;  | 
1698  | 0  |     }  | 
1699  |  |  | 
1700  |  |     /* order */  | 
1701  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ORDER);  | 
1702  | 0  |     if (!OSSL_PARAM_get_BN(ptmp, &order)  | 
1703  | 0  |         || (BN_is_negative(order) || BN_is_zero(order))  | 
1704  | 0  |         || (BN_num_bits(order) > (int)field_bits + 1)) { /* Hasse bound */ | 
1705  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER);  | 
1706  | 0  |         goto err;  | 
1707  | 0  |     }  | 
1708  |  |  | 
1709  |  |     /* Optional cofactor */  | 
1710  | 0  |     ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_COFACTOR);  | 
1711  | 0  |     if (ptmp != NULL) { | 
1712  | 0  |         cofactor = BN_CTX_get(bnctx);  | 
1713  | 0  |         if (cofactor == NULL || !OSSL_PARAM_get_BN(ptmp, &cofactor)) { | 
1714  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_COFACTOR);  | 
1715  | 0  |             goto err;  | 
1716  | 0  |         }  | 
1717  | 0  |     }  | 
1718  |  |  | 
1719  |  |     /* set the generator, order and cofactor (if present) */  | 
1720  | 0  |     if (!EC_GROUP_set_generator(group, point, order, cofactor)) { | 
1721  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_GENERATOR);  | 
1722  | 0  |         goto err;  | 
1723  | 0  |     }  | 
1724  |  |  | 
1725  | 0  |     named_group = ec_group_explicit_to_named(group, libctx, propq, bnctx);  | 
1726  | 0  |     if (named_group == NULL) { | 
1727  | 0  |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_NAMED_GROUP_CONVERSION);  | 
1728  | 0  |         goto err;  | 
1729  | 0  |     }  | 
1730  | 0  |     if (named_group == group) { | 
1731  |  |         /*  | 
1732  |  |          * If we did not find a named group then the encoding should be explicit  | 
1733  |  |          * if it was specified  | 
1734  |  |          */  | 
1735  | 0  |         ptmp = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_ENCODING);  | 
1736  | 0  |         if (ptmp != NULL  | 
1737  | 0  |             && !ossl_ec_encoding_param2id(ptmp, &encoding_flag)) { | 
1738  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);  | 
1739  | 0  |             goto err;  | 
1740  | 0  |         }  | 
1741  | 0  |         if (encoding_flag == OPENSSL_EC_NAMED_CURVE) { | 
1742  | 0  |             ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);  | 
1743  | 0  |             goto err;  | 
1744  | 0  |         }  | 
1745  | 0  |         EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);  | 
1746  | 0  |     } else { | 
1747  | 0  |         EC_GROUP_free(group);  | 
1748  | 0  |         group = named_group;  | 
1749  | 0  |     }  | 
1750  |  |     /* We've imported the group from explicit parameters, set it so. */  | 
1751  | 0  |     group->decoded_from_explicit_params = 1;  | 
1752  | 0  |     ok = 1;  | 
1753  | 0  |  err:  | 
1754  | 0  |     if (!ok) { | 
1755  | 0  |         EC_GROUP_free(group);  | 
1756  | 0  |         group = NULL;  | 
1757  | 0  |     }  | 
1758  | 0  |     EC_POINT_free(point);  | 
1759  | 0  |     BN_CTX_end(bnctx);  | 
1760  | 0  |     BN_CTX_free(bnctx);  | 
1761  |  | 
  | 
1762  | 0  |     return group;  | 
1763  | 0  | #endif /* FIPS_MODULE */  | 
1764  | 0  | }  | 
1765  |  |  | 
1766  |  | OSSL_PARAM *EC_GROUP_to_params(const EC_GROUP *group, OSSL_LIB_CTX *libctx,  | 
1767  |  |                                const char *propq, BN_CTX *bnctx)  | 
1768  | 0  | { | 
1769  | 0  |     OSSL_PARAM_BLD *tmpl = NULL;  | 
1770  | 0  |     BN_CTX *new_bnctx = NULL;  | 
1771  | 0  |     unsigned char *gen_buf = NULL;  | 
1772  | 0  |     OSSL_PARAM *params = NULL;  | 
1773  |  | 
  | 
1774  | 0  |     if (group == NULL)  | 
1775  | 0  |         goto err;  | 
1776  |  |  | 
1777  | 0  |     tmpl = OSSL_PARAM_BLD_new();  | 
1778  | 0  |     if (tmpl == NULL)  | 
1779  | 0  |         goto err;  | 
1780  |  |  | 
1781  | 0  |     if (bnctx == NULL)  | 
1782  | 0  |         bnctx = new_bnctx = BN_CTX_new_ex(libctx);  | 
1783  | 0  |     if (bnctx == NULL)  | 
1784  | 0  |         goto err;  | 
1785  | 0  |     BN_CTX_start(bnctx);  | 
1786  |  | 
  | 
1787  | 0  |     if (!ossl_ec_group_todata(  | 
1788  | 0  |             group, tmpl, NULL, libctx, propq, bnctx, &gen_buf))  | 
1789  | 0  |         goto err;  | 
1790  |  |  | 
1791  | 0  |     params = OSSL_PARAM_BLD_to_param(tmpl);  | 
1792  |  | 
  | 
1793  | 0  |  err:  | 
1794  | 0  |     OSSL_PARAM_BLD_free(tmpl);  | 
1795  | 0  |     OPENSSL_free(gen_buf);  | 
1796  | 0  |     BN_CTX_end(bnctx);  | 
1797  | 0  |     BN_CTX_free(new_bnctx);  | 
1798  | 0  |     return params;  | 
1799  | 0  | }  |