/src/openssl30/crypto/ec/ecp_smpl.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2001-2021 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 |  |  * ECDSA low level APIs are deprecated for public use, but still ok for | 
| 13 |  |  * internal use. | 
| 14 |  |  */ | 
| 15 |  | #include "internal/deprecated.h" | 
| 16 |  |  | 
| 17 |  | #include <openssl/err.h> | 
| 18 |  | #include <openssl/symhacks.h> | 
| 19 |  |  | 
| 20 |  | #include "ec_local.h" | 
| 21 |  |  | 
| 22 |  | const EC_METHOD *EC_GFp_simple_method(void) | 
| 23 | 0 | { | 
| 24 | 0 |     static const EC_METHOD ret = { | 
| 25 | 0 |         EC_FLAGS_DEFAULT_OCT, | 
| 26 | 0 |         NID_X9_62_prime_field, | 
| 27 | 0 |         ossl_ec_GFp_simple_group_init, | 
| 28 | 0 |         ossl_ec_GFp_simple_group_finish, | 
| 29 | 0 |         ossl_ec_GFp_simple_group_clear_finish, | 
| 30 | 0 |         ossl_ec_GFp_simple_group_copy, | 
| 31 | 0 |         ossl_ec_GFp_simple_group_set_curve, | 
| 32 | 0 |         ossl_ec_GFp_simple_group_get_curve, | 
| 33 | 0 |         ossl_ec_GFp_simple_group_get_degree, | 
| 34 | 0 |         ossl_ec_group_simple_order_bits, | 
| 35 | 0 |         ossl_ec_GFp_simple_group_check_discriminant, | 
| 36 | 0 |         ossl_ec_GFp_simple_point_init, | 
| 37 | 0 |         ossl_ec_GFp_simple_point_finish, | 
| 38 | 0 |         ossl_ec_GFp_simple_point_clear_finish, | 
| 39 | 0 |         ossl_ec_GFp_simple_point_copy, | 
| 40 | 0 |         ossl_ec_GFp_simple_point_set_to_infinity, | 
| 41 | 0 |         ossl_ec_GFp_simple_point_set_affine_coordinates, | 
| 42 | 0 |         ossl_ec_GFp_simple_point_get_affine_coordinates, | 
| 43 | 0 |         0, 0, 0, | 
| 44 | 0 |         ossl_ec_GFp_simple_add, | 
| 45 | 0 |         ossl_ec_GFp_simple_dbl, | 
| 46 | 0 |         ossl_ec_GFp_simple_invert, | 
| 47 | 0 |         ossl_ec_GFp_simple_is_at_infinity, | 
| 48 | 0 |         ossl_ec_GFp_simple_is_on_curve, | 
| 49 | 0 |         ossl_ec_GFp_simple_cmp, | 
| 50 | 0 |         ossl_ec_GFp_simple_make_affine, | 
| 51 | 0 |         ossl_ec_GFp_simple_points_make_affine, | 
| 52 | 0 |         0 /* mul */ , | 
| 53 | 0 |         0 /* precompute_mult */ , | 
| 54 | 0 |         0 /* have_precompute_mult */ , | 
| 55 | 0 |         ossl_ec_GFp_simple_field_mul, | 
| 56 | 0 |         ossl_ec_GFp_simple_field_sqr, | 
| 57 | 0 |         0 /* field_div */ , | 
| 58 | 0 |         ossl_ec_GFp_simple_field_inv, | 
| 59 | 0 |         0 /* field_encode */ , | 
| 60 | 0 |         0 /* field_decode */ , | 
| 61 | 0 |         0,                      /* field_set_to_one */ | 
| 62 | 0 |         ossl_ec_key_simple_priv2oct, | 
| 63 | 0 |         ossl_ec_key_simple_oct2priv, | 
| 64 | 0 |         0, /* set private */ | 
| 65 | 0 |         ossl_ec_key_simple_generate_key, | 
| 66 | 0 |         ossl_ec_key_simple_check_key, | 
| 67 | 0 |         ossl_ec_key_simple_generate_public_key, | 
| 68 | 0 |         0, /* keycopy */ | 
| 69 | 0 |         0, /* keyfinish */ | 
| 70 | 0 |         ossl_ecdh_simple_compute_key, | 
| 71 | 0 |         ossl_ecdsa_simple_sign_setup, | 
| 72 | 0 |         ossl_ecdsa_simple_sign_sig, | 
| 73 | 0 |         ossl_ecdsa_simple_verify_sig, | 
| 74 | 0 |         0, /* field_inverse_mod_ord */ | 
| 75 | 0 |         ossl_ec_GFp_simple_blind_coordinates, | 
| 76 | 0 |         ossl_ec_GFp_simple_ladder_pre, | 
| 77 | 0 |         ossl_ec_GFp_simple_ladder_step, | 
| 78 | 0 |         ossl_ec_GFp_simple_ladder_post | 
| 79 | 0 |     }; | 
| 80 |  | 
 | 
| 81 | 0 |     return &ret; | 
| 82 | 0 | } | 
| 83 |  |  | 
| 84 |  | /* | 
| 85 |  |  * Most method functions in this file are designed to work with | 
| 86 |  |  * non-trivial representations of field elements if necessary | 
| 87 |  |  * (see ecp_mont.c): while standard modular addition and subtraction | 
| 88 |  |  * are used, the field_mul and field_sqr methods will be used for | 
| 89 |  |  * multiplication, and field_encode and field_decode (if defined) | 
| 90 |  |  * will be used for converting between representations. | 
| 91 |  |  * | 
| 92 |  |  * Functions ec_GFp_simple_points_make_affine() and | 
| 93 |  |  * ec_GFp_simple_point_get_affine_coordinates() specifically assume | 
| 94 |  |  * that if a non-trivial representation is used, it is a Montgomery | 
| 95 |  |  * representation (i.e. 'encoding' means multiplying by some factor R). | 
| 96 |  |  */ | 
| 97 |  |  | 
| 98 |  | int ossl_ec_GFp_simple_group_init(EC_GROUP *group) | 
| 99 | 248k | { | 
| 100 | 248k |     group->field = BN_new(); | 
| 101 | 248k |     group->a = BN_new(); | 
| 102 | 248k |     group->b = BN_new(); | 
| 103 | 248k |     if (group->field == NULL || group->a == NULL || group->b == NULL) { | 
| 104 | 0 |         BN_free(group->field); | 
| 105 | 0 |         BN_free(group->a); | 
| 106 | 0 |         BN_free(group->b); | 
| 107 | 0 |         return 0; | 
| 108 | 0 |     } | 
| 109 | 248k |     group->a_is_minus3 = 0; | 
| 110 | 248k |     return 1; | 
| 111 | 248k | } | 
| 112 |  |  | 
| 113 |  | void ossl_ec_GFp_simple_group_finish(EC_GROUP *group) | 
| 114 | 248k | { | 
| 115 | 248k |     BN_free(group->field); | 
| 116 | 248k |     BN_free(group->a); | 
| 117 | 248k |     BN_free(group->b); | 
| 118 | 248k | } | 
| 119 |  |  | 
| 120 |  | void ossl_ec_GFp_simple_group_clear_finish(EC_GROUP *group) | 
| 121 | 0 | { | 
| 122 | 0 |     BN_clear_free(group->field); | 
| 123 | 0 |     BN_clear_free(group->a); | 
| 124 | 0 |     BN_clear_free(group->b); | 
| 125 | 0 | } | 
| 126 |  |  | 
| 127 |  | int ossl_ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src) | 
| 128 | 116k | { | 
| 129 | 116k |     if (!BN_copy(dest->field, src->field)) | 
| 130 | 0 |         return 0; | 
| 131 | 116k |     if (!BN_copy(dest->a, src->a)) | 
| 132 | 0 |         return 0; | 
| 133 | 116k |     if (!BN_copy(dest->b, src->b)) | 
| 134 | 0 |         return 0; | 
| 135 |  |  | 
| 136 | 116k |     dest->a_is_minus3 = src->a_is_minus3; | 
| 137 |  |  | 
| 138 | 116k |     return 1; | 
| 139 | 116k | } | 
| 140 |  |  | 
| 141 |  | int ossl_ec_GFp_simple_group_set_curve(EC_GROUP *group, | 
| 142 |  |                                        const BIGNUM *p, const BIGNUM *a, | 
| 143 |  |                                        const BIGNUM *b, BN_CTX *ctx) | 
| 144 | 64.3k | { | 
| 145 | 64.3k |     int ret = 0; | 
| 146 | 64.3k |     BN_CTX *new_ctx = NULL; | 
| 147 | 64.3k |     BIGNUM *tmp_a; | 
| 148 |  |  | 
| 149 |  |     /* p must be a prime > 3 */ | 
| 150 | 64.3k |     if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) { | 
| 151 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); | 
| 152 | 0 |         return 0; | 
| 153 | 0 |     } | 
| 154 |  |  | 
| 155 | 64.3k |     if (ctx == NULL) { | 
| 156 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 157 | 0 |         if (ctx == NULL) | 
| 158 | 0 |             return 0; | 
| 159 | 0 |     } | 
| 160 |  |  | 
| 161 | 64.3k |     BN_CTX_start(ctx); | 
| 162 | 64.3k |     tmp_a = BN_CTX_get(ctx); | 
| 163 | 64.3k |     if (tmp_a == NULL) | 
| 164 | 0 |         goto err; | 
| 165 |  |  | 
| 166 |  |     /* group->field */ | 
| 167 | 64.3k |     if (!BN_copy(group->field, p)) | 
| 168 | 0 |         goto err; | 
| 169 | 64.3k |     BN_set_negative(group->field, 0); | 
| 170 |  |  | 
| 171 |  |     /* group->a */ | 
| 172 | 64.3k |     if (!BN_nnmod(tmp_a, a, p, ctx)) | 
| 173 | 0 |         goto err; | 
| 174 | 64.3k |     if (group->meth->field_encode) { | 
| 175 | 53.2k |         if (!group->meth->field_encode(group, group->a, tmp_a, ctx)) | 
| 176 | 0 |             goto err; | 
| 177 | 53.2k |     } else if (!BN_copy(group->a, tmp_a)) | 
| 178 | 0 |         goto err; | 
| 179 |  |  | 
| 180 |  |     /* group->b */ | 
| 181 | 64.3k |     if (!BN_nnmod(group->b, b, p, ctx)) | 
| 182 | 0 |         goto err; | 
| 183 | 64.3k |     if (group->meth->field_encode) | 
| 184 | 53.2k |         if (!group->meth->field_encode(group, group->b, group->b, ctx)) | 
| 185 | 0 |             goto err; | 
| 186 |  |  | 
| 187 |  |     /* group->a_is_minus3 */ | 
| 188 | 64.3k |     if (!BN_add_word(tmp_a, 3)) | 
| 189 | 0 |         goto err; | 
| 190 | 64.3k |     group->a_is_minus3 = (0 == BN_cmp(tmp_a, group->field)); | 
| 191 |  |  | 
| 192 | 64.3k |     ret = 1; | 
| 193 |  |  | 
| 194 | 64.3k |  err: | 
| 195 | 64.3k |     BN_CTX_end(ctx); | 
| 196 | 64.3k |     BN_CTX_free(new_ctx); | 
| 197 | 64.3k |     return ret; | 
| 198 | 64.3k | } | 
| 199 |  |  | 
| 200 |  | int ossl_ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, | 
| 201 |  |                                        BIGNUM *a, BIGNUM *b, BN_CTX *ctx) | 
| 202 | 23.2k | { | 
| 203 | 23.2k |     int ret = 0; | 
| 204 | 23.2k |     BN_CTX *new_ctx = NULL; | 
| 205 |  |  | 
| 206 | 23.2k |     if (p != NULL) { | 
| 207 | 23.0k |         if (!BN_copy(p, group->field)) | 
| 208 | 0 |             return 0; | 
| 209 | 23.0k |     } | 
| 210 |  |  | 
| 211 | 23.2k |     if (a != NULL || b != NULL) { | 
| 212 | 23.0k |         if (group->meth->field_decode) { | 
| 213 | 23.0k |             if (ctx == NULL) { | 
| 214 | 179 |                 ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 215 | 179 |                 if (ctx == NULL) | 
| 216 | 0 |                     return 0; | 
| 217 | 179 |             } | 
| 218 | 23.0k |             if (a != NULL) { | 
| 219 | 23.0k |                 if (!group->meth->field_decode(group, a, group->a, ctx)) | 
| 220 | 0 |                     goto err; | 
| 221 | 23.0k |             } | 
| 222 | 23.0k |             if (b != NULL) { | 
| 223 | 23.0k |                 if (!group->meth->field_decode(group, b, group->b, ctx)) | 
| 224 | 0 |                     goto err; | 
| 225 | 23.0k |             } | 
| 226 | 23.0k |         } else { | 
| 227 | 24 |             if (a != NULL) { | 
| 228 | 24 |                 if (!BN_copy(a, group->a)) | 
| 229 | 0 |                     goto err; | 
| 230 | 24 |             } | 
| 231 | 24 |             if (b != NULL) { | 
| 232 | 24 |                 if (!BN_copy(b, group->b)) | 
| 233 | 0 |                     goto err; | 
| 234 | 24 |             } | 
| 235 | 24 |         } | 
| 236 | 23.0k |     } | 
| 237 |  |  | 
| 238 | 23.2k |     ret = 1; | 
| 239 |  |  | 
| 240 | 23.2k |  err: | 
| 241 | 23.2k |     BN_CTX_free(new_ctx); | 
| 242 | 23.2k |     return ret; | 
| 243 | 23.2k | } | 
| 244 |  |  | 
| 245 |  | int ossl_ec_GFp_simple_group_get_degree(const EC_GROUP *group) | 
| 246 | 2.34k | { | 
| 247 | 2.34k |     return BN_num_bits(group->field); | 
| 248 | 2.34k | } | 
| 249 |  |  | 
| 250 |  | int ossl_ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, | 
| 251 |  |                                                 BN_CTX *ctx) | 
| 252 |  | { | 
| 253 |  |     int ret = 0; | 
| 254 |  |     BIGNUM *a, *b, *order, *tmp_1, *tmp_2; | 
| 255 |  |     const BIGNUM *p = group->field; | 
| 256 |  |     BN_CTX *new_ctx = NULL; | 
| 257 |  |  | 
| 258 |  |     if (ctx == NULL) { | 
| 259 |  |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 260 |  |         if (ctx == NULL) { | 
| 261 |  |             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 262 |  |             goto err; | 
| 263 |  |         } | 
| 264 |  |     } | 
| 265 |  |     BN_CTX_start(ctx); | 
| 266 |  |     a = BN_CTX_get(ctx); | 
| 267 |  |     b = BN_CTX_get(ctx); | 
| 268 |  |     tmp_1 = BN_CTX_get(ctx); | 
| 269 |  |     tmp_2 = BN_CTX_get(ctx); | 
| 270 |  |     order = BN_CTX_get(ctx); | 
| 271 |  |     if (order == NULL) | 
| 272 |  |         goto err; | 
| 273 |  |  | 
| 274 |  |     if (group->meth->field_decode) { | 
| 275 |  |         if (!group->meth->field_decode(group, a, group->a, ctx)) | 
| 276 |  |             goto err; | 
| 277 |  |         if (!group->meth->field_decode(group, b, group->b, ctx)) | 
| 278 |  |             goto err; | 
| 279 |  |     } else { | 
| 280 |  |         if (!BN_copy(a, group->a)) | 
| 281 |  |             goto err; | 
| 282 |  |         if (!BN_copy(b, group->b)) | 
| 283 |  |             goto err; | 
| 284 |  |     } | 
| 285 |  |  | 
| 286 |  |     /*- | 
| 287 |  |      * check the discriminant: | 
| 288 |  |      * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p) | 
| 289 |  |      * 0 =< a, b < p | 
| 290 |  |      */ | 
| 291 |  |     if (BN_is_zero(a)) { | 
| 292 |  |         if (BN_is_zero(b)) | 
| 293 |  |             goto err; | 
| 294 |  |     } else if (!BN_is_zero(b)) { | 
| 295 |  |         if (!BN_mod_sqr(tmp_1, a, p, ctx)) | 
| 296 |  |             goto err; | 
| 297 |  |         if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) | 
| 298 |  |             goto err; | 
| 299 |  |         if (!BN_lshift(tmp_1, tmp_2, 2)) | 
| 300 |  |             goto err; | 
| 301 |  |         /* tmp_1 = 4*a^3 */ | 
| 302 |  |  | 
| 303 |  |         if (!BN_mod_sqr(tmp_2, b, p, ctx)) | 
| 304 |  |             goto err; | 
| 305 |  |         if (!BN_mul_word(tmp_2, 27)) | 
| 306 |  |             goto err; | 
| 307 |  |         /* tmp_2 = 27*b^2 */ | 
| 308 |  |  | 
| 309 |  |         if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) | 
| 310 |  |             goto err; | 
| 311 |  |         if (BN_is_zero(a)) | 
| 312 |  |             goto err; | 
| 313 |  |     } | 
| 314 |  |     ret = 1; | 
| 315 |  |  | 
| 316 |  |  err: | 
| 317 |  |     BN_CTX_end(ctx); | 
| 318 |  |     BN_CTX_free(new_ctx); | 
| 319 |  |     return ret; | 
| 320 |  | } | 
| 321 |  |  | 
| 322 |  | int ossl_ec_GFp_simple_point_init(EC_POINT *point) | 
| 323 | 535k | { | 
| 324 | 535k |     point->X = BN_new(); | 
| 325 | 535k |     point->Y = BN_new(); | 
| 326 | 535k |     point->Z = BN_new(); | 
| 327 | 535k |     point->Z_is_one = 0; | 
| 328 |  |  | 
| 329 | 535k |     if (point->X == NULL || point->Y == NULL || point->Z == NULL) { | 
| 330 | 0 |         BN_free(point->X); | 
| 331 | 0 |         BN_free(point->Y); | 
| 332 | 0 |         BN_free(point->Z); | 
| 333 | 0 |         return 0; | 
| 334 | 0 |     } | 
| 335 | 535k |     return 1; | 
| 336 | 535k | } | 
| 337 |  |  | 
| 338 |  | void ossl_ec_GFp_simple_point_finish(EC_POINT *point) | 
| 339 | 527k | { | 
| 340 | 527k |     BN_free(point->X); | 
| 341 | 527k |     BN_free(point->Y); | 
| 342 | 527k |     BN_free(point->Z); | 
| 343 | 527k | } | 
| 344 |  |  | 
| 345 |  | void ossl_ec_GFp_simple_point_clear_finish(EC_POINT *point) | 
| 346 | 8.15k | { | 
| 347 | 8.15k |     BN_clear_free(point->X); | 
| 348 | 8.15k |     BN_clear_free(point->Y); | 
| 349 | 8.15k |     BN_clear_free(point->Z); | 
| 350 | 8.15k |     point->Z_is_one = 0; | 
| 351 | 8.15k | } | 
| 352 |  |  | 
| 353 |  | int ossl_ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src) | 
| 354 | 277k | { | 
| 355 | 277k |     if (!BN_copy(dest->X, src->X)) | 
| 356 | 0 |         return 0; | 
| 357 | 277k |     if (!BN_copy(dest->Y, src->Y)) | 
| 358 | 0 |         return 0; | 
| 359 | 277k |     if (!BN_copy(dest->Z, src->Z)) | 
| 360 | 0 |         return 0; | 
| 361 | 277k |     dest->Z_is_one = src->Z_is_one; | 
| 362 | 277k |     dest->curve_name = src->curve_name; | 
| 363 |  |  | 
| 364 | 277k |     return 1; | 
| 365 | 277k | } | 
| 366 |  |  | 
| 367 |  | int ossl_ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, | 
| 368 |  |                                              EC_POINT *point) | 
| 369 | 3.68k | { | 
| 370 | 3.68k |     point->Z_is_one = 0; | 
| 371 | 3.68k |     BN_zero(point->Z); | 
| 372 | 3.68k |     return 1; | 
| 373 | 3.68k | } | 
| 374 |  |  | 
| 375 |  | int ossl_ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, | 
| 376 |  |                                                        EC_POINT *point, | 
| 377 |  |                                                        const BIGNUM *x, | 
| 378 |  |                                                        const BIGNUM *y, | 
| 379 |  |                                                        const BIGNUM *z, | 
| 380 |  |                                                        BN_CTX *ctx) | 
| 381 | 217k | { | 
| 382 | 217k |     BN_CTX *new_ctx = NULL; | 
| 383 | 217k |     int ret = 0; | 
| 384 |  |  | 
| 385 | 217k |     if (ctx == NULL) { | 
| 386 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 387 | 0 |         if (ctx == NULL) | 
| 388 | 0 |             return 0; | 
| 389 | 0 |     } | 
| 390 |  |  | 
| 391 | 217k |     if (x != NULL) { | 
| 392 | 217k |         if (!BN_nnmod(point->X, x, group->field, ctx)) | 
| 393 | 0 |             goto err; | 
| 394 | 217k |         if (group->meth->field_encode) { | 
| 395 | 166k |             if (!group->meth->field_encode(group, point->X, point->X, ctx)) | 
| 396 | 0 |                 goto err; | 
| 397 | 166k |         } | 
| 398 | 217k |     } | 
| 399 |  |  | 
| 400 | 217k |     if (y != NULL) { | 
| 401 | 217k |         if (!BN_nnmod(point->Y, y, group->field, ctx)) | 
| 402 | 0 |             goto err; | 
| 403 | 217k |         if (group->meth->field_encode) { | 
| 404 | 166k |             if (!group->meth->field_encode(group, point->Y, point->Y, ctx)) | 
| 405 | 0 |                 goto err; | 
| 406 | 166k |         } | 
| 407 | 217k |     } | 
| 408 |  |  | 
| 409 | 217k |     if (z != NULL) { | 
| 410 | 217k |         int Z_is_one; | 
| 411 |  |  | 
| 412 | 217k |         if (!BN_nnmod(point->Z, z, group->field, ctx)) | 
| 413 | 0 |             goto err; | 
| 414 | 217k |         Z_is_one = BN_is_one(point->Z); | 
| 415 | 217k |         if (group->meth->field_encode) { | 
| 416 | 166k |             if (Z_is_one && (group->meth->field_set_to_one != 0)) { | 
| 417 | 166k |                 if (!group->meth->field_set_to_one(group, point->Z, ctx)) | 
| 418 | 0 |                     goto err; | 
| 419 | 166k |             } else { | 
| 420 | 0 |                 if (!group-> | 
| 421 | 0 |                     meth->field_encode(group, point->Z, point->Z, ctx)) | 
| 422 | 0 |                     goto err; | 
| 423 | 0 |             } | 
| 424 | 166k |         } | 
| 425 | 217k |         point->Z_is_one = Z_is_one; | 
| 426 | 217k |     } | 
| 427 |  |  | 
| 428 | 217k |     ret = 1; | 
| 429 |  |  | 
| 430 | 217k |  err: | 
| 431 | 217k |     BN_CTX_free(new_ctx); | 
| 432 | 217k |     return ret; | 
| 433 | 217k | } | 
| 434 |  |  | 
| 435 |  | int ossl_ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, | 
| 436 |  |                                                        const EC_POINT *point, | 
| 437 |  |                                                        BIGNUM *x, BIGNUM *y, | 
| 438 |  |                                                        BIGNUM *z, BN_CTX *ctx) | 
| 439 | 0 | { | 
| 440 | 0 |     BN_CTX *new_ctx = NULL; | 
| 441 | 0 |     int ret = 0; | 
| 442 |  | 
 | 
| 443 | 0 |     if (group->meth->field_decode != 0) { | 
| 444 | 0 |         if (ctx == NULL) { | 
| 445 | 0 |             ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 446 | 0 |             if (ctx == NULL) | 
| 447 | 0 |                 return 0; | 
| 448 | 0 |         } | 
| 449 |  |  | 
| 450 | 0 |         if (x != NULL) { | 
| 451 | 0 |             if (!group->meth->field_decode(group, x, point->X, ctx)) | 
| 452 | 0 |                 goto err; | 
| 453 | 0 |         } | 
| 454 | 0 |         if (y != NULL) { | 
| 455 | 0 |             if (!group->meth->field_decode(group, y, point->Y, ctx)) | 
| 456 | 0 |                 goto err; | 
| 457 | 0 |         } | 
| 458 | 0 |         if (z != NULL) { | 
| 459 | 0 |             if (!group->meth->field_decode(group, z, point->Z, ctx)) | 
| 460 | 0 |                 goto err; | 
| 461 | 0 |         } | 
| 462 | 0 |     } else { | 
| 463 | 0 |         if (x != NULL) { | 
| 464 | 0 |             if (!BN_copy(x, point->X)) | 
| 465 | 0 |                 goto err; | 
| 466 | 0 |         } | 
| 467 | 0 |         if (y != NULL) { | 
| 468 | 0 |             if (!BN_copy(y, point->Y)) | 
| 469 | 0 |                 goto err; | 
| 470 | 0 |         } | 
| 471 | 0 |         if (z != NULL) { | 
| 472 | 0 |             if (!BN_copy(z, point->Z)) | 
| 473 | 0 |                 goto err; | 
| 474 | 0 |         } | 
| 475 | 0 |     } | 
| 476 |  |  | 
| 477 | 0 |     ret = 1; | 
| 478 |  | 
 | 
| 479 | 0 |  err: | 
| 480 | 0 |     BN_CTX_free(new_ctx); | 
| 481 | 0 |     return ret; | 
| 482 | 0 | } | 
| 483 |  |  | 
| 484 |  | int ossl_ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, | 
| 485 |  |                                                     EC_POINT *point, | 
| 486 |  |                                                     const BIGNUM *x, | 
| 487 |  |                                                     const BIGNUM *y, BN_CTX *ctx) | 
| 488 | 215k | { | 
| 489 | 215k |     if (x == NULL || y == NULL) { | 
| 490 |  |         /* | 
| 491 |  |          * unlike for projective coordinates, we do not tolerate this | 
| 492 |  |          */ | 
| 493 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); | 
| 494 | 0 |         return 0; | 
| 495 | 0 |     } | 
| 496 |  |  | 
| 497 | 215k |     return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, | 
| 498 | 215k |                                                     BN_value_one(), ctx); | 
| 499 | 215k | } | 
| 500 |  |  | 
| 501 |  | int ossl_ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, | 
| 502 |  |                                                     const EC_POINT *point, | 
| 503 |  |                                                     BIGNUM *x, BIGNUM *y, | 
| 504 |  |                                                     BN_CTX *ctx) | 
| 505 | 3.52k | { | 
| 506 | 3.52k |     BN_CTX *new_ctx = NULL; | 
| 507 | 3.52k |     BIGNUM *Z, *Z_1, *Z_2, *Z_3; | 
| 508 | 3.52k |     const BIGNUM *Z_; | 
| 509 | 3.52k |     int ret = 0; | 
| 510 |  |  | 
| 511 | 3.52k |     if (EC_POINT_is_at_infinity(group, point)) { | 
| 512 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_POINT_AT_INFINITY); | 
| 513 | 0 |         return 0; | 
| 514 | 0 |     } | 
| 515 |  |  | 
| 516 | 3.52k |     if (ctx == NULL) { | 
| 517 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 518 | 0 |         if (ctx == NULL) | 
| 519 | 0 |             return 0; | 
| 520 | 0 |     } | 
| 521 |  |  | 
| 522 | 3.52k |     BN_CTX_start(ctx); | 
| 523 | 3.52k |     Z = BN_CTX_get(ctx); | 
| 524 | 3.52k |     Z_1 = BN_CTX_get(ctx); | 
| 525 | 3.52k |     Z_2 = BN_CTX_get(ctx); | 
| 526 | 3.52k |     Z_3 = BN_CTX_get(ctx); | 
| 527 | 3.52k |     if (Z_3 == NULL) | 
| 528 | 0 |         goto err; | 
| 529 |  |  | 
| 530 |  |     /* transform  (X, Y, Z)  into  (x, y) := (X/Z^2, Y/Z^3) */ | 
| 531 |  |  | 
| 532 | 3.52k |     if (group->meth->field_decode) { | 
| 533 | 3.52k |         if (!group->meth->field_decode(group, Z, point->Z, ctx)) | 
| 534 | 0 |             goto err; | 
| 535 | 3.52k |         Z_ = Z; | 
| 536 | 3.52k |     } else { | 
| 537 | 0 |         Z_ = point->Z; | 
| 538 | 0 |     } | 
| 539 |  |  | 
| 540 | 3.52k |     if (BN_is_one(Z_)) { | 
| 541 | 3.35k |         if (group->meth->field_decode) { | 
| 542 | 3.35k |             if (x != NULL) { | 
| 543 | 3.35k |                 if (!group->meth->field_decode(group, x, point->X, ctx)) | 
| 544 | 0 |                     goto err; | 
| 545 | 3.35k |             } | 
| 546 | 3.35k |             if (y != NULL) { | 
| 547 | 3.33k |                 if (!group->meth->field_decode(group, y, point->Y, ctx)) | 
| 548 | 0 |                     goto err; | 
| 549 | 3.33k |             } | 
| 550 | 3.35k |         } else { | 
| 551 | 0 |             if (x != NULL) { | 
| 552 | 0 |                 if (!BN_copy(x, point->X)) | 
| 553 | 0 |                     goto err; | 
| 554 | 0 |             } | 
| 555 | 0 |             if (y != NULL) { | 
| 556 | 0 |                 if (!BN_copy(y, point->Y)) | 
| 557 | 0 |                     goto err; | 
| 558 | 0 |             } | 
| 559 | 0 |         } | 
| 560 | 3.35k |     } else { | 
| 561 | 173 |         if (!group->meth->field_inv(group, Z_1, Z_, ctx)) { | 
| 562 | 0 |             ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
| 563 | 0 |             goto err; | 
| 564 | 0 |         } | 
| 565 |  |  | 
| 566 | 173 |         if (group->meth->field_encode == 0) { | 
| 567 |  |             /* field_sqr works on standard representation */ | 
| 568 | 0 |             if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) | 
| 569 | 0 |                 goto err; | 
| 570 | 173 |         } else { | 
| 571 | 173 |             if (!BN_mod_sqr(Z_2, Z_1, group->field, ctx)) | 
| 572 | 0 |                 goto err; | 
| 573 | 173 |         } | 
| 574 |  |  | 
| 575 | 173 |         if (x != NULL) { | 
| 576 |  |             /* | 
| 577 |  |              * in the Montgomery case, field_mul will cancel out Montgomery | 
| 578 |  |              * factor in X: | 
| 579 |  |              */ | 
| 580 | 173 |             if (!group->meth->field_mul(group, x, point->X, Z_2, ctx)) | 
| 581 | 0 |                 goto err; | 
| 582 | 173 |         } | 
| 583 |  |  | 
| 584 | 173 |         if (y != NULL) { | 
| 585 | 76 |             if (group->meth->field_encode == 0) { | 
| 586 |  |                 /* | 
| 587 |  |                  * field_mul works on standard representation | 
| 588 |  |                  */ | 
| 589 | 0 |                 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) | 
| 590 | 0 |                     goto err; | 
| 591 | 76 |             } else { | 
| 592 | 76 |                 if (!BN_mod_mul(Z_3, Z_2, Z_1, group->field, ctx)) | 
| 593 | 0 |                     goto err; | 
| 594 | 76 |             } | 
| 595 |  |  | 
| 596 |  |             /* | 
| 597 |  |              * in the Montgomery case, field_mul will cancel out Montgomery | 
| 598 |  |              * factor in Y: | 
| 599 |  |              */ | 
| 600 | 76 |             if (!group->meth->field_mul(group, y, point->Y, Z_3, ctx)) | 
| 601 | 0 |                 goto err; | 
| 602 | 76 |         } | 
| 603 | 173 |     } | 
| 604 |  |  | 
| 605 | 3.52k |     ret = 1; | 
| 606 |  |  | 
| 607 | 3.52k |  err: | 
| 608 | 3.52k |     BN_CTX_end(ctx); | 
| 609 | 3.52k |     BN_CTX_free(new_ctx); | 
| 610 | 3.52k |     return ret; | 
| 611 | 3.52k | } | 
| 612 |  |  | 
| 613 |  | int ossl_ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | 
| 614 |  |                            const EC_POINT *b, BN_CTX *ctx) | 
| 615 | 42.1k | { | 
| 616 | 42.1k |     int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, | 
| 617 | 42.1k |                       const BIGNUM *, BN_CTX *); | 
| 618 | 42.1k |     int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | 
| 619 | 42.1k |     const BIGNUM *p; | 
| 620 | 42.1k |     BN_CTX *new_ctx = NULL; | 
| 621 | 42.1k |     BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6; | 
| 622 | 42.1k |     int ret = 0; | 
| 623 |  |  | 
| 624 | 42.1k |     if (a == b) | 
| 625 | 0 |         return EC_POINT_dbl(group, r, a, ctx); | 
| 626 | 42.1k |     if (EC_POINT_is_at_infinity(group, a)) | 
| 627 | 3.11k |         return EC_POINT_copy(r, b); | 
| 628 | 39.0k |     if (EC_POINT_is_at_infinity(group, b)) | 
| 629 | 453 |         return EC_POINT_copy(r, a); | 
| 630 |  |  | 
| 631 | 38.5k |     field_mul = group->meth->field_mul; | 
| 632 | 38.5k |     field_sqr = group->meth->field_sqr; | 
| 633 | 38.5k |     p = group->field; | 
| 634 |  |  | 
| 635 | 38.5k |     if (ctx == NULL) { | 
| 636 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 637 | 0 |         if (ctx == NULL) | 
| 638 | 0 |             return 0; | 
| 639 | 0 |     } | 
| 640 |  |  | 
| 641 | 38.5k |     BN_CTX_start(ctx); | 
| 642 | 38.5k |     n0 = BN_CTX_get(ctx); | 
| 643 | 38.5k |     n1 = BN_CTX_get(ctx); | 
| 644 | 38.5k |     n2 = BN_CTX_get(ctx); | 
| 645 | 38.5k |     n3 = BN_CTX_get(ctx); | 
| 646 | 38.5k |     n4 = BN_CTX_get(ctx); | 
| 647 | 38.5k |     n5 = BN_CTX_get(ctx); | 
| 648 | 38.5k |     n6 = BN_CTX_get(ctx); | 
| 649 | 38.5k |     if (n6 == NULL) | 
| 650 | 0 |         goto end; | 
| 651 |  |  | 
| 652 |  |     /* | 
| 653 |  |      * Note that in this function we must not read components of 'a' or 'b' | 
| 654 |  |      * once we have written the corresponding components of 'r'. ('r' might | 
| 655 |  |      * be one of 'a' or 'b'.) | 
| 656 |  |      */ | 
| 657 |  |  | 
| 658 |  |     /* n1, n2 */ | 
| 659 | 38.5k |     if (b->Z_is_one) { | 
| 660 | 34.2k |         if (!BN_copy(n1, a->X)) | 
| 661 | 0 |             goto end; | 
| 662 | 34.2k |         if (!BN_copy(n2, a->Y)) | 
| 663 | 0 |             goto end; | 
| 664 |  |         /* n1 = X_a */ | 
| 665 |  |         /* n2 = Y_a */ | 
| 666 | 34.2k |     } else { | 
| 667 | 4.30k |         if (!field_sqr(group, n0, b->Z, ctx)) | 
| 668 | 0 |             goto end; | 
| 669 | 4.30k |         if (!field_mul(group, n1, a->X, n0, ctx)) | 
| 670 | 0 |             goto end; | 
| 671 |  |         /* n1 = X_a * Z_b^2 */ | 
| 672 |  |  | 
| 673 | 4.30k |         if (!field_mul(group, n0, n0, b->Z, ctx)) | 
| 674 | 0 |             goto end; | 
| 675 | 4.30k |         if (!field_mul(group, n2, a->Y, n0, ctx)) | 
| 676 | 0 |             goto end; | 
| 677 |  |         /* n2 = Y_a * Z_b^3 */ | 
| 678 | 4.30k |     } | 
| 679 |  |  | 
| 680 |  |     /* n3, n4 */ | 
| 681 | 38.5k |     if (a->Z_is_one) { | 
| 682 | 967 |         if (!BN_copy(n3, b->X)) | 
| 683 | 0 |             goto end; | 
| 684 | 967 |         if (!BN_copy(n4, b->Y)) | 
| 685 | 0 |             goto end; | 
| 686 |  |         /* n3 = X_b */ | 
| 687 |  |         /* n4 = Y_b */ | 
| 688 | 37.6k |     } else { | 
| 689 | 37.6k |         if (!field_sqr(group, n0, a->Z, ctx)) | 
| 690 | 0 |             goto end; | 
| 691 | 37.6k |         if (!field_mul(group, n3, b->X, n0, ctx)) | 
| 692 | 0 |             goto end; | 
| 693 |  |         /* n3 = X_b * Z_a^2 */ | 
| 694 |  |  | 
| 695 | 37.6k |         if (!field_mul(group, n0, n0, a->Z, ctx)) | 
| 696 | 0 |             goto end; | 
| 697 | 37.6k |         if (!field_mul(group, n4, b->Y, n0, ctx)) | 
| 698 | 0 |             goto end; | 
| 699 |  |         /* n4 = Y_b * Z_a^3 */ | 
| 700 | 37.6k |     } | 
| 701 |  |  | 
| 702 |  |     /* n5, n6 */ | 
| 703 | 38.5k |     if (!BN_mod_sub_quick(n5, n1, n3, p)) | 
| 704 | 0 |         goto end; | 
| 705 | 38.5k |     if (!BN_mod_sub_quick(n6, n2, n4, p)) | 
| 706 | 0 |         goto end; | 
| 707 |  |     /* n5 = n1 - n3 */ | 
| 708 |  |     /* n6 = n2 - n4 */ | 
| 709 |  |  | 
| 710 | 38.5k |     if (BN_is_zero(n5)) { | 
| 711 | 601 |         if (BN_is_zero(n6)) { | 
| 712 |  |             /* a is the same point as b */ | 
| 713 | 1 |             BN_CTX_end(ctx); | 
| 714 | 1 |             ret = EC_POINT_dbl(group, r, a, ctx); | 
| 715 | 1 |             ctx = NULL; | 
| 716 | 1 |             goto end; | 
| 717 | 600 |         } else { | 
| 718 |  |             /* a is the inverse of b */ | 
| 719 | 600 |             BN_zero(r->Z); | 
| 720 | 600 |             r->Z_is_one = 0; | 
| 721 | 600 |             ret = 1; | 
| 722 | 600 |             goto end; | 
| 723 | 600 |         } | 
| 724 | 601 |     } | 
| 725 |  |  | 
| 726 |  |     /* 'n7', 'n8' */ | 
| 727 | 37.9k |     if (!BN_mod_add_quick(n1, n1, n3, p)) | 
| 728 | 0 |         goto end; | 
| 729 | 37.9k |     if (!BN_mod_add_quick(n2, n2, n4, p)) | 
| 730 | 0 |         goto end; | 
| 731 |  |     /* 'n7' = n1 + n3 */ | 
| 732 |  |     /* 'n8' = n2 + n4 */ | 
| 733 |  |  | 
| 734 |  |     /* Z_r */ | 
| 735 | 37.9k |     if (a->Z_is_one && b->Z_is_one) { | 
| 736 | 0 |         if (!BN_copy(r->Z, n5)) | 
| 737 | 0 |             goto end; | 
| 738 | 37.9k |     } else { | 
| 739 | 37.9k |         if (a->Z_is_one) { | 
| 740 | 967 |             if (!BN_copy(n0, b->Z)) | 
| 741 | 0 |                 goto end; | 
| 742 | 36.9k |         } else if (b->Z_is_one) { | 
| 743 | 33.6k |             if (!BN_copy(n0, a->Z)) | 
| 744 | 0 |                 goto end; | 
| 745 | 33.6k |         } else { | 
| 746 | 3.34k |             if (!field_mul(group, n0, a->Z, b->Z, ctx)) | 
| 747 | 0 |                 goto end; | 
| 748 | 3.34k |         } | 
| 749 | 37.9k |         if (!field_mul(group, r->Z, n0, n5, ctx)) | 
| 750 | 0 |             goto end; | 
| 751 | 37.9k |     } | 
| 752 | 37.9k |     r->Z_is_one = 0; | 
| 753 |  |     /* Z_r = Z_a * Z_b * n5 */ | 
| 754 |  |  | 
| 755 |  |     /* X_r */ | 
| 756 | 37.9k |     if (!field_sqr(group, n0, n6, ctx)) | 
| 757 | 0 |         goto end; | 
| 758 | 37.9k |     if (!field_sqr(group, n4, n5, ctx)) | 
| 759 | 0 |         goto end; | 
| 760 | 37.9k |     if (!field_mul(group, n3, n1, n4, ctx)) | 
| 761 | 0 |         goto end; | 
| 762 | 37.9k |     if (!BN_mod_sub_quick(r->X, n0, n3, p)) | 
| 763 | 0 |         goto end; | 
| 764 |  |     /* X_r = n6^2 - n5^2 * 'n7' */ | 
| 765 |  |  | 
| 766 |  |     /* 'n9' */ | 
| 767 | 37.9k |     if (!BN_mod_lshift1_quick(n0, r->X, p)) | 
| 768 | 0 |         goto end; | 
| 769 | 37.9k |     if (!BN_mod_sub_quick(n0, n3, n0, p)) | 
| 770 | 0 |         goto end; | 
| 771 |  |     /* n9 = n5^2 * 'n7' - 2 * X_r */ | 
| 772 |  |  | 
| 773 |  |     /* Y_r */ | 
| 774 | 37.9k |     if (!field_mul(group, n0, n0, n6, ctx)) | 
| 775 | 0 |         goto end; | 
| 776 | 37.9k |     if (!field_mul(group, n5, n4, n5, ctx)) | 
| 777 | 0 |         goto end;               /* now n5 is n5^3 */ | 
| 778 | 37.9k |     if (!field_mul(group, n1, n2, n5, ctx)) | 
| 779 | 0 |         goto end; | 
| 780 | 37.9k |     if (!BN_mod_sub_quick(n0, n0, n1, p)) | 
| 781 | 0 |         goto end; | 
| 782 | 37.9k |     if (BN_is_odd(n0)) | 
| 783 | 19.1k |         if (!BN_add(n0, n0, p)) | 
| 784 | 0 |             goto end; | 
| 785 |  |     /* now  0 <= n0 < 2*p,  and n0 is even */ | 
| 786 | 37.9k |     if (!BN_rshift1(r->Y, n0)) | 
| 787 | 0 |         goto end; | 
| 788 |  |     /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */ | 
| 789 |  |  | 
| 790 | 37.9k |     ret = 1; | 
| 791 |  |  | 
| 792 | 38.5k |  end: | 
| 793 | 38.5k |     BN_CTX_end(ctx); | 
| 794 | 38.5k |     BN_CTX_free(new_ctx); | 
| 795 | 38.5k |     return ret; | 
| 796 | 37.9k | } | 
| 797 |  |  | 
| 798 |  | int ossl_ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, | 
| 799 |  |                            BN_CTX *ctx) | 
| 800 | 267k | { | 
| 801 | 267k |     int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, | 
| 802 | 267k |                       const BIGNUM *, BN_CTX *); | 
| 803 | 267k |     int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | 
| 804 | 267k |     const BIGNUM *p; | 
| 805 | 267k |     BN_CTX *new_ctx = NULL; | 
| 806 | 267k |     BIGNUM *n0, *n1, *n2, *n3; | 
| 807 | 267k |     int ret = 0; | 
| 808 |  |  | 
| 809 | 267k |     if (EC_POINT_is_at_infinity(group, a)) { | 
| 810 | 25.3k |         BN_zero(r->Z); | 
| 811 | 25.3k |         r->Z_is_one = 0; | 
| 812 | 25.3k |         return 1; | 
| 813 | 25.3k |     } | 
| 814 |  |  | 
| 815 | 242k |     field_mul = group->meth->field_mul; | 
| 816 | 242k |     field_sqr = group->meth->field_sqr; | 
| 817 | 242k |     p = group->field; | 
| 818 |  |  | 
| 819 | 242k |     if (ctx == NULL) { | 
| 820 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 821 | 0 |         if (ctx == NULL) | 
| 822 | 0 |             return 0; | 
| 823 | 0 |     } | 
| 824 |  |  | 
| 825 | 242k |     BN_CTX_start(ctx); | 
| 826 | 242k |     n0 = BN_CTX_get(ctx); | 
| 827 | 242k |     n1 = BN_CTX_get(ctx); | 
| 828 | 242k |     n2 = BN_CTX_get(ctx); | 
| 829 | 242k |     n3 = BN_CTX_get(ctx); | 
| 830 | 242k |     if (n3 == NULL) | 
| 831 | 0 |         goto err; | 
| 832 |  |  | 
| 833 |  |     /* | 
| 834 |  |      * Note that in this function we must not read components of 'a' once we | 
| 835 |  |      * have written the corresponding components of 'r'. ('r' might the same | 
| 836 |  |      * as 'a'.) | 
| 837 |  |      */ | 
| 838 |  |  | 
| 839 |  |     /* n1 */ | 
| 840 | 242k |     if (a->Z_is_one) { | 
| 841 | 4.13k |         if (!field_sqr(group, n0, a->X, ctx)) | 
| 842 | 0 |             goto err; | 
| 843 | 4.13k |         if (!BN_mod_lshift1_quick(n1, n0, p)) | 
| 844 | 0 |             goto err; | 
| 845 | 4.13k |         if (!BN_mod_add_quick(n0, n0, n1, p)) | 
| 846 | 0 |             goto err; | 
| 847 | 4.13k |         if (!BN_mod_add_quick(n1, n0, group->a, p)) | 
| 848 | 0 |             goto err; | 
| 849 |  |         /* n1 = 3 * X_a^2 + a_curve */ | 
| 850 | 238k |     } else if (group->a_is_minus3) { | 
| 851 | 92.2k |         if (!field_sqr(group, n1, a->Z, ctx)) | 
| 852 | 0 |             goto err; | 
| 853 | 92.2k |         if (!BN_mod_add_quick(n0, a->X, n1, p)) | 
| 854 | 0 |             goto err; | 
| 855 | 92.2k |         if (!BN_mod_sub_quick(n2, a->X, n1, p)) | 
| 856 | 0 |             goto err; | 
| 857 | 92.2k |         if (!field_mul(group, n1, n0, n2, ctx)) | 
| 858 | 0 |             goto err; | 
| 859 | 92.2k |         if (!BN_mod_lshift1_quick(n0, n1, p)) | 
| 860 | 0 |             goto err; | 
| 861 | 92.2k |         if (!BN_mod_add_quick(n1, n0, n1, p)) | 
| 862 | 0 |             goto err; | 
| 863 |  |         /*- | 
| 864 |  |          * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2) | 
| 865 |  |          *    = 3 * X_a^2 - 3 * Z_a^4 | 
| 866 |  |          */ | 
| 867 | 146k |     } else { | 
| 868 | 146k |         if (!field_sqr(group, n0, a->X, ctx)) | 
| 869 | 0 |             goto err; | 
| 870 | 146k |         if (!BN_mod_lshift1_quick(n1, n0, p)) | 
| 871 | 0 |             goto err; | 
| 872 | 146k |         if (!BN_mod_add_quick(n0, n0, n1, p)) | 
| 873 | 0 |             goto err; | 
| 874 | 146k |         if (!field_sqr(group, n1, a->Z, ctx)) | 
| 875 | 0 |             goto err; | 
| 876 | 146k |         if (!field_sqr(group, n1, n1, ctx)) | 
| 877 | 0 |             goto err; | 
| 878 | 146k |         if (!field_mul(group, n1, n1, group->a, ctx)) | 
| 879 | 0 |             goto err; | 
| 880 | 146k |         if (!BN_mod_add_quick(n1, n1, n0, p)) | 
| 881 | 0 |             goto err; | 
| 882 |  |         /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */ | 
| 883 | 146k |     } | 
| 884 |  |  | 
| 885 |  |     /* Z_r */ | 
| 886 | 242k |     if (a->Z_is_one) { | 
| 887 | 4.13k |         if (!BN_copy(n0, a->Y)) | 
| 888 | 0 |             goto err; | 
| 889 | 238k |     } else { | 
| 890 | 238k |         if (!field_mul(group, n0, a->Y, a->Z, ctx)) | 
| 891 | 0 |             goto err; | 
| 892 | 238k |     } | 
| 893 | 242k |     if (!BN_mod_lshift1_quick(r->Z, n0, p)) | 
| 894 | 0 |         goto err; | 
| 895 | 242k |     r->Z_is_one = 0; | 
| 896 |  |     /* Z_r = 2 * Y_a * Z_a */ | 
| 897 |  |  | 
| 898 |  |     /* n2 */ | 
| 899 | 242k |     if (!field_sqr(group, n3, a->Y, ctx)) | 
| 900 | 0 |         goto err; | 
| 901 | 242k |     if (!field_mul(group, n2, a->X, n3, ctx)) | 
| 902 | 0 |         goto err; | 
| 903 | 242k |     if (!BN_mod_lshift_quick(n2, n2, 2, p)) | 
| 904 | 0 |         goto err; | 
| 905 |  |     /* n2 = 4 * X_a * Y_a^2 */ | 
| 906 |  |  | 
| 907 |  |     /* X_r */ | 
| 908 | 242k |     if (!BN_mod_lshift1_quick(n0, n2, p)) | 
| 909 | 0 |         goto err; | 
| 910 | 242k |     if (!field_sqr(group, r->X, n1, ctx)) | 
| 911 | 0 |         goto err; | 
| 912 | 242k |     if (!BN_mod_sub_quick(r->X, r->X, n0, p)) | 
| 913 | 0 |         goto err; | 
| 914 |  |     /* X_r = n1^2 - 2 * n2 */ | 
| 915 |  |  | 
| 916 |  |     /* n3 */ | 
| 917 | 242k |     if (!field_sqr(group, n0, n3, ctx)) | 
| 918 | 0 |         goto err; | 
| 919 | 242k |     if (!BN_mod_lshift_quick(n3, n0, 3, p)) | 
| 920 | 0 |         goto err; | 
| 921 |  |     /* n3 = 8 * Y_a^4 */ | 
| 922 |  |  | 
| 923 |  |     /* Y_r */ | 
| 924 | 242k |     if (!BN_mod_sub_quick(n0, n2, r->X, p)) | 
| 925 | 0 |         goto err; | 
| 926 | 242k |     if (!field_mul(group, n0, n1, n0, ctx)) | 
| 927 | 0 |         goto err; | 
| 928 | 242k |     if (!BN_mod_sub_quick(r->Y, n0, n3, p)) | 
| 929 | 0 |         goto err; | 
| 930 |  |     /* Y_r = n1 * (n2 - X_r) - n3 */ | 
| 931 |  |  | 
| 932 | 242k |     ret = 1; | 
| 933 |  |  | 
| 934 | 242k |  err: | 
| 935 | 242k |     BN_CTX_end(ctx); | 
| 936 | 242k |     BN_CTX_free(new_ctx); | 
| 937 | 242k |     return ret; | 
| 938 | 242k | } | 
| 939 |  |  | 
| 940 |  | int ossl_ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, | 
| 941 |  |                               BN_CTX *ctx) | 
| 942 | 18.5k | { | 
| 943 | 18.5k |     if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(point->Y)) | 
| 944 |  |         /* point is its own inverse */ | 
| 945 | 1.85k |         return 1; | 
| 946 |  |  | 
| 947 | 16.7k |     return BN_usub(point->Y, group->field, point->Y); | 
| 948 | 18.5k | } | 
| 949 |  |  | 
| 950 |  | int ossl_ec_GFp_simple_is_at_infinity(const EC_GROUP *group, | 
| 951 |  |                                       const EC_POINT *point) | 
| 952 | 772k | { | 
| 953 | 772k |     return BN_is_zero(point->Z); | 
| 954 | 772k | } | 
| 955 |  |  | 
| 956 |  | int ossl_ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, | 
| 957 |  |                                    BN_CTX *ctx) | 
| 958 | 217k | { | 
| 959 | 217k |     int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, | 
| 960 | 217k |                       const BIGNUM *, BN_CTX *); | 
| 961 | 217k |     int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | 
| 962 | 217k |     const BIGNUM *p; | 
| 963 | 217k |     BN_CTX *new_ctx = NULL; | 
| 964 | 217k |     BIGNUM *rh, *tmp, *Z4, *Z6; | 
| 965 | 217k |     int ret = -1; | 
| 966 |  |  | 
| 967 | 217k |     if (EC_POINT_is_at_infinity(group, point)) | 
| 968 | 0 |         return 1; | 
| 969 |  |  | 
| 970 | 217k |     field_mul = group->meth->field_mul; | 
| 971 | 217k |     field_sqr = group->meth->field_sqr; | 
| 972 | 217k |     p = group->field; | 
| 973 |  |  | 
| 974 | 217k |     if (ctx == NULL) { | 
| 975 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 976 | 0 |         if (ctx == NULL) | 
| 977 | 0 |             return -1; | 
| 978 | 0 |     } | 
| 979 |  |  | 
| 980 | 217k |     BN_CTX_start(ctx); | 
| 981 | 217k |     rh = BN_CTX_get(ctx); | 
| 982 | 217k |     tmp = BN_CTX_get(ctx); | 
| 983 | 217k |     Z4 = BN_CTX_get(ctx); | 
| 984 | 217k |     Z6 = BN_CTX_get(ctx); | 
| 985 | 217k |     if (Z6 == NULL) | 
| 986 | 0 |         goto err; | 
| 987 |  |  | 
| 988 |  |     /*- | 
| 989 |  |      * We have a curve defined by a Weierstrass equation | 
| 990 |  |      *      y^2 = x^3 + a*x + b. | 
| 991 |  |      * The point to consider is given in Jacobian projective coordinates | 
| 992 |  |      * where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3). | 
| 993 |  |      * Substituting this and multiplying by  Z^6  transforms the above equation into | 
| 994 |  |      *      Y^2 = X^3 + a*X*Z^4 + b*Z^6. | 
| 995 |  |      * To test this, we add up the right-hand side in 'rh'. | 
| 996 |  |      */ | 
| 997 |  |  | 
| 998 |  |     /* rh := X^2 */ | 
| 999 | 217k |     if (!field_sqr(group, rh, point->X, ctx)) | 
| 1000 | 0 |         goto err; | 
| 1001 |  |  | 
| 1002 | 217k |     if (!point->Z_is_one) { | 
| 1003 | 192 |         if (!field_sqr(group, tmp, point->Z, ctx)) | 
| 1004 | 0 |             goto err; | 
| 1005 | 192 |         if (!field_sqr(group, Z4, tmp, ctx)) | 
| 1006 | 0 |             goto err; | 
| 1007 | 192 |         if (!field_mul(group, Z6, Z4, tmp, ctx)) | 
| 1008 | 0 |             goto err; | 
| 1009 |  |  | 
| 1010 |  |         /* rh := (rh + a*Z^4)*X */ | 
| 1011 | 192 |         if (group->a_is_minus3) { | 
| 1012 | 140 |             if (!BN_mod_lshift1_quick(tmp, Z4, p)) | 
| 1013 | 0 |                 goto err; | 
| 1014 | 140 |             if (!BN_mod_add_quick(tmp, tmp, Z4, p)) | 
| 1015 | 0 |                 goto err; | 
| 1016 | 140 |             if (!BN_mod_sub_quick(rh, rh, tmp, p)) | 
| 1017 | 0 |                 goto err; | 
| 1018 | 140 |             if (!field_mul(group, rh, rh, point->X, ctx)) | 
| 1019 | 0 |                 goto err; | 
| 1020 | 140 |         } else { | 
| 1021 | 52 |             if (!field_mul(group, tmp, Z4, group->a, ctx)) | 
| 1022 | 0 |                 goto err; | 
| 1023 | 52 |             if (!BN_mod_add_quick(rh, rh, tmp, p)) | 
| 1024 | 0 |                 goto err; | 
| 1025 | 52 |             if (!field_mul(group, rh, rh, point->X, ctx)) | 
| 1026 | 0 |                 goto err; | 
| 1027 | 52 |         } | 
| 1028 |  |  | 
| 1029 |  |         /* rh := rh + b*Z^6 */ | 
| 1030 | 192 |         if (!field_mul(group, tmp, group->b, Z6, ctx)) | 
| 1031 | 0 |             goto err; | 
| 1032 | 192 |         if (!BN_mod_add_quick(rh, rh, tmp, p)) | 
| 1033 | 0 |             goto err; | 
| 1034 | 217k |     } else { | 
| 1035 |  |         /* point->Z_is_one */ | 
| 1036 |  |  | 
| 1037 |  |         /* rh := (rh + a)*X */ | 
| 1038 | 217k |         if (!BN_mod_add_quick(rh, rh, group->a, p)) | 
| 1039 | 0 |             goto err; | 
| 1040 | 217k |         if (!field_mul(group, rh, rh, point->X, ctx)) | 
| 1041 | 0 |             goto err; | 
| 1042 |  |         /* rh := rh + b */ | 
| 1043 | 217k |         if (!BN_mod_add_quick(rh, rh, group->b, p)) | 
| 1044 | 0 |             goto err; | 
| 1045 | 217k |     } | 
| 1046 |  |  | 
| 1047 |  |     /* 'lh' := Y^2 */ | 
| 1048 | 217k |     if (!field_sqr(group, tmp, point->Y, ctx)) | 
| 1049 | 0 |         goto err; | 
| 1050 |  |  | 
| 1051 | 217k |     ret = (0 == BN_ucmp(tmp, rh)); | 
| 1052 |  |  | 
| 1053 | 217k |  err: | 
| 1054 | 217k |     BN_CTX_end(ctx); | 
| 1055 | 217k |     BN_CTX_free(new_ctx); | 
| 1056 | 217k |     return ret; | 
| 1057 | 217k | } | 
| 1058 |  |  | 
| 1059 |  | int ossl_ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, | 
| 1060 |  |                            const EC_POINT *b, BN_CTX *ctx) | 
| 1061 | 33.2k | { | 
| 1062 |  |     /*- | 
| 1063 |  |      * return values: | 
| 1064 |  |      *  -1   error | 
| 1065 |  |      *   0   equal (in affine coordinates) | 
| 1066 |  |      *   1   not equal | 
| 1067 |  |      */ | 
| 1068 |  |  | 
| 1069 | 33.2k |     int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, | 
| 1070 | 33.2k |                       const BIGNUM *, BN_CTX *); | 
| 1071 | 33.2k |     int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | 
| 1072 | 33.2k |     BN_CTX *new_ctx = NULL; | 
| 1073 | 33.2k |     BIGNUM *tmp1, *tmp2, *Za23, *Zb23; | 
| 1074 | 33.2k |     const BIGNUM *tmp1_, *tmp2_; | 
| 1075 | 33.2k |     int ret = -1; | 
| 1076 |  |  | 
| 1077 | 33.2k |     if (EC_POINT_is_at_infinity(group, a)) { | 
| 1078 | 40 |         return EC_POINT_is_at_infinity(group, b) ? 0 : 1; | 
| 1079 | 40 |     } | 
| 1080 |  |  | 
| 1081 | 33.1k |     if (EC_POINT_is_at_infinity(group, b)) | 
| 1082 | 0 |         return 1; | 
| 1083 |  |  | 
| 1084 | 33.1k |     if (a->Z_is_one && b->Z_is_one) { | 
| 1085 | 33.0k |         return ((BN_cmp(a->X, b->X) == 0) && BN_cmp(a->Y, b->Y) == 0) ? 0 : 1; | 
| 1086 | 33.0k |     } | 
| 1087 |  |  | 
| 1088 | 184 |     field_mul = group->meth->field_mul; | 
| 1089 | 184 |     field_sqr = group->meth->field_sqr; | 
| 1090 |  |  | 
| 1091 | 184 |     if (ctx == NULL) { | 
| 1092 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 1093 | 0 |         if (ctx == NULL) | 
| 1094 | 0 |             return -1; | 
| 1095 | 0 |     } | 
| 1096 |  |  | 
| 1097 | 184 |     BN_CTX_start(ctx); | 
| 1098 | 184 |     tmp1 = BN_CTX_get(ctx); | 
| 1099 | 184 |     tmp2 = BN_CTX_get(ctx); | 
| 1100 | 184 |     Za23 = BN_CTX_get(ctx); | 
| 1101 | 184 |     Zb23 = BN_CTX_get(ctx); | 
| 1102 | 184 |     if (Zb23 == NULL) | 
| 1103 | 0 |         goto end; | 
| 1104 |  |  | 
| 1105 |  |     /*- | 
| 1106 |  |      * We have to decide whether | 
| 1107 |  |      *     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3), | 
| 1108 |  |      * or equivalently, whether | 
| 1109 |  |      *     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3). | 
| 1110 |  |      */ | 
| 1111 |  |  | 
| 1112 | 184 |     if (!b->Z_is_one) { | 
| 1113 | 157 |         if (!field_sqr(group, Zb23, b->Z, ctx)) | 
| 1114 | 0 |             goto end; | 
| 1115 | 157 |         if (!field_mul(group, tmp1, a->X, Zb23, ctx)) | 
| 1116 | 0 |             goto end; | 
| 1117 | 157 |         tmp1_ = tmp1; | 
| 1118 | 157 |     } else | 
| 1119 | 27 |         tmp1_ = a->X; | 
| 1120 | 184 |     if (!a->Z_is_one) { | 
| 1121 | 184 |         if (!field_sqr(group, Za23, a->Z, ctx)) | 
| 1122 | 0 |             goto end; | 
| 1123 | 184 |         if (!field_mul(group, tmp2, b->X, Za23, ctx)) | 
| 1124 | 0 |             goto end; | 
| 1125 | 184 |         tmp2_ = tmp2; | 
| 1126 | 184 |     } else | 
| 1127 | 0 |         tmp2_ = b->X; | 
| 1128 |  |  | 
| 1129 |  |     /* compare  X_a*Z_b^2  with  X_b*Z_a^2 */ | 
| 1130 | 184 |     if (BN_cmp(tmp1_, tmp2_) != 0) { | 
| 1131 | 27 |         ret = 1;                /* points differ */ | 
| 1132 | 27 |         goto end; | 
| 1133 | 27 |     } | 
| 1134 |  |  | 
| 1135 | 157 |     if (!b->Z_is_one) { | 
| 1136 | 157 |         if (!field_mul(group, Zb23, Zb23, b->Z, ctx)) | 
| 1137 | 0 |             goto end; | 
| 1138 | 157 |         if (!field_mul(group, tmp1, a->Y, Zb23, ctx)) | 
| 1139 | 0 |             goto end; | 
| 1140 |  |         /* tmp1_ = tmp1 */ | 
| 1141 | 157 |     } else | 
| 1142 | 0 |         tmp1_ = a->Y; | 
| 1143 | 157 |     if (!a->Z_is_one) { | 
| 1144 | 157 |         if (!field_mul(group, Za23, Za23, a->Z, ctx)) | 
| 1145 | 0 |             goto end; | 
| 1146 | 157 |         if (!field_mul(group, tmp2, b->Y, Za23, ctx)) | 
| 1147 | 0 |             goto end; | 
| 1148 |  |         /* tmp2_ = tmp2 */ | 
| 1149 | 157 |     } else | 
| 1150 | 0 |         tmp2_ = b->Y; | 
| 1151 |  |  | 
| 1152 |  |     /* compare  Y_a*Z_b^3  with  Y_b*Z_a^3 */ | 
| 1153 | 157 |     if (BN_cmp(tmp1_, tmp2_) != 0) { | 
| 1154 | 0 |         ret = 1;                /* points differ */ | 
| 1155 | 0 |         goto end; | 
| 1156 | 0 |     } | 
| 1157 |  |  | 
| 1158 |  |     /* points are equal */ | 
| 1159 | 157 |     ret = 0; | 
| 1160 |  |  | 
| 1161 | 184 |  end: | 
| 1162 | 184 |     BN_CTX_end(ctx); | 
| 1163 | 184 |     BN_CTX_free(new_ctx); | 
| 1164 | 184 |     return ret; | 
| 1165 | 157 | } | 
| 1166 |  |  | 
| 1167 |  | int ossl_ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, | 
| 1168 |  |                                    BN_CTX *ctx) | 
| 1169 | 0 | { | 
| 1170 | 0 |     BN_CTX *new_ctx = NULL; | 
| 1171 | 0 |     BIGNUM *x, *y; | 
| 1172 | 0 |     int ret = 0; | 
| 1173 |  | 
 | 
| 1174 | 0 |     if (point->Z_is_one || EC_POINT_is_at_infinity(group, point)) | 
| 1175 | 0 |         return 1; | 
| 1176 |  |  | 
| 1177 | 0 |     if (ctx == NULL) { | 
| 1178 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 1179 | 0 |         if (ctx == NULL) | 
| 1180 | 0 |             return 0; | 
| 1181 | 0 |     } | 
| 1182 |  |  | 
| 1183 | 0 |     BN_CTX_start(ctx); | 
| 1184 | 0 |     x = BN_CTX_get(ctx); | 
| 1185 | 0 |     y = BN_CTX_get(ctx); | 
| 1186 | 0 |     if (y == NULL) | 
| 1187 | 0 |         goto err; | 
| 1188 |  |  | 
| 1189 | 0 |     if (!EC_POINT_get_affine_coordinates(group, point, x, y, ctx)) | 
| 1190 | 0 |         goto err; | 
| 1191 | 0 |     if (!EC_POINT_set_affine_coordinates(group, point, x, y, ctx)) | 
| 1192 | 0 |         goto err; | 
| 1193 | 0 |     if (!point->Z_is_one) { | 
| 1194 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_INTERNAL_ERROR); | 
| 1195 | 0 |         goto err; | 
| 1196 | 0 |     } | 
| 1197 |  |  | 
| 1198 | 0 |     ret = 1; | 
| 1199 |  | 
 | 
| 1200 | 0 |  err: | 
| 1201 | 0 |     BN_CTX_end(ctx); | 
| 1202 | 0 |     BN_CTX_free(new_ctx); | 
| 1203 | 0 |     return ret; | 
| 1204 | 0 | } | 
| 1205 |  |  | 
| 1206 |  | int ossl_ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, | 
| 1207 |  |                                           EC_POINT *points[], BN_CTX *ctx) | 
| 1208 | 1.31k | { | 
| 1209 | 1.31k |     BN_CTX *new_ctx = NULL; | 
| 1210 | 1.31k |     BIGNUM *tmp, *tmp_Z; | 
| 1211 | 1.31k |     BIGNUM **prod_Z = NULL; | 
| 1212 | 1.31k |     size_t i; | 
| 1213 | 1.31k |     int ret = 0; | 
| 1214 |  |  | 
| 1215 | 1.31k |     if (num == 0) | 
| 1216 | 0 |         return 1; | 
| 1217 |  |  | 
| 1218 | 1.31k |     if (ctx == NULL) { | 
| 1219 | 0 |         ctx = new_ctx = BN_CTX_new_ex(group->libctx); | 
| 1220 | 0 |         if (ctx == NULL) | 
| 1221 | 0 |             return 0; | 
| 1222 | 0 |     } | 
| 1223 |  |  | 
| 1224 | 1.31k |     BN_CTX_start(ctx); | 
| 1225 | 1.31k |     tmp = BN_CTX_get(ctx); | 
| 1226 | 1.31k |     tmp_Z = BN_CTX_get(ctx); | 
| 1227 | 1.31k |     if (tmp_Z == NULL) | 
| 1228 | 0 |         goto err; | 
| 1229 |  |  | 
| 1230 | 1.31k |     prod_Z = OPENSSL_malloc(num * sizeof(prod_Z[0])); | 
| 1231 | 1.31k |     if (prod_Z == NULL) | 
| 1232 | 0 |         goto err; | 
| 1233 | 7.49k |     for (i = 0; i < num; i++) { | 
| 1234 | 6.17k |         prod_Z[i] = BN_new(); | 
| 1235 | 6.17k |         if (prod_Z[i] == NULL) | 
| 1236 | 0 |             goto err; | 
| 1237 | 6.17k |     } | 
| 1238 |  |  | 
| 1239 |  |     /* | 
| 1240 |  |      * Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z, | 
| 1241 |  |      * skipping any zero-valued inputs (pretend that they're 1). | 
| 1242 |  |      */ | 
| 1243 |  |  | 
| 1244 | 1.31k |     if (!BN_is_zero(points[0]->Z)) { | 
| 1245 | 1.31k |         if (!BN_copy(prod_Z[0], points[0]->Z)) | 
| 1246 | 0 |             goto err; | 
| 1247 | 1.31k |     } else { | 
| 1248 | 0 |         if (group->meth->field_set_to_one != 0) { | 
| 1249 | 0 |             if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) | 
| 1250 | 0 |                 goto err; | 
| 1251 | 0 |         } else { | 
| 1252 | 0 |             if (!BN_one(prod_Z[0])) | 
| 1253 | 0 |                 goto err; | 
| 1254 | 0 |         } | 
| 1255 | 0 |     } | 
| 1256 |  |  | 
| 1257 | 6.17k |     for (i = 1; i < num; i++) { | 
| 1258 | 4.85k |         if (!BN_is_zero(points[i]->Z)) { | 
| 1259 | 4.85k |             if (!group-> | 
| 1260 | 4.85k |                 meth->field_mul(group, prod_Z[i], prod_Z[i - 1], points[i]->Z, | 
| 1261 | 4.85k |                                 ctx)) | 
| 1262 | 0 |                 goto err; | 
| 1263 | 4.85k |         } else { | 
| 1264 | 0 |             if (!BN_copy(prod_Z[i], prod_Z[i - 1])) | 
| 1265 | 0 |                 goto err; | 
| 1266 | 0 |         } | 
| 1267 | 4.85k |     } | 
| 1268 |  |  | 
| 1269 |  |     /* | 
| 1270 |  |      * Now use a single explicit inversion to replace every non-zero | 
| 1271 |  |      * points[i]->Z by its inverse. | 
| 1272 |  |      */ | 
| 1273 |  |  | 
| 1274 | 1.31k |     if (!group->meth->field_inv(group, tmp, prod_Z[num - 1], ctx)) { | 
| 1275 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); | 
| 1276 | 0 |         goto err; | 
| 1277 | 0 |     } | 
| 1278 | 1.31k |     if (group->meth->field_encode != 0) { | 
| 1279 |  |         /* | 
| 1280 |  |          * In the Montgomery case, we just turned R*H (representing H) into | 
| 1281 |  |          * 1/(R*H), but we need R*(1/H) (representing 1/H); i.e. we need to | 
| 1282 |  |          * multiply by the Montgomery factor twice. | 
| 1283 |  |          */ | 
| 1284 | 1.31k |         if (!group->meth->field_encode(group, tmp, tmp, ctx)) | 
| 1285 | 0 |             goto err; | 
| 1286 | 1.31k |         if (!group->meth->field_encode(group, tmp, tmp, ctx)) | 
| 1287 | 0 |             goto err; | 
| 1288 | 1.31k |     } | 
| 1289 |  |  | 
| 1290 | 6.17k |     for (i = num - 1; i > 0; --i) { | 
| 1291 |  |         /* | 
| 1292 |  |          * Loop invariant: tmp is the product of the inverses of points[0]->Z | 
| 1293 |  |          * .. points[i]->Z (zero-valued inputs skipped). | 
| 1294 |  |          */ | 
| 1295 | 4.85k |         if (!BN_is_zero(points[i]->Z)) { | 
| 1296 |  |             /* | 
| 1297 |  |              * Set tmp_Z to the inverse of points[i]->Z (as product of Z | 
| 1298 |  |              * inverses 0 .. i, Z values 0 .. i - 1). | 
| 1299 |  |              */ | 
| 1300 | 4.85k |             if (!group-> | 
| 1301 | 4.85k |                 meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx)) | 
| 1302 | 0 |                 goto err; | 
| 1303 |  |             /* | 
| 1304 |  |              * Update tmp to satisfy the loop invariant for i - 1. | 
| 1305 |  |              */ | 
| 1306 | 4.85k |             if (!group->meth->field_mul(group, tmp, tmp, points[i]->Z, ctx)) | 
| 1307 | 0 |                 goto err; | 
| 1308 |  |             /* Replace points[i]->Z by its inverse. */ | 
| 1309 | 4.85k |             if (!BN_copy(points[i]->Z, tmp_Z)) | 
| 1310 | 0 |                 goto err; | 
| 1311 | 4.85k |         } | 
| 1312 | 4.85k |     } | 
| 1313 |  |  | 
| 1314 | 1.31k |     if (!BN_is_zero(points[0]->Z)) { | 
| 1315 |  |         /* Replace points[0]->Z by its inverse. */ | 
| 1316 | 1.31k |         if (!BN_copy(points[0]->Z, tmp)) | 
| 1317 | 0 |             goto err; | 
| 1318 | 1.31k |     } | 
| 1319 |  |  | 
| 1320 |  |     /* Finally, fix up the X and Y coordinates for all points. */ | 
| 1321 |  |  | 
| 1322 | 7.49k |     for (i = 0; i < num; i++) { | 
| 1323 | 6.17k |         EC_POINT *p = points[i]; | 
| 1324 |  |  | 
| 1325 | 6.17k |         if (!BN_is_zero(p->Z)) { | 
| 1326 |  |             /* turn  (X, Y, 1/Z)  into  (X/Z^2, Y/Z^3, 1) */ | 
| 1327 |  |  | 
| 1328 | 6.17k |             if (!group->meth->field_sqr(group, tmp, p->Z, ctx)) | 
| 1329 | 0 |                 goto err; | 
| 1330 | 6.17k |             if (!group->meth->field_mul(group, p->X, p->X, tmp, ctx)) | 
| 1331 | 0 |                 goto err; | 
| 1332 |  |  | 
| 1333 | 6.17k |             if (!group->meth->field_mul(group, tmp, tmp, p->Z, ctx)) | 
| 1334 | 0 |                 goto err; | 
| 1335 | 6.17k |             if (!group->meth->field_mul(group, p->Y, p->Y, tmp, ctx)) | 
| 1336 | 0 |                 goto err; | 
| 1337 |  |  | 
| 1338 | 6.17k |             if (group->meth->field_set_to_one != 0) { | 
| 1339 | 6.17k |                 if (!group->meth->field_set_to_one(group, p->Z, ctx)) | 
| 1340 | 0 |                     goto err; | 
| 1341 | 6.17k |             } else { | 
| 1342 | 0 |                 if (!BN_one(p->Z)) | 
| 1343 | 0 |                     goto err; | 
| 1344 | 0 |             } | 
| 1345 | 6.17k |             p->Z_is_one = 1; | 
| 1346 | 6.17k |         } | 
| 1347 | 6.17k |     } | 
| 1348 |  |  | 
| 1349 | 1.31k |     ret = 1; | 
| 1350 |  |  | 
| 1351 | 1.31k |  err: | 
| 1352 | 1.31k |     BN_CTX_end(ctx); | 
| 1353 | 1.31k |     BN_CTX_free(new_ctx); | 
| 1354 | 1.31k |     if (prod_Z != NULL) { | 
| 1355 | 7.49k |         for (i = 0; i < num; i++) { | 
| 1356 | 6.17k |             if (prod_Z[i] == NULL) | 
| 1357 | 0 |                 break; | 
| 1358 | 6.17k |             BN_clear_free(prod_Z[i]); | 
| 1359 | 6.17k |         } | 
| 1360 | 1.31k |         OPENSSL_free(prod_Z); | 
| 1361 | 1.31k |     } | 
| 1362 | 1.31k |     return ret; | 
| 1363 | 1.31k | } | 
| 1364 |  |  | 
| 1365 |  | int ossl_ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 
| 1366 |  |                                  const BIGNUM *b, BN_CTX *ctx) | 
| 1367 | 0 | { | 
| 1368 | 0 |     return BN_mod_mul(r, a, b, group->field, ctx); | 
| 1369 | 0 | } | 
| 1370 |  |  | 
| 1371 |  | int ossl_ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, | 
| 1372 |  |                                  BN_CTX *ctx) | 
| 1373 | 0 | { | 
| 1374 | 0 |     return BN_mod_sqr(r, a, group->field, ctx); | 
| 1375 | 0 | } | 
| 1376 |  |  | 
| 1377 |  | /*- | 
| 1378 |  |  * Computes the multiplicative inverse of a in GF(p), storing the result in r. | 
| 1379 |  |  * If a is zero (or equivalent), you'll get a EC_R_CANNOT_INVERT error. | 
| 1380 |  |  * Since we don't have a Mont structure here, SCA hardening is with blinding. | 
| 1381 |  |  * NB: "a" must be in _decoded_ form. (i.e. field_decode must precede.) | 
| 1382 |  |  */ | 
| 1383 |  | int ossl_ec_GFp_simple_field_inv(const EC_GROUP *group, BIGNUM *r, | 
| 1384 |  |                                  const BIGNUM *a, BN_CTX *ctx) | 
| 1385 | 0 | { | 
| 1386 | 0 |     BIGNUM *e = NULL; | 
| 1387 | 0 |     BN_CTX *new_ctx = NULL; | 
| 1388 | 0 |     int ret = 0; | 
| 1389 |  | 
 | 
| 1390 | 0 |     if (ctx == NULL | 
| 1391 | 0 |             && (ctx = new_ctx = BN_CTX_secure_new_ex(group->libctx)) == NULL) | 
| 1392 | 0 |         return 0; | 
| 1393 |  |  | 
| 1394 | 0 |     BN_CTX_start(ctx); | 
| 1395 | 0 |     if ((e = BN_CTX_get(ctx)) == NULL) | 
| 1396 | 0 |         goto err; | 
| 1397 |  |  | 
| 1398 | 0 |     do { | 
| 1399 | 0 |         if (!BN_priv_rand_range_ex(e, group->field, 0, ctx)) | 
| 1400 | 0 |         goto err; | 
| 1401 | 0 |     } while (BN_is_zero(e)); | 
| 1402 |  |  | 
| 1403 |  |     /* r := a * e */ | 
| 1404 | 0 |     if (!group->meth->field_mul(group, r, a, e, ctx)) | 
| 1405 | 0 |         goto err; | 
| 1406 |  |     /* r := 1/(a * e) */ | 
| 1407 | 0 |     if (!BN_mod_inverse(r, r, group->field, ctx)) { | 
| 1408 | 0 |         ERR_raise(ERR_LIB_EC, EC_R_CANNOT_INVERT); | 
| 1409 | 0 |         goto err; | 
| 1410 | 0 |     } | 
| 1411 |  |     /* r := e/(a * e) = 1/a */ | 
| 1412 | 0 |     if (!group->meth->field_mul(group, r, r, e, ctx)) | 
| 1413 | 0 |         goto err; | 
| 1414 |  |  | 
| 1415 | 0 |     ret = 1; | 
| 1416 |  | 
 | 
| 1417 | 0 |  err: | 
| 1418 | 0 |     BN_CTX_end(ctx); | 
| 1419 | 0 |     BN_CTX_free(new_ctx); | 
| 1420 | 0 |     return ret; | 
| 1421 | 0 | } | 
| 1422 |  |  | 
| 1423 |  | /*- | 
| 1424 |  |  * Apply randomization of EC point projective coordinates: | 
| 1425 |  |  * | 
| 1426 |  |  *   (X, Y ,Z ) = (lambda^2*X, lambda^3*Y, lambda*Z) | 
| 1427 |  |  *   lambda = [1,group->field) | 
| 1428 |  |  * | 
| 1429 |  |  */ | 
| 1430 |  | int ossl_ec_GFp_simple_blind_coordinates(const EC_GROUP *group, EC_POINT *p, | 
| 1431 |  |                                          BN_CTX *ctx) | 
| 1432 | 1.30k | { | 
| 1433 | 1.30k |     int ret = 0; | 
| 1434 | 1.30k |     BIGNUM *lambda = NULL; | 
| 1435 | 1.30k |     BIGNUM *temp = NULL; | 
| 1436 |  |  | 
| 1437 | 1.30k |     BN_CTX_start(ctx); | 
| 1438 | 1.30k |     lambda = BN_CTX_get(ctx); | 
| 1439 | 1.30k |     temp = BN_CTX_get(ctx); | 
| 1440 | 1.30k |     if (temp == NULL) { | 
| 1441 | 0 |         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); | 
| 1442 | 0 |         goto end; | 
| 1443 | 0 |     } | 
| 1444 |  |  | 
| 1445 |  |     /*- | 
| 1446 |  |      * Make sure lambda is not zero. | 
| 1447 |  |      * If the RNG fails, we cannot blind but nevertheless want | 
| 1448 |  |      * code to continue smoothly and not clobber the error stack. | 
| 1449 |  |      */ | 
| 1450 | 1.30k |     do { | 
| 1451 | 1.30k |         ERR_set_mark(); | 
| 1452 | 1.30k |         ret = BN_priv_rand_range_ex(lambda, group->field, 0, ctx); | 
| 1453 | 1.30k |         ERR_pop_to_mark(); | 
| 1454 | 1.30k |         if (ret == 0) { | 
| 1455 | 0 |             ret = 1; | 
| 1456 | 0 |             goto end; | 
| 1457 | 0 |         } | 
| 1458 | 1.30k |     } while (BN_is_zero(lambda)); | 
| 1459 |  |  | 
| 1460 |  |     /* if field_encode defined convert between representations */ | 
| 1461 | 1.30k |     if ((group->meth->field_encode != NULL | 
| 1462 | 1.30k |          && !group->meth->field_encode(group, lambda, lambda, ctx)) | 
| 1463 | 1.30k |         || !group->meth->field_mul(group, p->Z, p->Z, lambda, ctx) | 
| 1464 | 1.30k |         || !group->meth->field_sqr(group, temp, lambda, ctx) | 
| 1465 | 1.30k |         || !group->meth->field_mul(group, p->X, p->X, temp, ctx) | 
| 1466 | 1.30k |         || !group->meth->field_mul(group, temp, temp, lambda, ctx) | 
| 1467 | 1.30k |         || !group->meth->field_mul(group, p->Y, p->Y, temp, ctx)) | 
| 1468 | 0 |         goto end; | 
| 1469 |  |  | 
| 1470 | 1.30k |     p->Z_is_one = 0; | 
| 1471 | 1.30k |     ret = 1; | 
| 1472 |  |  | 
| 1473 | 1.30k |  end: | 
| 1474 | 1.30k |     BN_CTX_end(ctx); | 
| 1475 | 1.30k |     return ret; | 
| 1476 | 1.30k | } | 
| 1477 |  |  | 
| 1478 |  | /*- | 
| 1479 |  |  * Input: | 
| 1480 |  |  * - p: affine coordinates | 
| 1481 |  |  * | 
| 1482 |  |  * Output: | 
| 1483 |  |  * - s := p, r := 2p: blinded projective (homogeneous) coordinates | 
| 1484 |  |  * | 
| 1485 |  |  * For doubling we use Formula 3 from Izu-Takagi "A fast parallel elliptic curve | 
| 1486 |  |  * multiplication resistant against side channel attacks" appendix, described at | 
| 1487 |  |  * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#doubling-dbl-2002-it-2 | 
| 1488 |  |  * simplified for Z1=1. | 
| 1489 |  |  * | 
| 1490 |  |  * Blinding uses the equivalence relation (\lambda X, \lambda Y, \lambda Z) | 
| 1491 |  |  * for any non-zero \lambda that holds for projective (homogeneous) coords. | 
| 1492 |  |  */ | 
| 1493 |  | int ossl_ec_GFp_simple_ladder_pre(const EC_GROUP *group, | 
| 1494 |  |                                   EC_POINT *r, EC_POINT *s, | 
| 1495 |  |                                   EC_POINT *p, BN_CTX *ctx) | 
| 1496 | 1.32k | { | 
| 1497 | 1.32k |     BIGNUM *t1, *t2, *t3, *t4, *t5 = NULL; | 
| 1498 |  |  | 
| 1499 | 1.32k |     t1 = s->Z; | 
| 1500 | 1.32k |     t2 = r->Z; | 
| 1501 | 1.32k |     t3 = s->X; | 
| 1502 | 1.32k |     t4 = r->X; | 
| 1503 | 1.32k |     t5 = s->Y; | 
| 1504 |  |  | 
| 1505 | 1.32k |     if (!p->Z_is_one /* r := 2p */ | 
| 1506 | 1.32k |         || !group->meth->field_sqr(group, t3, p->X, ctx) | 
| 1507 | 1.32k |         || !BN_mod_sub_quick(t4, t3, group->a, group->field) | 
| 1508 | 1.32k |         || !group->meth->field_sqr(group, t4, t4, ctx) | 
| 1509 | 1.32k |         || !group->meth->field_mul(group, t5, p->X, group->b, ctx) | 
| 1510 | 1.32k |         || !BN_mod_lshift_quick(t5, t5, 3, group->field) | 
| 1511 |  |         /* r->X coord output */ | 
| 1512 | 1.32k |         || !BN_mod_sub_quick(r->X, t4, t5, group->field) | 
| 1513 | 1.32k |         || !BN_mod_add_quick(t1, t3, group->a, group->field) | 
| 1514 | 1.32k |         || !group->meth->field_mul(group, t2, p->X, t1, ctx) | 
| 1515 | 1.32k |         || !BN_mod_add_quick(t2, group->b, t2, group->field) | 
| 1516 |  |         /* r->Z coord output */ | 
| 1517 | 1.32k |         || !BN_mod_lshift_quick(r->Z, t2, 2, group->field)) | 
| 1518 | 0 |         return 0; | 
| 1519 |  |  | 
| 1520 |  |     /* make sure lambda (r->Y here for storage) is not zero */ | 
| 1521 | 1.32k |     do { | 
| 1522 | 1.32k |         if (!BN_priv_rand_range_ex(r->Y, group->field, 0, ctx)) | 
| 1523 | 0 |             return 0; | 
| 1524 | 1.32k |     } while (BN_is_zero(r->Y)); | 
| 1525 |  |  | 
| 1526 |  |     /* make sure lambda (s->Z here for storage) is not zero */ | 
| 1527 | 1.32k |     do { | 
| 1528 | 1.32k |         if (!BN_priv_rand_range_ex(s->Z, group->field, 0, ctx)) | 
| 1529 | 0 |             return 0; | 
| 1530 | 1.32k |     } while (BN_is_zero(s->Z)); | 
| 1531 |  |  | 
| 1532 |  |     /* if field_encode defined convert between representations */ | 
| 1533 | 1.32k |     if (group->meth->field_encode != NULL | 
| 1534 | 1.32k |         && (!group->meth->field_encode(group, r->Y, r->Y, ctx) | 
| 1535 | 1.32k |             || !group->meth->field_encode(group, s->Z, s->Z, ctx))) | 
| 1536 | 0 |         return 0; | 
| 1537 |  |  | 
| 1538 |  |     /* blind r and s independently */ | 
| 1539 | 1.32k |     if (!group->meth->field_mul(group, r->Z, r->Z, r->Y, ctx) | 
| 1540 | 1.32k |         || !group->meth->field_mul(group, r->X, r->X, r->Y, ctx) | 
| 1541 | 1.32k |         || !group->meth->field_mul(group, s->X, p->X, s->Z, ctx)) /* s := p */ | 
| 1542 | 0 |         return 0; | 
| 1543 |  |  | 
| 1544 | 1.32k |     r->Z_is_one = 0; | 
| 1545 | 1.32k |     s->Z_is_one = 0; | 
| 1546 |  |  | 
| 1547 | 1.32k |     return 1; | 
| 1548 | 1.32k | } | 
| 1549 |  |  | 
| 1550 |  | /*- | 
| 1551 |  |  * Input: | 
| 1552 |  |  * - s, r: projective (homogeneous) coordinates | 
| 1553 |  |  * - p: affine coordinates | 
| 1554 |  |  * | 
| 1555 |  |  * Output: | 
| 1556 |  |  * - s := r + s, r := 2r: projective (homogeneous) coordinates | 
| 1557 |  |  * | 
| 1558 |  |  * Differential addition-and-doubling using Eq. (9) and (10) from Izu-Takagi | 
| 1559 |  |  * "A fast parallel elliptic curve multiplication resistant against side channel | 
| 1560 |  |  * attacks", as described at | 
| 1561 |  |  * https://hyperelliptic.org/EFD/g1p/auto-shortw-xz.html#ladder-mladd-2002-it-4 | 
| 1562 |  |  */ | 
| 1563 |  | int ossl_ec_GFp_simple_ladder_step(const EC_GROUP *group, | 
| 1564 |  |                                    EC_POINT *r, EC_POINT *s, | 
| 1565 |  |                                    EC_POINT *p, BN_CTX *ctx) | 
| 1566 | 569k | { | 
| 1567 | 569k |     int ret = 0; | 
| 1568 | 569k |     BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL; | 
| 1569 |  |  | 
| 1570 | 569k |     BN_CTX_start(ctx); | 
| 1571 | 569k |     t0 = BN_CTX_get(ctx); | 
| 1572 | 569k |     t1 = BN_CTX_get(ctx); | 
| 1573 | 569k |     t2 = BN_CTX_get(ctx); | 
| 1574 | 569k |     t3 = BN_CTX_get(ctx); | 
| 1575 | 569k |     t4 = BN_CTX_get(ctx); | 
| 1576 | 569k |     t5 = BN_CTX_get(ctx); | 
| 1577 | 569k |     t6 = BN_CTX_get(ctx); | 
| 1578 |  |  | 
| 1579 | 569k |     if (t6 == NULL | 
| 1580 | 569k |         || !group->meth->field_mul(group, t6, r->X, s->X, ctx) | 
| 1581 | 569k |         || !group->meth->field_mul(group, t0, r->Z, s->Z, ctx) | 
| 1582 | 569k |         || !group->meth->field_mul(group, t4, r->X, s->Z, ctx) | 
| 1583 | 569k |         || !group->meth->field_mul(group, t3, r->Z, s->X, ctx) | 
| 1584 | 569k |         || !group->meth->field_mul(group, t5, group->a, t0, ctx) | 
| 1585 | 569k |         || !BN_mod_add_quick(t5, t6, t5, group->field) | 
| 1586 | 569k |         || !BN_mod_add_quick(t6, t3, t4, group->field) | 
| 1587 | 569k |         || !group->meth->field_mul(group, t5, t6, t5, ctx) | 
| 1588 | 569k |         || !group->meth->field_sqr(group, t0, t0, ctx) | 
| 1589 | 569k |         || !BN_mod_lshift_quick(t2, group->b, 2, group->field) | 
| 1590 | 569k |         || !group->meth->field_mul(group, t0, t2, t0, ctx) | 
| 1591 | 569k |         || !BN_mod_lshift1_quick(t5, t5, group->field) | 
| 1592 | 569k |         || !BN_mod_sub_quick(t3, t4, t3, group->field) | 
| 1593 |  |         /* s->Z coord output */ | 
| 1594 | 569k |         || !group->meth->field_sqr(group, s->Z, t3, ctx) | 
| 1595 | 569k |         || !group->meth->field_mul(group, t4, s->Z, p->X, ctx) | 
| 1596 | 569k |         || !BN_mod_add_quick(t0, t0, t5, group->field) | 
| 1597 |  |         /* s->X coord output */ | 
| 1598 | 569k |         || !BN_mod_sub_quick(s->X, t0, t4, group->field) | 
| 1599 | 569k |         || !group->meth->field_sqr(group, t4, r->X, ctx) | 
| 1600 | 569k |         || !group->meth->field_sqr(group, t5, r->Z, ctx) | 
| 1601 | 569k |         || !group->meth->field_mul(group, t6, t5, group->a, ctx) | 
| 1602 | 569k |         || !BN_mod_add_quick(t1, r->X, r->Z, group->field) | 
| 1603 | 569k |         || !group->meth->field_sqr(group, t1, t1, ctx) | 
| 1604 | 569k |         || !BN_mod_sub_quick(t1, t1, t4, group->field) | 
| 1605 | 569k |         || !BN_mod_sub_quick(t1, t1, t5, group->field) | 
| 1606 | 569k |         || !BN_mod_sub_quick(t3, t4, t6, group->field) | 
| 1607 | 569k |         || !group->meth->field_sqr(group, t3, t3, ctx) | 
| 1608 | 569k |         || !group->meth->field_mul(group, t0, t5, t1, ctx) | 
| 1609 | 569k |         || !group->meth->field_mul(group, t0, t2, t0, ctx) | 
| 1610 |  |         /* r->X coord output */ | 
| 1611 | 569k |         || !BN_mod_sub_quick(r->X, t3, t0, group->field) | 
| 1612 | 569k |         || !BN_mod_add_quick(t3, t4, t6, group->field) | 
| 1613 | 569k |         || !group->meth->field_sqr(group, t4, t5, ctx) | 
| 1614 | 569k |         || !group->meth->field_mul(group, t4, t4, t2, ctx) | 
| 1615 | 569k |         || !group->meth->field_mul(group, t1, t1, t3, ctx) | 
| 1616 | 569k |         || !BN_mod_lshift1_quick(t1, t1, group->field) | 
| 1617 |  |         /* r->Z coord output */ | 
| 1618 | 569k |         || !BN_mod_add_quick(r->Z, t4, t1, group->field)) | 
| 1619 | 0 |         goto err; | 
| 1620 |  |  | 
| 1621 | 569k |     ret = 1; | 
| 1622 |  |  | 
| 1623 | 569k |  err: | 
| 1624 | 569k |     BN_CTX_end(ctx); | 
| 1625 | 569k |     return ret; | 
| 1626 | 569k | } | 
| 1627 |  |  | 
| 1628 |  | /*- | 
| 1629 |  |  * Input: | 
| 1630 |  |  * - s, r: projective (homogeneous) coordinates | 
| 1631 |  |  * - p: affine coordinates | 
| 1632 |  |  * | 
| 1633 |  |  * Output: | 
| 1634 |  |  * - r := (x,y): affine coordinates | 
| 1635 |  |  * | 
| 1636 |  |  * Recovers the y-coordinate of r using Eq. (8) from Brier-Joye, "Weierstrass | 
| 1637 |  |  * Elliptic Curves and Side-Channel Attacks", modified to work in mixed | 
| 1638 |  |  * projective coords, i.e. p is affine and (r,s) in projective (homogeneous) | 
| 1639 |  |  * coords, and return r in affine coordinates. | 
| 1640 |  |  * | 
| 1641 |  |  * X4 = two*Y1*X2*Z3*Z2; | 
| 1642 |  |  * Y4 = two*b*Z3*SQR(Z2) + Z3*(a*Z2+X1*X2)*(X1*Z2+X2) - X3*SQR(X1*Z2-X2); | 
| 1643 |  |  * Z4 = two*Y1*Z3*SQR(Z2); | 
| 1644 |  |  * | 
| 1645 |  |  * Z4 != 0 because: | 
| 1646 |  |  *  - Z2==0 implies r is at infinity (handled by the BN_is_zero(r->Z) branch); | 
| 1647 |  |  *  - Z3==0 implies s is at infinity (handled by the BN_is_zero(s->Z) branch); | 
| 1648 |  |  *  - Y1==0 implies p has order 2, so either r or s are infinity and handled by | 
| 1649 |  |  *    one of the BN_is_zero(...) branches. | 
| 1650 |  |  */ | 
| 1651 |  | int ossl_ec_GFp_simple_ladder_post(const EC_GROUP *group, | 
| 1652 |  |                                    EC_POINT *r, EC_POINT *s, | 
| 1653 |  |                                    EC_POINT *p, BN_CTX *ctx) | 
| 1654 | 1.32k | { | 
| 1655 | 1.32k |     int ret = 0; | 
| 1656 | 1.32k |     BIGNUM *t0, *t1, *t2, *t3, *t4, *t5, *t6 = NULL; | 
| 1657 |  |  | 
| 1658 | 1.32k |     if (BN_is_zero(r->Z)) | 
| 1659 | 105 |         return EC_POINT_set_to_infinity(group, r); | 
| 1660 |  |  | 
| 1661 | 1.22k |     if (BN_is_zero(s->Z)) { | 
| 1662 | 14 |         if (!EC_POINT_copy(r, p) | 
| 1663 | 14 |             || !EC_POINT_invert(group, r, ctx)) | 
| 1664 | 0 |             return 0; | 
| 1665 | 14 |         return 1; | 
| 1666 | 14 |     } | 
| 1667 |  |  | 
| 1668 | 1.21k |     BN_CTX_start(ctx); | 
| 1669 | 1.21k |     t0 = BN_CTX_get(ctx); | 
| 1670 | 1.21k |     t1 = BN_CTX_get(ctx); | 
| 1671 | 1.21k |     t2 = BN_CTX_get(ctx); | 
| 1672 | 1.21k |     t3 = BN_CTX_get(ctx); | 
| 1673 | 1.21k |     t4 = BN_CTX_get(ctx); | 
| 1674 | 1.21k |     t5 = BN_CTX_get(ctx); | 
| 1675 | 1.21k |     t6 = BN_CTX_get(ctx); | 
| 1676 |  |  | 
| 1677 | 1.21k |     if (t6 == NULL | 
| 1678 | 1.21k |         || !BN_mod_lshift1_quick(t4, p->Y, group->field) | 
| 1679 | 1.21k |         || !group->meth->field_mul(group, t6, r->X, t4, ctx) | 
| 1680 | 1.21k |         || !group->meth->field_mul(group, t6, s->Z, t6, ctx) | 
| 1681 | 1.21k |         || !group->meth->field_mul(group, t5, r->Z, t6, ctx) | 
| 1682 | 1.21k |         || !BN_mod_lshift1_quick(t1, group->b, group->field) | 
| 1683 | 1.21k |         || !group->meth->field_mul(group, t1, s->Z, t1, ctx) | 
| 1684 | 1.21k |         || !group->meth->field_sqr(group, t3, r->Z, ctx) | 
| 1685 | 1.21k |         || !group->meth->field_mul(group, t2, t3, t1, ctx) | 
| 1686 | 1.21k |         || !group->meth->field_mul(group, t6, r->Z, group->a, ctx) | 
| 1687 | 1.21k |         || !group->meth->field_mul(group, t1, p->X, r->X, ctx) | 
| 1688 | 1.21k |         || !BN_mod_add_quick(t1, t1, t6, group->field) | 
| 1689 | 1.21k |         || !group->meth->field_mul(group, t1, s->Z, t1, ctx) | 
| 1690 | 1.21k |         || !group->meth->field_mul(group, t0, p->X, r->Z, ctx) | 
| 1691 | 1.21k |         || !BN_mod_add_quick(t6, r->X, t0, group->field) | 
| 1692 | 1.21k |         || !group->meth->field_mul(group, t6, t6, t1, ctx) | 
| 1693 | 1.21k |         || !BN_mod_add_quick(t6, t6, t2, group->field) | 
| 1694 | 1.21k |         || !BN_mod_sub_quick(t0, t0, r->X, group->field) | 
| 1695 | 1.21k |         || !group->meth->field_sqr(group, t0, t0, ctx) | 
| 1696 | 1.21k |         || !group->meth->field_mul(group, t0, t0, s->X, ctx) | 
| 1697 | 1.21k |         || !BN_mod_sub_quick(t0, t6, t0, group->field) | 
| 1698 | 1.21k |         || !group->meth->field_mul(group, t1, s->Z, t4, ctx) | 
| 1699 | 1.21k |         || !group->meth->field_mul(group, t1, t3, t1, ctx) | 
| 1700 | 1.21k |         || (group->meth->field_decode != NULL | 
| 1701 | 1.21k |             && !group->meth->field_decode(group, t1, t1, ctx)) | 
| 1702 | 1.21k |         || !group->meth->field_inv(group, t1, t1, ctx) | 
| 1703 | 1.21k |         || (group->meth->field_encode != NULL | 
| 1704 | 1.21k |             && !group->meth->field_encode(group, t1, t1, ctx)) | 
| 1705 | 1.21k |         || !group->meth->field_mul(group, r->X, t5, t1, ctx) | 
| 1706 | 1.21k |         || !group->meth->field_mul(group, r->Y, t0, t1, ctx)) | 
| 1707 | 0 |         goto err; | 
| 1708 |  |  | 
| 1709 | 1.21k |     if (group->meth->field_set_to_one != NULL) { | 
| 1710 | 1.21k |         if (!group->meth->field_set_to_one(group, r->Z, ctx)) | 
| 1711 | 0 |             goto err; | 
| 1712 | 1.21k |     } else { | 
| 1713 | 0 |         if (!BN_one(r->Z)) | 
| 1714 | 0 |             goto err; | 
| 1715 | 0 |     } | 
| 1716 |  |  | 
| 1717 | 1.21k |     r->Z_is_one = 1; | 
| 1718 | 1.21k |     ret = 1; | 
| 1719 |  |  | 
| 1720 | 1.21k |  err: | 
| 1721 | 1.21k |     BN_CTX_end(ctx); | 
| 1722 | 1.21k |     return ret; | 
| 1723 | 1.21k | } |