ecp 49.4 KB
Newer Older
ROOL's avatar
ROOL committed
1 2 3
/**
 * \file ecp.h
 *
ROOL's avatar
ROOL committed
4 5 6 7 8 9 10 11 12 13 14
 * \brief This file provides an API for Elliptic Curves over GF(P) (ECP).
 *
 * The use of ECP in cryptography and TLS is defined in
 * <em>Standards for Efficient Cryptography Group (SECG): SEC1
 * Elliptic Curve Cryptography</em> and
 * <em>RFC-4492: Elliptic Curve Cryptography (ECC) Cipher Suites
 * for Transport Layer Security (TLS)</em>.
 *
 * <em>RFC-2409: The Internet Key Exchange (IKE)</em> defines ECP
 * group types.
 *
ROOL's avatar
ROOL committed
15
 */
ROOL's avatar
ROOL committed
16

ROOL's avatar
ROOL committed
17
/*
ROOL's avatar
ROOL committed
18
 *  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
ROOL's avatar
ROOL committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *  SPDX-License-Identifier: Apache-2.0
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
ROOL's avatar
ROOL committed
33
 *  This file is part of Mbed TLS (https://tls.mbed.org)
ROOL's avatar
ROOL committed
34
 */
ROOL's avatar
ROOL committed
35

ROOL's avatar
ROOL committed
36 37 38 39 40 41 42 43 44 45
#ifndef MBEDTLS_ECP_H
#define MBEDTLS_ECP_H

#include "bignum.h"

/*
 * ECP error codes
 */
#define MBEDTLS_ERR_ECP_BAD_INPUT_DATA                    -0x4F80  /**< Bad input parameters to function. */
#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL                  -0x4F00  /**< The buffer is too small to write to. */
ROOL's avatar
ROOL committed
46
#define MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE               -0x4E80  /**< The requested feature is not available, for example, the requested curve is not supported. */
ROOL's avatar
ROOL committed
47 48
#define MBEDTLS_ERR_ECP_VERIFY_FAILED                     -0x4E00  /**< The signature is not valid. */
#define MBEDTLS_ERR_ECP_ALLOC_FAILED                      -0x4D80  /**< Memory allocation failed. */
ROOL's avatar
ROOL committed
49
#define MBEDTLS_ERR_ECP_RANDOM_FAILED                     -0x4D00  /**< Generation of random value, such as ephemeral key, failed. */
ROOL's avatar
ROOL committed
50
#define MBEDTLS_ERR_ECP_INVALID_KEY                       -0x4C80  /**< Invalid private or public key. */
ROOL's avatar
ROOL committed
51
#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH                  -0x4C00  /**< The buffer contains a valid signature followed by more data. */
ROOL's avatar
ROOL committed
52 53

/* MBEDTLS_ERR_ECP_HW_ACCEL_FAILED is deprecated and should not be used. */
ROOL's avatar
ROOL committed
54
#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED                   -0x4B80  /**< The ECP hardware accelerator failed. */
ROOL's avatar
ROOL committed
55

ROOL's avatar
ROOL committed
56 57
#define MBEDTLS_ERR_ECP_IN_PROGRESS                       -0x4B00  /**< Operation in progress, call again with the same parameters to continue. */

ROOL's avatar
ROOL committed
58 59 60 61 62
#ifdef __cplusplus
extern "C" {
#endif

/**
ROOL's avatar
ROOL committed
63
 * Domain-parameter identifiers: curve, subgroup, and generator.
ROOL's avatar
ROOL committed
64
 *
ROOL's avatar
ROOL committed
65
 * \note Only curves over prime fields are supported.
ROOL's avatar
ROOL committed
66 67
 *
 * \warning This library does not support validation of arbitrary domain
ROOL's avatar
ROOL committed
68
 * parameters. Therefore, only standardized domain parameters from trusted
ROOL's avatar
ROOL committed
69 70 71 72
 * sources should be used. See mbedtls_ecp_group_load().
 */
typedef enum
{
ROOL's avatar
ROOL committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    MBEDTLS_ECP_DP_NONE = 0,       /*!< Curve not defined. */
    MBEDTLS_ECP_DP_SECP192R1,      /*!< Domain parameters for the 192-bit curve defined by FIPS 186-4 and SEC1. */
    MBEDTLS_ECP_DP_SECP224R1,      /*!< Domain parameters for the 224-bit curve defined by FIPS 186-4 and SEC1. */
    MBEDTLS_ECP_DP_SECP256R1,      /*!< Domain parameters for the 256-bit curve defined by FIPS 186-4 and SEC1. */
    MBEDTLS_ECP_DP_SECP384R1,      /*!< Domain parameters for the 384-bit curve defined by FIPS 186-4 and SEC1. */
    MBEDTLS_ECP_DP_SECP521R1,      /*!< Domain parameters for the 521-bit curve defined by FIPS 186-4 and SEC1. */
    MBEDTLS_ECP_DP_BP256R1,        /*!< Domain parameters for 256-bit Brainpool curve. */
    MBEDTLS_ECP_DP_BP384R1,        /*!< Domain parameters for 384-bit Brainpool curve. */
    MBEDTLS_ECP_DP_BP512R1,        /*!< Domain parameters for 512-bit Brainpool curve. */
    MBEDTLS_ECP_DP_CURVE25519,     /*!< Domain parameters for Curve25519. */
    MBEDTLS_ECP_DP_SECP192K1,      /*!< Domain parameters for 192-bit "Koblitz" curve. */
    MBEDTLS_ECP_DP_SECP224K1,      /*!< Domain parameters for 224-bit "Koblitz" curve. */
    MBEDTLS_ECP_DP_SECP256K1,      /*!< Domain parameters for 256-bit "Koblitz" curve. */
    MBEDTLS_ECP_DP_CURVE448,       /*!< Domain parameters for Curve448. */
ROOL's avatar
ROOL committed
87 88 89
} mbedtls_ecp_group_id;

/**
ROOL's avatar
ROOL committed
90
 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
ROOL's avatar
ROOL committed
91
 *
ROOL's avatar
ROOL committed
92
 * \note Montgomery curves are currently excluded.
ROOL's avatar
ROOL committed
93 94 95 96
 */
#define MBEDTLS_ECP_DP_MAX     12

/**
ROOL's avatar
ROOL committed
97
 * Curve information, for use by other modules.
ROOL's avatar
ROOL committed
98
 */
ROOL's avatar
ROOL committed
99
typedef struct mbedtls_ecp_curve_info
ROOL's avatar
ROOL committed
100
{
ROOL's avatar
ROOL committed
101 102 103 104
    mbedtls_ecp_group_id grp_id;    /*!< An internal identifier. */
    uint16_t tls_id;                /*!< The TLS NamedCurve identifier. */
    uint16_t bit_size;              /*!< The curve size in bits. */
    const char *name;               /*!< A human-friendly name. */
ROOL's avatar
ROOL committed
105 106 107
} mbedtls_ecp_curve_info;

/**
ROOL's avatar
ROOL committed
108
 * \brief           The ECP point structure, in Jacobian coordinates.
ROOL's avatar
ROOL committed
109 110
 *
 * \note            All functions expect and return points satisfying
ROOL's avatar
ROOL committed
111 112 113 114 115 116
 *                  the following condition: <code>Z == 0</code> or
 *                  <code>Z == 1</code>. Other values of \p Z are
 *                  used only by internal functions.
 *                  The point is zero, or "at infinity", if <code>Z == 0</code>.
 *                  Otherwise, \p X and \p Y are its standard (affine)
 *                  coordinates.
ROOL's avatar
ROOL committed
117
 */
ROOL's avatar
ROOL committed
118
typedef struct mbedtls_ecp_point
ROOL's avatar
ROOL committed
119
{
ROOL's avatar
ROOL committed
120 121 122
    mbedtls_mpi X;          /*!< The X coordinate of the ECP point. */
    mbedtls_mpi Y;          /*!< The Y coordinate of the ECP point. */
    mbedtls_mpi Z;          /*!< The Z coordinate of the ECP point. */
ROOL's avatar
ROOL committed
123 124 125
}
mbedtls_ecp_point;

ROOL's avatar
ROOL committed
126 127 128
#if !defined(MBEDTLS_ECP_ALT)
/*
 * default mbed TLS elliptic curve arithmetic implementation
ROOL's avatar
ROOL committed
129
 *
ROOL's avatar
ROOL committed
130 131 132
 * (in case MBEDTLS_ECP_ALT is defined then the developer has to provide an
 * alternative implementation for the whole module and it will replace this
 * one.)
ROOL's avatar
ROOL committed
133 134 135
 */

/**
ROOL's avatar
ROOL committed
136 137 138 139 140 141 142 143
 * \brief           The ECP group structure.
 *
 * We consider two types of curve equations:
 * <ul><li>Short Weierstrass: <code>y^2 = x^3 + A x + B mod P</code>
 * (SEC1 + RFC-4492)</li>
 * <li>Montgomery: <code>y^2 = x^3 + A x^2 + x mod P</code> (Curve25519,
 * Curve448)</li></ul>
 * In both cases, the generator (\p G) for a prime-order subgroup is fixed.
ROOL's avatar
ROOL committed
144
 *
ROOL's avatar
ROOL committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 * For Short Weierstrass, this subgroup is the whole curve, and its
 * cardinality is denoted by \p N. Our code requires that \p N is an
 * odd prime as mbedtls_ecp_mul() requires an odd number, and
 * mbedtls_ecdsa_sign() requires that it is prime for blinding purposes.
 *
 * For Montgomery curves, we do not store \p A, but <code>(A + 2) / 4</code>,
 * which is the quantity used in the formulas. Additionally, \p nbits is
 * not the size of \p N but the required size for private keys.
 *
 * If \p modp is NULL, reduction modulo \p P is done using a generic algorithm.
 * Otherwise, \p modp must point to a function that takes an \p mbedtls_mpi in the
 * range of <code>0..2^(2*pbits)-1</code>, and transforms it in-place to an integer
 * which is congruent mod \p P to the given MPI, and is close enough to \p pbits
 * in size, so that it may be efficiently brought in the 0..P-1 range by a few
 * additions or subtractions. Therefore, it is only an approximative modular
 * reduction. It must return 0 on success and non-zero on failure.
ROOL's avatar
ROOL committed
161
 *
ROOL's avatar
ROOL committed
162 163 164 165
 * \note        Alternative implementations must keep the group IDs distinct. If
 *              two group structures have the same ID, then they must be
 *              identical.
 *
ROOL's avatar
ROOL committed
166
 */
ROOL's avatar
ROOL committed
167
typedef struct mbedtls_ecp_group
ROOL's avatar
ROOL committed
168
{
ROOL's avatar
ROOL committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    mbedtls_ecp_group_id id;    /*!< An internal group identifier. */
    mbedtls_mpi P;              /*!< The prime modulus of the base field. */
    mbedtls_mpi A;              /*!< For Short Weierstrass: \p A in the equation. For
                                     Montgomery curves: <code>(A + 2) / 4</code>. */
    mbedtls_mpi B;              /*!< For Short Weierstrass: \p B in the equation.
                                     For Montgomery curves: unused. */
    mbedtls_ecp_point G;        /*!< The generator of the subgroup used. */
    mbedtls_mpi N;              /*!< The order of \p G. */
    size_t pbits;               /*!< The number of bits in \p P.*/
    size_t nbits;               /*!< For Short Weierstrass: The number of bits in \p P.
                                     For Montgomery curves: the number of bits in the
                                     private keys. */
    unsigned int h;             /*!< \internal 1 if the constants are static. */
    int (*modp)(mbedtls_mpi *); /*!< The function for fast pseudo-reduction
                                     mod \p P (see above).*/
    int (*t_pre)(mbedtls_ecp_point *, void *);  /*!< Unused. */
    int (*t_post)(mbedtls_ecp_point *, void *); /*!< Unused. */
    void *t_data;               /*!< Unused. */
    mbedtls_ecp_point *T;       /*!< Pre-computed points for ecp_mul_comb(). */
    size_t T_size;              /*!< The number of pre-computed points. */
ROOL's avatar
ROOL committed
189
}
ROOL's avatar
ROOL committed
190
mbedtls_ecp_group;
ROOL's avatar
ROOL committed
191

ROOL's avatar
ROOL committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
#if defined(MBEDTLS_ECP_RESTARTABLE)

/**
 * \brief           Internal restart context for multiplication
 *
 * \note            Opaque struct
 */
typedef struct mbedtls_ecp_restart_mul mbedtls_ecp_restart_mul_ctx;

/**
 * \brief           Internal restart context for ecp_muladd()
 *
 * \note            Opaque struct
 */
typedef struct mbedtls_ecp_restart_muladd mbedtls_ecp_restart_muladd_ctx;

/**
 * \brief           General context for resuming ECC operations
 */
typedef struct
{
    unsigned ops_done;                  /*!<  current ops count             */
    unsigned depth;                     /*!<  call depth (0 = top-level)    */
    mbedtls_ecp_restart_mul_ctx *rsm;   /*!<  ecp_mul_comb() sub-context    */
    mbedtls_ecp_restart_muladd_ctx *ma; /*!<  ecp_muladd() sub-context      */
} mbedtls_ecp_restart_ctx;

/*
 * Operation counts for restartable functions
 */
#define MBEDTLS_ECP_OPS_CHK   3 /*!< basic ops count for ecp_check_pubkey()  */
#define MBEDTLS_ECP_OPS_DBL   8 /*!< basic ops count for ecp_double_jac()    */
#define MBEDTLS_ECP_OPS_ADD  11 /*!< basic ops count for see ecp_add_mixed() */
#define MBEDTLS_ECP_OPS_INV 120 /*!< empirical equivalent for mpi_mod_inv()  */

/**
 * \brief           Internal; for restartable functions in other modules.
 *                  Check and update basic ops budget.
 *
 * \param grp       Group structure
 * \param rs_ctx    Restart context
 * \param ops       Number of basic ops to do
 *
 * \return          \c 0 if doing \p ops basic ops is still allowed,
 * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS otherwise.
 */
int mbedtls_ecp_check_budget( const mbedtls_ecp_group *grp,
                              mbedtls_ecp_restart_ctx *rs_ctx,
                              unsigned ops );

/* Utility macro for checking and updating ops budget */
#define MBEDTLS_ECP_BUDGET( ops )   \
    MBEDTLS_MPI_CHK( mbedtls_ecp_check_budget( grp, rs_ctx, \
                                               (unsigned) (ops) ) );

#else /* MBEDTLS_ECP_RESTARTABLE */

#define MBEDTLS_ECP_BUDGET( ops )   /* no-op; for compatibility */

/* We want to declare restartable versions of existing functions anyway */
typedef void mbedtls_ecp_restart_ctx;

#endif /* MBEDTLS_ECP_RESTARTABLE */

ROOL's avatar
ROOL committed
256 257 258 259
/**
 * \name SECTION: Module settings
 *
 * The configuration options you can set for this module are in this section.
ROOL's avatar
ROOL committed
260
 * Either change them in config.h, or define them using the compiler command line.
ROOL's avatar
ROOL committed
261 262 263 264 265
 * \{
 */

#if !defined(MBEDTLS_ECP_MAX_BITS)
/**
ROOL's avatar
ROOL committed
266
 * The maximum size of the groups, that is, of \c N and \c P.
ROOL's avatar
ROOL committed
267
 */
ROOL's avatar
ROOL committed
268
#define MBEDTLS_ECP_MAX_BITS     521   /**< The maximum size of groups, in bits. */
ROOL's avatar
ROOL committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#endif

#define MBEDTLS_ECP_MAX_BYTES    ( ( MBEDTLS_ECP_MAX_BITS + 7 ) / 8 )
#define MBEDTLS_ECP_MAX_PT_LEN   ( 2 * MBEDTLS_ECP_MAX_BYTES + 1 )

#if !defined(MBEDTLS_ECP_WINDOW_SIZE)
/*
 * Maximum "window" size used for point multiplication.
 * Default: 6.
 * Minimum value: 2. Maximum value: 7.
 *
 * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) )
 * points used for point multiplication. This value is directly tied to EC
 * peak memory usage, so decreasing it by one should roughly cut memory usage
 * by two (if large curves are in use).
 *
 * Reduction in size may reduce speed, but larger curves are impacted first.
 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
 *      w-size:     6       5       4       3       2
 *      521       145     141     135     120      97
 *      384       214     209     198     177     146
 *      256       320     320     303     262     226
 *      224       475     475     453     398     342
 *      192       640     640     633     587     476
 */
ROOL's avatar
ROOL committed
294
#define MBEDTLS_ECP_WINDOW_SIZE    6   /**< The maximum window size used. */
ROOL's avatar
ROOL committed
295 296 297 298 299 300 301 302 303 304 305 306 307 308
#endif /* MBEDTLS_ECP_WINDOW_SIZE */

#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM)
/*
 * Trade memory for speed on fixed-point multiplication.
 *
 * This speeds up repeated multiplication of the generator (that is, the
 * multiplication in ECDSA signatures, and half of the multiplications in
 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
 *
 * The cost is increasing EC peak memory usage by a factor roughly 2.
 *
 * Change this value to 0 to reduce peak memory usage.
 */
ROOL's avatar
ROOL committed
309
#define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up. */
ROOL's avatar
ROOL committed
310 311 312 313
#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */

/* \} name SECTION: Module settings */

ROOL's avatar
ROOL committed
314 315 316 317 318 319 320 321 322 323 324 325
#else  /* MBEDTLS_ECP_ALT */
#include "ecp_alt.h"
#endif /* MBEDTLS_ECP_ALT */

/**
 * \brief    The ECP key-pair structure.
 *
 * A generic key-pair that may be used for ECDSA and fixed ECDH, for example.
 *
 * \note    Members are deliberately in the same order as in the
 *          ::mbedtls_ecdsa_context structure.
 */
ROOL's avatar
ROOL committed
326
typedef struct mbedtls_ecp_keypair
ROOL's avatar
ROOL committed
327 328 329 330 331 332 333
{
    mbedtls_ecp_group grp;      /*!<  Elliptic curve and base point     */
    mbedtls_mpi d;              /*!<  our secret value                  */
    mbedtls_ecp_point Q;        /*!<  our public value                  */
}
mbedtls_ecp_keypair;

ROOL's avatar
ROOL committed
334 335 336
/*
 * Point formats, from RFC 4492's enum ECPointFormat
 */
ROOL's avatar
ROOL committed
337 338
#define MBEDTLS_ECP_PF_UNCOMPRESSED    0   /**< Uncompressed point format. */
#define MBEDTLS_ECP_PF_COMPRESSED      1   /**< Compressed point format. */
ROOL's avatar
ROOL committed
339 340 341 342

/*
 * Some other constants from RFC 4492
 */
ROOL's avatar
ROOL committed
343
#define MBEDTLS_ECP_TLS_NAMED_CURVE    3   /**< The named_curve of ECCurveType. */
ROOL's avatar
ROOL committed
344

ROOL's avatar
ROOL committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
#if defined(MBEDTLS_ECP_RESTARTABLE)
/**
 * \brief           Set the maximum number of basic operations done in a row.
 *
 *                  If more operations are needed to complete a computation,
 *                  #MBEDTLS_ERR_ECP_IN_PROGRESS will be returned by the
 *                  function performing the computation. It is then the
 *                  caller's responsibility to either call again with the same
 *                  parameters until it returns 0 or an error code; or to free
 *                  the restart context if the operation is to be aborted.
 *
 *                  It is strictly required that all input parameters and the
 *                  restart context be the same on successive calls for the
 *                  same operation, but output parameters need not be the
 *                  same; they must not be used until the function finally
 *                  returns 0.
 *
 *                  This only applies to functions whose documentation
 *                  mentions they may return #MBEDTLS_ERR_ECP_IN_PROGRESS (or
 *                  #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS for functions in the
 *                  SSL module). For functions that accept a "restart context"
 *                  argument, passing NULL disables restart and makes the
 *                  function equivalent to the function with the same name
 *                  with \c _restartable removed. For functions in the ECDH
 *                  module, restart is disabled unless the function accepts
 *                  an "ECDH context" argument and
 *                  mbedtls_ecdh_enable_restart() was previously called on
 *                  that context. For function in the SSL module, restart is
 *                  only enabled for specific sides and key exchanges
 *                  (currently only for clients and ECDHE-ECDSA).
 *
 * \param max_ops   Maximum number of basic operations done in a row.
 *                  Default: 0 (unlimited).
 *                  Lower (non-zero) values mean ECC functions will block for
 *                  a lesser maximum amount of time.
 *
 * \note            A "basic operation" is defined as a rough equivalent of a
 *                  multiplication in GF(p) for the NIST P-256 curve.
 *                  As an indication, with default settings, a scalar
 *                  multiplication (full run of \c mbedtls_ecp_mul()) is:
 *                  - about 3300 basic operations for P-256
 *                  - about 9400 basic operations for P-384
 *
 * \note            Very low values are not always respected: sometimes
 *                  functions need to block for a minimum number of
 *                  operations, and will do so even if max_ops is set to a
 *                  lower value.  That minimum depends on the curve size, and
 *                  can be made lower by decreasing the value of
 *                  \c MBEDTLS_ECP_WINDOW_SIZE.  As an indication, here is the
 *                  lowest effective value for various curves and values of
 *                  that parameter (w for short):
 *                          w=6     w=5     w=4     w=3     w=2
 *                  P-256   208     208     160     136     124
 *                  P-384   682     416     320     272     248
 *                  P-521  1364     832     640     544     496
 *
 * \note            This setting is currently ignored by Curve25519.
 */
void mbedtls_ecp_set_max_ops( unsigned max_ops );

/**
 * \brief           Check if restart is enabled (max_ops != 0)
 *
 * \return          \c 0 if \c max_ops == 0 (restart disabled)
 * \return          \c 1 otherwise (restart enabled)
 */
int mbedtls_ecp_restart_is_enabled( void );
#endif /* MBEDTLS_ECP_RESTARTABLE */

ROOL's avatar
ROOL committed
414
/**
ROOL's avatar
ROOL committed
415 416 417
 * \brief           This function retrieves the information defined in
 *                  mbedtls_ecp_curve_info() for all supported curves in order
 *                  of preference.
ROOL's avatar
ROOL committed
418
 *
ROOL's avatar
ROOL committed
419
 * \return          A statically allocated array. The last entry is 0.
ROOL's avatar
ROOL committed
420 421 422 423
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );

/**
ROOL's avatar
ROOL committed
424 425 426
 * \brief           This function retrieves the list of internal group
 *                  identifiers of all supported curves in the order of
 *                  preference.
ROOL's avatar
ROOL committed
427 428 429 430 431 432 433
 *
 * \return          A statically allocated array,
 *                  terminated with MBEDTLS_ECP_DP_NONE.
 */
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list( void );

/**
ROOL's avatar
ROOL committed
434 435
 * \brief           This function retrieves curve information from an internal
 *                  group identifier.
ROOL's avatar
ROOL committed
436
 *
ROOL's avatar
ROOL committed
437
 * \param grp_id    An \c MBEDTLS_ECP_DP_XXX value.
ROOL's avatar
ROOL committed
438
 *
ROOL's avatar
ROOL committed
439 440
 * \return          The associated curve information on success.
 * \return          NULL on failure.
ROOL's avatar
ROOL committed
441 442 443 444
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );

/**
ROOL's avatar
ROOL committed
445 446
 * \brief           This function retrieves curve information from a TLS
 *                  NamedCurve value.
ROOL's avatar
ROOL committed
447
 *
ROOL's avatar
ROOL committed
448
 * \param tls_id    An \c MBEDTLS_ECP_DP_XXX value.
ROOL's avatar
ROOL committed
449
 *
ROOL's avatar
ROOL committed
450 451
 * \return          The associated curve information on success.
 * \return          NULL on failure.
ROOL's avatar
ROOL committed
452 453 454 455
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );

/**
ROOL's avatar
ROOL committed
456 457
 * \brief           This function retrieves curve information from a
 *                  human-readable name.
ROOL's avatar
ROOL committed
458
 *
ROOL's avatar
ROOL committed
459
 * \param name      The human-readable name.
ROOL's avatar
ROOL committed
460
 *
ROOL's avatar
ROOL committed
461 462
 * \return          The associated curve information on success.
 * \return          NULL on failure.
ROOL's avatar
ROOL committed
463 464 465 466
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );

/**
ROOL's avatar
ROOL committed
467 468 469
 * \brief           This function initializes a point as zero.
 *
 * \param pt        The point to initialize.
ROOL's avatar
ROOL committed
470 471 472 473
 */
void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
474 475 476 477 478 479 480
 * \brief           This function initializes an ECP group context
 *                  without loading any domain parameters.
 *
 * \note            After this function is called, domain parameters
 *                  for various ECP groups can be loaded through the
 *                  mbedtls_ecp_load() or mbedtls_ecp_tls_read_group()
 *                  functions.
ROOL's avatar
ROOL committed
481 482 483 484
 */
void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );

/**
ROOL's avatar
ROOL committed
485 486 487
 * \brief           This function initializes a key pair as an invalid one.
 *
 * \param key       The key pair to initialize.
ROOL's avatar
ROOL committed
488 489 490 491
 */
void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );

/**
ROOL's avatar
ROOL committed
492 493 494
 * \brief           This function frees the components of a point.
 *
 * \param pt        The point to free.
ROOL's avatar
ROOL committed
495 496 497 498
 */
void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
499
 * \brief           This function frees the components of an ECP group.
ROOL's avatar
ROOL committed
500 501 502 503
 *
 * \param grp       The group to free. This may be \c NULL, in which
 *                  case this function returns immediately. If it is not
 *                  \c NULL, it must point to an initialized ECP group.
ROOL's avatar
ROOL committed
504 505 506 507
 */
void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );

/**
ROOL's avatar
ROOL committed
508
 * \brief           This function frees the components of a key pair.
ROOL's avatar
ROOL committed
509 510 511 512
 *
 * \param key       The key pair to free. This may be \c NULL, in which
 *                  case this function returns immediately. If it is not
 *                  \c NULL, it must point to an initialized ECP key pair.
ROOL's avatar
ROOL committed
513 514 515
 */
void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );

ROOL's avatar
ROOL committed
516 517
#if defined(MBEDTLS_ECP_RESTARTABLE)
/**
ROOL's avatar
ROOL committed
518 519 520 521
 * \brief           Initialize a restart context.
 *
 * \param ctx       The restart context to initialize. This must
 *                  not be \c NULL.
ROOL's avatar
ROOL committed
522 523 524 525
 */
void mbedtls_ecp_restart_init( mbedtls_ecp_restart_ctx *ctx );

/**
ROOL's avatar
ROOL committed
526 527 528 529 530
 * \brief           Free the components of a restart context.
 *
 * \param ctx       The restart context to free. This may be \c NULL, in which
 *                  case this function returns immediately. If it is not
 *                  \c NULL, it must point to an initialized restart context.
ROOL's avatar
ROOL committed
531 532 533 534
 */
void mbedtls_ecp_restart_free( mbedtls_ecp_restart_ctx *ctx );
#endif /* MBEDTLS_ECP_RESTARTABLE */

ROOL's avatar
ROOL committed
535
/**
ROOL's avatar
ROOL committed
536 537
 * \brief           This function copies the contents of point \p Q into
 *                  point \p P.
ROOL's avatar
ROOL committed
538
 *
ROOL's avatar
ROOL committed
539 540
 * \param P         The destination point. This must be initialized.
 * \param Q         The source point. This must be initialized.
ROOL's avatar
ROOL committed
541
 *
ROOL's avatar
ROOL committed
542 543
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
544
 * \return          Another negative error code for other kinds of failure.
ROOL's avatar
ROOL committed
545 546 547 548
 */
int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );

/**
ROOL's avatar
ROOL committed
549 550
 * \brief           This function copies the contents of group \p src into
 *                  group \p dst.
ROOL's avatar
ROOL committed
551
 *
ROOL's avatar
ROOL committed
552 553
 * \param dst       The destination group. This must be initialized.
 * \param src       The source group. This must be initialized.
ROOL's avatar
ROOL committed
554
 *
ROOL's avatar
ROOL committed
555 556
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
557
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
558
 */
ROOL's avatar
ROOL committed
559 560
int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst,
                            const mbedtls_ecp_group *src );
ROOL's avatar
ROOL committed
561 562

/**
ROOL's avatar
ROOL committed
563
 * \brief           This function sets a point to the point at infinity.
ROOL's avatar
ROOL committed
564
 *
ROOL's avatar
ROOL committed
565
 * \param pt        The point to set. This must be initialized.
ROOL's avatar
ROOL committed
566
 *
ROOL's avatar
ROOL committed
567 568
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
569
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
570 571 572 573
 */
int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
574
 * \brief           This function checks if a point is the point at infinity.
ROOL's avatar
ROOL committed
575
 *
ROOL's avatar
ROOL committed
576
 * \param pt        The point to test. This must be initialized.
ROOL's avatar
ROOL committed
577
 *
ROOL's avatar
ROOL committed
578 579
 * \return          \c 1 if the point is zero.
 * \return          \c 0 if the point is non-zero.
ROOL's avatar
ROOL committed
580
 * \return          A negative error code on failure.
ROOL's avatar
ROOL committed
581 582 583 584
 */
int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
585
 * \brief           This function compares two points.
ROOL's avatar
ROOL committed
586
 *
ROOL's avatar
ROOL committed
587
 * \note            This assumes that the points are normalized. Otherwise,
ROOL's avatar
ROOL committed
588 589
 *                  they may compare as "not equal" even if they are.
 *
ROOL's avatar
ROOL committed
590 591
 * \param P         The first point to compare. This must be initialized.
 * \param Q         The second point to compare. This must be initialized.
ROOL's avatar
ROOL committed
592
 *
ROOL's avatar
ROOL committed
593 594
 * \return          \c 0 if the points are equal.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the points are not equal.
ROOL's avatar
ROOL committed
595 596 597 598 599
 */
int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
                           const mbedtls_ecp_point *Q );

/**
ROOL's avatar
ROOL committed
600 601
 * \brief           This function imports a non-zero point from two ASCII
 *                  strings.
ROOL's avatar
ROOL committed
602
 *
ROOL's avatar
ROOL committed
603
 * \param P         The destination point. This must be initialized.
ROOL's avatar
ROOL committed
604 605 606
 * \param radix     The numeric base of the input.
 * \param x         The first affine coordinate, as a null-terminated string.
 * \param y         The second affine coordinate, as a null-terminated string.
ROOL's avatar
ROOL committed
607
 *
ROOL's avatar
ROOL committed
608 609
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_MPI_XXX error code on failure.
ROOL's avatar
ROOL committed
610 611 612 613 614
 */
int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
                           const char *x, const char *y );

/**
ROOL's avatar
ROOL committed
615
 * \brief           This function exports a point into unsigned binary data.
ROOL's avatar
ROOL committed
616
 *
ROOL's avatar
ROOL committed
617
 * \param grp       The group to which the point should belong.
ROOL's avatar
ROOL committed
618 619 620 621 622 623 624 625 626 627
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param P         The point to export. This must be initialized.
 * \param format    The point format. This must be either
 *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
 * \param olen      The address at which to store the length of
 *                  the output in Bytes. This must not be \c NULL.
 * \param buf       The output buffer. This must be a writable buffer
 *                  of length \p buflen Bytes.
 * \param buflen    The length of the output buffer \p buf in Bytes.
ROOL's avatar
ROOL committed
628
 *
ROOL's avatar
ROOL committed
629
 * \return          \c 0 on success.
ROOL's avatar
ROOL committed
630 631 632
 * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output buffer
 *                  is too small to hold the point.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
633 634 635 636 637 638
 */
int mbedtls_ecp_point_write_binary( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *P,
                            int format, size_t *olen,
                            unsigned char *buf, size_t buflen );

/**
ROOL's avatar
ROOL committed
639
 * \brief           This function imports a point from unsigned binary data.
ROOL's avatar
ROOL committed
640
 *
ROOL's avatar
ROOL committed
641 642 643
 * \note            This function does not check that the point actually
 *                  belongs to the given group, see mbedtls_ecp_check_pubkey()
 *                  for that.
ROOL's avatar
ROOL committed
644
 *
ROOL's avatar
ROOL committed
645
 * \param grp       The group to which the point should belong.
ROOL's avatar
ROOL committed
646 647 648 649 650 651 652
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param P         The destination context to import the point to.
 *                  This must be initialized.
 * \param buf       The input buffer. This must be a readable buffer
 *                  of length \p ilen Bytes.
 * \param ilen      The length of the input buffer \p buf in Bytes.
ROOL's avatar
ROOL committed
653 654
 *
 * \return          \c 0 on success.
ROOL's avatar
ROOL committed
655
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
ROOL's avatar
ROOL committed
656 657
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
 * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the point format
ROOL's avatar
ROOL committed
658 659
 *                  is not implemented.
 */
ROOL's avatar
ROOL committed
660 661 662
int mbedtls_ecp_point_read_binary( const mbedtls_ecp_group *grp,
                                   mbedtls_ecp_point *P,
                                   const unsigned char *buf, size_t ilen );
ROOL's avatar
ROOL committed
663 664

/**
ROOL's avatar
ROOL committed
665
 * \brief           This function imports a point from a TLS ECPoint record.
ROOL's avatar
ROOL committed
666
 *
ROOL's avatar
ROOL committed
667
 * \note            On function return, \p *buf is updated to point immediately
ROOL's avatar
ROOL committed
668
 *                  after the ECPoint record.
ROOL's avatar
ROOL committed
669
 *
ROOL's avatar
ROOL committed
670 671 672
 * \param grp       The ECP group to use.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
ROOL's avatar
ROOL committed
673 674 675
 * \param pt        The destination point.
 * \param buf       The address of the pointer to the start of the input buffer.
 * \param len       The length of the buffer.
ROOL's avatar
ROOL committed
676
 *
ROOL's avatar
ROOL committed
677
 * \return          \c 0 on success.
ROOL's avatar
ROOL committed
678 679
 * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization
 *                  failure.
ROOL's avatar
ROOL committed
680
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
ROOL's avatar
ROOL committed
681
 */
ROOL's avatar
ROOL committed
682 683 684
int mbedtls_ecp_tls_read_point( const mbedtls_ecp_group *grp,
                                mbedtls_ecp_point *pt,
                                const unsigned char **buf, size_t len );
ROOL's avatar
ROOL committed
685 686

/**
ROOL's avatar
ROOL committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700
 * \brief           This function exports a point as a TLS ECPoint record
 *                  defined in RFC 4492, Section 5.4.
 *
 * \param grp       The ECP group to use.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param pt        The point to be exported. This must be initialized.
 * \param format    The point format to use. This must be either
 *                  #MBEDTLS_ECP_PF_COMPRESSED or #MBEDTLS_ECP_PF_UNCOMPRESSED.
 * \param olen      The address at which to store the length in Bytes
 *                  of the data written.
 * \param buf       The target buffer. This must be a writable buffer of
 *                  length \p blen Bytes.
 * \param blen      The length of the target buffer \p buf in Bytes.
ROOL's avatar
ROOL committed
701
 *
ROOL's avatar
ROOL committed
702
 * \return          \c 0 on success.
ROOL's avatar
ROOL committed
703 704 705 706
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the input is invalid.
 * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the target buffer
 *                  is too small to hold the exported point.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
707
 */
ROOL's avatar
ROOL committed
708 709 710 711
int mbedtls_ecp_tls_write_point( const mbedtls_ecp_group *grp,
                                 const mbedtls_ecp_point *pt,
                                 int format, size_t *olen,
                                 unsigned char *buf, size_t blen );
ROOL's avatar
ROOL committed
712 713

/**
ROOL's avatar
ROOL committed
714 715
 * \brief           This function sets up an ECP group context
 *                  from a standardized set of domain parameters.
ROOL's avatar
ROOL committed
716
 *
ROOL's avatar
ROOL committed
717 718 719 720
 * \note            The index should be a value of the NamedCurve enum,
 *                  as defined in <em>RFC-4492: Elliptic Curve Cryptography
 *                  (ECC) Cipher Suites for Transport Layer Security (TLS)</em>,
 *                  usually in the form of an \c MBEDTLS_ECP_DP_XXX macro.
ROOL's avatar
ROOL committed
721
 *
ROOL's avatar
ROOL committed
722
 * \param grp       The group context to setup. This must be initialized.
ROOL's avatar
ROOL committed
723
 * \param id        The identifier of the domain parameter set to load.
ROOL's avatar
ROOL committed
724
 *
ROOL's avatar
ROOL committed
725 726 727 728
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if \p id doesn't
 *                  correspond to a known group.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
729 730 731 732
 */
int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );

/**
ROOL's avatar
ROOL committed
733 734
 * \brief           This function sets up an ECP group context from a TLS
 *                  ECParameters record as defined in RFC 4492, Section 5.4.
ROOL's avatar
ROOL committed
735
 *
ROOL's avatar
ROOL committed
736 737
 * \note            The read pointer \p buf is updated to point right after
 *                  the ECParameters record on exit.
ROOL's avatar
ROOL committed
738
 *
ROOL's avatar
ROOL committed
739
 * \param grp       The group context to setup. This must be initialized.
ROOL's avatar
ROOL committed
740
 * \param buf       The address of the pointer to the start of the input buffer.
ROOL's avatar
ROOL committed
741
 * \param len       The length of the input buffer \c *buf in Bytes.
ROOL's avatar
ROOL committed
742
 *
ROOL's avatar
ROOL committed
743 744
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
ROOL's avatar
ROOL committed
745 746 747
 * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
 *                  recognized.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
748
 */
ROOL's avatar
ROOL committed
749 750
int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp,
                                const unsigned char **buf, size_t len );
ROOL's avatar
ROOL committed
751 752

/**
ROOL's avatar
ROOL committed
753 754 755 756 757
 * \brief           This function extracts an elliptic curve group ID from a
 *                  TLS ECParameters record as defined in RFC 4492, Section 5.4.
 *
 * \note            The read pointer \p buf is updated to point right after
 *                  the ECParameters record on exit.
ROOL's avatar
ROOL committed
758
 *
ROOL's avatar
ROOL committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
 * \param grp       The address at which to store the group id.
 *                  This must not be \c NULL.
 * \param buf       The address of the pointer to the start of the input buffer.
 * \param len       The length of the input buffer \c *buf in Bytes.
 *
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
 * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE if the group is not
 *                  recognized.
 * \return          Another negative error code on other kinds of failure.
 */
int mbedtls_ecp_tls_read_group_id( mbedtls_ecp_group_id *grp,
                                   const unsigned char **buf,
                                   size_t len );
/**
 * \brief           This function exports an elliptic curve as a TLS
 *                  ECParameters record as defined in RFC 4492, Section 5.4.
 *
 * \param grp       The ECP group to be exported.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param olen      The address at which to store the number of Bytes written.
 *                  This must not be \c NULL.
 * \param buf       The buffer to write to. This must be a writable buffer
 *                  of length \p blen Bytes.
 * \param blen      The length of the output buffer \p buf in Bytes.
ROOL's avatar
ROOL committed
785
 *
ROOL's avatar
ROOL committed
786
 * \return          \c 0 on success.
ROOL's avatar
ROOL committed
787 788 789
 * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL if the output
 *                  buffer is too small to hold the exported group.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
790
 */
ROOL's avatar
ROOL committed
791 792 793
int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp,
                                 size_t *olen,
                                 unsigned char *buf, size_t blen );
ROOL's avatar
ROOL committed
794 795

/**
ROOL's avatar
ROOL committed
796 797
 * \brief           This function performs a scalar multiplication of a point
 *                  by an integer: \p R = \p m * \p P.
ROOL's avatar
ROOL committed
798
 *
ROOL's avatar
ROOL committed
799
 *                  It is not thread-safe to use same group in multiple threads.
ROOL's avatar
ROOL committed
800
 *
ROOL's avatar
ROOL committed
801 802 803 804
 * \note            To prevent timing attacks, this function
 *                  executes the exact same sequence of base-field
 *                  operations for any valid \p m. It avoids any if-branch or
 *                  array index depending on the value of \p m.
ROOL's avatar
ROOL committed
805
 *
ROOL's avatar
ROOL committed
806 807 808 809
 * \note            If \p f_rng is not NULL, it is used to randomize
 *                  intermediate results to prevent potential timing attacks
 *                  targeting these results. We recommend always providing
 *                  a non-NULL \p f_rng. The overhead is negligible.
ROOL's avatar
ROOL committed
810
 *
ROOL's avatar
ROOL committed
811 812 813 814 815 816 817 818 819 820
 * \param grp       The ECP group to use.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param R         The point in which to store the result of the calculation.
 *                  This must be initialized.
 * \param m         The integer by which to multiply. This must be initialized.
 * \param P         The point to multiply. This must be initialized.
 * \param f_rng     The RNG function. This may be \c NULL if randomization
 *                  of intermediate results isn't desired (discouraged).
 * \param p_rng     The RNG context to be passed to \p p_rng.
ROOL's avatar
ROOL committed
821 822 823 824 825
 *
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
 *                  key, or \p P is not a valid public key.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
826
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
827 828 829 830 831
 */
int mbedtls_ecp_mul( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
             int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );

ROOL's avatar
ROOL committed
832 833 834 835 836 837 838 839 840 841
/**
 * \brief           This function performs multiplication of a point by
 *                  an integer: \p R = \p m * \p P in a restartable way.
 *
 * \see             mbedtls_ecp_mul()
 *
 * \note            This function does the same as \c mbedtls_ecp_mul(), but
 *                  it can return early and restart according to the limit set
 *                  with \c mbedtls_ecp_set_max_ops() to reduce blocking.
 *
ROOL's avatar
ROOL committed
842 843 844 845 846 847 848 849 850 851
 * \param grp       The ECP group to use.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param R         The point in which to store the result of the calculation.
 *                  This must be initialized.
 * \param m         The integer by which to multiply. This must be initialized.
 * \param P         The point to multiply. This must be initialized.
 * \param f_rng     The RNG function. This may be \c NULL if randomization
 *                  of intermediate results isn't desired (discouraged).
 * \param p_rng     The RNG context to be passed to \p p_rng.
ROOL's avatar
ROOL committed
852 853 854 855 856 857 858 859
 * \param rs_ctx    The restart context (NULL disables restart).
 *
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m is not a valid private
 *                  key, or \p P is not a valid public key.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
 * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
 *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
ROOL's avatar
ROOL committed
860
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
861 862 863 864 865 866
 */
int mbedtls_ecp_mul_restartable( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
             int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
             mbedtls_ecp_restart_ctx *rs_ctx );

ROOL's avatar
ROOL committed
867
/**
ROOL's avatar
ROOL committed
868 869 870 871
 * \brief           This function performs multiplication and addition of two
 *                  points by integers: \p R = \p m * \p P + \p n * \p Q
 *
 *                  It is not thread-safe to use same group in multiple threads.
ROOL's avatar
ROOL committed
872
 *
ROOL's avatar
ROOL committed
873 874
 * \note            In contrast to mbedtls_ecp_mul(), this function does not
 *                  guarantee a constant execution flow and timing.
ROOL's avatar
ROOL committed
875
 *
ROOL's avatar
ROOL committed
876 877 878 879 880
 * \param grp       The ECP group to use.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param R         The point in which to store the result of the calculation.
 *                  This must be initialized.
ROOL's avatar
ROOL committed
881
 * \param m         The integer by which to multiply \p P.
ROOL's avatar
ROOL committed
882 883
 *                  This must be initialized.
 * \param P         The point to multiply by \p m. This must be initialized.
ROOL's avatar
ROOL committed
884
 * \param n         The integer by which to multiply \p Q.
ROOL's avatar
ROOL committed
885
 *                  This must be initialized.
ROOL's avatar
ROOL committed
886
 * \param Q         The point to be multiplied by \p n.
ROOL's avatar
ROOL committed
887
 *                  This must be initialized.
ROOL's avatar
ROOL committed
888
 *
ROOL's avatar
ROOL committed
889 890 891 892 893
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
 *                  valid private keys, or \p P or \p Q are not valid public
 *                  keys.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
894
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
895 896 897 898 899
 */
int mbedtls_ecp_muladd( mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
             const mbedtls_mpi *n, const mbedtls_ecp_point *Q );

ROOL's avatar
ROOL committed
900 901 902 903 904 905 906 907 908 909 910
/**
 * \brief           This function performs multiplication and addition of two
 *                  points by integers: \p R = \p m * \p P + \p n * \p Q in a
 *                  restartable way.
 *
 * \see             \c mbedtls_ecp_muladd()
 *
 * \note            This function works the same as \c mbedtls_ecp_muladd(),
 *                  but it can return early and restart according to the limit
 *                  set with \c mbedtls_ecp_set_max_ops() to reduce blocking.
 *
ROOL's avatar
ROOL committed
911 912 913 914 915
 * \param grp       The ECP group to use.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param R         The point in which to store the result of the calculation.
 *                  This must be initialized.
ROOL's avatar
ROOL committed
916
 * \param m         The integer by which to multiply \p P.
ROOL's avatar
ROOL committed
917 918
 *                  This must be initialized.
 * \param P         The point to multiply by \p m. This must be initialized.
ROOL's avatar
ROOL committed
919
 * \param n         The integer by which to multiply \p Q.
ROOL's avatar
ROOL committed
920
 *                  This must be initialized.
ROOL's avatar
ROOL committed
921
 * \param Q         The point to be multiplied by \p n.
ROOL's avatar
ROOL committed
922
 *                  This must be initialized.
ROOL's avatar
ROOL committed
923 924 925 926 927 928 929 930 931
 * \param rs_ctx    The restart context (NULL disables restart).
 *
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if \p m or \p n are not
 *                  valid private keys, or \p P or \p Q are not valid public
 *                  keys.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
 * \return          #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
 *                  operations was reached: see \c mbedtls_ecp_set_max_ops().
ROOL's avatar
ROOL committed
932
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
933 934 935 936 937 938 939
 */
int mbedtls_ecp_muladd_restartable(
             mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
             const mbedtls_mpi *m, const mbedtls_ecp_point *P,
             const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
             mbedtls_ecp_restart_ctx *rs_ctx );

ROOL's avatar
ROOL committed
940
/**
ROOL's avatar
ROOL committed
941 942
 * \brief           This function checks that a point is a valid public key
 *                  on this curve.
ROOL's avatar
ROOL committed
943
 *
ROOL's avatar
ROOL committed
944 945 946 947 948 949 950
 *                  It only checks that the point is non-zero, has
 *                  valid coordinates and lies on the curve. It does not verify
 *                  that it is indeed a multiple of \p G. This additional
 *                  check is computationally more expensive, is not required
 *                  by standards, and should not be necessary if the group
 *                  used has a small cofactor. In particular, it is useless for
 *                  the NIST groups which all have a cofactor of 1.
ROOL's avatar
ROOL committed
951
 *
ROOL's avatar
ROOL committed
952 953 954 955
 * \note            This function uses bare components rather than an
 *                  ::mbedtls_ecp_keypair structure, to ease use with other
 *                  structures, such as ::mbedtls_ecdh_context or
 *                  ::mbedtls_ecdsa_context.
ROOL's avatar
ROOL committed
956
 *
ROOL's avatar
ROOL committed
957 958 959 960
 * \param grp       The ECP group the point should belong to.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param pt        The point to check. This must be initialized.
ROOL's avatar
ROOL committed
961
 *
ROOL's avatar
ROOL committed
962
 * \return          \c 0 if the point is a valid public key.
ROOL's avatar
ROOL committed
963 964 965
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not
 *                  a valid public key for the given curve.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
966
 */
ROOL's avatar
ROOL committed
967 968
int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp,
                              const mbedtls_ecp_point *pt );
ROOL's avatar
ROOL committed
969 970

/**
ROOL's avatar
ROOL committed
971 972
 * \brief           This function checks that an \p mbedtls_mpi is a
 *                  valid private key for this curve.
ROOL's avatar
ROOL committed
973
 *
ROOL's avatar
ROOL committed
974 975 976 977
 * \note            This function uses bare components rather than an
 *                  ::mbedtls_ecp_keypair structure to ease use with other
 *                  structures, such as ::mbedtls_ecdh_context or
 *                  ::mbedtls_ecdsa_context.
ROOL's avatar
ROOL committed
978
 *
ROOL's avatar
ROOL committed
979 980 981 982
 * \param grp       The ECP group the private key should belong to.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param d         The integer to check. This must be initialized.
ROOL's avatar
ROOL committed
983
 *
ROOL's avatar
ROOL committed
984
 * \return          \c 0 if the point is a valid private key.
ROOL's avatar
ROOL committed
985 986 987
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY if the point is not a valid
 *                  private key for the given curve.
 * \return          Another negative error code on other kinds of failure.
ROOL's avatar
ROOL committed
988
 */
ROOL's avatar
ROOL committed
989 990
int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp,
                               const mbedtls_mpi *d );
ROOL's avatar
ROOL committed
991

ROOL's avatar
ROOL committed
992 993 994
/**
 * \brief           This function generates a private key.
 *
ROOL's avatar
ROOL committed
995 996 997 998 999 1000 1001
 * \param grp       The ECP group to generate a private key for.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param d         The destination MPI (secret part). This must be initialized.
 * \param f_rng     The RNG function. This must not be \c NULL.
 * \param p_rng     The RNG parameter to be passed to \p f_rng. This may be
 *                  \c NULL if \p f_rng doesn't need a context argument.
ROOL's avatar
ROOL committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
 *
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
 *                  on failure.
 */
int mbedtls_ecp_gen_privkey( const mbedtls_ecp_group *grp,
                     mbedtls_mpi *d,
                     int (*f_rng)(void *, unsigned char *, size_t),
                     void *p_rng );

ROOL's avatar
ROOL committed
1012
/**
ROOL's avatar
ROOL committed
1013 1014
 * \brief           This function generates a keypair with a configurable base
 *                  point.
ROOL's avatar
ROOL committed
1015
 *
ROOL's avatar
ROOL committed
1016 1017 1018 1019
 * \note            This function uses bare components rather than an
 *                  ::mbedtls_ecp_keypair structure to ease use with other
 *                  structures, such as ::mbedtls_ecdh_context or
 *                  ::mbedtls_ecdsa_context.
ROOL's avatar
ROOL committed
1020
 *
ROOL's avatar
ROOL committed
1021 1022 1023 1024 1025 1026
 * \param grp       The ECP group to generate a key pair for.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
 * \param G         The base point to use. This must be initialized
 *                  and belong to \p grp. It replaces the default base
 *                  point \c grp->G used by mbedtls_ecp_gen_keypair().
ROOL's avatar
ROOL committed
1027
 * \param d         The destination MPI (secret part).
ROOL's avatar
ROOL committed
1028
 *                  This must be initialized.
ROOL's avatar
ROOL committed
1029
 * \param Q         The destination point (public part).
ROOL's avatar
ROOL committed
1030 1031 1032 1033
 *                  This must be initialized.
 * \param f_rng     The RNG function. This must not be \c NULL.
 * \param p_rng     The RNG context to be passed to \p f_rng. This may
 *                  be \c NULL if \p f_rng doesn't need a context argument.
ROOL's avatar
ROOL committed
1034
 *
ROOL's avatar
ROOL committed
1035 1036 1037
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
 *                  on failure.
ROOL's avatar
ROOL committed
1038 1039
 */
int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
ROOL's avatar
ROOL committed
1040 1041 1042 1043
                                  const mbedtls_ecp_point *G,
                                  mbedtls_mpi *d, mbedtls_ecp_point *Q,
                                  int (*f_rng)(void *, unsigned char *, size_t),
                                  void *p_rng );
ROOL's avatar
ROOL committed
1044 1045

/**
ROOL's avatar
ROOL committed
1046
 * \brief           This function generates an ECP keypair.
ROOL's avatar
ROOL committed
1047
 *
ROOL's avatar
ROOL committed
1048 1049 1050 1051
 * \note            This function uses bare components rather than an
 *                  ::mbedtls_ecp_keypair structure to ease use with other
 *                  structures, such as ::mbedtls_ecdh_context or
 *                  ::mbedtls_ecdsa_context.
ROOL's avatar
ROOL committed
1052
 *
ROOL's avatar
ROOL committed
1053 1054 1055
 * \param grp       The ECP group to generate a key pair for.
 *                  This must be initialized and have group parameters
 *                  set, for example through mbedtls_ecp_group_load().
ROOL's avatar
ROOL committed
1056
 * \param d         The destination MPI (secret part).
ROOL's avatar
ROOL committed
1057
 *                  This must be initialized.
ROOL's avatar
ROOL committed
1058
 * \param Q         The destination point (public part).
ROOL's avatar
ROOL committed
1059 1060 1061 1062
 *                  This must be initialized.
 * \param f_rng     The RNG function. This must not be \c NULL.
 * \param p_rng     The RNG context to be passed to \p f_rng. This may
 *                  be \c NULL if \p f_rng doesn't need a context argument.
ROOL's avatar
ROOL committed
1063
 *
ROOL's avatar
ROOL committed
1064 1065 1066
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
 *                  on failure.
ROOL's avatar
ROOL committed
1067
 */
ROOL's avatar
ROOL committed
1068 1069 1070 1071
int mbedtls_ecp_gen_keypair( mbedtls_ecp_group *grp, mbedtls_mpi *d,
                             mbedtls_ecp_point *Q,
                             int (*f_rng)(void *, unsigned char *, size_t),
                             void *p_rng );
ROOL's avatar
ROOL committed
1072 1073

/**
ROOL's avatar
ROOL committed
1074
 * \brief           This function generates an ECP key.
ROOL's avatar
ROOL committed
1075
 *
ROOL's avatar
ROOL committed
1076
 * \param grp_id    The ECP group identifier.
ROOL's avatar
ROOL committed
1077 1078 1079 1080
 * \param key       The destination key. This must be initialized.
 * \param f_rng     The RNG function to use. This must not be \c NULL.
 * \param p_rng     The RNG context to be passed to \p f_rng. This may
 *                  be \c NULL if \p f_rng doesn't need a context argument.
ROOL's avatar
ROOL committed
1081
 *
ROOL's avatar
ROOL committed
1082 1083 1084
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_ECP_XXX or \c MBEDTLS_MPI_XXX error code
 *                  on failure.
ROOL's avatar
ROOL committed
1085 1086
 */
int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
ROOL's avatar
ROOL committed
1087 1088
                         int (*f_rng)(void *, unsigned char *, size_t),
                         void *p_rng );
ROOL's avatar
ROOL committed
1089 1090

/**
ROOL's avatar
ROOL committed
1091 1092 1093 1094
 * \brief           This function checks that the keypair objects
 *                  \p pub and \p prv have the same group and the
 *                  same public point, and that the private key in
 *                  \p prv is consistent with the public key.
ROOL's avatar
ROOL committed
1095
 *
ROOL's avatar
ROOL committed
1096 1097 1098
 * \param pub       The keypair structure holding the public key. This
 *                  must be initialized. If it contains a private key, that
 *                  part is ignored.
ROOL's avatar
ROOL committed
1099
 * \param prv       The keypair structure holding the full keypair.
ROOL's avatar
ROOL committed
1100
 *                  This must be initialized.
ROOL's avatar
ROOL committed
1101
 *
ROOL's avatar
ROOL committed
1102 1103 1104 1105
 * \return          \c 0 on success, meaning that the keys are valid and match.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if the keys are invalid or do not match.
 * \return          An \c MBEDTLS_ERR_ECP_XXX or an \c MBEDTLS_ERR_MPI_XXX
 *                  error code on calculation failure.
ROOL's avatar
ROOL committed
1106
 */
ROOL's avatar
ROOL committed
1107 1108
int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub,
                                const mbedtls_ecp_keypair *prv );
ROOL's avatar
ROOL committed
1109 1110 1111 1112

#if defined(MBEDTLS_SELF_TEST)

/**
ROOL's avatar
ROOL committed
1113
 * \brief          The ECP checkup routine.
ROOL's avatar
ROOL committed
1114
 *
ROOL's avatar
ROOL committed
1115 1116
 * \return         \c 0 on success.
 * \return         \c 1 on failure.
ROOL's avatar
ROOL committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
 */
int mbedtls_ecp_self_test( int verbose );

#endif /* MBEDTLS_SELF_TEST */

#ifdef __cplusplus
}
#endif

#endif /* ecp.h */