ecp.h 25 KB

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