1 /* Originally written by Bodo Moeller for the OpenSSL project.
2 * ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
55 /* ====================================================================
56 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
58 * Portions of the attached software ("Contribution") are developed by
59 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
61 * The Contribution is licensed pursuant to the OpenSSL open source
62 * license provided above.
64 * The elliptic curve binary polynomial software is originally written by
65 * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
68 #include <openssl/ec.h>
70 #include <openssl/bn.h>
71 #include <openssl/err.h>
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) {
81 BN_CTX *new_ctx = NULL;
84 size_t field_len, i, skip;
86 if ((form != POINT_CONVERSION_COMPRESSED) &&
87 (form != POINT_CONVERSION_UNCOMPRESSED) &&
88 (form != POINT_CONVERSION_HYBRID)) {
89 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, EC_R_INVALID_FORM);
93 if (EC_POINT_is_at_infinity(group, point)) {
94 /* encodes to a single 0 octet */
97 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, EC_R_BUFFER_TOO_SMALL);
106 /* ret := required output buffer length */
107 field_len = BN_num_bytes(&group->field);
109 (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
111 /* if 'buf' is NULL, just return required length */
114 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, EC_R_BUFFER_TOO_SMALL);
119 ctx = new_ctx = BN_CTX_new();
131 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
134 if ((form == POINT_CONVERSION_COMPRESSED ||
135 form == POINT_CONVERSION_HYBRID) &&
143 skip = field_len - BN_num_bytes(x);
144 if (skip > field_len) {
145 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, ERR_R_INTERNAL_ERROR);
152 skip = BN_bn2bin(x, buf + i);
154 if (i != 1 + field_len) {
155 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, ERR_R_INTERNAL_ERROR);
159 if (form == POINT_CONVERSION_UNCOMPRESSED ||
160 form == POINT_CONVERSION_HYBRID) {
161 skip = field_len - BN_num_bytes(y);
162 if (skip > field_len) {
163 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, ERR_R_INTERNAL_ERROR);
170 skip = BN_bn2bin(y, buf + i);
175 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_point2oct, ERR_R_INTERNAL_ERROR);
183 BN_CTX_free(new_ctx);
190 BN_CTX_free(new_ctx);
195 static int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
196 const uint8_t *buf, size_t len,
198 point_conversion_form_t form;
200 BN_CTX *new_ctx = NULL;
202 size_t field_len, enc_len;
206 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_BUFFER_TOO_SMALL);
212 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED) &&
213 (form != POINT_CONVERSION_UNCOMPRESSED) &&
214 (form != POINT_CONVERSION_HYBRID)) {
215 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
218 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
219 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
225 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
229 return EC_POINT_set_to_infinity(group, point);
232 field_len = BN_num_bytes(&group->field);
234 (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
236 if (len != enc_len) {
237 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
242 ctx = new_ctx = BN_CTX_new();
253 if (!BN_bin2bn(buf + 1, field_len, x))
255 if (BN_ucmp(x, &group->field) >= 0) {
256 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
260 if (form == POINT_CONVERSION_COMPRESSED) {
261 if (!EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, ctx))
264 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
266 if (BN_ucmp(y, &group->field) >= 0) {
267 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
270 if (form == POINT_CONVERSION_HYBRID) {
271 if (y_bit != BN_is_odd(y)) {
272 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_INVALID_ENCODING);
277 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
281 if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
283 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_oct2point, EC_R_POINT_IS_NOT_ON_CURVE);
292 BN_CTX_free(new_ctx);
296 int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
297 const uint8_t *buf, size_t len, BN_CTX *ctx) {
298 if (group->meth->oct2point == 0 &&
299 !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
300 OPENSSL_PUT_ERROR(EC, EC_POINT_oct2point,
301 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
304 if (group->meth != point->meth) {
305 OPENSSL_PUT_ERROR(EC, EC_POINT_oct2point, EC_R_INCOMPATIBLE_OBJECTS);
308 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
309 return ec_GFp_simple_oct2point(group, point, buf, len, ctx);
312 return group->meth->oct2point(group, point, buf, len, ctx);
315 size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *point,
316 point_conversion_form_t form, uint8_t *buf,
317 size_t len, BN_CTX *ctx) {
318 if (group->meth->point2oct == 0 &&
319 !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
320 OPENSSL_PUT_ERROR(EC, EC_POINT_point2oct,
321 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
324 if (group->meth != point->meth) {
325 OPENSSL_PUT_ERROR(EC, EC_POINT_point2oct, EC_R_INCOMPATIBLE_OBJECTS);
328 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
329 return ec_GFp_simple_point2oct(group, point, form, buf, len, ctx);
332 return group->meth->point2oct(group, point, form, buf, len, ctx);
335 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
336 EC_POINT *point, const BIGNUM *x_,
337 int y_bit, BN_CTX *ctx) {
338 BN_CTX *new_ctx = NULL;
339 BIGNUM *tmp1, *tmp2, *x, *y;
345 ctx = new_ctx = BN_CTX_new();
351 y_bit = (y_bit != 0);
354 tmp1 = BN_CTX_get(ctx);
355 tmp2 = BN_CTX_get(ctx);
362 /* Recover y. We have a Weierstrass equation
363 * y^2 = x^3 + a*x + b,
364 * so y is one of the square roots of x^3 + a*x + b. */
367 if (!BN_nnmod(x, x_, &group->field, ctx)) {
371 if (group->meth->field_decode == 0) {
372 /* field_{sqr,mul} work on standard representation */
373 if (!group->meth->field_sqr(group, tmp2, x_, ctx) ||
374 !group->meth->field_mul(group, tmp1, tmp2, x_, ctx)) {
378 if (!BN_mod_sqr(tmp2, x_, &group->field, ctx) ||
379 !BN_mod_mul(tmp1, tmp2, x_, &group->field, ctx)) {
384 /* tmp1 := tmp1 + a*x */
385 if (group->a_is_minus3) {
386 if (!BN_mod_lshift1_quick(tmp2, x, &group->field) ||
387 !BN_mod_add_quick(tmp2, tmp2, x, &group->field) ||
388 !BN_mod_sub_quick(tmp1, tmp1, tmp2, &group->field)) {
392 if (group->meth->field_decode) {
393 if (!group->meth->field_decode(group, tmp2, &group->a, ctx) ||
394 !BN_mod_mul(tmp2, tmp2, x, &group->field, ctx)) {
398 /* field_mul works on standard representation */
399 if (!group->meth->field_mul(group, tmp2, &group->a, x, ctx)) {
404 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
409 /* tmp1 := tmp1 + b */
410 if (group->meth->field_decode) {
411 if (!group->meth->field_decode(group, tmp2, &group->b, ctx) ||
412 !BN_mod_add_quick(tmp1, tmp1, tmp2, &group->field)) {
416 if (!BN_mod_add_quick(tmp1, tmp1, &group->b, &group->field)) {
421 if (!BN_mod_sqrt(y, tmp1, &group->field, ctx)) {
422 unsigned long err = ERR_peek_last_error();
424 if (ERR_GET_LIB(err) == ERR_LIB_BN &&
425 ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
427 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates, EC_R_INVALID_COMPRESSED_POINT);
429 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates, ERR_R_BN_LIB);
434 if (y_bit != BN_is_odd(y)) {
438 kron = BN_kronecker(x, &group->field, ctx);
444 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates,
445 EC_R_INVALID_COMPRESSION_BIT);
447 /* BN_mod_sqrt() should have cought this error (not a square) */
448 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates,
449 EC_R_INVALID_COMPRESSED_POINT);
453 if (!BN_usub(y, &group->field, y)) {
457 if (y_bit != BN_is_odd(y)) {
458 OPENSSL_PUT_ERROR(EC, ec_GFp_simple_set_compressed_coordinates,
459 ERR_R_INTERNAL_ERROR);
463 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
471 BN_CTX_free(new_ctx);
475 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
476 EC_POINT *point, const BIGNUM *x,
477 int y_bit, BN_CTX *ctx) {
478 if (group->meth->point_set_compressed_coordinates == 0 &&
479 !(group->meth->flags & EC_FLAGS_DEFAULT_OCT)) {
480 OPENSSL_PUT_ERROR(EC, EC_POINT_set_compressed_coordinates_GFp,
481 ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
484 if (group->meth != point->meth) {
485 OPENSSL_PUT_ERROR(EC, EC_POINT_set_compressed_coordinates_GFp,
486 EC_R_INCOMPATIBLE_OBJECTS);
489 if (group->meth->flags & EC_FLAGS_DEFAULT_OCT) {
490 return ec_GFp_simple_set_compressed_coordinates(group, point, x, y_bit,
493 return group->meth->point_set_compressed_coordinates(group, point, x, y_bit,