ec.h 66 KB

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