1 /* crypto/ec/ecp_oct.c */
3 * Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
4 * for the OpenSSL project. Includes code written by Bodo Moeller for the
7 /* ====================================================================
8 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * openssl-core@openssl.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
60 /* ====================================================================
61 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
62 * Portions of this software developed by SUN MICROSYSTEMS, INC.,
63 * and contributed to the OpenSSL project.
66 #include <openssl/err.h>
67 #include <openssl/symhacks.h>
71 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP
*group
,
73 const BIGNUM
*x_
, int y_bit
,
76 BN_CTX
*new_ctx
= NULL
;
77 BIGNUM
*tmp1
, *tmp2
, *x
, *y
;
80 /* clear error queue */
84 ctx
= new_ctx
= BN_CTX_new();
92 tmp1
= BN_CTX_get(ctx
);
93 tmp2
= BN_CTX_get(ctx
);
100 * Recover y. We have a Weierstrass equation
101 * y^2 = x^3 + a*x + b,
102 * so y is one of the square roots of x^3 + a*x + b.
106 if (!BN_nnmod(x
, x_
, &group
->field
, ctx
))
108 if (group
->meth
->field_decode
== 0) {
109 /* field_{sqr,mul} work on standard representation */
110 if (!group
->meth
->field_sqr(group
, tmp2
, x_
, ctx
))
112 if (!group
->meth
->field_mul(group
, tmp1
, tmp2
, x_
, ctx
))
115 if (!BN_mod_sqr(tmp2
, x_
, &group
->field
, ctx
))
117 if (!BN_mod_mul(tmp1
, tmp2
, x_
, &group
->field
, ctx
))
121 /* tmp1 := tmp1 + a*x */
122 if (group
->a_is_minus3
) {
123 if (!BN_mod_lshift1_quick(tmp2
, x
, &group
->field
))
125 if (!BN_mod_add_quick(tmp2
, tmp2
, x
, &group
->field
))
127 if (!BN_mod_sub_quick(tmp1
, tmp1
, tmp2
, &group
->field
))
130 if (group
->meth
->field_decode
) {
131 if (!group
->meth
->field_decode(group
, tmp2
, &group
->a
, ctx
))
133 if (!BN_mod_mul(tmp2
, tmp2
, x
, &group
->field
, ctx
))
136 /* field_mul works on standard representation */
137 if (!group
->meth
->field_mul(group
, tmp2
, &group
->a
, x
, ctx
))
141 if (!BN_mod_add_quick(tmp1
, tmp1
, tmp2
, &group
->field
))
145 /* tmp1 := tmp1 + b */
146 if (group
->meth
->field_decode
) {
147 if (!group
->meth
->field_decode(group
, tmp2
, &group
->b
, ctx
))
149 if (!BN_mod_add_quick(tmp1
, tmp1
, tmp2
, &group
->field
))
152 if (!BN_mod_add_quick(tmp1
, tmp1
, &group
->b
, &group
->field
))
156 if (!BN_mod_sqrt(y
, tmp1
, &group
->field
, ctx
)) {
157 unsigned long err
= ERR_peek_last_error();
159 if (ERR_GET_LIB(err
) == ERR_LIB_BN
160 && ERR_GET_REASON(err
) == BN_R_NOT_A_SQUARE
) {
162 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES
,
163 EC_R_INVALID_COMPRESSED_POINT
);
165 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES
,
170 if (y_bit
!= BN_is_odd(y
)) {
174 kron
= BN_kronecker(x
, &group
->field
, ctx
);
179 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES
,
180 EC_R_INVALID_COMPRESSION_BIT
);
183 * BN_mod_sqrt() should have cought this error (not a square)
185 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES
,
186 EC_R_INVALID_COMPRESSED_POINT
);
189 if (!BN_usub(y
, &group
->field
, y
))
192 if (y_bit
!= BN_is_odd(y
)) {
193 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES
,
194 ERR_R_INTERNAL_ERROR
);
198 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
206 BN_CTX_free(new_ctx
);
210 size_t ec_GFp_simple_point2oct(const EC_GROUP
*group
, const EC_POINT
*point
,
211 point_conversion_form_t form
,
212 unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
215 BN_CTX
*new_ctx
= NULL
;
218 size_t field_len
, i
, skip
;
220 if ((form
!= POINT_CONVERSION_COMPRESSED
)
221 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
222 && (form
!= POINT_CONVERSION_HYBRID
)) {
223 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, EC_R_INVALID_FORM
);
227 if (EC_POINT_is_at_infinity(group
, point
)) {
228 /* encodes to a single 0 octet */
231 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, EC_R_BUFFER_TOO_SMALL
);
239 /* ret := required output buffer length */
240 field_len
= BN_num_bytes(&group
->field
);
243 POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2 * field_len
;
245 /* if 'buf' is NULL, just return required length */
248 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, EC_R_BUFFER_TOO_SMALL
);
253 ctx
= new_ctx
= BN_CTX_new();
265 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
268 if ((form
== POINT_CONVERSION_COMPRESSED
269 || form
== POINT_CONVERSION_HYBRID
) && BN_is_odd(y
))
276 skip
= field_len
- BN_num_bytes(x
);
277 if (skip
> field_len
) {
278 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
285 skip
= BN_bn2bin(x
, buf
+ i
);
287 if (i
!= 1 + field_len
) {
288 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
292 if (form
== POINT_CONVERSION_UNCOMPRESSED
293 || form
== POINT_CONVERSION_HYBRID
) {
294 skip
= field_len
- BN_num_bytes(y
);
295 if (skip
> field_len
) {
296 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
303 skip
= BN_bn2bin(y
, buf
+ i
);
308 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
316 BN_CTX_free(new_ctx
);
323 BN_CTX_free(new_ctx
);
327 int ec_GFp_simple_oct2point(const EC_GROUP
*group
, EC_POINT
*point
,
328 const unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
330 point_conversion_form_t form
;
332 BN_CTX
*new_ctx
= NULL
;
334 size_t field_len
, enc_len
;
338 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_BUFFER_TOO_SMALL
);
344 if ((form
!= 0) && (form
!= POINT_CONVERSION_COMPRESSED
)
345 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
346 && (form
!= POINT_CONVERSION_HYBRID
)) {
347 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
350 if ((form
== 0 || form
== POINT_CONVERSION_UNCOMPRESSED
) && y_bit
) {
351 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
357 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
361 return EC_POINT_set_to_infinity(group
, point
);
364 field_len
= BN_num_bytes(&group
->field
);
367 POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2 * field_len
;
369 if (len
!= enc_len
) {
370 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
375 ctx
= new_ctx
= BN_CTX_new();
386 if (!BN_bin2bn(buf
+ 1, field_len
, x
))
388 if (BN_ucmp(x
, &group
->field
) >= 0) {
389 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
393 if (form
== POINT_CONVERSION_COMPRESSED
) {
394 if (!EC_POINT_set_compressed_coordinates_GFp
395 (group
, point
, x
, y_bit
, ctx
))
398 if (!BN_bin2bn(buf
+ 1 + field_len
, field_len
, y
))
400 if (BN_ucmp(y
, &group
->field
) >= 0) {
401 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
404 if (form
== POINT_CONVERSION_HYBRID
) {
405 if (y_bit
!= BN_is_odd(y
)) {
406 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
412 * EC_POINT_set_affine_coordinates_GFp is responsible for checking that
413 * the point is on the curve.
415 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
))
424 BN_CTX_free(new_ctx
);