ec.h 67 KB

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