2 /* ====================================================================
3 * Copyright (c) 1998-2002 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).
60 #error EC is disabled.
63 #include <openssl/bn.h>
64 #include <openssl/asn1.h>
65 #include <openssl/symhacks.h>
73 /* values as defined in X9.62 (ECDSA) and elsewhere */
74 POINT_CONVERSION_COMPRESSED = 2,
75 POINT_CONVERSION_UNCOMPRESSED = 4,
76 POINT_CONVERSION_HYBRID = 6
77 } point_conversion_form_t;
80 typedef struct ec_method_st EC_METHOD;
82 typedef struct ec_group_st
87 -- optional generator with associated information (order, cofactor)
88 -- optional extra data (TODO: precomputed table for fast computation of multiples of generator)
92 typedef struct ec_point_st EC_POINT;
95 /* EC_METHODs for curves over GF(p).
96 * EC_GFp_simple_method provides the basis for the optimized methods.
98 const EC_METHOD *EC_GFp_simple_method(void);
99 const EC_METHOD *EC_GFp_mont_method(void);
101 const EC_METHOD *EC_GFp_recp_method(void); /* TODO */
102 const EC_METHOD *EC_GFp_nist_method(void); /* TODO */
106 EC_GROUP *EC_GROUP_new(const EC_METHOD *);
107 void EC_GROUP_free(EC_GROUP *);
108 void EC_GROUP_clear_free(EC_GROUP *);
109 int EC_GROUP_copy(EC_GROUP *, const EC_GROUP *);
111 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *);
112 int EC_METHOD_get_field_type(const EC_METHOD *);
114 int EC_GROUP_set_generator(EC_GROUP *, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor);
115 EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *);
116 int EC_GROUP_get_order(const EC_GROUP *, BIGNUM *order, BN_CTX *);
117 int EC_GROUP_get_cofactor(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
119 void EC_GROUP_set_nid(EC_GROUP *, int);
120 int EC_GROUP_get_nid(const EC_GROUP *);
123 /* We don't have types for field specifications and field elements in general.
124 * Otherwise we could declare
125 * int EC_GROUP_set_curve(EC_GROUP *, .....);
127 int EC_GROUP_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
128 int EC_GROUP_get_curve_GFp(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *);
130 /* EC_GROUP_check() returns 1 if 'group' defines a valid group, 0 otherwise */
131 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
132 /* EC_GROUP_check_discriminant() returns 1 if the discriminant of the
133 * elliptic curve is not zero, 0 otherwise */
134 int EC_GROUP_check_discriminant(const EC_GROUP *, BN_CTX *);
136 /* EC_GROUP_new_GFp() calls EC_GROUP_new() and EC_GROUP_set_GFp()
137 * after choosing an appropriate EC_METHOD */
138 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
140 /* EC_GROUP_new_by_nid() and EC_GROUP_new_by_name() also set
141 * generator and order */
142 EC_GROUP *EC_GROUP_new_by_nid(int nid);
143 EC_GROUP *EC_GROUP_new_by_name(int name);
144 /* Currently valid arguments to EC_GROUP_new_by_name() */
145 #define EC_GROUP_NO_CURVE 0
146 #define EC_GROUP_NIST_PRIME_192 NID_X9_62_prime192v1
147 #define EC_GROUP_NIST_PRIME_224 NID_secp224r1
148 #define EC_GROUP_NIST_PRIME_256 NID_X9_62_prime256v1
149 #define EC_GROUP_NIST_PRIME_384 NID_secp384r1
150 #define EC_GROUP_NIST_PRIME_521 NID_secp521r1
151 #define EC_GROUP_X9_62_PRIME_192V1 NID_X9_62_prime192v1
152 #define EC_GROUP_X9_62_PRIME_192V2 NID_X9_62_prime192v2
153 #define EC_GROUP_X9_62_PRIME_192V3 NID_X9_62_prime192v3
154 #define EC_GROUP_X9_62_PRIME_239V1 NID_X9_62_prime239v1
155 #define EC_GROUP_X9_62_PRIME_239V2 NID_X9_62_prime239v2
156 #define EC_GROUP_X9_62_PRIME_239V3 NID_X9_62_prime239v3
157 #define EC_GROUP_X9_62_PRIME_256V1 NID_X9_62_prime256v1
158 #define EC_GROUP_SECG_PRIME_112R1 NID_secp112r1
159 #define EC_GROUP_SECG_PRIME_112R2 NID_secp112r2
160 #define EC_GROUP_SECG_PRIME_128R1 NID_secp128r1
161 #define EC_GROUP_SECG_PRIME_128R2 NID_secp128r2
162 #define EC_GROUP_SECG_PRIME_160K1 NID_secp160k1
163 #define EC_GROUP_SECG_PRIME_160R1 NID_secp160r1
164 #define EC_GROUP_SECG_PRIME_160R2 NID_secp160r2
165 #define EC_GROUP_SECG_PRIME_192K1 NID_secp192k1
166 #define EC_GROUP_SECG_PRIME_192R1 NID_X9_62_prime192v1
167 #define EC_GROUP_SECG_PRIME_224K1 NID_secp224k1
168 #define EC_GROUP_SECG_PRIME_224R1 NID_secp224r1
169 #define EC_GROUP_SECG_PRIME_256K1 NID_secp256k1
170 #define EC_GROUP_SECG_PRIME_256R1 NID_X9_62_prime256v1
171 #define EC_GROUP_SECG_PRIME_384R1 NID_secp384r1
172 #define EC_GROUP_SECG_PRIME_521R1 NID_secp521r1
173 #define EC_GROUP_WTLS_6 NID_wap_wsg_idm_ecid_wtls6
174 #define EC_GROUP_WTLS_7 NID_secp160r1
175 #define EC_GROUP_WTLS_8 NID_wap_wsg_idm_ecid_wtls8
176 #define EC_GROUP_WTLS_9 NID_wap_wsg_idm_ecid_wtls9
177 #define EC_GROUP_WTLS_12 NID_secp224r1
179 EC_POINT *EC_POINT_new(const EC_GROUP *);
180 void EC_POINT_free(EC_POINT *);
181 void EC_POINT_clear_free(EC_POINT *);
182 int EC_POINT_copy(EC_POINT *, const EC_POINT *);
184 const EC_METHOD *EC_POINT_method_of(const EC_POINT *);
186 int EC_POINT_set_to_infinity(const EC_GROUP *, EC_POINT *);
187 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
188 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *);
189 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *, const EC_POINT *,
190 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *);
191 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *, EC_POINT *,
192 const BIGNUM *x, const BIGNUM *y, BN_CTX *);
193 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *, const EC_POINT *,
194 BIGNUM *x, BIGNUM *y, BN_CTX *);
195 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *, EC_POINT *,
196 const BIGNUM *x, int y_bit, BN_CTX *);
198 size_t EC_POINT_point2oct(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form,
199 unsigned char *buf, size_t len, BN_CTX *);
200 int EC_POINT_oct2point(const EC_GROUP *, EC_POINT *,
201 const unsigned char *buf, size_t len, BN_CTX *);
203 /* other interfaces to point2oct/oct2point: */
204 BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
205 point_conversion_form_t form, BIGNUM *, BN_CTX *);
206 EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *,
207 EC_POINT *, BN_CTX *);
208 char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
209 point_conversion_form_t form, BN_CTX *);
210 EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
211 EC_POINT *, BN_CTX *);
213 int EC_POINT_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
214 int EC_POINT_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
215 int EC_POINT_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
217 int EC_POINT_is_at_infinity(const EC_GROUP *, const EC_POINT *);
218 int EC_POINT_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
219 int EC_POINT_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
221 int EC_POINT_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
222 int EC_POINTs_make_affine(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *);
225 int EC_POINTs_mul(const EC_GROUP *, EC_POINT *r, const BIGNUM *, size_t num, const EC_POINT *[], const BIGNUM *[], BN_CTX *);
226 int EC_POINT_mul(const EC_GROUP *, EC_POINT *r, const BIGNUM *, const EC_POINT *, const BIGNUM *, BN_CTX *);
227 int EC_GROUP_precompute_mult(EC_GROUP *, BN_CTX *);
232 #define OPENSSL_EC_EXPLICIT 0x001
233 #define OPENSSL_EC_NAMED_CURVE 0x002
234 #define OPENSSL_EC_COMPRESSED 0x010
235 #define OPENSSL_EC_UNCOMPRESSED 0x020
236 #define OPENSSL_EC_HYBRID 0x040
238 typedef struct ec_parameters_st ECPARAMETERS;
239 typedef struct ecpk_parameters_st ECPKPARAMETERS;
241 DECLARE_ASN1_ITEM(ECPARAMETERS)
242 DECLARE_ASN1_ITEM(ECPKPARAMETERS)
243 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPARAMETERS, ECPARAMETERS)
244 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
246 EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *);
247 ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *, ECPKPARAMETERS *);
249 void EC_GROUP_set_asn1_flag(EC_GROUP *, int flag);
250 int EC_GROUP_get_asn1_flag(const EC_GROUP *);
252 EC_GROUP *d2i_ECParameters(EC_GROUP **, const unsigned char **in, long len);
253 int i2d_ECParameters(const EC_GROUP *, unsigned char **out);
255 EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
256 int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
260 /* BEGIN ERROR CODES */
261 /* The following lines are auto generated by the script mkerr.pl. Any changes
262 * made after this point may be overwritten when the script is next run.
264 void ERR_load_EC_strings(void);
266 /* Error codes for the EC functions. */
268 /* Function codes. */
269 #define EC_F_COMPUTE_WNAF 143
270 #define EC_F_D2I_ECDSAPARAMETERS 154
271 #define EC_F_D2I_ECPARAMETERS 155
272 #define EC_F_D2I_ECPKPARAMETERS 161
273 #define EC_F_EC_ASN1_GROUP2CURVE 159
274 #define EC_F_EC_ASN1_GROUP2FIELDID 156
275 #define EC_F_EC_ASN1_GROUP2PARAMETERS 160
276 #define EC_F_EC_ASN1_GROUP2PKPARAMETERS 162
277 #define EC_F_EC_ASN1_PARAMETERS2GROUP 157
278 #define EC_F_EC_ASN1_PKPARAMETERS2GROUP 163
279 #define EC_F_EC_GFP_MONT_FIELD_DECODE 133
280 #define EC_F_EC_GFP_MONT_FIELD_ENCODE 134
281 #define EC_F_EC_GFP_MONT_FIELD_MUL 131
282 #define EC_F_EC_GFP_MONT_FIELD_SQR 132
283 #define EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT 152
284 #define EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP 100
285 #define EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR 101
286 #define EC_F_EC_GFP_SIMPLE_MAKE_AFFINE 102
287 #define EC_F_EC_GFP_SIMPLE_OCT2POINT 103
288 #define EC_F_EC_GFP_SIMPLE_POINT2OCT 104
289 #define EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE 137
290 #define EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP 105
291 #define EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP 128
292 #define EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP 129
293 #define EC_F_EC_GROUP_CHECK 150
294 #define EC_F_EC_GROUP_CHECK_DISCRIMINANT 153
295 #define EC_F_EC_GROUP_COPY 106
296 #define EC_F_EC_GROUP_GET0_GENERATOR 139
297 #define EC_F_EC_GROUP_GET_COFACTOR 140
298 #define EC_F_EC_GROUP_GET_CURVE_GFP 130
299 #define EC_F_EC_GROUP_GET_EXTRA_DATA 107
300 #define EC_F_EC_GROUP_GET_ORDER 141
301 #define EC_F_EC_GROUP_GROUP2NID 147
302 #define EC_F_EC_GROUP_NEW 108
303 #define EC_F_EC_GROUP_NEW_BY_NAME 144
304 #define EC_F_EC_GROUP_NEW_BY_NID 146
305 #define EC_F_EC_GROUP_NEW_GFP_FROM_HEX 148
306 #define EC_F_EC_GROUP_PRECOMPUTE_MULT 142
307 #define EC_F_EC_GROUP_SET_CURVE_GFP 109
308 #define EC_F_EC_GROUP_SET_EXTRA_DATA 110
309 #define EC_F_EC_GROUP_SET_GENERATOR 111
310 #define EC_F_EC_POINTS_MAKE_AFFINE 136
311 #define EC_F_EC_POINTS_MUL 138
312 #define EC_F_EC_POINT_ADD 112
313 #define EC_F_EC_POINT_CMP 113
314 #define EC_F_EC_POINT_COPY 114
315 #define EC_F_EC_POINT_DBL 115
316 #define EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP 116
317 #define EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP 117
318 #define EC_F_EC_POINT_IS_AT_INFINITY 118
319 #define EC_F_EC_POINT_IS_ON_CURVE 119
320 #define EC_F_EC_POINT_MAKE_AFFINE 120
321 #define EC_F_EC_POINT_NEW 121
322 #define EC_F_EC_POINT_OCT2POINT 122
323 #define EC_F_EC_POINT_POINT2OCT 123
324 #define EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP 124
325 #define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP 125
326 #define EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP 126
327 #define EC_F_EC_POINT_SET_TO_INFINITY 127
328 #define EC_F_GFP_MONT_GROUP_SET_CURVE_GFP 135
329 #define EC_F_I2D_ECDSAPARAMETERS 158
330 #define EC_F_I2D_ECPARAMETERS 164
331 #define EC_F_I2D_ECPKPARAMETERS 165
334 #define EC_R_ASN1_ERROR 130
335 #define EC_R_ASN1_UNKNOWN_FIELD 131
336 #define EC_R_BUFFER_TOO_SMALL 100
337 #define EC_R_D2I_ECPARAMETERS_FAILURE 132
338 #define EC_R_D2I_ECPKPARAMETERS_FAILURE 133
339 #define EC_R_D2I_EC_PARAMETERS_FAILURE 123
340 #define EC_R_DISCRIMINANT_IS_ZERO 118
341 #define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 124
342 #define EC_R_GROUP2PARAMETERS_FAILURE 125
343 #define EC_R_GROUP2PKPARAMETERS_FAILURE 134
344 #define EC_R_I2D_ECPKPARAMETERS_FAILURE 135
345 #define EC_R_I2D_EC_PARAMETERS_FAILURE 126
346 #define EC_R_INCOMPATIBLE_OBJECTS 101
347 #define EC_R_INVALID_ARGUMENT 112
348 #define EC_R_INVALID_COMPRESSED_POINT 110
349 #define EC_R_INVALID_COMPRESSION_BIT 109
350 #define EC_R_INVALID_ENCODING 102
351 #define EC_R_INVALID_FIELD 103
352 #define EC_R_INVALID_FORM 104
353 #define EC_R_INVALID_GROUP_ORDER 119
354 #define EC_R_MISSING_PARAMETERS 127
355 #define EC_R_NOT_IMPLEMENTED 136
356 #define EC_R_NOT_INITIALIZED 111
357 #define EC_R_NO_SUCH_EXTRA_DATA 105
358 #define EC_R_PARAMETERS2GROUP_FAILURE 128
359 #define EC_R_PKPARAMETERS2GROUP_FAILURE 137
360 #define EC_R_POINT_AT_INFINITY 106
361 #define EC_R_POINT_IS_NOT_ON_CURVE 107
362 #define EC_R_SLOT_FULL 108
363 #define EC_R_UNDEFINED_GENERATOR 113
364 #define EC_R_UNDEFINED_ORDER 122
365 #define EC_R_UNKNOWN_GROUP 116
366 #define EC_R_UNKNOWN_NID 117
367 #define EC_R_UNKNOWN_ORDER 114
368 #define EC_R_UNKNOWN_PARAMETERS_TYPE 129