ec.h 66 KB

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