• 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   // p must be a prime > 3
105   if (BN_num_bits(p) <= 2 || !BN_is_odd(p)) {
106     OPENSSL_PUT_ERROR(EC, EC_R_INVALID_FIELD);
107     return 0;
108   }
109 
110   int ret = 0;
111   BN_CTX_start(ctx);
112   BIGNUM *tmp = BN_CTX_get(ctx);
113   if (tmp == NULL) {
114     goto err;
115   }
116 
117   // group->field
118   if (!BN_copy(&group->field, p)) {
119     goto err;
120   }
121   BN_set_negative(&group->field, 0);
122   // Store the field in minimal form, so it can be used with |BN_ULONG| arrays.
123   bn_set_minimal_width(&group->field);
124 
125   if (!ec_bignum_to_felem(group, &group->a, a) ||
126       !ec_bignum_to_felem(group, &group->b, b) ||
127       !ec_bignum_to_felem(group, &group->one, BN_value_one())) {
128     goto err;
129   }
130 
131   // group->a_is_minus3
132   if (!BN_copy(tmp, a) ||
133       !BN_add_word(tmp, 3)) {
134     goto err;
135   }
136   group->a_is_minus3 = (0 == BN_cmp(tmp, &group->field));
137 
138   ret = 1;
139 
140 err:
141   BN_CTX_end(ctx);
142   return ret;
143 }
144 
ec_GFp_simple_group_get_curve(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b)145 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
146                                   BIGNUM *b) {
147   if ((p != NULL && !BN_copy(p, &group->field)) ||
148       (a != NULL && !ec_felem_to_bignum(group, a, &group->a)) ||
149       (b != NULL && !ec_felem_to_bignum(group, b, &group->b))) {
150     return 0;
151   }
152   return 1;
153 }
154 
ec_GFp_simple_point_init(EC_RAW_POINT * point)155 void ec_GFp_simple_point_init(EC_RAW_POINT *point) {
156   OPENSSL_memset(&point->X, 0, sizeof(EC_FELEM));
157   OPENSSL_memset(&point->Y, 0, sizeof(EC_FELEM));
158   OPENSSL_memset(&point->Z, 0, sizeof(EC_FELEM));
159 }
160 
ec_GFp_simple_point_copy(EC_RAW_POINT * dest,const EC_RAW_POINT * src)161 void ec_GFp_simple_point_copy(EC_RAW_POINT *dest, const EC_RAW_POINT *src) {
162   OPENSSL_memcpy(&dest->X, &src->X, sizeof(EC_FELEM));
163   OPENSSL_memcpy(&dest->Y, &src->Y, sizeof(EC_FELEM));
164   OPENSSL_memcpy(&dest->Z, &src->Z, sizeof(EC_FELEM));
165 }
166 
ec_GFp_simple_point_set_to_infinity(const EC_GROUP * group,EC_RAW_POINT * point)167 void ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group,
168                                          EC_RAW_POINT *point) {
169   // Although it is strictly only necessary to zero Z, we zero the entire point
170   // in case |point| was stack-allocated and yet to be initialized.
171   ec_GFp_simple_point_init(point);
172 }
173 
ec_GFp_simple_invert(const EC_GROUP * group,EC_RAW_POINT * point)174 void ec_GFp_simple_invert(const EC_GROUP *group, EC_RAW_POINT *point) {
175   ec_felem_neg(group, &point->Y, &point->Y);
176 }
177 
ec_GFp_simple_is_at_infinity(const EC_GROUP * group,const EC_RAW_POINT * point)178 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group,
179                                  const EC_RAW_POINT *point) {
180   return ec_felem_non_zero_mask(group, &point->Z) == 0;
181 }
182 
ec_GFp_simple_is_on_curve(const EC_GROUP * group,const EC_RAW_POINT * point)183 int ec_GFp_simple_is_on_curve(const EC_GROUP *group,
184                               const EC_RAW_POINT *point) {
185   // We have a curve defined by a Weierstrass equation
186   //      y^2 = x^3 + a*x + b.
187   // The point to consider is given in Jacobian projective coordinates
188   // where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
189   // Substituting this and multiplying by  Z^6  transforms the above equation
190   // into
191   //      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
192   // To test this, we add up the right-hand side in 'rh'.
193   //
194   // This function may be used when double-checking the secret result of a point
195   // multiplication, so we proceed in constant-time.
196 
197   void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
198                           const EC_FELEM *b) = group->meth->felem_mul;
199   void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
200       group->meth->felem_sqr;
201 
202   // rh := X^2
203   EC_FELEM rh;
204   felem_sqr(group, &rh, &point->X);
205 
206   EC_FELEM tmp, Z4, Z6;
207   felem_sqr(group, &tmp, &point->Z);
208   felem_sqr(group, &Z4, &tmp);
209   felem_mul(group, &Z6, &Z4, &tmp);
210 
211   // rh := rh + a*Z^4
212   if (group->a_is_minus3) {
213     ec_felem_add(group, &tmp, &Z4, &Z4);
214     ec_felem_add(group, &tmp, &tmp, &Z4);
215     ec_felem_sub(group, &rh, &rh, &tmp);
216   } else {
217     felem_mul(group, &tmp, &Z4, &group->a);
218     ec_felem_add(group, &rh, &rh, &tmp);
219   }
220 
221   // rh := (rh + a*Z^4)*X
222   felem_mul(group, &rh, &rh, &point->X);
223 
224   // rh := rh + b*Z^6
225   felem_mul(group, &tmp, &group->b, &Z6);
226   ec_felem_add(group, &rh, &rh, &tmp);
227 
228   // 'lh' := Y^2
229   felem_sqr(group, &tmp, &point->Y);
230 
231   ec_felem_sub(group, &tmp, &tmp, &rh);
232   BN_ULONG not_equal = ec_felem_non_zero_mask(group, &tmp);
233 
234   // If Z = 0, the point is infinity, which is always on the curve.
235   BN_ULONG not_infinity = ec_felem_non_zero_mask(group, &point->Z);
236 
237   return 1 & ~(not_infinity & not_equal);
238 }
239 
ec_GFp_simple_points_equal(const EC_GROUP * group,const EC_RAW_POINT * a,const EC_RAW_POINT * b)240 int ec_GFp_simple_points_equal(const EC_GROUP *group, const EC_RAW_POINT *a,
241                                const EC_RAW_POINT *b) {
242   // This function is implemented in constant-time for two reasons. First,
243   // although EC points are usually public, their Jacobian Z coordinates may be
244   // secret, or at least are not obviously public. Second, more complex
245   // protocols will sometimes manipulate secret points.
246   //
247   // This does mean that we pay a 6M+2S Jacobian comparison when comparing two
248   // publicly affine points costs no field operations at all. If needed, we can
249   // restore this optimization by keeping better track of affine vs. Jacobian
250   // forms. See https://crbug.com/boringssl/326.
251 
252   // If neither |a| or |b| is infinity, we have to decide whether
253   //     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
254   // or equivalently, whether
255   //     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
256 
257   void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
258                           const EC_FELEM *b) = group->meth->felem_mul;
259   void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
260       group->meth->felem_sqr;
261 
262   EC_FELEM tmp1, tmp2, Za23, Zb23;
263   felem_sqr(group, &Zb23, &b->Z);         // Zb23 = Z_b^2
264   felem_mul(group, &tmp1, &a->X, &Zb23);  // tmp1 = X_a * Z_b^2
265   felem_sqr(group, &Za23, &a->Z);         // Za23 = Z_a^2
266   felem_mul(group, &tmp2, &b->X, &Za23);  // tmp2 = X_b * Z_a^2
267   ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
268   const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp1);
269 
270   felem_mul(group, &Zb23, &Zb23, &b->Z);  // Zb23 = Z_b^3
271   felem_mul(group, &tmp1, &a->Y, &Zb23);  // tmp1 = Y_a * Z_b^3
272   felem_mul(group, &Za23, &Za23, &a->Z);  // Za23 = Z_a^3
273   felem_mul(group, &tmp2, &b->Y, &Za23);  // tmp2 = Y_b * Z_a^3
274   ec_felem_sub(group, &tmp1, &tmp1, &tmp2);
275   const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp1);
276   const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
277 
278   const BN_ULONG a_not_infinity = ec_felem_non_zero_mask(group, &a->Z);
279   const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
280   const BN_ULONG a_and_b_infinity = ~(a_not_infinity | b_not_infinity);
281 
282   const BN_ULONG equal =
283       a_and_b_infinity | (a_not_infinity & b_not_infinity & x_and_y_equal);
284   return equal & 1;
285 }
286 
ec_affine_jacobian_equal(const EC_GROUP * group,const EC_AFFINE * a,const EC_RAW_POINT * b)287 int ec_affine_jacobian_equal(const EC_GROUP *group, const EC_AFFINE *a,
288                              const EC_RAW_POINT *b) {
289   // If |b| is not infinity, we have to decide whether
290   //     (X_a, Y_a) = (X_b/Z_b^2, Y_b/Z_b^3),
291   // or equivalently, whether
292   //     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b, Y_b).
293 
294   void (*const felem_mul)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a,
295                           const EC_FELEM *b) = group->meth->felem_mul;
296   void (*const felem_sqr)(const EC_GROUP *, EC_FELEM *r, const EC_FELEM *a) =
297       group->meth->felem_sqr;
298 
299   EC_FELEM tmp, Zb2;
300   felem_sqr(group, &Zb2, &b->Z);        // Zb2 = Z_b^2
301   felem_mul(group, &tmp, &a->X, &Zb2);  // tmp = X_a * Z_b^2
302   ec_felem_sub(group, &tmp, &tmp, &b->X);
303   const BN_ULONG x_not_equal = ec_felem_non_zero_mask(group, &tmp);
304 
305   felem_mul(group, &tmp, &a->Y, &Zb2);  // tmp = Y_a * Z_b^2
306   felem_mul(group, &tmp, &tmp, &b->Z);  // tmp = Y_a * Z_b^3
307   ec_felem_sub(group, &tmp, &tmp, &b->Y);
308   const BN_ULONG y_not_equal = ec_felem_non_zero_mask(group, &tmp);
309   const BN_ULONG x_and_y_equal = ~(x_not_equal | y_not_equal);
310 
311   const BN_ULONG b_not_infinity = ec_felem_non_zero_mask(group, &b->Z);
312 
313   const BN_ULONG equal = b_not_infinity & x_and_y_equal;
314   return equal & 1;
315 }
316 
ec_GFp_simple_cmp_x_coordinate(const EC_GROUP * group,const EC_RAW_POINT * p,const EC_SCALAR * r)317 int ec_GFp_simple_cmp_x_coordinate(const EC_GROUP *group, const EC_RAW_POINT *p,
318                                    const EC_SCALAR *r) {
319   if (ec_GFp_simple_is_at_infinity(group, p)) {
320     // |ec_get_x_coordinate_as_scalar| will check this internally, but this way
321     // we do not push to the error queue.
322     return 0;
323   }
324 
325   EC_SCALAR x;
326   return ec_get_x_coordinate_as_scalar(group, &x, p) &&
327          ec_scalar_equal_vartime(group, &x, r);
328 }
329 
ec_GFp_simple_felem_to_bytes(const EC_GROUP * group,uint8_t * out,size_t * out_len,const EC_FELEM * in)330 void ec_GFp_simple_felem_to_bytes(const EC_GROUP *group, uint8_t *out,
331                                   size_t *out_len, const EC_FELEM *in) {
332   size_t len = BN_num_bytes(&group->field);
333   bn_words_to_big_endian(out, len, in->words, group->field.width);
334   *out_len = len;
335 }
336 
ec_GFp_simple_felem_from_bytes(const EC_GROUP * group,EC_FELEM * out,const uint8_t * in,size_t len)337 int ec_GFp_simple_felem_from_bytes(const EC_GROUP *group, EC_FELEM *out,
338                                    const uint8_t *in, size_t len) {
339   if (len != BN_num_bytes(&group->field)) {
340     OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
341     return 0;
342   }
343 
344   bn_big_endian_to_words(out->words, group->field.width, in, len);
345 
346   if (!bn_less_than_words(out->words, group->field.d, group->field.width)) {
347     OPENSSL_PUT_ERROR(EC, EC_R_DECODE_ERROR);
348     return 0;
349   }
350 
351   return 1;
352 }
353