ec.h 64 KB

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