ec.h revision 430091c556d1086167da3320cbb60aa0079486e7
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_H
69#define OPENSSL_HEADER_EC_H
70
71#include <openssl/base.h>
72
73#if defined(__cplusplus)
74extern "C" {
75#endif
76
77
78/* Low-level operations on elliptic curves. */
79
80
81typedef struct ec_group_st EC_GROUP;
82typedef struct ec_point_st EC_POINT;
83
84/** Enum for the point conversion form as defined in X9.62 (ECDSA)
85 *  for the encoding of a elliptic curve point (x,y) */
86typedef enum {
87	/** the point is encoded as z||x, where the octet z specifies
88	 *  which solution of the quadratic equation y is  */
89	POINT_CONVERSION_COMPRESSED = 2,
90	/** the point is encoded as z||x||y, where z is the octet 0x02  */
91	POINT_CONVERSION_UNCOMPRESSED = 4
92} point_conversion_form_t;
93
94
95/* Elliptic curve groups. */
96
97/* EC_GROUP_new_by_curve_name returns a fresh EC_GROUP object for the elliptic
98 * curve specified by |nid|, or NULL on error.
99 *
100 * The supported NIDs are:
101 *   NID_secp224r1,
102 *   NID_X9_62_prime256v1,
103 *   NID_secp384r1,
104 *   NID_secp521r1 */
105OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
106
107/* EC_GROUP_free frees |group| and the data that it points to. */
108OPENSSL_EXPORT void EC_GROUP_free(EC_GROUP *group);
109
110/* EC_GROUP_dup returns a fresh |EC_GROUP| which is equal to |a| or NULL on
111 * error. */
112OPENSSL_EXPORT EC_GROUP *EC_GROUP_dup(const EC_GROUP *a);
113
114/* EC_GROUP_cmp returns zero if |a| and |b| are the same group and non-zero
115 * otherwise. */
116OPENSSL_EXPORT int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b,
117                                BN_CTX *ignored);
118
119/* EC_GROUP_get0_generator returns a pointer to the internal |EC_POINT| object
120 * in |group| that specifies the generator for the group. */
121OPENSSL_EXPORT const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group);
122
123/* EC_GROUP_get_order sets |*order| to the order of |group|, if it's not
124 * NULL. It returns one on success and zero otherwise. |ctx| is ignored. */
125OPENSSL_EXPORT int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order,
126                                      BN_CTX *ctx);
127
128/* EC_GROUP_get_cofactor sets |*cofactor| to the cofactor of |group| using
129 * |ctx|, if it's not NULL. It returns one on success and zero otherwise. */
130OPENSSL_EXPORT int EC_GROUP_get_cofactor(const EC_GROUP *group,
131                                         BIGNUM *cofactor, BN_CTX *ctx);
132
133/* EC_GROUP_get_curve_GFp gets various parameters about a group. It sets
134 * |*out_p| to the order of the coordinate field and |*out_a| and |*out_b| to
135 * the parameters of the curve when expressed as y² = x³ + ax + b. Any of the
136 * output parameters can be NULL. It returns one on success and zero on
137 * error. */
138OPENSSL_EXPORT int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *out_p,
139                                          BIGNUM *out_a, BIGNUM *out_b,
140                                          BN_CTX *ctx);
141
142/* EC_GROUP_get_curve_name returns a NID that identifies |group|. */
143OPENSSL_EXPORT int EC_GROUP_get_curve_name(const EC_GROUP *group);
144
145/* EC_GROUP_get_degree returns the number of bits needed to represent an
146 * element of the field underlying |group|. */
147OPENSSL_EXPORT int EC_GROUP_get_degree(const EC_GROUP *group);
148
149/* EC_GROUP_precompute_mult precomputes multiplies of the generator in order to
150 * speed up operations that involve calculating generator multiples. It returns
151 * one on sucess and zero otherwise. If |ctx| is not NULL, it may be used. */
152OPENSSL_EXPORT int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
153
154/* EC_GROUP_have_precompute_mult returns one if |group| contains precomputed
155 * generator multiples. */
156OPENSSL_EXPORT int EC_GROUP_have_precompute_mult(const EC_GROUP *group);
157
158
159/* Points on elliptic curves. */
160
161/* EC_POINT_new returns a fresh |EC_POINT| object in the given group, or NULL
162 * on error. */
163OPENSSL_EXPORT EC_POINT *EC_POINT_new(const EC_GROUP *group);
164
165/* EC_POINT_free frees |point| and the data that it points to. */
166OPENSSL_EXPORT void EC_POINT_free(EC_POINT *point);
167
168/* EC_POINT_clear_free clears the data that |point| points to, frees it and
169 * then frees |point| itself. */
170OPENSSL_EXPORT void EC_POINT_clear_free(EC_POINT *point);
171
172/* EC_POINT_copy sets |*dest| equal to |*src|. It returns one on success and
173 * zero otherwise. */
174OPENSSL_EXPORT int EC_POINT_copy(EC_POINT *dest, const EC_POINT *src);
175
176/* EC_POINT_dup returns a fresh |EC_POINT| that contains the same values as
177 * |src|, or NULL on error. */
178OPENSSL_EXPORT EC_POINT *EC_POINT_dup(const EC_POINT *src,
179                                      const EC_GROUP *group);
180
181/* EC_POINT_set_to_infinity sets |point| to be the "point at infinity" for the
182 * given group. */
183OPENSSL_EXPORT int EC_POINT_set_to_infinity(const EC_GROUP *group,
184                                            EC_POINT *point);
185
186/* EC_POINT_is_at_infinity returns one iff |point| is the point at infinity and
187 * zero otherwise. */
188OPENSSL_EXPORT int EC_POINT_is_at_infinity(const EC_GROUP *group,
189                                           const EC_POINT *point);
190
191/* EC_POINT_is_on_curve returns one if |point| is an element of |group| and
192 * zero otheriwse. If |ctx| is non-NULL, it may be used. */
193OPENSSL_EXPORT int EC_POINT_is_on_curve(const EC_GROUP *group,
194                                        const EC_POINT *point, BN_CTX *ctx);
195
196/* EC_POINT_cmp returns zero if |a| is equal to |b|, greater than zero is
197 * non-equal and -1 on error. If |ctx| is not NULL, it may be used. */
198OPENSSL_EXPORT int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a,
199                                const EC_POINT *b, BN_CTX *ctx);
200
201/* EC_POINT_make_affine converts |point| to affine form, internally. It returns
202 * one on success and zero otherwise. If |ctx| is not NULL, it may be used. */
203OPENSSL_EXPORT int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point,
204                                        BN_CTX *ctx);
205
206/* EC_POINTs_make_affine converts |num| points from |points| to affine form,
207 * internally. It returns one on success and zero otherwise. If |ctx| is not
208 * NULL, it may be used. */
209OPENSSL_EXPORT int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
210                                         EC_POINT *points[], BN_CTX *ctx);
211
212
213/* Point conversion. */
214
215/* EC_POINT_get_affine_coordinates_GFp sets |x| and |y| to the affine value of
216 * |point| using |ctx|, if it's not NULL. It returns one on success and zero
217 * otherwise. */
218OPENSSL_EXPORT int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
219                                                       const EC_POINT *point,
220                                                       BIGNUM *x, BIGNUM *y,
221                                                       BN_CTX *ctx);
222
223/* EC_POINT_set_affine_coordinates sets the value of |p| to be (|x|, |y|). The
224 * |ctx| argument may be used if not NULL. */
225OPENSSL_EXPORT int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group,
226                                                       EC_POINT *point,
227                                                       const BIGNUM *x,
228                                                       const BIGNUM *y,
229                                                       BN_CTX *ctx);
230
231/* EC_POINT_point2oct serialises |point| into the X9.62 form given by |form|
232 * into, at most, |len| bytes at |buf|. It returns the number of bytes written
233 * or zero on error if |buf| is non-NULL, else the number of bytes needed. The
234 * |ctx| argument may be used if not NULL. */
235OPENSSL_EXPORT size_t EC_POINT_point2oct(const EC_GROUP *group,
236                                         const EC_POINT *point,
237                                         point_conversion_form_t form,
238                                         uint8_t *buf, size_t len, BN_CTX *ctx);
239
240/* EC_POINT_oct2point sets |point| from |len| bytes of X9.62 format
241 * serialisation in |buf|. It returns one on success and zero otherwise. The
242 * |ctx| argument may be used if not NULL. */
243OPENSSL_EXPORT int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *point,
244                                      const uint8_t *buf, size_t len,
245                                      BN_CTX *ctx);
246
247/* EC_POINT_set_compressed_coordinates_GFp sets |point| to equal the point with
248 * the given |x| coordinate and the y coordinate specified by |y_bit| (see
249 * X9.62). It returns one on success and zero otherwise. */
250OPENSSL_EXPORT int EC_POINT_set_compressed_coordinates_GFp(
251    const EC_GROUP *group, EC_POINT *point, const BIGNUM *x, int y_bit,
252    BN_CTX *ctx);
253
254
255/* Group operations. */
256
257/* EC_POINT_add sets |r| equal to |a| plus |b|. It returns one on success and
258 * zero otherwise. If |ctx| is not NULL, it may be used. */
259OPENSSL_EXPORT int EC_POINT_add(const EC_GROUP *group, EC_POINT *r,
260                                const EC_POINT *a, const EC_POINT *b,
261                                BN_CTX *ctx);
262
263/* EC_POINT_dbl sets |r| equal to |a| plus |a|. It returns one on success and
264 * zero otherwise. If |ctx| is not NULL, it may be used. */
265OPENSSL_EXPORT int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r,
266                                const EC_POINT *a, BN_CTX *ctx);
267
268/* EC_POINT_dbl sets |a| equal to minus |a|. It returns one on success and zero
269 * otherwise. If |ctx| is not NULL, it may be used. */
270OPENSSL_EXPORT int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a,
271                                   BN_CTX *ctx);
272
273/* EC_POINT_mul sets r = generator*n + q*m. It returns one on success and zero
274 * otherwise. If |ctx| is not NULL, it may be used. */
275OPENSSL_EXPORT int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r,
276                                const BIGNUM *n, const EC_POINT *q,
277                                const BIGNUM *m, BN_CTX *ctx);
278
279/* EC_POINTs_mul sets r = generator*n + sum(p[i]*m[i]). It returns one on
280 * success and zero otherwise. If |ctx| is not NULL, it may be used. */
281OPENSSL_EXPORT int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r,
282                                 const BIGNUM *n, size_t num,
283                                 const EC_POINT *p[], const BIGNUM *m[],
284                                 BN_CTX *ctx);
285
286
287/* Deprecated functions. */
288
289/* EC_GROUP_new_curve_GFp creates a new, arbitrary elliptic curve group based
290 * on the equation y² = x³ + a·x + b. It returns the new group or NULL on
291 * error. */
292OPENSSL_EXPORT EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p,
293                                                const BIGNUM *a,
294                                                const BIGNUM *b, BN_CTX *ctx);
295
296/* EC_GROUP_set_generator sets the generator for |group| to |generator|, which
297 * must have the given order and cofactor. This should only be used with
298 * |EC_GROUP| objects returned by |EC_GROUP_new_curve_GFp|. */
299OPENSSL_EXPORT int EC_GROUP_set_generator(EC_GROUP *group,
300                                          const EC_POINT *generator,
301                                          const BIGNUM *order,
302                                          const BIGNUM *cofactor);
303
304/* EC_GROUP_set_asn1_flag does nothing. */
305OPENSSL_EXPORT void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
306
307#define OPENSSL_EC_NAMED_CURVE 0
308
309typedef struct ec_method_st EC_METHOD;
310
311/* EC_GROUP_method_of returns NULL. */
312OPENSSL_EXPORT const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group);
313
314/* EC_METHOD_get_field_type returns NID_X9_62_prime_field. */
315OPENSSL_EXPORT int EC_METHOD_get_field_type(const EC_METHOD *meth);
316
317/* EC_GROUP_set_point_conversion_form aborts the process if |form| is not
318 * |POINT_CONVERSION_UNCOMPRESSED| and otherwise does nothing. */
319OPENSSL_EXPORT void EC_GROUP_set_point_conversion_form(
320    EC_GROUP *group, point_conversion_form_t form);
321
322
323/* Old code expects to get EC_KEY from ec.h. */
324#if !defined(OPENSSL_HEADER_EC_KEY_H)
325#include <openssl/ec_key.h>
326#endif
327
328
329#if defined(__cplusplus)
330}  /* extern C */
331#endif
332
333#define EC_F_EC_GROUP_copy 100
334#define EC_F_EC_GROUP_get_curve_GFp 101
335#define EC_F_EC_GROUP_get_degree 102
336#define EC_F_EC_GROUP_new_by_curve_name 103
337#define EC_F_EC_KEY_check_key 104
338#define EC_F_EC_KEY_copy 105
339#define EC_F_EC_KEY_generate_key 106
340#define EC_F_EC_KEY_new_method 107
341#define EC_F_EC_KEY_set_public_key_affine_coordinates 108
342#define EC_F_EC_POINT_add 109
343#define EC_F_EC_POINT_cmp 110
344#define EC_F_EC_POINT_copy 111
345#define EC_F_EC_POINT_dbl 112
346#define EC_F_EC_POINT_dup 113
347#define EC_F_EC_POINT_get_affine_coordinates_GFp 114
348#define EC_F_EC_POINT_invert 115
349#define EC_F_EC_POINT_is_at_infinity 116
350#define EC_F_EC_POINT_is_on_curve 117
351#define EC_F_EC_POINT_make_affine 118
352#define EC_F_EC_POINT_new 119
353#define EC_F_EC_POINT_oct2point 120
354#define EC_F_EC_POINT_point2oct 121
355#define EC_F_EC_POINT_set_affine_coordinates_GFp 122
356#define EC_F_EC_POINT_set_compressed_coordinates_GFp 123
357#define EC_F_EC_POINT_set_to_infinity 124
358#define EC_F_EC_POINTs_make_affine 125
359#define EC_F_compute_wNAF 126
360#define EC_F_d2i_ECPKParameters 127
361#define EC_F_d2i_ECParameters 128
362#define EC_F_d2i_ECPrivateKey 129
363#define EC_F_ec_GFp_mont_field_decode 130
364#define EC_F_ec_GFp_mont_field_encode 131
365#define EC_F_ec_GFp_mont_field_mul 132
366#define EC_F_ec_GFp_mont_field_set_to_one 133
367#define EC_F_ec_GFp_mont_field_sqr 134
368#define EC_F_ec_GFp_mont_group_set_curve 135
369#define EC_F_ec_GFp_simple_group_check_discriminant 136
370#define EC_F_ec_GFp_simple_group_set_curve 137
371#define EC_F_ec_GFp_simple_make_affine 138
372#define EC_F_ec_GFp_simple_oct2point 139
373#define EC_F_ec_GFp_simple_point2oct 140
374#define EC_F_ec_GFp_simple_point_get_affine_coordinates 141
375#define EC_F_ec_GFp_simple_point_set_affine_coordinates 142
376#define EC_F_ec_GFp_simple_points_make_affine 143
377#define EC_F_ec_GFp_simple_set_compressed_coordinates 144
378#define EC_F_ec_asn1_group2pkparameters 145
379#define EC_F_ec_asn1_pkparameters2group 146
380#define EC_F_ec_group_new 147
381#define EC_F_ec_group_new_curve_GFp 148
382#define EC_F_ec_group_new_from_data 149
383#define EC_F_ec_point_set_Jprojective_coordinates_GFp 150
384#define EC_F_ec_pre_comp_new 151
385#define EC_F_ec_wNAF_mul 152
386#define EC_F_ec_wNAF_precompute_mult 153
387#define EC_F_i2d_ECPKParameters 154
388#define EC_F_i2d_ECParameters 155
389#define EC_F_i2d_ECPrivateKey 156
390#define EC_F_i2o_ECPublicKey 157
391#define EC_F_o2i_ECPublicKey 158
392#define EC_F_BN_to_felem 159
393#define EC_F_ec_GFp_nistp256_group_set_curve 160
394#define EC_F_ec_GFp_nistp256_point_get_affine_coordinates 161
395#define EC_F_ec_GFp_nistp256_points_mul 162
396#define EC_F_ec_group_copy 163
397#define EC_F_nistp256_pre_comp_new 164
398#define EC_F_EC_KEY_new_by_curve_name 165
399#define EC_F_EC_GROUP_new_curve_GFp 166
400#define EC_R_BUFFER_TOO_SMALL 100
401#define EC_R_COORDINATES_OUT_OF_RANGE 101
402#define EC_R_D2I_ECPKPARAMETERS_FAILURE 102
403#define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 103
404#define EC_R_GROUP2PKPARAMETERS_FAILURE 104
405#define EC_R_I2D_ECPKPARAMETERS_FAILURE 105
406#define EC_R_INCOMPATIBLE_OBJECTS 106
407#define EC_R_INVALID_COMPRESSED_POINT 107
408#define EC_R_INVALID_COMPRESSION_BIT 108
409#define EC_R_INVALID_ENCODING 109
410#define EC_R_INVALID_FIELD 110
411#define EC_R_INVALID_FORM 111
412#define EC_R_INVALID_GROUP_ORDER 112
413#define EC_R_INVALID_PRIVATE_KEY 113
414#define EC_R_MISSING_PARAMETERS 114
415#define EC_R_MISSING_PRIVATE_KEY 115
416#define EC_R_NON_NAMED_CURVE 116
417#define EC_R_NOT_INITIALIZED 117
418#define EC_R_PKPARAMETERS2GROUP_FAILURE 118
419#define EC_R_POINT_AT_INFINITY 119
420#define EC_R_POINT_IS_NOT_ON_CURVE 120
421#define EC_R_SLOT_FULL 121
422#define EC_R_UNDEFINED_GENERATOR 122
423#define EC_R_UNKNOWN_GROUP 123
424#define EC_R_UNKNOWN_ORDER 124
425#define EC_R_WRONG_ORDER 125
426#define EC_R_BIGNUM_OUT_OF_RANGE 126
427#define EC_R_WRONG_CURVE_PARAMETERS 127
428
429#endif  /* OPENSSL_HEADER_EC_H */
430