ecp.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. #include "bignum.h"
  37. /*
  38. * ECP error codes
  39. */
  40. #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
  41. #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
  42. #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< The requested feature is not available, for example, the requested curve is not supported. */
  43. #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
  44. #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
  45. #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as ephemeral key, failed. */
  46. #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
  47. #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< The buffer contains a valid signature followed by more data. */
  48. #define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED -0x4B80 /**< The ECP hardware accelerator failed. */
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /**
  53. * Domain-parameter identifiers: curve, subgroup, and generator.
  54. *
  55. * \note Only curves over prime fields are supported.
  56. *
  57. * \warning This library does not support validation of arbitrary domain
  58. * parameters. Therefore, only standardized domain parameters from trusted
  59. * sources should be used. See mbedtls_ecp_group_load().
  60. */
  61. typedef enum
  62. {
  63. MBEDTLS_ECP_DP_NONE = 0, /*!< Curve not defined. */
  64. MBEDTLS_ECP_DP_SECP192R1, /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
  65. MBEDTLS_ECP_DP_SECP224R1, /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
  66. MBEDTLS_ECP_DP_SECP256R1, /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
  67. MBEDTLS_ECP_DP_SECP384R1, /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
  68. MBEDTLS_ECP_DP_SECP521R1, /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
  69. MBEDTLS_ECP_DP_BP256R1, /*!< Domain parameters for 256-bit Brainpool curve. */
  70. MBEDTLS_ECP_DP_BP384R1, /*!< Domain parameters for 384-bit Brainpool curve. */
  71. MBEDTLS_ECP_DP_BP512R1, /*!< Domain parameters for 512-bit Brainpool curve. */
  72. MBEDTLS_ECP_DP_CURVE25519, /*!< Domain parameters for Curve25519. */
  73. MBEDTLS_ECP_DP_SECP192K1, /*!< Domain parameters for 192-bit "Koblitz" curve. */
  74. MBEDTLS_ECP_DP_SECP224K1, /*!< Domain parameters for 224-bit "Koblitz" curve. */
  75. MBEDTLS_ECP_DP_SECP256K1, /*!< Domain parameters for 256-bit "Koblitz" curve. */
  76. MBEDTLS_ECP_DP_CURVE448, /*!< Domain parameters for Curve448. */
  77. } mbedtls_ecp_group_id;
  78. /**
  79. * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
  80. *
  81. * \note Montgomery curves are currently excluded.
  82. */
  83. #define MBEDTLS_ECP_DP_MAX 12
  84. /**
  85. * Curve information, for use by other modules.
  86. */
  87. typedef struct
  88. {
  89. mbedtls_ecp_group_id grp_id; /*!< An internal identifier. */
  90. uint16_t tls_id; /*!< The TLS NamedCurve identifier. */
  91. uint16_t bit_size; /*!< The curve size in bits. */
  92. const char *name; /*!< A human-friendly name. */
  93. } mbedtls_ecp_curve_info;
  94. /**
  95. * \brief The ECP point structure, in Jacobian coordinates.
  96. *
  97. * \note All functions expect and return points satisfying
  98. * the following condition: <code>Z == 0</code> or
  99. * <code>Z == 1</code>. Other values of \p Z are
  100. * used only by internal functions.
  101. * The point is zero, or "at infinity", if <code>Z == 0</code>.
  102. * Otherwise, \p X and \p Y are its standard (affine)
  103. * coordinates.
  104. */
  105. typedef struct
  106. {
  107. mbedtls_mpi X; /*!< The X coordinate of the ECP point. */
  108. mbedtls_mpi Y; /*!< The Y coordinate of the ECP point. */
  109. mbedtls_mpi Z; /*!< The Z coordinate of the ECP point. */
  110. }
  111. mbedtls_ecp_point;
  112. #if !defined(MBEDTLS_ECP_ALT)
  113. /*
  114. * default mbed TLS elliptic curve arithmetic implementation
  115. *
  116. * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
  117. * alternative implementation for the whole module and it will replace this
  118. * one.)
  119. */
  120. /**
  121. * \brief The ECP group structure.
  122. *
  123. * We consider two types of curve equations:
  124. * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
  125. * (SEC1 + RFC-4492)</li>
  126. * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
  127. * Curve448)</li></ul>
  128. * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
  129. *
  130. * For Short Weierstrass, this subgroup is the whole curve, and its
  131. * cardinality is denoted by \p N. Our code requires that \p N is an
  132. * odd prime as mbedtls_ecp_mul() requires an odd number, and
  133. * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
  134. *
  135. * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
  136. * which is the quantity used in the formulas. Additionally, \p nbits is
  137. * not the size of \p N but the required size for private keys.
  138. *
  139. * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
  140. * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
  141. * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
  142. * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
  143. * in size, so that it may be efficiently brought in the 0..P-1 range by a few
  144. * additions or subtractions. Therefore, it is only an approximative modular
  145. * reduction. It must return 0 on success and non-zero on failure.
  146. *
  147. */
  148. typedef struct
  149. {
  150. mbedtls_ecp_group_id id; /*!< An internal group identifier. */
  151. mbedtls_mpi P; /*!< The prime modulus of the base field. */
  152. mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For
  153. Montgomery curves: <code>(A + 2) / 4</code>. */
  154. mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation.
  155. For Montgomery curves: unused. */
  156. mbedtls_ecp_point G; /*!< The generator of the subgroup used. */
  157. mbedtls_mpi N; /*!< The order of \p G. */
  158. size_t pbits; /*!< The number of bits in \p P.*/
  159. size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P.
  160. For Montgomery curves: the number of bits in the
  161. private keys. */
  162. unsigned int h; /*!< \internal 1 if the constants are static. */
  163. int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
  164. mod \p P (see above).*/
  165. int (*t_pre)(mbedtls_ecp_point *, void *); /*!< Unused. */
  166. int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
  167. void *t_data; /*!< Unused. */
  168. mbedtls_ecp_point *T; /*!< Pre-computed points for ecp_mul_comb(). */
  169. size_t T_size; /*!< The number of pre-computed points. */
  170. }
  171. mbedtls_ecp_group;
  172. /**
  173. * \name SECTION: Module settings
  174. *
  175. * The configuration options you can set for this module are in this section.
  176. * Either change them in config.h, or define them using the compiler command line.
  177. * \{
  178. */
  179. #if !defined(MBEDTLS_ECP_MAX_BITS)
  180. /**
  181. * The maximum size of the groups, that is, of \c N and \c P.
  182. */
  183. #define MBEDTLS_ECP_MAX_BITS 521 /**< The maximum size of groups, in bits. */
  184. #endif
  185. #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
  186. #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
  187. #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
  188. /*
  189. * Maximum "window" size used for point multiplication.
  190. * Default: 6.
  191. * Minimum value: 2. Maximum value: 7.
  192. *
  193. * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  194. * points used for point multiplication. This value is directly tied to EC
  195. * peak memory usage, so decreasing it by one should roughly cut memory usage
  196. * by two (if large curves are in use).
  197. *
  198. * Reduction in size may reduce speed, but larger curves are impacted first.
  199. * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
  200. * w-size: 6 5 4 3 2
  201. * 521 145 141 135 120 97
  202. * 384 214 209 198 177 146
  203. * 256 320 320 303 262 226
  204. * 224 475 475 453 398 342
  205. * 192 640 640 633 587 476
  206. */
  207. #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< The maximum window size used. */
  208. #endif /* MBEDTLS_ECP_WINDOW_SIZE */
  209. #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
  210. /*
  211. * Trade memory for speed on fixed-point multiplication.
  212. *
  213. * This speeds up repeated multiplication of the generator (that is, the
  214. * multiplication in ECDSA signatures, and half of the multiplications in
  215. * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
  216. *
  217. * The cost is increasing EC peak memory usage by a factor roughly 2.
  218. *
  219. * Change this value to 0 to reduce peak memory usage.
  220. */
  221. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */
  222. #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
  223. /* \} name SECTION: Module settings */
  224. #else /* MBEDTLS_ECP_ALT */
  225. #include "ecp_alt.h"
  226. #endif /* MBEDTLS_ECP_ALT */
  227. /**
  228. * \brief The ECP key-pair structure.
  229. *
  230. * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
  231. *
  232. * \note Members are deliberately in the same order as in the
  233. * ::mbedtls_ecdsa_context structure.
  234. */
  235. typedef struct
  236. {
  237. mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
  238. mbedtls_mpi d; /*!< our secret value */
  239. mbedtls_ecp_point Q; /*!< our public value */
  240. }
  241. mbedtls_ecp_keypair;
  242. /*
  243. * Point formats, from RFC 4492's enum ECPointFormat
  244. */
  245. #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format. */
  246. #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format. */
  247. /*
  248. * Some other constants from RFC 4492
  249. */
  250. #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< The named_curve of ECCurveType. */
  251. /**
  252. * \brief This function retrieves the information defined in
  253. * mbedtls_ecp_curve_info() for all supported curves in order
  254. * of preference.
  255. *
  256. * \return A statically allocated array. The last entry is 0.
  257. */
  258. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
  259. /**
  260. * \brief This function retrieves the list of internal group
  261. * identifiers of all supported curves in the order of
  262. * preference.
  263. *
  264. * \return A statically allocated array,
  265. * terminated with MBEDTLS_ECP_DP_NONE.
  266. */
  267. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
  268. /**
  269. * \brief This function retrieves curve information from an internal
  270. * group identifier.
  271. *
  272. * \param grp_id An \c MBEDTLS_ECP_DP_XXX value.
  273. *
  274. * \return The associated curve information on success.
  275. * \return NULL on failure.
  276. */
  277. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
  278. /**
  279. * \brief This function retrieves curve information from a TLS
  280. * NamedCurve value.
  281. *
  282. * \param tls_id An \c MBEDTLS_ECP_DP_XXX value.
  283. *
  284. * \return The associated curve information on success.
  285. * \return NULL on failure.
  286. */
  287. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
  288. /**
  289. * \brief This function retrieves curve information from a
  290. * human-readable name.
  291. *
  292. * \param name The human-readable name.
  293. *
  294. * \return The associated curve information on success.
  295. * \return NULL on failure.
  296. */
  297. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
  298. /**
  299. * \brief This function initializes a point as zero.
  300. *
  301. * \param pt The point to initialize.
  302. */
  303. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
  304. /**
  305. * \brief This function initializes an ECP group context
  306. * without loading any domain parameters.
  307. *
  308. * \note After this function is called, domain parameters
  309. * for various ECP groups can be loaded through the
  310. * mbedtls_ecp_load() or mbedtls_ecp_tls_read_group()
  311. * functions.
  312. */
  313. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
  314. /**
  315. * \brief This function initializes a key pair as an invalid one.
  316. *
  317. * \param key The key pair to initialize.
  318. */
  319. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
  320. /**
  321. * \brief This function frees the components of a point.
  322. *
  323. * \param pt The point to free.
  324. */
  325. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
  326. /**
  327. * \brief This function frees the components of an ECP group.
  328. * \param grp The group to free.
  329. */
  330. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
  331. /**
  332. * \brief This function frees the components of a key pair.
  333. * \param key The key pair to free.
  334. */
  335. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
  336. /**
  337. * \brief This function copies the contents of point \p Q into
  338. * point \p P.
  339. *
  340. * \param P The destination point.
  341. * \param Q The source point.
  342. *
  343. * \return \c 0 on success.
  344. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  345. */
  346. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
  347. /**
  348. * \brief This function copies the contents of group \p src into
  349. * group \p dst.
  350. *
  351. * \param dst The destination group.
  352. * \param src The source group.
  353. *
  354. * \return \c 0 on success.
  355. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  356. */
  357. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
  358. /**
  359. * \brief This function sets a point to zero.
  360. *
  361. * \param pt The point to set.
  362. *
  363. * \return \c 0 on success.
  364. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  365. */
  366. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
  367. /**
  368. * \brief This function checks if a point is zero.
  369. *
  370. * \param pt The point to test.
  371. *
  372. * \return \c 1 if the point is zero.
  373. * \return \c 0 if the point is non-zero.
  374. */
  375. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
  376. /**
  377. * \brief This function compares two points.
  378. *
  379. * \note This assumes that the points are normalized. Otherwise,
  380. * they may compare as "not equal" even if they are.
  381. *
  382. * \param P The first point to compare.
  383. * \param Q The second point to compare.
  384. *
  385. * \return \c 0 if the points are equal.
  386. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
  387. */
  388. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  389. const mbedtls_ecp_point *Q );
  390. /**
  391. * \brief This function imports a non-zero point from two ASCII
  392. * strings.
  393. *
  394. * \param P The destination point.
  395. * \param radix The numeric base of the input.
  396. * \param x The first affine coordinate, as a null-terminated string.
  397. * \param y The second affine coordinate, as a null-terminated string.
  398. *
  399. * \return \c 0 on success.
  400. * \return An \c MBEDTLS_ERR_MPI_XXX error code on failure.
  401. */
  402. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  403. const char *x, const char *y );
  404. /**
  405. * \brief This function exports a point into unsigned binary data.
  406. *
  407. * \param grp The group to which the point should belong.
  408. * \param P The point to export.
  409. * \param format The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro.
  410. * \param olen The length of the output.
  411. * \param buf The output buffer.
  412. * \param buflen The length of the output buffer.
  413. *
  414. * \return \c 0 on success.
  415. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  416. * or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
  417. */
  418. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
  419. int format, size_t *olen,
  420. unsigned char *buf, size_t buflen );
  421. /**
  422. * \brief This function imports a point from unsigned binary data.
  423. *
  424. * \note This function does not check that the point actually
  425. * belongs to the given group, see mbedtls_ecp_check_pubkey()
  426. * for that.
  427. *
  428. * \param grp The group to which the point should belong.
  429. * \param P The point to import.
  430. * \param buf The input buffer.
  431. * \param ilen The length of the input.
  432. *
  433. * \return \c 0 on success.
  434. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  435. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  436. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
  437. * is not implemented.
  438. *
  439. */
  440. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  441. const unsigned char *buf, size_t ilen );
  442. /**
  443. * \brief This function imports a point from a TLS ECPoint record.
  444. *
  445. * \note On function return, \p buf is updated to point to immediately
  446. * after the ECPoint record.
  447. *
  448. * \param grp The ECP group used.
  449. * \param pt The destination point.
  450. * \param buf The address of the pointer to the start of the input buffer.
  451. * \param len The length of the buffer.
  452. *
  453. * \return \c 0 on success.
  454. * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
  455. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  456. */
  457. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  458. const unsigned char **buf, size_t len );
  459. /**
  460. * \brief This function exports a point as a TLS ECPoint record.
  461. *
  462. * \param grp The ECP group used.
  463. * \param pt The point format to export to. The point format is an
  464. * \c MBEDTLS_ECP_PF_XXX constant.
  465. * \param format The export format.
  466. * \param olen The length of the data written.
  467. * \param buf The buffer to write to.
  468. * \param blen The length of the buffer.
  469. *
  470. * \return \c 0 on success.
  471. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or
  472. * #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
  473. */
  474. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
  475. int format, size_t *olen,
  476. unsigned char *buf, size_t blen );
  477. /**
  478. * \brief This function sets a group using standardized domain parameters.
  479. *
  480. * \note The index should be a value of the NamedCurve enum,
  481. * as defined in <em>RFC-4492: Elliptic Curve Cryptography
  482. * (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
  483. * usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
  484. *
  485. * \param grp The destination group.
  486. * \param id The identifier of the domain parameter set to load.
  487. *
  488. * \return \c 0 on success,
  489. * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
  490. * \return #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups.
  491. */
  492. int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );
  493. /**
  494. * \brief This function sets a group from a TLS ECParameters record.
  495. *
  496. * \note \p buf is updated to point right after the ECParameters record
  497. * on exit.
  498. *
  499. * \param grp The destination group.
  500. * \param buf The address of the pointer to the start of the input buffer.
  501. * \param len The length of the buffer.
  502. *
  503. * \return \c 0 on success.
  504. * \return An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
  505. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
  506. */
  507. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
  508. /**
  509. * \brief This function writes the TLS ECParameters record for a group.
  510. *
  511. * \param grp The ECP group used.
  512. * \param olen The number of Bytes written.
  513. * \param buf The buffer to write to.
  514. * \param blen The length of the buffer.
  515. *
  516. * \return \c 0 on success.
  517. * \return #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
  518. */
  519. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  520. unsigned char *buf, size_t blen );
  521. /**
  522. * \brief This function performs multiplication of a point by
  523. * an integer: \p R = \p m * \p P.
  524. *
  525. * It is not thread-safe to use same group in multiple threads.
  526. *
  527. * \note To prevent timing attacks, this function
  528. * executes the exact same sequence of base-field
  529. * operations for any valid \p m. It avoids any if-branch or
  530. * array index depending on the value of \p m.
  531. *
  532. * \note If \p f_rng is not NULL, it is used to randomize
  533. * intermediate results to prevent potential timing attacks
  534. * targeting these results. We recommend always providing
  535. * a non-NULL \p f_rng. The overhead is negligible.
  536. *
  537. * \param grp The ECP group.
  538. * \param R The destination point.
  539. * \param m The integer by which to multiply.
  540. * \param P The point to multiply.
  541. * \param f_rng The RNG function.
  542. * \param p_rng The RNG context.
  543. *
  544. * \return \c 0 on success.
  545. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
  546. * key, or \p P is not a valid public key.
  547. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  548. */
  549. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  550. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  551. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  552. /**
  553. * \brief This function performs multiplication and addition of two
  554. * points by integers: \p R = \p m * \p P + \p n * \p Q
  555. *
  556. * It is not thread-safe to use same group in multiple threads.
  557. *
  558. * \note In contrast to mbedtls_ecp_mul(), this function does not
  559. * guarantee a constant execution flow and timing.
  560. *
  561. * \param grp The ECP group.
  562. * \param R The destination point.
  563. * \param m The integer by which to multiply \p P.
  564. * \param P The point to multiply by \p m.
  565. * \param n The integer by which to multiply \p Q.
  566. * \param Q The point to be multiplied by \p n.
  567. *
  568. * \return \c 0 on success.
  569. * \return #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
  570. * valid private keys, or \p P or \p Q are not valid public
  571. * keys.
  572. * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
  573. */
  574. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  575. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  576. const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
  577. /**
  578. * \brief This function checks that a point is a valid public key
  579. * on this curve.
  580. *
  581. * It only checks that the point is non-zero, has
  582. * valid coordinates and lies on the curve. It does not verify
  583. * that it is indeed a multiple of \p G. This additional
  584. * check is computationally more expensive, is not required
  585. * by standards, and should not be necessary if the group
  586. * used has a small cofactor. In particular, it is useless for
  587. * the NIST groups which all have a cofactor of 1.
  588. *
  589. * \note This function uses bare components rather than an
  590. * ::mbedtls_ecp_keypair structure, to ease use with other
  591. * structures, such as ::mbedtls_ecdh_context or
  592. * ::mbedtls_ecdsa_context.
  593. *
  594. * \param grp The curve the point should lie on.
  595. * \param pt The point to check.
  596. *
  597. * \return \c 0 if the point is a valid public key.
  598. * \return #MBEDTLS_ERR_ECP_INVALID_KEY on failure.
  599. */
  600. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
  601. /**
  602. * \brief This function checks that an \p mbedtls_mpi is a valid private
  603. * key for this curve.
  604. *
  605. * \note This function uses bare components rather than an
  606. * ::mbedtls_ecp_keypair structure to ease use with other
  607. * structures, such as ::mbedtls_ecdh_context or
  608. * ::mbedtls_ecdsa_context.
  609. *
  610. * \param grp The group used.
  611. * \param d The integer to check.
  612. *
  613. * \return \c 0 if the point is a valid private key.
  614. * \return #MBEDTLS_ERR_ECP_INVALID_KEY on failure.
  615. */
  616. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
  617. /**
  618. * \brief This function generates a keypair with a configurable base
  619. * point.
  620. *
  621. * \note This function uses bare components rather than an
  622. * ::mbedtls_ecp_keypair structure to ease use with other
  623. * structures, such as ::mbedtls_ecdh_context or
  624. * ::mbedtls_ecdsa_context.
  625. *
  626. * \param grp The ECP group.
  627. * \param G The chosen base point.
  628. * \param d The destination MPI (secret part).
  629. * \param Q The destination point (public part).
  630. * \param f_rng The RNG function.
  631. * \param p_rng The RNG context.
  632. *
  633. * \return \c 0 on success.
  634. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  635. * on failure.
  636. */
  637. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  638. const mbedtls_ecp_point *G,
  639. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  640. int (*f_rng)(void *, unsigned char *, size_t),
  641. void *p_rng );
  642. /**
  643. * \brief This function generates an ECP keypair.
  644. *
  645. * \note This function uses bare components rather than an
  646. * ::mbedtls_ecp_keypair structure to ease use with other
  647. * structures, such as ::mbedtls_ecdh_context or
  648. * ::mbedtls_ecdsa_context.
  649. *
  650. * \param grp The ECP group.
  651. * \param d The destination MPI (secret part).
  652. * \param Q The destination point (public part).
  653. * \param f_rng The RNG function.
  654. * \param p_rng The RNG context.
  655. *
  656. * \return \c 0 on success.
  657. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  658. * on failure.
  659. */
  660. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  661. int (*f_rng)(void *, unsigned char *, size_t),
  662. void *p_rng );
  663. /**
  664. * \brief This function generates an ECP key.
  665. *
  666. * \param grp_id The ECP group identifier.
  667. * \param key The destination key.
  668. * \param f_rng The RNG function.
  669. * \param p_rng The RNG context.
  670. *
  671. * \return \c 0 on success.
  672. * \return An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
  673. * on failure.
  674. */
  675. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  676. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  677. /**
  678. * \brief This function checks that the keypair objects
  679. * \p pub and \p prv have the same group and the
  680. * same public point, and that the private key in
  681. * \p prv is consistent with the public key.
  682. *
  683. * \param pub The keypair structure holding the public key.
  684. * If it contains a private key, that part is ignored.
  685. * \param prv The keypair structure holding the full keypair.
  686. *
  687. * \return \c 0 on success, meaning that the keys are valid and match.
  688. * \return #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
  689. * \return An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
  690. * error code on calculation failure.
  691. */
  692. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
  693. #if defined(MBEDTLS_SELF_TEST)
  694. /**
  695. * \brief The ECP checkup routine.
  696. *
  697. * \return \c 0 on success.
  698. * \return \c 1 on failure.
  699. */
  700. int mbedtls_ecp_self_test( int verbose );
  701. #endif /* MBEDTLS_SELF_TEST */
  702. #ifdef __cplusplus
  703. }
  704. #endif
  705. #endif /* ecp.h */