Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:50

0001 /**
0002  * \file ecp.h
0003  *
0004  * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
0005  *
0006  * The use of ECP in cryptography and TLS is defined in
0007  * <em>Standards for Efficient Cryptography Group (SECG): SEC1
0008  * Elliptic Curve Cryptography</em> and
0009  * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
0010  * for Transport Layer Security (TLS)</em>.
0011  *
0012  * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
0013  * group types.
0014  *
0015  */
0016 
0017 /*
0018  *  Copyright The Mbed TLS Contributors
0019  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0020  */
0021 
0022 #ifndef MBEDTLS_ECP_H
0023 #define MBEDTLS_ECP_H
0024 #include "mbedtls/private_access.h"
0025 
0026 #include "tf-psa-crypto/build_info.h"
0027 #include "mbedtls/platform_util.h"
0028 
0029 #include "mbedtls/private/bignum.h"
0030 
0031 /*
0032  * ECP error codes
0033  */
0034 /** Bad input parameters to function. */
0035 #define MBEDTLS_ERR_ECP_BAD_INPUT_DATA                    PSA_ERROR_INVALID_ARGUMENT
0036 /** The buffer is too small to write to. */
0037 #define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL                  PSA_ERROR_BUFFER_TOO_SMALL
0038 /** The requested feature is not available, for example, the requested curve is not supported. */
0039 #define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE               -0x4E80
0040 /** The signature is not valid. */
0041 #define MBEDTLS_ERR_ECP_VERIFY_FAILED                     PSA_ERROR_INVALID_SIGNATURE
0042 /** Memory allocation failed. */
0043 #define MBEDTLS_ERR_ECP_ALLOC_FAILED                      PSA_ERROR_INSUFFICIENT_MEMORY
0044 /** Generation of random value, such as ephemeral key, failed. */
0045 #define MBEDTLS_ERR_ECP_RANDOM_FAILED                     -0x4D00
0046 /** Invalid private or public key. */
0047 #define MBEDTLS_ERR_ECP_INVALID_KEY                       -0x4C80
0048 /** Operation in progress, call again with the same parameters to continue. */
0049 #define MBEDTLS_ERR_ECP_IN_PROGRESS                       PSA_OPERATION_INCOMPLETE
0050 
0051 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0052 /* Flags indicating whether to include code that is specific to certain
0053  * types of curves. These flags are for internal library use only. */
0054 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || \
0055     defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \
0056     defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || \
0057     defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
0058     defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || \
0059     defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || \
0060     defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || \
0061     defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || \
0062     defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
0063 #define MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED
0064 #endif
0065 #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || \
0066     defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
0067 #define MBEDTLS_ECP_MONTGOMERY_ENABLED
0068 #endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED OR MBEDTLS_ECP_DP_CURVE448_ENABLED */
0069 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0070 
0071 #ifdef __cplusplus
0072 extern "C" {
0073 #endif
0074 
0075 /**
0076  * Domain-parameter identifiers: curve, subgroup, and generator.
0077  *
0078  * \note Only curves over prime fields are supported.
0079  *
0080  * \warning This library does not support validation of arbitrary domain
0081  * parameters. Therefore, only standardized domain parameters from trusted
0082  * sources should be used. See mbedtls_ecp_group_load().
0083  */
0084 /* Note: when adding a new curve:
0085  * - Add it at the end of this enum, otherwise you'll break the ABI by
0086  *   changing the numerical value for existing curves.
0087  * - Increment MBEDTLS_ECP_DP_MAX below if needed.
0088  * - Update the calculation of MBEDTLS_ECP_MAX_BITS below.
0089  * - Add the corresponding MBEDTLS_ECP_DP_xxx_ENABLED macro definition to
0090  *   mbedtls_config.h.
0091  * - List the curve as a dependency of MBEDTLS_ECP_C and
0092  *   MBEDTLS_ECDSA_C if supported in check_config.h.
0093  * - Add the curve to the appropriate curve type macro
0094  *   MBEDTLS_ECP_yyy_ENABLED above.
0095  * - Add the necessary definitions to ecp_curves.c.
0096  * - Add the curve to the ecp_supported_curves array in ecp.c.
0097  * - Add the curve to applicable profiles in x509_crt.c.
0098  * - Add the curve to applicable presets in ssl_tls.c.
0099  */
0100 typedef enum {
0101     MBEDTLS_ECP_DP_NONE = 0,       /*!< Curve not defined. */
0102     MBEDTLS_ECP_DP_SECP192R1,      /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
0103     MBEDTLS_ECP_DP_SECP256R1,      /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
0104     MBEDTLS_ECP_DP_SECP384R1,      /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
0105     MBEDTLS_ECP_DP_SECP521R1,      /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
0106     MBEDTLS_ECP_DP_BP256R1,        /*!< Domain parameters for 256-bit Brainpool curve. */
0107     MBEDTLS_ECP_DP_BP384R1,        /*!< Domain parameters for 384-bit Brainpool curve. */
0108     MBEDTLS_ECP_DP_BP512R1,        /*!< Domain parameters for 512-bit Brainpool curve. */
0109     MBEDTLS_ECP_DP_CURVE25519,     /*!< Domain parameters for Curve25519. */
0110     MBEDTLS_ECP_DP_SECP192K1,      /*!< Domain parameters for 192-bit "Koblitz" curve. */
0111     MBEDTLS_ECP_DP_SECP256K1,      /*!< Domain parameters for 256-bit "Koblitz" curve. */
0112     MBEDTLS_ECP_DP_CURVE448,       /*!< Domain parameters for Curve448. */
0113 } mbedtls_ecp_group_id;
0114 
0115 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0116 /**
0117  * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
0118  */
0119 #define MBEDTLS_ECP_DP_MAX     14
0120 
0121 /*
0122  * Curve types
0123  */
0124 typedef enum {
0125     MBEDTLS_ECP_TYPE_NONE = 0,
0126     MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS,    /* y^2 = x^3 + a x + b      */
0127     MBEDTLS_ECP_TYPE_MONTGOMERY,           /* y^2 = x^3 + a x^2 + x    */
0128 } mbedtls_ecp_curve_type;
0129 
0130 /**
0131  * Curve information, for use by other modules.
0132  *
0133  * The fields of this structure are part of the public API and can be
0134  * accessed directly by applications. Future versions of the library may
0135  * add extra fields or reorder existing fields.
0136  */
0137 typedef struct mbedtls_ecp_curve_info {
0138     mbedtls_ecp_group_id grp_id;    /*!< An internal identifier. */
0139     uint16_t tls_id;                /*!< The TLS NamedCurve identifier. */
0140     uint16_t bit_size;              /*!< The curve size in bits. */
0141     const char *name;               /*!< A human-friendly name. */
0142 } mbedtls_ecp_curve_info;
0143 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0144 
0145 /**
0146  * \brief           The ECP point structure, in Jacobian coordinates.
0147  *
0148  * \note            All functions expect and return points satisfying
0149  *                  the following condition: <code>Z == 0</code> or
0150  *                  <code>Z == 1</code>. Other values of \p Z are
0151  *                  used only by internal functions.
0152  *                  The point is zero, or "at infinity", if <code>Z == 0</code>.
0153  *                  Otherwise, \p X and \p Y are its standard (affine)
0154  *                  coordinates.
0155  */
0156 typedef struct mbedtls_ecp_point {
0157     mbedtls_mpi MBEDTLS_PRIVATE(X);          /*!< The X coordinate of the ECP point. */
0158     mbedtls_mpi MBEDTLS_PRIVATE(Y);          /*!< The Y coordinate of the ECP point. */
0159     mbedtls_mpi MBEDTLS_PRIVATE(Z);          /*!< The Z coordinate of the ECP point. */
0160 }
0161 mbedtls_ecp_point;
0162 
0163 #define MBEDTLS_ECP_POINT_INIT { MBEDTLS_MPI_INIT, MBEDTLS_MPI_INIT, MBEDTLS_MPI_INIT }
0164 
0165 /**
0166  * \brief           The ECP group structure.
0167  *
0168  * We consider two types of curve equations:
0169  * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
0170  * (SEC1 + RFC-4492)</li>
0171  * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
0172  * Curve448)</li></ul>
0173  * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
0174  *
0175  * For Short Weierstrass, this subgroup is the whole curve, and its
0176  * cardinality is denoted by \p N. Our code requires that \p N is an
0177  * odd prime as mbedtls_ecp_mul() requires an odd number, and
0178  * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
0179  *
0180  * The default implementation only initializes \p A without setting it to the
0181  * authentic value for curves with <code>A = -3</code>(SECP256R1, etc), in which
0182  * case you need to load \p A by yourself when using domain parameters directly,
0183  * for example:
0184  * \code
0185  * mbedtls_mpi_init(&A);
0186  * mbedtls_ecp_group_init(&grp);
0187  * CHECK_RETURN(mbedtls_ecp_group_load(&grp, grp_id));
0188  * if (mbedtls_ecp_group_a_is_minus_3(&grp)) {
0189  *     CHECK_RETURN(mbedtls_mpi_sub_int(&A, &grp.P, 3));
0190  * } else {
0191  *     CHECK_RETURN(mbedtls_mpi_copy(&A, &grp.A));
0192  * }
0193  *
0194  * do_something_with_a(&A);
0195  *
0196  * cleanup:
0197  * mbedtls_mpi_free(&A);
0198  * mbedtls_ecp_group_free(&grp);
0199  * \endcode
0200  *
0201  * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
0202  * which is the quantity used in the formulas. Additionally, \p nbits is
0203  * not the size of \p N but the required size for private keys.
0204  *
0205  * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
0206  * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
0207  * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
0208  * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
0209  * in size, so that it may be efficiently brought in the 0..P-1 range by a few
0210  * additions or subtractions. Therefore, it is only an approximate modular
0211  * reduction. It must return 0 on success and non-zero on failure.
0212  *
0213  * \note        Alternative implementations of the ECP module must obey the
0214  *              following constraints.
0215  *              * Group IDs must be distinct: if two group structures have
0216  *                the same ID, then they must be identical.
0217  *              * The fields \c id, \c P, \c A, \c B, \c G, \c N,
0218  *                \c pbits and \c nbits must have the same type and semantics
0219  *                as in the built-in implementation.
0220  *                They must be available for reading, but direct modification
0221  *                of these fields does not need to be supported.
0222  *                They do not need to be at the same offset in the structure.
0223  */
0224 typedef struct mbedtls_ecp_group {
0225     mbedtls_ecp_group_id id;    /*!< An internal group identifier. */
0226     mbedtls_mpi P;              /*!< The prime modulus of the base field. */
0227     mbedtls_mpi A;              /*!< For Short Weierstrass: \p A in the equation. Note that
0228                                      \p A is not set to the authentic value in some cases.
0229                                      Refer to detailed description of ::mbedtls_ecp_group if
0230                                      using domain parameters in the structure.
0231                                      For Montgomery curves: <code>(A + 2) / 4</code>. */
0232     mbedtls_mpi B;              /*!< For Short Weierstrass: \p B in the equation.
0233                                      For Montgomery curves: unused. */
0234     mbedtls_ecp_point G;        /*!< The generator of the subgroup used. */
0235     mbedtls_mpi N;              /*!< The order of \p G. */
0236     size_t pbits;               /*!< The number of bits in \p P.*/
0237     size_t nbits;               /*!< For Short Weierstrass: The number of bits in \p P.
0238                                      For Montgomery curves: the number of bits in the
0239                                      private keys. */
0240     /* End of public fields */
0241 
0242     unsigned int MBEDTLS_PRIVATE(h);             /*!< \internal 1 if the constants are static. */
0243     int(*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *);  /*!< The function for fast pseudo-reduction
0244                                                     mod \p P (see above).*/
0245     int(*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *);   /*!< Unused. */
0246     int(*MBEDTLS_PRIVATE(t_post))(mbedtls_ecp_point *, void *);  /*!< Unused. */
0247     void *MBEDTLS_PRIVATE(t_data);               /*!< Unused. */
0248     mbedtls_ecp_point *MBEDTLS_PRIVATE(T);       /*!< Pre-computed points for ecp_mul_comb(). */
0249     size_t MBEDTLS_PRIVATE(T_size);              /*!< The number of dynamic allocated pre-computed points. */
0250 }
0251 mbedtls_ecp_group;
0252 
0253 #define MBEDTLS_ECP_GROUP_INIT { MBEDTLS_ECP_DP_NONE, MBEDTLS_MPI_INIT, MBEDTLS_MPI_INIT, \
0254                                  MBEDTLS_MPI_INIT, MBEDTLS_ECP_POINT_INIT, MBEDTLS_MPI_INIT, \
0255                                  0, 0, 0, NULL, NULL, NULL, NULL, NULL, 0 }
0256 
0257 /**
0258  * \name SECTION: Module settings
0259  *
0260  * The configuration options you can set for this module are in this section.
0261  * Either change them in mbedtls_config.h, or define them using the compiler command line.
0262  * \{
0263  */
0264 
0265 #if !defined(MBEDTLS_ECP_WINDOW_SIZE)
0266 /*
0267  * Maximum "window" size used for point multiplication.
0268  * Default: a point where higher memory usage yields diminishing performance
0269  *          returns.
0270  * Minimum value: 2. Maximum value: 7.
0271  *
0272  * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
0273  * points used for point multiplication. This value is directly tied to EC
0274  * peak memory usage, so decreasing it by one should roughly cut memory usage
0275  * by two (if large curves are in use).
0276  *
0277  * Reduction in size may reduce speed, but larger curves are impacted first.
0278  * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
0279  *      w-size:     6       5       4       3       2
0280  *      521       145     141     135     120      97
0281  *      384       214     209     198     177     146
0282  *      256       320     320     303     262     226
0283  *      224       475     475     453     398     342
0284  *      192       640     640     633     587     476
0285  */
0286 #define MBEDTLS_ECP_WINDOW_SIZE    4   /**< The maximum window size used. */
0287 #endif /* MBEDTLS_ECP_WINDOW_SIZE */
0288 
0289 #if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
0290 /*
0291  * Trade code size for speed on fixed-point multiplication.
0292  *
0293  * This speeds up repeated multiplication of the generator (that is, the
0294  * multiplication in ECDSA signatures, and half of the multiplications in
0295  * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
0296  *
0297  * For each n-bit Short Weierstrass curve that is enabled, this adds 4n bytes
0298  * of code size if n < 384 and 8n otherwise.
0299  *
0300  * Change this value to 0 to reduce code size.
0301  */
0302 #define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up. */
0303 #endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */
0304 
0305 /** \} name SECTION: Module settings */
0306 
0307 /**
0308  * The maximum size of the groups, that is, of \c N and \c P.
0309  */
0310 #if !defined(MBEDTLS_ECP_LIGHT)
0311 /* Dummy definition to help code that has optional ECP support and
0312  * defines an MBEDTLS_ECP_MAX_BYTES-sized array unconditionally. */
0313 #define MBEDTLS_ECP_MAX_BITS 1
0314 /* Note: the curves must be listed in DECREASING size! */
0315 #elif defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
0316 #define MBEDTLS_ECP_MAX_BITS 521
0317 #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
0318 #define MBEDTLS_ECP_MAX_BITS 512
0319 #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
0320 #define MBEDTLS_ECP_MAX_BITS 448
0321 #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
0322 #define MBEDTLS_ECP_MAX_BITS 384
0323 #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
0324 #define MBEDTLS_ECP_MAX_BITS 384
0325 #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
0326 #define MBEDTLS_ECP_MAX_BITS 256
0327 #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
0328 #define MBEDTLS_ECP_MAX_BITS 256
0329 #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
0330 #define MBEDTLS_ECP_MAX_BITS 256
0331 #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
0332 #define MBEDTLS_ECP_MAX_BITS 255
0333 #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
0334 #define MBEDTLS_ECP_MAX_BITS 192
0335 #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
0336 #define MBEDTLS_ECP_MAX_BITS 192
0337 #else /* !MBEDTLS_ECP_LIGHT */
0338 #error "Missing definition of MBEDTLS_ECP_MAX_BITS"
0339 #endif /* !MBEDTLS_ECP_LIGHT */
0340 
0341 #define MBEDTLS_ECP_MAX_BYTES    ((MBEDTLS_ECP_MAX_BITS + 7) / 8)
0342 #define MBEDTLS_ECP_MAX_PT_LEN   (2 * MBEDTLS_ECP_MAX_BYTES + 1)
0343 
0344 #if defined(MBEDTLS_ECP_RESTARTABLE)
0345 
0346 /**
0347  * \brief           Internal restart context for multiplication
0348  *
0349  * \note            Opaque struct
0350  */
0351 typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;
0352 
0353 /**
0354  * \brief           Internal restart context for ecp_muladd()
0355  *
0356  * \note            Opaque struct
0357  */
0358 typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;
0359 
0360 /**
0361  * \brief           General context for resuming ECC operations
0362  */
0363 typedef struct {
0364     unsigned MBEDTLS_PRIVATE(ops_done);                  /*!<  current ops count             */
0365     unsigned MBEDTLS_PRIVATE(depth);                     /*!<  call depth (0 = top-level)    */
0366     mbedtls_ecp_restart_mul_ctx *MBEDTLS_PRIVATE(rsm);   /*!<  ecp_mul_comb() sub-context    */
0367     mbedtls_ecp_restart_muladd_ctx *MBEDTLS_PRIVATE(ma); /*!<  ecp_muladd() sub-context      */
0368 } mbedtls_ecp_restart_ctx;
0369 
0370 #define MBEDTLS_ECP_RESTART_INIT { 0, 0, NULL, NULL }
0371 
0372 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0373 /*
0374  * Operation counts for restartable functions
0375  */
0376 #define MBEDTLS_ECP_OPS_CHK   3 /*!< basic ops count for ecp_check_pubkey()  */
0377 #define MBEDTLS_ECP_OPS_DBL   8 /*!< basic ops count for ecp_double_jac()    */
0378 #define MBEDTLS_ECP_OPS_ADD  11 /*!< basic ops count for see ecp_add_mixed() */
0379 #define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv()  */
0380 
0381 /**
0382  * \brief           Internal; for restartable functions in other modules.
0383  *                  Check and update basic ops budget.
0384  *
0385  * \param grp       Group structure
0386  * \param rs_ctx    Restart context
0387  * \param ops       Number of basic ops to do
0388  *
0389  * \return          \c 0 if doing \p ops basic ops is still allowed,
0390  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
0391  */
0392 int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
0393                              mbedtls_ecp_restart_ctx *rs_ctx,
0394                              unsigned ops);
0395 
0396 /* Utility macro for checking and updating ops budget */
0397 #define MBEDTLS_ECP_BUDGET(ops)   \
0398     MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, rs_ctx, \
0399                                              (unsigned) (ops)));
0400 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0401 
0402 #else /* MBEDTLS_ECP_RESTARTABLE */
0403 
0404 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0405 #define MBEDTLS_ECP_BUDGET(ops)     /* no-op; for compatibility */
0406 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0407 
0408 /* We want to declare restartable versions of existing functions anyway */
0409 typedef void mbedtls_ecp_restart_ctx;
0410 
0411 #endif /* MBEDTLS_ECP_RESTARTABLE */
0412 
0413 /**
0414  * \brief    The ECP key-pair structure.
0415  *
0416  * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
0417  *
0418  * \note    Members are deliberately in the same order as in the
0419  *          ::mbedtls_ecdsa_context structure.
0420  */
0421 typedef struct mbedtls_ecp_keypair {
0422     mbedtls_ecp_group MBEDTLS_PRIVATE(grp);      /*!<  Elliptic curve and base point     */
0423     mbedtls_mpi MBEDTLS_PRIVATE(d);              /*!<  our secret value                  */
0424     mbedtls_ecp_point MBEDTLS_PRIVATE(Q);        /*!<  our public value                  */
0425 }
0426 mbedtls_ecp_keypair;
0427 
0428 #define MBEDTLS_ECP_KEYPAIR_INIT { MBEDTLS_ECP_GROUP_INIT, MBEDTLS_MPI_INIT, \
0429                                    MBEDTLS_ECP_POINT_INIT }
0430 
0431 /**
0432  * The uncompressed point format for Short Weierstrass curves
0433  * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX).
0434  */
0435 #define MBEDTLS_ECP_PF_UNCOMPRESSED    0
0436 /**
0437  * The compressed point format for Short Weierstrass curves
0438  * (MBEDTLS_ECP_DP_SECP_XXX and MBEDTLS_ECP_DP_BP_XXX).
0439  */
0440 #define MBEDTLS_ECP_PF_COMPRESSED      1
0441 
0442 /*
0443  * Some other constants from RFC 4492
0444  */
0445 #define MBEDTLS_ECP_TLS_NAMED_CURVE    3   /**< The named_curve of ECCurveType. */
0446 
0447 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0448 #if defined(MBEDTLS_ECP_RESTARTABLE)
0449 /**
0450  * \brief           Set the maximum number of basic operations done in a row.
0451  *
0452  *                  If more operations are needed to complete a computation,
0453  *                  #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
0454  *                  function performing the computation. It is then the
0455  *                  caller's responsibility to either call again with the same
0456  *                  parameters until it returns 0 or an error code; or to free
0457  *                  the restart context if the operation is to be aborted.
0458  *
0459  *                  It is strictly required that all input parameters and the
0460  *                  restart context be the same on successive calls for the
0461  *                  same operation, but output parameters need not be the
0462  *                  same; they must not be used until the function finally
0463  *                  returns 0.
0464  *
0465  *                  This only applies to functions whose documentation
0466  *                  mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS.
0467  *                  For functions that accept a "restart context"
0468  *                  argument, passing NULL disables restart and makes the
0469  *                  function equivalent to the function with the same name
0470  *                  with \c _restartable removed. For functions in the ECDH
0471  *                  module, restart is disabled unless the function accepts
0472  *                  an "ECDH context" argument and
0473  *                  mbedtls_ecdh_enable_restart() was previously called on
0474  *                  that context. For function in the SSL module, restart is
0475  *                  only enabled for specific sides and key exchanges
0476  *                  (currently only for clients and ECDHE-ECDSA).
0477  *
0478  * \warning         Using the PSA interruptible interfaces with keys in local
0479  *                  storage and no accelerator driver will also call this
0480  *                  function to set the values specified via those interfaces,
0481  *                  overwriting values previously set. Care should be taken if
0482  *                  mixing these two interfaces.
0483  *
0484  * \param max_ops   Maximum number of basic operations done in a row.
0485  *                  Default: 0 (unlimited).
0486  *                  Lower (non-zero) values mean ECC functions will block for
0487  *                  a lesser maximum amount of time.
0488  *
0489  * \note            A "basic operation" is defined as a rough equivalent of a
0490  *                  multiplication in GF(p) for the NIST P-256 curve.
0491  *                  As an indication, with default settings, a scalar
0492  *                  multiplication (full run of \c mbedtls_ecp_mul()) is:
0493  *                  - about 3300 basic operations for P-256
0494  *                  - about 9400 basic operations for P-384
0495  *
0496  * \note            Very low values are not always respected: sometimes
0497  *                  functions need to block for a minimum number of
0498  *                  operations, and will do so even if max_ops is set to a
0499  *                  lower value.  That minimum depends on the curve size, and
0500  *                  can be made lower by decreasing the value of
0501  *                  \c MBEDTLS_ECP_WINDOW_SIZE.  As an indication, here is the
0502  *                  lowest effective value for various curves and values of
0503  *                  that parameter (w for short):
0504  *                          w=6     w=5     w=4     w=3     w=2
0505  *                  P-256   208     208     160     136     124
0506  *                  P-384   682     416     320     272     248
0507  *                  P-521  1364     832     640     544     496
0508  *
0509  * \note            This setting is currently ignored by Curve25519.
0510  */
0511 void mbedtls_ecp_set_max_ops(unsigned max_ops);
0512 
0513 /**
0514  * \brief           Check if restart is enabled (max_ops != 0)
0515  *
0516  * \return          \c 0 if \c max_ops == 0 (restart disabled)
0517  * \return          \c 1 otherwise (restart enabled)
0518  */
0519 int mbedtls_ecp_restart_is_enabled(void);
0520 #endif /* MBEDTLS_ECP_RESTARTABLE */
0521 
0522 /*
0523  * Get the type of a curve
0524  */
0525 mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp);
0526 
0527 /**
0528  * \brief           This function retrieves the information defined in
0529  *                  mbedtls_ecp_curve_info() for all supported curves.
0530  *
0531  * \note            This function returns information about all curves
0532  *                  supported by the library. Some curves may not be
0533  *                  supported for all algorithms. Call mbedtls_ecdh_can_do()
0534  *                  or mbedtls_ecdsa_can_do() to check if a curve is
0535  *                  supported for ECDH or ECDSA.
0536  *
0537  * \return          A statically allocated array. The last entry is 0.
0538  */
0539 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void);
0540 
0541 /**
0542  * \brief           This function retrieves the list of internal group
0543  *                  identifiers of all supported curves in the order of
0544  *                  preference.
0545  *
0546  * \note            This function returns information about all curves
0547  *                  supported by the library. Some curves may not be
0548  *                  supported for all algorithms. Call mbedtls_ecdh_can_do()
0549  *                  or mbedtls_ecdsa_can_do() to check if a curve is
0550  *                  supported for ECDH or ECDSA.
0551  *
0552  * \return          A statically allocated array,
0553  *                  terminated with MBEDTLS_ECP_DP_NONE.
0554  */
0555 const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void);
0556 
0557 /**
0558  * \brief           This function retrieves curve information from an internal
0559  *                  group identifier.
0560  *
0561  * \param grp_id    An \c MBEDTLS_ECP_DP_XXX value.
0562  *
0563  * \return          The associated curve information on success.
0564  * \return          NULL on failure.
0565  */
0566 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id);
0567 
0568 /**
0569  * \brief           This function retrieves curve information from a TLS
0570  *                  NamedCurve value.
0571  *
0572  * \param tls_id    An \c MBEDTLS_ECP_DP_XXX value.
0573  *
0574  * \return          The associated curve information on success.
0575  * \return          NULL on failure.
0576  */
0577 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id);
0578 
0579 /**
0580  * \brief           This function retrieves curve information from a
0581  *                  human-readable name.
0582  *
0583  * \param name      The human-readable name.
0584  *
0585  * \return          The associated curve information on success.
0586  * \return          NULL on failure.
0587  */
0588 const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name);
0589 
0590 /**
0591  * \brief           This function initializes a point as zero.
0592  *
0593  * \param pt        The point to initialize.
0594  */
0595 void mbedtls_ecp_point_init(mbedtls_ecp_point *pt);
0596 
0597 /**
0598  * \brief           This function initializes an ECP group context
0599  *                  without loading any domain parameters.
0600  *
0601  * \note            After this function is called, domain parameters
0602  *                  for various ECP groups can be loaded through the
0603  *                  mbedtls_ecp_group_load() or mbedtls_ecp_tls_read_group()
0604  *                  functions.
0605  */
0606 void mbedtls_ecp_group_init(mbedtls_ecp_group *grp);
0607 
0608 /**
0609  * \brief           This function initializes a key pair as an invalid one.
0610  *
0611  * \param key       The key pair to initialize.
0612  */
0613 void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key);
0614 
0615 /**
0616  * \brief           This function frees the components of a point.
0617  *
0618  * \param pt        The point to free.
0619  */
0620 void mbedtls_ecp_point_free(mbedtls_ecp_point *pt);
0621 
0622 /**
0623  * \brief           This function frees the components of an ECP group.
0624  *
0625  * \param grp       The group to free. This may be \c NULL, in which
0626  *                  case this function returns immediately. If it is not
0627  *                  \c NULL, it must point to an initialized ECP group.
0628  */
0629 void mbedtls_ecp_group_free(mbedtls_ecp_group *grp);
0630 
0631 /**
0632  * \brief           This function frees the components of a key pair.
0633  *
0634  * \param key       The key pair to free. This may be \c NULL, in which
0635  *                  case this function returns immediately. If it is not
0636  *                  \c NULL, it must point to an initialized ECP key pair.
0637  */
0638 void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key);
0639 
0640 #if defined(MBEDTLS_ECP_RESTARTABLE)
0641 /**
0642  * \brief           Initialize a restart context.
0643  *
0644  * \param ctx       The restart context to initialize. This must
0645  *                  not be \c NULL.
0646  */
0647 void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx);
0648 
0649 /**
0650  * \brief           Free the components of a restart context.
0651  *
0652  * \param ctx       The restart context to free. This may be \c NULL, in which
0653  *                  case this function returns immediately. If it is not
0654  *                  \c NULL, it must point to an initialized restart context.
0655  */
0656 void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx);
0657 #endif /* MBEDTLS_ECP_RESTARTABLE */
0658 
0659 /**
0660  * \brief           This function copies the contents of point \p Q into
0661  *                  point \p P.
0662  *
0663  * \param P         The destination point. This must be initialized.
0664  * \param Q         The source point. This must be initialized.
0665  *
0666  * \return          \c 0 on success.
0667  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
0668  * \return          Another negative error code for other kinds of failure.
0669  */
0670 int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q);
0671 
0672 /**
0673  * \brief           This function copies the contents of group \p src into
0674  *                  group \p dst.
0675  *
0676  * \param dst       The destination group. This must be initialized.
0677  * \param src       The source group. This must be initialized.
0678  *
0679  * \return          \c 0 on success.
0680  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
0681  * \return          Another negative error code on other kinds of failure.
0682  */
0683 int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst,
0684                            const mbedtls_ecp_group *src);
0685 
0686 /**
0687  * \brief           This function sets a point to the point at infinity.
0688  *
0689  * \param pt        The point to set. This must be initialized.
0690  *
0691  * \return          \c 0 on success.
0692  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
0693  * \return          Another negative error code on other kinds of failure.
0694  */
0695 int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt);
0696 
0697 /**
0698  * \brief           This function checks if a point is the point at infinity.
0699  *
0700  * \param pt        The point to test. This must be initialized.
0701  *
0702  * \return          \c 1 if the point is zero.
0703  * \return          \c 0 if the point is non-zero.
0704  * \return          A negative error code on failure.
0705  */
0706 int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt);
0707 
0708 /**
0709  * \brief           This function compares two points.
0710  *
0711  * \note            This assumes that the points are normalized. Otherwise,
0712  *                  they may compare as "not equal" even if they are.
0713  *
0714  * \param P         The first point to compare. This must be initialized.
0715  * \param Q         The second point to compare. This must be initialized.
0716  *
0717  * \return          \c 0 if the points are equal.
0718  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
0719  */
0720 int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
0721                           const mbedtls_ecp_point *Q);
0722 
0723 /**
0724  * \brief           This function imports a non-zero point from two ASCII
0725  *                  strings.
0726  *
0727  * \param P         The destination point. This must be initialized.
0728  * \param radix     The numeric base of the input.
0729  * \param x         The first affine coordinate, as a null-terminated string.
0730  * \param y         The second affine coordinate, as a null-terminated string.
0731  *
0732  * \return          \c 0 on success.
0733  * \return          An \c MBEDTLS_ERR_MPI_XXX error code on failure.
0734  */
0735 int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
0736                                   const char *x, const char *y);
0737 
0738 /**
0739  * \brief           This function exports a point into unsigned binary data.
0740  *
0741  * \param grp       The group to which the point should belong.
0742  *                  This must be initialized and have group parameters
0743  *                  set, for example through mbedtls_ecp_group_load().
0744  * \param P         The point to export. This must be initialized.
0745  * \param format    The point format. This must be either
0746  *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
0747  *                  (For groups without these formats, this parameter is
0748  *                  ignored. But it still has to be either of the above
0749  *                  values.)
0750  * \param olen      The address at which to store the length of
0751  *                  the output in Bytes. This must not be \c NULL.
0752  * \param buf       The output buffer. This must be a writable buffer
0753  *                  of length \p buflen Bytes.
0754  * \param buflen    The length of the output buffer \p buf in Bytes.
0755  *
0756  * \return          \c 0 on success.
0757  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
0758  *                  is too small to hold the point.
0759  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
0760  *                  or the export for the given group is not implemented.
0761  * \return          Another negative error code on other kinds of failure.
0762  */
0763 int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
0764                                    const mbedtls_ecp_point *P,
0765                                    int format, size_t *olen,
0766                                    unsigned char *buf, size_t buflen);
0767 
0768 /**
0769  * \brief           This function imports a point from unsigned binary data.
0770  *
0771  * \note            This function does not check that the point actually
0772  *                  belongs to the given group, see mbedtls_ecp_check_pubkey()
0773  *                  for that.
0774  *
0775  * \note            For compressed points, see #MBEDTLS_ECP_PF_COMPRESSED for
0776  *                  limitations.
0777  *
0778  * \param grp       The group to which the point should belong.
0779  *                  This must be initialized and have group parameters
0780  *                  set, for example through mbedtls_ecp_group_load().
0781  * \param P         The destination context to import the point to.
0782  *                  This must be initialized.
0783  * \param buf       The input buffer. This must be a readable buffer
0784  *                  of length \p ilen Bytes.
0785  * \param ilen      The length of the input buffer \p buf in Bytes.
0786  *
0787  * \return          \c 0 on success.
0788  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
0789  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
0790  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the import for the
0791  *                  given group is not implemented.
0792  */
0793 int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
0794                                   mbedtls_ecp_point *P,
0795                                   const unsigned char *buf, size_t ilen);
0796 
0797 /**
0798  * \brief           This function imports a point from a TLS ECPoint record.
0799  *
0800  * \note            On function return, \p *buf is updated to point immediately
0801  *                  after the ECPoint record.
0802  *
0803  * \param grp       The ECP group to use.
0804  *                  This must be initialized and have group parameters
0805  *                  set, for example through mbedtls_ecp_group_load().
0806  * \param pt        The destination point.
0807  * \param buf       The address of the pointer to the start of the input buffer.
0808  * \param len       The length of the buffer.
0809  *
0810  * \return          \c 0 on success.
0811  * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization
0812  *                  failure.
0813  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
0814  */
0815 int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
0816                                mbedtls_ecp_point *pt,
0817                                const unsigned char **buf, size_t len);
0818 
0819 /**
0820  * \brief           This function exports a point as a TLS ECPoint record
0821  *                  defined in RFC 4492, Section 5.4.
0822  *
0823  * \param grp       The ECP group to use.
0824  *                  This must be initialized and have group parameters
0825  *                  set, for example through mbedtls_ecp_group_load().
0826  * \param pt        The point to be exported. This must be initialized.
0827  * \param format    The point format to use. This must be either
0828  *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
0829  * \param olen      The address at which to store the length in Bytes
0830  *                  of the data written.
0831  * \param buf       The target buffer. This must be a writable buffer of
0832  *                  length \p blen Bytes.
0833  * \param blen      The length of the target buffer \p buf in Bytes.
0834  *
0835  * \return          \c 0 on success.
0836  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
0837  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
0838  *                  is too small to hold the exported point.
0839  * \return          Another negative error code on other kinds of failure.
0840  */
0841 int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp,
0842                                 const mbedtls_ecp_point *pt,
0843                                 int format, size_t *olen,
0844                                 unsigned char *buf, size_t blen);
0845 
0846 /**
0847  * \brief           This function sets up an ECP group context
0848  *                  from a standardized set of domain parameters.
0849  *
0850  * \note            The index should be a value of the NamedCurve enum,
0851  *                  as defined in <em>RFC-4492: Elliptic Curve Cryptography
0852  *                  (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
0853  *                  usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
0854  *
0855  * \param grp       The group context to setup. This must be initialized.
0856  * \param id        The identifier of the domain parameter set to load.
0857  *
0858  * \return          \c 0 on success.
0859  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
0860  *                  correspond to a known group.
0861  * \return          Another negative error code on other kinds of failure.
0862  */
0863 int mbedtls_ecp_group_load(mbedtls_ecp_group *grp, mbedtls_ecp_group_id id);
0864 
0865 /**
0866  * \brief           This function sets up an ECP group context from a TLS
0867  *                  ECParameters record as defined in RFC 4492, Section 5.4.
0868  *
0869  * \note            The read pointer \p buf is updated to point right after
0870  *                  the ECParameters record on exit.
0871  *
0872  * \param grp       The group context to setup. This must be initialized.
0873  * \param buf       The address of the pointer to the start of the input buffer.
0874  * \param len       The length of the input buffer \c *buf in Bytes.
0875  *
0876  * \return          \c 0 on success.
0877  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
0878  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
0879  *                  recognized.
0880  * \return          Another negative error code on other kinds of failure.
0881  */
0882 int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
0883                                const unsigned char **buf, size_t len);
0884 
0885 /**
0886  * \brief           This function extracts an elliptic curve group ID from a
0887  *                  TLS ECParameters record as defined in RFC 4492, Section 5.4.
0888  *
0889  * \note            The read pointer \p buf is updated to point right after
0890  *                  the ECParameters record on exit.
0891  *
0892  * \param grp       The address at which to store the group id.
0893  *                  This must not be \c NULL.
0894  * \param buf       The address of the pointer to the start of the input buffer.
0895  * \param len       The length of the input buffer \c *buf in Bytes.
0896  *
0897  * \return          \c 0 on success.
0898  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
0899  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
0900  *                  recognized.
0901  * \return          Another negative error code on other kinds of failure.
0902  */
0903 int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
0904                                   const unsigned char **buf,
0905                                   size_t len);
0906 /**
0907  * \brief           This function exports an elliptic curve as a TLS
0908  *                  ECParameters record as defined in RFC 4492, Section 5.4.
0909  *
0910  * \param grp       The ECP group to be exported.
0911  *                  This must be initialized and have group parameters
0912  *                  set, for example through mbedtls_ecp_group_load().
0913  * \param olen      The address at which to store the number of Bytes written.
0914  *                  This must not be \c NULL.
0915  * \param buf       The buffer to write to. This must be a writable buffer
0916  *                  of length \p blen Bytes.
0917  * \param blen      The length of the output buffer \p buf in Bytes.
0918  *
0919  * \return          \c 0 on success.
0920  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
0921  *                  buffer is too small to hold the exported group.
0922  * \return          Another negative error code on other kinds of failure.
0923  */
0924 int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp,
0925                                 size_t *olen,
0926                                 unsigned char *buf, size_t blen);
0927 
0928 /**
0929  * \brief           This function performs a scalar multiplication of a point
0930  *                  by an integer: \p R = \p m * \p P.
0931  *
0932  *                  It is not thread-safe to use same group in multiple threads.
0933  *
0934  * \note            To prevent timing attacks, this function
0935  *                  executes the exact same sequence of base-field
0936  *                  operations for any valid \p m. It avoids any if-branch or
0937  *                  array index depending on the value of \p m. It also uses
0938  *                  \p f_rng to randomize some intermediate results.
0939  *
0940  * \param grp       The ECP group to use.
0941  *                  This must be initialized and have group parameters
0942  *                  set, for example through mbedtls_ecp_group_load().
0943  * \param R         The point in which to store the result of the calculation.
0944  *                  This must be initialized.
0945  * \param m         The integer by which to multiply. This must be initialized.
0946  * \param P         The point to multiply. This must be initialized.
0947  * \param f_rng     The RNG function. This must not be \c NULL.
0948  * \param p_rng     The RNG context to be passed to \p f_rng. This may be \c
0949  *                  NULL if \p f_rng doesn't need a context.
0950  *
0951  * \return          \c 0 on success.
0952  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
0953  *                  key, or \p P is not a valid public key.
0954  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
0955  * \return          Another negative error code on other kinds of failure.
0956  */
0957 int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
0958                     const mbedtls_mpi *m, const mbedtls_ecp_point *P,
0959                     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
0960 
0961 /**
0962  * \brief           This function performs multiplication of a point by
0963  *                  an integer: \p R = \p m * \p P in a restartable way.
0964  *
0965  * \see             mbedtls_ecp_mul()
0966  *
0967  * \note            This function does the same as \c mbedtls_ecp_mul(), but
0968  *                  it can return early and restart according to the limit set
0969  *                  with \c mbedtls_ecp_set_max_ops() to reduce blocking.
0970  *
0971  * \param grp       The ECP group to use.
0972  *                  This must be initialized and have group parameters
0973  *                  set, for example through mbedtls_ecp_group_load().
0974  * \param R         The point in which to store the result of the calculation.
0975  *                  This must be initialized.
0976  * \param m         The integer by which to multiply. This must be initialized.
0977  * \param P         The point to multiply. This must be initialized.
0978  * \param f_rng     The RNG function. This must not be \c NULL.
0979  * \param p_rng     The RNG context to be passed to \p f_rng. This may be \c
0980  *                  NULL if \p f_rng doesn't need a context.
0981  * \param rs_ctx    The restart context (NULL disables restart).
0982  *
0983  * \return          \c 0 on success.
0984  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
0985  *                  key, or \p P is not a valid public key.
0986  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
0987  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
0988  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
0989  * \return          Another negative error code on other kinds of failure.
0990  */
0991 int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
0992                                 const mbedtls_mpi *m, const mbedtls_ecp_point *P,
0993                                 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
0994                                 mbedtls_ecp_restart_ctx *rs_ctx);
0995 
0996 #if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
0997 /**
0998  * \brief           This function checks if domain parameter A of the curve is
0999  *                  \c -3.
1000  *
1001  * \note            This function is only defined for short Weierstrass curves.
1002  *                  It may not be included in builds without any short
1003  *                  Weierstrass curve.
1004  *
1005  * \param grp       The ECP group to use.
1006  *                  This must be initialized and have group parameters
1007  *                  set, for example through mbedtls_ecp_group_load().
1008  *
1009  * \return          \c 1 if <code>A = -3</code>.
1010  * \return          \c 0 Otherwise.
1011  */
1012 static inline int mbedtls_ecp_group_a_is_minus_3(const mbedtls_ecp_group *grp)
1013 {
1014     return grp->A.MBEDTLS_PRIVATE(p) == NULL;
1015 }
1016 
1017 /**
1018  * \brief           This function performs multiplication and addition of two
1019  *                  points by integers: \p R = \p m * \p P + \p n * \p Q
1020  *
1021  *                  It is not thread-safe to use same group in multiple threads.
1022  *
1023  * \note            In contrast to mbedtls_ecp_mul(), this function does not
1024  *                  guarantee a constant execution flow and timing.
1025  *
1026  * \note            This function is only defined for short Weierstrass curves.
1027  *                  It may not be included in builds without any short
1028  *                  Weierstrass curve.
1029  *
1030  * \param grp       The ECP group to use.
1031  *                  This must be initialized and have group parameters
1032  *                  set, for example through mbedtls_ecp_group_load().
1033  * \param R         The point in which to store the result of the calculation.
1034  *                  This must be initialized.
1035  * \param m         The integer by which to multiply \p P.
1036  *                  This must be initialized.
1037  * \param P         The point to multiply by \p m. This must be initialized.
1038  * \param n         The integer by which to multiply \p Q.
1039  *                  This must be initialized.
1040  * \param Q         The point to be multiplied by \p n.
1041  *                  This must be initialized.
1042  *
1043  * \return          \c 0 on success.
1044  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1045  *                  valid private keys, or \p P or \p Q are not valid public
1046  *                  keys.
1047  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1048  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1049  *                  designate a short Weierstrass curve.
1050  * \return          Another negative error code on other kinds of failure.
1051  */
1052 int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1053                        const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1054                        const mbedtls_mpi *n, const mbedtls_ecp_point *Q);
1055 
1056 /**
1057  * \brief           This function performs multiplication and addition of two
1058  *                  points by integers: \p R = \p m * \p P + \p n * \p Q in a
1059  *                  restartable way.
1060  *
1061  * \see             \c mbedtls_ecp_muladd()
1062  *
1063  * \note            This function works the same as \c mbedtls_ecp_muladd(),
1064  *                  but it can return early and restart according to the limit
1065  *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
1066  *
1067  * \note            This function is only defined for short Weierstrass curves.
1068  *                  It may not be included in builds without any short
1069  *                  Weierstrass curve.
1070  *
1071  * \param grp       The ECP group to use.
1072  *                  This must be initialized and have group parameters
1073  *                  set, for example through mbedtls_ecp_group_load().
1074  * \param R         The point in which to store the result of the calculation.
1075  *                  This must be initialized.
1076  * \param m         The integer by which to multiply \p P.
1077  *                  This must be initialized.
1078  * \param P         The point to multiply by \p m. This must be initialized.
1079  * \param n         The integer by which to multiply \p Q.
1080  *                  This must be initialized.
1081  * \param Q         The point to be multiplied by \p n.
1082  *                  This must be initialized.
1083  * \param rs_ctx    The restart context (NULL disables restart).
1084  *
1085  * \return          \c 0 on success.
1086  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
1087  *                  valid private keys, or \p P or \p Q are not valid public
1088  *                  keys.
1089  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1090  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p grp does not
1091  *                  designate a short Weierstrass curve.
1092  * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
1093  *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
1094  * \return          Another negative error code on other kinds of failure.
1095  */
1096 int mbedtls_ecp_muladd_restartable(
1097     mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
1098     const mbedtls_mpi *m, const mbedtls_ecp_point *P,
1099     const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
1100     mbedtls_ecp_restart_ctx *rs_ctx);
1101 #endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
1102 
1103 /**
1104  * \brief           This function checks that a point is a valid public key
1105  *                  on this curve.
1106  *
1107  *                  It only checks that the point is non-zero, has
1108  *                  valid coordinates and lies on the curve. It does not verify
1109  *                  that it is indeed a multiple of \c G. This additional
1110  *                  check is computationally more expensive, is not required
1111  *                  by standards, and should not be necessary if the group
1112  *                  used has a small cofactor. In particular, it is useless for
1113  *                  the NIST groups which all have a cofactor of 1.
1114  *
1115  * \note            This function uses bare components rather than an
1116  *                  ::mbedtls_ecp_keypair structure, to ease use with other
1117  *                  structures, such as ::mbedtls_ecdh_context or
1118  *                  ::mbedtls_ecdsa_context.
1119  *
1120  * \param grp       The ECP group the point should belong to.
1121  *                  This must be initialized and have group parameters
1122  *                  set, for example through mbedtls_ecp_group_load().
1123  * \param pt        The point to check. This must be initialized.
1124  *
1125  * \return          \c 0 if the point is a valid public key.
1126  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
1127  *                  a valid public key for the given curve.
1128  * \return          Another negative error code on other kinds of failure.
1129  */
1130 int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
1131                              const mbedtls_ecp_point *pt);
1132 
1133 /**
1134  * \brief           This function checks that an \c mbedtls_mpi is a
1135  *                  valid private key for this curve.
1136  *
1137  * \note            This function uses bare components rather than an
1138  *                  ::mbedtls_ecp_keypair structure to ease use with other
1139  *                  structures, such as ::mbedtls_ecdh_context or
1140  *                  ::mbedtls_ecdsa_context.
1141  *
1142  * \param grp       The ECP group the private key should belong to.
1143  *                  This must be initialized and have group parameters
1144  *                  set, for example through mbedtls_ecp_group_load().
1145  * \param d         The integer to check. This must be initialized.
1146  *
1147  * \return          \c 0 if the point is a valid private key.
1148  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
1149  *                  private key for the given curve.
1150  * \return          Another negative error code on other kinds of failure.
1151  */
1152 int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
1153                               const mbedtls_mpi *d);
1154 
1155 /**
1156  * \brief           This function generates a private key.
1157  *
1158  * \param grp       The ECP group to generate a private key for.
1159  *                  This must be initialized and have group parameters
1160  *                  set, for example through mbedtls_ecp_group_load().
1161  * \param d         The destination MPI (secret part). This must be initialized.
1162  * \param f_rng     The RNG function. This must not be \c NULL.
1163  * \param p_rng     The RNG parameter to be passed to \p f_rng. This may be
1164  *                  \c NULL if \p f_rng doesn't need a context argument.
1165  *
1166  * \return          \c 0 on success.
1167  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1168  *                  on failure.
1169  */
1170 int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
1171                             mbedtls_mpi *d,
1172                             int (*f_rng)(void *, unsigned char *, size_t),
1173                             void *p_rng);
1174 
1175 /**
1176  * \brief           This function generates a keypair with a configurable base
1177  *                  point.
1178  *
1179  * \note            This function uses bare components rather than an
1180  *                  ::mbedtls_ecp_keypair structure to ease use with other
1181  *                  structures, such as ::mbedtls_ecdh_context or
1182  *                  ::mbedtls_ecdsa_context.
1183  *
1184  * \param grp       The ECP group to generate a key pair for.
1185  *                  This must be initialized and have group parameters
1186  *                  set, for example through mbedtls_ecp_group_load().
1187  * \param G         The base point to use. This must be initialized
1188  *                  and belong to \p grp. It replaces the default base
1189  *                  point \c grp->G used by mbedtls_ecp_gen_keypair().
1190  * \param d         The destination MPI (secret part).
1191  *                  This must be initialized.
1192  * \param Q         The destination point (public part).
1193  *                  This must be initialized.
1194  * \param f_rng     The RNG function. This must not be \c NULL.
1195  * \param p_rng     The RNG context to be passed to \p f_rng. This may
1196  *                  be \c NULL if \p f_rng doesn't need a context argument.
1197  *
1198  * \return          \c 0 on success.
1199  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1200  *                  on failure.
1201  */
1202 int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
1203                                  const mbedtls_ecp_point *G,
1204                                  mbedtls_mpi *d, mbedtls_ecp_point *Q,
1205                                  int (*f_rng)(void *, unsigned char *, size_t),
1206                                  void *p_rng);
1207 
1208 /**
1209  * \brief           This function generates an ECP keypair.
1210  *
1211  * \note            This function uses bare components rather than an
1212  *                  ::mbedtls_ecp_keypair structure to ease use with other
1213  *                  structures, such as ::mbedtls_ecdh_context or
1214  *                  ::mbedtls_ecdsa_context.
1215  *
1216  * \param grp       The ECP group to generate a key pair for.
1217  *                  This must be initialized and have group parameters
1218  *                  set, for example through mbedtls_ecp_group_load().
1219  * \param d         The destination MPI (secret part).
1220  *                  This must be initialized.
1221  * \param Q         The destination point (public part).
1222  *                  This must be initialized.
1223  * \param f_rng     The RNG function. This must not be \c NULL.
1224  * \param p_rng     The RNG context to be passed to \p f_rng. This may
1225  *                  be \c NULL if \p f_rng doesn't need a context argument.
1226  *
1227  * \return          \c 0 on success.
1228  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1229  *                  on failure.
1230  */
1231 int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp, mbedtls_mpi *d,
1232                             mbedtls_ecp_point *Q,
1233                             int (*f_rng)(void *, unsigned char *, size_t),
1234                             void *p_rng);
1235 
1236 /**
1237  * \brief           This function generates an ECP key.
1238  *
1239  * \param grp_id    The ECP group identifier.
1240  * \param key       The destination key. This must be initialized.
1241  * \param f_rng     The RNG function to use. This must not be \c NULL.
1242  * \param p_rng     The RNG context to be passed to \p f_rng. This may
1243  *                  be \c NULL if \p f_rng doesn't need a context argument.
1244  *
1245  * \return          \c 0 on success.
1246  * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
1247  *                  on failure.
1248  */
1249 int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1250                         int (*f_rng)(void *, unsigned char *, size_t),
1251                         void *p_rng);
1252 
1253 /** \brief          Set the public key in a key pair object.
1254  *
1255  * \note            This function does not check that the point actually
1256  *                  belongs to the given group. Call mbedtls_ecp_check_pubkey()
1257  *                  on \p Q before calling this function to check that.
1258  *
1259  * \note            This function does not check that the public key matches
1260  *                  the private key that is already in \p key, if any.
1261  *                  To check the consistency of the resulting key pair object,
1262  *                  call mbedtls_ecp_check_pub_priv() after setting both
1263  *                  the public key and the private key.
1264  *
1265  * \param grp_id    The ECP group identifier.
1266  * \param key       The key pair object. It must be initialized.
1267  *                  If its group has already been set, it must match \p grp_id.
1268  *                  If its group has not been set, it will be set to \p grp_id.
1269  *                  If the public key has already been set, it is overwritten.
1270  * \param Q         The public key to copy. This must be a point on the
1271  *                  curve indicated by \p grp_id.
1272  *
1273  * \return          \c 0 on success.
1274  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if \p key does not
1275  *                  match \p grp_id.
1276  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1277  *                  the group is not implemented.
1278  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1279  * \return          Another negative error code on other kinds of failure.
1280  */
1281 int mbedtls_ecp_set_public_key(mbedtls_ecp_group_id grp_id,
1282                                mbedtls_ecp_keypair *key,
1283                                const mbedtls_ecp_point *Q);
1284 
1285 /**
1286  * \brief           This function reads an elliptic curve private key.
1287  *
1288  * \note            This function does not set the public key in the
1289  *                  key pair object. Without a public key, the key pair object
1290  *                  cannot be used with operations that require the public key.
1291  *                  Call mbedtls_ecp_keypair_calc_public() to set the public
1292  *                  key from the private key. Alternatively, you can call
1293  *                  mbedtls_ecp_set_public_key() to set the public key part,
1294  *                  and then optionally mbedtls_ecp_check_pub_priv() to check
1295  *                  that the private and public parts are consistent.
1296  *
1297  * \note            If a public key has already been set in the key pair
1298  *                  object, this function does not check that it is consistent
1299  *                  with the private key. Call mbedtls_ecp_check_pub_priv()
1300  *                  after setting both the public key and the private key
1301  *                  to make that check.
1302  *
1303  * \param grp_id    The ECP group identifier.
1304  * \param key       The destination key.
1305  * \param buf       The buffer containing the binary representation of the
1306  *                  key. (Big endian integer for Weierstrass curves, byte
1307  *                  string for Montgomery curves.)
1308  * \param buflen    The length of the buffer in bytes.
1309  *
1310  * \return          \c 0 on success.
1311  * \return          #MBEDTLS_ERR_ECP_INVALID_KEY error if the key is
1312  *                  invalid.
1313  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
1314  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the operation for
1315  *                  the group is not implemented.
1316  * \return          Another negative error code on different kinds of failure.
1317  */
1318 int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
1319                          const unsigned char *buf, size_t buflen);
1320 
1321 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
1322 /**
1323  * \brief           This function exports an elliptic curve private key.
1324  *
1325  * \deprecated      Note that although this function accepts an output
1326  *                  buffer that is smaller or larger than the key, most key
1327  *                  import interfaces require the output to have exactly
1328  *                  key's nominal length. It is generally simplest to
1329  *                  pass the key's nominal length as \c buflen, after
1330  *                  checking that the output buffer is large enough.
1331  *                  See the description of the \p buflen parameter for
1332  *                  how to calculate the nominal length.
1333  *                  To avoid this difficulty, use mbedtls_ecp_write_key_ext()
1334  *                  instead.
1335  *                  mbedtls_ecp_write_key() is deprecated and will be
1336  *                  removed in a future version of the library.
1337  *
1338  * \note            If the private key was not set in \p key,
1339  *                  the output is unspecified. Future versions
1340  *                  may return an error in that case.
1341  *
1342  * \param key       The private key.
1343  * \param buf       The output buffer for containing the binary representation
1344  *                  of the key.
1345  *                  For Weierstrass curves, this is the big-endian
1346  *                  representation, padded with null bytes at the beginning
1347  *                  to reach \p buflen bytes.
1348  *                  For Montgomery curves, this is the standard byte string
1349  *                  representation (which is little-endian), padded with
1350  *                  null bytes at the end to reach \p buflen bytes.
1351  * \param buflen    The total length of the buffer in bytes.
1352  *                  The length of the output is
1353  *                  (`grp->nbits` + 7) / 8 bytes
1354  *                  where `grp->nbits` is the private key size in bits.
1355  *                  For Weierstrass keys, if the output buffer is smaller,
1356  *                  leading zeros are trimmed to fit if possible. For
1357  *                  Montgomery keys, the output buffer must always be large
1358  *                  enough for the nominal length.
1359  *
1360  * \return          \c 0 on success.
1361  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL or
1362  *                  #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the \p key
1363  *                  representation is larger than the available space in \p buf.
1364  * \return          Another negative error code on different kinds of failure.
1365  */
1366 int MBEDTLS_DEPRECATED mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
1367                                              unsigned char *buf, size_t buflen);
1368 #endif /* MBEDTLS_DEPRECATED_REMOVED */
1369 
1370 /**
1371  * \brief           This function exports an elliptic curve private key.
1372  *
1373  * \param key       The private key.
1374  * \param olen      On success, the length of the private key.
1375  *                  This is always (`grp->nbits` + 7) / 8 bytes
1376  *                  where `grp->nbits` is the private key size in bits.
1377  * \param buf       The output buffer for containing the binary representation
1378  *                  of the key.
1379  * \param buflen    The total length of the buffer in bytes.
1380  *                  #MBEDTLS_ECP_MAX_BYTES is always sufficient.
1381  *
1382  * \return          \c 0 on success.
1383  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the \p key
1384  *                  representation is larger than the available space in \p buf.
1385  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if no private key is
1386  *                  set in \p key.
1387  * \return          Another negative error code on different kinds of failure.
1388  */
1389 int mbedtls_ecp_write_key_ext(const mbedtls_ecp_keypair *key,
1390                               size_t *olen, unsigned char *buf, size_t buflen);
1391 
1392 /**
1393  * \brief           This function exports an elliptic curve public key.
1394  *
1395  * \note            If the public key was not set in \p key,
1396  *                  the output is unspecified. Future versions
1397  *                  may return an error in that case.
1398  *
1399  * \param key       The public key.
1400  * \param format    The point format. This must be either
1401  *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
1402  *                  (For groups without these formats, this parameter is
1403  *                  ignored. But it still has to be either of the above
1404  *                  values.)
1405  * \param olen      The address at which to store the length of
1406  *                  the output in Bytes. This must not be \c NULL.
1407  * \param buf       The output buffer. This must be a writable buffer
1408  *                  of length \p buflen Bytes.
1409  * \param buflen    The length of the output buffer \p buf in Bytes.
1410  *
1411  * \return          \c 0 on success.
1412  * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
1413  *                  is too small to hold the point.
1414  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
1415  *                  or the export for the given group is not implemented.
1416  * \return          Another negative error code on other kinds of failure.
1417  */
1418 int mbedtls_ecp_write_public_key(const mbedtls_ecp_keypair *key,
1419                                  int format, size_t *olen,
1420                                  unsigned char *buf, size_t buflen);
1421 
1422 /**
1423  * \brief           This function checks that the keypair objects
1424  *                  \p pub and \p prv have the same group and the
1425  *                  same public point, and that the private key in
1426  *                  \p prv is consistent with the public key.
1427  *
1428  * \param pub       The keypair structure holding the public key. This
1429  *                  must be initialized. If it contains a private key, that
1430  *                  part is ignored.
1431  * \param prv       The keypair structure holding the full keypair.
1432  *                  This must be initialized.
1433  * \param f_rng     The RNG function. This must not be \c NULL.
1434  * \param p_rng     The RNG context to be passed to \p f_rng. This may be \c
1435  *                  NULL if \p f_rng doesn't need a context.
1436  *
1437  * \return          \c 0 on success, meaning that the keys are valid and match.
1438  * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
1439  * \return          An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
1440  *                  error code on calculation failure.
1441  */
1442 int mbedtls_ecp_check_pub_priv(
1443     const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
1444     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
1445 
1446 /** \brief          Calculate the public key from a private key in a key pair.
1447  *
1448  * \param key       A keypair structure. It must have a private key set.
1449  *                  If the public key is set, it will be overwritten.
1450  * \param f_rng     The RNG function. This must not be \c NULL.
1451  * \param p_rng     The RNG context to be passed to \p f_rng. This may be \c
1452  *                  NULL if \p f_rng doesn't need a context.
1453  *
1454  * \return          \c 0 on success. The key pair object can be used for
1455  *                  operations that require the public key.
1456  * \return          An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
1457  *                  error code on calculation failure.
1458  */
1459 int mbedtls_ecp_keypair_calc_public(
1460     mbedtls_ecp_keypair *key,
1461     int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
1462 
1463 /** \brief          Query the group that a key pair belongs to.
1464  *
1465  * \param key       The key pair to query.
1466  *
1467  * \return          The group ID for the group registered in the key pair
1468  *                  object.
1469  *                  This is \c MBEDTLS_ECP_DP_NONE if no group has been set
1470  *                  in the key pair object.
1471  */
1472 mbedtls_ecp_group_id mbedtls_ecp_keypair_get_group_id(
1473     const mbedtls_ecp_keypair *key);
1474 
1475 /**
1476  * \brief           This function exports generic key-pair parameters.
1477  *
1478  *                  Each of the output parameters can be a null pointer
1479  *                  if you do not need that parameter.
1480  *
1481  * \note            If the private key or the public key was not set in \p key,
1482  *                  the corresponding output is unspecified. Future versions
1483  *                  may return an error in that case.
1484  *
1485  * \param key       The key pair to export from.
1486  * \param grp       Slot for exported ECP group.
1487  *                  It must either be null or point to an initialized ECP group.
1488  * \param d         Slot for the exported secret value.
1489  *                  It must either be null or point to an initialized mpi.
1490  * \param Q         Slot for the exported public value.
1491  *                  It must either be null or point to an initialized ECP point.
1492  *
1493  * \return          \c 0 on success,
1494  * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
1495  * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if key id doesn't
1496  *                  correspond to a known group.
1497  * \return          Another negative error code on other kinds of failure.
1498  */
1499 int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
1500                        mbedtls_mpi *d, mbedtls_ecp_point *Q);
1501 
1502 #if defined(MBEDTLS_SELF_TEST)
1503 
1504 /**
1505  * \brief          The ECP checkup routine.
1506  *
1507  * \return         \c 0 on success.
1508  * \return         \c 1 on failure.
1509  */
1510 int mbedtls_ecp_self_test(int verbose);
1511 
1512 #endif /* MBEDTLS_SELF_TEST */
1513 
1514 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
1515 
1516 #ifdef __cplusplus
1517 }
1518 #endif
1519 
1520 #endif /* ecp.h */