You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ec.h 17 KiB

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