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) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 80 | size_t ret = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 81 | BN_CTX *new_ctx = NULL; |
| 82 | int used_ctx = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 83 | |
| 84 | if ((form != POINT_CONVERSION_COMPRESSED) && |
| 85 | (form != POINT_CONVERSION_UNCOMPRESSED)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 86 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 87 | goto err; |
| 88 | } |
| 89 | |
| 90 | if (EC_POINT_is_at_infinity(group, point)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 91 | OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY); |
| 92 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 95 | const size_t field_len = BN_num_bytes(&group->field); |
| 96 | size_t output_len = 1 /* type byte */ + field_len; |
| 97 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 98 | // Uncompressed points have a second coordinate. |
| 99 | output_len += field_len; |
| 100 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 101 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 102 | // if 'buf' is NULL, just return required length |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 103 | if (buf != NULL) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 104 | if (len < output_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 105 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 106 | goto err; |
| 107 | } |
| 108 | |
| 109 | if (ctx == NULL) { |
| 110 | ctx = new_ctx = BN_CTX_new(); |
| 111 | if (ctx == NULL) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 112 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | BN_CTX_start(ctx); |
| 117 | used_ctx = 1; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 118 | BIGNUM *x = BN_CTX_get(ctx); |
| 119 | BIGNUM *y = BN_CTX_get(ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 120 | if (y == NULL) { |
| 121 | goto err; |
| 122 | } |
| 123 | |
| 124 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) { |
| 125 | goto err; |
| 126 | } |
| 127 | |
| 128 | if ((form == POINT_CONVERSION_COMPRESSED) && |
| 129 | BN_is_odd(y)) { |
| 130 | buf[0] = form + 1; |
| 131 | } else { |
| 132 | buf[0] = form; |
| 133 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 134 | size_t i = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 135 | |
| 136 | if (!BN_bn2bin_padded(buf + i, field_len, x)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 137 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 138 | goto err; |
| 139 | } |
| 140 | i += field_len; |
| 141 | |
| 142 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 143 | if (!BN_bn2bin_padded(buf + i, field_len, y)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 144 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 145 | goto err; |
| 146 | } |
| 147 | i += field_len; |
| 148 | } |
| 149 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 150 | if (i != output_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 151 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 152 | goto err; |
| 153 | } |
| 154 | } |
| 155 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 156 | ret = output_len; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | |
| 158 | err: |
| 159 | if (used_ctx) { |
| 160 | BN_CTX_end(ctx); |
| 161 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 162 | BN_CTX_free(new_ctx); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 163 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 166 | static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 167 | const uint8_t *buf, size_t len, |
| 168 | BN_CTX *ctx) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 169 | BN_CTX *new_ctx = NULL; |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 170 | int ret = 0, used_ctx = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | |
| 172 | if (len == 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 173 | OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 174 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 175 | } |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 176 | |
| 177 | point_conversion_form_t form = buf[0]; |
| 178 | const int y_bit = form & 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 179 | form = form & ~1U; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 180 | if ((form != POINT_CONVERSION_COMPRESSED && |
| 181 | form != POINT_CONVERSION_UNCOMPRESSED) || |
| 182 | (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 183 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 184 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 185 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 186 | |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 187 | const size_t field_len = BN_num_bytes(&group->field); |
| 188 | size_t enc_len = 1 /* type byte */ + field_len; |
| 189 | if (form == POINT_CONVERSION_UNCOMPRESSED) { |
| 190 | // Uncompressed points have a second coordinate. |
| 191 | enc_len += field_len; |
| 192 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 193 | |
| 194 | if (len != enc_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 195 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 196 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | if (ctx == NULL) { |
| 200 | ctx = new_ctx = BN_CTX_new(); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 201 | if (ctx == NULL) { |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 202 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 203 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | BN_CTX_start(ctx); |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 207 | used_ctx = 1; |
| 208 | BIGNUM *x = BN_CTX_get(ctx); |
| 209 | BIGNUM *y = BN_CTX_get(ctx); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 210 | if (x == NULL || y == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 211 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 212 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 213 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 214 | if (!BN_bin2bn(buf + 1, field_len, x)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 215 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 216 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 217 | if (BN_ucmp(x, &group->field) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 218 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 219 | goto err; |
| 220 | } |
| 221 | |
| 222 | if (form == POINT_CONVERSION_COMPRESSED) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 223 | 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] | 224 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 225 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | } else { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 227 | if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 228 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 229 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 230 | if (BN_ucmp(y, &group->field) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 231 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | goto err; |
| 233 | } |
| 234 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 235 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 236 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 237 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | ret = 1; |
| 241 | |
| 242 | err: |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 243 | if (used_ctx) { |
| 244 | BN_CTX_end(ctx); |
| 245 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 246 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point, |
| 251 | const uint8_t *buf, size_t len, BN_CTX *ctx) { |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 252 | if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 253 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 254 | return 0; |
| 255 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 256 | return ec_GFp_simple_oct2point(group, point, buf, len, ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point, |
| 260 | point_conversion_form_t form, uint8_t *buf, |
| 261 | size_t len, BN_CTX *ctx) { |
Robert Sloan | 29c1d2c | 2017-10-30 14:10:28 -0700 | [diff] [blame] | 262 | if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 263 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 264 | return 0; |
| 265 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 266 | return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 269 | int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, |
| 270 | EC_POINT *point, const BIGNUM *x, |
| 271 | int y_bit, BN_CTX *ctx) { |
| 272 | if (EC_GROUP_cmp(group, point->group, NULL) != 0) { |
| 273 | OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS); |
| 274 | return 0; |
| 275 | } |
| 276 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 277 | if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 278 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 282 | BN_CTX *new_ctx = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | int ret = 0; |
| 284 | |
| 285 | ERR_clear_error(); |
| 286 | |
| 287 | if (ctx == NULL) { |
| 288 | ctx = new_ctx = BN_CTX_new(); |
| 289 | if (ctx == NULL) { |
| 290 | return 0; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | y_bit = (y_bit != 0); |
| 295 | |
| 296 | BN_CTX_start(ctx); |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 297 | BIGNUM *tmp1 = BN_CTX_get(ctx); |
| 298 | BIGNUM *tmp2 = BN_CTX_get(ctx); |
| 299 | BIGNUM *a = BN_CTX_get(ctx); |
| 300 | BIGNUM *b = BN_CTX_get(ctx); |
| 301 | BIGNUM *y = BN_CTX_get(ctx); |
| 302 | if (y == NULL || |
| 303 | !EC_GROUP_get_curve_GFp(group, NULL, a, b, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 304 | goto err; |
| 305 | } |
| 306 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 307 | // Recover y. We have a Weierstrass equation |
| 308 | // y^2 = x^3 + a*x + b, |
| 309 | // so y is one of the square roots of x^3 + a*x + b. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 310 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 311 | // tmp1 := x^3 |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 312 | if (!BN_mod_sqr(tmp2, x, &group->field, ctx) || |
| 313 | !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) { |
| 314 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 317 | // tmp1 := tmp1 + a*x |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 318 | if (group->a_is_minus3) { |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 319 | if (!bn_mod_lshift1_quick_ctx(tmp2, x, &group->field, ctx) || |
| 320 | !bn_mod_add_quick_ctx(tmp2, tmp2, x, &group->field, ctx) || |
| 321 | !bn_mod_sub_quick_ctx(tmp1, tmp1, tmp2, &group->field, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 322 | goto err; |
| 323 | } |
| 324 | } else { |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 325 | if (!BN_mod_mul(tmp2, a, x, &group->field, ctx) || |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 326 | !bn_mod_add_quick_ctx(tmp1, tmp1, tmp2, &group->field, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 327 | goto err; |
| 328 | } |
| 329 | } |
| 330 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 331 | // tmp1 := tmp1 + b |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 332 | if (!bn_mod_add_quick_ctx(tmp1, tmp1, b, &group->field, ctx)) { |
Robert Sloan | 0db7f54 | 2018-01-16 15:48:33 -0800 | [diff] [blame] | 333 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) { |
| 337 | unsigned long err = ERR_peek_last_error(); |
| 338 | |
| 339 | if (ERR_GET_LIB(err) == ERR_LIB_BN && |
| 340 | ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) { |
| 341 | ERR_clear_error(); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 342 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 343 | } else { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 344 | OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 345 | } |
| 346 | goto err; |
| 347 | } |
| 348 | |
| 349 | if (y_bit != BN_is_odd(y)) { |
| 350 | if (BN_is_zero(y)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 351 | OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 352 | goto err; |
| 353 | } |
| 354 | if (!BN_usub(y, &group->field, y)) { |
| 355 | goto err; |
| 356 | } |
| 357 | } |
| 358 | if (y_bit != BN_is_odd(y)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 359 | OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 360 | goto err; |
| 361 | } |
| 362 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 363 | if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 364 | goto err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 365 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 366 | |
| 367 | ret = 1; |
| 368 | |
| 369 | err: |
| 370 | BN_CTX_end(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 371 | BN_CTX_free(new_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 372 | return ret; |
| 373 | } |