]>
Commit | Line | Data |
---|---|---|
65e81670 BM |
1 | /* crypto/ec/ec_lcl.h */ |
2 | /* ==================================================================== | |
3 | * Copyright (c) 1998-2001 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 | */ | |
38e3c581 | 55 | |
3a12ce01 BM |
56 | |
57 | #include <stdlib.h> | |
58 | ||
38e3c581 | 59 | #include <openssl/ec.h> |
3a12ce01 BM |
60 | |
61 | ||
62 | /* Structure details are not part of the exported interface, | |
63 | * so all this may change in future versions. */ | |
64 | ||
65 | struct ec_method_st { | |
bb62a8b0 | 66 | /* used by EC_GROUP_new, EC_GROUP_free, EC_GROUP_clear_free, EC_GROUP_copy: */ |
3a12ce01 | 67 | int (*group_init)(EC_GROUP *); |
3a12ce01 | 68 | void (*group_finish)(EC_GROUP *); |
0657bf9c | 69 | void (*group_clear_finish)(EC_GROUP *); |
7d7db13e | 70 | int (*group_copy)(EC_GROUP *, const EC_GROUP *); |
3a12ce01 | 71 | |
bb62a8b0 BM |
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 *); | |
48fe4d62 | 74 | int (*group_get_curve_GFp)(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *); |
bb62a8b0 | 75 | |
48fe4d62 | 76 | /* used by EC_GROUP_set_generator, EC_GROUP_get0_generator, |
bb62a8b0 BM |
77 | * EC_GROUP_get_order, EC_GROUP_get_cofactor: |
78 | */ | |
7d7db13e BM |
79 | int (*group_set_generator)(EC_GROUP *, const EC_POINT *generator, |
80 | const BIGNUM *order, const BIGNUM *cofactor); | |
48fe4d62 BM |
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 *); | |
7d7db13e | 84 | |
af28dd6c BM |
85 | /* used by EC_GROUP_check: */ |
86 | int (*group_check)(const EC_GROUP *, BN_CTX *); | |
87 | ||
1d5bd6cf | 88 | /* used by EC_POINT_new, EC_POINT_free, EC_POINT_clear_free, EC_POINT_copy: */ |
3a12ce01 BM |
89 | int (*point_init)(EC_POINT *); |
90 | void (*point_finish)(EC_POINT *); | |
0657bf9c | 91 | void (*point_clear_finish)(EC_POINT *); |
7d7db13e | 92 | int (*point_copy)(EC_POINT *, const EC_POINT *); |
3a12ce01 | 93 | |
1d5bd6cf BM |
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: | |
98 | */ | |
226cc7de | 99 | int (*point_set_to_infinity)(const EC_GROUP *, EC_POINT *); |
1d5bd6cf BM |
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 *); | |
226cc7de BM |
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 *); | |
1d5bd6cf BM |
108 | int (*point_set_compressed_coordinates_GFp)(const EC_GROUP *, EC_POINT *, |
109 | const BIGNUM *x, int y_bit, BN_CTX *); | |
3a12ce01 | 110 | |
d8c79c7f | 111 | /* used by EC_POINT_point2oct, EC_POINT_oct2point: */ |
5b438e9b | 112 | size_t (*point2oct)(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form, |
7d7db13e BM |
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 *); | |
116 | ||
1d5bd6cf | 117 | /* used by EC_POINT_add, EC_POINT_dbl, ECP_POINT_invert: */ |
7d7db13e BM |
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 *); | |
1d5bd6cf | 120 | int (*invert)(const EC_GROUP *, EC_POINT *, BN_CTX *); |
3a12ce01 | 121 | |
1d5bd6cf | 122 | /* used by EC_POINT_is_at_infinity, EC_POINT_is_on_curve, EC_POINT_cmp: */ |
5b438e9b BM |
123 | int (*is_at_infinity)(const EC_GROUP *, const EC_POINT *); |
124 | int (*is_on_curve)(const EC_GROUP *, const EC_POINT *, BN_CTX *); | |
1d5bd6cf BM |
125 | int (*point_cmp)(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *); |
126 | ||
48fe4d62 | 127 | /* used by EC_POINT_make_affine, EC_POINTs_make_affine: */ |
e869d4bd | 128 | int (*make_affine)(const EC_GROUP *, EC_POINT *, BN_CTX *); |
48fe4d62 | 129 | int (*points_make_affine)(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *); |
fb171e53 | 130 | |
3a12ce01 BM |
131 | |
132 | /* internal functions */ | |
133 | ||
60428dbf | 134 | /* 'field_mul' and 'field_sqr' can be used by 'add' and 'dbl' so that |
3a12ce01 | 135 | * the same implementations of point operations can be used with different |
d8c79c7f | 136 | * optimized implementations of expensive field operations: */ |
60428dbf | 137 | int (*field_mul)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); |
7d7db13e BM |
138 | int (*field_sqr)(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); |
139 | ||
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 */ | |
48fe4d62 | 142 | int (*field_set_to_one)(const EC_GROUP *, BIGNUM *r, BN_CTX *); |
3a12ce01 BM |
143 | } /* EC_METHOD */; |
144 | ||
145 | ||
146 | struct ec_group_st { | |
0657bf9c | 147 | const EC_METHOD *meth; |
3a12ce01 | 148 | |
df9cc153 BM |
149 | void *extra_data; |
150 | void *(*extra_data_dup_func)(void *); | |
151 | void (*extra_data_free_func)(void *); | |
152 | void (*extra_data_clear_free_func)(void *); | |
153 | ||
154 | /* All members except 'meth' and 'extra_data...' are handled by | |
155 | * the method functions, even if they appear generic */ | |
0657bf9c | 156 | |
3a12ce01 BM |
157 | BIGNUM field; /* Field specification. |
158 | * For curves over GF(p), this is the modulus. */ | |
3a12ce01 BM |
159 | |
160 | BIGNUM a, b; /* Curve coefficients. | |
161 | * (Here the assumption is that BIGNUMs can be used | |
fb171e53 BM |
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 | |
e869d4bd | 165 | * y^2 = x^3 + a*x + b. |
fb171e53 | 166 | */ |
3a12ce01 BM |
167 | int a_is_minus3; /* enable optimized point arithmetics for special case */ |
168 | ||
7d7db13e BM |
169 | EC_POINT *generator; /* optional */ |
170 | BIGNUM order, cofactor; | |
60428dbf | 171 | |
945e15a2 BM |
172 | int nid; |
173 | ||
48fe4d62 BM |
174 | void *field_data1; /* method-specific (e.g., Montgomery structure) */ |
175 | void *field_data2; /* method-specific */ | |
3a12ce01 BM |
176 | } /* EC_GROUP */; |
177 | ||
178 | ||
df9cc153 BM |
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 | |
184 | * if necessary.) */ | |
df9cc153 BM |
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 *)); | |
48fe4d62 | 187 | void *EC_GROUP_get_extra_data(const EC_GROUP *, void *(*extra_data_dup_func)(void *), |
df9cc153 BM |
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 *); | |
191 | ||
192 | ||
193 | ||
3a12ce01 | 194 | struct ec_point_st { |
0657bf9c BM |
195 | const EC_METHOD *meth; |
196 | ||
197 | /* All members except 'meth' are handled by the method functions, | |
198 | * even if they appear generic */ | |
3a12ce01 | 199 | |
fb171e53 BM |
200 | BIGNUM X; |
201 | BIGNUM Y; | |
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 */ | |
3a12ce01 | 205 | } /* EC_POINT */; |
58fc6229 BM |
206 | |
207 | ||
208 | ||
209 | /* method functions in ecp_smpl.c */ | |
210 | int ec_GFp_simple_group_init(EC_GROUP *); | |
58fc6229 BM |
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 *); | |
bb62a8b0 | 214 | int ec_GFp_simple_group_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *); |
48fe4d62 | 215 | int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *); |
58fc6229 BM |
216 | int ec_GFp_simple_group_set_generator(EC_GROUP *, const EC_POINT *generator, |
217 | const BIGNUM *order, const BIGNUM *cofactor); | |
48fe4d62 BM |
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 *); | |
af28dd6c | 221 | int ec_GFp_simple_group_check(const EC_GROUP *, BN_CTX *); |
58fc6229 BM |
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 *); | |
226cc7de | 226 | int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *, EC_POINT *); |
1d5bd6cf BM |
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 *); | |
226cc7de BM |
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 *); | |
1d5bd6cf BM |
235 | int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP *, EC_POINT *, |
236 | const BIGNUM *x, int y_bit, BN_CTX *); | |
58fc6229 BM |
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 *); | |
1d5bd6cf | 243 | int ec_GFp_simple_invert(const EC_GROUP *, EC_POINT *, BN_CTX *); |
58fc6229 BM |
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 *); | |
1d5bd6cf | 246 | int ec_GFp_simple_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *); |
e869d4bd | 247 | int ec_GFp_simple_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *); |
48fe4d62 | 248 | int ec_GFp_simple_points_make_affine(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *); |
60428dbf | 249 | int ec_GFp_simple_field_mul(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *); |
58fc6229 BM |
250 | int ec_GFp_simple_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); |
251 | ||
252 | ||
253 | /* method functions in ecp_mont.c */ | |
f1f25544 | 254 | int ec_GFp_mont_group_init(EC_GROUP *); |
2e0db076 BM |
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 *); | |
60428dbf BM |
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 *); | |
58fc6229 BM |
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 *); | |
48fe4d62 | 263 | int ec_GFp_mont_field_set_to_one(const EC_GROUP *, BIGNUM *r, BN_CTX *); |
58fc6229 BM |
264 | |
265 | ||
266 | /* method functions in ecp_recp.c */ | |
f1f25544 | 267 | int ec_GFp_recp_group_init(EC_GROUP *); |
2e0db076 BM |
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 *); | |
60428dbf BM |
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 *); | |
58fc6229 | 273 | int ec_GFp_recp_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); |
58fc6229 BM |
274 | |
275 | ||
276 | /* method functions in ecp_nist.c */ | |
f1f25544 | 277 | int ec_GFp_nist_group_init(EC_GROUP *); |
2e0db076 BM |
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 *); | |
60428dbf BM |
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 *); | |
58fc6229 | 283 | int ec_GFp_nist_field_sqr(const EC_GROUP *, BIGNUM *r, const BIGNUM *a, BN_CTX *); |