ec.h 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. /* crypto/ec/ec.h */
  2. /*
  3. * Originally written by Bodo Moeller for the OpenSSL project.
  4. */
  5. /**
  6. * \file crypto/ec/ec.h Include file for the OpenSSL EC functions
  7. * \author Originally written by Bodo Moeller for the OpenSSL project
  8. */
  9. /* ====================================================================
  10. * Copyright (c) 1998-2019 The OpenSSL Project. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. *
  24. * 3. All advertising materials mentioning features or use of this
  25. * software must display the following acknowledgment:
  26. * "This product includes software developed by the OpenSSL Project
  27. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  28. *
  29. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  30. * endorse or promote products derived from this software without
  31. * prior written permission. For written permission, please contact
  32. * [email protected].
  33. *
  34. * 5. Products derived from this software may not be called "OpenSSL"
  35. * nor may "OpenSSL" appear in their names without prior written
  36. * permission of the OpenSSL Project.
  37. *
  38. * 6. Redistributions of any form whatsoever must retain the following
  39. * acknowledgment:
  40. * "This product includes software developed by the OpenSSL Project
  41. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  44. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  46. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  47. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  49. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  50. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  52. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  54. * OF THE POSSIBILITY OF SUCH DAMAGE.
  55. * ====================================================================
  56. *
  57. * This product includes cryptographic software written by Eric Young
  58. * ([email protected]). This product includes software written by Tim
  59. * Hudson ([email protected]).
  60. *
  61. */
  62. /* ====================================================================
  63. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  64. *
  65. * Portions of the attached software ("Contribution") are developed by
  66. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  67. *
  68. * The Contribution is licensed pursuant to the OpenSSL open source
  69. * license provided above.
  70. *
  71. * The elliptic curve binary polynomial software is originally written by
  72. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  73. *
  74. */
  75. #ifndef HEADER_EC_H
  76. # define HEADER_EC_H
  77. # include <openssl/opensslconf.h>
  78. # ifdef OPENSSL_NO_EC
  79. # error EC is disabled.
  80. # endif
  81. # include <openssl/asn1.h>
  82. # include <openssl/symhacks.h>
  83. # ifndef OPENSSL_NO_DEPRECATED
  84. # include <openssl/bn.h>
  85. # endif
  86. # ifdef __cplusplus
  87. extern "C" {
  88. # elif defined(__SUNPRO_C)
  89. # if __SUNPRO_C >= 0x520
  90. # pragma error_messages (off,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
  91. # endif
  92. # endif
  93. # ifndef OPENSSL_ECC_MAX_FIELD_BITS
  94. # define OPENSSL_ECC_MAX_FIELD_BITS 661
  95. # endif
  96. /** Enum for the point conversion form as defined in X9.62 (ECDSA)
  97. * for the encoding of a elliptic curve point (x,y) */
  98. typedef enum {
  99. /** the point is encoded as z||x, where the octet z specifies
  100. * which solution of the quadratic equation y is */
  101. POINT_CONVERSION_COMPRESSED = 2,
  102. /** the point is encoded as z||x||y, where z is the octet 0x04 */
  103. POINT_CONVERSION_UNCOMPRESSED = 4,
  104. /** the point is encoded as z||x||y, where the octet z specifies
  105. * which solution of the quadratic equation y is */
  106. POINT_CONVERSION_HYBRID = 6
  107. } point_conversion_form_t;
  108. typedef struct ec_method_st EC_METHOD;
  109. typedef struct ec_group_st
  110. /*-
  111. EC_METHOD *meth;
  112. -- field definition
  113. -- curve coefficients
  114. -- optional generator with associated information (order, cofactor)
  115. -- optional extra data (precomputed table for fast computation of multiples of generator)
  116. -- ASN1 stuff
  117. */
  118. EC_GROUP;
  119. typedef struct ec_point_st EC_POINT;
  120. /********************************************************************/
  121. /* EC_METHODs for curves over GF(p) */
  122. /********************************************************************/
  123. /** Returns the basic GFp ec methods which provides the basis for the
  124. * optimized methods.
  125. * \return EC_METHOD object
  126. */
  127. const EC_METHOD *EC_GFp_simple_method(void);
  128. /** Returns GFp methods using montgomery multiplication.
  129. * \return EC_METHOD object
  130. */
  131. const EC_METHOD *EC_GFp_mont_method(void);
  132. /** Returns GFp methods using optimized methods for NIST recommended curves
  133. * \return EC_METHOD object
  134. */
  135. const EC_METHOD *EC_GFp_nist_method(void);
  136. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  137. /** Returns 64-bit optimized methods for nistp224
  138. * \return EC_METHOD object
  139. */
  140. const EC_METHOD *EC_GFp_nistp224_method(void);
  141. /** Returns 64-bit optimized methods for nistp256
  142. * \return EC_METHOD object
  143. */
  144. const EC_METHOD *EC_GFp_nistp256_method(void);
  145. /** Returns 64-bit optimized methods for nistp521
  146. * \return EC_METHOD object
  147. */
  148. const EC_METHOD *EC_GFp_nistp521_method(void);
  149. # endif
  150. # ifndef OPENSSL_NO_EC2M
  151. /********************************************************************/
  152. /* EC_METHOD for curves over GF(2^m) */
  153. /********************************************************************/
  154. /** Returns the basic GF2m ec method
  155. * \return EC_METHOD object
  156. */
  157. const EC_METHOD *EC_GF2m_simple_method(void);
  158. # endif
  159. /********************************************************************/
  160. /* EC_GROUP functions */
  161. /********************************************************************/
  162. /** Creates a new EC_GROUP object
  163. * \param meth EC_METHOD to use
  164. * \return newly created EC_GROUP object or NULL in case of an error.
  165. */
  166. EC_GROUP *EC_GROUP_new(const EC_METHOD *meth);
  167. /** Frees a EC_GROUP object
  168. * \param group EC_GROUP object to be freed.
  169. */
  170. void EC_GROUP_free(EC_GROUP *group);
  171. /** Clears and frees a EC_GROUP object
  172. * \param group EC_GROUP object to be cleared and freed.
  173. */
  174. void EC_GROUP_clear_free(EC_GROUP *group);
  175. /** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD.
  176. * \param dst destination EC_GROUP object
  177. * \param src source EC_GROUP object
  178. * \return 1 on success and 0 if an error occurred.
  179. */
  180. int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src);
  181. /** Creates a new EC_GROUP object and copies the copies the content
  182. * form src to the newly created EC_KEY object
  183. * \param src source EC_GROUP object
  184. * \return newly created EC_GROUP object or NULL in case of an error.
  185. */
  186. EC_GROUP *EC_GROUP_dup(const EC_GROUP *src);
  187. /** Returns the EC_METHOD of the EC_GROUP object.
  188. * \param group EC_GROUP object
  189. * \return EC_METHOD used in this EC_GROUP object.
  190. */
  191. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group);
  192. /** Returns the field type of the EC_METHOD.
  193. * \param meth EC_METHOD object
  194. * \return NID of the underlying field type OID.
  195. */
  196. int EC_METHOD_get_field_type(const EC_METHOD *meth);
  197. /** Sets the generator and it's order/cofactor of a EC_GROUP object.
  198. * \param group EC_GROUP object
  199. * \param generator EC_POINT object with the generator.
  200. * \param order the order of the group generated by the generator.
  201. * \param cofactor the index of the sub-group generated by the generator
  202. * in the group of all points on the elliptic curve.
  203. * \return 1 on success and 0 if an error occured
  204. */
  205. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  206. const BIGNUM *order, const BIGNUM *cofactor);
  207. /** Returns the generator of a EC_GROUP object.
  208. * \param group EC_GROUP object
  209. * \return the currently used generator (possibly NULL).
  210. */
  211. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group);
  212. /** Returns the montgomery data for order(Generator)
  213. * \param group EC_GROUP object
  214. * \return the currently used generator (possibly NULL).
  215. */
  216. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group);
  217. /** Gets the order of a EC_GROUP
  218. * \param group EC_GROUP object
  219. * \param order BIGNUM to which the order is copied
  220. * \param ctx BN_CTX object (optional)
  221. * \return 1 on success and 0 if an error occured
  222. */
  223. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx);
  224. /** Gets the cofactor of a EC_GROUP
  225. * \param group EC_GROUP object
  226. * \param cofactor BIGNUM to which the cofactor is copied
  227. * \param ctx BN_CTX object (optional)
  228. * \return 1 on success and 0 if an error occured
  229. */
  230. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  231. BN_CTX *ctx);
  232. /** Sets the name of a EC_GROUP object
  233. * \param group EC_GROUP object
  234. * \param nid NID of the curve name OID
  235. */
  236. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid);
  237. /** Returns the curve name of a EC_GROUP object
  238. * \param group EC_GROUP object
  239. * \return NID of the curve name OID or 0 if not set.
  240. */
  241. int EC_GROUP_get_curve_name(const EC_GROUP *group);
  242. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
  243. int EC_GROUP_get_asn1_flag(const EC_GROUP *group);
  244. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  245. point_conversion_form_t form);
  246. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *);
  247. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x);
  248. size_t EC_GROUP_get_seed_len(const EC_GROUP *);
  249. size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len);
  250. /** Sets the parameter of a ec over GFp defined by y^2 = x^3 + a*x + b
  251. * \param group EC_GROUP object
  252. * \param p BIGNUM with the prime number
  253. * \param a BIGNUM with parameter a of the equation
  254. * \param b BIGNUM with parameter b of the equation
  255. * \param ctx BN_CTX object (optional)
  256. * \return 1 on success and 0 if an error occured
  257. */
  258. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  259. const BIGNUM *b, BN_CTX *ctx);
  260. /** Gets the parameter of the ec over GFp defined by y^2 = x^3 + a*x + b
  261. * \param group EC_GROUP object
  262. * \param p BIGNUM for the prime number
  263. * \param a BIGNUM for parameter a of the equation
  264. * \param b BIGNUM for parameter b of the equation
  265. * \param ctx BN_CTX object (optional)
  266. * \return 1 on success and 0 if an error occured
  267. */
  268. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  269. BIGNUM *b, BN_CTX *ctx);
  270. # ifndef OPENSSL_NO_EC2M
  271. /** Sets the parameter of a ec over GF2m defined by y^2 + x*y = x^3 + a*x^2 + b
  272. * \param group EC_GROUP object
  273. * \param p BIGNUM with the polynomial defining the underlying field
  274. * \param a BIGNUM with parameter a of the equation
  275. * \param b BIGNUM with parameter b of the equation
  276. * \param ctx BN_CTX object (optional)
  277. * \return 1 on success and 0 if an error occured
  278. */
  279. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  280. const BIGNUM *b, BN_CTX *ctx);
  281. /** Gets the parameter of the ec over GF2m defined by y^2 + x*y = x^3 + a*x^2 + b
  282. * \param group EC_GROUP object
  283. * \param p BIGNUM for the polynomial defining the underlying field
  284. * \param a BIGNUM for parameter a of the equation
  285. * \param b BIGNUM for parameter b of the equation
  286. * \param ctx BN_CTX object (optional)
  287. * \return 1 on success and 0 if an error occured
  288. */
  289. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  290. BIGNUM *b, BN_CTX *ctx);
  291. # endif
  292. /** Returns the number of bits needed to represent a field element
  293. * \param group EC_GROUP object
  294. * \return number of bits needed to represent a field element
  295. */
  296. int EC_GROUP_get_degree(const EC_GROUP *group);
  297. /** Checks whether the parameter in the EC_GROUP define a valid ec group
  298. * \param group EC_GROUP object
  299. * \param ctx BN_CTX object (optional)
  300. * \return 1 if group is a valid ec group and 0 otherwise
  301. */
  302. int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
  303. /** Checks whether the discriminant of the elliptic curve is zero or not
  304. * \param group EC_GROUP object
  305. * \param ctx BN_CTX object (optional)
  306. * \return 1 if the discriminant is not zero and 0 otherwise
  307. */
  308. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx);
  309. /** Compares two EC_GROUP objects
  310. * \param a first EC_GROUP object
  311. * \param b second EC_GROUP object
  312. * \param ctx BN_CTX object (optional)
  313. * \return 0 if both groups are equal and 1 otherwise
  314. */
  315. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx);
  316. /*
  317. * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after
  318. * choosing an appropriate EC_METHOD
  319. */
  320. /** Creates a new EC_GROUP object with the specified parameters defined
  321. * over GFp (defined by the equation y^2 = x^3 + a*x + b)
  322. * \param p BIGNUM with the prime number
  323. * \param a BIGNUM with the parameter a of the equation
  324. * \param b BIGNUM with the parameter b of the equation
  325. * \param ctx BN_CTX object (optional)
  326. * \return newly created EC_GROUP object with the specified parameters
  327. */
  328. EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
  329. const BIGNUM *b, BN_CTX *ctx);
  330. # ifndef OPENSSL_NO_EC2M
  331. /** Creates a new EC_GROUP object with the specified parameters defined
  332. * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b)
  333. * \param p BIGNUM with the polynomial defining the underlying field
  334. * \param a BIGNUM with the parameter a of the equation
  335. * \param b BIGNUM with the parameter b of the equation
  336. * \param ctx BN_CTX object (optional)
  337. * \return newly created EC_GROUP object with the specified parameters
  338. */
  339. EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
  340. const BIGNUM *b, BN_CTX *ctx);
  341. # endif
  342. /** Creates a EC_GROUP object with a curve specified by a NID
  343. * \param nid NID of the OID of the curve name
  344. * \return newly created EC_GROUP object with specified curve or NULL
  345. * if an error occurred
  346. */
  347. EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
  348. /********************************************************************/
  349. /* handling of internal curves */
  350. /********************************************************************/
  351. typedef struct {
  352. int nid;
  353. const char *comment;
  354. } EC_builtin_curve;
  355. /*
  356. * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all
  357. * available curves or zero if a error occurred. In case r ist not zero
  358. * nitems EC_builtin_curve structures are filled with the data of the first
  359. * nitems internal groups
  360. */
  361. size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems);
  362. const char *EC_curve_nid2nist(int nid);
  363. int EC_curve_nist2nid(const char *name);
  364. /********************************************************************/
  365. /* EC_POINT functions */
  366. /********************************************************************/
  367. /** Creates a new EC_POINT object for the specified EC_GROUP
  368. * \param group EC_GROUP the underlying EC_GROUP object
  369. * \return newly created EC_POINT object or NULL if an error occurred
  370. */
  371. EC_POINT *EC_POINT_new(const EC_GROUP *group);
  372. /** Frees a EC_POINT object
  373. * \param point EC_POINT object to be freed
  374. */
  375. void EC_POINT_free(EC_POINT *point);
  376. /** Clears and frees a EC_POINT object
  377. * \param point EC_POINT object to be cleared and freed
  378. */
  379. void EC_POINT_clear_free(EC_POINT *point);
  380. /** Copies EC_POINT object
  381. * \param dst destination EC_POINT object
  382. * \param src source EC_POINT object
  383. * \return 1 on success and 0 if an error occured
  384. */
  385. int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src);
  386. /** Creates a new EC_POINT object and copies the content of the supplied
  387. * EC_POINT
  388. * \param src source EC_POINT object
  389. * \param group underlying the EC_GROUP object
  390. * \return newly created EC_POINT object or NULL if an error occurred
  391. */
  392. EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group);
  393. /** Returns the EC_METHOD used in EC_POINT object
  394. * \param point EC_POINT object
  395. * \return the EC_METHOD used
  396. */
  397. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point);
  398. /** Sets a point to infinity (neutral element)
  399. * \param group underlying EC_GROUP object
  400. * \param point EC_POINT to set to infinity
  401. * \return 1 on success and 0 if an error occured
  402. */
  403. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point);
  404. /** Sets the jacobian projective coordinates of a EC_POINT over GFp
  405. * \param group underlying EC_GROUP object
  406. * \param p EC_POINT object
  407. * \param x BIGNUM with the x-coordinate
  408. * \param y BIGNUM with the y-coordinate
  409. * \param z BIGNUM with the z-coordinate
  410. * \param ctx BN_CTX object (optional)
  411. * \return 1 on success and 0 if an error occured
  412. */
  413. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  414. EC_POINT *p, const BIGNUM *x,
  415. const BIGNUM *y, const BIGNUM *z,
  416. BN_CTX *ctx);
  417. /** Gets the jacobian projective coordinates of a EC_POINT over GFp
  418. * \param group underlying EC_GROUP object
  419. * \param p EC_POINT object
  420. * \param x BIGNUM for the x-coordinate
  421. * \param y BIGNUM for the y-coordinate
  422. * \param z BIGNUM for the z-coordinate
  423. * \param ctx BN_CTX object (optional)
  424. * \return 1 on success and 0 if an error occured
  425. */
  426. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  427. const EC_POINT *p, BIGNUM *x,
  428. BIGNUM *y, BIGNUM *z,
  429. BN_CTX *ctx);
  430. /** Sets the affine coordinates of a EC_POINT over GFp
  431. * \param group underlying EC_GROUP object
  432. * \param p EC_POINT object
  433. * \param x BIGNUM with the x-coordinate
  434. * \param y BIGNUM with the y-coordinate
  435. * \param ctx BN_CTX object (optional)
  436. * \return 1 on success and 0 if an error occured
  437. */
  438. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *p,
  439. const BIGNUM *x, const BIGNUM *y,
  440. BN_CTX *ctx);
  441. /** Gets the affine coordinates of a EC_POINT over GFp
  442. * \param group underlying EC_GROUP object
  443. * \param p EC_POINT object
  444. * \param x BIGNUM for the x-coordinate
  445. * \param y BIGNUM for the y-coordinate
  446. * \param ctx BN_CTX object (optional)
  447. * \return 1 on success and 0 if an error occured
  448. */
  449. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  450. const EC_POINT *p, BIGNUM *x,
  451. BIGNUM *y, BN_CTX *ctx);
  452. /** Sets the x9.62 compressed coordinates of a EC_POINT over GFp
  453. * \param group underlying EC_GROUP object
  454. * \param p EC_POINT object
  455. * \param x BIGNUM with x-coordinate
  456. * \param y_bit integer with the y-Bit (either 0 or 1)
  457. * \param ctx BN_CTX object (optional)
  458. * \return 1 on success and 0 if an error occured
  459. */
  460. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  461. EC_POINT *p, const BIGNUM *x,
  462. int y_bit, BN_CTX *ctx);
  463. # ifndef OPENSSL_NO_EC2M
  464. /** Sets the affine coordinates of a EC_POINT over GF2m
  465. * \param group underlying EC_GROUP object
  466. * \param p EC_POINT object
  467. * \param x BIGNUM with the x-coordinate
  468. * \param y BIGNUM with the y-coordinate
  469. * \param ctx BN_CTX object (optional)
  470. * \return 1 on success and 0 if an error occured
  471. */
  472. int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *p,
  473. const BIGNUM *x, const BIGNUM *y,
  474. BN_CTX *ctx);
  475. /** Gets the affine coordinates of a EC_POINT over GF2m
  476. * \param group underlying EC_GROUP object
  477. * \param p EC_POINT object
  478. * \param x BIGNUM for the x-coordinate
  479. * \param y BIGNUM for the y-coordinate
  480. * \param ctx BN_CTX object (optional)
  481. * \return 1 on success and 0 if an error occured
  482. */
  483. int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group,
  484. const EC_POINT *p, BIGNUM *x,
  485. BIGNUM *y, BN_CTX *ctx);
  486. /** Sets the x9.62 compressed coordinates of a EC_POINT over GF2m
  487. * \param group underlying EC_GROUP object
  488. * \param p EC_POINT object
  489. * \param x BIGNUM with x-coordinate
  490. * \param y_bit integer with the y-Bit (either 0 or 1)
  491. * \param ctx BN_CTX object (optional)
  492. * \return 1 on success and 0 if an error occured
  493. */
  494. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
  495. EC_POINT *p, const BIGNUM *x,
  496. int y_bit, BN_CTX *ctx);
  497. # endif
  498. /** Encodes a EC_POINT object to a octet string
  499. * \param group underlying EC_GROUP object
  500. * \param p EC_POINT object
  501. * \param form point conversion form
  502. * \param buf memory buffer for the result. If NULL the function returns
  503. * required buffer size.
  504. * \param len length of the memory buffer
  505. * \param ctx BN_CTX object (optional)
  506. * \return the length of the encoded octet string or 0 if an error occurred
  507. */
  508. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
  509. point_conversion_form_t form,
  510. unsigned char *buf, size_t len, BN_CTX *ctx);
  511. /** Decodes a EC_POINT from a octet string
  512. * \param group underlying EC_GROUP object
  513. * \param p EC_POINT object
  514. * \param buf memory buffer with the encoded ec point
  515. * \param len length of the encoded ec point
  516. * \param ctx BN_CTX object (optional)
  517. * \return 1 on success and 0 if an error occured
  518. */
  519. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
  520. const unsigned char *buf, size_t len, BN_CTX *ctx);
  521. /* other interfaces to point2oct/oct2point: */
  522. BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
  523. point_conversion_form_t form, BIGNUM *, BN_CTX *);
  524. EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *,
  525. EC_POINT *, BN_CTX *);
  526. char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
  527. point_conversion_form_t form, BN_CTX *);
  528. EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
  529. EC_POINT *, BN_CTX *);
  530. /********************************************************************/
  531. /* functions for doing EC_POINT arithmetic */
  532. /********************************************************************/
  533. /** Computes the sum of two EC_POINT
  534. * \param group underlying EC_GROUP object
  535. * \param r EC_POINT object for the result (r = a + b)
  536. * \param a EC_POINT object with the first summand
  537. * \param b EC_POINT object with the second summand
  538. * \param ctx BN_CTX object (optional)
  539. * \return 1 on success and 0 if an error occured
  540. */
  541. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  542. const EC_POINT *b, BN_CTX *ctx);
  543. /** Computes the double of a EC_POINT
  544. * \param group underlying EC_GROUP object
  545. * \param r EC_POINT object for the result (r = 2 * a)
  546. * \param a EC_POINT object
  547. * \param ctx BN_CTX object (optional)
  548. * \return 1 on success and 0 if an error occured
  549. */
  550. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  551. BN_CTX *ctx);
  552. /** Computes the inverse of a EC_POINT
  553. * \param group underlying EC_GROUP object
  554. * \param a EC_POINT object to be inverted (it's used for the result as well)
  555. * \param ctx BN_CTX object (optional)
  556. * \return 1 on success and 0 if an error occured
  557. */
  558. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx);
  559. /** Checks whether the point is the neutral element of the group
  560. * \param group the underlying EC_GROUP object
  561. * \param p EC_POINT object
  562. * \return 1 if the point is the neutral element and 0 otherwise
  563. */
  564. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p);
  565. /** Checks whether the point is on the curve
  566. * \param group underlying EC_GROUP object
  567. * \param point EC_POINT object to check
  568. * \param ctx BN_CTX object (optional)
  569. * \return 1 if point if on the curve and 0 otherwise
  570. */
  571. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  572. BN_CTX *ctx);
  573. /** Compares two EC_POINTs
  574. * \param group underlying EC_GROUP object
  575. * \param a first EC_POINT object
  576. * \param b second EC_POINT object
  577. * \param ctx BN_CTX object (optional)
  578. * \return 0 if both points are equal and a value != 0 otherwise
  579. */
  580. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  581. BN_CTX *ctx);
  582. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx);
  583. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  584. EC_POINT *points[], BN_CTX *ctx);
  585. /** Computes r = generator * n sum_{i=0}^{num-1} p[i] * m[i]
  586. * \param group underlying EC_GROUP object
  587. * \param r EC_POINT object for the result
  588. * \param n BIGNUM with the multiplier for the group generator (optional)
  589. * \param num number futher summands
  590. * \param p array of size num of EC_POINT objects
  591. * \param m array of size num of BIGNUM objects
  592. * \param ctx BN_CTX object (optional)
  593. * \return 1 on success and 0 if an error occured
  594. */
  595. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
  596. size_t num, const EC_POINT *p[], const BIGNUM *m[],
  597. BN_CTX *ctx);
  598. /** Computes r = generator * n + q * m
  599. * \param group underlying EC_GROUP object
  600. * \param r EC_POINT object for the result
  601. * \param n BIGNUM with the multiplier for the group generator (optional)
  602. * \param q EC_POINT object with the first factor of the second summand
  603. * \param m BIGNUM with the second factor of the second summand
  604. * \param ctx BN_CTX object (optional)
  605. * \return 1 on success and 0 if an error occured
  606. */
  607. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
  608. const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
  609. /** Stores multiples of generator for faster point multiplication
  610. * \param group EC_GROUP object
  611. * \param ctx BN_CTX object (optional)
  612. * \return 1 on success and 0 if an error occured
  613. */
  614. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  615. /** Reports whether a precomputation has been done
  616. * \param group EC_GROUP object
  617. * \return 1 if a pre-computation has been done and 0 otherwise
  618. */
  619. int EC_GROUP_have_precompute_mult(const EC_GROUP *group);
  620. /********************************************************************/
  621. /* ASN1 stuff */
  622. /********************************************************************/
  623. /*
  624. * EC_GROUP_get_basis_type() returns the NID of the basis type used to
  625. * represent the field elements
  626. */
  627. int EC_GROUP_get_basis_type(const EC_GROUP *);
  628. # ifndef OPENSSL_NO_EC2M
  629. int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k);
  630. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1,
  631. unsigned int *k2, unsigned int *k3);
  632. # endif
  633. # define OPENSSL_EC_NAMED_CURVE 0x001
  634. typedef struct ecpk_parameters_st ECPKPARAMETERS;
  635. EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
  636. int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
  637. # define d2i_ECPKParameters_bio(bp,x) ASN1_d2i_bio_of(EC_GROUP,NULL,d2i_ECPKParameters,bp,x)
  638. # define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio_of_const(EC_GROUP,i2d_ECPKParameters,bp,x)
  639. # define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \
  640. (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x))
  641. # define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \
  642. (unsigned char *)(x))
  643. # ifndef OPENSSL_NO_BIO
  644. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off);
  645. # endif
  646. # ifndef OPENSSL_NO_FP_API
  647. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
  648. # endif
  649. /********************************************************************/
  650. /* EC_KEY functions */
  651. /********************************************************************/
  652. typedef struct ec_key_st EC_KEY;
  653. /* some values for the encoding_flag */
  654. # define EC_PKEY_NO_PARAMETERS 0x001
  655. # define EC_PKEY_NO_PUBKEY 0x002
  656. /* some values for the flags field */
  657. # define EC_FLAG_NON_FIPS_ALLOW 0x1
  658. # define EC_FLAG_FIPS_CHECKED 0x2
  659. /** Creates a new EC_KEY object.
  660. * \return EC_KEY object or NULL if an error occurred.
  661. */
  662. EC_KEY *EC_KEY_new(void);
  663. int EC_KEY_get_flags(const EC_KEY *key);
  664. void EC_KEY_set_flags(EC_KEY *key, int flags);
  665. void EC_KEY_clear_flags(EC_KEY *key, int flags);
  666. /** Creates a new EC_KEY object using a named curve as underlying
  667. * EC_GROUP object.
  668. * \param nid NID of the named curve.
  669. * \return EC_KEY object or NULL if an error occurred.
  670. */
  671. EC_KEY *EC_KEY_new_by_curve_name(int nid);
  672. /** Frees a EC_KEY object.
  673. * \param key EC_KEY object to be freed.
  674. */
  675. void EC_KEY_free(EC_KEY *key);
  676. /** Copies a EC_KEY object.
  677. * \param dst destination EC_KEY object
  678. * \param src src EC_KEY object
  679. * \return dst or NULL if an error occurred.
  680. */
  681. EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
  682. /** Creates a new EC_KEY object and copies the content from src to it.
  683. * \param src the source EC_KEY object
  684. * \return newly created EC_KEY object or NULL if an error occurred.
  685. */
  686. EC_KEY *EC_KEY_dup(const EC_KEY *src);
  687. /** Increases the internal reference count of a EC_KEY object.
  688. * \param key EC_KEY object
  689. * \return 1 on success and 0 if an error occurred.
  690. */
  691. int EC_KEY_up_ref(EC_KEY *key);
  692. /** Returns the EC_GROUP object of a EC_KEY object
  693. * \param key EC_KEY object
  694. * \return the EC_GROUP object (possibly NULL).
  695. */
  696. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
  697. /** Sets the EC_GROUP of a EC_KEY object.
  698. * \param key EC_KEY object
  699. * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY
  700. * object will use an own copy of the EC_GROUP).
  701. * \return 1 on success and 0 if an error occurred.
  702. */
  703. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
  704. /** Returns the private key of a EC_KEY object.
  705. * \param key EC_KEY object
  706. * \return a BIGNUM with the private key (possibly NULL).
  707. */
  708. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
  709. /** Sets the private key of a EC_KEY object.
  710. * \param key EC_KEY object
  711. * \param prv BIGNUM with the private key (note: the EC_KEY object
  712. * will use an own copy of the BIGNUM).
  713. * \return 1 on success and 0 if an error occurred.
  714. */
  715. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
  716. /** Returns the public key of a EC_KEY object.
  717. * \param key the EC_KEY object
  718. * \return a EC_POINT object with the public key (possibly NULL)
  719. */
  720. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
  721. /** Sets the public key of a EC_KEY object.
  722. * \param key EC_KEY object
  723. * \param pub EC_POINT object with the public key (note: the EC_KEY object
  724. * will use an own copy of the EC_POINT object).
  725. * \return 1 on success and 0 if an error occurred.
  726. */
  727. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
  728. unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
  729. void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags);
  730. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
  731. void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform);
  732. /* functions to set/get method specific data */
  733. void *EC_KEY_get_key_method_data(EC_KEY *key,
  734. void *(*dup_func) (void *),
  735. void (*free_func) (void *),
  736. void (*clear_free_func) (void *));
  737. /** Sets the key method data of an EC_KEY object, if none has yet been set.
  738. * \param key EC_KEY object
  739. * \param data opaque data to install.
  740. * \param dup_func a function that duplicates |data|.
  741. * \param free_func a function that frees |data|.
  742. * \param clear_free_func a function that wipes and frees |data|.
  743. * \return the previously set data pointer, or NULL if |data| was inserted.
  744. */
  745. void *EC_KEY_insert_key_method_data(EC_KEY *key, void *data,
  746. void *(*dup_func) (void *),
  747. void (*free_func) (void *),
  748. void (*clear_free_func) (void *));
  749. /* wrapper functions for the underlying EC_GROUP object */
  750. void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
  751. /** Creates a table of pre-computed multiples of the generator to
  752. * accelerate further EC_KEY operations.
  753. * \param key EC_KEY object
  754. * \param ctx BN_CTX object (optional)
  755. * \return 1 on success and 0 if an error occurred.
  756. */
  757. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
  758. /** Creates a new ec private (and optional a new public) key.
  759. * \param key EC_KEY object
  760. * \return 1 on success and 0 if an error occurred.
  761. */
  762. int EC_KEY_generate_key(EC_KEY *key);
  763. /** Verifies that a private and/or public key is valid.
  764. * \param key the EC_KEY object
  765. * \return 1 on success and 0 otherwise.
  766. */
  767. int EC_KEY_check_key(const EC_KEY *key);
  768. /** Sets a public key from affine coordindates performing
  769. * neccessary NIST PKV tests.
  770. * \param key the EC_KEY object
  771. * \param x public key x coordinate
  772. * \param y public key y coordinate
  773. * \return 1 on success and 0 otherwise.
  774. */
  775. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  776. BIGNUM *y);
  777. /********************************************************************/
  778. /* de- and encoding functions for SEC1 ECPrivateKey */
  779. /********************************************************************/
  780. /** Decodes a private key from a memory buffer.
  781. * \param key a pointer to a EC_KEY object which should be used (or NULL)
  782. * \param in pointer to memory with the DER encoded private key
  783. * \param len length of the DER encoded private key
  784. * \return the decoded private key or NULL if an error occurred.
  785. */
  786. EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len);
  787. /** Encodes a private key object and stores the result in a buffer.
  788. * \param key the EC_KEY object to encode
  789. * \param out the buffer for the result (if NULL the function returns number
  790. * of bytes needed).
  791. * \return 1 on success and 0 if an error occurred.
  792. */
  793. int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out);
  794. /********************************************************************/
  795. /* de- and encoding functions for EC parameters */
  796. /********************************************************************/
  797. /** Decodes ec parameter from a memory buffer.
  798. * \param key a pointer to a EC_KEY object which should be used (or NULL)
  799. * \param in pointer to memory with the DER encoded ec parameters
  800. * \param len length of the DER encoded ec parameters
  801. * \return a EC_KEY object with the decoded parameters or NULL if an error
  802. * occurred.
  803. */
  804. EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len);
  805. /** Encodes ec parameter and stores the result in a buffer.
  806. * \param key the EC_KEY object with ec paramters to encode
  807. * \param out the buffer for the result (if NULL the function returns number
  808. * of bytes needed).
  809. * \return 1 on success and 0 if an error occurred.
  810. */
  811. int i2d_ECParameters(EC_KEY *key, unsigned char **out);
  812. /********************************************************************/
  813. /* de- and encoding functions for EC public key */
  814. /* (octet string, not DER -- hence 'o2i' and 'i2o') */
  815. /********************************************************************/
  816. /** Decodes a ec public key from a octet string.
  817. * \param key a pointer to a EC_KEY object which should be used
  818. * \param in memory buffer with the encoded public key
  819. * \param len length of the encoded public key
  820. * \return EC_KEY object with decoded public key or NULL if an error
  821. * occurred.
  822. */
  823. EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len);
  824. /** Encodes a ec public key in an octet string.
  825. * \param key the EC_KEY object with the public key
  826. * \param out the buffer for the result (if NULL the function returns number
  827. * of bytes needed).
  828. * \return 1 on success and 0 if an error occurred
  829. */
  830. int i2o_ECPublicKey(EC_KEY *key, unsigned char **out);
  831. # ifndef OPENSSL_NO_BIO
  832. /** Prints out the ec parameters on human readable form.
  833. * \param bp BIO object to which the information is printed
  834. * \param key EC_KEY object
  835. * \return 1 on success and 0 if an error occurred
  836. */
  837. int ECParameters_print(BIO *bp, const EC_KEY *key);
  838. /** Prints out the contents of a EC_KEY object
  839. * \param bp BIO object to which the information is printed
  840. * \param key EC_KEY object
  841. * \param off line offset
  842. * \return 1 on success and 0 if an error occurred
  843. */
  844. int EC_KEY_print(BIO *bp, const EC_KEY *key, int off);
  845. # endif
  846. # ifndef OPENSSL_NO_FP_API
  847. /** Prints out the ec parameters on human readable form.
  848. * \param fp file descriptor to which the information is printed
  849. * \param key EC_KEY object
  850. * \return 1 on success and 0 if an error occurred
  851. */
  852. int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
  853. /** Prints out the contents of a EC_KEY object
  854. * \param fp file descriptor to which the information is printed
  855. * \param key EC_KEY object
  856. * \param off line offset
  857. * \return 1 on success and 0 if an error occurred
  858. */
  859. int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
  860. # endif
  861. # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x)
  862. # ifndef __cplusplus
  863. # if defined(__SUNPRO_C)
  864. # if __SUNPRO_C >= 0x520
  865. # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
  866. # endif
  867. # endif
  868. # endif
  869. # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \
  870. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  871. EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
  872. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL)
  873. # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \
  874. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  875. EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
  876. EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL)
  877. # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \
  878. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  879. EVP_PKEY_OP_DERIVE, \
  880. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL)
  881. # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \
  882. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  883. EVP_PKEY_OP_DERIVE, \
  884. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL)
  885. # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \
  886. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  887. EVP_PKEY_OP_DERIVE, \
  888. EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL)
  889. # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \
  890. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  891. EVP_PKEY_OP_DERIVE, \
  892. EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL)
  893. # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \
  894. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  895. EVP_PKEY_OP_DERIVE, \
  896. EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)md)
  897. # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \
  898. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  899. EVP_PKEY_OP_DERIVE, \
  900. EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)pmd)
  901. # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \
  902. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  903. EVP_PKEY_OP_DERIVE, \
  904. EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL)
  905. # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \
  906. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  907. EVP_PKEY_OP_DERIVE, \
  908. EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, (void *)plen)
  909. # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \
  910. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  911. EVP_PKEY_OP_DERIVE, \
  912. EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)p)
  913. # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \
  914. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  915. EVP_PKEY_OP_DERIVE, \
  916. EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)p)
  917. # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
  918. # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
  919. # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3)
  920. # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4)
  921. # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5)
  922. # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6)
  923. # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7)
  924. # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
  925. # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
  926. # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
  927. /* KDF types */
  928. # define EVP_PKEY_ECDH_KDF_NONE 1
  929. # define EVP_PKEY_ECDH_KDF_X9_62 2
  930. /* BEGIN ERROR CODES */
  931. /*
  932. * The following lines are auto generated by the script mkerr.pl. Any changes
  933. * made after this point may be overwritten when the script is next run.
  934. */
  935. void ERR_load_EC_strings(void);
  936. /* Error codes for the EC functions. */
  937. /* Function codes. */
  938. # define EC_F_BN_TO_FELEM 224
  939. # define EC_F_COMPUTE_WNAF 143
  940. # define EC_F_D2I_ECPARAMETERS 144
  941. # define EC_F_D2I_ECPKPARAMETERS 145
  942. # define EC_F_D2I_ECPRIVATEKEY 146
  943. # define EC_F_DO_EC_KEY_PRINT 221
  944. # define EC_F_ECDH_CMS_DECRYPT 238
  945. # define EC_F_ECDH_CMS_SET_SHARED_INFO 239
  946. # define EC_F_ECKEY_PARAM2TYPE 223
  947. # define EC_F_ECKEY_PARAM_DECODE 212
  948. # define EC_F_ECKEY_PRIV_DECODE 213
  949. # define EC_F_ECKEY_PRIV_ENCODE 214
  950. # define EC_F_ECKEY_PUB_DECODE 215
  951. # define EC_F_ECKEY_PUB_ENCODE 216
  952. # define EC_F_ECKEY_TYPE2PARAM 220
  953. # define EC_F_ECPARAMETERS_PRINT 147
  954. # define EC_F_ECPARAMETERS_PRINT_FP 148
  955. # define EC_F_ECPKPARAMETERS_PRINT 149
  956. # define EC_F_ECPKPARAMETERS_PRINT_FP 150
  957. # define EC_F_ECP_NISTZ256_GET_AFFINE 240
  958. # define EC_F_ECP_NISTZ256_MULT_PRECOMPUTE 243
  959. # define EC_F_ECP_NISTZ256_POINTS_MUL 241
  960. # define EC_F_ECP_NISTZ256_PRE_COMP_NEW 244
  961. # define EC_F_ECP_NISTZ256_SET_WORDS 245
  962. # define EC_F_ECP_NISTZ256_WINDOWED_MUL 242
  963. # define EC_F_ECP_NIST_MOD_192 203
  964. # define EC_F_ECP_NIST_MOD_224 204
  965. # define EC_F_ECP_NIST_MOD_256 205
  966. # define EC_F_ECP_NIST_MOD_521 206
  967. # define EC_F_EC_ASN1_GROUP2CURVE 153
  968. # define EC_F_EC_ASN1_GROUP2FIELDID 154
  969. # define EC_F_EC_ASN1_GROUP2PARAMETERS 155
  970. # define EC_F_EC_ASN1_GROUP2PKPARAMETERS 156
  971. # define EC_F_EC_ASN1_PARAMETERS2GROUP 157
  972. # define EC_F_EC_ASN1_PKPARAMETERS2GROUP 158
  973. # define EC_F_EC_EX_DATA_SET_DATA 211
  974. # define EC_F_EC_GF2M_MONTGOMERY_POINT_MULTIPLY 208
  975. # define EC_F_EC_GF2M_SIMPLE_GROUP_CHECK_DISCRIMINANT 159
  976. # define EC_F_EC_GF2M_SIMPLE_GROUP_SET_CURVE 195
  977. # define EC_F_EC_GF2M_SIMPLE_OCT2POINT 160
  978. # define EC_F_EC_GF2M_SIMPLE_POINT2OCT 161
  979. # define EC_F_EC_GF2M_SIMPLE_POINT_GET_AFFINE_COORDINATES 162
  980. # define EC_F_EC_GF2M_SIMPLE_POINT_SET_AFFINE_COORDINATES 163
  981. # define EC_F_EC_GF2M_SIMPLE_SET_COMPRESSED_COORDINATES 164
  982. # define EC_F_EC_GFP_MONT_FIELD_DECODE 133
  983. # define EC_F_EC_GFP_MONT_FIELD_ENCODE 134
  984. # define EC_F_EC_GFP_MONT_FIELD_MUL 131
  985. # define EC_F_EC_GFP_MONT_FIELD_SET_TO_ONE 209
  986. # define EC_F_EC_GFP_MONT_FIELD_SQR 132
  987. # define EC_F_EC_GFP_MONT_GROUP_SET_CURVE 189
  988. # define EC_F_EC_GFP_MONT_GROUP_SET_CURVE_GFP 135
  989. # define EC_F_EC_GFP_NISTP224_GROUP_SET_CURVE 225
  990. # define EC_F_EC_GFP_NISTP224_POINTS_MUL 228
  991. # define EC_F_EC_GFP_NISTP224_POINT_GET_AFFINE_COORDINATES 226
  992. # define EC_F_EC_GFP_NISTP256_GROUP_SET_CURVE 230
  993. # define EC_F_EC_GFP_NISTP256_POINTS_MUL 231
  994. # define EC_F_EC_GFP_NISTP256_POINT_GET_AFFINE_COORDINATES 232
  995. # define EC_F_EC_GFP_NISTP521_GROUP_SET_CURVE 233
  996. # define EC_F_EC_GFP_NISTP521_POINTS_MUL 234
  997. # define EC_F_EC_GFP_NISTP521_POINT_GET_AFFINE_COORDINATES 235
  998. # define EC_F_EC_GFP_NIST_FIELD_MUL 200
  999. # define EC_F_EC_GFP_NIST_FIELD_SQR 201
  1000. # define EC_F_EC_GFP_NIST_GROUP_SET_CURVE 202
  1001. # define EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT 165
  1002. # define EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE 166
  1003. # define EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP 100
  1004. # define EC_F_EC_GFP_SIMPLE_GROUP_SET_GENERATOR 101
  1005. # define EC_F_EC_GFP_SIMPLE_MAKE_AFFINE 102
  1006. # define EC_F_EC_GFP_SIMPLE_OCT2POINT 103
  1007. # define EC_F_EC_GFP_SIMPLE_POINT2OCT 104
  1008. # define EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE 137
  1009. # define EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES 167
  1010. # define EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP 105
  1011. # define EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES 168
  1012. # define EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP 128
  1013. # define EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES 169
  1014. # define EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP 129
  1015. # define EC_F_EC_GROUP_CHECK 170
  1016. # define EC_F_EC_GROUP_CHECK_DISCRIMINANT 171
  1017. # define EC_F_EC_GROUP_COPY 106
  1018. # define EC_F_EC_GROUP_GET0_GENERATOR 139
  1019. # define EC_F_EC_GROUP_GET_COFACTOR 140
  1020. # define EC_F_EC_GROUP_GET_CURVE_GF2M 172
  1021. # define EC_F_EC_GROUP_GET_CURVE_GFP 130
  1022. # define EC_F_EC_GROUP_GET_DEGREE 173
  1023. # define EC_F_EC_GROUP_GET_ORDER 141
  1024. # define EC_F_EC_GROUP_GET_PENTANOMIAL_BASIS 193
  1025. # define EC_F_EC_GROUP_GET_TRINOMIAL_BASIS 194
  1026. # define EC_F_EC_GROUP_NEW 108
  1027. # define EC_F_EC_GROUP_NEW_BY_CURVE_NAME 174
  1028. # define EC_F_EC_GROUP_NEW_FROM_DATA 175
  1029. # define EC_F_EC_GROUP_PRECOMPUTE_MULT 142
  1030. # define EC_F_EC_GROUP_SET_CURVE_GF2M 176
  1031. # define EC_F_EC_GROUP_SET_CURVE_GFP 109
  1032. # define EC_F_EC_GROUP_SET_EXTRA_DATA 110
  1033. # define EC_F_EC_GROUP_SET_GENERATOR 111
  1034. # define EC_F_EC_KEY_CHECK_KEY 177
  1035. # define EC_F_EC_KEY_COPY 178
  1036. # define EC_F_EC_KEY_GENERATE_KEY 179
  1037. # define EC_F_EC_KEY_NEW 182
  1038. # define EC_F_EC_KEY_PRINT 180
  1039. # define EC_F_EC_KEY_PRINT_FP 181
  1040. # define EC_F_EC_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES 229
  1041. # define EC_F_EC_POINTS_MAKE_AFFINE 136
  1042. # define EC_F_EC_POINT_ADD 112
  1043. # define EC_F_EC_POINT_CMP 113
  1044. # define EC_F_EC_POINT_COPY 114
  1045. # define EC_F_EC_POINT_DBL 115
  1046. # define EC_F_EC_POINT_GET_AFFINE_COORDINATES_GF2M 183
  1047. # define EC_F_EC_POINT_GET_AFFINE_COORDINATES_GFP 116
  1048. # define EC_F_EC_POINT_GET_JPROJECTIVE_COORDINATES_GFP 117
  1049. # define EC_F_EC_POINT_INVERT 210
  1050. # define EC_F_EC_POINT_IS_AT_INFINITY 118
  1051. # define EC_F_EC_POINT_IS_ON_CURVE 119
  1052. # define EC_F_EC_POINT_MAKE_AFFINE 120
  1053. # define EC_F_EC_POINT_MUL 184
  1054. # define EC_F_EC_POINT_NEW 121
  1055. # define EC_F_EC_POINT_OCT2POINT 122
  1056. # define EC_F_EC_POINT_POINT2OCT 123
  1057. # define EC_F_EC_POINT_SET_AFFINE_COORDINATES_GF2M 185
  1058. # define EC_F_EC_POINT_SET_AFFINE_COORDINATES_GFP 124
  1059. # define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GF2M 186
  1060. # define EC_F_EC_POINT_SET_COMPRESSED_COORDINATES_GFP 125
  1061. # define EC_F_EC_POINT_SET_JPROJECTIVE_COORDINATES_GFP 126
  1062. # define EC_F_EC_POINT_SET_TO_INFINITY 127
  1063. # define EC_F_EC_PRE_COMP_DUP 207
  1064. # define EC_F_EC_PRE_COMP_NEW 196
  1065. # define EC_F_EC_WNAF_MUL 187
  1066. # define EC_F_EC_WNAF_PRECOMPUTE_MULT 188
  1067. # define EC_F_I2D_ECPARAMETERS 190
  1068. # define EC_F_I2D_ECPKPARAMETERS 191
  1069. # define EC_F_I2D_ECPRIVATEKEY 192
  1070. # define EC_F_I2O_ECPUBLICKEY 151
  1071. # define EC_F_NISTP224_PRE_COMP_NEW 227
  1072. # define EC_F_NISTP256_PRE_COMP_NEW 236
  1073. # define EC_F_NISTP521_PRE_COMP_NEW 237
  1074. # define EC_F_O2I_ECPUBLICKEY 152
  1075. # define EC_F_OLD_EC_PRIV_DECODE 222
  1076. # define EC_F_PKEY_EC_CTRL 197
  1077. # define EC_F_PKEY_EC_CTRL_STR 198
  1078. # define EC_F_PKEY_EC_DERIVE 217
  1079. # define EC_F_PKEY_EC_KEYGEN 199
  1080. # define EC_F_PKEY_EC_PARAMGEN 219
  1081. # define EC_F_PKEY_EC_SIGN 218
  1082. /* Reason codes. */
  1083. # define EC_R_ASN1_ERROR 115
  1084. # define EC_R_ASN1_UNKNOWN_FIELD 116
  1085. # define EC_R_BIGNUM_OUT_OF_RANGE 144
  1086. # define EC_R_BUFFER_TOO_SMALL 100
  1087. # define EC_R_COORDINATES_OUT_OF_RANGE 146
  1088. # define EC_R_D2I_ECPKPARAMETERS_FAILURE 117
  1089. # define EC_R_DECODE_ERROR 142
  1090. # define EC_R_DISCRIMINANT_IS_ZERO 118
  1091. # define EC_R_EC_GROUP_NEW_BY_NAME_FAILURE 119
  1092. # define EC_R_FIELD_TOO_LARGE 143
  1093. # define EC_R_GF2M_NOT_SUPPORTED 147
  1094. # define EC_R_GROUP2PKPARAMETERS_FAILURE 120
  1095. # define EC_R_I2D_ECPKPARAMETERS_FAILURE 121
  1096. # define EC_R_INCOMPATIBLE_OBJECTS 101
  1097. # define EC_R_INVALID_ARGUMENT 112
  1098. # define EC_R_INVALID_COMPRESSED_POINT 110
  1099. # define EC_R_INVALID_COMPRESSION_BIT 109
  1100. # define EC_R_INVALID_CURVE 141
  1101. # define EC_R_INVALID_DIGEST 151
  1102. # define EC_R_INVALID_DIGEST_TYPE 138
  1103. # define EC_R_INVALID_ENCODING 102
  1104. # define EC_R_INVALID_FIELD 103
  1105. # define EC_R_INVALID_FORM 104
  1106. # define EC_R_INVALID_GROUP_ORDER 122
  1107. # define EC_R_INVALID_PENTANOMIAL_BASIS 132
  1108. # define EC_R_INVALID_PRIVATE_KEY 123
  1109. # define EC_R_INVALID_TRINOMIAL_BASIS 137
  1110. # define EC_R_KDF_PARAMETER_ERROR 148
  1111. # define EC_R_KEYS_NOT_SET 140
  1112. # define EC_R_MISSING_PARAMETERS 124
  1113. # define EC_R_MISSING_PRIVATE_KEY 125
  1114. # define EC_R_NOT_A_NIST_PRIME 135
  1115. # define EC_R_NOT_A_SUPPORTED_NIST_PRIME 136
  1116. # define EC_R_NOT_IMPLEMENTED 126
  1117. # define EC_R_NOT_INITIALIZED 111
  1118. # define EC_R_NO_FIELD_MOD 133
  1119. # define EC_R_NO_PARAMETERS_SET 139
  1120. # define EC_R_PASSED_NULL_PARAMETER 134
  1121. # define EC_R_PEER_KEY_ERROR 149
  1122. # define EC_R_PKPARAMETERS2GROUP_FAILURE 127
  1123. # define EC_R_POINT_AT_INFINITY 106
  1124. # define EC_R_POINT_IS_NOT_ON_CURVE 107
  1125. # define EC_R_SHARED_INFO_ERROR 150
  1126. # define EC_R_SLOT_FULL 108
  1127. # define EC_R_UNDEFINED_GENERATOR 113
  1128. # define EC_R_UNDEFINED_ORDER 128
  1129. # define EC_R_UNKNOWN_COFACTOR 152
  1130. # define EC_R_UNKNOWN_GROUP 129
  1131. # define EC_R_UNKNOWN_ORDER 114
  1132. # define EC_R_UNSUPPORTED_FIELD 131
  1133. # define EC_R_WRONG_CURVE_PARAMETERS 145
  1134. # define EC_R_WRONG_ORDER 130
  1135. # ifdef __cplusplus
  1136. }
  1137. # endif
  1138. #endif