ecp.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /**
  2. * \file ecp.h
  3. *
  4. * \brief Elliptic curves over GF(p)
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_ECP_H
  26. #define MBEDTLS_ECP_H
  27. #include "bignum.h"
  28. /*
  29. * ECP error codes
  30. */
  31. #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
  32. #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
  33. #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
  34. #define MBEDTLS_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
  35. #define MBEDTLS_ERR_ECP_ALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
  36. #define MBEDTLS_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
  37. #define MBEDTLS_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
  38. #define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH -0x4C00 /**< Signature is valid but shorter than the user-supplied length. */
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /**
  43. * Domain parameters (curve, subgroup and generator) identifiers.
  44. *
  45. * Only curves over prime fields are supported.
  46. *
  47. * \warning This library does not support validation of arbitrary domain
  48. * parameters. Therefore, only well-known domain parameters from trusted
  49. * sources should be used. See mbedtls_ecp_group_load().
  50. */
  51. typedef enum
  52. {
  53. MBEDTLS_ECP_DP_NONE = 0,
  54. MBEDTLS_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
  55. MBEDTLS_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
  56. MBEDTLS_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
  57. MBEDTLS_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
  58. MBEDTLS_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
  59. MBEDTLS_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
  60. MBEDTLS_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
  61. MBEDTLS_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
  62. MBEDTLS_ECP_DP_CURVE25519, /*!< Curve25519 */
  63. MBEDTLS_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
  64. MBEDTLS_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
  65. MBEDTLS_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
  66. } mbedtls_ecp_group_id;
  67. /**
  68. * Number of supported curves (plus one for NONE).
  69. *
  70. * (Montgomery curves excluded for now.)
  71. */
  72. #define MBEDTLS_ECP_DP_MAX 12
  73. /**
  74. * Curve information for use by other modules
  75. */
  76. typedef struct
  77. {
  78. mbedtls_ecp_group_id grp_id; /*!< Internal identifier */
  79. uint16_t tls_id; /*!< TLS NamedCurve identifier */
  80. uint16_t bit_size; /*!< Curve size in bits */
  81. const char *name; /*!< Human-friendly name */
  82. } mbedtls_ecp_curve_info;
  83. /**
  84. * \brief ECP point structure (jacobian coordinates)
  85. *
  86. * \note All functions expect and return points satisfying
  87. * the following condition: Z == 0 or Z == 1. (Other
  88. * values of Z are used by internal functions only.)
  89. * The point is zero, or "at infinity", if Z == 0.
  90. * Otherwise, X and Y are its standard (affine) coordinates.
  91. */
  92. typedef struct
  93. {
  94. mbedtls_mpi X; /*!< the point's X coordinate */
  95. mbedtls_mpi Y; /*!< the point's Y coordinate */
  96. mbedtls_mpi Z; /*!< the point's Z coordinate */
  97. }
  98. mbedtls_ecp_point;
  99. /**
  100. * \brief ECP group structure
  101. *
  102. * We consider two types of curves equations:
  103. * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
  104. * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (Curve25519 + draft)
  105. * In both cases, a generator G for a prime-order subgroup is fixed. In the
  106. * short weierstrass, this subgroup is actually the whole curve, and its
  107. * cardinal is denoted by N.
  108. *
  109. * In the case of Short Weierstrass curves, our code requires that N is an odd
  110. * prime. (Use odd in mbedtls_ecp_mul() and prime in mbedtls_ecdsa_sign() for blinding.)
  111. *
  112. * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
  113. * the quantity actually used in the formulas. Also, nbits is not the size of N
  114. * but the required size for private keys.
  115. *
  116. * If modp is NULL, reduction modulo P is done using a generic algorithm.
  117. * Otherwise, it must point to a function that takes an mbedtls_mpi in the range
  118. * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
  119. * than pbits, so that the integer may be efficiently brought in the 0..P-1
  120. * range by a few additions or substractions. It must return 0 on success and
  121. * non-zero on failure.
  122. */
  123. typedef struct
  124. {
  125. mbedtls_ecp_group_id id; /*!< internal group identifier */
  126. mbedtls_mpi P; /*!< prime modulus of the base field */
  127. mbedtls_mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
  128. mbedtls_mpi B; /*!< 1. B in the equation, or 2. unused */
  129. mbedtls_ecp_point G; /*!< generator of the (sub)group used */
  130. mbedtls_mpi N; /*!< 1. the order of G, or 2. unused */
  131. size_t pbits; /*!< number of bits in P */
  132. size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
  133. unsigned int h; /*!< internal: 1 if the constants are static */
  134. int (*modp)(mbedtls_mpi *); /*!< function for fast reduction mod P */
  135. int (*t_pre)(mbedtls_ecp_point *, void *); /*!< unused */
  136. int (*t_post)(mbedtls_ecp_point *, void *); /*!< unused */
  137. void *t_data; /*!< unused */
  138. mbedtls_ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
  139. size_t T_size; /*!< number for pre-computed points */
  140. }
  141. mbedtls_ecp_group;
  142. /**
  143. * \brief ECP key pair structure
  144. *
  145. * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
  146. *
  147. * \note Members purposefully in the same order as struc mbedtls_ecdsa_context.
  148. */
  149. typedef struct
  150. {
  151. mbedtls_ecp_group grp; /*!< Elliptic curve and base point */
  152. mbedtls_mpi d; /*!< our secret value */
  153. mbedtls_ecp_point Q; /*!< our public value */
  154. }
  155. mbedtls_ecp_keypair;
  156. /**
  157. * \name SECTION: Module settings
  158. *
  159. * The configuration options you can set for this module are in this section.
  160. * Either change them in config.h or define them on the compiler command line.
  161. * \{
  162. */
  163. #if !defined(MBEDTLS_ECP_MAX_BITS)
  164. /**
  165. * Maximum size of the groups (that is, of N and P)
  166. */
  167. #define MBEDTLS_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
  168. #endif
  169. #define MBEDTLS_ECP_MAX_BYTES ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
  170. #define MBEDTLS_ECP_MAX_PT_LEN ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )
  171. #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
  172. /*
  173. * Maximum "window" size used for point multiplication.
  174. * Default: 6.
  175. * Minimum value: 2. Maximum value: 7.
  176. *
  177. * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
  178. * points used for point multiplication. This value is directly tied to EC
  179. * peak memory usage, so decreasing it by one should roughly cut memory usage
  180. * by two (if large curves are in use).
  181. *
  182. * Reduction in size may reduce speed, but larger curves are impacted first.
  183. * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
  184. * w-size: 6 5 4 3 2
  185. * 521 145 141 135 120 97
  186. * 384 214 209 198 177 146
  187. * 256 320 320 303 262 226
  188. * 224 475 475 453 398 342
  189. * 192 640 640 633 587 476
  190. */
  191. #define MBEDTLS_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
  192. #endif /* MBEDTLS_ECP_WINDOW_SIZE */
  193. #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
  194. /*
  195. * Trade memory for speed on fixed-point multiplication.
  196. *
  197. * This speeds up repeated multiplication of the generator (that is, the
  198. * multiplication in ECDSA signatures, and half of the multiplications in
  199. * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
  200. *
  201. * The cost is increasing EC peak memory usage by a factor roughly 2.
  202. *
  203. * Change this value to 0 to reduce peak memory usage.
  204. */
  205. #define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
  206. #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
  207. /* \} name SECTION: Module settings */
  208. /*
  209. * Point formats, from RFC 4492's enum ECPointFormat
  210. */
  211. #define MBEDTLS_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
  212. #define MBEDTLS_ECP_PF_COMPRESSED 1 /**< Compressed point format */
  213. /*
  214. * Some other constants from RFC 4492
  215. */
  216. #define MBEDTLS_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
  217. /**
  218. * \brief Get the list of supported curves in order of preferrence
  219. * (full information)
  220. *
  221. * \return A statically allocated array, the last entry is 0.
  222. */
  223. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );
  224. /**
  225. * \brief Get the list of supported curves in order of preferrence
  226. * (grp_id only)
  227. *
  228. * \return A statically allocated array,
  229. * terminated with MBEDTLS_ECP_DP_NONE.
  230. */
  231. const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );
  232. /**
  233. * \brief Get curve information from an internal group identifier
  234. *
  235. * \param grp_id A MBEDTLS_ECP_DP_XXX value
  236. *
  237. * \return The associated curve information or NULL
  238. */
  239. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );
  240. /**
  241. * \brief Get curve information from a TLS NamedCurve value
  242. *
  243. * \param tls_id A MBEDTLS_ECP_DP_XXX value
  244. *
  245. * \return The associated curve information or NULL
  246. */
  247. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );
  248. /**
  249. * \brief Get curve information from a human-readable name
  250. *
  251. * \param name The name
  252. *
  253. * \return The associated curve information or NULL
  254. */
  255. const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );
  256. /**
  257. * \brief Initialize a point (as zero)
  258. */
  259. void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );
  260. /**
  261. * \brief Initialize a group (to something meaningless)
  262. */
  263. void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );
  264. /**
  265. * \brief Initialize a key pair (as an invalid one)
  266. */
  267. void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );
  268. /**
  269. * \brief Free the components of a point
  270. */
  271. void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );
  272. /**
  273. * \brief Free the components of an ECP group
  274. */
  275. void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );
  276. /**
  277. * \brief Free the components of a key pair
  278. */
  279. void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );
  280. /**
  281. * \brief Copy the contents of point Q into P
  282. *
  283. * \param P Destination point
  284. * \param Q Source point
  285. *
  286. * \return 0 if successful,
  287. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  288. */
  289. int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );
  290. /**
  291. * \brief Copy the contents of a group object
  292. *
  293. * \param dst Destination group
  294. * \param src Source group
  295. *
  296. * \return 0 if successful,
  297. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  298. */
  299. int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );
  300. /**
  301. * \brief Set a point to zero
  302. *
  303. * \param pt Destination point
  304. *
  305. * \return 0 if successful,
  306. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  307. */
  308. int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );
  309. /**
  310. * \brief Tell if a point is zero
  311. *
  312. * \param pt Point to test
  313. *
  314. * \return 1 if point is zero, 0 otherwise
  315. */
  316. int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );
  317. /**
  318. * \brief Compare two points
  319. *
  320. * \note This assumes the points are normalized. Otherwise,
  321. * they may compare as "not equal" even if they are.
  322. *
  323. * \param P First point to compare
  324. * \param Q Second point to compare
  325. *
  326. * \return 0 if the points are equal,
  327. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA otherwise
  328. */
  329. int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
  330. const mbedtls_ecp_point *Q );
  331. /**
  332. * \brief Import a non-zero point from two ASCII strings
  333. *
  334. * \param P Destination point
  335. * \param radix Input numeric base
  336. * \param x First affine coordinate as a null-terminated string
  337. * \param y Second affine coordinate as a null-terminated string
  338. *
  339. * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
  340. */
  341. int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
  342. const char *x, const char *y );
  343. /**
  344. * \brief Export a point into unsigned binary data
  345. *
  346. * \param grp Group to which the point should belong
  347. * \param P Point to export
  348. * \param format Point format, should be a MBEDTLS_ECP_PF_XXX macro
  349. * \param olen Length of the actual output
  350. * \param buf Output buffer
  351. * \param buflen Length of the output buffer
  352. *
  353. * \return 0 if successful,
  354. * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  355. * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  356. */
  357. int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
  358. int format, size_t *olen,
  359. unsigned char *buf, size_t buflen );
  360. /**
  361. * \brief Import a point from unsigned binary data
  362. *
  363. * \param grp Group to which the point should belong
  364. * \param P Point to import
  365. * \param buf Input buffer
  366. * \param ilen Actual length of input
  367. *
  368. * \return 0 if successful,
  369. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid,
  370. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
  371. * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
  372. * is not implemented.
  373. *
  374. * \note This function does NOT check that the point actually
  375. * belongs to the given group, see mbedtls_ecp_check_pubkey() for
  376. * that.
  377. */
  378. int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
  379. const unsigned char *buf, size_t ilen );
  380. /**
  381. * \brief Import a point from a TLS ECPoint record
  382. *
  383. * \param grp ECP group used
  384. * \param pt Destination point
  385. * \param buf $(Start of input buffer)
  386. * \param len Buffer length
  387. *
  388. * \note buf is updated to point right after the ECPoint on exit
  389. *
  390. * \return 0 if successful,
  391. * MBEDTLS_ERR_MPI_XXX if initialization failed
  392. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
  393. */
  394. int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
  395. const unsigned char **buf, size_t len );
  396. /**
  397. * \brief Export a point as a TLS ECPoint record
  398. *
  399. * \param grp ECP group used
  400. * \param pt Point to export
  401. * \param format Export format
  402. * \param olen length of data written
  403. * \param buf Buffer to write to
  404. * \param blen Buffer length
  405. *
  406. * \return 0 if successful,
  407. * or MBEDTLS_ERR_ECP_BAD_INPUT_DATA
  408. * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  409. */
  410. int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
  411. int format, size_t *olen,
  412. unsigned char *buf, size_t blen );
  413. /**
  414. * \brief Set a group using well-known domain parameters
  415. *
  416. * \param grp Destination group
  417. * \param index Index in the list of well-known domain parameters
  418. *
  419. * \return 0 if successful,
  420. * MBEDTLS_ERR_MPI_XXX if initialization failed
  421. * MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
  422. *
  423. * \note Index should be a value of RFC 4492's enum NamedCurve,
  424. * usually in the form of a MBEDTLS_ECP_DP_XXX macro.
  425. */
  426. int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id index );
  427. /**
  428. * \brief Set a group from a TLS ECParameters record
  429. *
  430. * \param grp Destination group
  431. * \param buf &(Start of input buffer)
  432. * \param len Buffer length
  433. *
  434. * \note buf is updated to point right after ECParameters on exit
  435. *
  436. * \return 0 if successful,
  437. * MBEDTLS_ERR_MPI_XXX if initialization failed
  438. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid
  439. */
  440. int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );
  441. /**
  442. * \brief Write the TLS ECParameters record for a group
  443. *
  444. * \param grp ECP group used
  445. * \param olen Number of bytes actually written
  446. * \param buf Buffer to write to
  447. * \param blen Buffer length
  448. *
  449. * \return 0 if successful,
  450. * or MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
  451. */
  452. int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen,
  453. unsigned char *buf, size_t blen );
  454. /**
  455. * \brief Multiplication by an integer: R = m * P
  456. * (Not thread-safe to use same group in multiple threads)
  457. *
  458. * \note In order to prevent timing attacks, this function
  459. * executes the exact same sequence of (base field)
  460. * operations for any valid m. It avoids any if-branch or
  461. * array index depending on the value of m.
  462. *
  463. * \note If f_rng is not NULL, it is used to randomize intermediate
  464. * results in order to prevent potential timing attacks
  465. * targeting these results. It is recommended to always
  466. * provide a non-NULL f_rng (the overhead is negligible).
  467. *
  468. * \param grp ECP group
  469. * \param R Destination point
  470. * \param m Integer by which to multiply
  471. * \param P Point to multiply
  472. * \param f_rng RNG function (see notes)
  473. * \param p_rng RNG parameter
  474. *
  475. * \return 0 if successful,
  476. * MBEDTLS_ERR_ECP_INVALID_KEY if m is not a valid privkey
  477. * or P is not a valid pubkey,
  478. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  479. */
  480. int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  481. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  482. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  483. /**
  484. * \brief Multiplication and addition of two points by integers:
  485. * R = m * P + n * Q
  486. * (Not thread-safe to use same group in multiple threads)
  487. *
  488. * \note In contrast to mbedtls_ecp_mul(), this function does not guarantee
  489. * a constant execution flow and timing.
  490. *
  491. * \param grp ECP group
  492. * \param R Destination point
  493. * \param m Integer by which to multiply P
  494. * \param P Point to multiply by m
  495. * \param n Integer by which to multiply Q
  496. * \param Q Point to be multiplied by n
  497. *
  498. * \return 0 if successful,
  499. * MBEDTLS_ERR_ECP_INVALID_KEY if m or n is not a valid privkey
  500. * or P or Q is not a valid pubkey,
  501. * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
  502. */
  503. int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
  504. const mbedtls_mpi *m, const mbedtls_ecp_point *P,
  505. const mbedtls_mpi *n, const mbedtls_ecp_point *Q );
  506. /**
  507. * \brief Check that a point is a valid public key on this curve
  508. *
  509. * \param grp Curve/group the point should belong to
  510. * \param pt Point to check
  511. *
  512. * \return 0 if point is a valid public key,
  513. * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
  514. *
  515. * \note This function only checks the point is non-zero, has valid
  516. * coordinates and lies on the curve, but not that it is
  517. * indeed a multiple of G. This is additional check is more
  518. * expensive, isn't required by standards, and shouldn't be
  519. * necessary if the group used has a small cofactor. In
  520. * particular, it is useless for the NIST groups which all
  521. * have a cofactor of 1.
  522. *
  523. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  524. * in order to ease use with other structures such as
  525. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  526. */
  527. int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );
  528. /**
  529. * \brief Check that an mbedtls_mpi is a valid private key for this curve
  530. *
  531. * \param grp Group used
  532. * \param d Integer to check
  533. *
  534. * \return 0 if point is a valid private key,
  535. * MBEDTLS_ERR_ECP_INVALID_KEY otherwise.
  536. *
  537. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  538. * in order to ease use with other structures such as
  539. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  540. */
  541. int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );
  542. /**
  543. * \brief Generate a keypair with configurable base point
  544. *
  545. * \param grp ECP group
  546. * \param G Chosen base point
  547. * \param d Destination MPI (secret part)
  548. * \param Q Destination point (public part)
  549. * \param f_rng RNG function
  550. * \param p_rng RNG parameter
  551. *
  552. * \return 0 if successful,
  553. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  554. *
  555. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  556. * in order to ease use with other structures such as
  557. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  558. */
  559. int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
  560. const mbedtls_ecp_point *G,
  561. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  562. int (*f_rng)(void *, unsigned char *, size_t),
  563. void *p_rng );
  564. /**
  565. * \brief Generate a keypair
  566. *
  567. * \param grp ECP group
  568. * \param d Destination MPI (secret part)
  569. * \param Q Destination point (public part)
  570. * \param f_rng RNG function
  571. * \param p_rng RNG parameter
  572. *
  573. * \return 0 if successful,
  574. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  575. *
  576. * \note Uses bare components rather than an mbedtls_ecp_keypair structure
  577. * in order to ease use with other structures such as
  578. * mbedtls_ecdh_context of mbedtls_ecdsa_context.
  579. */
  580. int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  581. int (*f_rng)(void *, unsigned char *, size_t),
  582. void *p_rng );
  583. /**
  584. * \brief Generate a keypair
  585. *
  586. * \param grp_id ECP group identifier
  587. * \param key Destination keypair
  588. * \param f_rng RNG function
  589. * \param p_rng RNG parameter
  590. *
  591. * \return 0 if successful,
  592. * or a MBEDTLS_ERR_ECP_XXX or MBEDTLS_MPI_XXX error code
  593. */
  594. int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
  595. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
  596. /**
  597. * \brief Check a public-private key pair
  598. *
  599. * \param pub Keypair structure holding a public key
  600. * \param prv Keypair structure holding a private (plus public) key
  601. *
  602. * \return 0 if successful (keys are valid and match), or
  603. * MBEDTLS_ERR_ECP_BAD_INPUT_DATA, or
  604. * a MBEDTLS_ERR_ECP_XXX or MBEDTLS_ERR_MPI_XXX code.
  605. */
  606. int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );
  607. #if defined(MBEDTLS_SELF_TEST)
  608. /**
  609. * \brief Checkup routine
  610. *
  611. * \return 0 if successful, or 1 if a test failed
  612. */
  613. int mbedtls_ecp_self_test( int verbose );
  614. #endif
  615. #ifdef __cplusplus
  616. }
  617. #endif
  618. #endif /* ecp.h */