blob: 38a3342ea331eaafffb5c02445f3d732842f1ea0 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
76static 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 Sloan8542c082018-02-05 09:07:34 -080080 size_t ret = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080081 BN_CTX *new_ctx = NULL;
82 int used_ctx = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -080083
84 if ((form != POINT_CONVERSION_COMPRESSED) &&
85 (form != POINT_CONVERSION_UNCOMPRESSED)) {
Kenny Rootb8494592015-09-25 02:29:14 +000086 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
Adam Langleyd9e397b2015-01-22 14:27:53 -080087 goto err;
88 }
89
90 if (EC_POINT_is_at_infinity(group, point)) {
Adam Langley4139edb2016-01-13 15:00:54 -080091 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
92 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -080093 }
94
Robert Sloan8542c082018-02-05 09:07:34 -080095 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 Langleyd9e397b2015-01-22 14:27:53 -0800101
Robert Sloan8f860b12017-08-28 07:37:06 -0700102 // if 'buf' is NULL, just return required length
Adam Langleyd9e397b2015-01-22 14:27:53 -0800103 if (buf != NULL) {
Robert Sloan8542c082018-02-05 09:07:34 -0800104 if (len < output_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000105 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800106 goto err;
107 }
108
109 if (ctx == NULL) {
110 ctx = new_ctx = BN_CTX_new();
111 if (ctx == NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800112 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800113 }
114 }
115
116 BN_CTX_start(ctx);
117 used_ctx = 1;
Robert Sloan8542c082018-02-05 09:07:34 -0800118 BIGNUM *x = BN_CTX_get(ctx);
119 BIGNUM *y = BN_CTX_get(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800120 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 Sloan8542c082018-02-05 09:07:34 -0800134 size_t i = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800135
136 if (!BN_bn2bin_padded(buf + i, field_len, x)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000137 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800138 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 Rootb8494592015-09-25 02:29:14 +0000144 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800145 goto err;
146 }
147 i += field_len;
148 }
149
Robert Sloan8542c082018-02-05 09:07:34 -0800150 if (i != output_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000151 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800152 goto err;
153 }
154 }
155
Robert Sloan8542c082018-02-05 09:07:34 -0800156 ret = output_len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157
158err:
159 if (used_ctx) {
160 BN_CTX_end(ctx);
161 }
Adam Langleye9ada862015-05-11 17:20:37 -0700162 BN_CTX_free(new_ctx);
Robert Sloan8542c082018-02-05 09:07:34 -0800163 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164}
165
Adam Langleyd9e397b2015-01-22 14:27:53 -0800166static 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 Langleyd9e397b2015-01-22 14:27:53 -0800169 BN_CTX *new_ctx = NULL;
Robert Sloan8542c082018-02-05 09:07:34 -0800170 int ret = 0, used_ctx = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800171
172 if (len == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000173 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Robert Sloan8542c082018-02-05 09:07:34 -0800174 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175 }
Robert Sloan8542c082018-02-05 09:07:34 -0800176
177 point_conversion_form_t form = buf[0];
178 const int y_bit = form & 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179 form = form & ~1U;
Adam Langley4139edb2016-01-13 15:00:54 -0800180 if ((form != POINT_CONVERSION_COMPRESSED &&
181 form != POINT_CONVERSION_UNCOMPRESSED) ||
182 (form == POINT_CONVERSION_UNCOMPRESSED && y_bit)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000183 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Robert Sloan8542c082018-02-05 09:07:34 -0800184 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800185 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186
Robert Sloan8542c082018-02-05 09:07:34 -0800187 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 Langleyd9e397b2015-01-22 14:27:53 -0800193
194 if (len != enc_len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000195 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Robert Sloan8542c082018-02-05 09:07:34 -0800196 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800197 }
198
199 if (ctx == NULL) {
200 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700201 if (ctx == NULL) {
Robert Sloan8542c082018-02-05 09:07:34 -0800202 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700203 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800204 }
205
206 BN_CTX_start(ctx);
Robert Sloan8542c082018-02-05 09:07:34 -0800207 used_ctx = 1;
208 BIGNUM *x = BN_CTX_get(ctx);
209 BIGNUM *y = BN_CTX_get(ctx);
Kenny Rootb8494592015-09-25 02:29:14 +0000210 if (x == NULL || y == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700212 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800213
Adam Langleye9ada862015-05-11 17:20:37 -0700214 if (!BN_bin2bn(buf + 1, field_len, x)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700216 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217 if (BN_ucmp(x, &group->field) >= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000218 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800219 goto err;
220 }
221
222 if (form == POINT_CONVERSION_COMPRESSED) {
Adam Langleye9ada862015-05-11 17:20:37 -0700223 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800224 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700225 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800226 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700227 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800228 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700229 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800230 if (BN_ucmp(y, &group->field) >= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000231 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800232 goto err;
233 }
234
Adam Langleye9ada862015-05-11 17:20:37 -0700235 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700237 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 }
239
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 ret = 1;
241
242err:
Robert Sloan8542c082018-02-05 09:07:34 -0800243 if (used_ctx) {
244 BN_CTX_end(ctx);
245 }
Adam Langleye9ada862015-05-11 17:20:37 -0700246 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800247 return ret;
248}
249
250int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
251 const uint8_t *buf, size_t len, BN_CTX *ctx) {
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700252 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000253 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254 return 0;
255 }
Kenny Roote99801b2015-11-06 15:31:15 -0800256 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257}
258
259size_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 Sloan29c1d2c2017-10-30 14:10:28 -0700262 if (EC_GROUP_cmp(group, point->group, NULL) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000263 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800264 return 0;
265 }
Kenny Roote99801b2015-11-06 15:31:15 -0800266 return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267}
268
Robert Sloan0db7f542018-01-16 15:48:33 -0800269int 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 Benjamin4969cc92016-04-22 15:02:23 -0400277 if (BN_is_negative(x) || BN_cmp(x, &group->field) >= 0) {
David Benjamin1b249672016-12-06 18:25:50 -0500278 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
David Benjamin4969cc92016-04-22 15:02:23 -0400279 return 0;
280 }
281
Adam Langleyd9e397b2015-01-22 14:27:53 -0800282 BN_CTX *new_ctx = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283 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 Sloan0db7f542018-01-16 15:48:33 -0800297 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 Langleyd9e397b2015-01-22 14:27:53 -0800304 goto err;
305 }
306
Robert Sloan8f860b12017-08-28 07:37:06 -0700307 // 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 Langleyd9e397b2015-01-22 14:27:53 -0800310
Robert Sloan8f860b12017-08-28 07:37:06 -0700311 // tmp1 := x^3
Robert Sloan0db7f542018-01-16 15:48:33 -0800312 if (!BN_mod_sqr(tmp2, x, &group->field, ctx) ||
313 !BN_mod_mul(tmp1, tmp2, x, &group->field, ctx)) {
314 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800315 }
316
Robert Sloan8f860b12017-08-28 07:37:06 -0700317 // tmp1 := tmp1 + a*x
Adam Langleyd9e397b2015-01-22 14:27:53 -0800318 if (group->a_is_minus3) {
Robert Sloanab8b8882018-03-26 11:39:51 -0700319 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 Langleyd9e397b2015-01-22 14:27:53 -0800322 goto err;
323 }
324 } else {
Robert Sloan0db7f542018-01-16 15:48:33 -0800325 if (!BN_mod_mul(tmp2, a, x, &group->field, ctx) ||
Robert Sloanab8b8882018-03-26 11:39:51 -0700326 !bn_mod_add_quick_ctx(tmp1, tmp1, tmp2, &group->field, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800327 goto err;
328 }
329 }
330
Robert Sloan8f860b12017-08-28 07:37:06 -0700331 // tmp1 := tmp1 + b
Robert Sloanab8b8882018-03-26 11:39:51 -0700332 if (!bn_mod_add_quick_ctx(tmp1, tmp1, b, &group->field, ctx)) {
Robert Sloan0db7f542018-01-16 15:48:33 -0800333 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 }
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 Rootb8494592015-09-25 02:29:14 +0000342 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800343 } else {
Kenny Rootb8494592015-09-25 02:29:14 +0000344 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800345 }
346 goto err;
347 }
348
349 if (y_bit != BN_is_odd(y)) {
350 if (BN_is_zero(y)) {
David Benjamin1b249672016-12-06 18:25:50 -0500351 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 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 Rootb8494592015-09-25 02:29:14 +0000359 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800360 goto err;
361 }
362
Adam Langleye9ada862015-05-11 17:20:37 -0700363 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700365 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800366
367 ret = 1;
368
369err:
370 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700371 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800372 return ret;
373}