ec.h 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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(const EC_GROUP *group, EC_POINT *p,
  530. const BIGNUM *x, const BIGNUM *y, const BIGNUM *z,
  531. BN_CTX *ctx);
  532. /** Gets the jacobian projective coordinates of a EC_POINT over GFp
  533. * \param group underlying EC_GROUP object
  534. * \param p EC_POINT object
  535. * \param x BIGNUM for the x-coordinate
  536. * \param y BIGNUM for the y-coordinate
  537. * \param z BIGNUM for the z-coordinate
  538. * \param ctx BN_CTX object (optional)
  539. * \return 1 on success and 0 if an error occurred
  540. */
  541. OSSL_DEPRECATEDIN_3_0 int EC_POINT_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *p,
  542. BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx);
  543. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  544. /** Sets the affine coordinates of an EC_POINT
  545. * \param group underlying EC_GROUP object
  546. * \param p EC_POINT object
  547. * \param x BIGNUM with the x-coordinate
  548. * \param y BIGNUM with the y-coordinate
  549. * \param ctx BN_CTX object (optional)
  550. * \return 1 on success and 0 if an error occurred
  551. */
  552. int EC_POINT_set_affine_coordinates(const EC_GROUP *group, EC_POINT *p,
  553. const BIGNUM *x, const BIGNUM *y,
  554. BN_CTX *ctx);
  555. /** Gets the affine coordinates of an EC_POINT.
  556. * \param group underlying EC_GROUP object
  557. * \param p EC_POINT object
  558. * \param x BIGNUM for the x-coordinate
  559. * \param y BIGNUM for the y-coordinate
  560. * \param ctx BN_CTX object (optional)
  561. * \return 1 on success and 0 if an error occurred
  562. */
  563. int EC_POINT_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *p,
  564. BIGNUM *x, BIGNUM *y, BN_CTX *ctx);
  565. #ifndef OPENSSL_NO_DEPRECATED_3_0
  566. /** Sets the affine coordinates of an EC_POINT. A synonym of
  567. * EC_POINT_set_affine_coordinates
  568. * \param group underlying EC_GROUP object
  569. * \param p EC_POINT object
  570. * \param x BIGNUM with the x-coordinate
  571. * \param y BIGNUM with the y-coordinate
  572. * \param ctx BN_CTX object (optional)
  573. * \return 1 on success and 0 if an error occurred
  574. */
  575. OSSL_DEPRECATEDIN_3_0 int EC_POINT_set_affine_coordinates_GFp(const EC_GROUP *group, EC_POINT *p,
  576. const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx);
  577. /** Gets the affine coordinates of an EC_POINT. A synonym of
  578. * EC_POINT_get_affine_coordinates
  579. * \param group underlying EC_GROUP object
  580. * \param p EC_POINT object
  581. * \param x BIGNUM for the x-coordinate
  582. * \param y BIGNUM for the y-coordinate
  583. * \param ctx BN_CTX object (optional)
  584. * \return 1 on success and 0 if an error occurred
  585. */
  586. OSSL_DEPRECATEDIN_3_0 int EC_POINT_get_affine_coordinates_GFp(const EC_GROUP *group, const EC_POINT *p,
  587. BIGNUM *x, BIGNUM *y, BN_CTX *ctx);
  588. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  589. /** Sets the x9.62 compressed coordinates of a EC_POINT
  590. * \param group underlying EC_GROUP object
  591. * \param p EC_POINT object
  592. * \param x BIGNUM with x-coordinate
  593. * \param y_bit integer with the y-Bit (either 0 or 1)
  594. * \param ctx BN_CTX object (optional)
  595. * \return 1 on success and 0 if an error occurred
  596. */
  597. int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, EC_POINT *p,
  598. const BIGNUM *x, int y_bit,
  599. BN_CTX *ctx);
  600. #ifndef OPENSSL_NO_DEPRECATED_3_0
  601. /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
  602. * EC_POINT_set_compressed_coordinates
  603. * \param group underlying EC_GROUP object
  604. * \param p EC_POINT object
  605. * \param x BIGNUM with x-coordinate
  606. * \param y_bit integer with the y-Bit (either 0 or 1)
  607. * \param ctx BN_CTX object (optional)
  608. * \return 1 on success and 0 if an error occurred
  609. */
  610. OSSL_DEPRECATEDIN_3_0 int EC_POINT_set_compressed_coordinates_GFp(const EC_GROUP *group, EC_POINT *p,
  611. const BIGNUM *x, int y_bit, BN_CTX *ctx);
  612. #ifndef OPENSSL_NO_EC2M
  613. /** Sets the affine coordinates of an EC_POINT. A synonym of
  614. * EC_POINT_set_affine_coordinates
  615. * \param group underlying EC_GROUP object
  616. * \param p EC_POINT object
  617. * \param x BIGNUM with the x-coordinate
  618. * \param y BIGNUM with the y-coordinate
  619. * \param ctx BN_CTX object (optional)
  620. * \return 1 on success and 0 if an error occurred
  621. */
  622. OSSL_DEPRECATEDIN_3_0 int EC_POINT_set_affine_coordinates_GF2m(const EC_GROUP *group, EC_POINT *p,
  623. const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx);
  624. /** Gets the affine coordinates of an EC_POINT. A synonym of
  625. * EC_POINT_get_affine_coordinates
  626. * \param group underlying EC_GROUP object
  627. * \param p EC_POINT object
  628. * \param x BIGNUM for the x-coordinate
  629. * \param y BIGNUM for the y-coordinate
  630. * \param ctx BN_CTX object (optional)
  631. * \return 1 on success and 0 if an error occurred
  632. */
  633. OSSL_DEPRECATEDIN_3_0 int EC_POINT_get_affine_coordinates_GF2m(const EC_GROUP *group, const EC_POINT *p,
  634. BIGNUM *x, BIGNUM *y, BN_CTX *ctx);
  635. /** Sets the x9.62 compressed coordinates of a EC_POINT. A synonym of
  636. * EC_POINT_set_compressed_coordinates
  637. * \param group underlying EC_GROUP object
  638. * \param p EC_POINT object
  639. * \param x BIGNUM with x-coordinate
  640. * \param y_bit integer with the y-Bit (either 0 or 1)
  641. * \param ctx BN_CTX object (optional)
  642. * \return 1 on success and 0 if an error occurred
  643. */
  644. OSSL_DEPRECATEDIN_3_0 int EC_POINT_set_compressed_coordinates_GF2m(const EC_GROUP *group, EC_POINT *p,
  645. const BIGNUM *x, int y_bit, BN_CTX *ctx);
  646. #endif
  647. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  648. /** Encodes a EC_POINT object to a octet string
  649. * \param group underlying EC_GROUP object
  650. * \param p EC_POINT object
  651. * \param form point conversion form
  652. * \param buf memory buffer for the result. If NULL the function returns
  653. * required buffer size.
  654. * \param len length of the memory buffer
  655. * \param ctx BN_CTX object (optional)
  656. * \return the length of the encoded octet string or 0 if an error occurred
  657. */
  658. size_t EC_POINT_point2oct(const EC_GROUP *group, const EC_POINT *p,
  659. point_conversion_form_t form,
  660. unsigned char *buf, size_t len, BN_CTX *ctx);
  661. /** Decodes a EC_POINT from a octet string
  662. * \param group underlying EC_GROUP object
  663. * \param p EC_POINT object
  664. * \param buf memory buffer with the encoded ec point
  665. * \param len length of the encoded ec point
  666. * \param ctx BN_CTX object (optional)
  667. * \return 1 on success and 0 if an error occurred
  668. */
  669. int EC_POINT_oct2point(const EC_GROUP *group, EC_POINT *p,
  670. const unsigned char *buf, size_t len, BN_CTX *ctx);
  671. /** Encodes an EC_POINT object to an allocated octet string
  672. * \param group underlying EC_GROUP object
  673. * \param point EC_POINT object
  674. * \param form point conversion form
  675. * \param pbuf returns pointer to allocated buffer
  676. * \param ctx BN_CTX object (optional)
  677. * \return the length of the encoded octet string or 0 if an error occurred
  678. */
  679. size_t EC_POINT_point2buf(const EC_GROUP *group, const EC_POINT *point,
  680. point_conversion_form_t form,
  681. unsigned char **pbuf, BN_CTX *ctx);
  682. /* other interfaces to point2oct/oct2point: */
  683. #ifndef OPENSSL_NO_DEPRECATED_3_0
  684. OSSL_DEPRECATEDIN_3_0 BIGNUM *EC_POINT_point2bn(const EC_GROUP *,
  685. const EC_POINT *,
  686. point_conversion_form_t form,
  687. BIGNUM *, BN_CTX *);
  688. OSSL_DEPRECATEDIN_3_0 EC_POINT *EC_POINT_bn2point(const EC_GROUP *,
  689. const BIGNUM *,
  690. EC_POINT *, BN_CTX *);
  691. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  692. char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *,
  693. point_conversion_form_t form, BN_CTX *);
  694. EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *,
  695. EC_POINT *, BN_CTX *);
  696. /********************************************************************/
  697. /* functions for doing EC_POINT arithmetic */
  698. /********************************************************************/
  699. /** Computes the sum of two EC_POINT
  700. * \param group underlying EC_GROUP object
  701. * \param r EC_POINT object for the result (r = a + b)
  702. * \param a EC_POINT object with the first summand
  703. * \param b EC_POINT object with the second summand
  704. * \param ctx BN_CTX object (optional)
  705. * \return 1 on success and 0 if an error occurred
  706. */
  707. int EC_POINT_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  708. const EC_POINT *b, BN_CTX *ctx);
  709. /** Computes the double of a EC_POINT
  710. * \param group underlying EC_GROUP object
  711. * \param r EC_POINT object for the result (r = 2 * a)
  712. * \param a EC_POINT object
  713. * \param ctx BN_CTX object (optional)
  714. * \return 1 on success and 0 if an error occurred
  715. */
  716. int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a,
  717. BN_CTX *ctx);
  718. /** Computes the inverse of a EC_POINT
  719. * \param group underlying EC_GROUP object
  720. * \param a EC_POINT object to be inverted (it's used for the result as well)
  721. * \param ctx BN_CTX object (optional)
  722. * \return 1 on success and 0 if an error occurred
  723. */
  724. int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx);
  725. /** Checks whether the point is the neutral element of the group
  726. * \param group the underlying EC_GROUP object
  727. * \param p EC_POINT object
  728. * \return 1 if the point is the neutral element and 0 otherwise
  729. */
  730. int EC_POINT_is_at_infinity(const EC_GROUP *group, const EC_POINT *p);
  731. /** Checks whether the point is on the curve
  732. * \param group underlying EC_GROUP object
  733. * \param point EC_POINT object to check
  734. * \param ctx BN_CTX object (optional)
  735. * \return 1 if the point is on the curve, 0 if not, or -1 on error
  736. */
  737. int EC_POINT_is_on_curve(const EC_GROUP *group, const EC_POINT *point,
  738. BN_CTX *ctx);
  739. /** Compares two EC_POINTs
  740. * \param group underlying EC_GROUP object
  741. * \param a first EC_POINT object
  742. * \param b second EC_POINT object
  743. * \param ctx BN_CTX object (optional)
  744. * \return 1 if the points are not equal, 0 if they are, or -1 on error
  745. */
  746. int EC_POINT_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b,
  747. BN_CTX *ctx);
  748. #ifndef OPENSSL_NO_DEPRECATED_3_0
  749. OSSL_DEPRECATEDIN_3_0 int EC_POINT_make_affine(const EC_GROUP *group,
  750. EC_POINT *point, BN_CTX *ctx);
  751. OSSL_DEPRECATEDIN_3_0 int EC_POINTs_make_affine(const EC_GROUP *group, size_t num,
  752. EC_POINT *points[], BN_CTX *ctx);
  753. /** Computes r = generator * n + sum_{i=0}^{num-1} p[i] * m[i]
  754. * \param group underlying EC_GROUP object
  755. * \param r EC_POINT object for the result
  756. * \param n BIGNUM with the multiplier for the group generator (optional)
  757. * \param num number further summands
  758. * \param p array of size num of EC_POINT objects
  759. * \param m array of size num of BIGNUM objects
  760. * \param ctx BN_CTX object (optional)
  761. * \return 1 on success and 0 if an error occurred
  762. */
  763. OSSL_DEPRECATEDIN_3_0 int EC_POINTs_mul(const EC_GROUP *group, EC_POINT *r,
  764. const BIGNUM *n, size_t num,
  765. const EC_POINT *p[], const BIGNUM *m[],
  766. BN_CTX *ctx);
  767. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  768. /** Computes r = generator * n + q * m
  769. * \param group underlying EC_GROUP object
  770. * \param r EC_POINT object for the result
  771. * \param n BIGNUM with the multiplier for the group generator (optional)
  772. * \param q EC_POINT object with the first factor of the second summand
  773. * \param m BIGNUM with the second factor of the second summand
  774. * \param ctx BN_CTX object (optional)
  775. * \return 1 on success and 0 if an error occurred
  776. */
  777. int EC_POINT_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
  778. const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
  779. #ifndef OPENSSL_NO_DEPRECATED_3_0
  780. /** Stores multiples of generator for faster point multiplication
  781. * \param group EC_GROUP object
  782. * \param ctx BN_CTX object (optional)
  783. * \return 1 on success and 0 if an error occurred
  784. */
  785. OSSL_DEPRECATEDIN_3_0 int EC_GROUP_precompute_mult(EC_GROUP *group, BN_CTX *ctx);
  786. /** Reports whether a precomputation has been done
  787. * \param group EC_GROUP object
  788. * \return 1 if a pre-computation has been done and 0 otherwise
  789. */
  790. OSSL_DEPRECATEDIN_3_0 int EC_GROUP_have_precompute_mult(const EC_GROUP *group);
  791. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  792. /********************************************************************/
  793. /* ASN1 stuff */
  794. /********************************************************************/
  795. DECLARE_ASN1_ITEM(ECPKPARAMETERS)
  796. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPKPARAMETERS)
  797. DECLARE_ASN1_ITEM(ECPARAMETERS)
  798. DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS)
  799. /*
  800. * EC_GROUP_get_basis_type() returns the NID of the basis type used to
  801. * represent the field elements
  802. */
  803. int EC_GROUP_get_basis_type(const EC_GROUP *);
  804. #ifndef OPENSSL_NO_EC2M
  805. int EC_GROUP_get_trinomial_basis(const EC_GROUP *, unsigned int *k);
  806. int EC_GROUP_get_pentanomial_basis(const EC_GROUP *, unsigned int *k1,
  807. unsigned int *k2, unsigned int *k3);
  808. #endif
  809. EC_GROUP *d2i_ECPKParameters(EC_GROUP **, const unsigned char **in, long len);
  810. int i2d_ECPKParameters(const EC_GROUP *, unsigned char **out);
  811. #define d2i_ECPKParameters_bio(bp, x) \
  812. ASN1_d2i_bio_of(EC_GROUP, NULL, d2i_ECPKParameters, bp, x)
  813. #define i2d_ECPKParameters_bio(bp, x) \
  814. ASN1_i2d_bio_of(EC_GROUP, i2d_ECPKParameters, bp, x)
  815. #define d2i_ECPKParameters_fp(fp, x) \
  816. (EC_GROUP *)ASN1_d2i_fp(NULL, (d2i_of_void *)d2i_ECPKParameters, (fp), \
  817. (void **)(x))
  818. #define i2d_ECPKParameters_fp(fp, x) \
  819. ASN1_i2d_fp((i2d_of_void *)i2d_ECPKParameters, (fp), (void *)(x))
  820. #ifndef OPENSSL_NO_DEPRECATED_3_0
  821. OSSL_DEPRECATEDIN_3_0 int ECPKParameters_print(BIO *bp, const EC_GROUP *x,
  822. int off);
  823. #ifndef OPENSSL_NO_STDIO
  824. OSSL_DEPRECATEDIN_3_0 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x,
  825. int off);
  826. #endif
  827. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  828. /********************************************************************/
  829. /* EC_KEY functions */
  830. /********************************************************************/
  831. /* some values for the encoding_flag */
  832. #define EC_PKEY_NO_PARAMETERS 0x001
  833. #define EC_PKEY_NO_PUBKEY 0x002
  834. /* some values for the flags field */
  835. #define EC_FLAG_SM2_RANGE 0x0004
  836. #define EC_FLAG_COFACTOR_ECDH 0x1000
  837. #define EC_FLAG_CHECK_NAMED_GROUP 0x2000
  838. #define EC_FLAG_CHECK_NAMED_GROUP_NIST 0x4000
  839. #define EC_FLAG_CHECK_NAMED_GROUP_MASK \
  840. (EC_FLAG_CHECK_NAMED_GROUP | EC_FLAG_CHECK_NAMED_GROUP_NIST)
  841. /* Deprecated flags - it was using 0x01..0x02 */
  842. #define EC_FLAG_NON_FIPS_ALLOW 0x0000
  843. #define EC_FLAG_FIPS_CHECKED 0x0000
  844. #ifndef OPENSSL_NO_DEPRECATED_3_0
  845. /**
  846. * Creates a new EC_KEY object.
  847. * \param ctx The library context for to use for this EC_KEY. May be NULL in
  848. * which case the default library context is used.
  849. * \return EC_KEY object or NULL if an error occurred.
  850. */
  851. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_new_ex(OSSL_LIB_CTX *ctx, const char *propq);
  852. /**
  853. * Creates a new EC_KEY object. Same as calling EC_KEY_new_ex with a
  854. * NULL library context
  855. * \return EC_KEY object or NULL if an error occurred.
  856. */
  857. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_new(void);
  858. OSSL_DEPRECATEDIN_3_0 int EC_KEY_get_flags(const EC_KEY *key);
  859. OSSL_DEPRECATEDIN_3_0 void EC_KEY_set_flags(EC_KEY *key, int flags);
  860. OSSL_DEPRECATEDIN_3_0 void EC_KEY_clear_flags(EC_KEY *key, int flags);
  861. OSSL_DEPRECATEDIN_3_0 int EC_KEY_decoded_from_explicit_params(const EC_KEY *key);
  862. /**
  863. * Creates a new EC_KEY object using a named curve as underlying
  864. * EC_GROUP object.
  865. * \param ctx The library context for to use for this EC_KEY. May be NULL in
  866. * which case the default library context is used.
  867. * \param propq Any property query string
  868. * \param nid NID of the named curve.
  869. * \return EC_KEY object or NULL if an error occurred.
  870. */
  871. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_new_by_curve_name_ex(OSSL_LIB_CTX *ctx,
  872. const char *propq,
  873. int nid);
  874. /**
  875. * Creates a new EC_KEY object using a named curve as underlying
  876. * EC_GROUP object. Same as calling EC_KEY_new_by_curve_name_ex with a NULL
  877. * library context and property query string.
  878. * \param nid NID of the named curve.
  879. * \return EC_KEY object or NULL if an error occurred.
  880. */
  881. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_new_by_curve_name(int nid);
  882. /** Frees a EC_KEY object.
  883. * \param key EC_KEY object to be freed.
  884. */
  885. OSSL_DEPRECATEDIN_3_0 void EC_KEY_free(EC_KEY *key);
  886. /** Copies a EC_KEY object.
  887. * \param dst destination EC_KEY object
  888. * \param src src EC_KEY object
  889. * \return dst or NULL if an error occurred.
  890. */
  891. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_copy(EC_KEY *dst, const EC_KEY *src);
  892. /** Creates a new EC_KEY object and copies the content from src to it.
  893. * \param src the source EC_KEY object
  894. * \return newly created EC_KEY object or NULL if an error occurred.
  895. */
  896. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_dup(const EC_KEY *src);
  897. /** Increases the internal reference count of a EC_KEY object.
  898. * \param key EC_KEY object
  899. * \return 1 on success and 0 if an error occurred.
  900. */
  901. OSSL_DEPRECATEDIN_3_0 int EC_KEY_up_ref(EC_KEY *key);
  902. /** Returns the ENGINE object of a EC_KEY object
  903. * \param eckey EC_KEY object
  904. * \return the ENGINE object (possibly NULL).
  905. */
  906. OSSL_DEPRECATEDIN_3_0 ENGINE *EC_KEY_get0_engine(const EC_KEY *eckey);
  907. /** Returns the EC_GROUP object of a EC_KEY object
  908. * \param key EC_KEY object
  909. * \return the EC_GROUP object (possibly NULL).
  910. */
  911. OSSL_DEPRECATEDIN_3_0 const EC_GROUP *EC_KEY_get0_group(const EC_KEY *key);
  912. /** Sets the EC_GROUP of a EC_KEY object.
  913. * \param key EC_KEY object
  914. * \param group EC_GROUP to use in the EC_KEY object (note: the EC_KEY
  915. * object will use an own copy of the EC_GROUP).
  916. * \return 1 on success and 0 if an error occurred.
  917. */
  918. OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_group(EC_KEY *key, const EC_GROUP *group);
  919. /** Returns the private key of a EC_KEY object.
  920. * \param key EC_KEY object
  921. * \return a BIGNUM with the private key (possibly NULL).
  922. */
  923. OSSL_DEPRECATEDIN_3_0 const BIGNUM *EC_KEY_get0_private_key(const EC_KEY *key);
  924. /** Sets the private key of a EC_KEY object.
  925. * \param key EC_KEY object
  926. * \param prv BIGNUM with the private key (note: the EC_KEY object
  927. * will use an own copy of the BIGNUM).
  928. * \return 1 on success and 0 if an error occurred.
  929. */
  930. OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_private_key(EC_KEY *key, const BIGNUM *prv);
  931. /** Returns the public key of a EC_KEY object.
  932. * \param key the EC_KEY object
  933. * \return a EC_POINT object with the public key (possibly NULL)
  934. */
  935. OSSL_DEPRECATEDIN_3_0 const EC_POINT *EC_KEY_get0_public_key(const EC_KEY *key);
  936. /** Sets the public key of a EC_KEY object.
  937. * \param key EC_KEY object
  938. * \param pub EC_POINT object with the public key (note: the EC_KEY object
  939. * will use an own copy of the EC_POINT object).
  940. * \return 1 on success and 0 if an error occurred.
  941. */
  942. OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_public_key(EC_KEY *key, const EC_POINT *pub);
  943. OSSL_DEPRECATEDIN_3_0 unsigned EC_KEY_get_enc_flags(const EC_KEY *key);
  944. OSSL_DEPRECATEDIN_3_0 void EC_KEY_set_enc_flags(EC_KEY *eckey, unsigned int flags);
  945. OSSL_DEPRECATEDIN_3_0 point_conversion_form_t EC_KEY_get_conv_form(const EC_KEY *key);
  946. OSSL_DEPRECATEDIN_3_0 void EC_KEY_set_conv_form(EC_KEY *eckey,
  947. point_conversion_form_t cform);
  948. #endif /*OPENSSL_NO_DEPRECATED_3_0 */
  949. #define EC_KEY_get_ex_new_index(l, p, newf, dupf, freef) \
  950. CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_EC_KEY, l, p, newf, dupf, freef)
  951. #ifndef OPENSSL_NO_DEPRECATED_3_0
  952. OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_ex_data(EC_KEY *key, int idx, void *arg);
  953. OSSL_DEPRECATEDIN_3_0 void *EC_KEY_get_ex_data(const EC_KEY *key, int idx);
  954. /* wrapper functions for the underlying EC_GROUP object */
  955. OSSL_DEPRECATEDIN_3_0 void EC_KEY_set_asn1_flag(EC_KEY *eckey, int asn1_flag);
  956. /** Creates a table of pre-computed multiples of the generator to
  957. * accelerate further EC_KEY operations.
  958. * \param key EC_KEY object
  959. * \param ctx BN_CTX object (optional)
  960. * \return 1 on success and 0 if an error occurred.
  961. */
  962. OSSL_DEPRECATEDIN_3_0 int EC_KEY_precompute_mult(EC_KEY *key, BN_CTX *ctx);
  963. /** Creates a new ec private (and optional a new public) key.
  964. * \param key EC_KEY object
  965. * \return 1 on success and 0 if an error occurred.
  966. */
  967. OSSL_DEPRECATEDIN_3_0 int EC_KEY_generate_key(EC_KEY *key);
  968. /** Verifies that a private and/or public key is valid.
  969. * \param key the EC_KEY object
  970. * \return 1 on success and 0 otherwise.
  971. */
  972. OSSL_DEPRECATEDIN_3_0 int EC_KEY_check_key(const EC_KEY *key);
  973. /** Indicates if an EC_KEY can be used for signing.
  974. * \param eckey the EC_KEY object
  975. * \return 1 if can sign and 0 otherwise.
  976. */
  977. OSSL_DEPRECATEDIN_3_0 int EC_KEY_can_sign(const EC_KEY *eckey);
  978. /** Sets a public key from affine coordinates performing
  979. * necessary NIST PKV tests.
  980. * \param key the EC_KEY object
  981. * \param x public key x coordinate
  982. * \param y public key y coordinate
  983. * \return 1 on success and 0 otherwise.
  984. */
  985. OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_public_key_affine_coordinates(EC_KEY *key,
  986. BIGNUM *x,
  987. BIGNUM *y);
  988. /** Encodes an EC_KEY public key to an allocated octet string
  989. * \param key key to encode
  990. * \param form point conversion form
  991. * \param pbuf returns pointer to allocated buffer
  992. * \param ctx BN_CTX object (optional)
  993. * \return the length of the encoded octet string or 0 if an error occurred
  994. */
  995. OSSL_DEPRECATEDIN_3_0 size_t EC_KEY_key2buf(const EC_KEY *key,
  996. point_conversion_form_t form,
  997. unsigned char **pbuf, BN_CTX *ctx);
  998. /** Decodes a EC_KEY public key from a octet string
  999. * \param key key to decode
  1000. * \param buf memory buffer with the encoded ec point
  1001. * \param len length of the encoded ec point
  1002. * \param ctx BN_CTX object (optional)
  1003. * \return 1 on success and 0 if an error occurred
  1004. */
  1005. OSSL_DEPRECATEDIN_3_0 int EC_KEY_oct2key(EC_KEY *key, const unsigned char *buf,
  1006. size_t len, BN_CTX *ctx);
  1007. /** Decodes an EC_KEY private key from an octet string
  1008. * \param key key to decode
  1009. * \param buf memory buffer with the encoded private key
  1010. * \param len length of the encoded key
  1011. * \return 1 on success and 0 if an error occurred
  1012. */
  1013. OSSL_DEPRECATEDIN_3_0 int EC_KEY_oct2priv(EC_KEY *key, const unsigned char *buf,
  1014. size_t len);
  1015. /** Encodes a EC_KEY private key to an octet string
  1016. * \param key key to encode
  1017. * \param buf memory buffer for the result. If NULL the function returns
  1018. * required buffer size.
  1019. * \param len length of the memory buffer
  1020. * \return the length of the encoded octet string or 0 if an error occurred
  1021. */
  1022. OSSL_DEPRECATEDIN_3_0 size_t EC_KEY_priv2oct(const EC_KEY *key,
  1023. unsigned char *buf, size_t len);
  1024. /** Encodes an EC_KEY private key to an allocated octet string
  1025. * \param eckey key to encode
  1026. * \param pbuf returns pointer to allocated buffer
  1027. * \return the length of the encoded octet string or 0 if an error occurred
  1028. */
  1029. OSSL_DEPRECATEDIN_3_0 size_t EC_KEY_priv2buf(const EC_KEY *eckey,
  1030. unsigned char **pbuf);
  1031. /********************************************************************/
  1032. /* de- and encoding functions for SEC1 ECPrivateKey */
  1033. /********************************************************************/
  1034. /** Decodes a private key from a memory buffer.
  1035. * \param key a pointer to a EC_KEY object which should be used (or NULL)
  1036. * \param in pointer to memory with the DER encoded private key
  1037. * \param len length of the DER encoded private key
  1038. * \return the decoded private key or NULL if an error occurred.
  1039. */
  1040. OSSL_DEPRECATEDIN_3_0 EC_KEY *d2i_ECPrivateKey(EC_KEY **key,
  1041. const unsigned char **in,
  1042. long len);
  1043. /** Encodes a private key object and stores the result in a buffer.
  1044. * \param key the EC_KEY object to encode
  1045. * \param out the buffer for the result (if NULL the function returns number
  1046. * of bytes needed).
  1047. * \return 1 on success and 0 if an error occurred.
  1048. */
  1049. OSSL_DEPRECATEDIN_3_0 int i2d_ECPrivateKey(const EC_KEY *key,
  1050. unsigned char **out);
  1051. /********************************************************************/
  1052. /* de- and encoding functions for EC parameters */
  1053. /********************************************************************/
  1054. /** Decodes ec parameter from a memory buffer.
  1055. * \param key a pointer to a EC_KEY object which should be used (or NULL)
  1056. * \param in pointer to memory with the DER encoded ec parameters
  1057. * \param len length of the DER encoded ec parameters
  1058. * \return a EC_KEY object with the decoded parameters or NULL if an error
  1059. * occurred.
  1060. */
  1061. OSSL_DEPRECATEDIN_3_0 EC_KEY *d2i_ECParameters(EC_KEY **key,
  1062. const unsigned char **in,
  1063. long len);
  1064. /** Encodes ec parameter and stores the result in a buffer.
  1065. * \param key the EC_KEY object with ec parameters to encode
  1066. * \param out the buffer for the result (if NULL the function returns number
  1067. * of bytes needed).
  1068. * \return 1 on success and 0 if an error occurred.
  1069. */
  1070. OSSL_DEPRECATEDIN_3_0 int i2d_ECParameters(const EC_KEY *key,
  1071. unsigned char **out);
  1072. /********************************************************************/
  1073. /* de- and encoding functions for EC public key */
  1074. /* (octet string, not DER -- hence 'o2i' and 'i2o') */
  1075. /********************************************************************/
  1076. /** Decodes an ec public key from a octet string.
  1077. * \param key a pointer to a EC_KEY object which should be used
  1078. * \param in memory buffer with the encoded public key
  1079. * \param len length of the encoded public key
  1080. * \return EC_KEY object with decoded public key or NULL if an error
  1081. * occurred.
  1082. */
  1083. OSSL_DEPRECATEDIN_3_0 EC_KEY *o2i_ECPublicKey(EC_KEY **key,
  1084. const unsigned char **in, long len);
  1085. /** Encodes an ec public key in an octet string.
  1086. * \param key the EC_KEY object with the public key
  1087. * \param out the buffer for the result (if NULL the function returns number
  1088. * of bytes needed).
  1089. * \return 1 on success and 0 if an error occurred
  1090. */
  1091. OSSL_DEPRECATEDIN_3_0 int i2o_ECPublicKey(const EC_KEY *key, unsigned char **out);
  1092. /** Prints out the ec parameters on human readable form.
  1093. * \param bp BIO object to which the information is printed
  1094. * \param key EC_KEY object
  1095. * \return 1 on success and 0 if an error occurred
  1096. */
  1097. OSSL_DEPRECATEDIN_3_0 int ECParameters_print(BIO *bp, const EC_KEY *key);
  1098. /** Prints out the contents of a EC_KEY object
  1099. * \param bp BIO object to which the information is printed
  1100. * \param key EC_KEY object
  1101. * \param off line offset
  1102. * \return 1 on success and 0 if an error occurred
  1103. */
  1104. OSSL_DEPRECATEDIN_3_0 int EC_KEY_print(BIO *bp, const EC_KEY *key, int off);
  1105. #ifndef OPENSSL_NO_STDIO
  1106. /** Prints out the ec parameters on human readable form.
  1107. * \param fp file descriptor to which the information is printed
  1108. * \param key EC_KEY object
  1109. * \return 1 on success and 0 if an error occurred
  1110. */
  1111. OSSL_DEPRECATEDIN_3_0 int ECParameters_print_fp(FILE *fp, const EC_KEY *key);
  1112. /** Prints out the contents of a EC_KEY object
  1113. * \param fp file descriptor to which the information is printed
  1114. * \param key EC_KEY object
  1115. * \param off line offset
  1116. * \return 1 on success and 0 if an error occurred
  1117. */
  1118. OSSL_DEPRECATEDIN_3_0 int EC_KEY_print_fp(FILE *fp, const EC_KEY *key, int off);
  1119. #endif /* OPENSSL_NO_STDIO */
  1120. OSSL_DEPRECATEDIN_3_0 const EC_KEY_METHOD *EC_KEY_OpenSSL(void);
  1121. OSSL_DEPRECATEDIN_3_0 const EC_KEY_METHOD *EC_KEY_get_default_method(void);
  1122. OSSL_DEPRECATEDIN_3_0 void EC_KEY_set_default_method(const EC_KEY_METHOD *meth);
  1123. OSSL_DEPRECATEDIN_3_0 const EC_KEY_METHOD *EC_KEY_get_method(const EC_KEY *key);
  1124. OSSL_DEPRECATEDIN_3_0 int EC_KEY_set_method(EC_KEY *key, const EC_KEY_METHOD *meth);
  1125. OSSL_DEPRECATEDIN_3_0 EC_KEY *EC_KEY_new_method(ENGINE *engine);
  1126. /** The old name for ecdh_KDF_X9_63
  1127. * The ECDH KDF specification has been mistakenly attributed to ANSI X9.62,
  1128. * it is actually specified in ANSI X9.63.
  1129. * This identifier is retained for backwards compatibility
  1130. */
  1131. OSSL_DEPRECATEDIN_3_0 int ECDH_KDF_X9_62(unsigned char *out, size_t outlen,
  1132. const unsigned char *Z, size_t Zlen,
  1133. const unsigned char *sinfo,
  1134. size_t sinfolen, const EVP_MD *md);
  1135. OSSL_DEPRECATEDIN_3_0 int ECDH_compute_key(void *out, size_t outlen,
  1136. const EC_POINT *pub_key,
  1137. const EC_KEY *ecdh,
  1138. void *(*KDF)(const void *in,
  1139. size_t inlen, void *out,
  1140. size_t *outlen));
  1141. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  1142. typedef struct ECDSA_SIG_st ECDSA_SIG;
  1143. /** Allocates and initialize a ECDSA_SIG structure
  1144. * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
  1145. */
  1146. ECDSA_SIG *ECDSA_SIG_new(void);
  1147. /** frees a ECDSA_SIG structure
  1148. * \param sig pointer to the ECDSA_SIG structure
  1149. */
  1150. void ECDSA_SIG_free(ECDSA_SIG *sig);
  1151. /** i2d_ECDSA_SIG encodes content of ECDSA_SIG (note: this function modifies *pp
  1152. * (*pp += length of the DER encoded signature)).
  1153. * \param sig pointer to the ECDSA_SIG object
  1154. * \param pp pointer to a unsigned char pointer for the output or NULL
  1155. * \return the length of the DER encoded ECDSA_SIG object or a negative value
  1156. * on error
  1157. */
  1158. DECLARE_ASN1_ENCODE_FUNCTIONS_only(ECDSA_SIG, ECDSA_SIG)
  1159. /** d2i_ECDSA_SIG decodes an ECDSA signature (note: this function modifies *pp
  1160. * (*pp += len)).
  1161. * \param sig pointer to ECDSA_SIG pointer (may be NULL)
  1162. * \param pp memory buffer with the DER encoded signature
  1163. * \param len length of the buffer
  1164. * \return pointer to the decoded ECDSA_SIG structure (or NULL)
  1165. */
  1166. /** Accessor for r and s fields of ECDSA_SIG
  1167. * \param sig pointer to ECDSA_SIG structure
  1168. * \param pr pointer to BIGNUM pointer for r (may be NULL)
  1169. * \param ps pointer to BIGNUM pointer for s (may be NULL)
  1170. */
  1171. void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
  1172. /** Accessor for r field of ECDSA_SIG
  1173. * \param sig pointer to ECDSA_SIG structure
  1174. */
  1175. const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig);
  1176. /** Accessor for s field of ECDSA_SIG
  1177. * \param sig pointer to ECDSA_SIG structure
  1178. */
  1179. const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig);
  1180. /** Setter for r and s fields of ECDSA_SIG
  1181. * \param sig pointer to ECDSA_SIG structure
  1182. * \param r pointer to BIGNUM for r
  1183. * \param s pointer to BIGNUM for s
  1184. */
  1185. int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s);
  1186. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1187. /** Computes the ECDSA signature of the given hash value using
  1188. * the supplied private key and returns the created signature.
  1189. * \param dgst pointer to the hash value
  1190. * \param dgst_len length of the hash value
  1191. * \param eckey EC_KEY object containing a private EC key
  1192. * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
  1193. */
  1194. OSSL_DEPRECATEDIN_3_0 ECDSA_SIG *ECDSA_do_sign(const unsigned char *dgst,
  1195. int dgst_len, EC_KEY *eckey);
  1196. /** Computes ECDSA signature of a given hash value using the supplied
  1197. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  1198. * \param dgst pointer to the hash value to sign
  1199. * \param dgstlen length of the hash value
  1200. * \param kinv BIGNUM with a pre-computed inverse k (optional)
  1201. * \param rp BIGNUM with a pre-computed rp value (optional),
  1202. * see ECDSA_sign_setup
  1203. * \param eckey EC_KEY object containing a private EC key
  1204. * \return pointer to a ECDSA_SIG structure or NULL if an error occurred
  1205. */
  1206. OSSL_DEPRECATEDIN_3_0 ECDSA_SIG *ECDSA_do_sign_ex(const unsigned char *dgst,
  1207. int dgstlen, const BIGNUM *kinv,
  1208. const BIGNUM *rp, EC_KEY *eckey);
  1209. /** Verifies that the supplied signature is a valid ECDSA
  1210. * signature of the supplied hash value using the supplied public key.
  1211. * \param dgst pointer to the hash value
  1212. * \param dgst_len length of the hash value
  1213. * \param sig ECDSA_SIG structure
  1214. * \param eckey EC_KEY object containing a public EC key
  1215. * \return 1 if the signature is valid, 0 if the signature is invalid
  1216. * and -1 on error
  1217. */
  1218. OSSL_DEPRECATEDIN_3_0 int ECDSA_do_verify(const unsigned char *dgst, int dgst_len,
  1219. const ECDSA_SIG *sig, EC_KEY *eckey);
  1220. /** Precompute parts of the signing operation
  1221. * \param eckey EC_KEY object containing a private EC key
  1222. * \param ctx BN_CTX object (optional)
  1223. * \param kinv BIGNUM pointer for the inverse of k
  1224. * \param rp BIGNUM pointer for x coordinate of k * generator
  1225. * \return 1 on success and 0 otherwise
  1226. */
  1227. OSSL_DEPRECATEDIN_3_0 int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx,
  1228. BIGNUM **kinv, BIGNUM **rp);
  1229. /** Computes ECDSA signature of a given hash value using the supplied
  1230. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  1231. * \param type this parameter is ignored
  1232. * \param dgst pointer to the hash value to sign
  1233. * \param dgstlen length of the hash value
  1234. * \param sig memory for the DER encoded created signature
  1235. * \param siglen pointer to the length of the returned signature
  1236. * \param eckey EC_KEY object containing a private EC key
  1237. * \return 1 on success and 0 otherwise
  1238. */
  1239. OSSL_DEPRECATEDIN_3_0 int ECDSA_sign(int type, const unsigned char *dgst,
  1240. int dgstlen, unsigned char *sig,
  1241. unsigned int *siglen, EC_KEY *eckey);
  1242. /** Computes ECDSA signature of a given hash value using the supplied
  1243. * private key (note: sig must point to ECDSA_size(eckey) bytes of memory).
  1244. * \param type this parameter is ignored
  1245. * \param dgst pointer to the hash value to sign
  1246. * \param dgstlen length of the hash value
  1247. * \param sig buffer to hold the DER encoded signature
  1248. * \param siglen pointer to the length of the returned signature
  1249. * \param kinv BIGNUM with a pre-computed inverse k (optional)
  1250. * \param rp BIGNUM with a pre-computed rp value (optional),
  1251. * see ECDSA_sign_setup
  1252. * \param eckey EC_KEY object containing a private EC key
  1253. * \return 1 on success and 0 otherwise
  1254. */
  1255. OSSL_DEPRECATEDIN_3_0 int ECDSA_sign_ex(int type, const unsigned char *dgst,
  1256. int dgstlen, unsigned char *sig,
  1257. unsigned int *siglen, const BIGNUM *kinv,
  1258. const BIGNUM *rp, EC_KEY *eckey);
  1259. /** Verifies that the given signature is valid ECDSA signature
  1260. * of the supplied hash value using the specified public key.
  1261. * \param type this parameter is ignored
  1262. * \param dgst pointer to the hash value
  1263. * \param dgstlen length of the hash value
  1264. * \param sig pointer to the DER encoded signature
  1265. * \param siglen length of the DER encoded signature
  1266. * \param eckey EC_KEY object containing a public EC key
  1267. * \return 1 if the signature is valid, 0 if the signature is invalid
  1268. * and -1 on error
  1269. */
  1270. OSSL_DEPRECATEDIN_3_0 int ECDSA_verify(int type, const unsigned char *dgst,
  1271. int dgstlen, const unsigned char *sig,
  1272. int siglen, EC_KEY *eckey);
  1273. /** Returns the maximum length of the DER encoded signature
  1274. * \param eckey EC_KEY object
  1275. * \return numbers of bytes required for the DER encoded signature
  1276. */
  1277. OSSL_DEPRECATEDIN_3_0 int ECDSA_size(const EC_KEY *eckey);
  1278. /********************************************************************/
  1279. /* EC_KEY_METHOD constructors, destructors, writers and accessors */
  1280. /********************************************************************/
  1281. OSSL_DEPRECATEDIN_3_0 EC_KEY_METHOD *EC_KEY_METHOD_new(const EC_KEY_METHOD *meth);
  1282. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_free(EC_KEY_METHOD *meth);
  1283. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_set_init(EC_KEY_METHOD *meth,
  1284. int (*init)(EC_KEY *key),
  1285. void (*finish)(EC_KEY *key),
  1286. int (*copy)(EC_KEY *dest, const EC_KEY *src),
  1287. int (*set_group)(EC_KEY *key, const EC_GROUP *grp),
  1288. int (*set_private)(EC_KEY *key, const BIGNUM *priv_key),
  1289. int (*set_public)(EC_KEY *key, const EC_POINT *pub_key));
  1290. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_set_keygen(EC_KEY_METHOD *meth,
  1291. int (*keygen)(EC_KEY *key));
  1292. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_set_compute_key(EC_KEY_METHOD *meth,
  1293. int (*ckey)(unsigned char **psec, size_t *pseclen,
  1294. const EC_POINT *pub_key, const EC_KEY *ecdh));
  1295. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_set_sign(EC_KEY_METHOD *meth,
  1296. int (*sign)(int type, const unsigned char *dgst,
  1297. int dlen, unsigned char *sig,
  1298. unsigned int *siglen,
  1299. const BIGNUM *kinv, const BIGNUM *r,
  1300. EC_KEY *eckey),
  1301. int (*sign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  1302. BIGNUM **kinvp, BIGNUM **rp),
  1303. ECDSA_SIG *(*sign_sig)(const unsigned char *dgst,
  1304. int dgst_len,
  1305. const BIGNUM *in_kinv,
  1306. const BIGNUM *in_r,
  1307. EC_KEY *eckey));
  1308. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_set_verify(EC_KEY_METHOD *meth,
  1309. int (*verify)(int type, const unsigned char *dgst, int dgst_len,
  1310. const unsigned char *sigbuf,
  1311. int sig_len, EC_KEY *eckey),
  1312. int (*verify_sig)(const unsigned char *dgst,
  1313. int dgst_len, const ECDSA_SIG *sig,
  1314. EC_KEY *eckey));
  1315. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_get_init(const EC_KEY_METHOD *meth,
  1316. int (**pinit)(EC_KEY *key),
  1317. void (**pfinish)(EC_KEY *key),
  1318. int (**pcopy)(EC_KEY *dest, const EC_KEY *src),
  1319. int (**pset_group)(EC_KEY *key, const EC_GROUP *grp),
  1320. int (**pset_private)(EC_KEY *key, const BIGNUM *priv_key),
  1321. int (**pset_public)(EC_KEY *key, const EC_POINT *pub_key));
  1322. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_get_keygen(const EC_KEY_METHOD *meth, int (**pkeygen)(EC_KEY *key));
  1323. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_get_compute_key(const EC_KEY_METHOD *meth,
  1324. int (**pck)(unsigned char **psec,
  1325. size_t *pseclen,
  1326. const EC_POINT *pub_key,
  1327. const EC_KEY *ecdh));
  1328. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_get_sign(const EC_KEY_METHOD *meth,
  1329. int (**psign)(int type, const unsigned char *dgst,
  1330. int dlen, unsigned char *sig,
  1331. unsigned int *siglen,
  1332. const BIGNUM *kinv, const BIGNUM *r,
  1333. EC_KEY *eckey),
  1334. int (**psign_setup)(EC_KEY *eckey, BN_CTX *ctx_in,
  1335. BIGNUM **kinvp, BIGNUM **rp),
  1336. ECDSA_SIG *(**psign_sig)(const unsigned char *dgst,
  1337. int dgst_len,
  1338. const BIGNUM *in_kinv,
  1339. const BIGNUM *in_r,
  1340. EC_KEY *eckey));
  1341. OSSL_DEPRECATEDIN_3_0 void EC_KEY_METHOD_get_verify(const EC_KEY_METHOD *meth,
  1342. int (**pverify)(int type, const unsigned char *dgst, int dgst_len,
  1343. const unsigned char *sigbuf,
  1344. int sig_len, EC_KEY *eckey),
  1345. int (**pverify_sig)(const unsigned char *dgst,
  1346. int dgst_len,
  1347. const ECDSA_SIG *sig,
  1348. EC_KEY *eckey));
  1349. #endif /* OPENSSL_NO_DEPRECATED_3_0 */
  1350. #define EVP_EC_gen(curve) \
  1351. EVP_PKEY_Q_keygen(NULL, NULL, "EC", (char *)(strstr(curve, "")))
  1352. /* strstr is used to enable type checking for the variadic string arg */
  1353. #define ECParameters_dup(x) ASN1_dup_of(EC_KEY, i2d_ECParameters, \
  1354. d2i_ECParameters, x)
  1355. #ifndef __cplusplus
  1356. #if defined(__SUNPRO_C)
  1357. #if __SUNPRO_C >= 0x520
  1358. #pragma error_messages(default, E_ARRAY_OF_INCOMPLETE_NONAME, E_ARRAY_OF_INCOMPLETE)
  1359. #endif
  1360. #endif
  1361. #endif
  1362. #endif
  1363. #ifdef __cplusplus
  1364. }
  1365. #endif
  1366. #endif