free NULL cleanup 7
[openssl.git] / crypto / ec / ec2_oct.c
1 /* crypto/ec/ec2_oct.c */
2 /* ====================================================================
3  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4  *
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.
8  *
9  * The ECC Code is licensed pursuant to the OpenSSL open source
10  * license provided below.
11  *
12  * The software is originally written by Sheueling Chang Shantz and
13  * Douglas Stebila of Sun Microsystems Laboratories.
14  *
15  */
16 /* ====================================================================
17  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  *
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  *
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
29  *    distribution.
30  *
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/)"
35  *
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.
40  *
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.
44  *
45  * 6. Redistributions of any form whatsoever must retain the following
46  *    acknowledgment:
47  *    "This product includes software developed by the OpenSSL Project
48  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
49  *
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  * ====================================================================
63  *
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).
67  *
68  */
69
70 #include <openssl/err.h>
71
72 #include "ec_lcl.h"
73
74 #ifndef OPENSSL_NO_EC2M
75
76 /*-
77  * Calculates and sets the affine coordinates of an EC_POINT from the given
78  * compressed coordinates.  Uses algorithm 2.3.4 of SEC 1.
79  * Note that the simple implementation only uses affine coordinates.
80  *
81  * The method is from the following publication:
82  *
83  *     Harper, Menezes, Vanstone:
84  *     "Public-Key Cryptosystems with Very Small Key Lengths",
85  *     EUROCRYPT '92, Springer-Verlag LNCS 658,
86  *     published February 1993
87  *
88  * US Patents 6,141,420 and 6,618,483 (Vanstone, Mullin, Agnew) describe
89  * the same method, but claim no priority date earlier than July 29, 1994
90  * (and additionally fail to cite the EUROCRYPT '92 publication as prior art).
91  */
92 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group,
93                                               EC_POINT *point,
94                                               const BIGNUM *x_, int y_bit,
95                                               BN_CTX *ctx)
96 {
97     BN_CTX *new_ctx = NULL;
98     BIGNUM *tmp, *x, *y, *z;
99     int ret = 0, z0;
100
101     /* clear error queue */
102     ERR_clear_error();
103
104     if (ctx == NULL) {
105         ctx = new_ctx = BN_CTX_new();
106         if (ctx == NULL)
107             return 0;
108     }
109
110     y_bit = (y_bit != 0) ? 1 : 0;
111
112     BN_CTX_start(ctx);
113     tmp = BN_CTX_get(ctx);
114     x = BN_CTX_get(ctx);
115     y = BN_CTX_get(ctx);
116     z = BN_CTX_get(ctx);
117     if (z == NULL)
118         goto err;
119
120     if (!BN_GF2m_mod_arr(x, x_, group->poly))
121         goto err;
122     if (BN_is_zero(x)) {
123         if (!BN_GF2m_mod_sqrt_arr(y, group->b, group->poly, ctx))
124             goto err;
125     } else {
126         if (!group->meth->field_sqr(group, tmp, x, ctx))
127             goto err;
128         if (!group->meth->field_div(group, tmp, group->b, tmp, ctx))
129             goto err;
130         if (!BN_GF2m_add(tmp, group->a, tmp))
131             goto err;
132         if (!BN_GF2m_add(tmp, x, tmp))
133             goto err;
134         if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx)) {
135             unsigned long err = ERR_peek_last_error();
136
137             if (ERR_GET_LIB(err) == ERR_LIB_BN
138                 && ERR_GET_REASON(err) == BN_R_NO_SOLUTION) {
139                 ERR_clear_error();
140                 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
141                       EC_R_INVALID_COMPRESSED_POINT);
142             } else
143                 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES,
144                       ERR_R_BN_LIB);
145             goto err;
146         }
147         z0 = (BN_is_odd(z)) ? 1 : 0;
148         if (!group->meth->field_mul(group, y, x, z, ctx))
149             goto err;
150         if (z0 != y_bit) {
151             if (!BN_GF2m_add(y, y, x))
152                 goto err;
153         }
154     }
155
156     if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))
157         goto err;
158
159     ret = 1;
160
161  err:
162     BN_CTX_end(ctx);
163     BN_CTX_free(new_ctx);
164     return ret;
165 }
166
167 /*
168  * Converts an EC_POINT to an octet string. If buf is NULL, the encoded
169  * length will be returned. If the length len of buf is smaller than required
170  * an error will be returned.
171  */
172 size_t 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)
175 {
176     size_t ret;
177     BN_CTX *new_ctx = NULL;
178     int used_ctx = 0;
179     BIGNUM *x, *y, *yxi;
180     size_t field_len, i, skip;
181
182     if ((form != POINT_CONVERSION_COMPRESSED)
183         && (form != POINT_CONVERSION_UNCOMPRESSED)
184         && (form != POINT_CONVERSION_HYBRID)) {
185         ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
186         goto err;
187     }
188
189     if (EC_POINT_is_at_infinity(group, point)) {
190         /* encodes to a single 0 octet */
191         if (buf != NULL) {
192             if (len < 1) {
193                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
194                 return 0;
195             }
196             buf[0] = 0;
197         }
198         return 1;
199     }
200
201     /* ret := required output buffer length */
202     field_len = (EC_GROUP_get_degree(group) + 7) / 8;
203     ret =
204         (form ==
205          POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
206
207     /* if 'buf' is NULL, just return required length */
208     if (buf != NULL) {
209         if (len < ret) {
210             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
211             goto err;
212         }
213
214         if (ctx == NULL) {
215             ctx = new_ctx = BN_CTX_new();
216             if (ctx == NULL)
217                 return 0;
218         }
219
220         BN_CTX_start(ctx);
221         used_ctx = 1;
222         x = BN_CTX_get(ctx);
223         y = BN_CTX_get(ctx);
224         yxi = BN_CTX_get(ctx);
225         if (yxi == NULL)
226             goto err;
227
228         if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx))
229             goto err;
230
231         buf[0] = form;
232         if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x)) {
233             if (!group->meth->field_div(group, yxi, y, x, ctx))
234                 goto err;
235             if (BN_is_odd(yxi))
236                 buf[0]++;
237         }
238
239         i = 1;
240
241         skip = field_len - BN_num_bytes(x);
242         if (skip > field_len) {
243             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
244             goto err;
245         }
246         while (skip > 0) {
247             buf[i++] = 0;
248             skip--;
249         }
250         skip = BN_bn2bin(x, buf + i);
251         i += skip;
252         if (i != 1 + field_len) {
253             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
254             goto err;
255         }
256
257         if (form == POINT_CONVERSION_UNCOMPRESSED
258             || form == POINT_CONVERSION_HYBRID) {
259             skip = field_len - BN_num_bytes(y);
260             if (skip > field_len) {
261                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
262                 goto err;
263             }
264             while (skip > 0) {
265                 buf[i++] = 0;
266                 skip--;
267             }
268             skip = BN_bn2bin(y, buf + i);
269             i += skip;
270         }
271
272         if (i != ret) {
273             ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
274             goto err;
275         }
276     }
277
278     if (used_ctx)
279         BN_CTX_end(ctx);
280     BN_CTX_free(new_ctx);
281     return ret;
282
283  err:
284     if (used_ctx)
285         BN_CTX_end(ctx);
286     BN_CTX_free(new_ctx);
287     return 0;
288 }
289
290 /*
291  * Converts an octet string representation to an EC_POINT. Note that the
292  * simple implementation only uses affine coordinates.
293  */
294 int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
295                              const unsigned char *buf, size_t len,
296                              BN_CTX *ctx)
297 {
298     point_conversion_form_t form;
299     int y_bit;
300     BN_CTX *new_ctx = NULL;
301     BIGNUM *x, *y, *yxi;
302     size_t field_len, enc_len;
303     int ret = 0;
304
305     if (len == 0) {
306         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
307         return 0;
308     }
309     form = buf[0];
310     y_bit = form & 1;
311     form = form & ~1U;
312     if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
313         && (form != POINT_CONVERSION_UNCOMPRESSED)
314         && (form != POINT_CONVERSION_HYBRID)) {
315         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
316         return 0;
317     }
318     if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
319         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
320         return 0;
321     }
322
323     if (form == 0) {
324         if (len != 1) {
325             ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
326             return 0;
327         }
328
329         return EC_POINT_set_to_infinity(group, point);
330     }
331
332     field_len = (EC_GROUP_get_degree(group) + 7) / 8;
333     enc_len =
334         (form ==
335          POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
336
337     if (len != enc_len) {
338         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
339         return 0;
340     }
341
342     if (ctx == NULL) {
343         ctx = new_ctx = BN_CTX_new();
344         if (ctx == NULL)
345             return 0;
346     }
347
348     BN_CTX_start(ctx);
349     x = BN_CTX_get(ctx);
350     y = BN_CTX_get(ctx);
351     yxi = BN_CTX_get(ctx);
352     if (yxi == NULL)
353         goto err;
354
355     if (!BN_bin2bn(buf + 1, field_len, x))
356         goto err;
357     if (BN_ucmp(x, group->field) >= 0) {
358         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
359         goto err;
360     }
361
362     if (form == POINT_CONVERSION_COMPRESSED) {
363         if (!EC_POINT_set_compressed_coordinates_GF2m
364             (group, point, x, y_bit, ctx))
365             goto err;
366     } else {
367         if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
368             goto err;
369         if (BN_ucmp(y, group->field) >= 0) {
370             ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
371             goto err;
372         }
373         if (form == POINT_CONVERSION_HYBRID) {
374             if (!group->meth->field_div(group, yxi, y, x, ctx))
375                 goto err;
376             if (y_bit != BN_is_odd(yxi)) {
377                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
378                 goto err;
379             }
380         }
381
382         if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx))
383             goto err;
384     }
385
386     /* test required by X9.62 */
387     if (!EC_POINT_is_on_curve(group, point, ctx)) {
388         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
389         goto err;
390     }
391
392     ret = 1;
393
394  err:
395     BN_CTX_end(ctx);
396     BN_CTX_free(new_ctx);
397     return ret;
398 }
399 #endif