Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1 | /* Originally written by Bodo Moeller for the OpenSSL project. |
| 2 | * ==================================================================== |
| 3 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in |
| 14 | * the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * |
| 17 | * 3. All advertising materials mentioning features or use of this |
| 18 | * software must display the following acknowledgment: |
| 19 | * "This product includes software developed by the OpenSSL Project |
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 21 | * |
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 23 | * endorse or promote products derived from this software without |
| 24 | * prior written permission. For written permission, please contact |
| 25 | * openssl-core@openssl.org. |
| 26 | * |
| 27 | * 5. Products derived from this software may not be called "OpenSSL" |
| 28 | * nor may "OpenSSL" appear in their names without prior written |
| 29 | * permission of the OpenSSL Project. |
| 30 | * |
| 31 | * 6. Redistributions of any form whatsoever must retain the following |
| 32 | * acknowledgment: |
| 33 | * "This product includes software developed by the OpenSSL Project |
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 35 | * |
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 48 | * ==================================================================== |
| 49 | * |
| 50 | * This product includes cryptographic software written by Eric Young |
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 52 | * Hudson (tjh@cryptsoft.com). |
| 53 | * |
| 54 | */ |
| 55 | /* ==================================================================== |
| 56 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 57 | * |
| 58 | * Portions of the attached software ("Contribution") are developed by |
| 59 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 60 | * |
| 61 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 62 | * license provided above. |
| 63 | * |
| 64 | * The elliptic curve binary polynomial software is originally written by |
| 65 | * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems |
| 66 | * Laboratories. */ |
| 67 | |
| 68 | #include <openssl/ec.h> |
| 69 | |
| 70 | #include <openssl/bn.h> |
| 71 | #include <openssl/err.h> |
| 72 | |
| 73 | #include "internal.h" |
| 74 | |
| 75 | |
| 76 | static size_t ec_GFp_simple_point2oct(const EC_GROUP *group, |
| 77 | const EC_POINT *point, |
| 78 | point_conversion_form_t form, |
| 79 | uint8_t *buf, size_t len, BN_CTX *ctx) { |
| 80 | size_t ret; |
| 81 | BN_CTX *new_ctx = NULL; |
| 82 | int used_ctx = 0; |
| 83 | BIGNUM *x, *y; |
| 84 | size_t field_len, i; |
| 85 | |
| 86 | if ((form != POINT_CONVERSION_COMPRESSED) && |
| 87 | (form != POINT_CONVERSION_UNCOMPRESSED)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 88 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 89 | goto err; |
| 90 | } |
| 91 | |
| 92 | if (EC_POINT_is_at_infinity(group, point)) { |
| 93 | /* encodes to a single 0 octet */ |
| 94 | if (buf != NULL) { |
| 95 | if (len < 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 96 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | buf[0] = 0; |
| 100 | } |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* ret := required output buffer length */ |
| 106 | field_len = BN_num_bytes(&group->field); |
| 107 | ret = |
| 108 | (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
| 109 | |
| 110 | /* if 'buf' is NULL, just return required length */ |
| 111 | if (buf != NULL) { |
| 112 | if (len < ret) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 113 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | goto err; |
| 115 | } |
| 116 | |
| 117 | if (ctx == NULL) { |
| 118 | ctx = new_ctx = BN_CTX_new(); |
| 119 | if (ctx == NULL) { |
| 120 | return 0; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | BN_CTX_start(ctx); |
| 125 | used_ctx = 1; |
| 126 | x = BN_CTX_get(ctx); |
| 127 | y = BN_CTX_get(ctx); |
| 128 | if (y == NULL) { |
| 129 | goto err; |
| 130 | } |
| 131 | |
| 132 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) { |
| 133 | goto err; |
| 134 | } |
| 135 | |
| 136 | if ((form == POINT_CONVERSION_COMPRESSED) && |
| 137 | BN_is_odd(y)) { |
| 138 | buf[0] = form + 1; |
| 139 | } else { |
| 140 | buf[0] = form; |
| 141 | } |
| 142 | i = 1; |
| 143 | |
| 144 | if (!BN_bn2bin_padded(buf + i, field_len, x)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 145 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 146 | goto err; |
| 147 | } |
| 148 | i += field_len; |
| 149 | |
| 150 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 151 | if (!BN_bn2bin_padded(buf + i, field_len, y)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 152 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 153 | goto err; |
| 154 | } |
| 155 | i += field_len; |
| 156 | } |
| 157 | |
| 158 | if (i != ret) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 159 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 160 | goto err; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (used_ctx) { |
| 165 | BN_CTX_end(ctx); |
| 166 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 167 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 168 | return ret; |
| 169 | |
| 170 | err: |
| 171 | if (used_ctx) { |
| 172 | BN_CTX_end(ctx); |
| 173 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 174 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 180 | const uint8_t *buf, size_t len, |
| 181 | BN_CTX *ctx) { |
| 182 | point_conversion_form_t form; |
| 183 | int y_bit; |
| 184 | BN_CTX *new_ctx = NULL; |
| 185 | BIGNUM *x, *y; |
| 186 | size_t field_len, enc_len; |
| 187 | int ret = 0; |
| 188 | |
| 189 | if (len == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 190 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | form = buf[0]; |
| 194 | y_bit = form & 1; |
| 195 | form = form & ~1U; |
| 196 | if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) && |
| 197 | (form != POINT_CONVERSION_UNCOMPRESSED)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 198 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 202 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | if (form == 0) { |
| 207 | if (len != 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 208 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | return EC_POINT_set_to_infinity(group, point); |
| 213 | } |
| 214 | |
| 215 | field_len = BN_num_bytes(&group->field); |
| 216 | enc_len = |
| 217 | (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
| 218 | |
| 219 | if (len != enc_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 220 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | if (ctx == NULL) { |
| 225 | ctx = new_ctx = BN_CTX_new(); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 226 | if (ctx == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 227 | return 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 228 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | BN_CTX_start(ctx); |
| 232 | x = BN_CTX_get(ctx); |
| 233 | y = BN_CTX_get(ctx); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 234 | if (x == NULL || y == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 235 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 236 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 237 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 238 | if (!BN_bin2bn(buf + 1, field_len, x)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 239 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 240 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 241 | if (BN_ucmp(x, &group->field) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 242 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | goto err; |
| 244 | } |
| 245 | |
| 246 | if (form == POINT_CONVERSION_COMPRESSED) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 247 | if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 248 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 249 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 250 | } else { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 251 | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 252 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 253 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 254 | if (BN_ucmp(y, &group->field) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 255 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 256 | goto err; |
| 257 | } |
| 258 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 259 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 260 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 261 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 264 | /* test required by X9.62 */ |
| 265 | if (!EC_POINT_is_on_curve(group, point, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 266 | OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | goto err; |
| 268 | } |
| 269 | |
| 270 | ret = 1; |
| 271 | |
| 272 | err: |
| 273 | BN_CTX_end(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 274 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 279 | const uint8_t *buf, size_t len, BN_CTX *ctx) { |
| 280 | if (group->meth->oct2point == 0 && |
| 281 | !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 282 | OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | if (group->meth != point->meth) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 286 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { |
| 290 | return ec_GFp_simple_oct2point(group, point, buf, len, ctx); |
| 291 | } |
| 292 | |
| 293 | return group->meth->oct2point(group, point, buf, len, ctx); |
| 294 | } |
| 295 | |
| 296 | size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, |
| 297 | point_conversion_form_t form, uint8_t *buf, |
| 298 | size_t len, BN_CTX *ctx) { |
| 299 | if (group->meth->point2oct == 0 && |
| 300 | !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 301 | OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | if (group->meth != point->meth) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 305 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { |
| 309 | return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx); |
| 310 | } |
| 311 | |
| 312 | return group->meth->point2oct(group, point, form, buf, len, ctx); |
| 313 | } |
| 314 | |
| 315 | int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, |
| 316 | EC_POINT *point, const BIGNUM *x_, |
| 317 | int y_bit, BN_CTX *ctx) { |
| 318 | BN_CTX *new_ctx = NULL; |
| 319 | BIGNUM *tmp1, *tmp2, *x, *y; |
| 320 | int ret = 0; |
| 321 | |
| 322 | ERR_clear_error(); |
| 323 | |
| 324 | if (ctx == NULL) { |
| 325 | ctx = new_ctx = BN_CTX_new(); |
| 326 | if (ctx == NULL) { |
| 327 | return 0; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | y_bit = (y_bit != 0); |
| 332 | |
| 333 | BN_CTX_start(ctx); |
| 334 | tmp1 = BN_CTX_get(ctx); |
| 335 | tmp2 = BN_CTX_get(ctx); |
| 336 | x = BN_CTX_get(ctx); |
| 337 | y = BN_CTX_get(ctx); |
| 338 | if (y == NULL) { |
| 339 | goto err; |
| 340 | } |
| 341 | |
| 342 | /* Recover y. We have a Weierstrass equation |
| 343 | * y^2 = x^3 + a*x + b, |
| 344 | * so y is one of the square roots of x^3 + a*x + b. */ |
| 345 | |
| 346 | /* tmp1 := x^3 */ |
| 347 | if (!BN_nnmod(x, x_, &group->field, ctx)) { |
| 348 | goto err; |
| 349 | } |
| 350 | |
| 351 | if (group->meth->field_decode == 0) { |
| 352 | /* field_{sqr,mul} work on standard representation */ |
| 353 | if (!group->meth->field_sqr(group, tmp2, x_, ctx) || |
| 354 | !group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) { |
| 355 | goto err; |
| 356 | } |
| 357 | } else { |
| 358 | if (!BN_mod_sqr(tmp2, x_, &group->field, ctx) || |
| 359 | !BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) { |
| 360 | goto err; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* tmp1 := tmp1 + a*x */ |
| 365 | if (group->a_is_minus3) { |
| 366 | if (!BN_mod_lshift1_quick(tmp2, x, &group->field) || |
| 367 | !BN_mod_add_quick(tmp2, tmp2, x, &group->field) || |
| 368 | !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) { |
| 369 | goto err; |
| 370 | } |
| 371 | } else { |
| 372 | if (group->meth->field_decode) { |
| 373 | if (!group->meth->field_decode(group, tmp2, &group->a, ctx) || |
| 374 | !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) { |
| 375 | goto err; |
| 376 | } |
| 377 | } else { |
| 378 | /* field_mul works on standard representation */ |
| 379 | if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) { |
| 380 | goto err; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) { |
| 385 | goto err; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* tmp1 := tmp1 + b */ |
| 390 | if (group->meth->field_decode) { |
| 391 | if (!group->meth->field_decode(group, tmp2, &group->b, ctx) || |
| 392 | !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) { |
| 393 | goto err; |
| 394 | } |
| 395 | } else { |
| 396 | if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) { |
| 397 | goto err; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) { |
| 402 | unsigned long err = ERR_peek_last_error(); |
| 403 | |
| 404 | if (ERR_GET_LIB(err) == ERR_LIB_BN && |
| 405 | ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
| 406 | ERR_clear_error(); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 407 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 408 | } else { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 409 | OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 410 | } |
| 411 | goto err; |
| 412 | } |
| 413 | |
| 414 | if (y_bit != BN_is_odd(y)) { |
| 415 | if (BN_is_zero(y)) { |
| 416 | int kron; |
| 417 | |
| 418 | kron = BN_kronecker(x, &group->field, ctx); |
| 419 | if (kron == -2) { |
| 420 | goto err; |
| 421 | } |
| 422 | |
| 423 | if (kron == 1) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 424 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 425 | } else { |
| 426 | /* BN_mod_sqrt() should have cought this error (not a square) */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 427 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 428 | } |
| 429 | goto err; |
| 430 | } |
| 431 | if (!BN_usub(y, &group->field, y)) { |
| 432 | goto err; |
| 433 | } |
| 434 | } |
| 435 | if (y_bit != BN_is_odd(y)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 436 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 437 | goto err; |
| 438 | } |
| 439 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 440 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 441 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 442 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 443 | |
| 444 | ret = 1; |
| 445 | |
| 446 | err: |
| 447 | BN_CTX_end(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 448 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, |
| 453 | EC_POINT *point, const BIGNUM *x, |
| 454 | int y_bit, BN_CTX *ctx) { |
| 455 | if (group->meth->point_set_compressed_coordinates == 0 && |
| 456 | !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 457 | OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 458 | return 0; |
| 459 | } |
| 460 | if (group->meth != point->meth) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame^] | 461 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) { |
| 465 | return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit, |
| 466 | ctx); |
| 467 | } |
| 468 | return group->meth->point_set_compressed_coordinates(group, point, x, y_bit, |
| 469 | ctx); |
| 470 | } |