ecp 30.2 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 52
#define MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH                  -0x4C00  /**< The buffer contains a valid signature followed by more data. */
#define MBEDTLS_ERR_ECP_HW_ACCEL_FAILED                   -0x4B80  /**< The ECP hardware accelerator failed. */
ROOL's avatar
ROOL committed
53 54 55 56 57 58

#ifdef __cplusplus
extern "C" {
#endif

/**
ROOL's avatar
ROOL committed
59
 * Domain-parameter identifiers: curve, subgroup, and generator.
ROOL's avatar
ROOL committed
60
 *
ROOL's avatar
ROOL committed
61
 * \note Only curves over prime fields are supported.
ROOL's avatar
ROOL committed
62 63
 *
 * \warning This library does not support validation of arbitrary domain
ROOL's avatar
ROOL committed
64
 * parameters. Therefore, only standardized domain parameters from trusted
ROOL's avatar
ROOL committed
65 66 67 68
 * sources should be used. See mbedtls_ecp_group_load().
 */
typedef enum
{
ROOL's avatar
ROOL committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82
    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
83 84 85
} mbedtls_ecp_group_id;

/**
ROOL's avatar
ROOL committed
86
 * The number of supported curves, plus one for #MBEDTLS_ECP_DP_NONE.
ROOL's avatar
ROOL committed
87
 *
ROOL's avatar
ROOL committed
88
 * \note Montgomery curves are currently excluded.
ROOL's avatar
ROOL committed
89 90 91 92
 */
#define MBEDTLS_ECP_DP_MAX     12

/**
ROOL's avatar
ROOL committed
93
 * Curve information, for use by other modules.
ROOL's avatar
ROOL committed
94 95 96
 */
typedef struct
{
ROOL's avatar
ROOL committed
97 98 99 100
    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
101 102 103
} mbedtls_ecp_curve_info;

/**
ROOL's avatar
ROOL committed
104
 * \brief           The ECP point structure, in Jacobian coordinates.
ROOL's avatar
ROOL committed
105 106
 *
 * \note            All functions expect and return points satisfying
ROOL's avatar
ROOL committed
107 108 109 110 111 112
 *                  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
113 114 115
 */
typedef struct
{
ROOL's avatar
ROOL committed
116 117 118
    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
119 120 121
}
mbedtls_ecp_point;

ROOL's avatar
ROOL committed
122 123 124
#if !defined(MBEDTLS_ECP_ALT)
/*
 * default mbed TLS elliptic curve arithmetic implementation
ROOL's avatar
ROOL committed
125
 *
ROOL's avatar
ROOL committed
126 127 128
 * (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
129 130 131
 */

/**
ROOL's avatar
ROOL committed
132 133 134 135 136 137 138 139
 * \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
140
 *
ROOL's avatar
ROOL committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
 * 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
157 158 159 160
 *
 */
typedef struct
{
ROOL's avatar
ROOL committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    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
181
}
ROOL's avatar
ROOL committed
182
mbedtls_ecp_group;
ROOL's avatar
ROOL committed
183 184 185 186 187

/**
 * \name SECTION: Module settings
 *
 * The configuration options you can set for this module are in this section.
ROOL's avatar
ROOL committed
188
 * Either change them in config.h, or define them using the compiler command line.
ROOL's avatar
ROOL committed
189 190 191 192 193
 * \{
 */

#if !defined(MBEDTLS_ECP_MAX_BITS)
/**
ROOL's avatar
ROOL committed
194
 * The maximum size of the groups, that is, of \c N and \c P.
ROOL's avatar
ROOL committed
195
 */
ROOL's avatar
ROOL committed
196
#define MBEDTLS_ECP_MAX_BITS     521   /**< The maximum size of groups, in bits. */
ROOL's avatar
ROOL committed
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
#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
222
#define MBEDTLS_ECP_WINDOW_SIZE    6   /**< The maximum window size used. */
ROOL's avatar
ROOL committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236
#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
237
#define MBEDTLS_ECP_FIXED_POINT_OPTIM  1   /**< Enable fixed-point speed-up. */
ROOL's avatar
ROOL committed
238 239 240 241
#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */

/* \} name SECTION: Module settings */

ROOL's avatar
ROOL committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
#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.
 */
typedef struct
{
    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
262 263 264
/*
 * Point formats, from RFC 4492's enum ECPointFormat
 */
ROOL's avatar
ROOL committed
265 266
#define MBEDTLS_ECP_PF_UNCOMPRESSED    0   /**< Uncompressed point format. */
#define MBEDTLS_ECP_PF_COMPRESSED      1   /**< Compressed point format. */
ROOL's avatar
ROOL committed
267 268 269 270

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

/**
ROOL's avatar
ROOL committed
274 275 276
 * \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
277
 *
ROOL's avatar
ROOL committed
278
 * \return          A statically allocated array. The last entry is 0.
ROOL's avatar
ROOL committed
279 280 281 282
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list( void );

/**
ROOL's avatar
ROOL committed
283 284 285
 * \brief           This function retrieves the list of internal group
 *                  identifiers of all supported curves in the order of
 *                  preference.
ROOL's avatar
ROOL committed
286 287 288 289 290 291 292
 *
 * \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
293 294
 * \brief           This function retrieves curve information from an internal
 *                  group identifier.
ROOL's avatar
ROOL committed
295
 *
ROOL's avatar
ROOL committed
296
 * \param grp_id    An \c MBEDTLS_ECP_DP_XXX value.
ROOL's avatar
ROOL committed
297
 *
ROOL's avatar
ROOL committed
298 299
 * \return          The associated curve information on success.
 * \return          NULL on failure.
ROOL's avatar
ROOL committed
300 301 302 303
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id( mbedtls_ecp_group_id grp_id );

/**
ROOL's avatar
ROOL committed
304 305
 * \brief           This function retrieves curve information from a TLS
 *                  NamedCurve value.
ROOL's avatar
ROOL committed
306
 *
ROOL's avatar
ROOL committed
307
 * \param tls_id    An \c MBEDTLS_ECP_DP_XXX value.
ROOL's avatar
ROOL committed
308
 *
ROOL's avatar
ROOL committed
309 310
 * \return          The associated curve information on success.
 * \return          NULL on failure.
ROOL's avatar
ROOL committed
311 312 313 314
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id( uint16_t tls_id );

/**
ROOL's avatar
ROOL committed
315 316
 * \brief           This function retrieves curve information from a
 *                  human-readable name.
ROOL's avatar
ROOL committed
317
 *
ROOL's avatar
ROOL committed
318
 * \param name      The human-readable name.
ROOL's avatar
ROOL committed
319
 *
ROOL's avatar
ROOL committed
320 321
 * \return          The associated curve information on success.
 * \return          NULL on failure.
ROOL's avatar
ROOL committed
322 323 324 325
 */
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name( const char *name );

/**
ROOL's avatar
ROOL committed
326 327 328
 * \brief           This function initializes a point as zero.
 *
 * \param pt        The point to initialize.
ROOL's avatar
ROOL committed
329 330 331 332
 */
void mbedtls_ecp_point_init( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
333 334 335 336 337 338 339
 * \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
340 341 342 343
 */
void mbedtls_ecp_group_init( mbedtls_ecp_group *grp );

/**
ROOL's avatar
ROOL committed
344 345 346
 * \brief           This function initializes a key pair as an invalid one.
 *
 * \param key       The key pair to initialize.
ROOL's avatar
ROOL committed
347 348 349 350
 */
void mbedtls_ecp_keypair_init( mbedtls_ecp_keypair *key );

/**
ROOL's avatar
ROOL committed
351 352 353
 * \brief           This function frees the components of a point.
 *
 * \param pt        The point to free.
ROOL's avatar
ROOL committed
354 355 356 357
 */
void mbedtls_ecp_point_free( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
358 359
 * \brief           This function frees the components of an ECP group.
 * \param grp       The group to free.
ROOL's avatar
ROOL committed
360 361 362 363
 */
void mbedtls_ecp_group_free( mbedtls_ecp_group *grp );

/**
ROOL's avatar
ROOL committed
364 365
 * \brief           This function frees the components of a key pair.
 * \param key       The key pair to free.
ROOL's avatar
ROOL committed
366 367 368 369
 */
void mbedtls_ecp_keypair_free( mbedtls_ecp_keypair *key );

/**
ROOL's avatar
ROOL committed
370 371
 * \brief           This function copies the contents of point \p Q into
 *                  point \p P.
ROOL's avatar
ROOL committed
372
 *
ROOL's avatar
ROOL committed
373 374
 * \param P         The destination point.
 * \param Q         The source point.
ROOL's avatar
ROOL committed
375
 *
ROOL's avatar
ROOL committed
376 377
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
378 379 380 381
 */
int mbedtls_ecp_copy( mbedtls_ecp_point *P, const mbedtls_ecp_point *Q );

/**
ROOL's avatar
ROOL committed
382 383
 * \brief           This function copies the contents of group \p src into
 *                  group \p dst.
ROOL's avatar
ROOL committed
384
 *
ROOL's avatar
ROOL committed
385 386
 * \param dst       The destination group.
 * \param src       The source group.
ROOL's avatar
ROOL committed
387
 *
ROOL's avatar
ROOL committed
388 389
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
390 391 392 393
 */
int mbedtls_ecp_group_copy( mbedtls_ecp_group *dst, const mbedtls_ecp_group *src );

/**
ROOL's avatar
ROOL committed
394
 * \brief           This function sets a point to zero.
ROOL's avatar
ROOL committed
395
 *
ROOL's avatar
ROOL committed
396
 * \param pt        The point to set.
ROOL's avatar
ROOL committed
397
 *
ROOL's avatar
ROOL committed
398 399
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_MPI_ALLOC_FAILED on memory-allocation failure.
ROOL's avatar
ROOL committed
400 401 402 403
 */
int mbedtls_ecp_set_zero( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
404
 * \brief           This function checks if a point is zero.
ROOL's avatar
ROOL committed
405
 *
ROOL's avatar
ROOL committed
406
 * \param pt        The point to test.
ROOL's avatar
ROOL committed
407
 *
ROOL's avatar
ROOL committed
408 409
 * \return          \c 1 if the point is zero.
 * \return          \c 0 if the point is non-zero.
ROOL's avatar
ROOL committed
410 411 412 413
 */
int mbedtls_ecp_is_zero( mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
414
 * \brief           This function compares two points.
ROOL's avatar
ROOL committed
415
 *
ROOL's avatar
ROOL committed
416
 * \note            This assumes that the points are normalized. Otherwise,
ROOL's avatar
ROOL committed
417 418
 *                  they may compare as "not equal" even if they are.
 *
ROOL's avatar
ROOL committed
419 420
 * \param P         The first point to compare.
 * \param Q         The second point to compare.
ROOL's avatar
ROOL committed
421
 *
ROOL's avatar
ROOL committed
422 423
 * \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
424 425 426 427 428
 */
int mbedtls_ecp_point_cmp( const mbedtls_ecp_point *P,
                           const mbedtls_ecp_point *Q );

/**
ROOL's avatar
ROOL committed
429 430
 * \brief           This function imports a non-zero point from two ASCII
 *                  strings.
ROOL's avatar
ROOL committed
431
 *
ROOL's avatar
ROOL committed
432 433 434 435
 * \param P         The destination point.
 * \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
436
 *
ROOL's avatar
ROOL committed
437 438
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_MPI_XXX error code on failure.
ROOL's avatar
ROOL committed
439 440 441 442 443
 */
int mbedtls_ecp_point_read_string( mbedtls_ecp_point *P, int radix,
                           const char *x, const char *y );

/**
ROOL's avatar
ROOL committed
444
 * \brief           This function exports a point into unsigned binary data.
ROOL's avatar
ROOL committed
445
 *
ROOL's avatar
ROOL committed
446 447 448 449 450 451
 * \param grp       The group to which the point should belong.
 * \param P         The point to export.
 * \param format    The point format. Should be an \c MBEDTLS_ECP_PF_XXX macro.
 * \param olen      The length of the output.
 * \param buf       The output buffer.
 * \param buflen    The length of the output buffer.
ROOL's avatar
ROOL committed
452
 *
ROOL's avatar
ROOL committed
453 454 455
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA
 *                  or #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
ROOL's avatar
ROOL committed
456 457 458 459 460 461
 */
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
462
 * \brief           This function imports a point from unsigned binary data.
ROOL's avatar
ROOL committed
463
 *
ROOL's avatar
ROOL committed
464 465 466
 * \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
467
 *
ROOL's avatar
ROOL committed
468 469 470 471 472 473 474 475 476
 * \param grp       The group to which the point should belong.
 * \param P         The point to import.
 * \param buf       The input buffer.
 * \param ilen      The length of the input.
 *
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
 * \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
477 478 479 480 481 482 483
 *                  is not implemented.
 *
 */
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
484
 * \brief           This function imports a point from a TLS ECPoint record.
ROOL's avatar
ROOL committed
485
 *
ROOL's avatar
ROOL committed
486 487
 * \note            On function return, \p buf is updated to point to immediately
 *                  after the ECPoint record.
ROOL's avatar
ROOL committed
488
 *
ROOL's avatar
ROOL committed
489 490 491 492
 * \param grp       The ECP group used.
 * \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
493
 *
ROOL's avatar
ROOL committed
494 495 496
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
ROOL's avatar
ROOL committed
497 498 499 500 501
 */
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
502
 * \brief           This function exports a point as a TLS ECPoint record.
ROOL's avatar
ROOL committed
503
 *
ROOL's avatar
ROOL committed
504 505 506 507 508 509 510
 * \param grp       The ECP group used.
 * \param pt        The point format to export to. The point format is an
 *                  \c MBEDTLS_ECP_PF_XXX constant.
 * \param format    The export format.
 * \param olen      The length of the data written.
 * \param buf       The buffer to write to.
 * \param blen      The length of the buffer.
ROOL's avatar
ROOL committed
511
 *
ROOL's avatar
ROOL committed
512 513 514
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA or
 *                  #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
ROOL's avatar
ROOL committed
515 516 517 518 519 520
 */
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
521
 * \brief           This function sets a group using standardized domain parameters.
ROOL's avatar
ROOL committed
522
 *
ROOL's avatar
ROOL committed
523 524 525 526
 * \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
527
 *
ROOL's avatar
ROOL committed
528 529
 * \param grp       The destination group.
 * \param id        The identifier of the domain parameter set to load.
ROOL's avatar
ROOL committed
530
 *
ROOL's avatar
ROOL committed
531 532 533 534
 * \return          \c 0 on success,
 * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
 * \return          #MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups.

ROOL's avatar
ROOL committed
535 536 537 538
 */
int mbedtls_ecp_group_load( mbedtls_ecp_group *grp, mbedtls_ecp_group_id id );

/**
ROOL's avatar
ROOL committed
539
 * \brief           This function sets a group from a TLS ECParameters record.
ROOL's avatar
ROOL committed
540
 *
ROOL's avatar
ROOL committed
541 542
 * \note            \p buf is updated to point right after the ECParameters record
 *                  on exit.
ROOL's avatar
ROOL committed
543
 *
ROOL's avatar
ROOL committed
544 545 546
 * \param grp       The destination group.
 * \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
547
 *
ROOL's avatar
ROOL committed
548 549 550
 * \return          \c 0 on success.
 * \return          An \c MBEDTLS_ERR_MPI_XXX error code on initialization failure.
 * \return          #MBEDTLS_ERR_ECP_BAD_INPUT_DATA if input is invalid.
ROOL's avatar
ROOL committed
551 552 553 554
 */
int mbedtls_ecp_tls_read_group( mbedtls_ecp_group *grp, const unsigned char **buf, size_t len );

/**
ROOL's avatar
ROOL committed
555
 * \brief           This function writes the TLS ECParameters record for a group.
ROOL's avatar
ROOL committed
556
 *
ROOL's avatar
ROOL committed
557 558 559 560
 * \param grp       The ECP group used.
 * \param olen      The number of Bytes written.
 * \param buf       The buffer to write to.
 * \param blen      The length of the buffer.
ROOL's avatar
ROOL committed
561
 *
ROOL's avatar
ROOL committed
562 563
 * \return          \c 0 on success.
 * \return          #MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL on failure.
ROOL's avatar
ROOL committed
564 565 566 567 568
 */
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
569 570
 * \brief           This function performs multiplication of a point by
 *                  an integer: \p R = \p m * \p P.
ROOL's avatar
ROOL committed
571
 *
ROOL's avatar
ROOL committed
572
 *                  It is not thread-safe to use same group in multiple threads.
ROOL's avatar
ROOL committed
573
 *
ROOL's avatar
ROOL committed
574 575 576 577
 * \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
578
 *
ROOL's avatar
ROOL committed
579 580 581 582
 * \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
583
 *
ROOL's avatar
ROOL committed
584 585 586 587 588 589 590 591 592 593 594
 * \param grp       The ECP group.
 * \param R         The destination point.
 * \param m         The integer by which to multiply.
 * \param P         The point to multiply.
 * \param f_rng     The RNG function.
 * \param p_rng     The RNG context.
 *
 * \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
595 596 597 598 599 600
 */
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
601 602 603 604
 * \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
605
 *
ROOL's avatar
ROOL committed
606 607
 * \note            In contrast to mbedtls_ecp_mul(), this function does not
 *                  guarantee a constant execution flow and timing.
ROOL's avatar
ROOL committed
608
 *
ROOL's avatar
ROOL committed
609 610 611 612 613 614
 * \param grp       The ECP group.
 * \param R         The destination point.
 * \param m         The integer by which to multiply \p P.
 * \param P         The point to multiply by \p m.
 * \param n         The integer by which to multiply \p Q.
 * \param Q         The point to be multiplied by \p n.
ROOL's avatar
ROOL committed
615
 *
ROOL's avatar
ROOL committed
616 617 618 619 620
 * \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
621 622 623 624 625 626
 */
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
627 628
 * \brief           This function checks that a point is a valid public key
 *                  on this curve.
ROOL's avatar
ROOL committed
629
 *
ROOL's avatar
ROOL committed
630 631 632 633 634 635 636
 *                  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
637
 *
ROOL's avatar
ROOL committed
638 639 640 641
 * \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
642
 *
ROOL's avatar
ROOL committed
643 644
 * \param grp       The curve the point should lie on.
 * \param pt        The point to check.
ROOL's avatar
ROOL committed
645
 *
ROOL's avatar
ROOL committed
646 647
 * \return          \c 0 if the point is a valid public key.
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY on failure.
ROOL's avatar
ROOL committed
648 649 650 651
 */
int mbedtls_ecp_check_pubkey( const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt );

/**
ROOL's avatar
ROOL committed
652 653
 * \brief           This function checks that an \p mbedtls_mpi is a valid private
 *                  key for this curve.
ROOL's avatar
ROOL committed
654
 *
ROOL's avatar
ROOL committed
655 656 657 658
 * \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
659
 *
ROOL's avatar
ROOL committed
660 661
 * \param grp       The group used.
 * \param d         The integer to check.
ROOL's avatar
ROOL committed
662
 *
ROOL's avatar
ROOL committed
663 664
 * \return          \c 0 if the point is a valid private key.
 * \return          #MBEDTLS_ERR_ECP_INVALID_KEY on failure.
ROOL's avatar
ROOL committed
665 666 667 668
 */
int mbedtls_ecp_check_privkey( const mbedtls_ecp_group *grp, const mbedtls_mpi *d );

/**
ROOL's avatar
ROOL committed
669 670
 * \brief           This function generates a keypair with a configurable base
 *                  point.
ROOL's avatar
ROOL committed
671
 *
ROOL's avatar
ROOL committed
672 673 674 675
 * \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
676
 *
ROOL's avatar
ROOL committed
677 678 679 680 681 682
 * \param grp       The ECP group.
 * \param G         The chosen base point.
 * \param d         The destination MPI (secret part).
 * \param Q         The destination point (public part).
 * \param f_rng     The RNG function.
 * \param p_rng     The RNG context.
ROOL's avatar
ROOL committed
683
 *
ROOL's avatar
ROOL committed
684 685 686
 * \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
687 688 689 690 691 692 693 694
 */
int mbedtls_ecp_gen_keypair_base( mbedtls_ecp_group *grp,
                     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
695
 * \brief           This function generates an ECP keypair.
ROOL's avatar
ROOL committed
696
 *
ROOL's avatar
ROOL committed
697 698 699 700
 * \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
701
 *
ROOL's avatar
ROOL committed
702 703 704 705 706
 * \param grp       The ECP group.
 * \param d         The destination MPI (secret part).
 * \param Q         The destination point (public part).
 * \param f_rng     The RNG function.
 * \param p_rng     The RNG context.
ROOL's avatar
ROOL committed
707
 *
ROOL's avatar
ROOL committed
708 709 710
 * \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
711 712 713 714 715 716
 */
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
717
 * \brief           This function generates an ECP key.
ROOL's avatar
ROOL committed
718
 *
ROOL's avatar
ROOL committed
719 720 721 722
 * \param grp_id    The ECP group identifier.
 * \param key       The destination key.
 * \param f_rng     The RNG function.
 * \param p_rng     The RNG context.
ROOL's avatar
ROOL committed
723
 *
ROOL's avatar
ROOL committed
724 725 726
 * \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
727 728 729 730 731
 */
int mbedtls_ecp_gen_key( mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
                int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );

/**
ROOL's avatar
ROOL committed
732 733 734 735
 * \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
736
 *
ROOL's avatar
ROOL committed
737 738 739
 * \param pub       The keypair structure holding the public key.
 *                  If it contains a private key, that part is ignored.
 * \param prv       The keypair structure holding the full keypair.
ROOL's avatar
ROOL committed
740
 *
ROOL's avatar
ROOL committed
741 742 743 744
 * \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
745 746 747 748 749 750
 */
int mbedtls_ecp_check_pub_priv( const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv );

#if defined(MBEDTLS_SELF_TEST)

/**
ROOL's avatar
ROOL committed
751
 * \brief          The ECP checkup routine.
ROOL's avatar
ROOL committed
752
 *
ROOL's avatar
ROOL committed
753 754
 * \return         \c 0 on success.
 * \return         \c 1 on failure.
ROOL's avatar
ROOL committed
755 756 757 758 759 760 761 762 763 764
 */
int mbedtls_ecp_self_test( int verbose );

#endif /* MBEDTLS_SELF_TEST */

#ifdef __cplusplus
}
#endif

#endif /* ecp.h */