blob: cb50e1722904d362d0257d5ee6631f7672f2bf7f [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) {
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 Rootb8494592015-09-25 02:29:14 +000088 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FORM);
Adam Langleyd9e397b2015-01-22 14:27:53 -080089 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 Rootb8494592015-09-25 02:29:14 +000096 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -080097 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 Rootb8494592015-09-25 02:29:14 +0000113 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800114 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 Rootb8494592015-09-25 02:29:14 +0000145 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146 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 Rootb8494592015-09-25 02:29:14 +0000152 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153 goto err;
154 }
155 i += field_len;
156 }
157
158 if (i != ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000159 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800160 goto err;
161 }
162 }
163
164 if (used_ctx) {
165 BN_CTX_end(ctx);
166 }
Adam Langleye9ada862015-05-11 17:20:37 -0700167 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800168 return ret;
169
170err:
171 if (used_ctx) {
172 BN_CTX_end(ctx);
173 }
Adam Langleye9ada862015-05-11 17:20:37 -0700174 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175 return 0;
176}
177
178
179static 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 Rootb8494592015-09-25 02:29:14 +0000190 OPENSSL_PUT_ERROR(EC, EC_R_BUFFER_TOO_SMALL);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800191 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 Rootb8494592015-09-25 02:29:14 +0000198 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800199 return 0;
200 }
201 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
Kenny Rootb8494592015-09-25 02:29:14 +0000202 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203 return 0;
204 }
205
206 if (form == 0) {
207 if (len != 1) {
Kenny Rootb8494592015-09-25 02:29:14 +0000208 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800209 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 Rootb8494592015-09-25 02:29:14 +0000220 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800221 return 0;
222 }
223
224 if (ctx == NULL) {
225 ctx = new_ctx = BN_CTX_new();
Adam Langleye9ada862015-05-11 17:20:37 -0700226 if (ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800227 return 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700228 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 }
230
231 BN_CTX_start(ctx);
232 x = BN_CTX_get(ctx);
233 y = BN_CTX_get(ctx);
Kenny Rootb8494592015-09-25 02:29:14 +0000234 if (x == NULL || y == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800235 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700236 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237
Adam Langleye9ada862015-05-11 17:20:37 -0700238 if (!BN_bin2bn(buf + 1, field_len, x)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800239 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700240 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241 if (BN_ucmp(x, &group->field) >= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000242 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 goto err;
244 }
245
246 if (form == POINT_CONVERSION_COMPRESSED) {
Adam Langleye9ada862015-05-11 17:20:37 -0700247 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800248 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700249 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250 } else {
Adam Langleye9ada862015-05-11 17:20:37 -0700251 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800252 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700253 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800254 if (BN_ucmp(y, &group->field) >= 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000255 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_ENCODING);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800256 goto err;
257 }
258
Adam Langleye9ada862015-05-11 17:20:37 -0700259 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800260 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700261 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800262 }
263
Adam Langleye9ada862015-05-11 17:20:37 -0700264 /* test required by X9.62 */
265 if (!EC_POINT_is_on_curve(group, point, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000266 OPENSSL_PUT_ERROR(EC, EC_R_POINT_IS_NOT_ON_CURVE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800267 goto err;
268 }
269
270 ret = 1;
271
272err:
273 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700274 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800275 return ret;
276}
277
278int 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 Rootb8494592015-09-25 02:29:14 +0000282 OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800283 return 0;
284 }
285 if (group->meth != point->meth) {
Kenny Rootb8494592015-09-25 02:29:14 +0000286 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800287 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
296size_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 Rootb8494592015-09-25 02:29:14 +0000301 OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800302 return 0;
303 }
304 if (group->meth != point->meth) {
Kenny Rootb8494592015-09-25 02:29:14 +0000305 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800306 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
315int 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 Rootb8494592015-09-25 02:29:14 +0000407 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800408 } else {
Kenny Rootb8494592015-09-25 02:29:14 +0000409 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800410 }
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 Rootb8494592015-09-25 02:29:14 +0000424 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSION_BIT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800425 } else {
426 /* BN_mod_sqrt() should have cought this error (not a square) */
Kenny Rootb8494592015-09-25 02:29:14 +0000427 OPENSSL_PUT_ERROR(EC, EC_R_INVALID_COMPRESSED_POINT);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800428 }
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 Rootb8494592015-09-25 02:29:14 +0000436 OPENSSL_PUT_ERROR(EC, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800437 goto err;
438 }
439
Adam Langleye9ada862015-05-11 17:20:37 -0700440 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441 goto err;
Adam Langleye9ada862015-05-11 17:20:37 -0700442 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443
444 ret = 1;
445
446err:
447 BN_CTX_end(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700448 BN_CTX_free(new_ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800449 return ret;
450}
451
452int 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 Rootb8494592015-09-25 02:29:14 +0000457 OPENSSL_PUT_ERROR(EC, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800458 return 0;
459 }
460 if (group->meth != point->meth) {
Kenny Rootb8494592015-09-25 02:29:14 +0000461 OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800462 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}