ec.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #ifndef HEADER_EC_H
  11. # define HEADER_EC_H
  12. # include <openssl/opensslconf.h>
  13. # ifndef OPENSSL_NO_EC
  14. # include <openssl/asn1.h>
  15. # include <openssl/symhacks.h>
  16. # if OPENSSL_API_COMPAT < 0x10100000L
  17. # include <openssl/bn.h>
  18. # endif
  19. # include <openssl/ecerr.h>
  20. # ifdef __cplusplus
  21. extern "C" {
  22. # endif
  23. # ifndef OPENSSL_ECC_MAX_FIELD_BITS
  24. # define OPENSSL_ECC_MAX_FIELD_BITS 661
  25. # endif
  26. /** Enum for the point conversion form as defined in X9.62 (ECDSA)
  27. * for the encoding of a elliptic curve point (x,y) */
  28. typedef enum {
  29. /** the point is encoded as z||x, where the octet z specifies
  30. * which solution of the quadratic equation y is */
  31. POINT_CONVERSION_COMPRESSED = 2,
  32. /** the point is encoded as z||x||y, where z is the octet 0x04 */
  33. POINT_CONVERSION_UNCOMPRESSED = 4,
  34. /** the point is encoded as z||x||y, where the octet z specifies
  35. * which solution of the quadratic equation y is */
  36. POINT_CONVERSION_HYBRID = 6
  37. } point_conversion_form_t;
  38. typedef struct ec_method_st EC_METHOD;
  39. typedef struct ec_group_st EC_GROUP;
  40. typedef struct ec_point_st EC_POINT;
  41. typedef struct ecpk_parameters_st ECPKPARAMETERS;
  42. typedef struct ec_parameters_st ECPARAMETERS;
  43. /********************************************************************/
  44. /* EC_METHODs for curves over GF(p) */
  45. /********************************************************************/
  46. /** Returns the basic GFp ec methods which provides the basis for the
  47. * optimized methods.
  48. * \return EC_METHOD object
  49. */
  50. const EC_METHOD *EC_GFp_simple_method(void);
  51. /** Returns GFp methods using montgomery multiplication.
  52. * \return EC_METHOD object
  53. */
  54. const EC_METHOD *EC_GFp_mont_method(void);
  55. /** Returns GFp methods using optimized methods for NIST recommended curves
  56. * \return EC_METHOD object
  57. */
  58. const EC_METHOD *EC_GFp_nist_method(void);
  59. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  60. /** Returns 64-bit optimized methods for nistp224
  61. * \return EC_METHOD object
  62. */
  63. const EC_METHOD *EC_GFp_nistp224_method(void);
  64. /** Returns 64-bit optimized methods for nistp256
  65. * \return EC_METHOD object
  66. */
  67. const EC_METHOD *EC_GFp_nistp256_method(void);
  68. /** Returns 64-bit optimized methods for nistp521
  69. * \return EC_METHOD object
  70. */
  71. const EC_METHOD *EC_GFp_nistp521_method(void);
  72. # endif
  73. # ifndef OPENSSL_NO_EC2M
  74. /********************************************************************/
  75. /* EC_METHOD for curves over GF(2^m) */
  76. /********************************************************************/
  77. /** Returns the basic GF2m ec method
  78. * \return EC_METHOD object
  79. */
  80. const EC_METHOD *EC_GF2m_simple_method(void);
  81. # endif
  82. /********************************************************************/
  83. /* EC_GROUP functions */
  84. /********************************************************************/
  85. /** Creates a new EC_GROUP object
  86. * \param meth EC_METHOD to use
  87. * \return newly created EC_GROUP object or NULL in case of an error.
  88. */
  89. EC_GROUP *EC_GROUP_new(const EC_METHOD *meth);
  90. /** Frees a EC_GROUP object
  91. * \param group EC_GROUP object to be freed.
  92. */
  93. void EC_GROUP_free(EC_GROUP *group);
  94. /** Clears and frees a EC_GROUP object
  95. * \param group EC_GROUP object to be cleared and freed.
  96. */
  97. void EC_GROUP_clear_free(EC_GROUP *group);
  98. /** Copies EC_GROUP objects. Note: both EC_GROUPs must use the same EC_METHOD.
  99. * \param dst destination EC_GROUP object
  100. * \param src source EC_GROUP object
  101. * \return 1 on success and 0 if an error occurred.
  102. */
  103. int EC_GROUP_copy(EC_GROUP *dst, const EC_GROUP *src);
  104. /** Creates a new EC_GROUP object and copies the copies the content
  105. * form src to the newly created EC_KEY object
  106. * \param src source EC_GROUP object
  107. * \return newly created EC_GROUP object or NULL in case of an error.
  108. */
  109. EC_GROUP *EC_GROUP_dup(const EC_GROUP *src);
  110. /** Returns the EC_METHOD of the EC_GROUP object.
  111. * \param group EC_GROUP object
  112. * \return EC_METHOD used in this EC_GROUP object.
  113. */
  114. const EC_METHOD *EC_GROUP_method_of(const EC_GROUP *group);
  115. /** Returns the field type of the EC_METHOD.
  116. * \param meth EC_METHOD object
  117. * \return NID of the underlying field type OID.
  118. */
  119. int EC_METHOD_get_field_type(const EC_METHOD *meth);
  120. /** Sets the generator and it's order/cofactor of a EC_GROUP object.
  121. * \param group EC_GROUP object
  122. * \param generator EC_POINT object with the generator.
  123. * \param order the order of the group generated by the generator.
  124. * \param cofactor the index of the sub-group generated by the generator
  125. * in the group of all points on the elliptic curve.
  126. * \return 1 on success and 0 if an error occurred
  127. */
  128. int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator,
  129. const BIGNUM *order, const BIGNUM *cofactor);
  130. /** Returns the generator of a EC_GROUP object.
  131. * \param group EC_GROUP object
  132. * \return the currently used generator (possibly NULL).
  133. */
  134. const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group);
  135. /** Returns the montgomery data for order(Generator)
  136. * \param group EC_GROUP object
  137. * \return the currently used montgomery data (possibly NULL).
  138. */
  139. BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group);
  140. /** Gets the order of a EC_GROUP
  141. * \param group EC_GROUP object
  142. * \param order BIGNUM to which the order is copied
  143. * \param ctx unused
  144. * \return 1 on success and 0 if an error occurred
  145. */
  146. int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx);
  147. /** Gets the order of an EC_GROUP
  148. * \param group EC_GROUP object
  149. * \return the group order
  150. */
  151. const BIGNUM *EC_GROUP_get0_order(const EC_GROUP *group);
  152. /** Gets the number of bits of the order of an EC_GROUP
  153. * \param group EC_GROUP object
  154. * \return number of bits of group order.
  155. */
  156. int EC_GROUP_order_bits(const EC_GROUP *group);
  157. /** Gets the cofactor of a EC_GROUP
  158. * \param group EC_GROUP object
  159. * \param cofactor BIGNUM to which the cofactor is copied
  160. * \param ctx unused
  161. * \return 1 on success and 0 if an error occurred
  162. */
  163. int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor,
  164. BN_CTX *ctx);
  165. /** Gets the cofactor of an EC_GROUP
  166. * \param group EC_GROUP object
  167. * \return the group cofactor
  168. */
  169. const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group);
  170. /** Sets the name of a EC_GROUP object
  171. * \param group EC_GROUP object
  172. * \param nid NID of the curve name OID
  173. */
  174. void EC_GROUP_set_curve_name(EC_GROUP *group, int nid);
  175. /** Returns the curve name of a EC_GROUP object
  176. * \param group EC_GROUP object
  177. * \return NID of the curve name OID or 0 if not set.
  178. */
  179. int EC_GROUP_get_curve_name(const EC_GROUP *group);
  180. void EC_GROUP_set_asn1_flag(EC_GROUP *group, int flag);
  181. int EC_GROUP_get_asn1_flag(const EC_GROUP *group);
  182. void EC_GROUP_set_point_conversion_form(EC_GROUP *group,
  183. point_conversion_form_t form);
  184. point_conversion_form_t EC_GROUP_get_point_conversion_form(const EC_GROUP *);
  185. unsigned char *EC_GROUP_get0_seed(const EC_GROUP *x);
  186. size_t EC_GROUP_get_seed_len(const EC_GROUP *);
  187. size_t EC_GROUP_set_seed(EC_GROUP *, const unsigned char *, size_t len);
  188. /** Sets the parameter of a ec over GFp defined by y^2 = x^3 + a*x + b
  189. * \param group EC_GROUP object
  190. * \param p BIGNUM with the prime number
  191. * \param a BIGNUM with parameter a of the equation
  192. * \param b BIGNUM with parameter b of the equation
  193. * \param ctx BN_CTX object (optional)
  194. * \return 1 on success and 0 if an error occurred
  195. */
  196. int EC_GROUP_set_curve_GFp(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  197. const BIGNUM *b, BN_CTX *ctx);
  198. /** Gets the parameter of the ec over GFp defined by y^2 = x^3 + a*x + b
  199. * \param group EC_GROUP object
  200. * \param p BIGNUM for the prime number
  201. * \param a BIGNUM for parameter a of the equation
  202. * \param b BIGNUM for parameter b of the equation
  203. * \param ctx BN_CTX object (optional)
  204. * \return 1 on success and 0 if an error occurred
  205. */
  206. int EC_GROUP_get_curve_GFp(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  207. BIGNUM *b, BN_CTX *ctx);
  208. # ifndef OPENSSL_NO_EC2M
  209. /** Sets the parameter of a ec over GF2m defined by y^2 + x*y = x^3 + a*x^2 + b
  210. * \param group EC_GROUP object
  211. * \param p BIGNUM with the polynomial defining the underlying field
  212. * \param a BIGNUM with parameter a of the equation
  213. * \param b BIGNUM with parameter b of the equation
  214. * \param ctx BN_CTX object (optional)
  215. * \return 1 on success and 0 if an error occurred
  216. */
  217. int EC_GROUP_set_curve_GF2m(EC_GROUP *group, const BIGNUM *p, const BIGNUM *a,
  218. const BIGNUM *b, BN_CTX *ctx);
  219. /** Gets the parameter of the ec over GF2m defined by y^2 + x*y = x^3 + a*x^2 + b
  220. * \param group EC_GROUP object
  221. * \param p BIGNUM for the polynomial defining the underlying field
  222. * \param a BIGNUM for parameter a of the equation
  223. * \param b BIGNUM for parameter b of the equation
  224. * \param ctx BN_CTX object (optional)
  225. * \return 1 on success and 0 if an error occurred
  226. */
  227. int EC_GROUP_get_curve_GF2m(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
  228. BIGNUM *b, BN_CTX *ctx);
  229. # endif
  230. /** Returns the number of bits needed to represent a field element
  231. * \param group EC_GROUP object
  232. * \return number of bits needed to represent a field element
  233. */
  234. int EC_GROUP_get_degree(const EC_GROUP *group);
  235. /** Checks whether the parameter in the EC_GROUP define a valid ec group
  236. * \param group EC_GROUP object
  237. * \param ctx BN_CTX object (optional)
  238. * \return 1 if group is a valid ec group and 0 otherwise
  239. */
  240. int EC_GROUP_check(const EC_GROUP *group, BN_CTX *ctx);
  241. /** Checks whether the discriminant of the elliptic curve is zero or not
  242. * \param group EC_GROUP object
  243. * \param ctx BN_CTX object (optional)
  244. * \return 1 if the discriminant is not zero and 0 otherwise
  245. */
  246. int EC_GROUP_check_discriminant(const EC_GROUP *group, BN_CTX *ctx);
  247. /** Compares two EC_GROUP objects
  248. * \param a first EC_GROUP object
  249. * \param b second EC_GROUP object
  250. * \param ctx BN_CTX object (optional)
  251. * \return 0 if the groups are equal, 1 if not, or -1 on error
  252. */
  253. int EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx);
  254. /*
  255. * EC_GROUP_new_GF*() calls EC_GROUP_new() and EC_GROUP_set_GF*() after
  256. * choosing an appropriate EC_METHOD
  257. */
  258. /** Creates a new EC_GROUP object with the specified parameters defined
  259. * over GFp (defined by the equation y^2 = x^3 + a*x + b)
  260. * \param p BIGNUM with the prime number
  261. * \param a BIGNUM with the parameter a of the equation
  262. * \param b BIGNUM with the parameter b of the equation
  263. * \param ctx BN_CTX object (optional)
  264. * \return newly created EC_GROUP object with the specified parameters
  265. */
  266. EC_GROUP *EC_GROUP_new_curve_GFp(const BIGNUM *p, const BIGNUM *a,
  267. const BIGNUM *b, BN_CTX *ctx);
  268. # ifndef OPENSSL_NO_EC2M
  269. /** Creates a new EC_GROUP object with the specified parameters defined
  270. * over GF2m (defined by the equation y^2 + x*y = x^3 + a*x^2 + b)
  271. * \param p BIGNUM with the polynomial defining the underlying field
  272. * \param a BIGNUM with the parameter a of the equation
  273. * \param b BIGNUM with the parameter b of the equation
  274. * \param ctx BN_CTX object (optional)
  275. * \return newly created EC_GROUP object with the specified parameters
  276. */
  277. EC_GROUP *EC_GROUP_new_curve_GF2m(const BIGNUM *p, const BIGNUM *a,
  278. const BIGNUM *b, BN_CTX *ctx);
  279. # endif
  280. /** Creates a EC_GROUP object with a curve specified by a NID
  281. * \param nid NID of the OID of the curve name
  282. * \return newly created EC_GROUP object with specified curve or NULL
  283. * if an error occurred
  284. */
  285. EC_GROUP *EC_GROUP_new_by_curve_name(int nid);
  286. /** Creates a new EC_GROUP object from an ECPARAMETERS object
  287. * \param params pointer to the ECPARAMETERS object
  288. * \return newly created EC_GROUP object with specified curve or NULL
  289. * if an error occurred
  290. */
  291. EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params);
  292. /** Creates an ECPARAMETERS object for the given EC_GROUP object.
  293. * \param group pointer to the EC_GROUP object
  294. * \param params pointer to an existing ECPARAMETERS object or NULL
  295. * \return pointer to the new ECPARAMETERS object or NULL
  296. * if an error occurred.
  297. */
  298. ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group,
  299. ECPARAMETERS *params);
  300. /** Creates a new EC_GROUP object from an ECPKPARAMETERS object
  301. * \param params pointer to an existing ECPKPARAMETERS object, or NULL
  302. * \return newly created EC_GROUP object with specified curve, or NULL
  303. * if an error occurred
  304. */
  305. EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params);
  306. /** Creates an ECPKPARAMETERS object for the given EC_GROUP object.
  307. * \param group pointer to the EC_GROUP object
  308. * \param params pointer to an existing ECPKPARAMETERS object or NULL
  309. * \return pointer to the new ECPKPARAMETERS object or NULL
  310. * if an error occurred.
  311. */
  312. ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group,
  313. ECPKPARAMETERS *params);
  314. /********************************************************************/
  315. /* handling of internal curves */
  316. /********************************************************************/
  317. typedef struct {
  318. int nid;
  319. const char *comment;
  320. } EC_builtin_curve;
  321. /*
  322. * EC_builtin_curves(EC_builtin_curve *r, size_t size) returns number of all
  323. * available curves or zero if a error occurred. In case r is not zero,
  324. * nitems EC_builtin_curve structures are filled with the data of the first
  325. * nitems internal groups
  326. */
  327. size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems);
  328. const char *EC_curve_nid2nist(int nid);
  329. int EC_curve_nist2nid(const char *name);
  330. /********************************************************************/
  331. /* EC_POINT functions */
  332. /********************************************************************/
  333. /** Creates a new EC_POINT object for the specified EC_GROUP
  334. * \param group EC_GROUP the underlying EC_GROUP object
  335. * \return newly created EC_POINT object or NULL if an error occurred
  336. */
  337. EC_POINT *EC_POINT_new(const EC_GROUP *group);
  338. /** Frees a EC_POINT object
  339. * \param point EC_POINT object to be freed
  340. */
  341. void EC_POINT_free(EC_POINT *point);
  342. /** Clears and frees a EC_POINT object
  343. * \param point EC_POINT object to be cleared and freed
  344. */
  345. void EC_POINT_clear_free(EC_POINT *point);
  346. /** Copies EC_POINT object
  347. * \param dst destination EC_POINT object
  348. * \param src source EC_POINT object
  349. * \return 1 on success and 0 if an error occurred
  350. */
  351. int EC_POINT_copy(EC_POINT *dst, const EC_POINT *src);
  352. /** Creates a new EC_POINT object and copies the content of the supplied
  353. * EC_POINT
  354. * \param src source EC_POINT object
  355. * \param group underlying the EC_GROUP object
  356. * \return newly created EC_POINT object or NULL if an error occurred
  357. */
  358. EC_POINT *EC_POINT_dup(const EC_POINT *src, const EC_GROUP *group);
  359. /** Returns the EC_METHOD used in EC_POINT object
  360. * \param point EC_POINT object
  361. * \return the EC_METHOD used
  362. */
  363. const EC_METHOD *EC_POINT_method_of(const EC_POINT *point);
  364. /** Sets a point to infinity (neutral element)
  365. * \param group underlying EC_GROUP object
  366. * \param point EC_POINT to set to infinity
  367. * \return 1 on success and 0 if an error occurred
  368. */
  369. int EC_POINT_set_to_infinity(const EC_GROUP *group, EC_POINT *point);
  370. /** Sets the jacobian projective coordinates of a EC_POINT over GFp
  371. * \param group underlying EC_GROUP object
  372. * \param p EC_POINT object
  373. * \param x BIGNUM with the x-coordinate
  374. * \param y BIGNUM with the y-coordinate
  375. * \param z BIGNUM with the z-coordinate
  376. * \param ctx BN_CTX object (optional)
  377. * \return 1 on success and 0 if an error occurred
  378. */
  379. int EC_POINT_set_Jprojective_coordinates_GFp(const EC_GROUP *group,
  380. EC_POINT *p, const BIGNUM *x,
  381. const BIGNUM *y, const BIGNUM *z,
  382. BN_CTX *ctx);
  383. /** Gets the jacobian projective coordinates of a EC_POINT over GFp
  384. * \param group underlying EC_GROUP object
  385. * \param p EC_POINT object
  386. * \param x BIGNUM for the x-coordinate
  387. * \param y BIGNUM for the y-coordinate
  388. * \param z BIGNUM for the z-coordinate
  389. * \param ctx BN_CTX object (optional)
  390. * \return 1 on success and 0 if an error occurred
  391. */
  392. int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group,
  393. const EC_POINT *p, BIGNUM *x,
  394. BIGNUM *y, BIGNUM *z,
  395. BN_CTX *ctx);
  396. /** Sets the affine coordinates of a EC_POINT over GFp
  397. * \param group underlying EC_GROUP object
  398. * \param p EC_POINT object
  399. * \param x BIGNUM with the x-coordinate
  400. * \param y BIGNUM with the y-coordinate
  401. * \param ctx BN_CTX object (optional)
  402. * \return 1 on success and 0 if an error occurred
  403. */
  404. int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *p,
  405. const BIGNUM *x, const BIGNUM *y,
  406. BN_CTX *ctx);
  407. /** Gets the affine coordinates of a EC_POINT over GFp
  408. * \param group underlying EC_GROUP object
  409. * \param p EC_POINT object
  410. * \param x BIGNUM for the x-coordinate
  411. * \param y BIGNUM for the y-coordinate
  412. * \param ctx BN_CTX object (optional)
  413. * \return 1 on success and 0 if an error occurred
  414. */
  415. int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group,
  416. const EC_POINT *p, BIGNUM *x,
  417. BIGNUM *y, BN_CTX *ctx);
  418. /** Sets the x9.62 compressed coordinates of a EC_POINT over GFp
  419. * \param group underlying EC_GROUP object
  420. * \param p EC_POINT object
  421. * \param x BIGNUM with x-coordinate
  422. * \param y_bit integer with the y-Bit (either 0 or 1)
  423. * \param ctx BN_CTX object (optional)
  424. * \return 1 on success and 0 if an error occurred
  425. */
  426. int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group,
  427. EC_POINT *p, const BIGNUM *x,
  428. int y_bit, BN_CTX *ctx);
  429. # ifndef OPENSSL_NO_EC2M
  430. /** Sets the affine coordinates of a EC_POINT over GF2m
  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 occurred
  437. */
  438. int EC_POINT_set_affine_coordinates_GF2m(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 GF2m
  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 occurred
  448. */
  449. int EC_POINT_get_affine_coordinates_GF2m(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 GF2m
  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 occurred
  459. */
  460. int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group,
  461. EC_POINT *p, const BIGNUM *x,
  462. int y_bit, BN_CTX *ctx);
  463. # endif
  464. /** Encodes a EC_POINT object to a octet string
  465. * \param group underlying EC_GROUP object
  466. * \param p EC_POINT object
  467. * \param form point conversion form
  468. * \param buf memory buffer for the result. If NULL the function returns
  469. * required buffer size.
  470. * \param len length of the memory buffer
  471. * \param ctx BN_CTX object (optional)
  472. * \return the length of the encoded octet string or 0 if an error occurred
  473. */
  474. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
  475. point_conversion_form_t form,
  476. unsigned char *buf, size_t len, BN_CTX *ctx);
  477. /** Decodes a EC_POINT from a octet string
  478. * \param group underlying EC_GROUP object
  479. * \param p EC_POINT object
  480. * \param buf memory buffer with the encoded ec point
  481. * \param len length of the encoded ec point
  482. * \param ctx BN_CTX object (optional)
  483. * \return 1 on success and 0 if an error occurred
  484. */
  485. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
  486. const unsigned char *buf, size_t len, BN_CTX *ctx);
  487. /** Encodes an EC_POINT object to an allocated octet string
  488. * \param group underlying EC_GROUP object
  489. * \param point EC_POINT object
  490. * \param form point conversion form
  491. * \param pbuf returns pointer to allocated buffer
  492. * \param ctx BN_CTX object (optional)
  493. * \return the length of the encoded octet string or 0 if an error occurred
  494. */
  495. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  496. point_conversion_form_t form,
  497. unsigned char **pbuf, BN_CTX *ctx);
  498. /* other interfaces to point2oct/oct2point: */
  499. BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *,
  500. point_conversion_form_t form, BIGNUM *, BN_CTX *);
  501. EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *,
  502. EC_POINT *, BN_CTX *);
  503. char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
  504. point_conversion_form_t form, BN_CTX *);
  505. EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
  506. EC_POINT *, BN_CTX *);
  507. /********************************************************************/
  508. /* functions for doing EC_POINT arithmetic */
  509. /********************************************************************/
  510. /** Computes the sum of two EC_POINT
  511. * \param group underlying EC_GROUP object
  512. * \param r EC_POINT object for the result (r = a + b)
  513. * \param a EC_POINT object with the first summand
  514. * \param b EC_POINT object with the second summand
  515. * \param ctx BN_CTX object (optional)
  516. * \return 1 on success and 0 if an error occurred
  517. */
  518. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  519. const EC_POINT *b, BN_CTX *ctx);
  520. /** Computes the double of a EC_POINT
  521. * \param group underlying EC_GROUP object
  522. * \param r EC_POINT object for the result (r = 2 * a)
  523. * \param a EC_POINT object
  524. * \param ctx BN_CTX object (optional)
  525. * \return 1 on success and 0 if an error occurred
  526. */
  527. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  528. BN_CTX *ctx);
  529. /** Computes the inverse of a EC_POINT
  530. * \param group underlying EC_GROUP object
  531. * \param a EC_POINT object to be inverted (it's used for the result as well)
  532. * \param ctx BN_CTX object (optional)
  533. * \return 1 on success and 0 if an error occurred
  534. */
  535. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx);
  536. /** Checks whether the point is the neutral element of the group
  537. * \param group the underlying EC_GROUP object
  538. * \param p EC_POINT object
  539. * \return 1 if the point is the neutral element and 0 otherwise
  540. */
  541. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p);
  542. /** Checks whether the point is on the curve
  543. * \param group underlying EC_GROUP object
  544. * \param point EC_POINT object to check
  545. * \param ctx BN_CTX object (optional)
  546. * \return 1 if the point is on the curve, 0 if not, or -1 on error
  547. */
  548. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  549. BN_CTX *ctx);
  550. /** Compares two EC_POINTs
  551. * \param group underlying EC_GROUP object
  552. * \param a first EC_POINT object
  553. * \param b second EC_POINT object
  554. * \param ctx BN_CTX object (optional)
  555. * \return 1 if the points are not equal, 0 if they are, or -1 on error
  556. */
  557. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  558. BN_CTX *ctx);
  559. int EC_POINT_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx);
  560. int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  561. EC_POINT *points[], BN_CTX *ctx);
  562. /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i]
  563. * \param group underlying EC_GROUP object
  564. * \param r EC_POINT object for the result
  565. * \param n BIGNUM with the multiplier for the group generator (optional)
  566. * \param num number further summands
  567. * \param p array of size num of EC_POINT objects
  568. * \param m array of size num of BIGNUM objects
  569. * \param ctx BN_CTX object (optional)
  570. * \return 1 on success and 0 if an error occurred
  571. */
  572. int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
  573. size_t num, const EC_POINT *p[], const BIGNUM *m[],
  574. BN_CTX *ctx);
  575. /** Computes r = generator * n + q * m
  576. * \param group underlying EC_GROUP object
  577. * \param r EC_POINT object for the result
  578. * \param n BIGNUM with the multiplier for the group generator (optional)
  579. * \param q EC_POINT object with the first factor of the second summand
  580. * \param m BIGNUM with the second factor of the second summand
  581. * \param ctx BN_CTX object (optional)
  582. * \return 1 on success and 0 if an error occurred
  583. */
  584. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
  585. const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
  586. /** Stores multiples of generator for faster point multiplication
  587. * \param group EC_GROUP object
  588. * \param ctx BN_CTX object (optional)
  589. * \return 1 on success and 0 if an error occurred
  590. */
  591. int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  592. /** Reports whether a precomputation has been done
  593. * \param group EC_GROUP object
  594. * \return 1 if a pre-computation has been done and 0 otherwise
  595. */
  596. int EC_GROUP_have_precompute_mult(const EC_GROUP *group);
  597. /********************************************************************/
  598. /* ASN1 stuff */
  599. /********************************************************************/
  600. DECLARE_ASN1_ITEM(ECPKPARAMETERS)
  601. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS)
  602. DECLARE_ASN1_ITEM(ECPARAMETERS)
  603. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  604. /*
  605. * EC_GROUP_get_basis_type() returns the NID of the basis type used to
  606. * represent the field elements
  607. */
  608. int EC_GROUP_get_basis_type(const EC_GROUP *);
  609. # ifndef OPENSSL_NO_EC2M
  610. int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k);
  611. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1,
  612. unsigned int *k2, unsigned int *k3);
  613. # endif
  614. # define OPENSSL_EC_EXPLICIT_CURVE 0x000
  615. # define OPENSSL_EC_NAMED_CURVE 0x001
  616. EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
  617. int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
  618. # define d2i_ECPKParameters_bio(bp,x) ASN1_d2i_bio_of(EC_GROUP,NULL,d2i_ECPKParameters,bp,x)
  619. # define i2d_ECPKParameters_bio(bp,x) ASN1_i2d_bio_of_const(EC_GROUP,i2d_ECPKParameters,bp,x)
  620. # define d2i_ECPKParameters_fp(fp,x) (EC_GROUP *)ASN1_d2i_fp(NULL, \
  621. (char *(*)())d2i_ECPKParameters,(fp),(unsigned char **)(x))
  622. # define i2d_ECPKParameters_fp(fp,x) ASN1_i2d_fp(i2d_ECPKParameters,(fp), \
  623. (unsigned char *)(x))
  624. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off);
  625. # ifndef OPENSSL_NO_STDIO
  626. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off);
  627. # endif
  628. /********************************************************************/
  629. /* EC_KEY functions */
  630. /********************************************************************/
  631. /* some values for the encoding_flag */
  632. # define EC_PKEY_NO_PARAMETERS 0x001
  633. # define EC_PKEY_NO_PUBKEY 0x002
  634. /* some values for the flags field */
  635. # define EC_FLAG_NON_FIPS_ALLOW 0x1
  636. # define EC_FLAG_FIPS_CHECKED 0x2
  637. # define EC_FLAG_COFACTOR_ECDH 0x1000
  638. /** Creates a new EC_KEY object.
  639. * \return EC_KEY object or NULL if an error occurred.
  640. */
  641. EC_KEY *EC_KEY_new(void);
  642. int EC_KEY_get_flags(const EC_KEY *key);
  643. void EC_KEY_set_flags(EC_KEY *key, int flags);
  644. void EC_KEY_clear_flags(EC_KEY *key, int flags);
  645. /** Creates a new EC_KEY object using a named curve as underlying
  646. * EC_GROUP object.
  647. * \param nid NID of the named curve.
  648. * \return EC_KEY object or NULL if an error occurred.
  649. */
  650. EC_KEY *EC_KEY_new_by_curve_name(int nid);
  651. /** Frees a EC_KEY object.
  652. * \param key EC_KEY object to be freed.
  653. */
  654. void EC_KEY_free(EC_KEY *key);
  655. /** Copies a EC_KEY object.
  656. * \param dst destination EC_KEY object
  657. * \param src src EC_KEY object
  658. * \return dst or NULL if an error occurred.
  659. */
  660. EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
  661. /** Creates a new EC_KEY object and copies the content from src to it.
  662. * \param src the source EC_KEY object
  663. * \return newly created EC_KEY object or NULL if an error occurred.
  664. */
  665. EC_KEY *EC_KEY_dup(const EC_KEY *src);
  666. /** Increases the internal reference count of a EC_KEY object.
  667. * \param key EC_KEY object
  668. * \return 1 on success and 0 if an error occurred.
  669. */
  670. int EC_KEY_up_ref(EC_KEY *key);
  671. /** Returns the ENGINE object of a EC_KEY object
  672. * \param eckey EC_KEY object
  673. * \return the ENGINE object (possibly NULL).
  674. */
  675. ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey);
  676. /** Returns the EC_GROUP object of a EC_KEY object
  677. * \param key EC_KEY object
  678. * \return the EC_GROUP object (possibly NULL).
  679. */
  680. const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
  681. /** Sets the EC_GROUP of a EC_KEY object.
  682. * \param key EC_KEY object
  683. * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY
  684. * object will use an own copy of the EC_GROUP).
  685. * \return 1 on success and 0 if an error occurred.
  686. */
  687. int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
  688. /** Returns the private key of a EC_KEY object.
  689. * \param key EC_KEY object
  690. * \return a BIGNUM with the private key (possibly NULL).
  691. */
  692. const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
  693. /** Sets the private key of a EC_KEY object.
  694. * \param key EC_KEY object
  695. * \param prv BIGNUM with the private key (note: the EC_KEY object
  696. * will use an own copy of the BIGNUM).
  697. * \return 1 on success and 0 if an error occurred.
  698. */
  699. int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
  700. /** Returns the public key of a EC_KEY object.
  701. * \param key the EC_KEY object
  702. * \return a EC_POINT object with the public key (possibly NULL)
  703. */
  704. const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
  705. /** Sets the public key of a EC_KEY object.
  706. * \param key EC_KEY object
  707. * \param pub EC_POINT object with the public key (note: the EC_KEY object
  708. * will use an own copy of the EC_POINT object).
  709. * \return 1 on success and 0 if an error occurred.
  710. */
  711. int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
  712. unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
  713. void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags);
  714. point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
  715. void EC_KEY_set_conv_form(EC_KEY *eckey, point_conversion_form_t cform);
  716. #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \
  717. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef)
  718. int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg);
  719. void *EC_KEY_get_ex_data(const EC_KEY *key, int idx);
  720. /* wrapper functions for the underlying EC_GROUP object */
  721. void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
  722. /** Creates a table of pre-computed multiples of the generator to
  723. * accelerate further EC_KEY operations.
  724. * \param key EC_KEY object
  725. * \param ctx BN_CTX object (optional)
  726. * \return 1 on success and 0 if an error occurred.
  727. */
  728. int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
  729. /** Creates a new ec private (and optional a new public) key.
  730. * \param key EC_KEY object
  731. * \return 1 on success and 0 if an error occurred.
  732. */
  733. int EC_KEY_generate_key(EC_KEY *key);
  734. /** Verifies that a private and/or public key is valid.
  735. * \param key the EC_KEY object
  736. * \return 1 on success and 0 otherwise.
  737. */
  738. int EC_KEY_check_key(const EC_KEY *key);
  739. /** Indicates if an EC_KEY can be used for signing.
  740. * \param eckey the EC_KEY object
  741. * \return 1 if can can sign and 0 otherwise.
  742. */
  743. int EC_KEY_can_sign(const EC_KEY *eckey);
  744. /** Sets a public key from affine coordinates performing
  745. * necessary NIST PKV tests.
  746. * \param key the EC_KEY object
  747. * \param x public key x coordinate
  748. * \param y public key y coordinate
  749. * \return 1 on success and 0 otherwise.
  750. */
  751. int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key, BIGNUM *x,
  752. BIGNUM *y);
  753. /** Encodes an EC_KEY public key to an allocated octet string
  754. * \param key key to encode
  755. * \param form point conversion form
  756. * \param pbuf returns pointer to allocated buffer
  757. * \param ctx BN_CTX object (optional)
  758. * \return the length of the encoded octet string or 0 if an error occurred
  759. */
  760. size_t EC_KEY_key2buf(const EC_KEY *key, point_conversion_form_t form,
  761. unsigned char **pbuf, BN_CTX *ctx);
  762. /** Decodes a EC_KEY public key from a octet string
  763. * \param key key to decode
  764. * \param buf memory buffer with the encoded ec point
  765. * \param len length of the encoded ec point
  766. * \param ctx BN_CTX object (optional)
  767. * \return 1 on success and 0 if an error occurred
  768. */
  769. int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf, size_t len,
  770. BN_CTX *ctx);
  771. /** Decodes an EC_KEY private key from an octet string
  772. * \param key key to decode
  773. * \param buf memory buffer with the encoded private key
  774. * \param len length of the encoded key
  775. * \return 1 on success and 0 if an error occurred
  776. */
  777. int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf, size_t len);
  778. /** Encodes a EC_KEY private key to an octet string
  779. * \param key key to encode
  780. * \param buf memory buffer for the result. If NULL the function returns
  781. * required buffer size.
  782. * \param len length of the memory buffer
  783. * \return the length of the encoded octet string or 0 if an error occurred
  784. */
  785. size_t EC_KEY_priv2oct(const EC_KEY *key, unsigned char *buf, size_t len);
  786. /** Encodes an EC_KEY private key to an allocated octet string
  787. * \param eckey key to encode
  788. * \param pbuf returns pointer to allocated buffer
  789. * \return the length of the encoded octet string or 0 if an error occurred
  790. */
  791. size_t EC_KEY_priv2buf(const EC_KEY *eckey, unsigned char **pbuf);
  792. /********************************************************************/
  793. /* de- and encoding functions for SEC1 ECPrivateKey */
  794. /********************************************************************/
  795. /** Decodes a private key from a memory buffer.
  796. * \param key a pointer to a EC_KEY object which should be used (or NULL)
  797. * \param in pointer to memory with the DER encoded private key
  798. * \param len length of the DER encoded private key
  799. * \return the decoded private key or NULL if an error occurred.
  800. */
  801. EC_KEY *d2i_ECPrivateKey(EC_KEY **key, const unsigned char **in, long len);
  802. /** Encodes a private key object and stores the result in a buffer.
  803. * \param key the EC_KEY object to encode
  804. * \param out the buffer for the result (if NULL the function returns number
  805. * of bytes needed).
  806. * \return 1 on success and 0 if an error occurred.
  807. */
  808. int i2d_ECPrivateKey(EC_KEY *key, unsigned char **out);
  809. /********************************************************************/
  810. /* de- and encoding functions for EC parameters */
  811. /********************************************************************/
  812. /** Decodes ec parameter from a memory buffer.
  813. * \param key a pointer to a EC_KEY object which should be used (or NULL)
  814. * \param in pointer to memory with the DER encoded ec parameters
  815. * \param len length of the DER encoded ec parameters
  816. * \return a EC_KEY object with the decoded parameters or NULL if an error
  817. * occurred.
  818. */
  819. EC_KEY *d2i_ECParameters(EC_KEY **key, const unsigned char **in, long len);
  820. /** Encodes ec parameter and stores the result in a buffer.
  821. * \param key the EC_KEY object with ec parameters to encode
  822. * \param out the buffer for the result (if NULL the function returns number
  823. * of bytes needed).
  824. * \return 1 on success and 0 if an error occurred.
  825. */
  826. int i2d_ECParameters(EC_KEY *key, unsigned char **out);
  827. /********************************************************************/
  828. /* de- and encoding functions for EC public key */
  829. /* (octet string, not DER -- hence 'o2i' and 'i2o') */
  830. /********************************************************************/
  831. /** Decodes a ec public key from a octet string.
  832. * \param key a pointer to a EC_KEY object which should be used
  833. * \param in memory buffer with the encoded public key
  834. * \param len length of the encoded public key
  835. * \return EC_KEY object with decoded public key or NULL if an error
  836. * occurred.
  837. */
  838. EC_KEY *o2i_ECPublicKey(EC_KEY **key, const unsigned char **in, long len);
  839. /** Encodes a ec public key in an octet string.
  840. * \param key the EC_KEY object with the public key
  841. * \param out the buffer for the result (if NULL the function returns number
  842. * of bytes needed).
  843. * \return 1 on success and 0 if an error occurred
  844. */
  845. int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out);
  846. /** Prints out the ec parameters on human readable form.
  847. * \param bp BIO object to which the information is printed
  848. * \param key EC_KEY object
  849. * \return 1 on success and 0 if an error occurred
  850. */
  851. int ECParameters_print(BIO *bp, const EC_KEY *key);
  852. /** Prints out the contents of a EC_KEY object
  853. * \param bp BIO object to which the information is printed
  854. * \param key EC_KEY object
  855. * \param off line offset
  856. * \return 1 on success and 0 if an error occurred
  857. */
  858. int EC_KEY_print(BIO *bp, const EC_KEY *key, int off);
  859. # ifndef OPENSSL_NO_STDIO
  860. /** Prints out the ec parameters on human readable form.
  861. * \param fp file descriptor to which the information is printed
  862. * \param key EC_KEY object
  863. * \return 1 on success and 0 if an error occurred
  864. */
  865. int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
  866. /** Prints out the contents of a EC_KEY object
  867. * \param fp file descriptor to which the information is printed
  868. * \param key EC_KEY object
  869. * \param off line offset
  870. * \return 1 on success and 0 if an error occurred
  871. */
  872. int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
  873. # endif
  874. const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
  875. const EC_KEY_METHOD *EC_KEY_get_default_method(void);
  876. void EC_KEY_set_default_method(const EC_KEY_METHOD *meth);
  877. const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
  878. int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
  879. EC_KEY *EC_KEY_new_method(ENGINE *engine);
  880. int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
  881. const unsigned char *Z, size_t Zlen,
  882. const unsigned char *sinfo, size_t sinfolen,
  883. const EVP_MD *md);
  884. int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key,
  885. const EC_KEY *ecdh,
  886. void *(*KDF) (const void *in, size_t inlen,
  887. void *out, size_t *outlen));
  888. typedef struct ECDSA_SIG_st ECDSA_SIG;
  889. /** Allocates and initialize a ECDSA_SIG structure
  890. * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
  891. */
  892. ECDSA_SIG *ECDSA_SIG_new(void);
  893. /** frees a ECDSA_SIG structure
  894. * \param sig pointer to the ECDSA_SIG structure
  895. */
  896. void ECDSA_SIG_free(ECDSA_SIG *sig);
  897. /** DER encode content of ECDSA_SIG object (note: this function modifies *pp
  898. * (*pp += length of the DER encoded signature)).
  899. * \param sig pointer to the ECDSA_SIG object
  900. * \param pp pointer to a unsigned char pointer for the output or NULL
  901. * \return the length of the DER encoded ECDSA_SIG object or 0
  902. */
  903. int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **pp);
  904. /** Decodes a DER encoded ECDSA signature (note: this function changes *pp
  905. * (*pp += len)).
  906. * \param sig pointer to ECDSA_SIG pointer (may be NULL)
  907. * \param pp memory buffer with the DER encoded signature
  908. * \param len length of the buffer
  909. * \return pointer to the decoded ECDSA_SIG structure (or NULL)
  910. */
  911. ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **sig, const unsigned char **pp, long len);
  912. /** Accessor for r and s fields of ECDSA_SIG
  913. * \param sig pointer to ECDSA_SIG pointer
  914. * \param pr pointer to BIGNUM pointer for r (may be NULL)
  915. * \param ps pointer to BIGNUM pointer for s (may be NULL)
  916. */
  917. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  918. /** Setter for r and s fields of ECDSA_SIG
  919. * \param sig pointer to ECDSA_SIG pointer
  920. * \param r pointer to BIGNUM for r (may be NULL)
  921. * \param s pointer to BIGNUM for s (may be NULL)
  922. */
  923. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  924. /** Computes the ECDSA signature of the given hash value using
  925. * the supplied private key and returns the created signature.
  926. * \param dgst pointer to the hash value
  927. * \param dgst_len length of the hash value
  928. * \param eckey EC_KEY object containing a private EC key
  929. * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
  930. */
  931. ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst, int dgst_len,
  932. EC_KEY *eckey);
  933. /** Computes ECDSA signature of a given hash value using the supplied
  934. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  935. * \param dgst pointer to the hash value to sign
  936. * \param dgstlen length of the hash value
  937. * \param kinv BIGNUM with a pre-computed inverse k (optional)
  938. * \param rp BIGNUM with a pre-computed rp value (optional),
  939. * see ECDSA_sign_setup
  940. * \param eckey EC_KEY object containing a private EC key
  941. * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
  942. */
  943. ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst, int dgstlen,
  944. const BIGNUM *kinv, const BIGNUM *rp,
  945. EC_KEY *eckey);
  946. /** Verifies that the supplied signature is a valid ECDSA
  947. * signature of the supplied hash value using the supplied public key.
  948. * \param dgst pointer to the hash value
  949. * \param dgst_len length of the hash value
  950. * \param sig ECDSA_SIG structure
  951. * \param eckey EC_KEY object containing a public EC key
  952. * \return 1 if the signature is valid, 0 if the signature is invalid
  953. * and -1 on error
  954. */
  955. int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
  956. const ECDSA_SIG *sig, EC_KEY *eckey);
  957. /** Precompute parts of the signing operation
  958. * \param eckey EC_KEY object containing a private EC key
  959. * \param ctx BN_CTX object (optional)
  960. * \param kinv BIGNUM pointer for the inverse of k
  961. * \param rp BIGNUM pointer for x coordinate of k * generator
  962. * \return 1 on success and 0 otherwise
  963. */
  964. int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp);
  965. /** Computes ECDSA signature of a given hash value using the supplied
  966. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  967. * \param type this parameter is ignored
  968. * \param dgst pointer to the hash value to sign
  969. * \param dgstlen length of the hash value
  970. * \param sig memory for the DER encoded created signature
  971. * \param siglen pointer to the length of the returned signature
  972. * \param eckey EC_KEY object containing a private EC key
  973. * \return 1 on success and 0 otherwise
  974. */
  975. int ECDSA_sign(int type, const unsigned char *dgst, int dgstlen,
  976. unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
  977. /** Computes ECDSA signature of a given hash value using the supplied
  978. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  979. * \param type this parameter is ignored
  980. * \param dgst pointer to the hash value to sign
  981. * \param dgstlen length of the hash value
  982. * \param sig buffer to hold the DER encoded signature
  983. * \param siglen pointer to the length of the returned signature
  984. * \param kinv BIGNUM with a pre-computed inverse k (optional)
  985. * \param rp BIGNUM with a pre-computed rp value (optional),
  986. * see ECDSA_sign_setup
  987. * \param eckey EC_KEY object containing a private EC key
  988. * \return 1 on success and 0 otherwise
  989. */
  990. int ECDSA_sign_ex(int type, const unsigned char *dgst, int dgstlen,
  991. unsigned char *sig, unsigned int *siglen,
  992. const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey);
  993. /** Verifies that the given signature is valid ECDSA signature
  994. * of the supplied hash value using the specified public key.
  995. * \param type this parameter is ignored
  996. * \param dgst pointer to the hash value
  997. * \param dgstlen length of the hash value
  998. * \param sig pointer to the DER encoded signature
  999. * \param siglen length of the DER encoded signature
  1000. * \param eckey EC_KEY object containing a public EC key
  1001. * \return 1 if the signature is valid, 0 if the signature is invalid
  1002. * and -1 on error
  1003. */
  1004. int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen,
  1005. const unsigned char *sig, int siglen, EC_KEY *eckey);
  1006. /** Returns the maximum length of the DER encoded signature
  1007. * \param eckey EC_KEY object
  1008. * \return numbers of bytes required for the DER encoded signature
  1009. */
  1010. int ECDSA_size(const EC_KEY *eckey);
  1011. /********************************************************************/
  1012. /* EC_KEY_METHOD constructors, destructors, writers and accessors */
  1013. /********************************************************************/
  1014. EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth);
  1015. void EC_KEY_METHOD_free(EC_KEY_METHOD *meth);
  1016. void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
  1017. int (*init)(EC_KEY *key),
  1018. void (*finish)(EC_KEY *key),
  1019. int (*copy)(EC_KEY *dest, const EC_KEY *src),
  1020. int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
  1021. int (*set_private)(EC_KEY *key,
  1022. const BIGNUM *priv_key),
  1023. int (*set_public)(EC_KEY *key,
  1024. const EC_POINT *pub_key));
  1025. void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
  1026. int (*keygen)(EC_KEY *key));
  1027. void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
  1028. int (*ckey)(unsigned char **psec,
  1029. size_t *pseclen,
  1030. const EC_POINT *pub_key,
  1031. const EC_KEY *ecdh));
  1032. void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
  1033. int (*sign)(int type, const unsigned char *dgst,
  1034. int dlen, unsigned char *sig,
  1035. unsigned int *siglen,
  1036. const BIGNUM *kinv, const BIGNUM *r,
  1037. EC_KEY *eckey),
  1038. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  1039. BIGNUM **kinvp, BIGNUM **rp),
  1040. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
  1041. int dgst_len,
  1042. const BIGNUM *in_kinv,
  1043. const BIGNUM *in_r,
  1044. EC_KEY *eckey));
  1045. void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
  1046. int (*verify)(int type, const unsigned
  1047. char *dgst, int dgst_len,
  1048. const unsigned char *sigbuf,
  1049. int sig_len, EC_KEY *eckey),
  1050. int (*verify_sig)(const unsigned char *dgst,
  1051. int dgst_len,
  1052. const ECDSA_SIG *sig,
  1053. EC_KEY *eckey));
  1054. void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
  1055. int (**pinit)(EC_KEY *key),
  1056. void (**pfinish)(EC_KEY *key),
  1057. int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
  1058. int (**pset_group)(EC_KEY *key,
  1059. const EC_GROUP *grp),
  1060. int (**pset_private)(EC_KEY *key,
  1061. const BIGNUM *priv_key),
  1062. int (**pset_public)(EC_KEY *key,
  1063. const EC_POINT *pub_key));
  1064. void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth,
  1065. int (**pkeygen)(EC_KEY *key));
  1066. void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
  1067. int (**pck)(unsigned char **psec,
  1068. size_t *pseclen,
  1069. const EC_POINT *pub_key,
  1070. const EC_KEY *ecdh));
  1071. void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
  1072. int (**psign)(int type, const unsigned char *dgst,
  1073. int dlen, unsigned char *sig,
  1074. unsigned int *siglen,
  1075. const BIGNUM *kinv, const BIGNUM *r,
  1076. EC_KEY *eckey),
  1077. int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  1078. BIGNUM **kinvp, BIGNUM **rp),
  1079. ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
  1080. int dgst_len,
  1081. const BIGNUM *in_kinv,
  1082. const BIGNUM *in_r,
  1083. EC_KEY *eckey));
  1084. void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
  1085. int (**pverify)(int type, const unsigned
  1086. char *dgst, int dgst_len,
  1087. const unsigned char *sigbuf,
  1088. int sig_len, EC_KEY *eckey),
  1089. int (**pverify_sig)(const unsigned char *dgst,
  1090. int dgst_len,
  1091. const ECDSA_SIG *sig,
  1092. EC_KEY *eckey));
  1093. # define ECParameters_dup(x) ASN1_dup_of(EC_KEY,i2d_ECParameters,d2i_ECParameters,x)
  1094. # ifndef __cplusplus
  1095. # if defined(__SUNPRO_C)
  1096. # if __SUNPRO_C >= 0x520
  1097. # pragma error_messages (default,E_ARRAY_OF_INCOMPLETE_NONAME,E_ARRAY_OF_INCOMPLETE)
  1098. # endif
  1099. # endif
  1100. # endif
  1101. # define EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid) \
  1102. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1103. EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
  1104. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, nid, NULL)
  1105. # define EVP_PKEY_CTX_set_ec_param_enc(ctx, flag) \
  1106. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1107. EVP_PKEY_OP_PARAMGEN|EVP_PKEY_OP_KEYGEN, \
  1108. EVP_PKEY_CTRL_EC_PARAM_ENC, flag, NULL)
  1109. # define EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, flag) \
  1110. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1111. EVP_PKEY_OP_DERIVE, \
  1112. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, flag, NULL)
  1113. # define EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx) \
  1114. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1115. EVP_PKEY_OP_DERIVE, \
  1116. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, -2, NULL)
  1117. # define EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, kdf) \
  1118. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1119. EVP_PKEY_OP_DERIVE, \
  1120. EVP_PKEY_CTRL_EC_KDF_TYPE, kdf, NULL)
  1121. # define EVP_PKEY_CTX_get_ecdh_kdf_type(ctx) \
  1122. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1123. EVP_PKEY_OP_DERIVE, \
  1124. EVP_PKEY_CTRL_EC_KDF_TYPE, -2, NULL)
  1125. # define EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, md) \
  1126. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1127. EVP_PKEY_OP_DERIVE, \
  1128. EVP_PKEY_CTRL_EC_KDF_MD, 0, (void *)(md))
  1129. # define EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, pmd) \
  1130. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1131. EVP_PKEY_OP_DERIVE, \
  1132. EVP_PKEY_CTRL_GET_EC_KDF_MD, 0, (void *)(pmd))
  1133. # define EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, len) \
  1134. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1135. EVP_PKEY_OP_DERIVE, \
  1136. EVP_PKEY_CTRL_EC_KDF_OUTLEN, len, NULL)
  1137. # define EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, plen) \
  1138. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1139. EVP_PKEY_OP_DERIVE, \
  1140. EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, 0, \
  1141. (void *)(plen))
  1142. # define EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p, plen) \
  1143. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1144. EVP_PKEY_OP_DERIVE, \
  1145. EVP_PKEY_CTRL_EC_KDF_UKM, plen, (void *)(p))
  1146. # define EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p) \
  1147. EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_EC, \
  1148. EVP_PKEY_OP_DERIVE, \
  1149. EVP_PKEY_CTRL_GET_EC_KDF_UKM, 0, (void *)(p))
  1150. # define EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID (EVP_PKEY_ALG_CTRL + 1)
  1151. # define EVP_PKEY_CTRL_EC_PARAM_ENC (EVP_PKEY_ALG_CTRL + 2)
  1152. # define EVP_PKEY_CTRL_EC_ECDH_COFACTOR (EVP_PKEY_ALG_CTRL + 3)
  1153. # define EVP_PKEY_CTRL_EC_KDF_TYPE (EVP_PKEY_ALG_CTRL + 4)
  1154. # define EVP_PKEY_CTRL_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 5)
  1155. # define EVP_PKEY_CTRL_GET_EC_KDF_MD (EVP_PKEY_ALG_CTRL + 6)
  1156. # define EVP_PKEY_CTRL_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 7)
  1157. # define EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN (EVP_PKEY_ALG_CTRL + 8)
  1158. # define EVP_PKEY_CTRL_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 9)
  1159. # define EVP_PKEY_CTRL_GET_EC_KDF_UKM (EVP_PKEY_ALG_CTRL + 10)
  1160. /* KDF types */
  1161. # define EVP_PKEY_ECDH_KDF_NONE 1
  1162. # define EVP_PKEY_ECDH_KDF_X9_62 2
  1163. # ifdef __cplusplus
  1164. }
  1165. # endif
  1166. # endif
  1167. #endif