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