• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Originally written by Bodo Moeller for the OpenSSL project.
2  * ====================================================================
3  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55 /* ====================================================================
56  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
57  *
58  * Portions of the attached software ("Contribution") are developed by
59  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
60  *
61  * The Contribution is licensed pursuant to the OpenSSL open source
62  * license provided above.
63  *
64  * The elliptic curve binary polynomial software is originally written by
65  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
66  * Laboratories. */
67 
68 #include <openssl/ec.h>
69 
70 #include <string.h>
71 
72 #include <openssl/bn.h>
73 #include <openssl/err.h>
74 #include <openssl/mem.h>
75 
76 #include "internal.h"
77 #include "../../internal.h"
78 
79 
80 // Most method functions in this file are designed to work with non-trivial
81 // representations of field elements if necessary (see ecp_mont.c): while
82 // standard modular addition and subtraction are used, the field_mul and
83 // field_sqr methods will be used for multiplication, and field_encode and
84 // field_decode (if defined) will be used for converting between
85 // representations.
86 //
87 // Functions here specifically assume that if a non-trivial representation is
88 // used, it is a Montgomery representation (i.e. 'encoding' means multiplying
89 // by some factor R).
90 
ec_GFp_simple_group_init(EC_GROUP * group)91 int ec_GFp_simple_group_init(EC_GROUP *group) {
92   BN_init(&group->field);
93   group->a_is_minus3 = 0;
94   return 1;
95 }
96 
ec_GFp_simple_group_finish(EC_GROUP * group)97 void ec_GFp_simple_group_finish(EC_GROUP *group) {
98   BN_free(&group->field);
99 }
100 
ec_GFp_simple_group_set_curve(EC_GROUP * group,const BIGNUM * p,const BIGNUM * a,const BIGNUM * b,BN_CTX * ctx)101 int ec_GFp_simple_group_set_curve(EC_GROUP *group, const BIGNUM *p,
102                                   const BIGNUM *a, const BIGNUM *b,
103                                   BN_CTX *ctx) {
104   int ret = 0;
105   BN_CTX *new_ctx = NULL;
106 
107   // p must be a prime > 3
108   if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
109     OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
110     return 0;
111   }
112 
113   if (ctx == NULL) {
114     ctx = new_ctx = BN_CTX_new();
115     if (ctx == NULL) {
116       return 0;
117     }
118   }
119 
120   BN_CTX_start(ctx);
121   BIGNUM *tmp = BN_CTX_get(ctx);
122   if (tmp == NULL) {
123     goto err;
124   }
125 
126   // group->field
127   if (!BN_copy(&group->field, p)) {
128     goto err;
129   }
130   BN_set_negative(&group->field, 0);
131   // Store the field in minimal form, so it can be used with |BN_ULONG| arrays.
132   bn_set_minimal_width(&group->field);
133 
134   // group->a
135   if (!BN_nnmod(tmp, a, &group->field, ctx) ||
136       !ec_bignum_to_felem(group, &group->a, tmp)) {
137     goto err;
138   }
139 
140   // group->a_is_minus3
141   if (!BN_add_word(tmp, 3)) {
142     goto err;
143   }
144   group->a_is_minus3 = (0 == BN_cmp(tmp, &group->field));
145 
146   // group->b
147   if (!BN_nnmod(tmp, b, &group->field, ctx) ||
148       !ec_bignum_to_felem(group, &group->b, tmp)) {
149     goto err;
150   }
151 
152   if (!ec_bignum_to_felem(group, &group->one, BN_value_one())) {
153     goto err;
154   }
155 
156   ret = 1;
157 
158 err:
159   BN_CTX_end(ctx);
160   BN_CTX_free(new_ctx);
161   return ret;
162 }
163 
ec_GFp_simple_group_get_curve(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b)164 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
165                                   BIGNUM *b) {
166   if ((p != NULL && !BN_copy(p, &group->field)) ||
167       (a != NULL && !ec_felem_to_bignum(group, a, &group->a)) ||
168       (b != NULL && !ec_felem_to_bignum(group, b, &group->b))) {
169     return 0;
170   }
171   return 1;
172 }
173 
ec_GFp_simple_point_init(EC_RAW_POINT * point)174 void ec_GFp_simple_point_init(EC_RAW_POINT *point) {
175   OPENSSL_memset(&point->X, 0, sizeof(EC_FELEM));
176   OPENSSL_memset(&point->Y, 0, sizeof(EC_FELEM));
177   OPENSSL_memset(&point->Z, 0, sizeof(EC_FELEM));
178 }
179 
ec_GFp_simple_point_copy(EC_RAW_POINT * dest,const EC_RAW_POINT * src)180 void ec_GFp_simple_point_copy(EC_RAW_POINT *dest, const EC_RAW_POINT *src) {
181   OPENSSL_memcpy(&dest->X, &src->X, sizeof(EC_FELEM));
182   OPENSSL_memcpy(&dest->Y, &src->Y, sizeof(EC_FELEM));
183   OPENSSL_memcpy(&dest->Z, &src->Z, sizeof(EC_FELEM));
184 }
185 
ec_GFp_simple_point_set_to_infinity(const EC_GROUP * group,EC_RAW_POINT * point)186 void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
187                                          EC_RAW_POINT *point) {
188   // Although it is strictly only necessary to zero Z, we zero the entire point
189   // in case |point| was stack-allocated and yet to be initialized.
190   ec_GFp_simple_point_init(point);
191 }
192 
ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP * group,EC_RAW_POINT * point,const BIGNUM * x,const BIGNUM * y)193 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group,
194                                                EC_RAW_POINT *point,
195                                                const BIGNUM *x,
196                                                const BIGNUM *y) {
197   if (x == NULL || y == NULL) {
198     OPENSSL_PUT_ERROR(EC, ERR_R_PASSED_NULL_PARAMETER);
199     return 0;
200   }
201 
202   if (!ec_bignum_to_felem(group, &point->X, x) ||
203       !ec_bignum_to_felem(group, &point->Y, y)) {
204     return 0;
205   }
206   OPENSSL_memcpy(&point->Z, &group->one, sizeof(EC_FELEM));
207 
208   return 1;
209 }
210 
ec_GFp_simple_invert(const EC_GROUP * group,EC_RAW_POINT * point)211 void ec_GFp_simple_invert(const EC_GROUP *group, EC_RAW_POINT *point) {
212   ec_felem_neg(group, &point->Y, &point->Y);
213 }
214 
ec_GFp_simple_is_at_infinity(const EC_GROUP * group,const EC_RAW_POINT * point)215 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group,
216                                  const EC_RAW_POINT *point) {
217   return ec_felem_non_zero_mask(group, &point->Z) == 0;
218 }
219 
ec_GFp_simple_is_on_curve(const EC_GROUP * group,const EC_RAW_POINT * point)220 int ec_GFp_simple_is_on_curve(const EC_GROUP *group,
221                               const EC_RAW_POINT *point) {
222   if (ec_GFp_simple_is_at_infinity(group, point)) {
223     return 1;
224   }
225 
226   // We have a curve defined by a Weierstrass equation
227   //      y^2 = x^3 + a*x + b.
228   // The point to consider is given in Jacobian projective coordinates
229   // where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
230   // Substituting this and multiplying by  Z^6  transforms the above equation
231   // into
232   //      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
233   // To test this, we add up the right-hand side in 'rh'.
234 
235   void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
236                           const EC_FELEM *b) = group->meth->felem_mul;
237   void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
238       group->meth->felem_sqr;
239 
240   // rh := X^2
241   EC_FELEM rh;
242   felem_sqr(group, &rh, &point->X);
243 
244   EC_FELEM tmp, Z4, Z6;
245   if (!ec_felem_equal(group, &point->Z, &group->one)) {
246     felem_sqr(group, &tmp, &point->Z);
247     felem_sqr(group, &Z4, &tmp);
248     felem_mul(group, &Z6, &Z4, &tmp);
249 
250     // rh := (rh + a*Z^4)*X
251     if (group->a_is_minus3) {
252       ec_felem_add(group, &tmp, &Z4, &Z4);
253       ec_felem_add(group, &tmp, &tmp, &Z4);
254       ec_felem_sub(group, &rh, &rh, &tmp);
255       felem_mul(group, &rh, &rh, &point->X);
256     } else {
257       felem_mul(group, &tmp, &Z4, &group->a);
258       ec_felem_add(group, &rh, &rh, &tmp);
259       felem_mul(group, &rh, &rh, &point->X);
260     }
261 
262     // rh := rh + b*Z^6
263     felem_mul(group, &tmp, &group->b, &Z6);
264     ec_felem_add(group, &rh, &rh, &tmp);
265   } else {
266     // rh := (rh + a)*X
267     ec_felem_add(group, &rh, &rh, &group->a);
268     felem_mul(group, &rh, &rh, &point->X);
269     // rh := rh + b
270     ec_felem_add(group, &rh, &rh, &group->b);
271   }
272 
273   // 'lh' := Y^2
274   felem_sqr(group, &tmp, &point->Y);
275   return ec_felem_equal(group, &tmp, &rh);
276 }
277 
ec_GFp_simple_cmp(const EC_GROUP * group,const EC_RAW_POINT * a,const EC_RAW_POINT * b)278 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_RAW_POINT *a,
279                       const EC_RAW_POINT *b) {
280   // Note this function returns zero if |a| and |b| are equal and 1 if they are
281   // not equal.
282   if (ec_GFp_simple_is_at_infinity(group, a)) {
283     return ec_GFp_simple_is_at_infinity(group, b) ? 0 : 1;
284   }
285 
286   if (ec_GFp_simple_is_at_infinity(group, b)) {
287     return 1;
288   }
289 
290   int a_Z_is_one = ec_felem_equal(group, &a->Z, &group->one);
291   int b_Z_is_one = ec_felem_equal(group, &b->Z, &group->one);
292 
293   if (a_Z_is_one && b_Z_is_one) {
294     return !ec_felem_equal(group, &a->X, &b->X) ||
295            !ec_felem_equal(group, &a->Y, &b->Y);
296   }
297 
298   void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
299                           const EC_FELEM *b) = group->meth->felem_mul;
300   void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
301       group->meth->felem_sqr;
302 
303   // We have to decide whether
304   //     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
305   // or equivalently, whether
306   //     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
307 
308   EC_FELEM tmp1, tmp2, Za23, Zb23;
309   const EC_FELEM *tmp1_, *tmp2_;
310   if (!b_Z_is_one) {
311     felem_sqr(group, &Zb23, &b->Z);
312     felem_mul(group, &tmp1, &a->X, &Zb23);
313     tmp1_ = &tmp1;
314   } else {
315     tmp1_ = &a->X;
316   }
317   if (!a_Z_is_one) {
318     felem_sqr(group, &Za23, &a->Z);
319     felem_mul(group, &tmp2, &b->X, &Za23);
320     tmp2_ = &tmp2;
321   } else {
322     tmp2_ = &b->X;
323   }
324 
325   // Compare  X_a*Z_b^2  with  X_b*Z_a^2.
326   if (!ec_felem_equal(group, tmp1_, tmp2_)) {
327     return 1;  // The points differ.
328   }
329 
330   if (!b_Z_is_one) {
331     felem_mul(group, &Zb23, &Zb23, &b->Z);
332     felem_mul(group, &tmp1, &a->Y, &Zb23);
333     // tmp1_ = &tmp1
334   } else {
335     tmp1_ = &a->Y;
336   }
337   if (!a_Z_is_one) {
338     felem_mul(group, &Za23, &Za23, &a->Z);
339     felem_mul(group, &tmp2, &b->Y, &Za23);
340     // tmp2_ = &tmp2
341   } else {
342     tmp2_ = &b->Y;
343   }
344 
345   // Compare  Y_a*Z_b^3  with  Y_b*Z_a^3.
346   if (!ec_felem_equal(group, tmp1_, tmp2_)) {
347     return 1;  // The points differ.
348   }
349 
350   // The points are equal.
351   return 0;
352 }
353 
ec_GFp_simple_mont_inv_mod_ord_vartime(const EC_GROUP * group,EC_SCALAR * out,const EC_SCALAR * in)354 int ec_GFp_simple_mont_inv_mod_ord_vartime(const EC_GROUP *group,
355                                            EC_SCALAR *out,
356                                            const EC_SCALAR *in) {
357   // This implementation (in fact) runs in constant time,
358   // even though for this interface it is not mandatory.
359 
360   // out = in^-1 in the Montgomery domain. This is
361   // |ec_scalar_to_montgomery| followed by |ec_scalar_inv_montgomery|, but
362   // |ec_scalar_inv_montgomery| followed by |ec_scalar_from_montgomery| is
363   // equivalent and slightly more efficient.
364   ec_scalar_inv_montgomery(group, out, in);
365   ec_scalar_from_montgomery(group, out, out);
366   return 1;
367 }
368 
ec_GFp_simple_cmp_x_coordinate(const EC_GROUP * group,const EC_RAW_POINT * p,const EC_SCALAR * r)369 int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_RAW_POINT *p,
370                                    const EC_SCALAR *r) {
371   if (ec_GFp_simple_is_at_infinity(group, p)) {
372     // |ec_get_x_coordinate_as_scalar| will check this internally, but this way
373     // we do not push to the error queue.
374     return 0;
375   }
376 
377   EC_SCALAR x;
378   return ec_get_x_coordinate_as_scalar(group, &x, p) &&
379          ec_scalar_equal_vartime(group, &x, r);
380 }
381