1 /* $OpenBSD: ec2_oct.c,v 1.8 2017/01/29 17:49:23 beck Exp $ */
2 /* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
5 * The Elliptic Curve Public-Key Crypto Library (ECC Code) included
6 * herein is developed by SUN MICROSYSTEMS, INC., and is contributed
7 * to the OpenSSL project.
9 * The ECC Code is licensed pursuant to the OpenSSL open source
10 * license provided below.
12 * The software is originally written by Sheueling Chang Shantz and
13 * Douglas Stebila of Sun Microsystems Laboratories.
16 /* ====================================================================
17 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
31 * 3. All advertising materials mentioning features or use of this
32 * software must display the following acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
36 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
37 * endorse or promote products derived from this software without
38 * prior written permission. For written permission, please contact
39 * openssl-core@openssl.org.
41 * 5. Products derived from this software may not be called "OpenSSL"
42 * nor may "OpenSSL" appear in their names without prior written
43 * permission of the OpenSSL Project.
45 * 6. Redistributions of any form whatsoever must retain the following
47 * "This product includes software developed by the OpenSSL Project
48 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
50 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
51 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
54 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
61 * OF THE POSSIBILITY OF SUCH DAMAGE.
62 * ====================================================================
64 * This product includes cryptographic software written by Eric Young
65 * (eay@cryptsoft.com). This product includes software written by Tim
66 * Hudson (tjh@cryptsoft.com).
70 #include <openssl/opensslconf.h>
72 #include <openssl/err.h>
76 #ifndef OPENSSL_NO_EC2M
78 /* Calculates and sets the affine coordinates of an EC_POINT from the given
79 * compressed coordinates. Uses algorithm 2.3.4 of SEC 1.
80 * Note that the simple implementation only uses affine coordinates.
82 * The method is from the following publication:
84 * Harper, Menezes, Vanstone:
85 * "Public-Key Cryptosystems with Very Small Key Lengths",
86 * EUROCRYPT '92, Springer-Verlag LNCS 658,
87 * published February 1993
89 * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
90 * the same method, but claim no priority date earlier than July 29, 1994
91 * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
94 ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP
*group
, EC_POINT
*point
,
95 const BIGNUM
*x_
, int y_bit
, BN_CTX
*ctx
)
97 BN_CTX
*new_ctx
= NULL
;
98 BIGNUM
*tmp
, *x
, *y
, *z
;
101 /* clear error queue */
105 ctx
= new_ctx
= BN_CTX_new();
109 y_bit
= (y_bit
!= 0) ? 1 : 0;
112 if ((tmp
= BN_CTX_get(ctx
)) == NULL
)
114 if ((x
= BN_CTX_get(ctx
)) == NULL
)
116 if ((y
= BN_CTX_get(ctx
)) == NULL
)
118 if ((z
= BN_CTX_get(ctx
)) == NULL
)
121 if (!BN_GF2m_mod_arr(x
, x_
, group
->poly
))
124 if (!BN_GF2m_mod_sqrt_arr(y
, &group
->b
, group
->poly
, ctx
))
127 if (!group
->meth
->field_sqr(group
, tmp
, x
, ctx
))
129 if (!group
->meth
->field_div(group
, tmp
, &group
->b
, tmp
, ctx
))
131 if (!BN_GF2m_add(tmp
, &group
->a
, tmp
))
133 if (!BN_GF2m_add(tmp
, x
, tmp
))
135 if (!BN_GF2m_mod_solve_quad_arr(z
, tmp
, group
->poly
, ctx
)) {
136 unsigned long err
= ERR_peek_last_error();
138 if (ERR_GET_LIB(err
) == ERR_LIB_BN
&&
139 ERR_GET_REASON(err
) == BN_R_NO_SOLUTION
) {
141 ECerror(EC_R_INVALID_COMPRESSED_POINT
);
143 ECerror(ERR_R_BN_LIB
);
146 z0
= (BN_is_odd(z
)) ? 1 : 0;
147 if (!group
->meth
->field_mul(group
, y
, x
, z
, ctx
))
150 if (!BN_GF2m_add(y
, y
, x
))
155 if (!EC_POINT_set_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
162 BN_CTX_free(new_ctx
);
167 /* Converts an EC_POINT to an octet string.
168 * If buf is NULL, the encoded length will be returned.
169 * If the length len of buf is smaller than required an error will be returned.
172 ec_GF2m_simple_point2oct(const EC_GROUP
*group
, const EC_POINT
*point
,
173 point_conversion_form_t form
,
174 unsigned char *buf
, size_t len
, BN_CTX
* ctx
)
177 BN_CTX
*new_ctx
= NULL
;
180 size_t field_len
, i
, skip
;
182 if ((form
!= POINT_CONVERSION_COMPRESSED
)
183 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
184 && (form
!= POINT_CONVERSION_HYBRID
)) {
185 ECerror(EC_R_INVALID_FORM
);
188 if (EC_POINT_is_at_infinity(group
, point
) > 0) {
189 /* encodes to a single 0 octet */
192 ECerror(EC_R_BUFFER_TOO_SMALL
);
199 /* ret := required output buffer length */
200 field_len
= (EC_GROUP_get_degree(group
) + 7) / 8;
201 ret
= (form
== POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
:
204 /* if 'buf' is NULL, just return required length */
207 ECerror(EC_R_BUFFER_TOO_SMALL
);
211 ctx
= new_ctx
= BN_CTX_new();
217 if ((x
= BN_CTX_get(ctx
)) == NULL
)
219 if ((y
= BN_CTX_get(ctx
)) == NULL
)
221 if ((yxi
= BN_CTX_get(ctx
)) == NULL
)
224 if (!EC_POINT_get_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
228 if ((form
!= POINT_CONVERSION_UNCOMPRESSED
) && !BN_is_zero(x
)) {
229 if (!group
->meth
->field_div(group
, yxi
, y
, x
, ctx
))
236 skip
= field_len
- BN_num_bytes(x
);
237 if (skip
> field_len
) {
238 ECerror(ERR_R_INTERNAL_ERROR
);
245 skip
= BN_bn2bin(x
, buf
+ i
);
247 if (i
!= 1 + field_len
) {
248 ECerror(ERR_R_INTERNAL_ERROR
);
251 if (form
== POINT_CONVERSION_UNCOMPRESSED
||
252 form
== POINT_CONVERSION_HYBRID
) {
253 skip
= field_len
- BN_num_bytes(y
);
254 if (skip
> field_len
) {
255 ECerror(ERR_R_INTERNAL_ERROR
);
262 skip
= BN_bn2bin(y
, buf
+ i
);
266 ECerror(ERR_R_INTERNAL_ERROR
);
272 BN_CTX_free(new_ctx
);
278 BN_CTX_free(new_ctx
);
283 /* Converts an octet string representation to an EC_POINT.
284 * Note that the simple implementation only uses affine coordinates.
287 ec_GF2m_simple_oct2point(const EC_GROUP
*group
, EC_POINT
*point
,
288 const unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
290 point_conversion_form_t form
;
292 BN_CTX
*new_ctx
= NULL
;
294 size_t field_len
, enc_len
;
298 ECerror(EC_R_BUFFER_TOO_SMALL
);
304 if ((form
!= 0) && (form
!= POINT_CONVERSION_COMPRESSED
) &&
305 (form
!= POINT_CONVERSION_UNCOMPRESSED
) &&
306 (form
!= POINT_CONVERSION_HYBRID
)) {
307 ECerror(EC_R_INVALID_ENCODING
);
310 if ((form
== 0 || form
== POINT_CONVERSION_UNCOMPRESSED
) && y_bit
) {
311 ECerror(EC_R_INVALID_ENCODING
);
316 ECerror(EC_R_INVALID_ENCODING
);
319 return EC_POINT_set_to_infinity(group
, point
);
321 field_len
= (EC_GROUP_get_degree(group
) + 7) / 8;
322 enc_len
= (form
== POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
:
325 if (len
!= enc_len
) {
326 ECerror(EC_R_INVALID_ENCODING
);
330 ctx
= new_ctx
= BN_CTX_new();
335 if ((x
= BN_CTX_get(ctx
)) == NULL
)
337 if ((y
= BN_CTX_get(ctx
)) == NULL
)
339 if ((yxi
= BN_CTX_get(ctx
)) == NULL
)
342 if (!BN_bin2bn(buf
+ 1, field_len
, x
))
344 if (BN_ucmp(x
, &group
->field
) >= 0) {
345 ECerror(EC_R_INVALID_ENCODING
);
348 if (form
== POINT_CONVERSION_COMPRESSED
) {
349 if (!EC_POINT_set_compressed_coordinates_GF2m(group
, point
, x
, y_bit
, ctx
))
352 if (!BN_bin2bn(buf
+ 1 + field_len
, field_len
, y
))
354 if (BN_ucmp(y
, &group
->field
) >= 0) {
355 ECerror(EC_R_INVALID_ENCODING
);
358 if (form
== POINT_CONVERSION_HYBRID
) {
359 if (!group
->meth
->field_div(group
, yxi
, y
, x
, ctx
))
361 if (y_bit
!= BN_is_odd(yxi
)) {
362 ECerror(EC_R_INVALID_ENCODING
);
366 if (!EC_POINT_set_affine_coordinates_GF2m(group
, point
, x
, y
, ctx
))
370 /* test required by X9.62 */
371 if (EC_POINT_is_on_curve(group
, point
, ctx
) <= 0) {
372 ECerror(EC_R_POINT_IS_NOT_ON_CURVE
);
379 BN_CTX_free(new_ctx
);