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 #ifndef OPENSSL_HEADER_EC_INTERNAL_H
69 #define OPENSSL_HEADER_EC_INTERNAL_H
70 
71 #include <openssl/base.h>
72 
73 #include <openssl/bn.h>
74 #include <openssl/ex_data.h>
75 #include <openssl/thread.h>
76 
77 #if defined(__cplusplus)
78 extern "C" {
79 #endif
80 
81 
82 struct ec_method_st {
83   /* used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free, EC_GROUP_copy: */
84   int (*group_init)(EC_GROUP *);
85   void (*group_finish)(EC_GROUP *);
86   void (*group_clear_finish)(EC_GROUP *);
87   int (*group_copy)(EC_GROUP *, const EC_GROUP *);
88 
89   /* used by EC_GROUP_set_curve_GFp, EC_GROUP_get_curve_GFp, */
90   /* EC_GROUP_set_curve_GF2m, and EC_GROUP_get_curve_GF2m: */
91   int (*group_set_curve)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
92                          const BIGNUM *b, BN_CTX *);
93 
94   /* used by EC_POINT_get_affine_coordinates_GFp: */
95   int (*point_get_affine_coordinates)(const EC_GROUP *, const EC_POINT *,
96                                       BIGNUM *x, BIGNUM *y, BN_CTX *);
97 
98   /* Computes |r = g_scalar*generator + p_scalar*p| if |g_scalar| and |p_scalar|
99    * are both non-null. Computes |r = g_scalar*generator| if |p_scalar| is null.
100    * Computes |r = p_scalar*p| if g_scalar is null. At least one of |g_scalar|
101    * and |p_scalar| must be non-null, and |p| must be non-null if |p_scalar| is
102    * non-null. */
103   int (*mul)(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
104              const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx);
105 
106   /* |check_pub_key_order| checks that the public key is in the proper subgroup
107    * by checking that |pub_key*group->order| is the point at infinity. This may
108    * be NULL for |EC_METHOD|s specialized for prime-order curves (i.e. with
109    * cofactor one), as this check is not necessary for such curves (See section
110    * A.3 of the NSA's "Suite B Implementer's Guide to FIPS 186-3
111    * (ECDSA)"). */
112   int (*check_pub_key_order)(const EC_GROUP *group, const EC_POINT *pub_key,
113                              BN_CTX *ctx);
114 
115   /* internal functions */
116 
117   /* 'field_mul' and 'field_sqr' can be used by 'add' and 'dbl' so that the
118    * same implementations of point operations can be used with different
119    * optimized implementations of expensive field operations: */
120   int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
121                    const BIGNUM *b, BN_CTX *);
122   int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
123 
124   int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
125                       BN_CTX *); /* e.g. to Montgomery */
126   int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
127                       BN_CTX *); /* e.g. from Montgomery */
128   int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *);
129 } /* EC_METHOD */;
130 
131 const EC_METHOD* EC_GFp_mont_method(void);
132 
133 struct ec_group_st {
134   const EC_METHOD *meth;
135 
136   EC_POINT *generator; /* optional */
137   BIGNUM order, cofactor;
138 
139   int curve_name; /* optional NID for named curve */
140 
141   const BN_MONT_CTX *mont_data; /* data for ECDSA inverse */
142 
143   /* The following members are handled by the method functions,
144    * even if they appear generic */
145 
146   BIGNUM field; /* For curves over GF(p), this is the modulus. */
147 
148   BIGNUM a, b; /* Curve coefficients. */
149 
150   int a_is_minus3; /* enable optimized point arithmetics for special case */
151 
152   BN_MONT_CTX *mont; /* Montgomery structure. */
153   BIGNUM *one; /* The value one */
154 } /* EC_GROUP */;
155 
156 struct ec_point_st {
157   const EC_METHOD *meth;
158 
159   /* All members except 'meth' are handled by the method functions,
160    * even if they appear generic */
161 
162   BIGNUM X;
163   BIGNUM Y;
164   BIGNUM Z; /* Jacobian projective coordinates:
165              * (X, Y, Z)  represents  (X/Z^2, Y/Z^3)  if  Z != 0 */
166   int Z_is_one; /* enable optimized point arithmetics for special case */
167 } /* EC_POINT */;
168 
169 EC_GROUP *ec_group_new(const EC_METHOD *meth);
170 int ec_group_copy(EC_GROUP *dest, const EC_GROUP *src);
171 
172 /* ec_group_get_mont_data returns a Montgomery context for operations in the
173  * scalar field of |group|. It may return NULL in the case that |group| is not
174  * a built-in group. */
175 const BN_MONT_CTX *ec_group_get_mont_data(const EC_GROUP *group);
176 
177 int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *g_scalar,
178                 const EC_POINT *p, const BIGNUM *p_scalar, BN_CTX *ctx);
179 
180 /* method functions in simple.c */
181 int ec_GFp_simple_group_init(EC_GROUP *);
182 void ec_GFp_simple_group_finish(EC_GROUP *);
183 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
184 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
185 int ec_GFp_simple_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
186                                   const BIGNUM *b, BN_CTX *);
187 int ec_GFp_simple_group_get_curve(const EC_GROUP *, BIGNUM *p, BIGNUM *a,
188                                   BIGNUM *b, BN_CTX *);
189 unsigned ec_GFp_simple_group_get_degree(const EC_GROUP *);
190 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
191 int ec_GFp_simple_point_init(EC_POINT *);
192 void ec_GFp_simple_point_finish(EC_POINT *);
193 void ec_GFp_simple_point_clear_finish(EC_POINT *);
194 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
195 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
196 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
197                                                   const BIGNUM *x,
198                                                   const BIGNUM *y,
199                                                   const BIGNUM *z, BN_CTX *);
200 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *,
201                                                   const EC_POINT *, BIGNUM *x,
202                                                   BIGNUM *y, BIGNUM *z,
203                                                   BN_CTX *);
204 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *, EC_POINT *,
205                                                const BIGNUM *x, const BIGNUM *y,
206                                                BN_CTX *);
207 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *,
208                                                const EC_POINT *, BIGNUM *x,
209                                                BIGNUM *y, BN_CTX *);
210 int ec_GFp_simple_set_compressed_coordinates(const EC_GROUP *, EC_POINT *,
211                                              const BIGNUM *x, int y_bit,
212                                              BN_CTX *);
213 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
214                       const EC_POINT *b, BN_CTX *);
215 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a,
216                       BN_CTX *);
217 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
218 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
219 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
220 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b,
221                       BN_CTX *);
222 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
223 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num,
224                                      EC_POINT * [], BN_CTX *);
225 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
226                             const BIGNUM *b, BN_CTX *);
227 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
228                             BN_CTX *);
229 
230 /* method functions in montgomery.c */
231 int ec_GFp_mont_group_init(EC_GROUP *);
232 int ec_GFp_mont_group_set_curve(EC_GROUP *, const BIGNUM *p, const BIGNUM *a,
233                                 const BIGNUM *b, BN_CTX *);
234 void ec_GFp_mont_group_finish(EC_GROUP *);
235 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
236 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
237 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
238                           const BIGNUM *b, BN_CTX *);
239 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
240                           BN_CTX *);
241 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
242                              BN_CTX *);
243 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a,
244                              BN_CTX *);
245 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
246 
247 int ec_point_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
248                                              EC_POINT *point, const BIGNUM *x,
249                                              const BIGNUM *y, const BIGNUM *z,
250                                              BN_CTX *ctx);
251 
252 void ec_GFp_nistp_points_make_affine_internal(
253     size_t num, void *point_array, size_t felem_size, void *tmp_felems,
254     void (*felem_one)(void *out), int (*felem_is_zero)(const void *in),
255     void (*felem_assign)(void *out, const void *in),
256     void (*felem_square)(void *out, const void *in),
257     void (*felem_mul)(void *out, const void *in1, const void *in2),
258     void (*felem_inv)(void *out, const void *in),
259     void (*felem_contract)(void *out, const void *in));
260 
261 void ec_GFp_nistp_recode_scalar_bits(uint8_t *sign, uint8_t *digit, uint8_t in);
262 
263 const EC_METHOD *EC_GFp_nistp224_method(void);
264 const EC_METHOD *EC_GFp_nistp256_method(void);
265 
266 /* Returns GFp methods using montgomery multiplication, with x86-64
267  * optimized P256. See http://eprint.iacr.org/2013/816. */
268 const EC_METHOD *EC_GFp_nistz256_method(void);
269 
270 struct ec_key_st {
271   int version;
272 
273   EC_GROUP *group;
274 
275   EC_POINT *pub_key;
276   BIGNUM *priv_key;
277 
278   unsigned int enc_flag;
279   point_conversion_form_t conv_form;
280 
281   CRYPTO_refcount_t references;
282   int flags;
283 
284   ECDSA_METHOD *ecdsa_meth;
285 
286   CRYPTO_EX_DATA ex_data;
287 } /* EC_KEY */;
288 
289 /* curve_data contains data about a built-in elliptic curve. */
290 struct curve_data {
291   /* comment is a human-readable string describing the curve. */
292   const char *comment;
293   /* param_len is the number of bytes needed to store a field element. */
294   uint8_t param_len;
295   /* cofactor is the cofactor of the group (i.e. the number of elements in the
296    * group divided by the size of the main subgroup. */
297   uint8_t cofactor; /* promoted to BN_ULONG */
298   /* data points to an array of 6*|param_len| bytes which hold the field
299    * elements of the following (in big-endian order): prime, a, b, generator x,
300    * generator y, order. */
301   const uint8_t data[];
302 };
303 
304 struct built_in_curve {
305   int nid;
306   const struct curve_data *data;
307   const EC_METHOD *(*method)(void);
308 };
309 
310 /* OPENSSL_built_in_curves is terminated with an entry where |nid| is
311  * |NID_undef|. */
312 extern const struct built_in_curve OPENSSL_built_in_curves[];
313 
314 #if defined(__cplusplus)
315 }  /* extern C */
316 #endif
317 
318 #endif  /* OPENSSL_HEADER_EC_INTERNAL_H */
319