2 * Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * for the OpenSSL project. Includes code written by Bodo Moeller for the
6 /* ====================================================================
7 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@openssl.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* ====================================================================
60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
62 * and contributed to the OpenSSL project.
65 #include <openssl/err.h>
66 #include <openssl/symhacks.h>
70 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
72 const BIGNUM *x_, int y_bit,
75 BN_CTX *new_ctx = NULL;
76 BIGNUM *tmp1, *tmp2, *x, *y;
79 /* clear error queue */
83 ctx = new_ctx = BN_CTX_new();
91 tmp1 = BN_CTX_get(ctx);
92 tmp2 = BN_CTX_get(ctx);
99 * Recover y. We have a Weierstrass equation
100 * y^2 = x^3 + a*x + b,
101 * so y is one of the square roots of x^3 + a*x + b.
105 if (!BN_nnmod(x, x_, group->field, ctx))
107 if (group->meth->field_decode == 0) {
108 /* field_{sqr,mul} work on standard representation */
109 if (!group->meth->field_sqr(group, tmp2, x_, ctx))
111 if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
114 if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
116 if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
120 /* tmp1 := tmp1 + a*x */
121 if (group->a_is_minus3) {
122 if (!BN_mod_lshift1_quick(tmp2, x, group->field))
124 if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
126 if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
129 if (group->meth->field_decode) {
130 if (!group->meth->field_decode(group, tmp2, group->a, ctx))
132 if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
135 /* field_mul works on standard representation */
136 if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
140 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
144 /* tmp1 := tmp1 + b */
145 if (group->meth->field_decode) {
146 if (!group->meth->field_decode(group, tmp2, group->b, ctx))
148 if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
151 if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
155 if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
156 unsigned long err = ERR_peek_last_error();
158 if (ERR_GET_LIB(err) == ERR_LIB_BN
159 && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
161 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
162 EC_R_INVALID_COMPRESSED_POINT);
164 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
169 if (y_bit != BN_is_odd(y)) {
173 kron = BN_kronecker(x, group->field, ctx);
178 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
179 EC_R_INVALID_COMPRESSION_BIT);
182 * BN_mod_sqrt() should have cought this error (not a square)
184 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
185 EC_R_INVALID_COMPRESSED_POINT);
188 if (!BN_usub(y, group->field, y))
191 if (y_bit != BN_is_odd(y)) {
192 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
193 ERR_R_INTERNAL_ERROR);
197 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
204 BN_CTX_free(new_ctx);
208 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
209 point_conversion_form_t form,
210 unsigned char *buf, size_t len, BN_CTX *ctx)
213 BN_CTX *new_ctx = NULL;
216 size_t field_len, i, skip;
218 if ((form != POINT_CONVERSION_COMPRESSED)
219 && (form != POINT_CONVERSION_UNCOMPRESSED)
220 && (form != POINT_CONVERSION_HYBRID)) {
221 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
225 if (EC_POINT_is_at_infinity(group, point)) {
226 /* encodes to a single 0 octet */
229 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
237 /* ret := required output buffer length */
238 field_len = BN_num_bytes(group->field);
241 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
243 /* if 'buf' is NULL, just return required length */
246 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
251 ctx = new_ctx = BN_CTX_new();
263 if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
266 if ((form == POINT_CONVERSION_COMPRESSED
267 || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
274 skip = field_len - BN_num_bytes(x);
275 if (skip > field_len) {
276 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
283 skip = BN_bn2bin(x, buf + i);
285 if (i != 1 + field_len) {
286 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
290 if (form == POINT_CONVERSION_UNCOMPRESSED
291 || form == POINT_CONVERSION_HYBRID) {
292 skip = field_len - BN_num_bytes(y);
293 if (skip > field_len) {
294 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
301 skip = BN_bn2bin(y, buf + i);
306 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
313 BN_CTX_free(new_ctx);
319 BN_CTX_free(new_ctx);
323 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
324 const unsigned char *buf, size_t len, BN_CTX *ctx)
326 point_conversion_form_t form;
328 BN_CTX *new_ctx = NULL;
330 size_t field_len, enc_len;
334 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
340 if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
341 && (form != POINT_CONVERSION_UNCOMPRESSED)
342 && (form != POINT_CONVERSION_HYBRID)) {
343 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
346 if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
347 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
353 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
357 return EC_POINT_set_to_infinity(group, point);
360 field_len = BN_num_bytes(group->field);
363 POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
365 if (len != enc_len) {
366 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
371 ctx = new_ctx = BN_CTX_new();
382 if (!BN_bin2bn(buf + 1, field_len, x))
384 if (BN_ucmp(x, group->field) >= 0) {
385 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
389 if (form == POINT_CONVERSION_COMPRESSED) {
390 if (!EC_POINT_set_compressed_coordinates_GFp
391 (group, point, x, y_bit, ctx))
394 if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
396 if (BN_ucmp(y, group->field) >= 0) {
397 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
400 if (form == POINT_CONVERSION_HYBRID) {
401 if (y_bit != BN_is_odd(y)) {
402 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
407 if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
411 /* test required by X9.62 */
412 if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
413 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
421 BN_CTX_free(new_ctx);