Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
[openssl.git] / crypto / ec / ecp_oct.c
1 /* crypto/ec/ecp_oct.c */
2 /*
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
5  * OpenSSL project.
6  */
7 /* ====================================================================
8  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
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
20  *    distribution.
21  *
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/)"
26  *
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.
31  *
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.
35  *
36  * 6. Redistributions of any form whatsoever must retain the following
37  *    acknowledgment:
38  *    "This product includes software developed by the OpenSSL Project
39  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40  *
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  * ====================================================================
54  *
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).
58  *
59  */
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.
64  */
65
66 #include <openssl/err.h>
67 #include <openssl/symhacks.h>
68
69 #include "ec_lcl.h"
70
71 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *group,
72                                              EC_POINT *point,
73                                              const BIGNUM *x_, int y_bit,
74                                              BN_CTX *ctx)
75 {
76     BN_CTX *new_ctx = NULL;
77     BIGNUM *tmp1, *tmp2, *x, *y;
78     int ret = 0;
79
80     /* clear error queue */
81     ERR_clear_error();
82
83     if (ctx == NULL) {
84         ctx = new_ctx = BN_CTX_new();
85         if (ctx == NULL)
86             return 0;
87     }
88
89     y_bit = (y_bit != 0);
90
91     BN_CTX_start(ctx);
92     tmp1 = BN_CTX_get(ctx);
93     tmp2 = BN_CTX_get(ctx);
94     x = BN_CTX_get(ctx);
95     y = BN_CTX_get(ctx);
96     if (y == NULL)
97         goto err;
98
99     /*-
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.
103      */
104
105     /* tmp1 := x^3 */
106     if (!BN_nnmod(x, x_, group->field, ctx))
107         goto err;
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))
111             goto err;
112         if (!group->meth->field_mul(group, tmp1, tmp2, x_, ctx))
113             goto err;
114     } else {
115         if (!BN_mod_sqr(tmp2, x_, group->field, ctx))
116             goto err;
117         if (!BN_mod_mul(tmp1, tmp2, x_, group->field, ctx))
118             goto err;
119     }
120
121     /* tmp1 := tmp1 + a*x */
122     if (group->a_is_minus3) {
123         if (!BN_mod_lshift1_quick(tmp2, x, group->field))
124             goto err;
125         if (!BN_mod_add_quick(tmp2, tmp2, x, group->field))
126             goto err;
127         if (!BN_mod_sub_quick(tmp1, tmp1, tmp2, group->field))
128             goto err;
129     } else {
130         if (group->meth->field_decode) {
131             if (!group->meth->field_decode(group, tmp2, group->a, ctx))
132                 goto err;
133             if (!BN_mod_mul(tmp2, tmp2, x, group->field, ctx))
134                 goto err;
135         } else {
136             /* field_mul works on standard representation */
137             if (!group->meth->field_mul(group, tmp2, group->a, x, ctx))
138                 goto err;
139         }
140
141         if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
142             goto err;
143     }
144
145     /* tmp1 := tmp1 + b */
146     if (group->meth->field_decode) {
147         if (!group->meth->field_decode(group, tmp2, group->b, ctx))
148             goto err;
149         if (!BN_mod_add_quick(tmp1, tmp1, tmp2, group->field))
150             goto err;
151     } else {
152         if (!BN_mod_add_quick(tmp1, tmp1, group->b, group->field))
153             goto err;
154     }
155
156     if (!BN_mod_sqrt(y, tmp1, group->field, ctx)) {
157         unsigned long err = ERR_peek_last_error();
158
159         if (ERR_GET_LIB(err) == ERR_LIB_BN
160             && ERR_GET_REASON(err) == BN_R_NOT_A_SQUARE) {
161             ERR_clear_error();
162             ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
163                   EC_R_INVALID_COMPRESSED_POINT);
164         } else
165             ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
166                   ERR_R_BN_LIB);
167         goto err;
168     }
169
170     if (y_bit != BN_is_odd(y)) {
171         if (BN_is_zero(y)) {
172             int kron;
173
174             kron = BN_kronecker(x, group->field, ctx);
175             if (kron == -2)
176                 goto err;
177
178             if (kron == 1)
179                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
180                       EC_R_INVALID_COMPRESSION_BIT);
181             else
182                 /*
183                  * BN_mod_sqrt() should have cought this error (not a square)
184                  */
185                 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
186                       EC_R_INVALID_COMPRESSED_POINT);
187             goto err;
188         }
189         if (!BN_usub(y, group->field, y))
190             goto err;
191     }
192     if (y_bit != BN_is_odd(y)) {
193         ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES,
194               ERR_R_INTERNAL_ERROR);
195         goto err;
196     }
197
198     if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
199         goto err;
200
201     ret = 1;
202
203  err:
204     BN_CTX_end(ctx);
205     BN_CTX_free(new_ctx);
206     return ret;
207 }
208
209 size_t ec_GFp_simple_point2oct(const EC_GROUP *group, const EC_POINT *point,
210                                point_conversion_form_t form,
211                                unsigned char *buf, size_t len, BN_CTX *ctx)
212 {
213     size_t ret;
214     BN_CTX *new_ctx = NULL;
215     int used_ctx = 0;
216     BIGNUM *x, *y;
217     size_t field_len, i, skip;
218
219     if ((form != POINT_CONVERSION_COMPRESSED)
220         && (form != POINT_CONVERSION_UNCOMPRESSED)
221         && (form != POINT_CONVERSION_HYBRID)) {
222         ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_INVALID_FORM);
223         goto err;
224     }
225
226     if (EC_POINT_is_at_infinity(group, point)) {
227         /* encodes to a single 0 octet */
228         if (buf != NULL) {
229             if (len < 1) {
230                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
231                 return 0;
232             }
233             buf[0] = 0;
234         }
235         return 1;
236     }
237
238     /* ret := required output buffer length */
239     field_len = BN_num_bytes(group->field);
240     ret =
241         (form ==
242          POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
243
244     /* if 'buf' is NULL, just return required length */
245     if (buf != NULL) {
246         if (len < ret) {
247             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
248             goto err;
249         }
250
251         if (ctx == NULL) {
252             ctx = new_ctx = BN_CTX_new();
253             if (ctx == NULL)
254                 return 0;
255         }
256
257         BN_CTX_start(ctx);
258         used_ctx = 1;
259         x = BN_CTX_get(ctx);
260         y = BN_CTX_get(ctx);
261         if (y == NULL)
262             goto err;
263
264         if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
265             goto err;
266
267         if ((form == POINT_CONVERSION_COMPRESSED
268              || form == POINT_CONVERSION_HYBRID) && BN_is_odd(y))
269             buf[0] = form + 1;
270         else
271             buf[0] = form;
272
273         i = 1;
274
275         skip = field_len - BN_num_bytes(x);
276         if (skip > field_len) {
277             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
278             goto err;
279         }
280         while (skip > 0) {
281             buf[i++] = 0;
282             skip--;
283         }
284         skip = BN_bn2bin(x, buf + i);
285         i += skip;
286         if (i != 1 + field_len) {
287             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
288             goto err;
289         }
290
291         if (form == POINT_CONVERSION_UNCOMPRESSED
292             || form == POINT_CONVERSION_HYBRID) {
293             skip = field_len - BN_num_bytes(y);
294             if (skip > field_len) {
295                 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
296                 goto err;
297             }
298             while (skip > 0) {
299                 buf[i++] = 0;
300                 skip--;
301             }
302             skip = BN_bn2bin(y, buf + i);
303             i += skip;
304         }
305
306         if (i != ret) {
307             ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, ERR_R_INTERNAL_ERROR);
308             goto err;
309         }
310     }
311
312     if (used_ctx)
313         BN_CTX_end(ctx);
314     BN_CTX_free(new_ctx);
315     return ret;
316
317  err:
318     if (used_ctx)
319         BN_CTX_end(ctx);
320     BN_CTX_free(new_ctx);
321     return 0;
322 }
323
324 int ec_GFp_simple_oct2point(const EC_GROUP *group, EC_POINT *point,
325                             const unsigned char *buf, size_t len, BN_CTX *ctx)
326 {
327     point_conversion_form_t form;
328     int y_bit;
329     BN_CTX *new_ctx = NULL;
330     BIGNUM *x, *y;
331     size_t field_len, enc_len;
332     int ret = 0;
333
334     if (len == 0) {
335         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_BUFFER_TOO_SMALL);
336         return 0;
337     }
338     form = buf[0];
339     y_bit = form & 1;
340     form = form & ~1U;
341     if ((form != 0) && (form != POINT_CONVERSION_COMPRESSED)
342         && (form != POINT_CONVERSION_UNCOMPRESSED)
343         && (form != POINT_CONVERSION_HYBRID)) {
344         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
345         return 0;
346     }
347     if ((form == 0 || form == POINT_CONVERSION_UNCOMPRESSED) && y_bit) {
348         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
349         return 0;
350     }
351
352     if (form == 0) {
353         if (len != 1) {
354             ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
355             return 0;
356         }
357
358         return EC_POINT_set_to_infinity(group, point);
359     }
360
361     field_len = BN_num_bytes(group->field);
362     enc_len =
363         (form ==
364          POINT_CONVERSION_COMPRESSED) ? 1 + field_len : 1 + 2 * field_len;
365
366     if (len != enc_len) {
367         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
368         return 0;
369     }
370
371     if (ctx == NULL) {
372         ctx = new_ctx = BN_CTX_new();
373         if (ctx == NULL)
374             return 0;
375     }
376
377     BN_CTX_start(ctx);
378     x = BN_CTX_get(ctx);
379     y = BN_CTX_get(ctx);
380     if (y == NULL)
381         goto err;
382
383     if (!BN_bin2bn(buf + 1, field_len, x))
384         goto err;
385     if (BN_ucmp(x, group->field) >= 0) {
386         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
387         goto err;
388     }
389
390     if (form == POINT_CONVERSION_COMPRESSED) {
391         if (!EC_POINT_set_compressed_coordinates_GFp
392             (group, point, x, y_bit, ctx))
393             goto err;
394     } else {
395         if (!BN_bin2bn(buf + 1 + field_len, field_len, y))
396             goto err;
397         if (BN_ucmp(y, group->field) >= 0) {
398             ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
399             goto err;
400         }
401         if (form == POINT_CONVERSION_HYBRID) {
402             if (y_bit != BN_is_odd(y)) {
403                 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_INVALID_ENCODING);
404                 goto err;
405             }
406         }
407
408         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
409             goto err;
410     }
411
412     /* test required by X9.62 */
413     if (EC_POINT_is_on_curve(group, point, ctx) <= 0) {
414         ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT, EC_R_POINT_IS_NOT_ON_CURVE);
415         goto err;
416     }
417
418     ret = 1;
419
420  err:
421     BN_CTX_end(ctx);
422     BN_CTX_free(new_ctx);
423     return ret;
424 }