1 /* crypto/ec/ec_lcl.h */
2 /* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
59 #include <openssl/ec.h>
62 /* Structure details are not part of the exported interface,
63 * so all this may change in future versions. */
66 /* used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free, EC_GROUP_copy: */
67 int (*group_init)(EC_GROUP *);
68 void (*group_finish)(EC_GROUP *);
69 void (*group_clear_finish)(EC_GROUP *);
70 int (*group_copy)(EC_GROUP *, const EC_GROUP *);
72 /* used by EC_GROUP_set_curve_GFp and EC_GROUP_get_curve_GFp: */
73 int (*group_set_curve_GFp)(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
74 int (*group_get_curve_GFp)(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *);
76 /* used by EC_GROUP_set_generator, EC_GROUP_get0_generator,
77 * EC_GROUP_get_order, EC_GROUP_get_cofactor:
79 int (*group_set_generator)(EC_GROUP *, const EC_POINT *generator,
80 const BIGNUM *order, const BIGNUM *cofactor);
81 EC_POINT *(*group_get0_generator)(const EC_GROUP *);
82 int (*group_get_order)(const EC_GROUP *, BIGNUM *order, BN_CTX *);
83 int (*group_get_cofactor)(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
85 /* used by EC_GROUP_check: */
86 int (*group_check_discriminant)(const EC_GROUP *, BN_CTX *);
88 /* used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free, EC_POINT_copy: */
89 int (*point_init)(EC_POINT *);
90 void (*point_finish)(EC_POINT *);
91 void (*point_clear_finish)(EC_POINT *);
92 int (*point_copy)(EC_POINT *, const EC_POINT *);
94 /* used by EC_POINT_set_to_infinity,
95 * EC_POINT_set_Jprojective_coordinates_GFp, EC_POINT_get_Jprojective_coordinates_GFp,
96 * EC_POINT_set_affine_coordinates_GFp, EC_POINT_get_affine_coordinates_GFp,
97 * EC_POINT_set_compressed_coordinates_GFp:
99 int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *);
100 int (*point_set_Jprojective_coordinates_GFp)(const EC_GROUP *, EC_POINT *,
101 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *);
102 int (*point_get_Jprojective_coordinates_GFp)(const EC_GROUP *, const EC_POINT *,
103 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *);
104 int (*point_set_affine_coordinates_GFp)(const EC_GROUP *, EC_POINT *,
105 const BIGNUM *x, const BIGNUM *y, BN_CTX *);
106 int (*point_get_affine_coordinates_GFp)(const EC_GROUP *, const EC_POINT *,
107 BIGNUM *x, BIGNUM *y, BN_CTX *);
108 int (*point_set_compressed_coordinates_GFp)(const EC_GROUP *, EC_POINT *,
109 const BIGNUM *x, int y_bit, BN_CTX *);
111 /* used by EC_POINT_point2oct, EC_POINT_oct2point: */
112 size_t (*point2oct)(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form,
113 unsigned char *buf, size_t len, BN_CTX *);
114 int (*oct2point)(const EC_GROUP *, EC_POINT *,
115 const unsigned char *buf, size_t len, BN_CTX *);
117 /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */
118 int (*add)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
119 int (*dbl)(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
120 int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *);
122 /* used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp: */
123 int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *);
124 int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *);
125 int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
127 /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */
128 int (*make_affine)(const EC_GROUP *, EC_POINT *, BN_CTX *);
129 int (*points_make_affine)(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *);
132 /* internal functions */
134 /* 'field_mul' and 'field_sqr' can be used by 'add' and 'dbl' so that
135 * the same implementations of point operations can be used with different
136 * optimized implementations of expensive field operations: */
137 int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
138 int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
140 int (*field_encode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); /* e.g. to Montgomery */
141 int (*field_decode)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); /* e.g. from Montgomery */
142 int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *);
147 const EC_METHOD *meth;
150 void *(*extra_data_dup_func)(void *);
151 void (*extra_data_free_func)(void *);
152 void (*extra_data_clear_free_func)(void *);
154 /* All members except 'meth' and 'extra_data...' are handled by
155 * the method functions, even if they appear generic */
157 BIGNUM field; /* Field specification.
158 * For curves over GF(p), this is the modulus. */
160 BIGNUM a, b; /* Curve coefficients.
161 * (Here the assumption is that BIGNUMs can be used
162 * or abused for all kinds of fields, not just GF(p).)
163 * For characteristic > 3, the curve is defined
164 * by a Weierstrass equation of the form
165 * y^2 = x^3 + a*x + b.
167 int a_is_minus3; /* enable optimized point arithmetics for special case */
169 EC_POINT *generator; /* optional */
170 BIGNUM order, cofactor;
174 void *field_data1; /* method-specific (e.g., Montgomery structure) */
175 void *field_data2; /* method-specific */
179 /* Basically a 'mixin' for extra data, but available for EC_GROUPs only
180 * (with visibility limited to 'package' level for now).
181 * We use the function pointers as index for retrieval; this obviates
182 * global ex_data-style index tables.
183 * (Currently, we have one slot only, but is is possible to extend this
185 int EC_GROUP_set_extra_data(EC_GROUP *, void *extra_data, void *(*extra_data_dup_func)(void *),
186 void (*extra_data_free_func)(void *), void (*extra_data_clear_free_func)(void *));
187 void *EC_GROUP_get_extra_data(const EC_GROUP *, void *(*extra_data_dup_func)(void *),
188 void (*extra_data_free_func)(void *), void (*extra_data_clear_free_func)(void *));
189 void EC_GROUP_free_extra_data(EC_GROUP *);
190 void EC_GROUP_clear_free_extra_data(EC_GROUP *);
195 const EC_METHOD *meth;
197 /* All members except 'meth' are handled by the method functions,
198 * even if they appear generic */
202 BIGNUM Z; /* Jacobian projective coordinates:
203 * (X, Y, Z) represents (X/Z^2, Y/Z^3) if Z != 0 */
204 int Z_is_one; /* enable optimized point arithmetics for special case */
209 /* method functions in ecp_smpl.c */
210 int ec_GFp_simple_group_init(EC_GROUP *);
211 void ec_GFp_simple_group_finish(EC_GROUP *);
212 void ec_GFp_simple_group_clear_finish(EC_GROUP *);
213 int ec_GFp_simple_group_copy(EC_GROUP *, const EC_GROUP *);
214 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
215 int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *);
216 int ec_GFp_simple_group_set_generator(EC_GROUP *, const EC_POINT *generator,
217 const BIGNUM *order, const BIGNUM *cofactor);
218 EC_POINT *ec_GFp_simple_group_get0_generator(const EC_GROUP *);
219 int ec_GFp_simple_group_get_order(const EC_GROUP *, BIGNUM *order, BN_CTX *);
220 int ec_GFp_simple_group_get_cofactor(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
221 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *, BN_CTX *);
222 int ec_GFp_simple_point_init(EC_POINT *);
223 void ec_GFp_simple_point_finish(EC_POINT *);
224 void ec_GFp_simple_point_clear_finish(EC_POINT *);
225 int ec_GFp_simple_point_copy(EC_POINT *, const EC_POINT *);
226 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *);
227 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
228 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *);
229 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *, const EC_POINT *,
230 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *);
231 int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP *, EC_POINT *,
232 const BIGNUM *x, const BIGNUM *y, BN_CTX *);
233 int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP *, const EC_POINT *,
234 BIGNUM *x, BIGNUM *y, BN_CTX *);
235 int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *, EC_POINT *,
236 const BIGNUM *x, int y_bit, BN_CTX *);
237 size_t ec_GFp_simple_point2oct(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form,
238 unsigned char *buf, size_t len, BN_CTX *);
239 int ec_GFp_simple_oct2point(const EC_GROUP *, EC_POINT *,
240 const unsigned char *buf, size_t len, BN_CTX *);
241 int ec_GFp_simple_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
242 int ec_GFp_simple_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
243 int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
244 int ec_GFp_simple_is_at_infinity(const EC_GROUP *, const EC_POINT *);
245 int ec_GFp_simple_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
246 int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
247 int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
248 int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *);
249 int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
250 int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
253 /* method functions in ecp_mont.c */
254 int ec_GFp_mont_group_init(EC_GROUP *);
255 int ec_GFp_mont_group_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
256 void ec_GFp_mont_group_finish(EC_GROUP *);
257 void ec_GFp_mont_group_clear_finish(EC_GROUP *);
258 int ec_GFp_mont_group_copy(EC_GROUP *, const EC_GROUP *);
259 int ec_GFp_mont_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
260 int ec_GFp_mont_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
261 int ec_GFp_mont_field_encode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
262 int ec_GFp_mont_field_decode(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
263 int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *);
266 /* method functions in ecp_recp.c */
267 int ec_GFp_recp_group_init(EC_GROUP *);
268 int ec_GFp_recp_group_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
269 void ec_GFp_recp_group_finish(EC_GROUP *);
270 void ec_GFp_recp_group_clear_finish(EC_GROUP *);
271 int ec_GFp_recp_group_copy(EC_GROUP *, const EC_GROUP *);
272 int ec_GFp_recp_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
273 int ec_GFp_recp_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);
276 /* method functions in ecp_nist.c */
277 int ec_GFp_nist_group_init(EC_GROUP *);
278 int ec_GFp_nist_group_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
279 void ec_GFp_nist_group_finish(EC_GROUP *);
280 void ec_GFp_nist_group_clear_finish(EC_GROUP *);
281 int ec_GFp_nist_group_copy(EC_GROUP *, const EC_GROUP *);
282 int ec_GFp_nist_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
283 int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *);