Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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