ecp.h 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  18. * SPDX-License-Identifier: Apache-2.0
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  21. * not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  28. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. *
  32. * This file is part of Mbed TLS (https://tls.mbed.org)
  33. */
  34. #ifndef MBEDTLS_ECP_H
  35. #define MBEDTLS_ECP_H
  36. #if !defined(MBEDTLS_CONFIG_FILE)
  37. #include "config.h"
  38. #else
  39. #include MBEDTLS_CONFIG_FILE
  40. #endif
  41. #include "bignum.h"
  42. /*
  43. * ECP error codes
  44. */
  45. #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
  46. #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
  47. #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */
  48. #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
  49. #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
  50. #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */
  51. #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
  52. #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
  53. /* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
  54. #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
  55. #define MBEDTLS_ERR_ECP_IN_PROGRESS -0x4B00 /**< Operation in progress, call again with the same parameters to continue. */
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /**
  60. * Domain-parameter identifiers: curve, subgroup, and generator.
  61. *
  62. * \note Only curves over prime fields are supported.
  63. *
  64. * \warning This library does not support validation of arbitrary domain
  65. * parameters. Therefore, only standardized domain parameters from trusted
  66. * sources should be used. See mbedtls_ecp_group_load().
  67. */
  68. typedef enum
  69. {
  70. MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
  71. MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
  72. MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
  73. MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
  74. MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
  75. MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
  76. MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
  77. MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
  78. MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
  79. MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
  80. MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
  81. MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
  82. MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
  83. MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
  84. } mbedtls_ecp_group_id;
  85. /**
  86. * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
  87. *
  88. * \note Montgomery curves are currently excluded.
  89. */
  90. #define MBEDTLS_ECP_DP_MAX 12
  91. /**
  92. * Curve information, for use by other modules.
  93. */
  94. typedef struct mbedtls_ecp_curve_info
  95. {
  96. mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
  97. uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
  98. uint16_t bit_size; /*!< The curve size in bits. */
  99. const char *name; /*!< A human-friendly name. */
  100. } mbedtls_ecp_curve_info;
  101. /**
  102. * \brief The ECP point structure, in Jacobian coordinates.
  103. *
  104. * \note All functions expect and return points satisfying
  105. * the following condition: <code>Z == 0</code> or
  106. * <code>Z == 1</code>. Other values of \p Z are
  107. * used only by internal functions.
  108. * The point is zero, or "at infinity", if <code>Z == 0</code>.
  109. * Otherwise, \p X and \p Y are its standard (affine)
  110. * coordinates.
  111. */
  112. typedef struct mbedtls_ecp_point
  113. {
  114. mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
  115. mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
  116. mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
  117. }
  118. mbedtls_ecp_point;
  119. #if !defined(MBEDTLS_ECP_ALT)
  120. /*
  121. * default mbed TLS elliptic curve arithmetic implementation
  122. *
  123. * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
  124. * alternative implementation for the whole module and it will replace this
  125. * one.)
  126. */
  127. /**
  128. * \brief The ECP group structure.
  129. *
  130. * We consider two types of curve equations:
  131. * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
  132. * (SEC1 + RFC-4492)</li>
  133. * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
  134. * Curve448)</li></ul>
  135. * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
  136. *
  137. * For Short Weierstrass, this subgroup is the whole curve, and its
  138. * cardinality is denoted by \p N. Our code requires that \p N is an
  139. * odd prime as mbedtls_ecp_mul() requires an odd number, and
  140. * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
  141. *
  142. * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
  143. * which is the quantity used in the formulas. Additionally, \p nbits is
  144. * not the size of \p N but the required size for private keys.
  145. *
  146. * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
  147. * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
  148. * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
  149. * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
  150. * in size, so that it may be efficiently brought in the 0..P-1 range by a few
  151. * additions or subtractions. Therefore, it is only an approximative modular
  152. * reduction. It must return 0 on success and non-zero on failure.
  153. *
  154. * \note Alternative implementations must keep the group IDs distinct. If
  155. * two group structures have the same ID, then they must be
  156. * identical.
  157. *
  158. */
  159. typedef struct mbedtls_ecp_group
  160. {
  161. mbedtls_ecp_group_id id; /*!< An internal group identifier. */
  162. mbedtls_mpi P; /*!< The prime modulus of the base field. */
  163. mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For
  164. Montgomery curves: <code>(A + 2) / 4</code>. */
  165. mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
  166. For Montgomery curves: unused. */
  167. mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
  168. mbedtls_mpi N; /*!< The order of \p G. */
  169. size_t pbits; /*!< The number of bits in \p P.*/
  170. size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
  171. For Montgomery curves: the number of bits in the
  172. private keys. */
  173. unsigned int h; /*!< \internal 1 if the constants are static. */
  174. int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
  175. mod \p P (see above).*/
  176. int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
  177. int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
  178. void *t_data; /*!< Unused. */
  179. mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
  180. size_t T_size; /*!< The number of pre-computed points. */
  181. }
  182. mbedtls_ecp_group;
  183. /**
  184. * \name SECTION: Module settings
  185. *
  186. * The configuration options you can set for this module are in this section.
  187. * Either change them in config.h, or define them using the compiler command line.
  188. * \{
  189. */
  190. #if !defined(MBEDTLS_ECP_MAX_BITS)
  191. /**
  192. * The maximum size of the groups, that is, of \c N and \c P.
  193. */
  194. #define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
  195. #endif
  196. #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
  197. #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
  198. #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
  199. /*
  200. * Maximum "window" size used for point multiplication.
  201. * Default: 6.
  202. * Minimum value: 2. Maximum value: 7.
  203. *
  204. * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  205. * points used for point multiplication. This value is directly tied to EC
  206. * peak memory usage, so decreasing it by one should roughly cut memory usage
  207. * by two (if large curves are in use).
  208. *
  209. * Reduction in size may reduce speed, but larger curves are impacted first.
  210. * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
  211. * w-size: 6 5 4 3 2
  212. * 521 145 141 135 120 97
  213. * 384 214 209 198 177 146
  214. * 256 320 320 303 262 226
  215. * 224 475 475 453 398 342
  216. * 192 640 640 633 587 476
  217. */
  218. #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
  219. #endif /* MBEDTLS_ECP_WINDOW_SIZE */
  220. #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
  221. /*
  222. * Trade memory for speed on fixed-point multiplication.
  223. *
  224. * This speeds up repeated multiplication of the generator (that is, the
  225. * multiplication in ECDSA signatures, and half of the multiplications in
  226. * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
  227. *
  228. * The cost is increasing EC peak memory usage by a factor roughly 2.
  229. *
  230. * Change this value to 0 to reduce peak memory usage.
  231. */
  232. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
  233. #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
  234. /* \} name SECTION: Module settings */
  235. #else /* MBEDTLS_ECP_ALT */
  236. #include "ecp_alt.h"
  237. #endif /* MBEDTLS_ECP_ALT */
  238. #if defined(MBEDTLS_ECP_RESTARTABLE)
  239. /**
  240. * \brief Internal restart context for multiplication
  241. *
  242. * \note Opaque struct
  243. */
  244. typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
  245. /**
  246. * \brief Internal restart context for ecp_muladd()
  247. *
  248. * \note Opaque struct
  249. */
  250. typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
  251. /**
  252. * \brief General context for resuming ECC operations
  253. */
  254. typedef struct
  255. {
  256. unsigned ops_done; /*!< current ops count */
  257. unsigned depth; /*!< call depth (0 = top-level) */
  258. mbedtls_ecp_restart_mul_ctx *rsm; /*!< ecp_mul_comb() sub-context */
  259. mbedtls_ecp_restart_muladd_ctx *ma; /*!< ecp_muladd() sub-context */
  260. } mbedtls_ecp_restart_ctx;
  261. /*
  262. * Operation counts for restartable functions
  263. */
  264. #define MBEDTLS_ECP_OPS_CHK 3 /*!< basic ops count for ecp_check_pubkey() */
  265. #define MBEDTLS_ECP_OPS_DBL 8 /*!< basic ops count for ecp_double_jac() */
  266. #define MBEDTLS_ECP_OPS_ADD 11 /*!< basic ops count for see ecp_add_mixed() */
  267. #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv() */
  268. /**
  269. * \brief Internal; for restartable functions in other modules.
  270. * Check and update basic ops budget.
  271. *
  272. * \param grp Group structure
  273. * \param rs_ctx Restart context
  274. * \param ops Number of basic ops to do
  275. *
  276. * \return \c 0 if doing \p ops basic ops is still allowed,
  277. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
  278. */
  279. int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
  280. mbedtls_ecp_restart_ctx *rs_ctx,
  281. unsigned ops );
  282. /* Utility macro for checking and updating ops budget */
  283. #define MBEDTLS_ECP_BUDGET( ops ) \
  284. MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
  285. (unsigned) (ops) ) );
  286. #else /* MBEDTLS_ECP_RESTARTABLE */
  287. #define MBEDTLS_ECP_BUDGET( ops ) /* no-op; for compatibility */
  288. /* We want to declare restartable versions of existing functions anyway */
  289. typedef void mbedtls_ecp_restart_ctx;
  290. #endif /* MBEDTLS_ECP_RESTARTABLE */
  291. /**
  292. * \brief The ECP key-pair structure.
  293. *
  294. * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
  295. *
  296. * \note Members are deliberately in the same order as in the
  297. * ::mbedtls_ecdsa_context structure.
  298. */
  299. typedef struct mbedtls_ecp_keypair
  300. {
  301. mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
  302. mbedtls_mpi d; /*!< our secret value */
  303. mbedtls_ecp_point Q; /*!< our public value */
  304. }
  305. mbedtls_ecp_keypair;
  306. /*
  307. * Point formats, from RFC 4492's enum ECPointFormat
  308. */
  309. #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
  310. #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
  311. /*
  312. * Some other constants from RFC 4492
  313. */
  314. #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
  315. #if defined(MBEDTLS_ECP_RESTARTABLE)
  316. /**
  317. * \brief Set the maximum number of basic operations done in a row.
  318. *
  319. * If more operations are needed to complete a computation,
  320. * #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
  321. * function performing the computation. It is then the
  322. * caller's responsibility to either call again with the same
  323. * parameters until it returns 0 or an error code; or to free
  324. * the restart context if the operation is to be aborted.
  325. *
  326. * It is strictly required that all input parameters and the
  327. * restart context be the same on successive calls for the
  328. * same operation, but output parameters need not be the
  329. * same; they must not be used until the function finally
  330. * returns 0.
  331. *
  332. * This only applies to functions whose documentation
  333. * mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
  334. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
  335. * SSL module). For functions that accept a "restart context"
  336. * argument, passing NULL disables restart and makes the
  337. * function equivalent to the function with the same name
  338. * with \c _restartable removed. For functions in the ECDH
  339. * module, restart is disabled unless the function accepts
  340. * an "ECDH context" argument and
  341. * mbedtls_ecdh_enable_restart() was previously called on
  342. * that context. For function in the SSL module, restart is
  343. * only enabled for specific sides and key exchanges
  344. * (currently only for clients and ECDHE-ECDSA).
  345. *
  346. * \param max_ops Maximum number of basic operations done in a row.
  347. * Default: 0 (unlimited).
  348. * Lower (non-zero) values mean ECC functions will block for
  349. * a lesser maximum amount of time.
  350. *
  351. * \note A "basic operation" is defined as a rough equivalent of a
  352. * multiplication in GF(p) for the NIST P-256 curve.
  353. * As an indication, with default settings, a scalar
  354. * multiplication (full run of \c mbedtls_ecp_mul()) is:
  355. * - about 3300 basic operations for P-256
  356. * - about 9400 basic operations for P-384
  357. *
  358. * \note Very low values are not always respected: sometimes
  359. * functions need to block for a minimum number of
  360. * operations, and will do so even if max_ops is set to a
  361. * lower value. That minimum depends on the curve size, and
  362. * can be made lower by decreasing the value of
  363. * \c MBEDTLS_ECP_WINDOW_SIZE. As an indication, here is the
  364. * lowest effective value for various curves and values of
  365. * that parameter (w for short):
  366. * w=6 w=5 w=4 w=3 w=2
  367. * P-256 208 208 160 136 124
  368. * P-384 682 416 320 272 248
  369. * P-521 1364 832 640 544 496
  370. *
  371. * \note This setting is currently ignored by Curve25519.
  372. */
  373. void mbedtls_ecp_set_max_ops( unsigned max_ops );
  374. /**
  375. * \brief Check if restart is enabled (max_ops != 0)
  376. *
  377. * \return \c 0 if \c max_ops == 0 (restart disabled)
  378. * \return \c 1 otherwise (restart enabled)
  379. */
  380. int mbedtls_ecp_restart_is_enabled( void );
  381. #endif /* MBEDTLS_ECP_RESTARTABLE */
  382. /**
  383. * \brief This function retrieves the information defined in
  384. * mbedtls_ecp_curve_info() for all supported curves in order
  385. * of preference.
  386. *
  387. * \return A statically allocated array. The last entry is 0.
  388. */
  389. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
  390. /**
  391. * \brief This function retrieves the list of internal group
  392. * identifiers of all supported curves in the order of
  393. * preference.
  394. *
  395. * \return A statically allocated array,
  396. * terminated with MBEDTLS_ECP_DP_NONE.
  397. */
  398. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
  399. /**
  400. * \brief This function retrieves curve information from an internal
  401. * group identifier.
  402. *
  403. * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
  404. *
  405. * \return The associated curve information on success.
  406. * \return NULL on failure.
  407. */
  408. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
  409. /**
  410. * \brief This function retrieves curve information from a TLS
  411. * NamedCurve value.
  412. *
  413. * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
  414. *
  415. * \return The associated curve information on success.
  416. * \return NULL on failure.
  417. */
  418. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
  419. /**
  420. * \brief This function retrieves curve information from a
  421. * human-readable name.
  422. *
  423. * \param name The human-readable name.
  424. *
  425. * \return The associated curve information on success.
  426. * \return NULL on failure.
  427. */
  428. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
  429. /**
  430. * \brief This function initializes a point as zero.
  431. *
  432. * \param pt The point to initialize.
  433. */
  434. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
  435. /**
  436. * \brief This function initializes an ECP group context
  437. * without loading any domain parameters.
  438. *
  439. * \note After this function is called, domain parameters
  440. * for various ECP groups can be loaded through the
  441. * mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
  442. * functions.
  443. */
  444. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
  445. /**
  446. * \brief This function initializes a key pair as an invalid one.
  447. *
  448. * \param key The key pair to initialize.
  449. */
  450. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
  451. /**
  452. * \brief This function frees the components of a point.
  453. *
  454. * \param pt The point to free.
  455. */
  456. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
  457. /**
  458. * \brief This function frees the components of an ECP group.
  459. *
  460. * \param grp The group to free. This may be \c NULL, in which
  461. * case this function returns immediately. If it is not
  462. * \c NULL, it must point to an initialized ECP group.
  463. */
  464. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
  465. /**
  466. * \brief This function frees the components of a key pair.
  467. *
  468. * \param key The key pair to free. This may be \c NULL, in which
  469. * case this function returns immediately. If it is not
  470. * \c NULL, it must point to an initialized ECP key pair.
  471. */
  472. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
  473. #if defined(MBEDTLS_ECP_RESTARTABLE)
  474. /**
  475. * \brief Initialize a restart context.
  476. *
  477. * \param ctx The restart context to initialize. This must
  478. * not be \c NULL.
  479. */
  480. void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );
  481. /**
  482. * \brief Free the components of a restart context.
  483. *
  484. * \param ctx The restart context to free. This may be \c NULL, in which
  485. * case this function returns immediately. If it is not
  486. * \c NULL, it must point to an initialized restart context.
  487. */
  488. void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
  489. #endif /* MBEDTLS_ECP_RESTARTABLE */
  490. /**
  491. * \brief This function copies the contents of point \p Q into
  492. * point \p P.
  493. *
  494. * \param P The destination point. This must be initialized.
  495. * \param Q The source point. This must be initialized.
  496. *
  497. * \return \c 0 on success.
  498. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  499. * \return Another negative error code for other kinds of failure.
  500. */
  501. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
  502. /**
  503. * \brief This function copies the contents of group \p src into
  504. * group \p dst.
  505. *
  506. * \param dst The destination group. This must be initialized.
  507. * \param src The source group. This must be initialized.
  508. *
  509. * \return \c 0 on success.
  510. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  511. * \return Another negative error code on other kinds of failure.
  512. */
  513. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst,
  514. const mbedtls_ecp_group *src );
  515. /**
  516. * \brief This function sets a point to the point at infinity.
  517. *
  518. * \param pt The point to set. This must be initialized.
  519. *
  520. * \return \c 0 on success.
  521. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  522. * \return Another negative error code on other kinds of failure.
  523. */
  524. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
  525. /**
  526. * \brief This function checks if a point is the point at infinity.
  527. *
  528. * \param pt The point to test. This must be initialized.
  529. *
  530. * \return \c 1 if the point is zero.
  531. * \return \c 0 if the point is non-zero.
  532. * \return A negative error code on failure.
  533. */
  534. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
  535. /**
  536. * \brief This function compares two points.
  537. *
  538. * \note This assumes that the points are normalized. Otherwise,
  539. * they may compare as "not equal" even if they are.
  540. *
  541. * \param P The first point to compare. This must be initialized.
  542. * \param Q The second point to compare. This must be initialized.
  543. *
  544. * \return \c 0 if the points are equal.
  545. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
  546. */
  547. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  548. const mbedtls_ecp_point *Q );
  549. /**
  550. * \brief This function imports a non-zero point from two ASCII
  551. * strings.
  552. *
  553. * \param P The destination point. This must be initialized.
  554. * \param radix The numeric base of the input.
  555. * \param x The first affine coordinate, as a null-terminated string.
  556. * \param y The second affine coordinate, as a null-terminated string.
  557. *
  558. * \return \c 0 on success.
  559. * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
  560. */
  561. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  562. const char *x, const char *y );
  563. /**
  564. * \brief This function exports a point into unsigned binary data.
  565. *
  566. * \param grp The group to which the point should belong.
  567. * This must be initialized and have group parameters
  568. * set, for example through mbedtls_ecp_group_load().
  569. * \param P The point to export. This must be initialized.
  570. * \param format The point format. This must be either
  571. * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
  572. * \param olen The address at which to store the length of
  573. * the output in Bytes. This must not be \c NULL.
  574. * \param buf The output buffer. This must be a writable buffer
  575. * of length \p buflen Bytes.
  576. * \param buflen The length of the output buffer \p buf in Bytes.
  577. *
  578. * \return \c 0 on success.
  579. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
  580. * is too small to hold the point.
  581. * \return Another negative error code on other kinds of failure.
  582. */
  583. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
  584. int format, size_t *olen,
  585. unsigned char *buf, size_t buflen );
  586. /**
  587. * \brief This function imports a point from unsigned binary data.
  588. *
  589. * \note This function does not check that the point actually
  590. * belongs to the given group, see mbedtls_ecp_check_pubkey()
  591. * for that.
  592. *
  593. * \param grp The group to which the point should belong.
  594. * This must be initialized and have group parameters
  595. * set, for example through mbedtls_ecp_group_load().
  596. * \param P The destination context to import the point to.
  597. * This must be initialized.
  598. * \param buf The input buffer. This must be a readable buffer
  599. * of length \p ilen Bytes.
  600. * \param ilen The length of the input buffer \p buf in Bytes.
  601. *
  602. * \return \c 0 on success.
  603. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
  604. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  605. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
  606. * is not implemented.
  607. */
  608. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
  609. mbedtls_ecp_point *P,
  610. const unsigned char *buf, size_t ilen );
  611. /**
  612. * \brief This function imports a point from a TLS ECPoint record.
  613. *
  614. * \note On function return, \p *buf is updated to point immediately
  615. * after the ECPoint record.
  616. *
  617. * \param grp The ECP group to use.
  618. * This must be initialized and have group parameters
  619. * set, for example through mbedtls_ecp_group_load().
  620. * \param pt The destination point.
  621. * \param buf The address of the pointer to the start of the input buffer.
  622. * \param len The length of the buffer.
  623. *
  624. * \return \c 0 on success.
  625. * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization
  626. * failure.
  627. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  628. */
  629. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
  630. mbedtls_ecp_point *pt,
  631. const unsigned char **buf, size_t len );
  632. /**
  633. * \brief This function exports a point as a TLS ECPoint record
  634. * defined in RFC 4492, Section 5.4.
  635. *
  636. * \param grp The ECP group to use.
  637. * This must be initialized and have group parameters
  638. * set, for example through mbedtls_ecp_group_load().
  639. * \param pt The point to be exported. This must be initialized.
  640. * \param format The point format to use. This must be either
  641. * #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
  642. * \param olen The address at which to store the length in Bytes
  643. * of the data written.
  644. * \param buf The target buffer. This must be a writable buffer of
  645. * length \p blen Bytes.
  646. * \param blen The length of the target buffer \p buf in Bytes.
  647. *
  648. * \return \c 0 on success.
  649. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
  650. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
  651. * is too small to hold the exported point.
  652. * \return Another negative error code on other kinds of failure.
  653. */
  654. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp,
  655. const mbedtls_ecp_point *pt,
  656. int format, size_t *olen,
  657. unsigned char *buf, size_t blen );
  658. /**
  659. * \brief This function sets up an ECP group context
  660. * from a standardized set of domain parameters.
  661. *
  662. * \note The index should be a value of the NamedCurve enum,
  663. * as defined in <em>RFC-4492: Elliptic Curve Cryptography
  664. * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
  665. * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
  666. *
  667. * \param grp The group context to setup. This must be initialized.
  668. * \param id The identifier of the domain parameter set to load.
  669. *
  670. * \return \c 0 on success.
  671. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
  672. * correspond to a known group.
  673. * \return Another negative error code on other kinds of failure.
  674. */
  675. int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
  676. /**
  677. * \brief This function sets up an ECP group context from a TLS
  678. * ECParameters record as defined in RFC 4492, Section 5.4.
  679. *
  680. * \note The read pointer \p buf is updated to point right after
  681. * the ECParameters record on exit.
  682. *
  683. * \param grp The group context to setup. This must be initialized.
  684. * \param buf The address of the pointer to the start of the input buffer.
  685. * \param len The length of the input buffer \c *buf in Bytes.
  686. *
  687. * \return \c 0 on success.
  688. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  689. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
  690. * recognized.
  691. * \return Another negative error code on other kinds of failure.
  692. */
  693. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
  694. const unsigned char **buf, size_t len );
  695. /**
  696. * \brief This function extracts an elliptic curve group ID from a
  697. * TLS ECParameters record as defined in RFC 4492, Section 5.4.
  698. *
  699. * \note The read pointer \p buf is updated to point right after
  700. * the ECParameters record on exit.
  701. *
  702. * \param grp The address at which to store the group id.
  703. * This must not be \c NULL.
  704. * \param buf The address of the pointer to the start of the input buffer.
  705. * \param len The length of the input buffer \c *buf in Bytes.
  706. *
  707. * \return \c 0 on success.
  708. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  709. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
  710. * recognized.
  711. * \return Another negative error code on other kinds of failure.
  712. */
  713. int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
  714. const unsigned char **buf,
  715. size_t len );
  716. /**
  717. * \brief This function exports an elliptic curve as a TLS
  718. * ECParameters record as defined in RFC 4492, Section 5.4.
  719. *
  720. * \param grp The ECP group to be exported.
  721. * This must be initialized and have group parameters
  722. * set, for example through mbedtls_ecp_group_load().
  723. * \param olen The address at which to store the number of Bytes written.
  724. * This must not be \c NULL.
  725. * \param buf The buffer to write to. This must be a writable buffer
  726. * of length \p blen Bytes.
  727. * \param blen The length of the output buffer \p buf in Bytes.
  728. *
  729. * \return \c 0 on success.
  730. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
  731. * buffer is too small to hold the exported group.
  732. * \return Another negative error code on other kinds of failure.
  733. */
  734. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp,
  735. size_t *olen,
  736. unsigned char *buf, size_t blen );
  737. /**
  738. * \brief This function performs a scalar multiplication of a point
  739. * by an integer: \p R = \p m * \p P.
  740. *
  741. * It is not thread-safe to use same group in multiple threads.
  742. *
  743. * \note To prevent timing attacks, this function
  744. * executes the exact same sequence of base-field
  745. * operations for any valid \p m. It avoids any if-branch or
  746. * array index depending on the value of \p m.
  747. *
  748. * \note If \p f_rng is not NULL, it is used to randomize
  749. * intermediate results to prevent potential timing attacks
  750. * targeting these results. We recommend always providing
  751. * a non-NULL \p f_rng. The overhead is negligible.
  752. *
  753. * \param grp The ECP group to use.
  754. * This must be initialized and have group parameters
  755. * set, for example through mbedtls_ecp_group_load().
  756. * \param R The point in which to store the result of the calculation.
  757. * This must be initialized.
  758. * \param m The integer by which to multiply. This must be initialized.
  759. * \param P The point to multiply. This must be initialized.
  760. * \param f_rng The RNG function. This may be \c NULL if randomization
  761. * of intermediate results isn't desired (discouraged).
  762. * \param p_rng The RNG context to be passed to \p p_rng.
  763. *
  764. * \return \c 0 on success.
  765. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
  766. * key, or \p P is not a valid public key.
  767. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  768. * \return Another negative error code on other kinds of failure.
  769. */
  770. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  771. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  772. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  773. /**
  774. * \brief This function performs multiplication of a point by
  775. * an integer: \p R = \p m * \p P in a restartable way.
  776. *
  777. * \see mbedtls_ecp_mul()
  778. *
  779. * \note This function does the same as \c mbedtls_ecp_mul(), but
  780. * it can return early and restart according to the limit set
  781. * with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  782. *
  783. * \param grp The ECP group to use.
  784. * This must be initialized and have group parameters
  785. * set, for example through mbedtls_ecp_group_load().
  786. * \param R The point in which to store the result of the calculation.
  787. * This must be initialized.
  788. * \param m The integer by which to multiply. This must be initialized.
  789. * \param P The point to multiply. This must be initialized.
  790. * \param f_rng The RNG function. This may be \c NULL if randomization
  791. * of intermediate results isn't desired (discouraged).
  792. * \param p_rng The RNG context to be passed to \p p_rng.
  793. * \param rs_ctx The restart context (NULL disables restart).
  794. *
  795. * \return \c 0 on success.
  796. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
  797. * key, or \p P is not a valid public key.
  798. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  799. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  800. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  801. * \return Another negative error code on other kinds of failure.
  802. */
  803. int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  804. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  805. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  806. mbedtls_ecp_restart_ctx *rs_ctx );
  807. /**
  808. * \brief This function performs multiplication and addition of two
  809. * points by integers: \p R = \p m * \p P + \p n * \p Q
  810. *
  811. * It is not thread-safe to use same group in multiple threads.
  812. *
  813. * \note In contrast to mbedtls_ecp_mul(), this function does not
  814. * guarantee a constant execution flow and timing.
  815. *
  816. * \param grp The ECP group to use.
  817. * This must be initialized and have group parameters
  818. * set, for example through mbedtls_ecp_group_load().
  819. * \param R The point in which to store the result of the calculation.
  820. * This must be initialized.
  821. * \param m The integer by which to multiply \p P.
  822. * This must be initialized.
  823. * \param P The point to multiply by \p m. This must be initialized.
  824. * \param n The integer by which to multiply \p Q.
  825. * This must be initialized.
  826. * \param Q The point to be multiplied by \p n.
  827. * This must be initialized.
  828. *
  829. * \return \c 0 on success.
  830. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
  831. * valid private keys, or \p P or \p Q are not valid public
  832. * keys.
  833. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  834. * \return Another negative error code on other kinds of failure.
  835. */
  836. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  837. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  838. const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
  839. /**
  840. * \brief This function performs multiplication and addition of two
  841. * points by integers: \p R = \p m * \p P + \p n * \p Q in a
  842. * restartable way.
  843. *
  844. * \see \c mbedtls_ecp_muladd()
  845. *
  846. * \note This function works the same as \c mbedtls_ecp_muladd(),
  847. * but it can return early and restart according to the limit
  848. * set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
  849. *
  850. * \param grp The ECP group to use.
  851. * This must be initialized and have group parameters
  852. * set, for example through mbedtls_ecp_group_load().
  853. * \param R The point in which to store the result of the calculation.
  854. * This must be initialized.
  855. * \param m The integer by which to multiply \p P.
  856. * This must be initialized.
  857. * \param P The point to multiply by \p m. This must be initialized.
  858. * \param n The integer by which to multiply \p Q.
  859. * This must be initialized.
  860. * \param Q The point to be multiplied by \p n.
  861. * This must be initialized.
  862. * \param rs_ctx The restart context (NULL disables restart).
  863. *
  864. * \return \c 0 on success.
  865. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
  866. * valid private keys, or \p P or \p Q are not valid public
  867. * keys.
  868. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  869. * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
  870. * operations was reached: see \c mbedtls_ecp_set_max_ops().
  871. * \return Another negative error code on other kinds of failure.
  872. */
  873. int mbedtls_ecp_muladd_restartable(
  874. mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  875. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  876. const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
  877. mbedtls_ecp_restart_ctx *rs_ctx );
  878. /**
  879. * \brief This function checks that a point is a valid public key
  880. * on this curve.
  881. *
  882. * It only checks that the point is non-zero, has
  883. * valid coordinates and lies on the curve. It does not verify
  884. * that it is indeed a multiple of \p G. This additional
  885. * check is computationally more expensive, is not required
  886. * by standards, and should not be necessary if the group
  887. * used has a small cofactor. In particular, it is useless for
  888. * the NIST groups which all have a cofactor of 1.
  889. *
  890. * \note This function uses bare components rather than an
  891. * ::mbedtls_ecp_keypair structure, to ease use with other
  892. * structures, such as ::mbedtls_ecdh_context or
  893. * ::mbedtls_ecdsa_context.
  894. *
  895. * \param grp The ECP group the point should belong to.
  896. * This must be initialized and have group parameters
  897. * set, for example through mbedtls_ecp_group_load().
  898. * \param pt The point to check. This must be initialized.
  899. *
  900. * \return \c 0 if the point is a valid public key.
  901. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
  902. * a valid public key for the given curve.
  903. * \return Another negative error code on other kinds of failure.
  904. */
  905. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
  906. const mbedtls_ecp_point *pt );
  907. /**
  908. * \brief This function checks that an \p mbedtls_mpi is a
  909. * valid private key for this curve.
  910. *
  911. * \note This function uses bare components rather than an
  912. * ::mbedtls_ecp_keypair structure to ease use with other
  913. * structures, such as ::mbedtls_ecdh_context or
  914. * ::mbedtls_ecdsa_context.
  915. *
  916. * \param grp The ECP group the private key should belong to.
  917. * This must be initialized and have group parameters
  918. * set, for example through mbedtls_ecp_group_load().
  919. * \param d The integer to check. This must be initialized.
  920. *
  921. * \return \c 0 if the point is a valid private key.
  922. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
  923. * private key for the given curve.
  924. * \return Another negative error code on other kinds of failure.
  925. */
  926. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
  927. const mbedtls_mpi *d );
  928. /**
  929. * \brief This function generates a private key.
  930. *
  931. * \param grp The ECP group to generate a private key for.
  932. * This must be initialized and have group parameters
  933. * set, for example through mbedtls_ecp_group_load().
  934. * \param d The destination MPI (secret part). This must be initialized.
  935. * \param f_rng The RNG function. This must not be \c NULL.
  936. * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
  937. * \c NULL if \p f_rng doesn't need a context argument.
  938. *
  939. * \return \c 0 on success.
  940. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  941. * on failure.
  942. */
  943. int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
  944. mbedtls_mpi *d,
  945. int (*f_rng)(void *, unsigned char *, size_t),
  946. void *p_rng );
  947. /**
  948. * \brief This function generates a keypair with a configurable base
  949. * point.
  950. *
  951. * \note This function uses bare components rather than an
  952. * ::mbedtls_ecp_keypair structure to ease use with other
  953. * structures, such as ::mbedtls_ecdh_context or
  954. * ::mbedtls_ecdsa_context.
  955. *
  956. * \param grp The ECP group to generate a key pair for.
  957. * This must be initialized and have group parameters
  958. * set, for example through mbedtls_ecp_group_load().
  959. * \param G The base point to use. This must be initialized
  960. * and belong to \p grp. It replaces the default base
  961. * point \c grp->G used by mbedtls_ecp_gen_keypair().
  962. * \param d The destination MPI (secret part).
  963. * This must be initialized.
  964. * \param Q The destination point (public part).
  965. * This must be initialized.
  966. * \param f_rng The RNG function. This must not be \c NULL.
  967. * \param p_rng The RNG context to be passed to \p f_rng. This may
  968. * be \c NULL if \p f_rng doesn't need a context argument.
  969. *
  970. * \return \c 0 on success.
  971. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  972. * on failure.
  973. */
  974. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  975. const mbedtls_ecp_point *G,
  976. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  977. int (*f_rng)(void *, unsigned char *, size_t),
  978. void *p_rng );
  979. /**
  980. * \brief This function generates an ECP keypair.
  981. *
  982. * \note This function uses bare components rather than an
  983. * ::mbedtls_ecp_keypair structure to ease use with other
  984. * structures, such as ::mbedtls_ecdh_context or
  985. * ::mbedtls_ecdsa_context.
  986. *
  987. * \param grp The ECP group to generate a key pair for.
  988. * This must be initialized and have group parameters
  989. * set, for example through mbedtls_ecp_group_load().
  990. * \param d The destination MPI (secret part).
  991. * This must be initialized.
  992. * \param Q The destination point (public part).
  993. * This must be initialized.
  994. * \param f_rng The RNG function. This must not be \c NULL.
  995. * \param p_rng The RNG context to be passed to \p f_rng. This may
  996. * be \c NULL if \p f_rng doesn't need a context argument.
  997. *
  998. * \return \c 0 on success.
  999. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  1000. * on failure.
  1001. */
  1002. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d,
  1003. mbedtls_ecp_point *Q,
  1004. int (*f_rng)(void *, unsigned char *, size_t),
  1005. void *p_rng );
  1006. /**
  1007. * \brief This function generates an ECP key.
  1008. *
  1009. * \param grp_id The ECP group identifier.
  1010. * \param key The destination key. This must be initialized.
  1011. * \param f_rng The RNG function to use. This must not be \c NULL.
  1012. * \param p_rng The RNG context to be passed to \p f_rng. This may
  1013. * be \c NULL if \p f_rng doesn't need a context argument.
  1014. *
  1015. * \return \c 0 on success.
  1016. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  1017. * on failure.
  1018. */
  1019. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  1020. int (*f_rng)(void *, unsigned char *, size_t),
  1021. void *p_rng );
  1022. /**
  1023. * \brief This function checks that the keypair objects
  1024. * \p pub and \p prv have the same group and the
  1025. * same public point, and that the private key in
  1026. * \p prv is consistent with the public key.
  1027. *
  1028. * \param pub The keypair structure holding the public key. This
  1029. * must be initialized. If it contains a private key, that
  1030. * part is ignored.
  1031. * \param prv The keypair structure holding the full keypair.
  1032. * This must be initialized.
  1033. *
  1034. * \return \c 0 on success, meaning that the keys are valid and match.
  1035. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
  1036. * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
  1037. * error code on calculation failure.
  1038. */
  1039. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub,
  1040. const mbedtls_ecp_keypair *prv );
  1041. #if defined(MBEDTLS_SELF_TEST)
  1042. /**
  1043. * \brief The ECP checkup routine.
  1044. *
  1045. * \return \c 0 on success.
  1046. * \return \c 1 on failure.
  1047. */
  1048. int mbedtls_ecp_self_test( int verbose );
  1049. #endif /* MBEDTLS_SELF_TEST */
  1050. #ifdef __cplusplus
  1051. }
  1052. #endif
  1053. #endif /* ecp.h */