ecp.h 57 KB

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