| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/ec/ecp_smpl.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* $OpenBSD: ecp_smpl.c,v 1.22 2018/07/16 17:32:39 tb Exp $ */ | - | ||||||||||||||||||||||||||||||
| 2 | /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> | - | ||||||||||||||||||||||||||||||
| 3 | * for the OpenSSL project. | - | ||||||||||||||||||||||||||||||
| 4 | * Includes code written by Bodo Moeller for the OpenSSL project. | - | ||||||||||||||||||||||||||||||
| 5 | */ | - | ||||||||||||||||||||||||||||||
| 6 | /* ==================================================================== | - | ||||||||||||||||||||||||||||||
| 7 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | - | ||||||||||||||||||||||||||||||
| 8 | * | - | ||||||||||||||||||||||||||||||
| 9 | * Redistribution and use in source and binary forms, with or without | - | ||||||||||||||||||||||||||||||
| 10 | * modification, are permitted provided that the following conditions | - | ||||||||||||||||||||||||||||||
| 11 | * are met: | - | ||||||||||||||||||||||||||||||
| 12 | * | - | ||||||||||||||||||||||||||||||
| 13 | * 1. Redistributions of source code must retain the above copyright | - | ||||||||||||||||||||||||||||||
| 14 | * notice, this list of conditions and the following disclaimer. | - | ||||||||||||||||||||||||||||||
| 15 | * | - | ||||||||||||||||||||||||||||||
| 16 | * 2. Redistributions in binary form must reproduce the above copyright | - | ||||||||||||||||||||||||||||||
| 17 | * notice, this list of conditions and the following disclaimer in | - | ||||||||||||||||||||||||||||||
| 18 | * the documentation and/or other materials provided with the | - | ||||||||||||||||||||||||||||||
| 19 | * distribution. | - | ||||||||||||||||||||||||||||||
| 20 | * | - | ||||||||||||||||||||||||||||||
| 21 | * 3. All advertising materials mentioning features or use of this | - | ||||||||||||||||||||||||||||||
| 22 | * software must display the following acknowledgment: | - | ||||||||||||||||||||||||||||||
| 23 | * "This product includes software developed by the OpenSSL Project | - | ||||||||||||||||||||||||||||||
| 24 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | - | ||||||||||||||||||||||||||||||
| 25 | * | - | ||||||||||||||||||||||||||||||
| 26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | - | ||||||||||||||||||||||||||||||
| 27 | * endorse or promote products derived from this software without | - | ||||||||||||||||||||||||||||||
| 28 | * prior written permission. For written permission, please contact | - | ||||||||||||||||||||||||||||||
| 29 | * openssl-core@openssl.org. | - | ||||||||||||||||||||||||||||||
| 30 | * | - | ||||||||||||||||||||||||||||||
| 31 | * 5. Products derived from this software may not be called "OpenSSL" | - | ||||||||||||||||||||||||||||||
| 32 | * nor may "OpenSSL" appear in their names without prior written | - | ||||||||||||||||||||||||||||||
| 33 | * permission of the OpenSSL Project. | - | ||||||||||||||||||||||||||||||
| 34 | * | - | ||||||||||||||||||||||||||||||
| 35 | * 6. Redistributions of any form whatsoever must retain the following | - | ||||||||||||||||||||||||||||||
| 36 | * acknowledgment: | - | ||||||||||||||||||||||||||||||
| 37 | * "This product includes software developed by the OpenSSL Project | - | ||||||||||||||||||||||||||||||
| 38 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | - | ||||||||||||||||||||||||||||||
| 39 | * | - | ||||||||||||||||||||||||||||||
| 40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | - | ||||||||||||||||||||||||||||||
| 41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | - | ||||||||||||||||||||||||||||||
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | - | ||||||||||||||||||||||||||||||
| 43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | - | ||||||||||||||||||||||||||||||
| 44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | - | ||||||||||||||||||||||||||||||
| 45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | - | ||||||||||||||||||||||||||||||
| 46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | - | ||||||||||||||||||||||||||||||
| 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | - | ||||||||||||||||||||||||||||||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | - | ||||||||||||||||||||||||||||||
| 49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | - | ||||||||||||||||||||||||||||||
| 50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | - | ||||||||||||||||||||||||||||||
| 51 | * OF THE POSSIBILITY OF SUCH DAMAGE. | - | ||||||||||||||||||||||||||||||
| 52 | * ==================================================================== | - | ||||||||||||||||||||||||||||||
| 53 | * | - | ||||||||||||||||||||||||||||||
| 54 | * This product includes cryptographic software written by Eric Young | - | ||||||||||||||||||||||||||||||
| 55 | * (eay@cryptsoft.com). This product includes software written by Tim | - | ||||||||||||||||||||||||||||||
| 56 | * Hudson (tjh@cryptsoft.com). | - | ||||||||||||||||||||||||||||||
| 57 | * | - | ||||||||||||||||||||||||||||||
| 58 | */ | - | ||||||||||||||||||||||||||||||
| 59 | /* ==================================================================== | - | ||||||||||||||||||||||||||||||
| 60 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. | - | ||||||||||||||||||||||||||||||
| 61 | * Portions of this software developed by SUN MICROSYSTEMS, INC., | - | ||||||||||||||||||||||||||||||
| 62 | * and contributed to the OpenSSL project. | - | ||||||||||||||||||||||||||||||
| 63 | */ | - | ||||||||||||||||||||||||||||||
| 64 | - | |||||||||||||||||||||||||||||||
| 65 | #include <openssl/err.h> | - | ||||||||||||||||||||||||||||||
| 66 | - | |||||||||||||||||||||||||||||||
| 67 | #include "bn_lcl.h" | - | ||||||||||||||||||||||||||||||
| 68 | #include "ec_lcl.h" | - | ||||||||||||||||||||||||||||||
| 69 | - | |||||||||||||||||||||||||||||||
| 70 | const EC_METHOD * | - | ||||||||||||||||||||||||||||||
| 71 | EC_GFp_simple_method(void) | - | ||||||||||||||||||||||||||||||
| 72 | { | - | ||||||||||||||||||||||||||||||
| 73 | static const EC_METHOD ret = { | - | ||||||||||||||||||||||||||||||
| 74 | .flags = EC_FLAGS_DEFAULT_OCT, | - | ||||||||||||||||||||||||||||||
| 75 | .field_type = NID_X9_62_prime_field, | - | ||||||||||||||||||||||||||||||
| 76 | .group_init = ec_GFp_simple_group_init, | - | ||||||||||||||||||||||||||||||
| 77 | .group_finish = ec_GFp_simple_group_finish, | - | ||||||||||||||||||||||||||||||
| 78 | .group_clear_finish = ec_GFp_simple_group_clear_finish, | - | ||||||||||||||||||||||||||||||
| 79 | .group_copy = ec_GFp_simple_group_copy, | - | ||||||||||||||||||||||||||||||
| 80 | .group_set_curve = ec_GFp_simple_group_set_curve, | - | ||||||||||||||||||||||||||||||
| 81 | .group_get_curve = ec_GFp_simple_group_get_curve, | - | ||||||||||||||||||||||||||||||
| 82 | .group_get_degree = ec_GFp_simple_group_get_degree, | - | ||||||||||||||||||||||||||||||
| 83 | .group_check_discriminant = | - | ||||||||||||||||||||||||||||||
| 84 | ec_GFp_simple_group_check_discriminant, | - | ||||||||||||||||||||||||||||||
| 85 | .point_init = ec_GFp_simple_point_init, | - | ||||||||||||||||||||||||||||||
| 86 | .point_finish = ec_GFp_simple_point_finish, | - | ||||||||||||||||||||||||||||||
| 87 | .point_clear_finish = ec_GFp_simple_point_clear_finish, | - | ||||||||||||||||||||||||||||||
| 88 | .point_copy = ec_GFp_simple_point_copy, | - | ||||||||||||||||||||||||||||||
| 89 | .point_set_to_infinity = ec_GFp_simple_point_set_to_infinity, | - | ||||||||||||||||||||||||||||||
| 90 | .point_set_Jprojective_coordinates_GFp = | - | ||||||||||||||||||||||||||||||
| 91 | ec_GFp_simple_set_Jprojective_coordinates_GFp, | - | ||||||||||||||||||||||||||||||
| 92 | .point_get_Jprojective_coordinates_GFp = | - | ||||||||||||||||||||||||||||||
| 93 | ec_GFp_simple_get_Jprojective_coordinates_GFp, | - | ||||||||||||||||||||||||||||||
| 94 | .point_set_affine_coordinates = | - | ||||||||||||||||||||||||||||||
| 95 | ec_GFp_simple_point_set_affine_coordinates, | - | ||||||||||||||||||||||||||||||
| 96 | .point_get_affine_coordinates = | - | ||||||||||||||||||||||||||||||
| 97 | ec_GFp_simple_point_get_affine_coordinates, | - | ||||||||||||||||||||||||||||||
| 98 | .add = ec_GFp_simple_add, | - | ||||||||||||||||||||||||||||||
| 99 | .dbl = ec_GFp_simple_dbl, | - | ||||||||||||||||||||||||||||||
| 100 | .invert = ec_GFp_simple_invert, | - | ||||||||||||||||||||||||||||||
| 101 | .is_at_infinity = ec_GFp_simple_is_at_infinity, | - | ||||||||||||||||||||||||||||||
| 102 | .is_on_curve = ec_GFp_simple_is_on_curve, | - | ||||||||||||||||||||||||||||||
| 103 | .point_cmp = ec_GFp_simple_cmp, | - | ||||||||||||||||||||||||||||||
| 104 | .make_affine = ec_GFp_simple_make_affine, | - | ||||||||||||||||||||||||||||||
| 105 | .points_make_affine = ec_GFp_simple_points_make_affine, | - | ||||||||||||||||||||||||||||||
| 106 | .mul_generator_ct = ec_GFp_simple_mul_generator_ct, | - | ||||||||||||||||||||||||||||||
| 107 | .mul_single_ct = ec_GFp_simple_mul_single_ct, | - | ||||||||||||||||||||||||||||||
| 108 | .mul_double_nonct = ec_GFp_simple_mul_double_nonct, | - | ||||||||||||||||||||||||||||||
| 109 | .field_mul = ec_GFp_simple_field_mul, | - | ||||||||||||||||||||||||||||||
| 110 | .field_sqr = ec_GFp_simple_field_sqr | - | ||||||||||||||||||||||||||||||
| 111 | }; | - | ||||||||||||||||||||||||||||||
| 112 | - | |||||||||||||||||||||||||||||||
| 113 | return &ret; never executed: return &ret; | 0 | ||||||||||||||||||||||||||||||
| 114 | } | - | ||||||||||||||||||||||||||||||
| 115 | - | |||||||||||||||||||||||||||||||
| 116 | - | |||||||||||||||||||||||||||||||
| 117 | /* Most method functions in this file are designed to work with | - | ||||||||||||||||||||||||||||||
| 118 | * non-trivial representations of field elements if necessary | - | ||||||||||||||||||||||||||||||
| 119 | * (see ecp_mont.c): while standard modular addition and subtraction | - | ||||||||||||||||||||||||||||||
| 120 | * are used, the field_mul and field_sqr methods will be used for | - | ||||||||||||||||||||||||||||||
| 121 | * multiplication, and field_encode and field_decode (if defined) | - | ||||||||||||||||||||||||||||||
| 122 | * will be used for converting between representations. | - | ||||||||||||||||||||||||||||||
| 123 | - | |||||||||||||||||||||||||||||||
| 124 | * Functions ec_GFp_simple_points_make_affine() and | - | ||||||||||||||||||||||||||||||
| 125 | * ec_GFp_simple_point_get_affine_coordinates() specifically assume | - | ||||||||||||||||||||||||||||||
| 126 | * that if a non-trivial representation is used, it is a Montgomery | - | ||||||||||||||||||||||||||||||
| 127 | * representation (i.e. 'encoding' means multiplying by some factor R). | - | ||||||||||||||||||||||||||||||
| 128 | */ | - | ||||||||||||||||||||||||||||||
| 129 | - | |||||||||||||||||||||||||||||||
| 130 | - | |||||||||||||||||||||||||||||||
| 131 | int | - | ||||||||||||||||||||||||||||||
| 132 | ec_GFp_simple_group_init(EC_GROUP * group) | - | ||||||||||||||||||||||||||||||
| 133 | { | - | ||||||||||||||||||||||||||||||
| 134 | BN_init(&group->field); | - | ||||||||||||||||||||||||||||||
| 135 | BN_init(&group->a); | - | ||||||||||||||||||||||||||||||
| 136 | BN_init(&group->b); | - | ||||||||||||||||||||||||||||||
| 137 | group->a_is_minus3 = 0; | - | ||||||||||||||||||||||||||||||
| 138 | return 1; executed 393 times by 4 tests: return 1;Executed by:
| 393 | ||||||||||||||||||||||||||||||
| 139 | } | - | ||||||||||||||||||||||||||||||
| 140 | - | |||||||||||||||||||||||||||||||
| 141 | - | |||||||||||||||||||||||||||||||
| 142 | void | - | ||||||||||||||||||||||||||||||
| 143 | ec_GFp_simple_group_finish(EC_GROUP * group) | - | ||||||||||||||||||||||||||||||
| 144 | { | - | ||||||||||||||||||||||||||||||
| 145 | BN_free(&group->field); | - | ||||||||||||||||||||||||||||||
| 146 | BN_free(&group->a); | - | ||||||||||||||||||||||||||||||
| 147 | BN_free(&group->b); | - | ||||||||||||||||||||||||||||||
| 148 | } executed 393 times by 4 tests: end of blockExecuted by:
| 393 | ||||||||||||||||||||||||||||||
| 149 | - | |||||||||||||||||||||||||||||||
| 150 | - | |||||||||||||||||||||||||||||||
| 151 | void | - | ||||||||||||||||||||||||||||||
| 152 | ec_GFp_simple_group_clear_finish(EC_GROUP * group) | - | ||||||||||||||||||||||||||||||
| 153 | { | - | ||||||||||||||||||||||||||||||
| 154 | BN_clear_free(&group->field); | - | ||||||||||||||||||||||||||||||
| 155 | BN_clear_free(&group->a); | - | ||||||||||||||||||||||||||||||
| 156 | BN_clear_free(&group->b); | - | ||||||||||||||||||||||||||||||
| 157 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 158 | - | |||||||||||||||||||||||||||||||
| 159 | - | |||||||||||||||||||||||||||||||
| 160 | int | - | ||||||||||||||||||||||||||||||
| 161 | ec_GFp_simple_group_copy(EC_GROUP * dest, const EC_GROUP * src) | - | ||||||||||||||||||||||||||||||
| 162 | { | - | ||||||||||||||||||||||||||||||
| 163 | if (!BN_copy(&dest->field, &src->field))
| 0-143 | ||||||||||||||||||||||||||||||
| 164 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 165 | if (!BN_copy(&dest->a, &src->a))
| 0-143 | ||||||||||||||||||||||||||||||
| 166 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 167 | if (!BN_copy(&dest->b, &src->b))
| 0-143 | ||||||||||||||||||||||||||||||
| 168 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 169 | - | |||||||||||||||||||||||||||||||
| 170 | dest->a_is_minus3 = src->a_is_minus3; | - | ||||||||||||||||||||||||||||||
| 171 | - | |||||||||||||||||||||||||||||||
| 172 | return 1; executed 143 times by 3 tests: return 1;Executed by:
| 143 | ||||||||||||||||||||||||||||||
| 173 | } | - | ||||||||||||||||||||||||||||||
| 174 | - | |||||||||||||||||||||||||||||||
| 175 | - | |||||||||||||||||||||||||||||||
| 176 | int | - | ||||||||||||||||||||||||||||||
| 177 | ec_GFp_simple_group_set_curve(EC_GROUP * group, | - | ||||||||||||||||||||||||||||||
| 178 | const BIGNUM * p, const BIGNUM * a, const BIGNUM * b, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 179 | { | - | ||||||||||||||||||||||||||||||
| 180 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 181 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 182 | BIGNUM *tmp_a; | - | ||||||||||||||||||||||||||||||
| 183 | - | |||||||||||||||||||||||||||||||
| 184 | /* p must be a prime > 3 */ | - | ||||||||||||||||||||||||||||||
| 185 | if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
| 0-256 | ||||||||||||||||||||||||||||||
| 186 | ECerror(EC_R_INVALID_FIELD); | - | ||||||||||||||||||||||||||||||
| 187 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 188 | } | - | ||||||||||||||||||||||||||||||
| 189 | if (ctx == NULL) {
| 0-256 | ||||||||||||||||||||||||||||||
| 190 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 191 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 192 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 193 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 194 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 195 | if ((tmp_a = BN_CTX_get(ctx)) == NULL)
| 0-256 | ||||||||||||||||||||||||||||||
| 196 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 197 | - | |||||||||||||||||||||||||||||||
| 198 | /* group->field */ | - | ||||||||||||||||||||||||||||||
| 199 | if (!BN_copy(&group->field, p))
| 0-256 | ||||||||||||||||||||||||||||||
| 200 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 201 | BN_set_negative(&group->field, 0); | - | ||||||||||||||||||||||||||||||
| 202 | - | |||||||||||||||||||||||||||||||
| 203 | /* group->a */ | - | ||||||||||||||||||||||||||||||
| 204 | if (!BN_nnmod(tmp_a, a, p, ctx))
| 0-256 | ||||||||||||||||||||||||||||||
| 205 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 206 | if (group->meth->field_encode) {
| 0-256 | ||||||||||||||||||||||||||||||
| 207 | if (!group->meth->field_encode(group, &group->a, tmp_a, ctx))
| 0-256 | ||||||||||||||||||||||||||||||
| 208 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 209 | } else if (!BN_copy(&group->a, tmp_a)) executed 256 times by 4 tests: end of blockExecuted by:
| 0-256 | ||||||||||||||||||||||||||||||
| 210 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 211 | - | |||||||||||||||||||||||||||||||
| 212 | /* group->b */ | - | ||||||||||||||||||||||||||||||
| 213 | if (!BN_nnmod(&group->b, b, p, ctx))
| 0-256 | ||||||||||||||||||||||||||||||
| 214 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 215 | if (group->meth->field_encode)
| 0-256 | ||||||||||||||||||||||||||||||
| 216 | if (!group->meth->field_encode(group, &group->b, &group->b, ctx))
| 0-256 | ||||||||||||||||||||||||||||||
| 217 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 218 | - | |||||||||||||||||||||||||||||||
| 219 | /* group->a_is_minus3 */ | - | ||||||||||||||||||||||||||||||
| 220 | if (!BN_add_word(tmp_a, 3))
| 0-256 | ||||||||||||||||||||||||||||||
| 221 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 222 | group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field)); | - | ||||||||||||||||||||||||||||||
| 223 | - | |||||||||||||||||||||||||||||||
| 224 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 225 | - | |||||||||||||||||||||||||||||||
| 226 | err: code before this statement executed 256 times by 4 tests: err:Executed by:
| 256 | ||||||||||||||||||||||||||||||
| 227 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 228 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 229 | return ret; executed 256 times by 4 tests: return ret;Executed by:
| 256 | ||||||||||||||||||||||||||||||
| 230 | } | - | ||||||||||||||||||||||||||||||
| 231 | - | |||||||||||||||||||||||||||||||
| 232 | - | |||||||||||||||||||||||||||||||
| 233 | int | - | ||||||||||||||||||||||||||||||
| 234 | ec_GFp_simple_group_get_curve(const EC_GROUP * group, BIGNUM * p, BIGNUM * a, BIGNUM * b, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 235 | { | - | ||||||||||||||||||||||||||||||
| 236 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 237 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 238 | - | |||||||||||||||||||||||||||||||
| 239 | if (p != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 240 | if (!BN_copy(p, &group->field))
| 0-1 | ||||||||||||||||||||||||||||||
| 241 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 242 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 243 | if (a != NULL || b != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 244 | if (group->meth->field_decode) {
| 0-1 | ||||||||||||||||||||||||||||||
| 245 | if (ctx == NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 246 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 247 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 248 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 249 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 250 | if (a != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 251 | if (!group->meth->field_decode(group, a, &group->a, ctx))
| 0-1 | ||||||||||||||||||||||||||||||
| 252 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 253 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 254 | if (b != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 255 | if (!group->meth->field_decode(group, b, &group->b, ctx))
| 0-1 | ||||||||||||||||||||||||||||||
| 256 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 257 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 258 | } else { executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 259 | if (a != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 260 | if (!BN_copy(a, &group->a))
| 0 | ||||||||||||||||||||||||||||||
| 261 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 262 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 263 | if (b != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 264 | if (!BN_copy(b, &group->b))
| 0 | ||||||||||||||||||||||||||||||
| 265 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 266 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 267 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 268 | } | - | ||||||||||||||||||||||||||||||
| 269 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 270 | - | |||||||||||||||||||||||||||||||
| 271 | err: code before this statement executed 1 time by 1 test: err:Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 272 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 273 | return ret; executed 1 time by 1 test: return ret;Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 274 | } | - | ||||||||||||||||||||||||||||||
| 275 | - | |||||||||||||||||||||||||||||||
| 276 | - | |||||||||||||||||||||||||||||||
| 277 | int | - | ||||||||||||||||||||||||||||||
| 278 | ec_GFp_simple_group_get_degree(const EC_GROUP * group) | - | ||||||||||||||||||||||||||||||
| 279 | { | - | ||||||||||||||||||||||||||||||
| 280 | return BN_num_bits(&group->field); executed 165 times by 4 tests: return BN_num_bits(&group->field);Executed by:
| 165 | ||||||||||||||||||||||||||||||
| 281 | } | - | ||||||||||||||||||||||||||||||
| 282 | - | |||||||||||||||||||||||||||||||
| 283 | - | |||||||||||||||||||||||||||||||
| 284 | int | - | ||||||||||||||||||||||||||||||
| 285 | ec_GFp_simple_group_check_discriminant(const EC_GROUP * group, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 286 | { | - | ||||||||||||||||||||||||||||||
| 287 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 288 | BIGNUM *a, *b, *order, *tmp_1, *tmp_2; | - | ||||||||||||||||||||||||||||||
| 289 | const BIGNUM *p = &group->field; | - | ||||||||||||||||||||||||||||||
| 290 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 291 | - | |||||||||||||||||||||||||||||||
| 292 | if (ctx == NULL) {
| 0-48 | ||||||||||||||||||||||||||||||
| 293 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 294 | if (ctx == NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 295 | ECerror(ERR_R_MALLOC_FAILURE); | - | ||||||||||||||||||||||||||||||
| 296 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 297 | } | - | ||||||||||||||||||||||||||||||
| 298 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 299 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 300 | if ((a = BN_CTX_get(ctx)) == NULL)
| 0-48 | ||||||||||||||||||||||||||||||
| 301 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 302 | if ((b = BN_CTX_get(ctx)) == NULL)
| 0-48 | ||||||||||||||||||||||||||||||
| 303 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 304 | if ((tmp_1 = BN_CTX_get(ctx)) == NULL)
| 0-48 | ||||||||||||||||||||||||||||||
| 305 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 306 | if ((tmp_2 = BN_CTX_get(ctx)) == NULL)
| 0-48 | ||||||||||||||||||||||||||||||
| 307 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 308 | if ((order = BN_CTX_get(ctx)) == NULL)
| 0-48 | ||||||||||||||||||||||||||||||
| 309 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 310 | - | |||||||||||||||||||||||||||||||
| 311 | if (group->meth->field_decode) {
| 0-48 | ||||||||||||||||||||||||||||||
| 312 | if (!group->meth->field_decode(group, a, &group->a, ctx))
| 0-48 | ||||||||||||||||||||||||||||||
| 313 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 314 | if (!group->meth->field_decode(group, b, &group->b, ctx))
| 0-48 | ||||||||||||||||||||||||||||||
| 315 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 316 | } else { executed 48 times by 1 test: end of blockExecuted by:
| 48 | ||||||||||||||||||||||||||||||
| 317 | if (!BN_copy(a, &group->a))
| 0 | ||||||||||||||||||||||||||||||
| 318 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 319 | if (!BN_copy(b, &group->b))
| 0 | ||||||||||||||||||||||||||||||
| 320 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 321 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 322 | - | |||||||||||||||||||||||||||||||
| 323 | /* | - | ||||||||||||||||||||||||||||||
| 324 | * check the discriminant: y^2 = x^3 + a*x + b is an elliptic curve | - | ||||||||||||||||||||||||||||||
| 325 | * <=> 4*a^3 + 27*b^2 != 0 (mod p) 0 =< a, b < p | - | ||||||||||||||||||||||||||||||
| 326 | */ | - | ||||||||||||||||||||||||||||||
| 327 | if (BN_is_zero(a)) {
| 6-42 | ||||||||||||||||||||||||||||||
| 328 | if (BN_is_zero(b))
| 0-6 | ||||||||||||||||||||||||||||||
| 329 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 330 | } else if (!BN_is_zero(b)) { executed 6 times by 1 test: end of blockExecuted by:
| 0-42 | ||||||||||||||||||||||||||||||
| 331 | if (!BN_mod_sqr(tmp_1, a, p, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 332 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 333 | if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 334 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 335 | if (!BN_lshift(tmp_1, tmp_2, 2))
| 0-42 | ||||||||||||||||||||||||||||||
| 336 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 337 | /* tmp_1 = 4*a^3 */ | - | ||||||||||||||||||||||||||||||
| 338 | - | |||||||||||||||||||||||||||||||
| 339 | if (!BN_mod_sqr(tmp_2, b, p, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 340 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 341 | if (!BN_mul_word(tmp_2, 27))
| 0-42 | ||||||||||||||||||||||||||||||
| 342 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 343 | /* tmp_2 = 27*b^2 */ | - | ||||||||||||||||||||||||||||||
| 344 | - | |||||||||||||||||||||||||||||||
| 345 | if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 346 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 347 | if (BN_is_zero(a))
| 0-42 | ||||||||||||||||||||||||||||||
| 348 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 349 | } executed 42 times by 1 test: end of blockExecuted by:
| 42 | ||||||||||||||||||||||||||||||
| 350 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 351 | - | |||||||||||||||||||||||||||||||
| 352 | err: code before this statement executed 48 times by 1 test: err:Executed by:
| 48 | ||||||||||||||||||||||||||||||
| 353 | if (ctx != NULL)
| 0-48 | ||||||||||||||||||||||||||||||
| 354 | BN_CTX_end(ctx); executed 48 times by 1 test: BN_CTX_end(ctx);Executed by:
| 48 | ||||||||||||||||||||||||||||||
| 355 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 356 | return ret; executed 48 times by 1 test: return ret;Executed by:
| 48 | ||||||||||||||||||||||||||||||
| 357 | } | - | ||||||||||||||||||||||||||||||
| 358 | - | |||||||||||||||||||||||||||||||
| 359 | - | |||||||||||||||||||||||||||||||
| 360 | int | - | ||||||||||||||||||||||||||||||
| 361 | ec_GFp_simple_point_init(EC_POINT * point) | - | ||||||||||||||||||||||||||||||
| 362 | { | - | ||||||||||||||||||||||||||||||
| 363 | BN_init(&point->X); | - | ||||||||||||||||||||||||||||||
| 364 | BN_init(&point->Y); | - | ||||||||||||||||||||||||||||||
| 365 | BN_init(&point->Z); | - | ||||||||||||||||||||||||||||||
| 366 | point->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 367 | - | |||||||||||||||||||||||||||||||
| 368 | return 1; executed 3983 times by 4 tests: return 1;Executed by:
| 3983 | ||||||||||||||||||||||||||||||
| 369 | } | - | ||||||||||||||||||||||||||||||
| 370 | - | |||||||||||||||||||||||||||||||
| 371 | - | |||||||||||||||||||||||||||||||
| 372 | void | - | ||||||||||||||||||||||||||||||
| 373 | ec_GFp_simple_point_finish(EC_POINT * point) | - | ||||||||||||||||||||||||||||||
| 374 | { | - | ||||||||||||||||||||||||||||||
| 375 | BN_free(&point->X); | - | ||||||||||||||||||||||||||||||
| 376 | BN_free(&point->Y); | - | ||||||||||||||||||||||||||||||
| 377 | BN_free(&point->Z); | - | ||||||||||||||||||||||||||||||
| 378 | } executed 1903 times by 4 tests: end of blockExecuted by:
| 1903 | ||||||||||||||||||||||||||||||
| 379 | - | |||||||||||||||||||||||||||||||
| 380 | - | |||||||||||||||||||||||||||||||
| 381 | void | - | ||||||||||||||||||||||||||||||
| 382 | ec_GFp_simple_point_clear_finish(EC_POINT * point) | - | ||||||||||||||||||||||||||||||
| 383 | { | - | ||||||||||||||||||||||||||||||
| 384 | BN_clear_free(&point->X); | - | ||||||||||||||||||||||||||||||
| 385 | BN_clear_free(&point->Y); | - | ||||||||||||||||||||||||||||||
| 386 | BN_clear_free(&point->Z); | - | ||||||||||||||||||||||||||||||
| 387 | point->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 388 | } executed 2080 times by 1 test: end of blockExecuted by:
| 2080 | ||||||||||||||||||||||||||||||
| 389 | - | |||||||||||||||||||||||||||||||
| 390 | - | |||||||||||||||||||||||||||||||
| 391 | int | - | ||||||||||||||||||||||||||||||
| 392 | ec_GFp_simple_point_copy(EC_POINT * dest, const EC_POINT * src) | - | ||||||||||||||||||||||||||||||
| 393 | { | - | ||||||||||||||||||||||||||||||
| 394 | if (!BN_copy(&dest->X, &src->X))
| 0-1890 | ||||||||||||||||||||||||||||||
| 395 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 396 | if (!BN_copy(&dest->Y, &src->Y))
| 0-1890 | ||||||||||||||||||||||||||||||
| 397 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 398 | if (!BN_copy(&dest->Z, &src->Z))
| 0-1890 | ||||||||||||||||||||||||||||||
| 399 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 400 | dest->Z_is_one = src->Z_is_one; | - | ||||||||||||||||||||||||||||||
| 401 | - | |||||||||||||||||||||||||||||||
| 402 | return 1; executed 1890 times by 4 tests: return 1;Executed by:
| 1890 | ||||||||||||||||||||||||||||||
| 403 | } | - | ||||||||||||||||||||||||||||||
| 404 | - | |||||||||||||||||||||||||||||||
| 405 | - | |||||||||||||||||||||||||||||||
| 406 | int | - | ||||||||||||||||||||||||||||||
| 407 | ec_GFp_simple_point_set_to_infinity(const EC_GROUP * group, EC_POINT * point) | - | ||||||||||||||||||||||||||||||
| 408 | { | - | ||||||||||||||||||||||||||||||
| 409 | point->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 410 | BN_zero(&point->Z); | - | ||||||||||||||||||||||||||||||
| 411 | return 1; executed 2 times by 1 test: return 1;Executed by:
| 2 | ||||||||||||||||||||||||||||||
| 412 | } | - | ||||||||||||||||||||||||||||||
| 413 | - | |||||||||||||||||||||||||||||||
| 414 | - | |||||||||||||||||||||||||||||||
| 415 | int | - | ||||||||||||||||||||||||||||||
| 416 | ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP * group, EC_POINT * point, | - | ||||||||||||||||||||||||||||||
| 417 | const BIGNUM * x, const BIGNUM * y, const BIGNUM * z, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 418 | { | - | ||||||||||||||||||||||||||||||
| 419 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 420 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 421 | - | |||||||||||||||||||||||||||||||
| 422 | if (ctx == NULL) {
| 0-305 | ||||||||||||||||||||||||||||||
| 423 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 424 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 425 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 426 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 427 | if (x != NULL) {
| 0-305 | ||||||||||||||||||||||||||||||
| 428 | if (!BN_nnmod(&point->X, x, &group->field, ctx))
| 0-305 | ||||||||||||||||||||||||||||||
| 429 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 430 | if (group->meth->field_encode) {
| 0-305 | ||||||||||||||||||||||||||||||
| 431 | if (!group->meth->field_encode(group, &point->X, &point->X, ctx))
| 0-305 | ||||||||||||||||||||||||||||||
| 432 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 433 | } executed 305 times by 4 tests: end of blockExecuted by:
| 305 | ||||||||||||||||||||||||||||||
| 434 | } executed 305 times by 4 tests: end of blockExecuted by:
| 305 | ||||||||||||||||||||||||||||||
| 435 | if (y != NULL) {
| 0-305 | ||||||||||||||||||||||||||||||
| 436 | if (!BN_nnmod(&point->Y, y, &group->field, ctx))
| 0-305 | ||||||||||||||||||||||||||||||
| 437 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 438 | if (group->meth->field_encode) {
| 0-305 | ||||||||||||||||||||||||||||||
| 439 | if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx))
| 0-305 | ||||||||||||||||||||||||||||||
| 440 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 441 | } executed 305 times by 4 tests: end of blockExecuted by:
| 305 | ||||||||||||||||||||||||||||||
| 442 | } executed 305 times by 4 tests: end of blockExecuted by:
| 305 | ||||||||||||||||||||||||||||||
| 443 | if (z != NULL) {
| 0-305 | ||||||||||||||||||||||||||||||
| 444 | int Z_is_one; | - | ||||||||||||||||||||||||||||||
| 445 | - | |||||||||||||||||||||||||||||||
| 446 | if (!BN_nnmod(&point->Z, z, &group->field, ctx))
| 0-305 | ||||||||||||||||||||||||||||||
| 447 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 448 | Z_is_one = BN_is_one(&point->Z);
| 0-305 | ||||||||||||||||||||||||||||||
| 449 | if (group->meth->field_encode) {
| 0-305 | ||||||||||||||||||||||||||||||
| 450 | if (Z_is_one && (group->meth->field_set_to_one != 0)) {
| 0-305 | ||||||||||||||||||||||||||||||
| 451 | if (!group->meth->field_set_to_one(group, &point->Z, ctx))
| 0-305 | ||||||||||||||||||||||||||||||
| 452 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 453 | } else { executed 305 times by 4 tests: end of blockExecuted by:
| 305 | ||||||||||||||||||||||||||||||
| 454 | if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx))
| 0 | ||||||||||||||||||||||||||||||
| 455 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 456 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 457 | } | - | ||||||||||||||||||||||||||||||
| 458 | point->Z_is_one = Z_is_one; | - | ||||||||||||||||||||||||||||||
| 459 | } executed 305 times by 4 tests: end of blockExecuted by:
| 305 | ||||||||||||||||||||||||||||||
| 460 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 461 | - | |||||||||||||||||||||||||||||||
| 462 | err: code before this statement executed 305 times by 4 tests: err:Executed by:
| 305 | ||||||||||||||||||||||||||||||
| 463 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 464 | return ret; executed 305 times by 4 tests: return ret;Executed by:
| 305 | ||||||||||||||||||||||||||||||
| 465 | } | - | ||||||||||||||||||||||||||||||
| 466 | - | |||||||||||||||||||||||||||||||
| 467 | - | |||||||||||||||||||||||||||||||
| 468 | int | - | ||||||||||||||||||||||||||||||
| 469 | ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP * group, const EC_POINT * point, | - | ||||||||||||||||||||||||||||||
| 470 | BIGNUM * x, BIGNUM * y, BIGNUM * z, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 471 | { | - | ||||||||||||||||||||||||||||||
| 472 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 473 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 474 | - | |||||||||||||||||||||||||||||||
| 475 | if (group->meth->field_decode != 0) {
| 0-1 | ||||||||||||||||||||||||||||||
| 476 | if (ctx == NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 477 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 478 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 479 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 480 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 481 | if (x != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 482 | if (!group->meth->field_decode(group, x, &point->X, ctx))
| 0-1 | ||||||||||||||||||||||||||||||
| 483 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 484 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 485 | if (y != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 486 | if (!group->meth->field_decode(group, y, &point->Y, ctx))
| 0-1 | ||||||||||||||||||||||||||||||
| 487 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 488 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 489 | if (z != NULL) {
| 0-1 | ||||||||||||||||||||||||||||||
| 490 | if (!group->meth->field_decode(group, z, &point->Z, ctx))
| 0-1 | ||||||||||||||||||||||||||||||
| 491 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 492 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 493 | } else { executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||||||||
| 494 | if (x != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 495 | if (!BN_copy(x, &point->X))
| 0 | ||||||||||||||||||||||||||||||
| 496 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 497 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 498 | if (y != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 499 | if (!BN_copy(y, &point->Y))
| 0 | ||||||||||||||||||||||||||||||
| 500 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 501 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 502 | if (z != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 503 | if (!BN_copy(z, &point->Z))
| 0 | ||||||||||||||||||||||||||||||
| 504 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 505 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 506 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 507 | - | |||||||||||||||||||||||||||||||
| 508 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 509 | - | |||||||||||||||||||||||||||||||
| 510 | err: code before this statement executed 1 time by 1 test: err:Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 511 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 512 | return ret; executed 1 time by 1 test: return ret;Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 513 | } | - | ||||||||||||||||||||||||||||||
| 514 | - | |||||||||||||||||||||||||||||||
| 515 | - | |||||||||||||||||||||||||||||||
| 516 | int | - | ||||||||||||||||||||||||||||||
| 517 | ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP * group, EC_POINT * point, | - | ||||||||||||||||||||||||||||||
| 518 | const BIGNUM * x, const BIGNUM * y, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 519 | { | - | ||||||||||||||||||||||||||||||
| 520 | if (x == NULL || y == NULL) {
| 0-305 | ||||||||||||||||||||||||||||||
| 521 | /* unlike for projective coordinates, we do not tolerate this */ | - | ||||||||||||||||||||||||||||||
| 522 | ECerror(ERR_R_PASSED_NULL_PARAMETER); | - | ||||||||||||||||||||||||||||||
| 523 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 524 | } | - | ||||||||||||||||||||||||||||||
| 525 | return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx); executed 305 times by 4 tests: return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx);Executed by:
| 305 | ||||||||||||||||||||||||||||||
| 526 | } | - | ||||||||||||||||||||||||||||||
| 527 | - | |||||||||||||||||||||||||||||||
| 528 | - | |||||||||||||||||||||||||||||||
| 529 | int | - | ||||||||||||||||||||||||||||||
| 530 | ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP * group, const EC_POINT * point, | - | ||||||||||||||||||||||||||||||
| 531 | BIGNUM * x, BIGNUM * y, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 532 | { | - | ||||||||||||||||||||||||||||||
| 533 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 534 | BIGNUM *Z, *Z_1, *Z_2, *Z_3; | - | ||||||||||||||||||||||||||||||
| 535 | const BIGNUM *Z_; | - | ||||||||||||||||||||||||||||||
| 536 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 537 | - | |||||||||||||||||||||||||||||||
| 538 | if (EC_POINT_is_at_infinity(group, point) > 0) {
| 0-385 | ||||||||||||||||||||||||||||||
| 539 | ECerror(EC_R_POINT_AT_INFINITY); | - | ||||||||||||||||||||||||||||||
| 540 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 541 | } | - | ||||||||||||||||||||||||||||||
| 542 | if (ctx == NULL) {
| 0-385 | ||||||||||||||||||||||||||||||
| 543 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 544 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 545 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 546 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 547 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 548 | if ((Z = BN_CTX_get(ctx)) == NULL)
| 0-385 | ||||||||||||||||||||||||||||||
| 549 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 550 | if ((Z_1 = BN_CTX_get(ctx)) == NULL)
| 0-385 | ||||||||||||||||||||||||||||||
| 551 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 552 | if ((Z_2 = BN_CTX_get(ctx)) == NULL)
| 0-385 | ||||||||||||||||||||||||||||||
| 553 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 554 | if ((Z_3 = BN_CTX_get(ctx)) == NULL)
| 0-385 | ||||||||||||||||||||||||||||||
| 555 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 556 | - | |||||||||||||||||||||||||||||||
| 557 | /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */ | - | ||||||||||||||||||||||||||||||
| 558 | - | |||||||||||||||||||||||||||||||
| 559 | if (group->meth->field_decode) {
| 0-385 | ||||||||||||||||||||||||||||||
| 560 | if (!group->meth->field_decode(group, Z, &point->Z, ctx))
| 0-385 | ||||||||||||||||||||||||||||||
| 561 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 562 | Z_ = Z; | - | ||||||||||||||||||||||||||||||
| 563 | } else { executed 385 times by 4 tests: end of blockExecuted by:
| 385 | ||||||||||||||||||||||||||||||
| 564 | Z_ = &point->Z; | - | ||||||||||||||||||||||||||||||
| 565 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 566 | - | |||||||||||||||||||||||||||||||
| 567 | if (BN_is_one(Z_)) {
| 0-375 | ||||||||||||||||||||||||||||||
| 568 | if (group->meth->field_decode) {
| 0-10 | ||||||||||||||||||||||||||||||
| 569 | if (x != NULL) {
| 0-10 | ||||||||||||||||||||||||||||||
| 570 | if (!group->meth->field_decode(group, x, &point->X, ctx))
| 0-10 | ||||||||||||||||||||||||||||||
| 571 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 572 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||||||||||||||||||||||||||
| 573 | if (y != NULL) {
| 0-10 | ||||||||||||||||||||||||||||||
| 574 | if (!group->meth->field_decode(group, y, &point->Y, ctx))
| 0-10 | ||||||||||||||||||||||||||||||
| 575 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 576 | } executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||||||||||||||||||||||||||
| 577 | } else { executed 10 times by 1 test: end of blockExecuted by:
| 10 | ||||||||||||||||||||||||||||||
| 578 | if (x != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 579 | if (!BN_copy(x, &point->X))
| 0 | ||||||||||||||||||||||||||||||
| 580 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 581 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 582 | if (y != NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 583 | if (!BN_copy(y, &point->Y))
| 0 | ||||||||||||||||||||||||||||||
| 584 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 585 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 586 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 587 | } else { | - | ||||||||||||||||||||||||||||||
| 588 | if (!BN_mod_inverse_ct(Z_1, Z_, &group->field, ctx)) {
| 0-375 | ||||||||||||||||||||||||||||||
| 589 | ECerror(ERR_R_BN_LIB); | - | ||||||||||||||||||||||||||||||
| 590 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 591 | } | - | ||||||||||||||||||||||||||||||
| 592 | if (group->meth->field_encode == 0) {
| 0-375 | ||||||||||||||||||||||||||||||
| 593 | /* field_sqr works on standard representation */ | - | ||||||||||||||||||||||||||||||
| 594 | if (!group->meth->field_sqr(group, Z_2, Z_1, ctx))
| 0 | ||||||||||||||||||||||||||||||
| 595 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 596 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 597 | if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx))
| 0-375 | ||||||||||||||||||||||||||||||
| 598 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 599 | } executed 375 times by 4 tests: end of blockExecuted by:
| 375 | ||||||||||||||||||||||||||||||
| 600 | - | |||||||||||||||||||||||||||||||
| 601 | if (x != NULL) {
| 0-375 | ||||||||||||||||||||||||||||||
| 602 | /* | - | ||||||||||||||||||||||||||||||
| 603 | * in the Montgomery case, field_mul will cancel out | - | ||||||||||||||||||||||||||||||
| 604 | * Montgomery factor in X: | - | ||||||||||||||||||||||||||||||
| 605 | */ | - | ||||||||||||||||||||||||||||||
| 606 | if (!group->meth->field_mul(group, x, &point->X, Z_2, ctx))
| 0-375 | ||||||||||||||||||||||||||||||
| 607 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 608 | } executed 375 times by 4 tests: end of blockExecuted by:
| 375 | ||||||||||||||||||||||||||||||
| 609 | if (y != NULL) {
| 123-252 | ||||||||||||||||||||||||||||||
| 610 | if (group->meth->field_encode == 0) {
| 0-123 | ||||||||||||||||||||||||||||||
| 611 | /* field_mul works on standard representation */ | - | ||||||||||||||||||||||||||||||
| 612 | if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx))
| 0 | ||||||||||||||||||||||||||||||
| 613 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 614 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 615 | if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx))
| 0-123 | ||||||||||||||||||||||||||||||
| 616 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 617 | } executed 123 times by 3 tests: end of blockExecuted by:
| 123 | ||||||||||||||||||||||||||||||
| 618 | - | |||||||||||||||||||||||||||||||
| 619 | /* | - | ||||||||||||||||||||||||||||||
| 620 | * in the Montgomery case, field_mul will cancel out | - | ||||||||||||||||||||||||||||||
| 621 | * Montgomery factor in Y: | - | ||||||||||||||||||||||||||||||
| 622 | */ | - | ||||||||||||||||||||||||||||||
| 623 | if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx))
| 0-123 | ||||||||||||||||||||||||||||||
| 624 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 625 | } executed 123 times by 3 tests: end of blockExecuted by:
| 123 | ||||||||||||||||||||||||||||||
| 626 | } executed 375 times by 4 tests: end of blockExecuted by:
| 375 | ||||||||||||||||||||||||||||||
| 627 | - | |||||||||||||||||||||||||||||||
| 628 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 629 | - | |||||||||||||||||||||||||||||||
| 630 | err: code before this statement executed 385 times by 4 tests: err:Executed by:
| 385 | ||||||||||||||||||||||||||||||
| 631 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 632 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 633 | return ret; executed 385 times by 4 tests: return ret;Executed by:
| 385 | ||||||||||||||||||||||||||||||
| 634 | } | - | ||||||||||||||||||||||||||||||
| 635 | - | |||||||||||||||||||||||||||||||
| 636 | int | - | ||||||||||||||||||||||||||||||
| 637 | ec_GFp_simple_add(const EC_GROUP * group, EC_POINT * r, const EC_POINT * a, const EC_POINT * b, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 638 | { | - | ||||||||||||||||||||||||||||||
| 639 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 640 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 641 | const BIGNUM *p; | - | ||||||||||||||||||||||||||||||
| 642 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 643 | BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6; | - | ||||||||||||||||||||||||||||||
| 644 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 645 | - | |||||||||||||||||||||||||||||||
| 646 | if (a == b)
| 0-134510 | ||||||||||||||||||||||||||||||
| 647 | return EC_POINT_dbl(group, r, a, ctx); never executed: return EC_POINT_dbl(group, r, a, ctx); | 0 | ||||||||||||||||||||||||||||||
| 648 | if (EC_POINT_is_at_infinity(group, a) > 0)
| 5316-129194 | ||||||||||||||||||||||||||||||
| 649 | return EC_POINT_copy(r, b); executed 5316 times by 2 tests: return EC_POINT_copy(r, b);Executed by:
| 5316 | ||||||||||||||||||||||||||||||
| 650 | if (EC_POINT_is_at_infinity(group, b) > 0)
| 0-129194 | ||||||||||||||||||||||||||||||
| 651 | return EC_POINT_copy(r, a); never executed: return EC_POINT_copy(r, a); | 0 | ||||||||||||||||||||||||||||||
| 652 | - | |||||||||||||||||||||||||||||||
| 653 | field_mul = group->meth->field_mul; | - | ||||||||||||||||||||||||||||||
| 654 | field_sqr = group->meth->field_sqr; | - | ||||||||||||||||||||||||||||||
| 655 | p = &group->field; | - | ||||||||||||||||||||||||||||||
| 656 | - | |||||||||||||||||||||||||||||||
| 657 | if (ctx == NULL) {
| 0-129194 | ||||||||||||||||||||||||||||||
| 658 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 659 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 660 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 661 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 662 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 663 | if ((n0 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 664 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 665 | if ((n1 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 666 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 667 | if ((n2 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 668 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 669 | if ((n3 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 670 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 671 | if ((n4 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 672 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 673 | if ((n5 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 674 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 675 | if ((n6 = BN_CTX_get(ctx)) == NULL)
| 0-129194 | ||||||||||||||||||||||||||||||
| 676 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 677 | - | |||||||||||||||||||||||||||||||
| 678 | /* | - | ||||||||||||||||||||||||||||||
| 679 | * Note that in this function we must not read components of 'a' or | - | ||||||||||||||||||||||||||||||
| 680 | * 'b' once we have written the corresponding components of 'r'. ('r' | - | ||||||||||||||||||||||||||||||
| 681 | * might be one of 'a' or 'b'.) | - | ||||||||||||||||||||||||||||||
| 682 | */ | - | ||||||||||||||||||||||||||||||
| 683 | - | |||||||||||||||||||||||||||||||
| 684 | /* n1, n2 */ | - | ||||||||||||||||||||||||||||||
| 685 | if (b->Z_is_one) {
| 21377-107817 | ||||||||||||||||||||||||||||||
| 686 | if (!BN_copy(n1, &a->X))
| 0-21377 | ||||||||||||||||||||||||||||||
| 687 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 688 | if (!BN_copy(n2, &a->Y))
| 0-21377 | ||||||||||||||||||||||||||||||
| 689 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 690 | /* n1 = X_a */ | - | ||||||||||||||||||||||||||||||
| 691 | /* n2 = Y_a */ | - | ||||||||||||||||||||||||||||||
| 692 | } else { executed 21377 times by 4 tests: end of blockExecuted by:
| 21377 | ||||||||||||||||||||||||||||||
| 693 | if (!field_sqr(group, n0, &b->Z, ctx))
| 0-107817 | ||||||||||||||||||||||||||||||
| 694 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 695 | if (!field_mul(group, n1, &a->X, n0, ctx))
| 0-107817 | ||||||||||||||||||||||||||||||
| 696 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 697 | /* n1 = X_a * Z_b^2 */ | - | ||||||||||||||||||||||||||||||
| 698 | - | |||||||||||||||||||||||||||||||
| 699 | if (!field_mul(group, n0, n0, &b->Z, ctx))
| 0-107817 | ||||||||||||||||||||||||||||||
| 700 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 701 | if (!field_mul(group, n2, &a->Y, n0, ctx))
| 0-107817 | ||||||||||||||||||||||||||||||
| 702 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 703 | /* n2 = Y_a * Z_b^3 */ | - | ||||||||||||||||||||||||||||||
| 704 | } executed 107817 times by 4 tests: end of blockExecuted by:
| 107817 | ||||||||||||||||||||||||||||||
| 705 | - | |||||||||||||||||||||||||||||||
| 706 | /* n3, n4 */ | - | ||||||||||||||||||||||||||||||
| 707 | if (a->Z_is_one) {
| 443-128751 | ||||||||||||||||||||||||||||||
| 708 | if (!BN_copy(n3, &b->X))
| 0-443 | ||||||||||||||||||||||||||||||
| 709 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 710 | if (!BN_copy(n4, &b->Y))
| 0-443 | ||||||||||||||||||||||||||||||
| 711 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 712 | /* n3 = X_b */ | - | ||||||||||||||||||||||||||||||
| 713 | /* n4 = Y_b */ | - | ||||||||||||||||||||||||||||||
| 714 | } else { executed 443 times by 4 tests: end of blockExecuted by:
| 443 | ||||||||||||||||||||||||||||||
| 715 | if (!field_sqr(group, n0, &a->Z, ctx))
| 0-128751 | ||||||||||||||||||||||||||||||
| 716 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 717 | if (!field_mul(group, n3, &b->X, n0, ctx))
| 0-128751 | ||||||||||||||||||||||||||||||
| 718 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 719 | /* n3 = X_b * Z_a^2 */ | - | ||||||||||||||||||||||||||||||
| 720 | - | |||||||||||||||||||||||||||||||
| 721 | if (!field_mul(group, n0, n0, &a->Z, ctx))
| 0-128751 | ||||||||||||||||||||||||||||||
| 722 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 723 | if (!field_mul(group, n4, &b->Y, n0, ctx))
| 0-128751 | ||||||||||||||||||||||||||||||
| 724 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 725 | /* n4 = Y_b * Z_a^3 */ | - | ||||||||||||||||||||||||||||||
| 726 | } executed 128751 times by 4 tests: end of blockExecuted by:
| 128751 | ||||||||||||||||||||||||||||||
| 727 | - | |||||||||||||||||||||||||||||||
| 728 | /* n5, n6 */ | - | ||||||||||||||||||||||||||||||
| 729 | if (!BN_mod_sub_quick(n5, n1, n3, p))
| 0-129194 | ||||||||||||||||||||||||||||||
| 730 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 731 | if (!BN_mod_sub_quick(n6, n2, n4, p))
| 0-129194 | ||||||||||||||||||||||||||||||
| 732 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 733 | /* n5 = n1 - n3 */ | - | ||||||||||||||||||||||||||||||
| 734 | /* n6 = n2 - n4 */ | - | ||||||||||||||||||||||||||||||
| 735 | - | |||||||||||||||||||||||||||||||
| 736 | if (BN_is_zero(n5)) {
| 106-129088 | ||||||||||||||||||||||||||||||
| 737 | if (BN_is_zero(n6)) {
| 1-105 | ||||||||||||||||||||||||||||||
| 738 | /* a is the same point as b */ | - | ||||||||||||||||||||||||||||||
| 739 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 740 | ret = EC_POINT_dbl(group, r, a, ctx); | - | ||||||||||||||||||||||||||||||
| 741 | ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 742 | goto end; executed 1 time by 1 test: goto end;Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 743 | } else { | - | ||||||||||||||||||||||||||||||
| 744 | /* a is the inverse of b */ | - | ||||||||||||||||||||||||||||||
| 745 | BN_zero(&r->Z); | - | ||||||||||||||||||||||||||||||
| 746 | r->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 747 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 748 | goto end; executed 105 times by 2 tests: goto end;Executed by:
| 105 | ||||||||||||||||||||||||||||||
| 749 | } | - | ||||||||||||||||||||||||||||||
| 750 | } | - | ||||||||||||||||||||||||||||||
| 751 | /* 'n7', 'n8' */ | - | ||||||||||||||||||||||||||||||
| 752 | if (!BN_mod_add_quick(n1, n1, n3, p))
| 0-129088 | ||||||||||||||||||||||||||||||
| 753 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 754 | if (!BN_mod_add_quick(n2, n2, n4, p))
| 0-129088 | ||||||||||||||||||||||||||||||
| 755 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 756 | /* 'n7' = n1 + n3 */ | - | ||||||||||||||||||||||||||||||
| 757 | /* 'n8' = n2 + n4 */ | - | ||||||||||||||||||||||||||||||
| 758 | - | |||||||||||||||||||||||||||||||
| 759 | /* Z_r */ | - | ||||||||||||||||||||||||||||||
| 760 | if (a->Z_is_one && b->Z_is_one) {
| 40-128647 | ||||||||||||||||||||||||||||||
| 761 | if (!BN_copy(&r->Z, n5))
| 0-40 | ||||||||||||||||||||||||||||||
| 762 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 763 | } else { executed 40 times by 1 test: end of blockExecuted by:
| 40 | ||||||||||||||||||||||||||||||
| 764 | if (a->Z_is_one) {
| 401-128647 | ||||||||||||||||||||||||||||||
| 765 | if (!BN_copy(n0, &b->Z))
| 0-401 | ||||||||||||||||||||||||||||||
| 766 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 767 | } else if (b->Z_is_one) { executed 401 times by 4 tests: end of blockExecuted by:
| 401-107313 | ||||||||||||||||||||||||||||||
| 768 | if (!BN_copy(n0, &a->Z))
| 0-21334 | ||||||||||||||||||||||||||||||
| 769 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 770 | } else { executed 21334 times by 4 tests: end of blockExecuted by:
| 21334 | ||||||||||||||||||||||||||||||
| 771 | if (!field_mul(group, n0, &a->Z, &b->Z, ctx))
| 0-107313 | ||||||||||||||||||||||||||||||
| 772 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 773 | } executed 107313 times by 4 tests: end of blockExecuted by:
| 107313 | ||||||||||||||||||||||||||||||
| 774 | if (!field_mul(group, &r->Z, n0, n5, ctx))
| 0-129048 | ||||||||||||||||||||||||||||||
| 775 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 776 | } executed 129048 times by 4 tests: end of blockExecuted by:
| 129048 | ||||||||||||||||||||||||||||||
| 777 | r->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 778 | /* Z_r = Z_a * Z_b * n5 */ | - | ||||||||||||||||||||||||||||||
| 779 | - | |||||||||||||||||||||||||||||||
| 780 | /* X_r */ | - | ||||||||||||||||||||||||||||||
| 781 | if (!field_sqr(group, n0, n6, ctx))
| 0-129088 | ||||||||||||||||||||||||||||||
| 782 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 783 | if (!field_sqr(group, n4, n5, ctx))
| 0-129088 | ||||||||||||||||||||||||||||||
| 784 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 785 | if (!field_mul(group, n3, n1, n4, ctx))
| 0-129088 | ||||||||||||||||||||||||||||||
| 786 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 787 | if (!BN_mod_sub_quick(&r->X, n0, n3, p))
| 0-129088 | ||||||||||||||||||||||||||||||
| 788 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 789 | /* X_r = n6^2 - n5^2 * 'n7' */ | - | ||||||||||||||||||||||||||||||
| 790 | - | |||||||||||||||||||||||||||||||
| 791 | /* 'n9' */ | - | ||||||||||||||||||||||||||||||
| 792 | if (!BN_mod_lshift1_quick(n0, &r->X, p))
| 0-129088 | ||||||||||||||||||||||||||||||
| 793 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 794 | if (!BN_mod_sub_quick(n0, n3, n0, p))
| 0-129088 | ||||||||||||||||||||||||||||||
| 795 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 796 | /* n9 = n5^2 * 'n7' - 2 * X_r */ | - | ||||||||||||||||||||||||||||||
| 797 | - | |||||||||||||||||||||||||||||||
| 798 | /* Y_r */ | - | ||||||||||||||||||||||||||||||
| 799 | if (!field_mul(group, n0, n0, n6, ctx))
| 0-129088 | ||||||||||||||||||||||||||||||
| 800 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 801 | if (!field_mul(group, n5, n4, n5, ctx))
| 0-129088 | ||||||||||||||||||||||||||||||
| 802 | goto end; /* now n5 is n5^3 */ never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 803 | if (!field_mul(group, n1, n2, n5, ctx))
| 0-129088 | ||||||||||||||||||||||||||||||
| 804 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 805 | if (!BN_mod_sub_quick(n0, n0, n1, p))
| 0-129088 | ||||||||||||||||||||||||||||||
| 806 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 807 | if (BN_is_odd(n0))
| 0-129088 | ||||||||||||||||||||||||||||||
| 808 | if (!BN_add(n0, n0, p))
| 0-64550 | ||||||||||||||||||||||||||||||
| 809 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 810 | /* now 0 <= n0 < 2*p, and n0 is even */ | - | ||||||||||||||||||||||||||||||
| 811 | if (!BN_rshift1(&r->Y, n0))
| 0-129088 | ||||||||||||||||||||||||||||||
| 812 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 813 | /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */ | - | ||||||||||||||||||||||||||||||
| 814 | - | |||||||||||||||||||||||||||||||
| 815 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 816 | - | |||||||||||||||||||||||||||||||
| 817 | end: code before this statement executed 129088 times by 4 tests: end:Executed by:
| 129088 | ||||||||||||||||||||||||||||||
| 818 | if (ctx) /* otherwise we already called BN_CTX_end */
| 1-129193 | ||||||||||||||||||||||||||||||
| 819 | BN_CTX_end(ctx); executed 129193 times by 4 tests: BN_CTX_end(ctx);Executed by:
| 129193 | ||||||||||||||||||||||||||||||
| 820 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 821 | return ret; executed 129194 times by 4 tests: return ret;Executed by:
| 129194 | ||||||||||||||||||||||||||||||
| 822 | } | - | ||||||||||||||||||||||||||||||
| 823 | - | |||||||||||||||||||||||||||||||
| 824 | - | |||||||||||||||||||||||||||||||
| 825 | int | - | ||||||||||||||||||||||||||||||
| 826 | ec_GFp_simple_dbl(const EC_GROUP * group, EC_POINT * r, const EC_POINT * a, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 827 | { | - | ||||||||||||||||||||||||||||||
| 828 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 829 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 830 | const BIGNUM *p; | - | ||||||||||||||||||||||||||||||
| 831 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 832 | BIGNUM *n0, *n1, *n2, *n3; | - | ||||||||||||||||||||||||||||||
| 833 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 834 | - | |||||||||||||||||||||||||||||||
| 835 | if (EC_POINT_is_at_infinity(group, a) > 0) {
| 5332-163199 | ||||||||||||||||||||||||||||||
| 836 | BN_zero(&r->Z); | - | ||||||||||||||||||||||||||||||
| 837 | r->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 838 | return 1; executed 5332 times by 2 tests: return 1;Executed by:
| 5332 | ||||||||||||||||||||||||||||||
| 839 | } | - | ||||||||||||||||||||||||||||||
| 840 | field_mul = group->meth->field_mul; | - | ||||||||||||||||||||||||||||||
| 841 | field_sqr = group->meth->field_sqr; | - | ||||||||||||||||||||||||||||||
| 842 | p = &group->field; | - | ||||||||||||||||||||||||||||||
| 843 | - | |||||||||||||||||||||||||||||||
| 844 | if (ctx == NULL) {
| 0-163199 | ||||||||||||||||||||||||||||||
| 845 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 846 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 847 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 848 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 849 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 850 | if ((n0 = BN_CTX_get(ctx)) == NULL)
| 0-163199 | ||||||||||||||||||||||||||||||
| 851 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 852 | if ((n1 = BN_CTX_get(ctx)) == NULL)
| 0-163199 | ||||||||||||||||||||||||||||||
| 853 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 854 | if ((n2 = BN_CTX_get(ctx)) == NULL)
| 0-163199 | ||||||||||||||||||||||||||||||
| 855 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 856 | if ((n3 = BN_CTX_get(ctx)) == NULL)
| 0-163199 | ||||||||||||||||||||||||||||||
| 857 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 858 | - | |||||||||||||||||||||||||||||||
| 859 | /* | - | ||||||||||||||||||||||||||||||
| 860 | * Note that in this function we must not read components of 'a' once | - | ||||||||||||||||||||||||||||||
| 861 | * we have written the corresponding components of 'r'. ('r' might | - | ||||||||||||||||||||||||||||||
| 862 | * the same as 'a'.) | - | ||||||||||||||||||||||||||||||
| 863 | */ | - | ||||||||||||||||||||||||||||||
| 864 | - | |||||||||||||||||||||||||||||||
| 865 | /* n1 */ | - | ||||||||||||||||||||||||||||||
| 866 | if (a->Z_is_one) {
| 909-162290 | ||||||||||||||||||||||||||||||
| 867 | if (!field_sqr(group, n0, &a->X, ctx))
| 0-909 | ||||||||||||||||||||||||||||||
| 868 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 869 | if (!BN_mod_lshift1_quick(n1, n0, p))
| 0-909 | ||||||||||||||||||||||||||||||
| 870 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 871 | if (!BN_mod_add_quick(n0, n0, n1, p))
| 0-909 | ||||||||||||||||||||||||||||||
| 872 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 873 | if (!BN_mod_add_quick(n1, n0, &group->a, p))
| 0-909 | ||||||||||||||||||||||||||||||
| 874 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 875 | /* n1 = 3 * X_a^2 + a_curve */ | - | ||||||||||||||||||||||||||||||
| 876 | } else if (group->a_is_minus3) { executed 909 times by 4 tests: end of blockExecuted by:
| 909-121241 | ||||||||||||||||||||||||||||||
| 877 | if (!field_sqr(group, n1, &a->Z, ctx))
| 0-121241 | ||||||||||||||||||||||||||||||
| 878 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 879 | if (!BN_mod_add_quick(n0, &a->X, n1, p))
| 0-121241 | ||||||||||||||||||||||||||||||
| 880 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 881 | if (!BN_mod_sub_quick(n2, &a->X, n1, p))
| 0-121241 | ||||||||||||||||||||||||||||||
| 882 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 883 | if (!field_mul(group, n1, n0, n2, ctx))
| 0-121241 | ||||||||||||||||||||||||||||||
| 884 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 885 | if (!BN_mod_lshift1_quick(n0, n1, p))
| 0-121241 | ||||||||||||||||||||||||||||||
| 886 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 887 | if (!BN_mod_add_quick(n1, n0, n1, p))
| 0-121241 | ||||||||||||||||||||||||||||||
| 888 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 889 | /* | - | ||||||||||||||||||||||||||||||
| 890 | * n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2) = 3 * X_a^2 - 3 * | - | ||||||||||||||||||||||||||||||
| 891 | * Z_a^4 | - | ||||||||||||||||||||||||||||||
| 892 | */ | - | ||||||||||||||||||||||||||||||
| 893 | } else { executed 121241 times by 4 tests: end of blockExecuted by:
| 121241 | ||||||||||||||||||||||||||||||
| 894 | if (!field_sqr(group, n0, &a->X, ctx))
| 0-41049 | ||||||||||||||||||||||||||||||
| 895 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 896 | if (!BN_mod_lshift1_quick(n1, n0, p))
| 0-41049 | ||||||||||||||||||||||||||||||
| 897 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 898 | if (!BN_mod_add_quick(n0, n0, n1, p))
| 0-41049 | ||||||||||||||||||||||||||||||
| 899 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 900 | if (!field_sqr(group, n1, &a->Z, ctx))
| 0-41049 | ||||||||||||||||||||||||||||||
| 901 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 902 | if (!field_sqr(group, n1, n1, ctx))
| 0-41049 | ||||||||||||||||||||||||||||||
| 903 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 904 | if (!field_mul(group, n1, n1, &group->a, ctx))
| 0-41049 | ||||||||||||||||||||||||||||||
| 905 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 906 | if (!BN_mod_add_quick(n1, n1, n0, p))
| 0-41049 | ||||||||||||||||||||||||||||||
| 907 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 908 | /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */ | - | ||||||||||||||||||||||||||||||
| 909 | } executed 41049 times by 3 tests: end of blockExecuted by:
| 41049 | ||||||||||||||||||||||||||||||
| 910 | - | |||||||||||||||||||||||||||||||
| 911 | /* Z_r */ | - | ||||||||||||||||||||||||||||||
| 912 | if (a->Z_is_one) {
| 909-162290 | ||||||||||||||||||||||||||||||
| 913 | if (!BN_copy(n0, &a->Y))
| 0-909 | ||||||||||||||||||||||||||||||
| 914 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 915 | } else { executed 909 times by 4 tests: end of blockExecuted by:
| 909 | ||||||||||||||||||||||||||||||
| 916 | if (!field_mul(group, n0, &a->Y, &a->Z, ctx))
| 0-162290 | ||||||||||||||||||||||||||||||
| 917 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 918 | } executed 162290 times by 4 tests: end of blockExecuted by:
| 162290 | ||||||||||||||||||||||||||||||
| 919 | if (!BN_mod_lshift1_quick(&r->Z, n0, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 920 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 921 | r->Z_is_one = 0; | - | ||||||||||||||||||||||||||||||
| 922 | /* Z_r = 2 * Y_a * Z_a */ | - | ||||||||||||||||||||||||||||||
| 923 | - | |||||||||||||||||||||||||||||||
| 924 | /* n2 */ | - | ||||||||||||||||||||||||||||||
| 925 | if (!field_sqr(group, n3, &a->Y, ctx))
| 0-163199 | ||||||||||||||||||||||||||||||
| 926 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 927 | if (!field_mul(group, n2, &a->X, n3, ctx))
| 0-163199 | ||||||||||||||||||||||||||||||
| 928 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 929 | if (!BN_mod_lshift_quick(n2, n2, 2, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 930 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 931 | /* n2 = 4 * X_a * Y_a^2 */ | - | ||||||||||||||||||||||||||||||
| 932 | - | |||||||||||||||||||||||||||||||
| 933 | /* X_r */ | - | ||||||||||||||||||||||||||||||
| 934 | if (!BN_mod_lshift1_quick(n0, n2, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 935 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 936 | if (!field_sqr(group, &r->X, n1, ctx))
| 0-163199 | ||||||||||||||||||||||||||||||
| 937 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 938 | if (!BN_mod_sub_quick(&r->X, &r->X, n0, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 939 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 940 | /* X_r = n1^2 - 2 * n2 */ | - | ||||||||||||||||||||||||||||||
| 941 | - | |||||||||||||||||||||||||||||||
| 942 | /* n3 */ | - | ||||||||||||||||||||||||||||||
| 943 | if (!field_sqr(group, n0, n3, ctx))
| 0-163199 | ||||||||||||||||||||||||||||||
| 944 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 945 | if (!BN_mod_lshift_quick(n3, n0, 3, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 946 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 947 | /* n3 = 8 * Y_a^4 */ | - | ||||||||||||||||||||||||||||||
| 948 | - | |||||||||||||||||||||||||||||||
| 949 | /* Y_r */ | - | ||||||||||||||||||||||||||||||
| 950 | if (!BN_mod_sub_quick(n0, n2, &r->X, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 951 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 952 | if (!field_mul(group, n0, n1, n0, ctx))
| 0-163199 | ||||||||||||||||||||||||||||||
| 953 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 954 | if (!BN_mod_sub_quick(&r->Y, n0, n3, p))
| 0-163199 | ||||||||||||||||||||||||||||||
| 955 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 956 | /* Y_r = n1 * (n2 - X_r) - n3 */ | - | ||||||||||||||||||||||||||||||
| 957 | - | |||||||||||||||||||||||||||||||
| 958 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 959 | - | |||||||||||||||||||||||||||||||
| 960 | err: code before this statement executed 163199 times by 4 tests: err:Executed by:
| 163199 | ||||||||||||||||||||||||||||||
| 961 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 962 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 963 | return ret; executed 163199 times by 4 tests: return ret;Executed by:
| 163199 | ||||||||||||||||||||||||||||||
| 964 | } | - | ||||||||||||||||||||||||||||||
| 965 | - | |||||||||||||||||||||||||||||||
| 966 | - | |||||||||||||||||||||||||||||||
| 967 | int | - | ||||||||||||||||||||||||||||||
| 968 | ec_GFp_simple_invert(const EC_GROUP * group, EC_POINT * point, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 969 | { | - | ||||||||||||||||||||||||||||||
| 970 | if (EC_POINT_is_at_infinity(group, point) > 0 || BN_is_zero(&point->Y))
| 0-10524 | ||||||||||||||||||||||||||||||
| 971 | /* point is its own inverse */ | - | ||||||||||||||||||||||||||||||
| 972 | return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||||||||
| 973 | - | |||||||||||||||||||||||||||||||
| 974 | return BN_usub(&point->Y, &group->field, &point->Y); executed 10524 times by 2 tests: return BN_usub(&point->Y, &group->field, &point->Y);Executed by:
| 10524 | ||||||||||||||||||||||||||||||
| 975 | } | - | ||||||||||||||||||||||||||||||
| 976 | - | |||||||||||||||||||||||||||||||
| 977 | - | |||||||||||||||||||||||||||||||
| 978 | int | - | ||||||||||||||||||||||||||||||
| 979 | ec_GFp_simple_is_at_infinity(const EC_GROUP * group, const EC_POINT * point) | - | ||||||||||||||||||||||||||||||
| 980 | { | - | ||||||||||||||||||||||||||||||
| 981 | return BN_is_zero(&point->Z); executed 443739 times by 4 tests: return ((&point->Z)->top == 0);Executed by:
| 443739 | ||||||||||||||||||||||||||||||
| 982 | } | - | ||||||||||||||||||||||||||||||
| 983 | - | |||||||||||||||||||||||||||||||
| 984 | - | |||||||||||||||||||||||||||||||
| 985 | int | - | ||||||||||||||||||||||||||||||
| 986 | ec_GFp_simple_is_on_curve(const EC_GROUP * group, const EC_POINT * point, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 987 | { | - | ||||||||||||||||||||||||||||||
| 988 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 989 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 990 | const BIGNUM *p; | - | ||||||||||||||||||||||||||||||
| 991 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 992 | BIGNUM *rh, *tmp, *Z4, *Z6; | - | ||||||||||||||||||||||||||||||
| 993 | int ret = -1; | - | ||||||||||||||||||||||||||||||
| 994 | - | |||||||||||||||||||||||||||||||
| 995 | if (EC_POINT_is_at_infinity(group, point) > 0)
| 0-209 | ||||||||||||||||||||||||||||||
| 996 | return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||||||||
| 997 | - | |||||||||||||||||||||||||||||||
| 998 | field_mul = group->meth->field_mul; | - | ||||||||||||||||||||||||||||||
| 999 | field_sqr = group->meth->field_sqr; | - | ||||||||||||||||||||||||||||||
| 1000 | p = &group->field; | - | ||||||||||||||||||||||||||||||
| 1001 | - | |||||||||||||||||||||||||||||||
| 1002 | if (ctx == NULL) {
| 0-209 | ||||||||||||||||||||||||||||||
| 1003 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 1004 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 1005 | return -1; never executed: return -1; | 0 | ||||||||||||||||||||||||||||||
| 1006 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1007 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 1008 | if ((rh = BN_CTX_get(ctx)) == NULL)
| 0-209 | ||||||||||||||||||||||||||||||
| 1009 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1010 | if ((tmp = BN_CTX_get(ctx)) == NULL)
| 0-209 | ||||||||||||||||||||||||||||||
| 1011 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1012 | if ((Z4 = BN_CTX_get(ctx)) == NULL)
| 0-209 | ||||||||||||||||||||||||||||||
| 1013 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1014 | if ((Z6 = BN_CTX_get(ctx)) == NULL)
| 0-209 | ||||||||||||||||||||||||||||||
| 1015 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1016 | - | |||||||||||||||||||||||||||||||
| 1017 | /* | - | ||||||||||||||||||||||||||||||
| 1018 | * We have a curve defined by a Weierstrass equation y^2 = x^3 + a*x | - | ||||||||||||||||||||||||||||||
| 1019 | * + b. The point to consider is given in Jacobian projective | - | ||||||||||||||||||||||||||||||
| 1020 | * coordinates where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3). | - | ||||||||||||||||||||||||||||||
| 1021 | * Substituting this and multiplying by Z^6 transforms the above | - | ||||||||||||||||||||||||||||||
| 1022 | * equation into Y^2 = X^3 + a*X*Z^4 + b*Z^6. To test this, we add up | - | ||||||||||||||||||||||||||||||
| 1023 | * the right-hand side in 'rh'. | - | ||||||||||||||||||||||||||||||
| 1024 | */ | - | ||||||||||||||||||||||||||||||
| 1025 | - | |||||||||||||||||||||||||||||||
| 1026 | /* rh := X^2 */ | - | ||||||||||||||||||||||||||||||
| 1027 | if (!field_sqr(group, rh, &point->X, ctx))
| 0-209 | ||||||||||||||||||||||||||||||
| 1028 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1029 | - | |||||||||||||||||||||||||||||||
| 1030 | if (!point->Z_is_one) {
| 59-150 | ||||||||||||||||||||||||||||||
| 1031 | if (!field_sqr(group, tmp, &point->Z, ctx))
| 0-59 | ||||||||||||||||||||||||||||||
| 1032 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1033 | if (!field_sqr(group, Z4, tmp, ctx))
| 0-59 | ||||||||||||||||||||||||||||||
| 1034 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1035 | if (!field_mul(group, Z6, Z4, tmp, ctx))
| 0-59 | ||||||||||||||||||||||||||||||
| 1036 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1037 | - | |||||||||||||||||||||||||||||||
| 1038 | /* rh := (rh + a*Z^4)*X */ | - | ||||||||||||||||||||||||||||||
| 1039 | if (group->a_is_minus3) {
| 19-40 | ||||||||||||||||||||||||||||||
| 1040 | if (!BN_mod_lshift1_quick(tmp, Z4, p))
| 0-40 | ||||||||||||||||||||||||||||||
| 1041 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1042 | if (!BN_mod_add_quick(tmp, tmp, Z4, p))
| 0-40 | ||||||||||||||||||||||||||||||
| 1043 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1044 | if (!BN_mod_sub_quick(rh, rh, tmp, p))
| 0-40 | ||||||||||||||||||||||||||||||
| 1045 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1046 | if (!field_mul(group, rh, rh, &point->X, ctx))
| 0-40 | ||||||||||||||||||||||||||||||
| 1047 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1048 | } else { executed 40 times by 3 tests: end of blockExecuted by:
| 40 | ||||||||||||||||||||||||||||||
| 1049 | if (!field_mul(group, tmp, Z4, &group->a, ctx))
| 0-19 | ||||||||||||||||||||||||||||||
| 1050 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1051 | if (!BN_mod_add_quick(rh, rh, tmp, p))
| 0-19 | ||||||||||||||||||||||||||||||
| 1052 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1053 | if (!field_mul(group, rh, rh, &point->X, ctx))
| 0-19 | ||||||||||||||||||||||||||||||
| 1054 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1055 | } executed 19 times by 2 tests: end of blockExecuted by:
| 19 | ||||||||||||||||||||||||||||||
| 1056 | - | |||||||||||||||||||||||||||||||
| 1057 | /* rh := rh + b*Z^6 */ | - | ||||||||||||||||||||||||||||||
| 1058 | if (!field_mul(group, tmp, &group->b, Z6, ctx))
| 0-59 | ||||||||||||||||||||||||||||||
| 1059 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1060 | if (!BN_mod_add_quick(rh, rh, tmp, p))
| 0-59 | ||||||||||||||||||||||||||||||
| 1061 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1062 | } else { executed 59 times by 3 tests: end of blockExecuted by:
| 59 | ||||||||||||||||||||||||||||||
| 1063 | /* point->Z_is_one */ | - | ||||||||||||||||||||||||||||||
| 1064 | - | |||||||||||||||||||||||||||||||
| 1065 | /* rh := (rh + a)*X */ | - | ||||||||||||||||||||||||||||||
| 1066 | if (!BN_mod_add_quick(rh, rh, &group->a, p))
| 0-150 | ||||||||||||||||||||||||||||||
| 1067 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1068 | if (!field_mul(group, rh, rh, &point->X, ctx))
| 0-150 | ||||||||||||||||||||||||||||||
| 1069 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1070 | /* rh := rh + b */ | - | ||||||||||||||||||||||||||||||
| 1071 | if (!BN_mod_add_quick(rh, rh, &group->b, p))
| 0-150 | ||||||||||||||||||||||||||||||
| 1072 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1073 | } executed 150 times by 2 tests: end of blockExecuted by:
| 150 | ||||||||||||||||||||||||||||||
| 1074 | - | |||||||||||||||||||||||||||||||
| 1075 | /* 'lh' := Y^2 */ | - | ||||||||||||||||||||||||||||||
| 1076 | if (!field_sqr(group, tmp, &point->Y, ctx))
| 0-209 | ||||||||||||||||||||||||||||||
| 1077 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1078 | - | |||||||||||||||||||||||||||||||
| 1079 | ret = (0 == BN_ucmp(tmp, rh)); | - | ||||||||||||||||||||||||||||||
| 1080 | - | |||||||||||||||||||||||||||||||
| 1081 | err: code before this statement executed 209 times by 4 tests: err:Executed by:
| 209 | ||||||||||||||||||||||||||||||
| 1082 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 1083 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 1084 | return ret; executed 209 times by 4 tests: return ret;Executed by:
| 209 | ||||||||||||||||||||||||||||||
| 1085 | } | - | ||||||||||||||||||||||||||||||
| 1086 | - | |||||||||||||||||||||||||||||||
| 1087 | - | |||||||||||||||||||||||||||||||
| 1088 | int | - | ||||||||||||||||||||||||||||||
| 1089 | ec_GFp_simple_cmp(const EC_GROUP * group, const EC_POINT * a, const EC_POINT * b, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 1090 | { | - | ||||||||||||||||||||||||||||||
| 1091 | /* | - | ||||||||||||||||||||||||||||||
| 1092 | * return values: -1 error 0 equal (in affine coordinates) 1 | - | ||||||||||||||||||||||||||||||
| 1093 | * not equal | - | ||||||||||||||||||||||||||||||
| 1094 | */ | - | ||||||||||||||||||||||||||||||
| 1095 | - | |||||||||||||||||||||||||||||||
| 1096 | int (*field_mul) (const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 1097 | int (*field_sqr) (const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *); | - | ||||||||||||||||||||||||||||||
| 1098 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 1099 | BIGNUM *tmp1, *tmp2, *Za23, *Zb23; | - | ||||||||||||||||||||||||||||||
| 1100 | const BIGNUM *tmp1_, *tmp2_; | - | ||||||||||||||||||||||||||||||
| 1101 | int ret = -1; | - | ||||||||||||||||||||||||||||||
| 1102 | - | |||||||||||||||||||||||||||||||
| 1103 | if (EC_POINT_is_at_infinity(group, a) > 0) {
| 18-46 | ||||||||||||||||||||||||||||||
| 1104 | return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1; executed 18 times by 1 test: return EC_POINT_is_at_infinity(group, b) > 0 ? 0 : 1;Executed by:
| 0-18 | ||||||||||||||||||||||||||||||
| 1105 | } | - | ||||||||||||||||||||||||||||||
| 1106 | if (EC_POINT_is_at_infinity(group, b) > 0)
| 0-46 | ||||||||||||||||||||||||||||||
| 1107 | return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||||||||
| 1108 | - | |||||||||||||||||||||||||||||||
| 1109 | if (a->Z_is_one && b->Z_is_one) {
| 1-42 | ||||||||||||||||||||||||||||||
| 1110 | return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1; executed 3 times by 1 test: return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;Executed by:
| 0-3 | ||||||||||||||||||||||||||||||
| 1111 | } | - | ||||||||||||||||||||||||||||||
| 1112 | field_mul = group->meth->field_mul; | - | ||||||||||||||||||||||||||||||
| 1113 | field_sqr = group->meth->field_sqr; | - | ||||||||||||||||||||||||||||||
| 1114 | - | |||||||||||||||||||||||||||||||
| 1115 | if (ctx == NULL) {
| 0-43 | ||||||||||||||||||||||||||||||
| 1116 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 1117 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 1118 | return -1; never executed: return -1; | 0 | ||||||||||||||||||||||||||||||
| 1119 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1120 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 1121 | if ((tmp1 = BN_CTX_get(ctx)) == NULL)
| 0-43 | ||||||||||||||||||||||||||||||
| 1122 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1123 | if ((tmp2 = BN_CTX_get(ctx)) == NULL)
| 0-43 | ||||||||||||||||||||||||||||||
| 1124 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1125 | if ((Za23 = BN_CTX_get(ctx)) == NULL)
| 0-43 | ||||||||||||||||||||||||||||||
| 1126 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1127 | if ((Zb23 = BN_CTX_get(ctx)) == NULL)
| 0-43 | ||||||||||||||||||||||||||||||
| 1128 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1129 | - | |||||||||||||||||||||||||||||||
| 1130 | /* | - | ||||||||||||||||||||||||||||||
| 1131 | * We have to decide whether (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, | - | ||||||||||||||||||||||||||||||
| 1132 | * Y_b/Z_b^3), or equivalently, whether (X_a*Z_b^2, Y_a*Z_b^3) = | - | ||||||||||||||||||||||||||||||
| 1133 | * (X_b*Z_a^2, Y_b*Z_a^3). | - | ||||||||||||||||||||||||||||||
| 1134 | */ | - | ||||||||||||||||||||||||||||||
| 1135 | - | |||||||||||||||||||||||||||||||
| 1136 | if (!b->Z_is_one) {
| 0-43 | ||||||||||||||||||||||||||||||
| 1137 | if (!field_sqr(group, Zb23, &b->Z, ctx))
| 0-43 | ||||||||||||||||||||||||||||||
| 1138 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1139 | if (!field_mul(group, tmp1, &a->X, Zb23, ctx))
| 0-43 | ||||||||||||||||||||||||||||||
| 1140 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1141 | tmp1_ = tmp1; | - | ||||||||||||||||||||||||||||||
| 1142 | } else executed 43 times by 2 tests: end of blockExecuted by:
| 43 | ||||||||||||||||||||||||||||||
| 1143 | tmp1_ = &a->X; never executed: tmp1_ = &a->X; | 0 | ||||||||||||||||||||||||||||||
| 1144 | if (!a->Z_is_one) {
| 1-42 | ||||||||||||||||||||||||||||||
| 1145 | if (!field_sqr(group, Za23, &a->Z, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 1146 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1147 | if (!field_mul(group, tmp2, &b->X, Za23, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 1148 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1149 | tmp2_ = tmp2; | - | ||||||||||||||||||||||||||||||
| 1150 | } else executed 42 times by 1 test: end of blockExecuted by:
| 42 | ||||||||||||||||||||||||||||||
| 1151 | tmp2_ = &b->X; executed 1 time by 1 test: tmp2_ = &b->X;Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 1152 | - | |||||||||||||||||||||||||||||||
| 1153 | /* compare X_a*Z_b^2 with X_b*Z_a^2 */ | - | ||||||||||||||||||||||||||||||
| 1154 | if (BN_cmp(tmp1_, tmp2_) != 0) {
| 0-43 | ||||||||||||||||||||||||||||||
| 1155 | ret = 1; /* points differ */ | - | ||||||||||||||||||||||||||||||
| 1156 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1157 | } | - | ||||||||||||||||||||||||||||||
| 1158 | if (!b->Z_is_one) {
| 0-43 | ||||||||||||||||||||||||||||||
| 1159 | if (!field_mul(group, Zb23, Zb23, &b->Z, ctx))
| 0-43 | ||||||||||||||||||||||||||||||
| 1160 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1161 | if (!field_mul(group, tmp1, &a->Y, Zb23, ctx))
| 0-43 | ||||||||||||||||||||||||||||||
| 1162 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1163 | /* tmp1_ = tmp1 */ | - | ||||||||||||||||||||||||||||||
| 1164 | } else executed 43 times by 2 tests: end of blockExecuted by:
| 43 | ||||||||||||||||||||||||||||||
| 1165 | tmp1_ = &a->Y; never executed: tmp1_ = &a->Y; | 0 | ||||||||||||||||||||||||||||||
| 1166 | if (!a->Z_is_one) {
| 1-42 | ||||||||||||||||||||||||||||||
| 1167 | if (!field_mul(group, Za23, Za23, &a->Z, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 1168 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1169 | if (!field_mul(group, tmp2, &b->Y, Za23, ctx))
| 0-42 | ||||||||||||||||||||||||||||||
| 1170 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1171 | /* tmp2_ = tmp2 */ | - | ||||||||||||||||||||||||||||||
| 1172 | } else executed 42 times by 1 test: end of blockExecuted by:
| 42 | ||||||||||||||||||||||||||||||
| 1173 | tmp2_ = &b->Y; executed 1 time by 1 test: tmp2_ = &b->Y;Executed by:
| 1 | ||||||||||||||||||||||||||||||
| 1174 | - | |||||||||||||||||||||||||||||||
| 1175 | /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */ | - | ||||||||||||||||||||||||||||||
| 1176 | if (BN_cmp(tmp1_, tmp2_) != 0) {
| 0-43 | ||||||||||||||||||||||||||||||
| 1177 | ret = 1; /* points differ */ | - | ||||||||||||||||||||||||||||||
| 1178 | goto end; never executed: goto end; | 0 | ||||||||||||||||||||||||||||||
| 1179 | } | - | ||||||||||||||||||||||||||||||
| 1180 | /* points are equal */ | - | ||||||||||||||||||||||||||||||
| 1181 | ret = 0; | - | ||||||||||||||||||||||||||||||
| 1182 | - | |||||||||||||||||||||||||||||||
| 1183 | end: code before this statement executed 43 times by 2 tests: end:Executed by:
| 43 | ||||||||||||||||||||||||||||||
| 1184 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 1185 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 1186 | return ret; executed 43 times by 2 tests: return ret;Executed by:
| 43 | ||||||||||||||||||||||||||||||
| 1187 | } | - | ||||||||||||||||||||||||||||||
| 1188 | - | |||||||||||||||||||||||||||||||
| 1189 | - | |||||||||||||||||||||||||||||||
| 1190 | int | - | ||||||||||||||||||||||||||||||
| 1191 | ec_GFp_simple_make_affine(const EC_GROUP * group, EC_POINT * point, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 1192 | { | - | ||||||||||||||||||||||||||||||
| 1193 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 1194 | BIGNUM *x, *y; | - | ||||||||||||||||||||||||||||||
| 1195 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 1196 | - | |||||||||||||||||||||||||||||||
| 1197 | if (point->Z_is_one || EC_POINT_is_at_infinity(group, point) > 0)
| 0 | ||||||||||||||||||||||||||||||
| 1198 | return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||||||||
| 1199 | - | |||||||||||||||||||||||||||||||
| 1200 | if (ctx == NULL) {
| 0 | ||||||||||||||||||||||||||||||
| 1201 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 1202 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 1203 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 1204 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1205 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 1206 | if ((x = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 1207 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1208 | if ((y = BN_CTX_get(ctx)) == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 1209 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1210 | - | |||||||||||||||||||||||||||||||
| 1211 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
| 0 | ||||||||||||||||||||||||||||||
| 1212 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1213 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
| 0 | ||||||||||||||||||||||||||||||
| 1214 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1215 | if (!point->Z_is_one) {
| 0 | ||||||||||||||||||||||||||||||
| 1216 | ECerror(ERR_R_INTERNAL_ERROR); | - | ||||||||||||||||||||||||||||||
| 1217 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1218 | } | - | ||||||||||||||||||||||||||||||
| 1219 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 1220 | - | |||||||||||||||||||||||||||||||
| 1221 | err: code before this statement never executed: err: | 0 | ||||||||||||||||||||||||||||||
| 1222 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 1223 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 1224 | return ret; never executed: return ret; | 0 | ||||||||||||||||||||||||||||||
| 1225 | } | - | ||||||||||||||||||||||||||||||
| 1226 | - | |||||||||||||||||||||||||||||||
| 1227 | - | |||||||||||||||||||||||||||||||
| 1228 | int | - | ||||||||||||||||||||||||||||||
| 1229 | ec_GFp_simple_points_make_affine(const EC_GROUP * group, size_t num, EC_POINT * points[], BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 1230 | { | - | ||||||||||||||||||||||||||||||
| 1231 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 1232 | BIGNUM *tmp0, *tmp1; | - | ||||||||||||||||||||||||||||||
| 1233 | size_t pow2 = 0; | - | ||||||||||||||||||||||||||||||
| 1234 | BIGNUM **heap = NULL; | - | ||||||||||||||||||||||||||||||
| 1235 | size_t i; | - | ||||||||||||||||||||||||||||||
| 1236 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 1237 | - | |||||||||||||||||||||||||||||||
| 1238 | if (num == 0)
| 0-210 | ||||||||||||||||||||||||||||||
| 1239 | return 1; never executed: return 1; | 0 | ||||||||||||||||||||||||||||||
| 1240 | - | |||||||||||||||||||||||||||||||
| 1241 | if (ctx == NULL) {
| 0-210 | ||||||||||||||||||||||||||||||
| 1242 | ctx = new_ctx = BN_CTX_new(); | - | ||||||||||||||||||||||||||||||
| 1243 | if (ctx == NULL)
| 0 | ||||||||||||||||||||||||||||||
| 1244 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 1245 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1246 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 1247 | if ((tmp0 = BN_CTX_get(ctx)) == NULL)
| 0-210 | ||||||||||||||||||||||||||||||
| 1248 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1249 | if ((tmp1 = BN_CTX_get(ctx)) == NULL)
| 0-210 | ||||||||||||||||||||||||||||||
| 1250 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1251 | - | |||||||||||||||||||||||||||||||
| 1252 | /* | - | ||||||||||||||||||||||||||||||
| 1253 | * Before converting the individual points, compute inverses of all Z | - | ||||||||||||||||||||||||||||||
| 1254 | * values. Modular inversion is rather slow, but luckily we can do | - | ||||||||||||||||||||||||||||||
| 1255 | * with a single explicit inversion, plus about 3 multiplications per | - | ||||||||||||||||||||||||||||||
| 1256 | * input value. | - | ||||||||||||||||||||||||||||||
| 1257 | */ | - | ||||||||||||||||||||||||||||||
| 1258 | - | |||||||||||||||||||||||||||||||
| 1259 | pow2 = 1; | - | ||||||||||||||||||||||||||||||
| 1260 | while (num > pow2)
| 210-680 | ||||||||||||||||||||||||||||||
| 1261 | pow2 <<= 1; executed 680 times by 1 test: pow2 <<= 1;Executed by:
| 680 | ||||||||||||||||||||||||||||||
| 1262 | /* | - | ||||||||||||||||||||||||||||||
| 1263 | * Now pow2 is the smallest power of 2 satifsying pow2 >= num. We | - | ||||||||||||||||||||||||||||||
| 1264 | * need twice that. | - | ||||||||||||||||||||||||||||||
| 1265 | */ | - | ||||||||||||||||||||||||||||||
| 1266 | pow2 <<= 1; | - | ||||||||||||||||||||||||||||||
| 1267 | - | |||||||||||||||||||||||||||||||
| 1268 | heap = reallocarray(NULL, pow2, sizeof heap[0]); | - | ||||||||||||||||||||||||||||||
| 1269 | if (heap == NULL)
| 0-210 | ||||||||||||||||||||||||||||||
| 1270 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1271 | - | |||||||||||||||||||||||||||||||
| 1272 | /* | - | ||||||||||||||||||||||||||||||
| 1273 | * The array is used as a binary tree, exactly as in heapsort: | - | ||||||||||||||||||||||||||||||
| 1274 | * | - | ||||||||||||||||||||||||||||||
| 1275 | * heap[1] heap[2] heap[3] heap[4] heap[5] | - | ||||||||||||||||||||||||||||||
| 1276 | * heap[6] heap[7] heap[8]heap[9] heap[10]heap[11] | - | ||||||||||||||||||||||||||||||
| 1277 | * heap[12]heap[13] heap[14] heap[15] | - | ||||||||||||||||||||||||||||||
| 1278 | * | - | ||||||||||||||||||||||||||||||
| 1279 | * We put the Z's in the last line; then we set each other node to the | - | ||||||||||||||||||||||||||||||
| 1280 | * product of its two child-nodes (where empty or 0 entries are | - | ||||||||||||||||||||||||||||||
| 1281 | * treated as ones); then we invert heap[1]; then we invert each | - | ||||||||||||||||||||||||||||||
| 1282 | * other node by replacing it by the product of its parent (after | - | ||||||||||||||||||||||||||||||
| 1283 | * inversion) and its sibling (before inversion). | - | ||||||||||||||||||||||||||||||
| 1284 | */ | - | ||||||||||||||||||||||||||||||
| 1285 | heap[0] = NULL; | - | ||||||||||||||||||||||||||||||
| 1286 | for (i = pow2 / 2 - 1; i > 0; i--)
| 210-1870 | ||||||||||||||||||||||||||||||
| 1287 | heap[i] = NULL; executed 1870 times by 1 test: heap[i] = ((void *)0) ;Executed by:
| 1870 | ||||||||||||||||||||||||||||||
| 1288 | for (i = 0; i < num; i++)
| 210-2080 | ||||||||||||||||||||||||||||||
| 1289 | heap[pow2 / 2 + i] = &points[i]->Z; executed 2080 times by 1 test: heap[pow2 / 2 + i] = &points[i]->Z;Executed by:
| 2080 | ||||||||||||||||||||||||||||||
| 1290 | for (i = pow2 / 2 + num; i < pow2; i++)
| 0-210 | ||||||||||||||||||||||||||||||
| 1291 | heap[i] = NULL; never executed: heap[i] = ((void *)0) ; | 0 | ||||||||||||||||||||||||||||||
| 1292 | - | |||||||||||||||||||||||||||||||
| 1293 | /* set each node to the product of its children */ | - | ||||||||||||||||||||||||||||||
| 1294 | for (i = pow2 / 2 - 1; i > 0; i--) {
| 210-1870 | ||||||||||||||||||||||||||||||
| 1295 | heap[i] = BN_new(); | - | ||||||||||||||||||||||||||||||
| 1296 | if (heap[i] == NULL)
| 0-1870 | ||||||||||||||||||||||||||||||
| 1297 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1298 | - | |||||||||||||||||||||||||||||||
| 1299 | if (heap[2 * i] != NULL) {
| 0-1870 | ||||||||||||||||||||||||||||||
| 1300 | if ((heap[2 * i + 1] == NULL) || BN_is_zero(heap[2 * i + 1])) {
| 0-1870 | ||||||||||||||||||||||||||||||
| 1301 | if (!BN_copy(heap[i], heap[2 * i]))
| 0 | ||||||||||||||||||||||||||||||
| 1302 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1303 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1304 | if (BN_is_zero(heap[2 * i])) {
| 0-1870 | ||||||||||||||||||||||||||||||
| 1305 | if (!BN_copy(heap[i], heap[2 * i + 1]))
| 0 | ||||||||||||||||||||||||||||||
| 1306 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1307 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1308 | if (!group->meth->field_mul(group, heap[i],
| 0-1870 | ||||||||||||||||||||||||||||||
| 1309 | heap[2 * i], heap[2 * i + 1], ctx))
| 0-1870 | ||||||||||||||||||||||||||||||
| 1310 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1311 | } executed 1870 times by 1 test: end of blockExecuted by:
| 1870 | ||||||||||||||||||||||||||||||
| 1312 | } | - | ||||||||||||||||||||||||||||||
| 1313 | } | - | ||||||||||||||||||||||||||||||
| 1314 | } executed 1870 times by 1 test: end of blockExecuted by:
| 1870 | ||||||||||||||||||||||||||||||
| 1315 | - | |||||||||||||||||||||||||||||||
| 1316 | /* invert heap[1] */ | - | ||||||||||||||||||||||||||||||
| 1317 | if (!BN_is_zero(heap[1])) {
| 0-210 | ||||||||||||||||||||||||||||||
| 1318 | if (!BN_mod_inverse_ct(heap[1], heap[1], &group->field, ctx)) {
| 0-210 | ||||||||||||||||||||||||||||||
| 1319 | ECerror(ERR_R_BN_LIB); | - | ||||||||||||||||||||||||||||||
| 1320 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1321 | } | - | ||||||||||||||||||||||||||||||
| 1322 | } executed 210 times by 1 test: end of blockExecuted by:
| 210 | ||||||||||||||||||||||||||||||
| 1323 | if (group->meth->field_encode != 0) {
| 0-210 | ||||||||||||||||||||||||||||||
| 1324 | /* | - | ||||||||||||||||||||||||||||||
| 1325 | * in the Montgomery case, we just turned R*H (representing | - | ||||||||||||||||||||||||||||||
| 1326 | * H) into 1/(R*H), but we need R*(1/H) (representing | - | ||||||||||||||||||||||||||||||
| 1327 | * 1/H); i.e. we have need to multiply by the Montgomery | - | ||||||||||||||||||||||||||||||
| 1328 | * factor twice | - | ||||||||||||||||||||||||||||||
| 1329 | */ | - | ||||||||||||||||||||||||||||||
| 1330 | if (!group->meth->field_encode(group, heap[1], heap[1], ctx))
| 0-210 | ||||||||||||||||||||||||||||||
| 1331 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1332 | if (!group->meth->field_encode(group, heap[1], heap[1], ctx))
| 0-210 | ||||||||||||||||||||||||||||||
| 1333 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1334 | } executed 210 times by 1 test: end of blockExecuted by:
| 210 | ||||||||||||||||||||||||||||||
| 1335 | /* set other heap[i]'s to their inverses */ | - | ||||||||||||||||||||||||||||||
| 1336 | for (i = 2; i < pow2 / 2 + num; i += 2) {
| 210-1870 | ||||||||||||||||||||||||||||||
| 1337 | /* i is even */ | - | ||||||||||||||||||||||||||||||
| 1338 | if ((heap[i + 1] != NULL) && !BN_is_zero(heap[i + 1])) {
| 0-1870 | ||||||||||||||||||||||||||||||
| 1339 | if (!group->meth->field_mul(group, tmp0, heap[i / 2], heap[i + 1], ctx))
| 0-1870 | ||||||||||||||||||||||||||||||
| 1340 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1341 | if (!group->meth->field_mul(group, tmp1, heap[i / 2], heap[i], ctx))
| 0-1870 | ||||||||||||||||||||||||||||||
| 1342 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1343 | if (!BN_copy(heap[i], tmp0))
| 0-1870 | ||||||||||||||||||||||||||||||
| 1344 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1345 | if (!BN_copy(heap[i + 1], tmp1))
| 0-1870 | ||||||||||||||||||||||||||||||
| 1346 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1347 | } else { executed 1870 times by 1 test: end of blockExecuted by:
| 1870 | ||||||||||||||||||||||||||||||
| 1348 | if (!BN_copy(heap[i], heap[i / 2]))
| 0 | ||||||||||||||||||||||||||||||
| 1349 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1350 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1351 | } | - | ||||||||||||||||||||||||||||||
| 1352 | - | |||||||||||||||||||||||||||||||
| 1353 | /* | - | ||||||||||||||||||||||||||||||
| 1354 | * we have replaced all non-zero Z's by their inverses, now fix up | - | ||||||||||||||||||||||||||||||
| 1355 | * all the points | - | ||||||||||||||||||||||||||||||
| 1356 | */ | - | ||||||||||||||||||||||||||||||
| 1357 | for (i = 0; i < num; i++) {
| 210-2080 | ||||||||||||||||||||||||||||||
| 1358 | EC_POINT *p = points[i]; | - | ||||||||||||||||||||||||||||||
| 1359 | - | |||||||||||||||||||||||||||||||
| 1360 | if (!BN_is_zero(&p->Z)) {
| 0-2080 | ||||||||||||||||||||||||||||||
| 1361 | /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */ | - | ||||||||||||||||||||||||||||||
| 1362 | - | |||||||||||||||||||||||||||||||
| 1363 | if (!group->meth->field_sqr(group, tmp1, &p->Z, ctx))
| 0-2080 | ||||||||||||||||||||||||||||||
| 1364 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1365 | if (!group->meth->field_mul(group, &p->X, &p->X, tmp1, ctx))
| 0-2080 | ||||||||||||||||||||||||||||||
| 1366 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1367 | - | |||||||||||||||||||||||||||||||
| 1368 | if (!group->meth->field_mul(group, tmp1, tmp1, &p->Z, ctx))
| 0-2080 | ||||||||||||||||||||||||||||||
| 1369 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1370 | if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp1, ctx))
| 0-2080 | ||||||||||||||||||||||||||||||
| 1371 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1372 | - | |||||||||||||||||||||||||||||||
| 1373 | if (group->meth->field_set_to_one != 0) {
| 0-2080 | ||||||||||||||||||||||||||||||
| 1374 | if (!group->meth->field_set_to_one(group, &p->Z, ctx))
| 0-2080 | ||||||||||||||||||||||||||||||
| 1375 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1376 | } else { executed 2080 times by 1 test: end of blockExecuted by:
| 2080 | ||||||||||||||||||||||||||||||
| 1377 | if (!BN_one(&p->Z))
| 0 | ||||||||||||||||||||||||||||||
| 1378 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1379 | } never executed: end of block | 0 | ||||||||||||||||||||||||||||||
| 1380 | p->Z_is_one = 1; | - | ||||||||||||||||||||||||||||||
| 1381 | } executed 2080 times by 1 test: end of blockExecuted by:
| 2080 | ||||||||||||||||||||||||||||||
| 1382 | } executed 2080 times by 1 test: end of blockExecuted by:
| 2080 | ||||||||||||||||||||||||||||||
| 1383 | - | |||||||||||||||||||||||||||||||
| 1384 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 1385 | - | |||||||||||||||||||||||||||||||
| 1386 | err: code before this statement executed 210 times by 1 test: err:Executed by:
| 210 | ||||||||||||||||||||||||||||||
| 1387 | BN_CTX_end(ctx); | - | ||||||||||||||||||||||||||||||
| 1388 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 1389 | if (heap != NULL) {
| 0-210 | ||||||||||||||||||||||||||||||
| 1390 | /* | - | ||||||||||||||||||||||||||||||
| 1391 | * heap[pow2/2] .. heap[pow2-1] have not been allocated | - | ||||||||||||||||||||||||||||||
| 1392 | * locally! | - | ||||||||||||||||||||||||||||||
| 1393 | */ | - | ||||||||||||||||||||||||||||||
| 1394 | for (i = pow2 / 2 - 1; i > 0; i--) {
| 210-1870 | ||||||||||||||||||||||||||||||
| 1395 | BN_clear_free(heap[i]); | - | ||||||||||||||||||||||||||||||
| 1396 | } executed 1870 times by 1 test: end of blockExecuted by:
| 1870 | ||||||||||||||||||||||||||||||
| 1397 | free(heap); | - | ||||||||||||||||||||||||||||||
| 1398 | } executed 210 times by 1 test: end of blockExecuted by:
| 210 | ||||||||||||||||||||||||||||||
| 1399 | return ret; executed 210 times by 1 test: return ret;Executed by:
| 210 | ||||||||||||||||||||||||||||||
| 1400 | } | - | ||||||||||||||||||||||||||||||
| 1401 | - | |||||||||||||||||||||||||||||||
| 1402 | - | |||||||||||||||||||||||||||||||
| 1403 | int | - | ||||||||||||||||||||||||||||||
| 1404 | ec_GFp_simple_field_mul(const EC_GROUP * group, BIGNUM * r, const BIGNUM * a, const BIGNUM * b, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 1405 | { | - | ||||||||||||||||||||||||||||||
| 1406 | return BN_mod_mul(r, a, b, &group->field, ctx); never executed: return BN_mod_mul(r, a, b, &group->field, ctx); | 0 | ||||||||||||||||||||||||||||||
| 1407 | } | - | ||||||||||||||||||||||||||||||
| 1408 | - | |||||||||||||||||||||||||||||||
| 1409 | - | |||||||||||||||||||||||||||||||
| 1410 | int | - | ||||||||||||||||||||||||||||||
| 1411 | ec_GFp_simple_field_sqr(const EC_GROUP * group, BIGNUM * r, const BIGNUM * a, BN_CTX * ctx) | - | ||||||||||||||||||||||||||||||
| 1412 | { | - | ||||||||||||||||||||||||||||||
| 1413 | return BN_mod_sqr(r, a, &group->field, ctx); never executed: return BN_mod_sqr(r, a, &group->field, ctx); | 0 | ||||||||||||||||||||||||||||||
| 1414 | } | - | ||||||||||||||||||||||||||||||
| 1415 | - | |||||||||||||||||||||||||||||||
| 1416 | #define EC_POINT_BN_set_flags(P, flags) do { \ | - | ||||||||||||||||||||||||||||||
| 1417 | BN_set_flags(&(P)->X, (flags)); \ | - | ||||||||||||||||||||||||||||||
| 1418 | BN_set_flags(&(P)->Y, (flags)); \ | - | ||||||||||||||||||||||||||||||
| 1419 | BN_set_flags(&(P)->Z, (flags)); \ | - | ||||||||||||||||||||||||||||||
| 1420 | } while(0) | - | ||||||||||||||||||||||||||||||
| 1421 | - | |||||||||||||||||||||||||||||||
| 1422 | #define EC_POINT_CSWAP(c, a, b, w, t) do { \ | - | ||||||||||||||||||||||||||||||
| 1423 | if (!BN_swap_ct(c, &(a)->X, &(b)->X, w) || \ | - | ||||||||||||||||||||||||||||||
| 1424 | !BN_swap_ct(c, &(a)->Y, &(b)->Y, w) || \ | - | ||||||||||||||||||||||||||||||
| 1425 | !BN_swap_ct(c, &(a)->Z, &(b)->Z, w)) \ | - | ||||||||||||||||||||||||||||||
| 1426 | goto err; \ | - | ||||||||||||||||||||||||||||||
| 1427 | t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \ | - | ||||||||||||||||||||||||||||||
| 1428 | (a)->Z_is_one ^= (t); \ | - | ||||||||||||||||||||||||||||||
| 1429 | (b)->Z_is_one ^= (t); \ | - | ||||||||||||||||||||||||||||||
| 1430 | } while(0) | - | ||||||||||||||||||||||||||||||
| 1431 | - | |||||||||||||||||||||||||||||||
| 1432 | /* | - | ||||||||||||||||||||||||||||||
| 1433 | * This function computes (in constant time) a point multiplication over the | - | ||||||||||||||||||||||||||||||
| 1434 | * EC group. | - | ||||||||||||||||||||||||||||||
| 1435 | * | - | ||||||||||||||||||||||||||||||
| 1436 | * At a high level, it is Montgomery ladder with conditional swaps. | - | ||||||||||||||||||||||||||||||
| 1437 | * | - | ||||||||||||||||||||||||||||||
| 1438 | * It performs either a fixed point multiplication | - | ||||||||||||||||||||||||||||||
| 1439 | * (scalar * generator) | - | ||||||||||||||||||||||||||||||
| 1440 | * when point is NULL, or a variable point multiplication | - | ||||||||||||||||||||||||||||||
| 1441 | * (scalar * point) | - | ||||||||||||||||||||||||||||||
| 1442 | * when point is not NULL. | - | ||||||||||||||||||||||||||||||
| 1443 | * | - | ||||||||||||||||||||||||||||||
| 1444 | * scalar should be in the range [0,n) otherwise all constant time bets are off. | - | ||||||||||||||||||||||||||||||
| 1445 | * | - | ||||||||||||||||||||||||||||||
| 1446 | * NB: This says nothing about EC_POINT_add and EC_POINT_dbl, | - | ||||||||||||||||||||||||||||||
| 1447 | * which of course are not constant time themselves. | - | ||||||||||||||||||||||||||||||
| 1448 | * | - | ||||||||||||||||||||||||||||||
| 1449 | * The product is stored in r. | - | ||||||||||||||||||||||||||||||
| 1450 | * | - | ||||||||||||||||||||||||||||||
| 1451 | * Returns 1 on success, 0 otherwise. | - | ||||||||||||||||||||||||||||||
| 1452 | */ | - | ||||||||||||||||||||||||||||||
| 1453 | static int | - | ||||||||||||||||||||||||||||||
| 1454 | ec_GFp_simple_mul_ct(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, | - | ||||||||||||||||||||||||||||||
| 1455 | const EC_POINT *point, BN_CTX *ctx) | - | ||||||||||||||||||||||||||||||
| 1456 | { | - | ||||||||||||||||||||||||||||||
| 1457 | int i, cardinality_bits, group_top, kbit, pbit, Z_is_one; | - | ||||||||||||||||||||||||||||||
| 1458 | EC_POINT *s = NULL; | - | ||||||||||||||||||||||||||||||
| 1459 | BIGNUM *k = NULL; | - | ||||||||||||||||||||||||||||||
| 1460 | BIGNUM *lambda = NULL; | - | ||||||||||||||||||||||||||||||
| 1461 | BIGNUM *cardinality = NULL; | - | ||||||||||||||||||||||||||||||
| 1462 | BN_CTX *new_ctx = NULL; | - | ||||||||||||||||||||||||||||||
| 1463 | int ret = 0; | - | ||||||||||||||||||||||||||||||
| 1464 | - | |||||||||||||||||||||||||||||||
| 1465 | if (ctx == NULL && (ctx = new_ctx = BN_CTX_new()) == NULL)
| 0-708 | ||||||||||||||||||||||||||||||
| 1466 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||||||||
| 1467 | - | |||||||||||||||||||||||||||||||
| 1468 | BN_CTX_start(ctx); | - | ||||||||||||||||||||||||||||||
| 1469 | - | |||||||||||||||||||||||||||||||
| 1470 | if ((s = EC_POINT_new(group)) == NULL)
| 0-714 | ||||||||||||||||||||||||||||||
| 1471 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1472 | - | |||||||||||||||||||||||||||||||
| 1473 | if (point == NULL) {
| 206-508 | ||||||||||||||||||||||||||||||
| 1474 | if (!EC_POINT_copy(s, group->generator))
| 0-508 | ||||||||||||||||||||||||||||||
| 1475 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1476 | } else { executed 508 times by 4 tests: end of blockExecuted by:
| 508 | ||||||||||||||||||||||||||||||
| 1477 | if (!EC_POINT_copy(s, point))
| 0-206 | ||||||||||||||||||||||||||||||
| 1478 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1479 | } executed 206 times by 4 tests: end of blockExecuted by:
| 206 | ||||||||||||||||||||||||||||||
| 1480 | - | |||||||||||||||||||||||||||||||
| 1481 | EC_POINT_BN_set_flags(s, BN_FLG_CONSTTIME); | - | ||||||||||||||||||||||||||||||
| 1482 | - | |||||||||||||||||||||||||||||||
| 1483 | if ((cardinality = BN_CTX_get(ctx)) == NULL)
| 0-714 | ||||||||||||||||||||||||||||||
| 1484 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1485 | if ((lambda = BN_CTX_get(ctx)) == NULL)
| 0-714 | ||||||||||||||||||||||||||||||
| 1486 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1487 | if ((k = BN_CTX_get(ctx)) == NULL)
| 0-714 | ||||||||||||||||||||||||||||||
| 1488 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1489 | if (!BN_mul(cardinality, &group->order, &group->cofactor, ctx))
| 0-714 | ||||||||||||||||||||||||||||||
| 1490 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1491 | - | |||||||||||||||||||||||||||||||
| 1492 | /* | - | ||||||||||||||||||||||||||||||
| 1493 | * Group cardinalities are often on a word boundary. | - | ||||||||||||||||||||||||||||||
| 1494 | * So when we pad the scalar, some timing diff might | - | ||||||||||||||||||||||||||||||
| 1495 | * pop if it needs to be expanded due to carries. | - | ||||||||||||||||||||||||||||||
| 1496 | * So expand ahead of time. | - | ||||||||||||||||||||||||||||||
| 1497 | */ | - | ||||||||||||||||||||||||||||||
| 1498 | cardinality_bits = BN_num_bits(cardinality); | - | ||||||||||||||||||||||||||||||
| 1499 | group_top = cardinality->top; | - | ||||||||||||||||||||||||||||||
| 1500 | if ((bn_wexpand(k, group_top + 1) == NULL) ||
| 0-714 | ||||||||||||||||||||||||||||||
| 1501 | (bn_wexpand(lambda, group_top + 1) == NULL))
| 0-714 | ||||||||||||||||||||||||||||||
| 1502 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1503 | - | |||||||||||||||||||||||||||||||
| 1504 | if (!BN_copy(k, scalar))
| 0-714 | ||||||||||||||||||||||||||||||
| 1505 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1506 | - | |||||||||||||||||||||||||||||||
| 1507 | BN_set_flags(k, BN_FLG_CONSTTIME); | - | ||||||||||||||||||||||||||||||
| 1508 | - | |||||||||||||||||||||||||||||||
| 1509 | if (BN_num_bits(k) > cardinality_bits || BN_is_negative(k)) {
| 16-656 | ||||||||||||||||||||||||||||||
| 1510 | /* | - | ||||||||||||||||||||||||||||||
| 1511 | * This is an unusual input, and we don't guarantee | - | ||||||||||||||||||||||||||||||
| 1512 | * constant-timeness | - | ||||||||||||||||||||||||||||||
| 1513 | */ | - | ||||||||||||||||||||||||||||||
| 1514 | if (!BN_nnmod(k, k, cardinality, ctx))
| 0-74 | ||||||||||||||||||||||||||||||
| 1515 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1516 | } executed 74 times by 2 tests: end of blockExecuted by:
| 74 | ||||||||||||||||||||||||||||||
| 1517 | - | |||||||||||||||||||||||||||||||
| 1518 | if (!BN_add(lambda, k, cardinality))
| 0-714 | ||||||||||||||||||||||||||||||
| 1519 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1520 | BN_set_flags(lambda, BN_FLG_CONSTTIME); | - | ||||||||||||||||||||||||||||||
| 1521 | if (!BN_add(k, lambda, cardinality))
| 0-714 | ||||||||||||||||||||||||||||||
| 1522 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1523 | /* | - | ||||||||||||||||||||||||||||||
| 1524 | * lambda := scalar + cardinality | - | ||||||||||||||||||||||||||||||
| 1525 | * k := scalar + 2*cardinality | - | ||||||||||||||||||||||||||||||
| 1526 | */ | - | ||||||||||||||||||||||||||||||
| 1527 | kbit = BN_is_bit_set(lambda, cardinality_bits); | - | ||||||||||||||||||||||||||||||
| 1528 | if (!BN_swap_ct(kbit, k, lambda, group_top + 1))
| 0-714 | ||||||||||||||||||||||||||||||
| 1529 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1530 | - | |||||||||||||||||||||||||||||||
| 1531 | group_top = group->field.top; | - | ||||||||||||||||||||||||||||||
| 1532 | if ((bn_wexpand(&s->X, group_top) == NULL) ||
| 0-714 | ||||||||||||||||||||||||||||||
| 1533 | (bn_wexpand(&s->Y, group_top) == NULL) ||
| 0-714 | ||||||||||||||||||||||||||||||
| 1534 | (bn_wexpand(&s->Z, group_top) == NULL) ||
| 0-714 | ||||||||||||||||||||||||||||||
| 1535 | (bn_wexpand(&r->X, group_top) == NULL) ||
| 0-714 | ||||||||||||||||||||||||||||||
| 1536 | (bn_wexpand(&r->Y, group_top) == NULL) ||
| 0-714 | ||||||||||||||||||||||||||||||
| 1537 | (bn_wexpand(&r->Z, group_top) == NULL))
| 0-714 | ||||||||||||||||||||||||||||||
| 1538 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1539 | - | |||||||||||||||||||||||||||||||
| 1540 | /* top bit is a 1, in a fixed pos */ | - | ||||||||||||||||||||||||||||||
| 1541 | if (!EC_POINT_copy(r, s))
| 0-714 | ||||||||||||||||||||||||||||||
| 1542 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1543 | - | |||||||||||||||||||||||||||||||
| 1544 | EC_POINT_BN_set_flags(r, BN_FLG_CONSTTIME); | - | ||||||||||||||||||||||||||||||
| 1545 | - | |||||||||||||||||||||||||||||||
| 1546 | if (!EC_POINT_dbl(group, s, s, ctx))
| 0-714 | ||||||||||||||||||||||||||||||
| 1547 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1548 | - | |||||||||||||||||||||||||||||||
| 1549 | pbit = 0; | - | ||||||||||||||||||||||||||||||
| 1550 | - | |||||||||||||||||||||||||||||||
| 1551 | /* | - | ||||||||||||||||||||||||||||||
| 1552 | * The ladder step, with branches, is | - | ||||||||||||||||||||||||||||||
| 1553 | * | - | ||||||||||||||||||||||||||||||
| 1554 | * k[i] == 0: S = add(R, S), R = dbl(R) | - | ||||||||||||||||||||||||||||||
| 1555 | * k[i] == 1: R = add(S, R), S = dbl(S) | - | ||||||||||||||||||||||||||||||
| 1556 | * | - | ||||||||||||||||||||||||||||||
| 1557 | * Swapping R, S conditionally on k[i] leaves you with state | - | ||||||||||||||||||||||||||||||
| 1558 | * | - | ||||||||||||||||||||||||||||||
| 1559 | * k[i] == 0: T, U = R, S | - | ||||||||||||||||||||||||||||||
| 1560 | * k[i] == 1: T, U = S, R | - | ||||||||||||||||||||||||||||||
| 1561 | * | - | ||||||||||||||||||||||||||||||
| 1562 | * Then perform the ECC ops. | - | ||||||||||||||||||||||||||||||
| 1563 | * | - | ||||||||||||||||||||||||||||||
| 1564 | * U = add(T, U) | - | ||||||||||||||||||||||||||||||
| 1565 | * T = dbl(T) | - | ||||||||||||||||||||||||||||||
| 1566 | * | - | ||||||||||||||||||||||||||||||
| 1567 | * Which leaves you with state | - | ||||||||||||||||||||||||||||||
| 1568 | * | - | ||||||||||||||||||||||||||||||
| 1569 | * k[i] == 0: U = add(R, S), T = dbl(R) | - | ||||||||||||||||||||||||||||||
| 1570 | * k[i] == 1: U = add(S, R), T = dbl(S) | - | ||||||||||||||||||||||||||||||
| 1571 | * | - | ||||||||||||||||||||||||||||||
| 1572 | * Swapping T, U conditionally on k[i] leaves you with state | - | ||||||||||||||||||||||||||||||
| 1573 | * | - | ||||||||||||||||||||||||||||||
| 1574 | * k[i] == 0: R, S = T, U | - | ||||||||||||||||||||||||||||||
| 1575 | * k[i] == 1: R, S = U, T | - | ||||||||||||||||||||||||||||||
| 1576 | * | - | ||||||||||||||||||||||||||||||
| 1577 | * Which leaves you with state | - | ||||||||||||||||||||||||||||||
| 1578 | * | - | ||||||||||||||||||||||||||||||
| 1579 | * k[i] == 0: S = add(R, S), R = dbl(R) | - | ||||||||||||||||||||||||||||||
| 1580 | * k[i] == 1: R = add(S, R), S = dbl(S) | - | ||||||||||||||||||||||||||||||
| 1581 | * | - | ||||||||||||||||||||||||||||||
| 1582 | * So we get the same logic, but instead of a branch it's a | - | ||||||||||||||||||||||||||||||
| 1583 | * conditional swap, followed by ECC ops, then another conditional swap. | - | ||||||||||||||||||||||||||||||
| 1584 | * | - | ||||||||||||||||||||||||||||||
| 1585 | * Optimization: The end of iteration i and start of i-1 looks like | - | ||||||||||||||||||||||||||||||
| 1586 | * | - | ||||||||||||||||||||||||||||||
| 1587 | * ... | - | ||||||||||||||||||||||||||||||
| 1588 | * CSWAP(k[i], R, S) | - | ||||||||||||||||||||||||||||||
| 1589 | * ECC | - | ||||||||||||||||||||||||||||||
| 1590 | * CSWAP(k[i], R, S) | - | ||||||||||||||||||||||||||||||
| 1591 | * (next iteration) | - | ||||||||||||||||||||||||||||||
| 1592 | * CSWAP(k[i-1], R, S) | - | ||||||||||||||||||||||||||||||
| 1593 | * ECC | - | ||||||||||||||||||||||||||||||
| 1594 | * CSWAP(k[i-1], R, S) | - | ||||||||||||||||||||||||||||||
| 1595 | * ... | - | ||||||||||||||||||||||||||||||
| 1596 | * | - | ||||||||||||||||||||||||||||||
| 1597 | * So instead of two contiguous swaps, you can merge the condition | - | ||||||||||||||||||||||||||||||
| 1598 | * bits and do a single swap. | - | ||||||||||||||||||||||||||||||
| 1599 | * | - | ||||||||||||||||||||||||||||||
| 1600 | * k[i] k[i-1] Outcome | - | ||||||||||||||||||||||||||||||
| 1601 | * 0 0 No Swap | - | ||||||||||||||||||||||||||||||
| 1602 | * 0 1 Swap | - | ||||||||||||||||||||||||||||||
| 1603 | * 1 0 Swap | - | ||||||||||||||||||||||||||||||
| 1604 | * 1 1 No Swap | - | ||||||||||||||||||||||||||||||
| 1605 | * | - | ||||||||||||||||||||||||||||||
| 1606 | * This is XOR. pbit tracks the previous bit of k. | - | ||||||||||||||||||||||||||||||
| 1607 | */ | - | ||||||||||||||||||||||||||||||
| 1608 | - | |||||||||||||||||||||||||||||||
| 1609 | for (i = cardinality_bits - 1; i >= 0; i--) {
| 714-195481 | ||||||||||||||||||||||||||||||
| 1610 | kbit = BN_is_bit_set(k, i) ^ pbit; | - | ||||||||||||||||||||||||||||||
| 1611 | EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one); never executed: goto err;
| 0-195481 | ||||||||||||||||||||||||||||||
| 1612 | if (!EC_POINT_add(group, s, r, s, ctx))
| 0-195481 | ||||||||||||||||||||||||||||||
| 1613 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1614 | if (!EC_POINT_dbl(group, r, r, ctx))
| 0-195481 | ||||||||||||||||||||||||||||||
| 1615 | goto err; never executed: goto err; | 0 | ||||||||||||||||||||||||||||||
| 1616 | /* | - | ||||||||||||||||||||||||||||||
| 1617 | * pbit logic merges this cswap with that of the | - | ||||||||||||||||||||||||||||||
| 1618 | * next iteration | - | ||||||||||||||||||||||||||||||
| 1619 | */ | - | ||||||||||||||||||||||||||||||
| 1620 | pbit ^= kbit; | - | ||||||||||||||||||||||||||||||
| 1621 | } executed 195481 times by 4 tests: end of blockExecuted by:
| 195481 | ||||||||||||||||||||||||||||||
| 1622 | /* one final cswap to move the right value into r */ | - | ||||||||||||||||||||||||||||||
| 1623 | EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one); never executed: goto err;
| 0-714 | ||||||||||||||||||||||||||||||
| 1624 | - | |||||||||||||||||||||||||||||||
| 1625 | ret = 1; | - | ||||||||||||||||||||||||||||||
| 1626 | - | |||||||||||||||||||||||||||||||
| 1627 | err: code before this statement executed 714 times by 4 tests: err:Executed by:
| 714 | ||||||||||||||||||||||||||||||
| 1628 | EC_POINT_free(s); | - | ||||||||||||||||||||||||||||||
| 1629 | if (ctx != NULL)
| 0-714 | ||||||||||||||||||||||||||||||
| 1630 | BN_CTX_end(ctx); executed 714 times by 4 tests: BN_CTX_end(ctx);Executed by:
| 714 | ||||||||||||||||||||||||||||||
| 1631 | BN_CTX_free(new_ctx); | - | ||||||||||||||||||||||||||||||
| 1632 | - | |||||||||||||||||||||||||||||||
| 1633 | return ret; executed 714 times by 4 tests: return ret;Executed by:
| 714 | ||||||||||||||||||||||||||||||
| 1634 | } | - | ||||||||||||||||||||||||||||||
| 1635 | - | |||||||||||||||||||||||||||||||
| 1636 | #undef EC_POINT_BN_set_flags | - | ||||||||||||||||||||||||||||||
| 1637 | #undef EC_POINT_CSWAP | - | ||||||||||||||||||||||||||||||
| 1638 | - | |||||||||||||||||||||||||||||||
| 1639 | int | - | ||||||||||||||||||||||||||||||
| 1640 | ec_GFp_simple_mul_generator_ct(const EC_GROUP *group, EC_POINT *r, | - | ||||||||||||||||||||||||||||||
| 1641 | const BIGNUM *scalar, BN_CTX *ctx) | - | ||||||||||||||||||||||||||||||
| 1642 | { | - | ||||||||||||||||||||||||||||||
| 1643 | return ec_GFp_simple_mul_ct(group, r, scalar, NULL, ctx); executed 508 times by 4 tests: return ec_GFp_simple_mul_ct(group, r, scalar, ((void *)0) , ctx);Executed by:
| 508 | ||||||||||||||||||||||||||||||
| 1644 | } | - | ||||||||||||||||||||||||||||||
| 1645 | - | |||||||||||||||||||||||||||||||
| 1646 | int | - | ||||||||||||||||||||||||||||||
| 1647 | ec_GFp_simple_mul_single_ct(const EC_GROUP *group, EC_POINT *r, | - | ||||||||||||||||||||||||||||||
| 1648 | const BIGNUM *scalar, const EC_POINT *point, BN_CTX *ctx) | - | ||||||||||||||||||||||||||||||
| 1649 | { | - | ||||||||||||||||||||||||||||||
| 1650 | return ec_GFp_simple_mul_ct(group, r, scalar, point, ctx); executed 206 times by 4 tests: return ec_GFp_simple_mul_ct(group, r, scalar, point, ctx);Executed by:
| 206 | ||||||||||||||||||||||||||||||
| 1651 | } | - | ||||||||||||||||||||||||||||||
| 1652 | - | |||||||||||||||||||||||||||||||
| 1653 | int | - | ||||||||||||||||||||||||||||||
| 1654 | ec_GFp_simple_mul_double_nonct(const EC_GROUP *group, EC_POINT *r, | - | ||||||||||||||||||||||||||||||
| 1655 | const BIGNUM *g_scalar, const BIGNUM *p_scalar, const EC_POINT *point, | - | ||||||||||||||||||||||||||||||
| 1656 | BN_CTX *ctx) | - | ||||||||||||||||||||||||||||||
| 1657 | { | - | ||||||||||||||||||||||||||||||
| 1658 | return ec_wNAF_mul(group, r, g_scalar, 1, &point, &p_scalar, ctx); executed 379 times by 1 test: return ec_wNAF_mul(group, r, g_scalar, 1, &point, &p_scalar, ctx);Executed by:
| 379 | ||||||||||||||||||||||||||||||
| 1659 | } | - | ||||||||||||||||||||||||||||||
| Source code | Switch to Preprocessed file |