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> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 71 | #include <openssl/bytestring.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 72 | #include <openssl/err.h> |
| 73 | |
| 74 | #include "internal.h" |
| 75 | |
| 76 | |
| 77 | static size_t ec_GFp_simple_point2oct(const EC_GROUP *group, |
| 78 | const EC_POINT *point, |
| 79 | point_conversion_form_t form, |
| 80 | uint8_t *buf, size_t len, BN_CTX *ctx) { |
| 81 | size_t ret; |
| 82 | BN_CTX *new_ctx = NULL; |
| 83 | int used_ctx = 0; |
| 84 | BIGNUM *x, *y; |
| 85 | size_t field_len, i; |
| 86 | |
| 87 | if ((form != POINT_CONVERSION_COMPRESSED) && |
| 88 | (form != POINT_CONVERSION_UNCOMPRESSED)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 89 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 90 | goto err; |
| 91 | } |
| 92 | |
| 93 | if (EC_POINT_is_at_infinity(group, point)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 94 | OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); |
| 95 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 98 | /* ret := required output buffer length */ |
| 99 | field_len = BN_num_bytes(&group->field); |
| 100 | ret = |
| 101 | (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
| 102 | |
| 103 | /* if 'buf' is NULL, just return required length */ |
| 104 | if (buf != NULL) { |
| 105 | if (len < ret) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 106 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 107 | goto err; |
| 108 | } |
| 109 | |
| 110 | if (ctx == NULL) { |
| 111 | ctx = new_ctx = BN_CTX_new(); |
| 112 | if (ctx == NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 113 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | BN_CTX_start(ctx); |
| 118 | used_ctx = 1; |
| 119 | x = BN_CTX_get(ctx); |
| 120 | y = BN_CTX_get(ctx); |
| 121 | if (y == NULL) { |
| 122 | goto err; |
| 123 | } |
| 124 | |
| 125 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) { |
| 126 | goto err; |
| 127 | } |
| 128 | |
| 129 | if ((form == POINT_CONVERSION_COMPRESSED) && |
| 130 | BN_is_odd(y)) { |
| 131 | buf[0] = form + 1; |
| 132 | } else { |
| 133 | buf[0] = form; |
| 134 | } |
| 135 | i = 1; |
| 136 | |
| 137 | if (!BN_bn2bin_padded(buf + i, field_len, x)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 138 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 139 | goto err; |
| 140 | } |
| 141 | i += field_len; |
| 142 | |
| 143 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 144 | if (!BN_bn2bin_padded(buf + i, field_len, y)) { |
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 | |
| 151 | if (i != ret) { |
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 | } |
| 156 | |
| 157 | if (used_ctx) { |
| 158 | BN_CTX_end(ctx); |
| 159 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 160 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 161 | return ret; |
| 162 | |
| 163 | err: |
| 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 0; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 173 | const uint8_t *buf, size_t len, |
| 174 | BN_CTX *ctx) { |
| 175 | point_conversion_form_t form; |
| 176 | int y_bit; |
| 177 | BN_CTX *new_ctx = NULL; |
| 178 | BIGNUM *x, *y; |
| 179 | size_t field_len, enc_len; |
| 180 | int ret = 0; |
| 181 | |
| 182 | if (len == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 183 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | form = buf[0]; |
| 187 | y_bit = form & 1; |
| 188 | form = form & ~1U; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 189 | if ((form != POINT_CONVERSION_COMPRESSED && |
| 190 | form != POINT_CONVERSION_UNCOMPRESSED) || |
| 191 | (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 192 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 193 | return 0; |
| 194 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 195 | |
| 196 | field_len = BN_num_bytes(&group->field); |
| 197 | enc_len = |
| 198 | (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len; |
| 199 | |
| 200 | if (len != enc_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 201 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | if (ctx == NULL) { |
| 206 | ctx = new_ctx = BN_CTX_new(); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 207 | if (ctx == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 208 | return 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 209 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | BN_CTX_start(ctx); |
| 213 | x = BN_CTX_get(ctx); |
| 214 | y = BN_CTX_get(ctx); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 215 | if (x == NULL || y == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 217 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 218 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 219 | if (!BN_bin2bn(buf + 1, field_len, x)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 221 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 222 | if (BN_ucmp(x, &group->field) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 223 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 224 | goto err; |
| 225 | } |
| 226 | |
| 227 | if (form == POINT_CONVERSION_COMPRESSED) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 228 | 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] | 229 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 230 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 231 | } else { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 232 | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 233 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 234 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 235 | if (BN_ucmp(y, &group->field) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 236 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 237 | goto err; |
| 238 | } |
| 239 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 240 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 241 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 242 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 245 | ret = 1; |
| 246 | |
| 247 | err: |
| 248 | BN_CTX_end(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 249 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 254 | const uint8_t *buf, size_t len, BN_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 255 | if (group->meth != point->meth) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 256 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | return 0; |
| 258 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 259 | return ec_GFp_simple_oct2point(group, point, buf, len, ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, |
| 263 | point_conversion_form_t form, uint8_t *buf, |
| 264 | size_t len, BN_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 265 | if (group->meth != point->meth) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 266 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | return 0; |
| 268 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 269 | return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 270 | } |
| 271 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 272 | int EC_POINT_point2cbb(CBB *out, const EC_GROUP *group, const EC_POINT *point, |
| 273 | point_conversion_form_t form, BN_CTX *ctx) { |
| 274 | size_t len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); |
| 275 | if (len == 0) { |
| 276 | return 0; |
| 277 | } |
| 278 | uint8_t *p; |
| 279 | return CBB_add_space(out, &p, len) && |
| 280 | EC_POINT_point2oct(group, point, form, p, len, ctx) == len; |
| 281 | } |
| 282 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 284 | EC_POINT *point, const BIGNUM *x, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 285 | int y_bit, BN_CTX *ctx) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 286 | if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 287 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | BN_CTX *new_ctx = NULL; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 292 | BIGNUM *tmp1, *tmp2, *y; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 293 | int ret = 0; |
| 294 | |
| 295 | ERR_clear_error(); |
| 296 | |
| 297 | if (ctx == NULL) { |
| 298 | ctx = new_ctx = BN_CTX_new(); |
| 299 | if (ctx == NULL) { |
| 300 | return 0; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | y_bit = (y_bit != 0); |
| 305 | |
| 306 | BN_CTX_start(ctx); |
| 307 | tmp1 = BN_CTX_get(ctx); |
| 308 | tmp2 = BN_CTX_get(ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 309 | y = BN_CTX_get(ctx); |
| 310 | if (y == NULL) { |
| 311 | goto err; |
| 312 | } |
| 313 | |
| 314 | /* Recover y. We have a Weierstrass equation |
| 315 | * y^2 = x^3 + a*x + b, |
| 316 | * so y is one of the square roots of x^3 + a*x + b. */ |
| 317 | |
| 318 | /* tmp1 := x^3 */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 319 | if (group->meth->field_decode == 0) { |
| 320 | /* field_{sqr,mul} work on standard representation */ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 321 | if (!group->meth->field_sqr(group, tmp2, x, ctx) || |
| 322 | !group->meth->field_mul(group, tmp1, tmp2, x, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 323 | goto err; |
| 324 | } |
| 325 | } else { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 326 | if (!BN_mod_sqr(tmp2, x, &group->field, ctx) || |
| 327 | !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 328 | goto err; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* tmp1 := tmp1 + a*x */ |
| 333 | if (group->a_is_minus3) { |
| 334 | if (!BN_mod_lshift1_quick(tmp2, x, &group->field) || |
| 335 | !BN_mod_add_quick(tmp2, tmp2, x, &group->field) || |
| 336 | !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) { |
| 337 | goto err; |
| 338 | } |
| 339 | } else { |
| 340 | if (group->meth->field_decode) { |
| 341 | if (!group->meth->field_decode(group, tmp2, &group->a, ctx) || |
| 342 | !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) { |
| 343 | goto err; |
| 344 | } |
| 345 | } else { |
| 346 | /* field_mul works on standard representation */ |
| 347 | if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) { |
| 348 | goto err; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) { |
| 353 | goto err; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* tmp1 := tmp1 + b */ |
| 358 | if (group->meth->field_decode) { |
| 359 | if (!group->meth->field_decode(group, tmp2, &group->b, ctx) || |
| 360 | !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) { |
| 361 | goto err; |
| 362 | } |
| 363 | } else { |
| 364 | if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) { |
| 365 | goto err; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) { |
| 370 | unsigned long err = ERR_peek_last_error(); |
| 371 | |
| 372 | if (ERR_GET_LIB(err) == ERR_LIB_BN && |
| 373 | ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
| 374 | ERR_clear_error(); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 375 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 376 | } else { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 377 | OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 378 | } |
| 379 | goto err; |
| 380 | } |
| 381 | |
| 382 | if (y_bit != BN_is_odd(y)) { |
| 383 | if (BN_is_zero(y)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 384 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 385 | goto err; |
| 386 | } |
| 387 | if (!BN_usub(y, &group->field, y)) { |
| 388 | goto err; |
| 389 | } |
| 390 | } |
| 391 | if (y_bit != BN_is_odd(y)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 392 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 393 | goto err; |
| 394 | } |
| 395 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 396 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 397 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 398 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 399 | |
| 400 | ret = 1; |
| 401 | |
| 402 | err: |
| 403 | BN_CTX_end(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 404 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, |
| 409 | EC_POINT *point, const BIGNUM *x, |
| 410 | int y_bit, BN_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 411 | if (group->meth != point->meth) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 413 | return 0; |
| 414 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 415 | return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit, ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 416 | } |