ecp.h 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /**
  2. * \file ecp.h
  3. *
  4. * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
  5. *
  6. * The use of ECP in cryptography and TLS is defined in
  7. * <em>Standards for Efficient Cryptography Group (SECG): SEC1
  8. * Elliptic Curve Cryptography</em> and
  9. * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
  10. * for Transport Layer Security (TLS)</em>.
  11. *
  12. * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
  13. * group types.
  14. *
  15. */
  16. /*
  17. * Copyright The Mbed TLS Contributors
  18. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  19. */
  20. #ifndef MBEDTLS_ECP_H
  21. #define MBEDTLS_ECP_H
  22. #include "mbedtls/private_access.h"
  23. #include "mbedtls/build_info.h"
  24. #include "mbedtls/platform_util.h"
  25. #include "mbedtls/bignum.h"
  26. /*
  27. * ECP error codes
  28. */
  29. /** Bad input parameters to function. */
  30. #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80
  31. /** The buffer is too small to write to. */
  32. #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00
  33. /** The requested feature is not available, for example, the requested curve is not supported. */
  34. #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80
  35. /** The signature is not valid. */
  36. #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00
  37. /** Memory allocation failed. */
  38. #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80
  39. /** Generation of random value, such as ephemeral key, failed. */
  40. #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00
  41. /** Invalid private or public key. */
  42. #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80
  43. /** The buffer contains a valid signature followed by more data. */
  44. #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00
  45. /** Operation in progress, call again with the same parameters to continue. */
  46. #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00
  47. /* Flags indicating whether to include code that is specific to certain
  48. * types of curves. These flags are for internal library use only. */
  49. #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
  50. defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || \
  51. defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
  52. defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
  53. defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
  54. defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
  55. defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
  56. defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
  57. defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
  58. defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || \
  59. defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  60. #define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED
  61. #endif
  62. #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
  63. defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
  64. #define MBEDTLS_ECP_MONTGOMERY_ENABLED
  65. #endif
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. /**
  70. * Domain-parameter identifiers: curve, subgroup, and generator.
  71. *
  72. * \note Only curves over prime fields are supported.
  73. *
  74. * \warning This library does not support validation of arbitrary domain
  75. * parameters. Therefore, only standardized domain parameters from trusted
  76. * sources should be used. See mbedtls_ecp_group_load().
  77. */
  78. /* Note: when adding a new curve:
  79. * - Add it at the end of this enum, otherwise you'll break the ABI by
  80. * changing the numerical value for existing curves.
  81. * - Increment MBEDTLS_ECP_DP_MAX below if needed.
  82. * - Update the calculation of MBEDTLS_ECP_MAX_BITS below.
  83. * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to
  84. * mbedtls_config.h.
  85. * - List the curve as a dependency of MBEDTLS_ECP_C and
  86. * MBEDTLS_ECDSA_C if supported in check_config.h.
  87. * - Add the curve to the appropriate curve type macro
  88. * MBEDTLS_ECP_yyy_ENABLED above.
  89. * - Add the necessary definitions to ecp_curves.c.
  90. * - Add the curve to the ecp_supported_curves array in ecp.c.
  91. * - Add the curve to applicable profiles in x509_crt.c.
  92. * - Add the curve to applicable presets in ssl_tls.c.
  93. */
  94. typedef enum {
  95. MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
  96. MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
  97. MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
  98. MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
  99. MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
  100. MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
  101. MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
  102. MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
  103. MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
  104. MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
  105. MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
  106. MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
  107. MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
  108. MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
  109. } mbedtls_ecp_group_id;
  110. /**
  111. * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
  112. */
  113. #define MBEDTLS_ECP_DP_MAX 14
  114. /*
  115. * Curve types
  116. */
  117. typedef enum {
  118. MBEDTLS_ECP_TYPE_NONE = 0,
  119. MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
  120. MBEDTLS_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
  121. } mbedtls_ecp_curve_type;
  122. /**
  123. * Curve information, for use by other modules.
  124. *
  125. * The fields of this structure are part of the public API and can be
  126. * accessed directly by applications. Future versions of the library may
  127. * add extra fields or reorder existing fields.
  128. */
  129. typedef struct mbedtls_ecp_curve_info {
  130. mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
  131. uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
  132. uint16_t bit_size; /*!< The curve size in bits. */
  133. const char *name; /*!< A human-friendly name. */
  134. } mbedtls_ecp_curve_info;
  135. /**
  136. * \brief The ECP point structure, in Jacobian coordinates.
  137. *
  138. * \note All functions expect and return points satisfying
  139. * the following condition: <code>Z == 0</code> or
  140. * <code>Z == 1</code>. Other values of \p Z are
  141. * used only by internal functions.
  142. * The point is zero, or "at infinity", if <code>Z == 0</code>.
  143. * Otherwise, \p X and \p Y are its standard (affine)
  144. * coordinates.
  145. */
  146. typedef struct mbedtls_ecp_point {
  147. mbedtls_mpi MBEDTLS_PRIVATE(X); /*!< The X coordinate of the ECP point. */
  148. mbedtls_mpi MBEDTLS_PRIVATE(Y); /*!< The Y coordinate of the ECP point. */
  149. mbedtls_mpi MBEDTLS_PRIVATE(Z); /*!< The Z coordinate of the ECP point. */
  150. }
  151. mbedtls_ecp_point;
  152. #if !defined(MBEDTLS_ECP_ALT)
  153. /*
  154. * default Mbed TLS elliptic curve arithmetic implementation
  155. *
  156. * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
  157. * alternative implementation for the whole module and it will replace this
  158. * one.)
  159. */
  160. /**
  161. * \brief The ECP group structure.
  162. *
  163. * We consider two types of curve equations:
  164. * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
  165. * (SEC1 + RFC-4492)</li>
  166. * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
  167. * Curve448)</li></ul>
  168. * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
  169. *
  170. * For Short Weierstrass, this subgroup is the whole curve, and its
  171. * cardinality is denoted by \p N. Our code requires that \p N is an
  172. * odd prime as mbedtls_ecp_mul() requires an odd number, and
  173. * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
  174. *
  175. * The default implementation only initializes \p A without setting it to the
  176. * authentic value for curves with <code>A = -3</code>(SECP256R1, etc), in which
  177. * case you need to load \p A by yourself when using domain parameters directly,
  178. * for example:
  179. * \code
  180. * mbedtls_mpi_init(&A);
  181. * mbedtls_ecp_group_init(&grp);
  182. * CHECK_RETURN(mbedtls_ecp_group_load(&grp, grp_id));
  183. * if (mbedtls_ecp_group_a_is_minus_3(&grp)) {
  184. * CHECK_RETURN(mbedtls_mpi_sub_int(&A, &grp.P, 3));
  185. * } else {
  186. * CHECK_RETURN(mbedtls_mpi_copy(&A, &grp.A));
  187. * }
  188. *
  189. * do_something_with_a(&A);
  190. *
  191. * cleanup:
  192. * mbedtls_mpi_free(&A);
  193. * mbedtls_ecp_group_free(&grp);
  194. * \endcode
  195. *
  196. * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
  197. * which is the quantity used in the formulas. Additionally, \p nbits is
  198. * not the size of \p N but the required size for private keys.
  199. *
  200. * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
  201. * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
  202. * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
  203. * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
  204. * in size, so that it may be efficiently brought in the 0..P-1 range by a few
  205. * additions or subtractions. Therefore, it is only an approximate modular
  206. * reduction. It must return 0 on success and non-zero on failure.
  207. *
  208. * \note Alternative implementations of the ECP module must obey the
  209. * following constraints.
  210. * * Group IDs must be distinct: if two group structures have
  211. * the same ID, then they must be identical.
  212. * * The fields \c id, \c P, \c A, \c B, \c G, \c N,
  213. * \c pbits and \c nbits must have the same type and semantics
  214. * as in the built-in implementation.
  215. * They must be available for reading, but direct modification
  216. * of these fields does not need to be supported.
  217. * They do not need to be at the same offset in the structure.
  218. */
  219. typedef struct mbedtls_ecp_group {
  220. mbedtls_ecp_group_id id; /*!< An internal group identifier. */
  221. mbedtls_mpi P; /*!< The prime modulus of the base field. */
  222. mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. Note that
  223. \p A is not set to the authentic value in some cases.
  224. Refer to detailed description of ::mbedtls_ecp_group if
  225. using domain parameters in the structure.
  226. For Montgomery curves: <code>(A + 2) / 4</code>. */
  227. mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
  228. For Montgomery curves: unused. */
  229. mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
  230. mbedtls_mpi N; /*!< The order of \p G. */
  231. size_t pbits; /*!< The number of bits in \p P.*/
  232. size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
  233. For Montgomery curves: the number of bits in the
  234. private keys. */
  235. /* End of public fields */
  236. unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if the constants are static. */
  237. int(*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
  238. mod \p P (see above).*/
  239. int(*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *); /*!< Unused. */
  240. int(*MBEDTLS_PRIVATE(t_post))(mbedtls_ecp_point *, void *); /*!< Unused. */
  241. void *MBEDTLS_PRIVATE(t_data); /*!< Unused. */
  242. mbedtls_ecp_point *MBEDTLS_PRIVATE(T); /*!< Pre-computed points for ecp_mul_comb(). */
  243. size_t MBEDTLS_PRIVATE(T_size); /*!< The number of dynamic allocated pre-computed points. */
  244. }
  245. mbedtls_ecp_group;
  246. /**
  247. * \name SECTION: Module settings
  248. *
  249. * The configuration options you can set for this module are in this section.
  250. * Either change them in mbedtls_config.h, or define them using the compiler command line.
  251. * \{
  252. */
  253. #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
  254. /*
  255. * Maximum "window" size used for point multiplication.
  256. * Default: a point where higher memory usage yields diminishing performance
  257. * returns.
  258. * Minimum value: 2. Maximum value: 7.
  259. *
  260. * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  261. * points used for point multiplication. This value is directly tied to EC
  262. * peak memory usage, so decreasing it by one should roughly cut memory usage
  263. * by two (if large curves are in use).
  264. *
  265. * Reduction in size may reduce speed, but larger curves are impacted first.
  266. * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
  267. * w-size: 6 5 4 3 2
  268. * 521 145 141 135 120 97
  269. * 384 214 209 198 177 146
  270. * 256 320 320 303 262 226
  271. * 224 475 475 453 398 342
  272. * 192 640 640 633 587 476
  273. */
  274. #define MBEDTLS_ECP_WINDOW_SIZE 4 /**< The maximum window size used. */
  275. #endif /* MBEDTLS_ECP_WINDOW_SIZE */
  276. #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
  277. /*
  278. * Trade code size for speed on fixed-point multiplication.
  279. *
  280. * This speeds up repeated multiplication of the generator (that is, the
  281. * multiplication in ECDSA signatures, and half of the multiplications in
  282. * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
  283. *
  284. * For each n-bit Short Weierstrass curve that is enabled, this adds 4n bytes
  285. * of code size if n < 384 and 8n otherwise.
  286. *
  287. * Change this value to 0 to reduce code size.
  288. */
  289. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
  290. #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
  291. /** \} name SECTION: Module settings */
  292. #else /* MBEDTLS_ECP_ALT */
  293. #include "ecp_alt.h"
  294. #endif /* MBEDTLS_ECP_ALT */
  295. /**
  296. * The maximum size of the groups, that is, of \c N and \c P.
  297. */
  298. #if !defined(MBEDTLS_ECP_LIGHT)
  299. /* Dummy definition to help code that has optional ECP support and
  300. * defines an MBEDTLS_ECP_MAX_BYTES-sized array unconditionally. */
  301. #define MBEDTLS_ECP_MAX_BITS 1
  302. /* Note: the curves must be listed in DECREASING size! */
  303. #elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  304. #define MBEDTLS_ECP_MAX_BITS 521
  305. #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  306. #define MBEDTLS_ECP_MAX_BITS 512
  307. #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
  308. #define MBEDTLS_ECP_MAX_BITS 448
  309. #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  310. #define MBEDTLS_ECP_MAX_BITS 384
  311. #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  312. #define MBEDTLS_ECP_MAX_BITS 384
  313. #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  314. #define MBEDTLS_ECP_MAX_BITS 256
  315. #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  316. #define MBEDTLS_ECP_MAX_BITS 256
  317. #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  318. #define MBEDTLS_ECP_MAX_BITS 256
  319. #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  320. #define MBEDTLS_ECP_MAX_BITS 255
  321. #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  322. #define MBEDTLS_ECP_MAX_BITS 225 // n is slightly above 2^224
  323. #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  324. #define MBEDTLS_ECP_MAX_BITS 224
  325. #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  326. #define MBEDTLS_ECP_MAX_BITS 192
  327. #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  328. #define MBEDTLS_ECP_MAX_BITS 192
  329. #else /* !MBEDTLS_ECP_LIGHT */
  330. #error "Missing definition of MBEDTLS_ECP_MAX_BITS"
  331. #endif /* !MBEDTLS_ECP_LIGHT */
  332. #define MBEDTLS_ECP_MAX_BYTES ((MBEDTLS_ECP_MAX_BITS + 7) / 8)
  333. #define MBEDTLS_ECP_MAX_PT_LEN (2 * MBEDTLS_ECP_MAX_BYTES + 1)
  334. #if defined(MBEDTLS_ECP_RESTARTABLE)
  335. /**
  336. * \brief Internal restart context for multiplication
  337. *
  338. * \note Opaque struct
  339. */
  340. typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
  341. /**
  342. * \brief Internal restart context for ecp_muladd()
  343. *
  344. * \note Opaque struct
  345. */
  346. typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
  347. /**
  348. * \brief General context for resuming ECC operations
  349. */
  350. typedef struct {
  351. unsigned MBEDTLS_PRIVATE(ops_done); /*!< current ops count */
  352. unsigned MBEDTLS_PRIVATE(depth); /*!< call depth (0 = top-level) */
  353. mbedtls_ecp_restart_mul_ctx *MBEDTLS_PRIVATE(rsm); /*!< ecp_mul_comb() sub-context */
  354. mbedtls_ecp_restart_muladd_ctx *MBEDTLS_PRIVATE(ma); /*!< ecp_muladd() sub-context */
  355. } mbedtls_ecp_restart_ctx;
  356. /*
  357. * Operation counts for restartable functions
  358. */
  359. #define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
  360. #define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
  361. #define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
  362. #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
  363. /**
  364. * \brief Internal; for restartable functions in other modules.
  365. * Check and update basic ops budget.
  366. *
  367. * \param grp Group structure
  368. * \param rs_ctx Restart context
  369. * \param ops Number of basic ops to do
  370. *
  371. * \return \c 0 if doing \p ops basic ops is still allowed,
  372. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
  373. */
  374. int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
  375. mbedtls_ecp_restart_ctx *rs_ctx,
  376. unsigned ops);
  377. /* Utility macro for checking and updating ops budget */
  378. #define MBEDTLS_ECP_BUDGET(ops) \
  379. MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, rs_ctx, \
  380. (unsigned) (ops)));
  381. #else /* MBEDTLS_ECP_RESTARTABLE */
  382. #define MBEDTLS_ECP_BUDGET(ops) /* no-op; for compatibility */
  383. /* We want to declare restartable versions of existing functions anyway */
  384. typedef void mbedtls_ecp_restart_ctx;
  385. #endif /* MBEDTLS_ECP_RESTARTABLE */
  386. /**
  387. * \brief The ECP key-pair structure.
  388. *
  389. * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
  390. *
  391. * \note Members are deliberately in the same order as in the
  392. * ::mbedtls_ecdsa_context structure.
  393. */
  394. typedef struct mbedtls_ecp_keypair {
  395. mbedtls_ecp_group MBEDTLS_PRIVATE(grp); /*!< Elliptic curve and base point */
  396. mbedtls_mpi MBEDTLS_PRIVATE(d); /*!< our secret value */
  397. mbedtls_ecp_point MBEDTLS_PRIVATE(Q); /*!< our public value */
  398. }
  399. mbedtls_ecp_keypair;
  400. /**
  401. * The uncompressed point format for Short Weierstrass curves
  402. * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX).
  403. */
  404. #define MBEDTLS_ECP_PF_UNCOMPRESSED 0
  405. /**
  406. * The compressed point format for Short Weierstrass curves
  407. * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX).
  408. *
  409. * \warning While this format is supported for all concerned curves for
  410. * writing, when it comes to parsing, it is not supported for all
  411. * curves. Specifically, parsing compressed points on
  412. * MBEDTLS_ECP_DP_SECP224R1 and MBEDTLS_ECP_DP_SECP224K1 is not
  413. * supported.
  414. */
  415. #define MBEDTLS_ECP_PF_COMPRESSED 1
  416. /*
  417. * Some other constants from RFC 4492
  418. */
  419. #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
  420. #if defined(MBEDTLS_ECP_RESTARTABLE)
  421. /**
  422. * \brief Set the maximum number of basic operations done in a row.
  423. *
  424. * If more operations are needed to complete a computation,
  425. * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
  426. * function performing the computation. It is then the
  427. * caller's responsibility to either call again with the same
  428. * parameters until it returns 0 or an error code; or to free
  429. * the restart context if the operation is to be aborted.
  430. *
  431. * It is strictly required that all input parameters and the
  432. * restart context be the same on successive calls for the
  433. * same operation, but output parameters need not be the
  434. * same; they must not be used until the function finally
  435. * returns 0.
  436. *
  437. * This only applies to functions whose documentation
  438. * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
  439. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
  440. * SSL module). For functions that accept a "restart context"
  441. * argument, passing NULL disables restart and makes the
  442. * function equivalent to the function with the same name
  443. * with \c _restartable removed. For functions in the ECDH
  444. * module, restart is disabled unless the function accepts
  445. * an "ECDH context" argument and
  446. * mbedtls_ecdh_enable_restart() was previously called on
  447. * that context. For function in the SSL module, restart is
  448. * only enabled for specific sides and key exchanges
  449. * (currently only for clients and ECDHE-ECDSA).
  450. *
  451. * \warning Using the PSA interruptible interfaces with keys in local
  452. * storage and no accelerator driver will also call this
  453. * function to set the values specified via those interfaces,
  454. * overwriting values previously set. Care should be taken if
  455. * mixing these two interfaces.
  456. *
  457. * \param max_ops Maximum number of basic operations done in a row.
  458. * Default: 0 (unlimited).
  459. * Lower (non-zero) values mean ECC functions will block for
  460. * a lesser maximum amount of time.
  461. *
  462. * \note A "basic operation" is defined as a rough equivalent of a
  463. * multiplication in GF(p) for the NIST P-256 curve.
  464. * As an indication, with default settings, a scalar
  465. * multiplication (full run of \c mbedtls_ecp_mul()) is:
  466. * - about 3300 basic operations for P-256
  467. * - about 9400 basic operations for P-384
  468. *
  469. * \note Very low values are not always respected: sometimes
  470. * functions need to block for a minimum number of
  471. * operations, and will do so even if max_ops is set to a
  472. * lower value. That minimum depends on the curve size, and
  473. * can be made lower by decreasing the value of
  474. * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
  475. * lowest effective value for various curves and values of
  476. * that parameter (w for short):
  477. * w=6 w=5 w=4 w=3 w=2
  478. * P-256 208 208 160 136 124
  479. * P-384 682 416 320 272 248
  480. * P-521 1364 832 640 544 496
  481. *
  482. * \note This setting is currently ignored by Curve25519.
  483. */
  484. void mbedtls_ecp_set_max_ops(unsigned max_ops);
  485. /**
  486. * \brief Check if restart is enabled (max_ops != 0)
  487. *
  488. * \return \c 0 if \c max_ops == 0 (restart disabled)
  489. * \return \c 1 otherwise (restart enabled)
  490. */
  491. int mbedtls_ecp_restart_is_enabled(void);
  492. #endif /* MBEDTLS_ECP_RESTARTABLE */
  493. /*
  494. * Get the type of a curve
  495. */
  496. mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp);
  497. /**
  498. * \brief This function retrieves the information defined in
  499. * mbedtls_ecp_curve_info() for all supported curves.
  500. *
  501. * \note This function returns information about all curves
  502. * supported by the library. Some curves may not be
  503. * supported for all algorithms. Call mbedtls_ecdh_can_do()
  504. * or mbedtls_ecdsa_can_do() to check if a curve is
  505. * supported for ECDH or ECDSA.
  506. *
  507. * \return A statically allocated array. The last entry is 0.
  508. */
  509. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void);
  510. /**
  511. * \brief This function retrieves the list of internal group
  512. * identifiers of all supported curves in the order of
  513. * preference.
  514. *
  515. * \note This function returns information about all curves
  516. * supported by the library. Some curves may not be
  517. * supported for all algorithms. Call mbedtls_ecdh_can_do()
  518. * or mbedtls_ecdsa_can_do() to check if a curve is
  519. * supported for ECDH or ECDSA.
  520. *
  521. * \return A statically allocated array,
  522. * terminated with MBEDTLS_ECP_DP_NONE.
  523. */
  524. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void);
  525. /**
  526. * \brief This function retrieves curve information from an internal
  527. * group identifier.
  528. *
  529. * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
  530. *
  531. * \return The associated curve information on success.
  532. * \return NULL on failure.
  533. */
  534. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id);
  535. /**
  536. * \brief This function retrieves curve information from a TLS
  537. * NamedCurve value.
  538. *
  539. * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
  540. *
  541. * \return The associated curve information on success.
  542. * \return NULL on failure.
  543. */
  544. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id);
  545. /**
  546. * \brief This function retrieves curve information from a
  547. * human-readable name.
  548. *
  549. * \param name The human-readable name.
  550. *
  551. * \return The associated curve information on success.
  552. * \return NULL on failure.
  553. */
  554. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name);
  555. /**
  556. * \brief This function initializes a point as zero.
  557. *
  558. * \param pt The point to initialize.
  559. */
  560. void mbedtls_ecp_point_init(mbedtls_ecp_point *pt);
  561. /**
  562. * \brief This function initializes an ECP group context
  563. * without loading any domain parameters.
  564. *
  565. * \note After this function is called, domain parameters
  566. * for various ECP groups can be loaded through the
  567. * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
  568. * functions.
  569. */
  570. void mbedtls_ecp_group_init(mbedtls_ecp_group *grp);
  571. /**
  572. * \brief This function initializes a key pair as an invalid one.
  573. *
  574. * \param key The key pair to initialize.
  575. */
  576. void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key);
  577. /**
  578. * \brief This function frees the components of a point.
  579. *
  580. * \param pt The point to free.
  581. */
  582. void mbedtls_ecp_point_free(mbedtls_ecp_point *pt);
  583. /**
  584. * \brief This function frees the components of an ECP group.
  585. *
  586. * \param grp The group to free. This may be \c NULL, in which
  587. * case this function returns immediately. If it is not
  588. * \c NULL, it must point to an initialized ECP group.
  589. */
  590. void mbedtls_ecp_group_free(mbedtls_ecp_group *grp);
  591. /**
  592. * \brief This function frees the components of a key pair.
  593. *
  594. * \param key The key pair to free. This may be \c NULL, in which
  595. * case this function returns immediately. If it is not
  596. * \c NULL, it must point to an initialized ECP key pair.
  597. */
  598. void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key);
  599. #if defined(MBEDTLS_ECP_RESTARTABLE)
  600. /**
  601. * \brief Initialize a restart context.
  602. *
  603. * \param ctx The restart context to initialize. This must
  604. * not be \c NULL.
  605. */
  606. void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx);
  607. /**
  608. * \brief Free the components of a restart context.
  609. *
  610. * \param ctx The restart context to free. This may be \c NULL, in which
  611. * case this function returns immediately. If it is not
  612. * \c NULL, it must point to an initialized restart context.
  613. */
  614. void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx);
  615. #endif /* MBEDTLS_ECP_RESTARTABLE */
  616. /**
  617. * \brief This function copies the contents of point \p Q into
  618. * point \p P.
  619. *
  620. * \param P The destination point. This must be initialized.
  621. * \param Q The source point. This must be initialized.
  622. *
  623. * \return \c 0 on success.
  624. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  625. * \return Another negative error code for other kinds of failure.
  626. */
  627. int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q);
  628. /**
  629. * \brief This function copies the contents of group \p src into
  630. * group \p dst.
  631. *
  632. * \param dst The destination group. This must be initialized.
  633. * \param src The source group. This must be initialized.
  634. *
  635. * \return \c 0 on success.
  636. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  637. * \return Another negative error code on other kinds of failure.
  638. */
  639. int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst,
  640. const mbedtls_ecp_group *src);
  641. /**
  642. * \brief This function sets a point to the point at infinity.
  643. *
  644. * \param pt The point to set. This must be initialized.
  645. *
  646. * \return \c 0 on success.
  647. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  648. * \return Another negative error code on other kinds of failure.
  649. */
  650. int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt);
  651. /**
  652. * \brief This function checks if a point is the point at infinity.
  653. *
  654. * \param pt The point to test. This must be initialized.
  655. *
  656. * \return \c 1 if the point is zero.
  657. * \return \c 0 if the point is non-zero.
  658. * \return A negative error code on failure.
  659. */
  660. int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt);
  661. /**
  662. * \brief This function compares two points.
  663. *
  664. * \note This assumes that the points are normalized. Otherwise,
  665. * they may compare as "not equal" even if they are.
  666. *
  667. * \param P The first point to compare. This must be initialized.
  668. * \param Q The second point to compare. This must be initialized.
  669. *
  670. * \return \c 0 if the points are equal.
  671. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
  672. */
  673. int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
  674. const mbedtls_ecp_point *Q);
  675. /**
  676. * \brief This function imports a non-zero point from two ASCII
  677. * strings.
  678. *
  679. * \param P The destination point. This must be initialized.
  680. * \param radix The numeric base of the input.
  681. * \param x The first affine coordinate, as a null-terminated string.
  682. * \param y The second affine coordinate, as a null-terminated string.
  683. *
  684. * \return \c 0 on success.
  685. * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
  686. */
  687. int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
  688. const char *x, const char *y);
  689. /**
  690. * \brief This function exports a point into unsigned binary data.
  691. *
  692. * \param grp The group to which the point should belong.
  693. * This must be initialized and have group parameters
  694. * set, for example through mbedtls_ecp_group_load().
  695. * \param P The point to export. This must be initialized.
  696. * \param format The point format. This must be either
  697. * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
  698. * (For groups without these formats, this parameter is
  699. * ignored. But it still has to be either of the above
  700. * values.)
  701. * \param olen The address at which to store the length of
  702. * the output in Bytes. This must not be \c NULL.
  703. * \param buf The output buffer. This must be a writable buffer
  704. * of length \p buflen Bytes.
  705. * \param buflen The length of the output buffer \p buf in Bytes.
  706. *
  707. * \return \c 0 on success.
  708. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
  709. * is too small to hold the point.
  710. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
  711. * or the export for the given group is not implemented.
  712. * \return Another negative error code on other kinds of failure.
  713. */
  714. int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
  715. const mbedtls_ecp_point *P,
  716. int format, size_t *olen,
  717. unsigned char *buf, size_t buflen);
  718. /**
  719. * \brief This function imports a point from unsigned binary data.
  720. *
  721. * \note This function does not check that the point actually
  722. * belongs to the given group, see mbedtls_ecp_check_pubkey()
  723. * for that.
  724. *
  725. * \note For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
  726. * limitations.
  727. *
  728. * \param grp The group to which the point should belong.
  729. * This must be initialized and have group parameters
  730. * set, for example through mbedtls_ecp_group_load().
  731. * \param P The destination context to import the point to.
  732. * This must be initialized.
  733. * \param buf The input buffer. This must be a readable buffer
  734. * of length \p ilen Bytes.
  735. * \param ilen The length of the input buffer \p buf in Bytes.
  736. *
  737. * \return \c 0 on success.
  738. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
  739. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  740. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the
  741. * given group is not implemented.
  742. */
  743. int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
  744. mbedtls_ecp_point *P,
  745. const unsigned char *buf, size_t ilen);
  746. /**
  747. * \brief This function imports a point from a TLS ECPoint record.
  748. *
  749. * \note On function return, \p *buf is updated to point immediately
  750. * after the ECPoint record.
  751. *
  752. * \param grp The ECP group to use.
  753. * This must be initialized and have group parameters
  754. * set, for example through mbedtls_ecp_group_load().
  755. * \param pt The destination point.
  756. * \param buf The address of the pointer to the start of the input buffer.
  757. * \param len The length of the buffer.
  758. *
  759. * \return \c 0 on success.
  760. * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
  761. * failure.
  762. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  763. */
  764. int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
  765. mbedtls_ecp_point *pt,
  766. const unsigned char **buf, size_t len);
  767. /**
  768. * \brief This function exports a point as a TLS ECPoint record
  769. * defined in RFC 4492, Section 5.4.
  770. *
  771. * \param grp The ECP group to use.
  772. * This must be initialized and have group parameters
  773. * set, for example through mbedtls_ecp_group_load().
  774. * \param pt The point to be exported. This must be initialized.
  775. * \param format The point format to use. This must be either
  776. * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
  777. * \param olen The address at which to store the length in Bytes
  778. * of the data written.
  779. * \param buf The target buffer. This must be a writable buffer of
  780. * length \p blen Bytes.
  781. * \param blen The length of the target buffer \p buf in Bytes.
  782. *
  783. * \return \c 0 on success.
  784. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
  785. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
  786. * is too small to hold the exported point.
  787. * \return Another negative error code on other kinds of failure.
  788. */
  789. int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp,
  790. const mbedtls_ecp_point *pt,
  791. int format, size_t *olen,
  792. unsigned char *buf, size_t blen);
  793. /**
  794. * \brief This function sets up an ECP group context
  795. * from a standardized set of domain parameters.
  796. *
  797. * \note The index should be a value of the NamedCurve enum,
  798. * as defined in <em>RFC-4492: Elliptic Curve Cryptography
  799. * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
  800. * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
  801. *
  802. * \param grp The group context to setup. This must be initialized.
  803. * \param id The identifier of the domain parameter set to load.
  804. *
  805. * \return \c 0 on success.
  806. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
  807. * correspond to a known group.
  808. * \return Another negative error code on other kinds of failure.
  809. */
  810. int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id);
  811. /**
  812. * \brief This function sets up an ECP group context from a TLS
  813. * ECParameters record as defined in RFC 4492, Section 5.4.
  814. *
  815. * \note The read pointer \p buf is updated to point right after
  816. * the ECParameters record on exit.
  817. *
  818. * \param grp The group context to setup. This must be initialized.
  819. * \param buf The address of the pointer to the start of the input buffer.
  820. * \param len The length of the input buffer \c *buf in Bytes.
  821. *
  822. * \return \c 0 on success.
  823. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  824. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
  825. * recognized.
  826. * \return Another negative error code on other kinds of failure.
  827. */
  828. int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
  829. const unsigned char **buf, size_t len);
  830. /**
  831. * \brief This function extracts an elliptic curve group ID from a
  832. * TLS ECParameters record as defined in RFC 4492, Section 5.4.
  833. *
  834. * \note The read pointer \p buf is updated to point right after
  835. * the ECParameters record on exit.
  836. *
  837. * \param grp The address at which to store the group id.
  838. * This must not be \c NULL.
  839. * \param buf The address of the pointer to the start of the input buffer.
  840. * \param len The length of the input buffer \c *buf in Bytes.
  841. *
  842. * \return \c 0 on success.
  843. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  844. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
  845. * recognized.
  846. * \return Another negative error code on other kinds of failure.
  847. */
  848. int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
  849. const unsigned char **buf,
  850. size_t len);
  851. /**
  852. * \brief This function exports an elliptic curve as a TLS
  853. * ECParameters record as defined in RFC 4492, Section 5.4.
  854. *
  855. * \param grp The ECP group to be exported.
  856. * This must be initialized and have group parameters
  857. * set, for example through mbedtls_ecp_group_load().
  858. * \param olen The address at which to store the number of Bytes written.
  859. * This must not be \c NULL.
  860. * \param buf The buffer to write to. This must be a writable buffer
  861. * of length \p blen Bytes.
  862. * \param blen The length of the output buffer \p buf in Bytes.
  863. *
  864. * \return \c 0 on success.
  865. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
  866. * buffer is too small to hold the exported group.
  867. * \return Another negative error code on other kinds of failure.
  868. */
  869. int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp,
  870. size_t *olen,
  871. unsigned char *buf, size_t blen);
  872. /**
  873. * \brief This function performs a scalar multiplication of a point
  874. * by an integer: \p R = \p m * \p P.
  875. *
  876. * It is not thread-safe to use same group in multiple threads.
  877. *
  878. * \note To prevent timing attacks, this function
  879. * executes the exact same sequence of base-field
  880. * operations for any valid \p m. It avoids any if-branch or
  881. * array index depending on the value of \p m. It also uses
  882. * \p f_rng to randomize some intermediate results.
  883. *
  884. * \param grp The ECP group to use.
  885. * This must be initialized and have group parameters
  886. * set, for example through mbedtls_ecp_group_load().
  887. * \param R The point in which to store the result of the calculation.
  888. * This must be initialized.
  889. * \param m The integer by which to multiply. This must be initialized.
  890. * \param P The point to multiply. This must be initialized.
  891. * \param f_rng The RNG function. This must not be \c NULL.
  892. * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
  893. * NULL if \p f_rng doesn't need a context.
  894. *
  895. * \return \c 0 on success.
  896. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
  897. * key, or \p P is not a valid public key.
  898. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  899. * \return Another negative error code on other kinds of failure.
  900. */
  901. int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  902. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  903. mbedtls_f_rng_t *f_rng, void *p_rng);
  904. /**
  905. * \brief This function performs multiplication of a point by
  906. * an integer: \p R = \p m * \p P in a restartable way.
  907. *
  908. * \see mbedtls_ecp_mul()
  909. *
  910. * \note This function does the same as \c mbedtls_ecp_mul(), but
  911. * it can return early and restart according to the limit set
  912. * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  913. *
  914. * \param grp The ECP group to use.
  915. * This must be initialized and have group parameters
  916. * set, for example through mbedtls_ecp_group_load().
  917. * \param R The point in which to store the result of the calculation.
  918. * This must be initialized.
  919. * \param m The integer by which to multiply. This must be initialized.
  920. * \param P The point to multiply. This must be initialized.
  921. * \param f_rng The RNG function. This must not be \c NULL.
  922. * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
  923. * NULL if \p f_rng doesn't need a context.
  924. * \param rs_ctx The restart context (NULL disables restart).
  925. *
  926. * \return \c 0 on success.
  927. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
  928. * key, or \p P is not a valid public key.
  929. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  930. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  931. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  932. * \return Another negative error code on other kinds of failure.
  933. */
  934. int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  935. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  936. mbedtls_f_rng_t *f_rng, void *p_rng,
  937. mbedtls_ecp_restart_ctx *rs_ctx);
  938. #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
  939. /**
  940. * \brief This function checks if domain parameter A of the curve is
  941. * \c -3.
  942. *
  943. * \note This function is only defined for short Weierstrass curves.
  944. * It may not be included in builds without any short
  945. * Weierstrass curve.
  946. *
  947. * \param grp The ECP group to use.
  948. * This must be initialized and have group parameters
  949. * set, for example through mbedtls_ecp_group_load().
  950. *
  951. * \return \c 1 if <code>A = -3</code>.
  952. * \return \c 0 Otherwise.
  953. */
  954. static inline int mbedtls_ecp_group_a_is_minus_3(const mbedtls_ecp_group *grp)
  955. {
  956. return grp->A.MBEDTLS_PRIVATE(p) == NULL;
  957. }
  958. /**
  959. * \brief This function performs multiplication and addition of two
  960. * points by integers: \p R = \p m * \p P + \p n * \p Q
  961. *
  962. * It is not thread-safe to use same group in multiple threads.
  963. *
  964. * \note In contrast to mbedtls_ecp_mul(), this function does not
  965. * guarantee a constant execution flow and timing.
  966. *
  967. * \note This function is only defined for short Weierstrass curves.
  968. * It may not be included in builds without any short
  969. * Weierstrass curve.
  970. *
  971. * \param grp The ECP group to use.
  972. * This must be initialized and have group parameters
  973. * set, for example through mbedtls_ecp_group_load().
  974. * \param R The point in which to store the result of the calculation.
  975. * This must be initialized.
  976. * \param m The integer by which to multiply \p P.
  977. * This must be initialized.
  978. * \param P The point to multiply by \p m. This must be initialized.
  979. * \param n The integer by which to multiply \p Q.
  980. * This must be initialized.
  981. * \param Q The point to be multiplied by \p n.
  982. * This must be initialized.
  983. *
  984. * \return \c 0 on success.
  985. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
  986. * valid private keys, or \p P or \p Q are not valid public
  987. * keys.
  988. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  989. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
  990. * designate a short Weierstrass curve.
  991. * \return Another negative error code on other kinds of failure.
  992. */
  993. int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  994. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  995. const mbedtls_mpi *n, const mbedtls_ecp_point *Q);
  996. /**
  997. * \brief This function performs multiplication and addition of two
  998. * points by integers: \p R = \p m * \p P + \p n * \p Q in a
  999. * restartable way.
  1000. *
  1001. * \see \c mbedtls_ecp_muladd()
  1002. *
  1003. * \note This function works the same as \c mbedtls_ecp_muladd(),
  1004. * but it can return early and restart according to the limit
  1005. * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  1006. *
  1007. * \note This function is only defined for short Weierstrass curves.
  1008. * It may not be included in builds without any short
  1009. * Weierstrass curve.
  1010. *
  1011. * \param grp The ECP group to use.
  1012. * This must be initialized and have group parameters
  1013. * set, for example through mbedtls_ecp_group_load().
  1014. * \param R The point in which to store the result of the calculation.
  1015. * This must be initialized.
  1016. * \param m The integer by which to multiply \p P.
  1017. * This must be initialized.
  1018. * \param P The point to multiply by \p m. This must be initialized.
  1019. * \param n The integer by which to multiply \p Q.
  1020. * This must be initialized.
  1021. * \param Q The point to be multiplied by \p n.
  1022. * This must be initialized.
  1023. * \param rs_ctx The restart context (NULL disables restart).
  1024. *
  1025. * \return \c 0 on success.
  1026. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
  1027. * valid private keys, or \p P or \p Q are not valid public
  1028. * keys.
  1029. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  1030. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
  1031. * designate a short Weierstrass curve.
  1032. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  1033. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  1034. * \return Another negative error code on other kinds of failure.
  1035. */
  1036. int mbedtls_ecp_muladd_restartable(
  1037. mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  1038. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  1039. const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
  1040. mbedtls_ecp_restart_ctx *rs_ctx);
  1041. #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
  1042. /**
  1043. * \brief This function checks that a point is a valid public key
  1044. * on this curve.
  1045. *
  1046. * It only checks that the point is non-zero, has
  1047. * valid coordinates and lies on the curve. It does not verify
  1048. * that it is indeed a multiple of \c G. This additional
  1049. * check is computationally more expensive, is not required
  1050. * by standards, and should not be necessary if the group
  1051. * used has a small cofactor. In particular, it is useless for
  1052. * the NIST groups which all have a cofactor of 1.
  1053. *
  1054. * \note This function uses bare components rather than an
  1055. * ::mbedtls_ecp_keypair structure, to ease use with other
  1056. * structures, such as ::mbedtls_ecdh_context or
  1057. * ::mbedtls_ecdsa_context.
  1058. *
  1059. * \param grp The ECP group the point should belong to.
  1060. * This must be initialized and have group parameters
  1061. * set, for example through mbedtls_ecp_group_load().
  1062. * \param pt The point to check. This must be initialized.
  1063. *
  1064. * \return \c 0 if the point is a valid public key.
  1065. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
  1066. * a valid public key for the given curve.
  1067. * \return Another negative error code on other kinds of failure.
  1068. */
  1069. int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
  1070. const mbedtls_ecp_point *pt);
  1071. /**
  1072. * \brief This function checks that an \c mbedtls_mpi is a
  1073. * valid private key for this curve.
  1074. *
  1075. * \note This function uses bare components rather than an
  1076. * ::mbedtls_ecp_keypair structure to ease use with other
  1077. * structures, such as ::mbedtls_ecdh_context or
  1078. * ::mbedtls_ecdsa_context.
  1079. *
  1080. * \param grp The ECP group the private key should belong to.
  1081. * This must be initialized and have group parameters
  1082. * set, for example through mbedtls_ecp_group_load().
  1083. * \param d The integer to check. This must be initialized.
  1084. *
  1085. * \return \c 0 if the point is a valid private key.
  1086. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
  1087. * private key for the given curve.
  1088. * \return Another negative error code on other kinds of failure.
  1089. */
  1090. int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
  1091. const mbedtls_mpi *d);
  1092. /**
  1093. * \brief This function generates a private key.
  1094. *
  1095. * \param grp The ECP group to generate a private key for.
  1096. * This must be initialized and have group parameters
  1097. * set, for example through mbedtls_ecp_group_load().
  1098. * \param d The destination MPI (secret part). This must be initialized.
  1099. * \param f_rng The RNG function. This must not be \c NULL.
  1100. * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
  1101. * \c NULL if \p f_rng doesn't need a context argument.
  1102. *
  1103. * \return \c 0 on success.
  1104. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  1105. * on failure.
  1106. */
  1107. int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
  1108. mbedtls_mpi *d,
  1109. mbedtls_f_rng_t *f_rng,
  1110. void *p_rng);
  1111. /**
  1112. * \brief This function generates a keypair with a configurable base
  1113. * point.
  1114. *
  1115. * \note This function uses bare components rather than an
  1116. * ::mbedtls_ecp_keypair structure to ease use with other
  1117. * structures, such as ::mbedtls_ecdh_context or
  1118. * ::mbedtls_ecdsa_context.
  1119. *
  1120. * \param grp The ECP group to generate a key pair for.
  1121. * This must be initialized and have group parameters
  1122. * set, for example through mbedtls_ecp_group_load().
  1123. * \param G The base point to use. This must be initialized
  1124. * and belong to \p grp. It replaces the default base
  1125. * point \c grp->G used by mbedtls_ecp_gen_keypair().
  1126. * \param d The destination MPI (secret part).
  1127. * This must be initialized.
  1128. * \param Q The destination point (public part).
  1129. * This must be initialized.
  1130. * \param f_rng The RNG function. This must not be \c NULL.
  1131. * \param p_rng The RNG context to be passed to \p f_rng. This may
  1132. * be \c NULL if \p f_rng doesn't need a context argument.
  1133. *
  1134. * \return \c 0 on success.
  1135. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  1136. * on failure.
  1137. */
  1138. int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
  1139. const mbedtls_ecp_point *G,
  1140. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  1141. mbedtls_f_rng_t *f_rng,
  1142. void *p_rng);
  1143. /**
  1144. * \brief This function generates an ECP keypair.
  1145. *
  1146. * \note This function uses bare components rather than an
  1147. * ::mbedtls_ecp_keypair structure to ease use with other
  1148. * structures, such as ::mbedtls_ecdh_context or
  1149. * ::mbedtls_ecdsa_context.
  1150. *
  1151. * \param grp The ECP group to generate a key pair for.
  1152. * This must be initialized and have group parameters
  1153. * set, for example through mbedtls_ecp_group_load().
  1154. * \param d The destination MPI (secret part).
  1155. * This must be initialized.
  1156. * \param Q The destination point (public part).
  1157. * This must be initialized.
  1158. * \param f_rng The RNG function. This must not be \c NULL.
  1159. * \param p_rng The RNG context to be passed to \p f_rng. This may
  1160. * be \c NULL if \p f_rng doesn't need a context argument.
  1161. *
  1162. * \return \c 0 on success.
  1163. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  1164. * on failure.
  1165. */
  1166. int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d,
  1167. mbedtls_ecp_point *Q,
  1168. mbedtls_f_rng_t *f_rng,
  1169. void *p_rng);
  1170. /**
  1171. * \brief This function generates an ECP key.
  1172. *
  1173. * \param grp_id The ECP group identifier.
  1174. * \param key The destination key. This must be initialized.
  1175. * \param f_rng The RNG function to use. This must not be \c NULL.
  1176. * \param p_rng The RNG context to be passed to \p f_rng. This may
  1177. * be \c NULL if \p f_rng doesn't need a context argument.
  1178. *
  1179. * \return \c 0 on success.
  1180. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  1181. * on failure.
  1182. */
  1183. int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  1184. mbedtls_f_rng_t *f_rng,
  1185. void *p_rng);
  1186. /** \brief Set the public key in a key pair object.
  1187. *
  1188. * \note This function does not check that the point actually
  1189. * belongs to the given group. Call mbedtls_ecp_check_pubkey()
  1190. * on \p Q before calling this function to check that.
  1191. *
  1192. * \note This function does not check that the public key matches
  1193. * the private key that is already in \p key, if any.
  1194. * To check the consistency of the resulting key pair object,
  1195. * call mbedtls_ecp_check_pub_priv() after setting both
  1196. * the public key and the private key.
  1197. *
  1198. * \param grp_id The ECP group identifier.
  1199. * \param key The key pair object. It must be initialized.
  1200. * If its group has already been set, it must match \p grp_id.
  1201. * If its group has not been set, it will be set to \p grp_id.
  1202. * If the public key has already been set, it is overwritten.
  1203. * \param Q The public key to copy. This must be a point on the
  1204. * curve indicated by \p grp_id.
  1205. *
  1206. * \return \c 0 on success.
  1207. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p key does not
  1208. * match \p grp_id.
  1209. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
  1210. * the group is not implemented.
  1211. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  1212. * \return Another negative error code on other kinds of failure.
  1213. */
  1214. int mbedtls_ecp_set_public_key(mbedtls_ecp_group_id grp_id,
  1215. mbedtls_ecp_keypair *key,
  1216. const mbedtls_ecp_point *Q);
  1217. /**
  1218. * \brief This function reads an elliptic curve private key.
  1219. *
  1220. * \note This function does not set the public key in the
  1221. * key pair object. Without a public key, the key pair object
  1222. * cannot be used with operations that require the public key.
  1223. * Call mbedtls_ecp_keypair_calc_public() to set the public
  1224. * key from the private key. Alternatively, you can call
  1225. * mbedtls_ecp_set_public_key() to set the public key part,
  1226. * and then optionally mbedtls_ecp_check_pub_priv() to check
  1227. * that the private and public parts are consistent.
  1228. *
  1229. * \note If a public key has already been set in the key pair
  1230. * object, this function does not check that it is consistent
  1231. * with the private key. Call mbedtls_ecp_check_pub_priv()
  1232. * after setting both the public key and the private key
  1233. * to make that check.
  1234. *
  1235. * \param grp_id The ECP group identifier.
  1236. * \param key The destination key.
  1237. * \param buf The buffer containing the binary representation of the
  1238. * key. (Big endian integer for Weierstrass curves, byte
  1239. * string for Montgomery curves.)
  1240. * \param buflen The length of the buffer in bytes.
  1241. *
  1242. * \return \c 0 on success.
  1243. * \return #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is
  1244. * invalid.
  1245. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
  1246. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
  1247. * the group is not implemented.
  1248. * \return Another negative error code on different kinds of failure.
  1249. */
  1250. int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  1251. const unsigned char *buf, size_t buflen);
  1252. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  1253. /**
  1254. * \brief This function exports an elliptic curve private key.
  1255. *
  1256. * \deprecated Note that although this function accepts an output
  1257. * buffer that is smaller or larger than the key, most key
  1258. * import interfaces require the output to have exactly
  1259. * key's nominal length. It is generally simplest to
  1260. * pass the key's nominal length as \c buflen, after
  1261. * checking that the output buffer is large enough.
  1262. * See the description of the \p buflen parameter for
  1263. * how to calculate the nominal length.
  1264. * To avoid this difficulty, use mbedtls_ecp_write_key_ext()
  1265. * instead.
  1266. * mbedtls_ecp_write_key() is deprecated and will be
  1267. * removed in a future version of the library.
  1268. *
  1269. * \note If the private key was not set in \p key,
  1270. * the output is unspecified. Future versions
  1271. * may return an error in that case.
  1272. *
  1273. * \param key The private key.
  1274. * \param buf The output buffer for containing the binary representation
  1275. * of the key.
  1276. * For Weierstrass curves, this is the big-endian
  1277. * representation, padded with null bytes at the beginning
  1278. * to reach \p buflen bytes.
  1279. * For Montgomery curves, this is the standard byte string
  1280. * representation (which is little-endian), padded with
  1281. * null bytes at the end to reach \p buflen bytes.
  1282. * \param buflen The total length of the buffer in bytes.
  1283. * The length of the output is
  1284. * (`grp->nbits` + 7) / 8 bytes
  1285. * where `grp->nbits` is the private key size in bits.
  1286. * For Weierstrass keys, if the output buffer is smaller,
  1287. * leading zeros are trimmed to fit if possible. For
  1288. * Montgomery keys, the output buffer must always be large
  1289. * enough for the nominal length.
  1290. *
  1291. * \return \c 0 on success.
  1292. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL or
  1293. * #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the \p key
  1294. * representation is larger than the available space in \p buf.
  1295. * \return Another negative error code on different kinds of failure.
  1296. */
  1297. int MBEDTLS_DEPRECATED mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
  1298. unsigned char *buf, size_t buflen);
  1299. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  1300. /**
  1301. * \brief This function exports an elliptic curve private key.
  1302. *
  1303. * \param key The private key.
  1304. * \param olen On success, the length of the private key.
  1305. * This is always (`grp->nbits` + 7) / 8 bytes
  1306. * where `grp->nbits` is the private key size in bits.
  1307. * \param buf The output buffer for containing the binary representation
  1308. * of the key.
  1309. * \param buflen The total length of the buffer in bytes.
  1310. * #MBEDTLS_ECP_MAX_BYTES is always sufficient.
  1311. *
  1312. * \return \c 0 on success.
  1313. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key
  1314. * representation is larger than the available space in \p buf.
  1315. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if no private key is
  1316. * set in \p key.
  1317. * \return Another negative error code on different kinds of failure.
  1318. */
  1319. int mbedtls_ecp_write_key_ext(const mbedtls_ecp_keypair *key,
  1320. size_t *olen, unsigned char *buf, size_t buflen);
  1321. /**
  1322. * \brief This function exports an elliptic curve public key.
  1323. *
  1324. * \note If the public key was not set in \p key,
  1325. * the output is unspecified. Future versions
  1326. * may return an error in that case.
  1327. *
  1328. * \param key The public key.
  1329. * \param format The point format. This must be either
  1330. * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
  1331. * (For groups without these formats, this parameter is
  1332. * ignored. But it still has to be either of the above
  1333. * values.)
  1334. * \param olen The address at which to store the length of
  1335. * the output in Bytes. This must not be \c NULL.
  1336. * \param buf The output buffer. This must be a writable buffer
  1337. * of length \p buflen Bytes.
  1338. * \param buflen The length of the output buffer \p buf in Bytes.
  1339. *
  1340. * \return \c 0 on success.
  1341. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
  1342. * is too small to hold the point.
  1343. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
  1344. * or the export for the given group is not implemented.
  1345. * \return Another negative error code on other kinds of failure.
  1346. */
  1347. int mbedtls_ecp_write_public_key(const mbedtls_ecp_keypair *key,
  1348. int format, size_t *olen,
  1349. unsigned char *buf, size_t buflen);
  1350. /**
  1351. * \brief This function checks that the keypair objects
  1352. * \p pub and \p prv have the same group and the
  1353. * same public point, and that the private key in
  1354. * \p prv is consistent with the public key.
  1355. *
  1356. * \param pub The keypair structure holding the public key. This
  1357. * must be initialized. If it contains a private key, that
  1358. * part is ignored.
  1359. * \param prv The keypair structure holding the full keypair.
  1360. * This must be initialized.
  1361. * \param f_rng The RNG function. This must not be \c NULL.
  1362. * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
  1363. * NULL if \p f_rng doesn't need a context.
  1364. *
  1365. * \return \c 0 on success, meaning that the keys are valid and match.
  1366. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
  1367. * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
  1368. * error code on calculation failure.
  1369. */
  1370. int mbedtls_ecp_check_pub_priv(
  1371. const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
  1372. mbedtls_f_rng_t *f_rng, void *p_rng);
  1373. /** \brief Calculate the public key from a private key in a key pair.
  1374. *
  1375. * \param key A keypair structure. It must have a private key set.
  1376. * If the public key is set, it will be overwritten.
  1377. * \param f_rng The RNG function. This must not be \c NULL.
  1378. * \param p_rng The RNG context to be passed to \p f_rng. This may be \c
  1379. * NULL if \p f_rng doesn't need a context.
  1380. *
  1381. * \return \c 0 on success. The key pair object can be used for
  1382. * operations that require the public key.
  1383. * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
  1384. * error code on calculation failure.
  1385. */
  1386. int mbedtls_ecp_keypair_calc_public(
  1387. mbedtls_ecp_keypair *key,
  1388. mbedtls_f_rng_t *f_rng, void *p_rng);
  1389. /** \brief Query the group that a key pair belongs to.
  1390. *
  1391. * \param key The key pair to query.
  1392. *
  1393. * \return The group ID for the group registered in the key pair
  1394. * object.
  1395. * This is \c MBEDTLS_ECP_DP_NONE if no group has been set
  1396. * in the key pair object.
  1397. */
  1398. mbedtls_ecp_group_id mbedtls_ecp_keypair_get_group_id(
  1399. const mbedtls_ecp_keypair *key);
  1400. /**
  1401. * \brief This function exports generic key-pair parameters.
  1402. *
  1403. * Each of the output parameters can be a null pointer
  1404. * if you do not need that parameter.
  1405. *
  1406. * \note If the private key or the public key was not set in \p key,
  1407. * the corresponding output is unspecified. Future versions
  1408. * may return an error in that case.
  1409. *
  1410. * \param key The key pair to export from.
  1411. * \param grp Slot for exported ECP group.
  1412. * It must either be null or point to an initialized ECP group.
  1413. * \param d Slot for the exported secret value.
  1414. * It must either be null or point to an initialized mpi.
  1415. * \param Q Slot for the exported public value.
  1416. * It must either be null or point to an initialized ECP point.
  1417. *
  1418. * \return \c 0 on success,
  1419. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  1420. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if key id doesn't
  1421. * correspond to a known group.
  1422. * \return Another negative error code on other kinds of failure.
  1423. */
  1424. int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
  1425. mbedtls_mpi *d, mbedtls_ecp_point *Q);
  1426. #if defined(MBEDTLS_SELF_TEST)
  1427. /**
  1428. * \brief The ECP checkup routine.
  1429. *
  1430. * \return \c 0 on success.
  1431. * \return \c 1 on failure.
  1432. */
  1433. int mbedtls_ecp_self_test(int verbose);
  1434. #endif /* MBEDTLS_SELF_TEST */
  1435. #ifdef __cplusplus
  1436. }
  1437. #endif
  1438. #endif /* ecp.h */