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)
93 typedef struct ec_point_st EC_POINT;
96 /* EC_METHODs for curves over GF(p).
97 * EC_GFp_simple_method provides the basis for the optimized methods.
99 const EC_METHOD *EC_GFp_simple_method(void);
100 const EC_METHOD *EC_GFp_mont_method(void);
102 const EC_METHOD *EC_GFp_recp_method(void); /* TODO */
103 const EC_METHOD *EC_GFp_nist_method(void); /* TODO */
107 EC_GROUP *EC_GROUP_new(const EC_METHOD *);
108 void EC_GROUP_free(EC_GROUP *);
109 void EC_GROUP_clear_free(EC_GROUP *);
110 int EC_GROUP_copy(EC_GROUP *, const EC_GROUP *);
112 const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *);
113 int EC_METHOD_get_field_type(const EC_METHOD *);
115 int EC_GROUP_set_generator(EC_GROUP *, const EC_POINT *generator, const BIGNUM *order, const BIGNUM *cofactor);
116 EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *);
117 int EC_GROUP_get_order(const EC_GROUP *, BIGNUM *order, BN_CTX *);
118 int EC_GROUP_get_cofactor(const EC_GROUP *, BIGNUM *cofactor, BN_CTX *);
120 void EC_GROUP_set_nid(EC_GROUP *, int); /* curve name */
121 int EC_GROUP_get_nid(const EC_GROUP *);
123 void EC_GROUP_set_asn1_flag(EC_GROUP *, int flag);
124 int EC_GROUP_get_asn1_flag(const EC_GROUP *);
126 void EC_GROUP_set_point_conversion_form(EC_GROUP *, point_conversion_form_t);
127 point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *);
129 unsigned char *EC_GROUP_get0_seed(const EC_GROUP *);
130 size_t EC_GROUP_get_seed_len(const EC_GROUP *);
131 size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len);
133 /* We don't have types for field specifications and field elements in general.
134 * Otherwise we could declare
135 * int EC_GROUP_set_curve(EC_GROUP *, .....);
137 int EC_GROUP_set_curve_GFp(EC_GROUP *, const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
138 int EC_GROUP_get_curve_GFp(const EC_GROUP *, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *);
140 /* EC_GROUP_check() returns 1 if 'group' defines a valid group, 0 otherwise */
141 int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
142 /* EC_GROUP_check_discriminant() returns 1 if the discriminant of the
143 * elliptic curve is not zero, 0 otherwise */
144 int EC_GROUP_check_discriminant(const EC_GROUP *, BN_CTX *);
146 /* EC_GROUP_new_GFp() calls EC_GROUP_new() and EC_GROUP_set_GFp()
147 * after choosing an appropriate EC_METHOD */
148 EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *);
150 /* EC_GROUP_new_by_nid() and EC_GROUP_new_by_name() also set
151 * generator and order */
152 EC_GROUP *EC_GROUP_new_by_nid(int nid);
153 EC_GROUP *EC_GROUP_new_by_name(int name);
154 /* Currently valid arguments to EC_GROUP_new_by_name() */
155 #define EC_GROUP_NO_CURVE 0
156 #define EC_GROUP_NIST_PRIME_192 NID_X9_62_prime192v1
157 #define EC_GROUP_NIST_PRIME_224 NID_secp224r1
158 #define EC_GROUP_NIST_PRIME_256 NID_X9_62_prime256v1
159 #define EC_GROUP_NIST_PRIME_384 NID_secp384r1
160 #define EC_GROUP_NIST_PRIME_521 NID_secp521r1
161 #define EC_GROUP_X9_62_PRIME_192V1 NID_X9_62_prime192v1
162 #define EC_GROUP_X9_62_PRIME_192V2 NID_X9_62_prime192v2
163 #define EC_GROUP_X9_62_PRIME_192V3 NID_X9_62_prime192v3
164 #define EC_GROUP_X9_62_PRIME_239V1 NID_X9_62_prime239v1
165 #define EC_GROUP_X9_62_PRIME_239V2 NID_X9_62_prime239v2
166 #define EC_GROUP_X9_62_PRIME_239V3 NID_X9_62_prime239v3
167 #define EC_GROUP_X9_62_PRIME_256V1 NID_X9_62_prime256v1
168 #define EC_GROUP_SECG_PRIME_112R1 NID_secp112r1
169 #define EC_GROUP_SECG_PRIME_112R2 NID_secp112r2
170 #define EC_GROUP_SECG_PRIME_128R1 NID_secp128r1
171 #define EC_GROUP_SECG_PRIME_128R2 NID_secp128r2
172 #define EC_GROUP_SECG_PRIME_160K1 NID_secp160k1
173 #define EC_GROUP_SECG_PRIME_160R1 NID_secp160r1
174 #define EC_GROUP_SECG_PRIME_160R2 NID_secp160r2
175 #define EC_GROUP_SECG_PRIME_192K1 NID_secp192k1
176 #define EC_GROUP_SECG_PRIME_192R1 NID_X9_62_prime192v1
177 #define EC_GROUP_SECG_PRIME_224K1 NID_secp224k1
178 #define EC_GROUP_SECG_PRIME_224R1 NID_secp224r1
179 #define EC_GROUP_SECG_PRIME_256K1 NID_secp256k1
180 #define EC_GROUP_SECG_PRIME_256R1 NID_X9_62_prime256v1
181 #define EC_GROUP_SECG_PRIME_384R1 NID_secp384r1
182 #define EC_GROUP_SECG_PRIME_521R1 NID_secp521r1
183 #define EC_GROUP_WTLS_6 NID_wap_wsg_idm_ecid_wtls6
184 #define EC_GROUP_WTLS_7 NID_secp160r1
185 #define EC_GROUP_WTLS_8 NID_wap_wsg_idm_ecid_wtls8
186 #define EC_GROUP_WTLS_9 NID_wap_wsg_idm_ecid_wtls9
187 #define EC_GROUP_WTLS_12 NID_secp224r1
189 EC_POINT *EC_POINT_new(const EC_GROUP *);
190 void EC_POINT_free(EC_POINT *);
191 void EC_POINT_clear_free(EC_POINT *);
192 int EC_POINT_copy(EC_POINT *, const EC_POINT *);
194 const EC_METHOD *EC_POINT_method_of(const EC_POINT *);
196 int EC_POINT_set_to_infinity(const EC_GROUP *, EC_POINT *);
197 int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *, EC_POINT *,
198 const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *);
199 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *, const EC_POINT *,
200 BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *);
201 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *, EC_POINT *,
202 const BIGNUM *x, const BIGNUM *y, BN_CTX *);
203 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *, const EC_POINT *,
204 BIGNUM *x, BIGNUM *y, BN_CTX *);
205 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *, EC_POINT *,
206 const BIGNUM *x, int y_bit, BN_CTX *);
208 size_t EC_POINT_point2oct(const EC_GROUP *, const EC_POINT *, point_conversion_form_t form,
209 unsigned char *buf, size_t len, BN_CTX *);
210 int EC_POINT_oct2point(const EC_GROUP *, EC_POINT *,
211 const unsigned char *buf, size_t len, BN_CTX *);
213 /* other interfaces to point2oct/oct2point: */
214 BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
215 point_conversion_form_t form, BIGNUM *, BN_CTX *);
216 EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *,
217 EC_POINT *, BN_CTX *);
218 char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
219 point_conversion_form_t form, BN_CTX *);
220 EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
221 EC_POINT *, BN_CTX *);
223 int EC_POINT_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
224 int EC_POINT_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *);
225 int EC_POINT_invert(const EC_GROUP *, EC_POINT *, BN_CTX *);
227 int EC_POINT_is_at_infinity(const EC_GROUP *, const EC_POINT *);
228 int EC_POINT_is_on_curve(const EC_GROUP *, const EC_POINT *, BN_CTX *);
229 int EC_POINT_cmp(const EC_GROUP *, const EC_POINT *a, const EC_POINT *b, BN_CTX *);
231 int EC_POINT_make_affine(const EC_GROUP *, EC_POINT *, BN_CTX *);
232 int EC_POINTs_make_affine(const EC_GROUP *, size_t num, EC_POINT *[], BN_CTX *);
235 int EC_POINTs_mul(const EC_GROUP *, EC_POINT *r, const BIGNUM *, size_t num, const EC_POINT *[], const BIGNUM *[], BN_CTX *);
236 int EC_POINT_mul(const EC_GROUP *, EC_POINT *r, const BIGNUM *, const EC_POINT *, const BIGNUM *, BN_CTX *);
237 int EC_GROUP_precompute_mult(EC_GROUP *, BN_CTX *);
242 #define OPENSSL_EC_NAMED_CURVE 0x001
244 typedef struct ec_parameters_st ECPARAMETERS;
245 typedef struct ecpk_parameters_st ECPKPARAMETERS;
247 DECLARE_ASN1_ITEM(ECPARAMETERS)
248 DECLARE_ASN1_ITEM(ECPKPARAMETERS)
249 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPARAMETERS, ECPARAMETERS)
250 DECLARE_ASN1_ENCODE_FUNCTIONS_const(ECPKPARAMETERS, ECPKPARAMETERS)
252 EC_GROUP *EC_ASN1_pkparameters2group(const ECPKPARAMETERS *);
253 ECPKPARAMETERS *EC_ASN1_group2pkparameters(const EC_GROUP *, ECPKPARAMETERS *);
256 EC_GROUP *d2i_ECParameters(EC_GROUP **, const unsigned char **in, long len);
257 int i2d_ECParameters(const EC_GROUP *, unsigned char **out);
259 EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
260 int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
262 #define d2i_ECPKParameters_bio(bp,x) (EC_GROUP *)ASN1_d2i_bio(NULL, \
263 (char *(*)())d2i_ECPKParameters,(bp),(unsigned char **)(x))
264 #define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio(i2d_ECPKParameters,(bp), \
265 (unsigned char *)(x))
266 #define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \
267 (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x))
268 #define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \
269 (unsigned char *)(x))
271 #ifndef OPENSSL_NO_BIO
272 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off);
274 #ifndef OPENSSL_NO_FP_API
275 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
278 /* SEC1 ECPrivateKey */
279 typedef struct ec_privatekey_st {
281 ASN1_OCTET_STRING *privateKey;
282 ECPKPARAMETERS *parameters;
283 ASN1_BIT_STRING *publicKey;
286 DECLARE_ASN1_ITEM(EC_PRIVATEKEY)
287 DECLARE_ASN1_FUNCTIONS_const(EC_PRIVATEKEY)
288 DECLARE_ASN1_ENCODE_FUNCTIONS_const(EC_PRIVATEKEY, EC_PRIVATEKEY)
291 /* BEGIN ERROR CODES */
292 /* The following lines are auto generated by the script mkerr.pl. Any changes
293 * made after this point may be overwritten when the script is next run.
295 void ERR_load_EC_strings(void);
297 /* Error codes for the EC functions. */
299 /* Function codes. */
300 #define EC_F_COMPUTE_WNAF 143
301 #define EC_F_D2I_ECDSAPARAMETERS 154
302 #define EC_F_D2I_ECPARAMETERS 155
303 #define EC_F_D2I_ECPKPARAMETERS 161
304 #define EC_F_ECPKPARAMETERS_PRINT 166
305 #define EC_F_ECPKPARAMETERS_PRINT_FP 167
306 #define EC_F_EC_ASN1_GROUP2CURVE 159
307 #define EC_F_EC_ASN1_GROUP2FIELDID 156
308 #define EC_F_EC_ASN1_GROUP2PARAMETERS 160
309 #define EC_F_EC_ASN1_GROUP2PKPARAMETERS 162
310 #define EC_F_EC_ASN1_PARAMETERS2GROUP 157
311 #define EC_F_EC_ASN1_PKPARAMETERS2GROUP 163
312 #define EC_F_EC_GFP_MONT_FIELD_DECODE 133
313 #define EC_F_EC_GFP_MONT_FIELD_ENCODE 134
314 #define EC_F_EC_GFP_MONT_FIELD_MUL 131
315 #define EC_F_EC_GFP_MONT_FIELD_SQR 132
316 #define EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT 152
317 #define EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP 100
318 #define EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR 101
319 #define EC_F_EC_GFP_SIMPLE_MAKE_AFFINE 102
320 #define EC_F_EC_GFP_SIMPLE_OCT2POINT 103
321 #define EC_F_EC_GFP_SIMPLE_POINT2OCT 104
322 #define EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE 137
323 #define EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP 105
324 #define EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP 128
325 #define EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP 129
326 #define EC_F_EC_GROUP_CHECK 150
327 #define EC_F_EC_GROUP_CHECK_DISCRIMINANT 153
328 #define EC_F_EC_GROUP_COPY 106
329 #define EC_F_EC_GROUP_GET0_GENERATOR 139
330 #define EC_F_EC_GROUP_GET_COFACTOR 140
331 #define EC_F_EC_GROUP_GET_CURVE_GFP 130
332 #define EC_F_EC_GROUP_GET_EXTRA_DATA 107
333 #define EC_F_EC_GROUP_GET_ORDER 141
334 #define EC_F_EC_GROUP_GROUP2NID 147
335 #define EC_F_EC_GROUP_NEW 108
336 #define EC_F_EC_GROUP_NEW_BY_NAME 144
337 #define EC_F_EC_GROUP_NEW_BY_NID 146
338 #define EC_F_EC_GROUP_NEW_GFP_FROM_HEX 148
339 #define EC_F_EC_GROUP_PRECOMPUTE_MULT 142
340 #define EC_F_EC_GROUP_SET_CURVE_GFP 109
341 #define EC_F_EC_GROUP_SET_EXTRA_DATA 110
342 #define EC_F_EC_GROUP_SET_GENERATOR 111
343 #define EC_F_EC_POINTS_MAKE_AFFINE 136
344 #define EC_F_EC_POINTS_MUL 138
345 #define EC_F_EC_POINT_ADD 112
346 #define EC_F_EC_POINT_CMP 113
347 #define EC_F_EC_POINT_COPY 114
348 #define EC_F_EC_POINT_DBL 115
349 #define EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP 116
350 #define EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP 117
351 #define EC_F_EC_POINT_IS_AT_INFINITY 118
352 #define EC_F_EC_POINT_IS_ON_CURVE 119
353 #define EC_F_EC_POINT_MAKE_AFFINE 120
354 #define EC_F_EC_POINT_NEW 121
355 #define EC_F_EC_POINT_OCT2POINT 122
356 #define EC_F_EC_POINT_POINT2OCT 123
357 #define EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP 124
358 #define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP 125
359 #define EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP 126
360 #define EC_F_EC_POINT_SET_TO_INFINITY 127
361 #define EC_F_GFP_MONT_GROUP_SET_CURVE_GFP 135
362 #define EC_F_I2D_ECDSAPARAMETERS 158
363 #define EC_F_I2D_ECPARAMETERS 164
364 #define EC_F_I2D_ECPKPARAMETERS 165
367 #define EC_R_ASN1_ERROR 130
368 #define EC_R_ASN1_UNKNOWN_FIELD 131
369 #define EC_R_BUFFER_TOO_SMALL 100
370 #define EC_R_D2I_ECPARAMETERS_FAILURE 132
371 #define EC_R_D2I_ECPKPARAMETERS_FAILURE 133
372 #define EC_R_D2I_EC_PARAMETERS_FAILURE 123
373 #define EC_R_DISCRIMINANT_IS_ZERO 118
374 #define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 124
375 #define EC_R_GROUP2PARAMETERS_FAILURE 125
376 #define EC_R_GROUP2PKPARAMETERS_FAILURE 134
377 #define EC_R_I2D_ECPKPARAMETERS_FAILURE 135
378 #define EC_R_I2D_EC_PARAMETERS_FAILURE 126
379 #define EC_R_INCOMPATIBLE_OBJECTS 101
380 #define EC_R_INVALID_ARGUMENT 112
381 #define EC_R_INVALID_COMPRESSED_POINT 110
382 #define EC_R_INVALID_COMPRESSION_BIT 109
383 #define EC_R_INVALID_ENCODING 102
384 #define EC_R_INVALID_FIELD 103
385 #define EC_R_INVALID_FORM 104
386 #define EC_R_INVALID_GROUP_ORDER 119
387 #define EC_R_MISSING_PARAMETERS 127
388 #define EC_R_NOT_IMPLEMENTED 136
389 #define EC_R_NOT_INITIALIZED 111
390 #define EC_R_NO_SUCH_EXTRA_DATA 105
391 #define EC_R_PARAMETERS2GROUP_FAILURE 128
392 #define EC_R_PKPARAMETERS2GROUP_FAILURE 137
393 #define EC_R_POINT_AT_INFINITY 106
394 #define EC_R_POINT_IS_NOT_ON_CURVE 107
395 #define EC_R_SLOT_FULL 108
396 #define EC_R_UNDEFINED_GENERATOR 113
397 #define EC_R_UNDEFINED_ORDER 122
398 #define EC_R_UNKNOWN_GROUP 116
399 #define EC_R_UNKNOWN_NID 117
400 #define EC_R_UNKNOWN_ORDER 114
401 #define EC_R_UNKNOWN_PARAMETERS_TYPE 129