Reorganise ECC code for inclusion in FIPS module.
[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 #define OPENSSL_FIPSAPI
71
72 #include <openssl/err.h>
73
74 #include "ec_lcl.h"
75
76 #ifndef OPENSSL_NO_EC2M
77
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.
81  *
82  * The method is from the following publication:
83  * 
84  *     Harper, Menezes, Vanstone:
85  *     "Public-Key Cryptosystems with Very Small Key Lengths",
86  *     EUROCRYPT '92, Springer-Verlag LNCS 658,
87  *     published February 1993
88  *
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).
92  */
93 int ec_GF2m_simple_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *point,
94         const BIGNUM *x_, int y_bit, BN_CTX *ctx)
95         {
96         BN_CTX *new_ctx = NULL;
97         BIGNUM *tmp, *x, *y, *z;
98         int ret = 0, z0;
99
100         /* clear error queue */
101         ERR_clear_error();
102
103         if (ctx == NULL)
104                 {
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) goto err;
118
119         if (!BN_GF2m_mod_arr(x, x_, group->poly)) goto err;
120         if (BN_is_zero(x))
121                 {
122                 if (!BN_GF2m_mod_sqrt_arr(y, &group->b, group->poly, ctx)) goto err;
123                 }
124         else
125                 {
126                 if (!group->meth->field_sqr(group, tmp, x, ctx)) goto err;
127                 if (!group->meth->field_div(group, tmp, &group->b, tmp, ctx)) goto err;
128                 if (!BN_GF2m_add(tmp, &group->a, tmp)) goto err;
129                 if (!BN_GF2m_add(tmp, x, tmp)) goto err;
130                 if (!BN_GF2m_mod_solve_quad_arr(z, tmp, group->poly, ctx))
131                         {
132                         unsigned long err = ERR_peek_last_error();
133                         
134                         if (ERR_GET_LIB(err) == ERR_LIB_BN && ERR_GET_REASON(err) == BN_R_NO_SOLUTION)
135                                 {
136                                 ERR_clear_error();
137                                 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, EC_R_INVALID_COMPRESSED_POINT);
138                                 }
139                         else
140                                 ECerr(EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES, ERR_R_BN_LIB);
141                         goto err;
142                         }
143                 z0 = (BN_is_odd(z)) ? 1 : 0;
144                 if (!group->meth->field_mul(group, y, x, z, ctx)) goto err;
145                 if (z0 != y_bit)
146                         {
147                         if (!BN_GF2m_add(y, y, x)) goto err;
148                         }
149                 }
150
151         if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
152
153         ret = 1;
154
155  err:
156         BN_CTX_end(ctx);
157         if (new_ctx != NULL)
158                 BN_CTX_free(new_ctx);
159         return ret;
160         }
161
162
163 /* Converts an EC_POINT to an octet string.  
164  * If buf is NULL, the encoded length will be returned.
165  * If the length len of buf is smaller than required an error will be returned.
166  */
167 size_t ec_GF2m_simple_point2oct(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form,
168         unsigned char *buf, size_t len, BN_CTX *ctx)
169         {
170         size_t ret;
171         BN_CTX *new_ctx = NULL;
172         int used_ctx = 0;
173         BIGNUM *x, *y, *yxi;
174         size_t field_len, i, skip;
175
176         if ((form != POINT_CONVERSION_COMPRESSED)
177                 && (form != POINT_CONVERSION_UNCOMPRESSED)
178                 && (form != POINT_CONVERSION_HYBRID))
179                 {
180                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
181                 goto err;
182                 }
183
184         if (EC_POINT_is_at_infinity(group, point))
185                 {
186                 /* encodes to a single 0 octet */
187                 if (buf != NULL)
188                         {
189                         if (len < 1)
190                                 {
191                                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
192                                 return 0;
193                                 }
194                         buf[0] = 0;
195                         }
196                 return 1;
197                 }
198
199
200         /* ret := required output buffer length */
201         field_len = (EC_GROUP_get_degree(group) + 7) / 8;
202         ret = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
203
204         /* if 'buf' is NULL, just return required length */
205         if (buf != NULL)
206                 {
207                 if (len < ret)
208                         {
209                         ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
210                         goto err;
211                         }
212
213                 if (ctx == NULL)
214                         {
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) goto err;
226
227                 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
228
229                 buf[0] = form;
230                 if ((form != POINT_CONVERSION_UNCOMPRESSED) && !BN_is_zero(x))
231                         {
232                         if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
233                         if (BN_is_odd(yxi)) buf[0]++;
234                         }
235
236                 i = 1;
237                 
238                 skip = field_len - BN_num_bytes(x);
239                 if (skip > field_len)
240                         {
241                         ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
242                         goto err;
243                         }
244                 while (skip > 0)
245                         {
246                         buf[i++] = 0;
247                         skip--;
248                         }
249                 skip = BN_bn2bin(x, buf + i);
250                 i += skip;
251                 if (i != 1 + field_len)
252                         {
253                         ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
254                         goto err;
255                         }
256
257                 if (form == POINT_CONVERSION_UNCOMPRESSED || form == POINT_CONVERSION_HYBRID)
258                         {
259                         skip = field_len - BN_num_bytes(y);
260                         if (skip > field_len)
261                                 {
262                                 ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
263                                 goto err;
264                                 }
265                         while (skip > 0)
266                                 {
267                                 buf[i++] = 0;
268                                 skip--;
269                                 }
270                         skip = BN_bn2bin(y, buf + i);
271                         i += skip;
272                         }
273
274                 if (i != ret)
275                         {
276                         ECerr(EC_F_EC_GF2M_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
277                         goto err;
278                         }
279                 }
280         
281         if (used_ctx)
282                 BN_CTX_end(ctx);
283         if (new_ctx != NULL)
284                 BN_CTX_free(new_ctx);
285         return ret;
286
287  err:
288         if (used_ctx)
289                 BN_CTX_end(ctx);
290         if (new_ctx != NULL)
291                 BN_CTX_free(new_ctx);
292         return 0;
293         }
294
295
296 /* Converts an octet string representation to an EC_POINT. 
297  * Note that the simple implementation only uses affine coordinates.
298  */
299 int ec_GF2m_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
300         const unsigned char *buf, size_t len, BN_CTX *ctx)
301         {
302         point_conversion_form_t form;
303         int y_bit;
304         BN_CTX *new_ctx = NULL;
305         BIGNUM *x, *y, *yxi;
306         size_t field_len, enc_len;
307         int ret = 0;
308
309         if (len == 0)
310                 {
311                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
312                 return 0;
313                 }
314         form = buf[0];
315         y_bit = form & 1;
316         form = form & ~1U;
317         if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
318                 && (form != POINT_CONVERSION_UNCOMPRESSED)
319                 && (form != POINT_CONVERSION_HYBRID))
320                 {
321                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
322                 return 0;
323                 }
324         if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit)
325                 {
326                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
327                 return 0;
328                 }
329
330         if (form == 0)
331                 {
332                 if (len != 1)
333                         {
334                         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
335                         return 0;
336                         }
337
338                 return EC_POINT_set_to_infinity(group, point);
339                 }
340         
341         field_len = (EC_GROUP_get_degree(group) + 7) / 8;
342         enc_len = (form == POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2*field_len;
343
344         if (len != enc_len)
345                 {
346                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
347                 return 0;
348                 }
349
350         if (ctx == NULL)
351                 {
352                 ctx = new_ctx = BN_CTX_new();
353                 if (ctx == NULL)
354                         return 0;
355                 }
356
357         BN_CTX_start(ctx);
358         x = BN_CTX_get(ctx);
359         y = BN_CTX_get(ctx);
360         yxi = BN_CTX_get(ctx);
361         if (yxi == NULL) goto err;
362
363         if (!BN_bin2bn(buf + 1, field_len, x)) goto err;
364         if (BN_ucmp(x, &group->field) >= 0)
365                 {
366                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
367                 goto err;
368                 }
369
370         if (form == POINT_CONVERSION_COMPRESSED)
371                 {
372                 if (!EC_POINT_set_compressed_coordinates_GF2m(group, point, x, y_bit, ctx)) goto err;
373                 }
374         else
375                 {
376                 if (!BN_bin2bn(buf + 1 + field_len, field_len, y)) goto err;
377                 if (BN_ucmp(y, &group->field) >= 0)
378                         {
379                         ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
380                         goto err;
381                         }
382                 if (form == POINT_CONVERSION_HYBRID)
383                         {
384                         if (!group->meth->field_div(group, yxi, y, x, ctx)) goto err;
385                         if (y_bit != BN_is_odd(yxi))
386                                 {
387                                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
388                                 goto err;
389                                 }
390                         }
391
392                 if (!EC_POINT_set_affine_coordinates_GF2m(group, point, x, y, ctx)) goto err;
393                 }
394         
395         if (!EC_POINT_is_on_curve(group, point, ctx)) /* test required by X9.62 */
396                 {
397                 ECerr(EC_F_EC_GF2M_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
398                 goto err;
399                 }
400
401         ret = 1;
402         
403  err:
404         BN_CTX_end(ctx);
405         if (new_ctx != NULL)
406                 BN_CTX_free(new_ctx);
407         return ret;
408         }
409 #endif