blob: b35b0d1d04d42b37df65f2626d33b4fda0af1d7e [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/**
2 * \file ecp.h
3 *
4 * \brief Elliptic curves over GF(p)
5 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01006 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_ECP_H
28#define POLARSSL_ECP_H
29
Manuel Pégourié-Gonnardbdc96762013-10-03 11:50:39 +020030#include "bignum.h"
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010031
32/*
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +010033 * ECP error codes
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010034 */
Paul Bakkercf4365f2013-01-16 17:00:43 +010035#define POLARSSL_ERR_ECP_BAD_INPUT_DATA -0x4F80 /**< Bad input parameters to function. */
Paul Bakkerfd3eac52013-06-29 23:31:33 +020036#define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +020037#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +020038#define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +020039#define POLARSSL_ERR_ECP_MALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
40#define POLARSSL_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
41#define POLARSSL_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010047/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020048 * Domain parameters (curve, subgroup and generator) identifiers.
49 *
50 * Only curves over prime fields are supported.
51 *
52 * \warning This library does not support validation of arbitrary domain
53 * parameters. Therefore, only well-known domain parameters from trusted
54 * sources should be used. See ecp_use_known_dp().
55 */
56typedef enum
57{
58 POLARSSL_ECP_DP_NONE = 0,
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020059 POLARSSL_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
60 POLARSSL_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
61 POLARSSL_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
62 POLARSSL_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
63 POLARSSL_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020064 POLARSSL_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
65 POLARSSL_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
66 POLARSSL_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010067 POLARSSL_ECP_DP_M221, /*!< (not implemented yet) */
68 POLARSSL_ECP_DP_M255, /*!< Curve25519 */
69 POLARSSL_ECP_DP_M383, /*!< (not implemented yet) */
70 POLARSSL_ECP_DP_M511, /*!< (not implemented yet) */
Manuel Pégourié-Gonnardad3fab62014-01-28 20:05:16 +010071 POLARSSL_ECP_DP_SECP192K1, /*!< 192-bits "Koblitz" curve */
72 POLARSSL_ECP_DP_SECP224K1, /*!< 224-bits "Koblitz" curve */
73 POLARSSL_ECP_DP_SECP256K1, /*!< 256-bits "Koblitz" curve */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020074} ecp_group_id;
75
76/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010077 * Number of supported curves (plus one for NONE).
78 *
79 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020080 */
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +010081#define POLARSSL_ECP_DP_MAX 12
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020082
83/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020084 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020085 */
86typedef struct
87{
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020088 ecp_group_id grp_id; /*!< Internal identifier */
89 uint16_t tls_id; /*!< TLS NamedCurve identifier */
90 uint16_t size; /*!< Curve size in bits */
91 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020092} ecp_curve_info;
93
94/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010095 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010096 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010097 * \note All functions expect and return points satisfying
98 * the following condition: Z == 0 or Z == 1. (Other
99 * values of Z are used by internal functions only.)
100 * The point is zero, or "at infinity", if Z == 0.
101 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100102 */
103typedef struct
104{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100105 mpi X; /*!< the point's X coordinate */
106 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100107 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100108}
109ecp_point;
110
111/**
112 * \brief ECP group structure
113 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100114 * We consider two types of curves equations:
115 * 1. Short Weierstrass y^2 = x^3 + A x + B mod P (SEC1 + RFC 4492)
116 * 2. Montgomery, y^2 = x^3 + A x^2 + x mod P (M255 + draft)
117 * In both cases, a generator G for a prime-order subgroup is fixed. In the
118 * short weierstrass, this subgroup is actually the whole curve, and its
119 * cardinal is denoted by N.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100120 *
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100121 * In the case of Montgomery curves, we don't store A but (A + 2) / 4 which is
122 * the quantity actualy used in the formulas. Also, nbits is not the size of N
123 * but the required size for private keys.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100124 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200125 * If modp is NULL, reduction modulo P is done using a generic algorithm.
126 * Otherwise, it must point to a function that takes an mpi in the range
127 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
128 * than pbits, so that the integer may be efficiently brought in the 0..P-1
129 * range by a few additions or substractions. It must return 0 on success and
130 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100131 */
132typedef struct
133{
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200134 ecp_group_id id; /*!< internal group identifier */
135 mpi P; /*!< prime modulus of the base field */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100136 mpi A; /*!< 1. A in the equation, or 2. (A + 2) / 4 */
137 mpi B; /*!< 1. B in the equation, or 2. unused */
138 ecp_point G; /*!< generator of the (sub)group used */
139 mpi N; /*!< 1. the order of G, or 2. unused */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200140 size_t pbits; /*!< number of bits in P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100141 size_t nbits; /*!< number of bits in 1. P, or 2. private keys */
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100142 unsigned int h; /*!< internal: 1 if the constants are static */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200143 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100144 int (*t_pre)(ecp_point *, void *); /*!< unused */
145 int (*t_post)(ecp_point *, void *); /*!< unused */
146 void *t_data; /*!< unused */
147 ecp_point *T; /*!< pre-computed points for ecp_mul_comb() */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200148 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100149}
150ecp_group;
151
152/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200153 * \brief ECP key pair structure
154 *
155 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200156 *
157 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200158 */
159typedef struct
160{
161 ecp_group grp; /*!< Elliptic curve and base point */
162 mpi d; /*!< our secret value */
163 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200164}
165ecp_keypair;
166
Paul Bakkere1b665e2013-12-11 16:02:58 +0100167#if !defined(POLARSSL_CONFIG_OPTIONS)
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200168/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200169 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100170 */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100171#define POLARSSL_ECP_MAX_BITS 521 /**< Maximum bit size of groups */
172#endif
173
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200174#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200175#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100176
Paul Bakkere1b665e2013-12-11 16:02:58 +0100177#if !defined(POLARSSL_CONFIG_OPTIONS)
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100178/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100179 * Maximum "window" size used for point multiplication.
180 * Default: 6.
181 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100182 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100183 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100184 * points used for point multiplication. This value is directly tied to EC
185 * peak memory usage, so decreasing it by one should roughly cut memory usage
186 * by two (if large curves are in use).
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100187 *
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100188 * Reduction in size may reduce speed, but larger curves are impacted first.
189 * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1):
190 * w-size: 6 5 4 3 2
191 * 521 145 141 135 120 97
192 * 384 214 209 198 177 146
193 * 256 320 320 303 262 226
194 * 224 475 475 453 398 342
195 * 192 640 640 633 587 476
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100196 */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100197#define POLARSSL_ECP_WINDOW_SIZE 6 /**< Maximum window size used */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +0100198
199/*
200 * Trade memory for speed on fixed-point multiplication.
201 *
202 * This speeds up repeated multiplication of the generator (that is, the
203 * multiplication in ECDSA signatures, and half of the multiplications in
204 * ECDSA verification and ECDHE) by a factor roughly 3 to 4.
205 *
206 * The cost is increasing EC peak memory usage by a factor roughly 2.
207 *
208 * Change this value to 0 to reduce peak memory usage.
209 */
210#define POLARSSL_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up */
Paul Bakkere1b665e2013-12-11 16:02:58 +0100211#endif
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100212
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100213/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100214 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100215 */
216#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
217#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
218
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100219/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100220 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100221 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100222#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100223
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100224/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100225 * \brief Get the list of supported curves in order of preferrence
226 * (full information)
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200227 *
228 * \return A statically allocated array, the last entry is 0.
229 */
230const ecp_curve_info *ecp_curve_list( void );
231
232/**
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100233 * \brief Get the list of supported curves in order of preferrence
234 * (grp_id only)
235 *
236 * \return A statically allocated array,
237 * terminated with POLARSSL_ECP_DP_NONE.
238 */
239const ecp_group_id *ecp_grp_id_list( void );
240
241/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200242 * \brief Get curve information from an internal group identifier
243 *
244 * \param grp_id A POLARSSL_ECP_DP_XXX value
245 *
246 * \return The associated curve information or NULL
247 */
248const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id );
249
250/**
251 * \brief Get curve information from a TLS NamedCurve value
252 *
Paul Bakkera36d23e2013-12-30 17:57:27 +0100253 * \param tls_id A POLARSSL_ECP_DP_XXX value
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200254 *
255 * \return The associated curve information or NULL
256 */
257const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id );
258
259/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100260 * \brief Get curve information from a human-readable name
261 *
262 * \param name The name
263 *
264 * \return The associated curve information or NULL
265 */
266const ecp_curve_info *ecp_curve_info_from_name( const char *name );
267
268/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100269 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100270 */
271void ecp_point_init( ecp_point *pt );
272
273/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100274 * \brief Initialize a group (to something meaningless)
275 */
276void ecp_group_init( ecp_group *grp );
277
278/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200279 * \brief Initialize a key pair (as an invalid one)
280 */
281void ecp_keypair_init( ecp_keypair *key );
282
283/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100284 * \brief Free the components of a point
285 */
286void ecp_point_free( ecp_point *pt );
287
288/**
289 * \brief Free the components of an ECP group
290 */
291void ecp_group_free( ecp_group *grp );
292
293/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200294 * \brief Free the components of a key pair
295 */
296void ecp_keypair_free( ecp_keypair *key );
297
298/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100299 * \brief Copy the contents of point Q into P
300 *
301 * \param P Destination point
302 * \param Q Source point
303 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100304 * \return 0 if successful,
305 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100306 */
307int ecp_copy( ecp_point *P, const ecp_point *Q );
308
309/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200310 * \brief Copy the contents of a group object
311 *
312 * \param dst Destination group
313 * \param src Source group
314 *
315 * \return 0 if successful,
316 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
317 */
318int ecp_group_copy( ecp_group *dst, const ecp_group *src );
319
320/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200321 * \brief Set a point to zero
322 *
323 * \param pt Destination point
324 *
325 * \return 0 if successful,
326 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
327 */
328int ecp_set_zero( ecp_point *pt );
329
330/**
331 * \brief Tell if a point is zero
332 *
333 * \param pt Point to test
334 *
335 * \return 1 if point is zero, 0 otherwise
336 */
337int ecp_is_zero( ecp_point *pt );
338
339/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100340 * \brief Import a non-zero point from two ASCII strings
341 *
342 * \param P Destination point
343 * \param radix Input numeric base
344 * \param x First affine coordinate as a null-terminated string
345 * \param y Second affine coordinate as a null-terminated string
346 *
347 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
348 */
349int ecp_point_read_string( ecp_point *P, int radix,
350 const char *x, const char *y );
351
352/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100353 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100354 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100355 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100356 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100357 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100358 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100359 * \param buf Output buffer
360 * \param buflen Length of the output buffer
361 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100362 * \return 0 if successful,
363 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
364 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100365 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100366int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100367 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100368 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100369
370/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100371 * \brief Import a point from unsigned binary data
372 *
373 * \param grp Group to which the point should belong
374 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100375 * \param buf Input buffer
376 * \param ilen Actual length of input
377 *
378 * \return 0 if successful,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200379 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100380 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
381 *
382 * \note This function does NOT check that the point actually
383 * belongs to the given group, see ecp_check_pubkey() for
384 * that.
385 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100386int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
387 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100388
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100389/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200390 * \brief Import a point from a TLS ECPoint record
391 *
392 * \param grp ECP group used
393 * \param pt Destination point
394 * \param buf $(Start of input buffer)
395 * \param len Buffer length
396 *
397 * \return O if successful,
398 * POLARSSL_ERR_MPI_XXX if initialization failed
399 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
400 */
401int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
402 const unsigned char **buf, size_t len );
403
404/**
405 * \brief Export a point as a TLS ECPoint record
406 *
407 * \param grp ECP group used
408 * \param pt Point to export
409 * \param format Export format
410 * \param olen length of data written
411 * \param buf Buffer to write to
412 * \param blen Buffer length
413 *
414 * \return 0 if successful,
415 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
416 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
417 */
418int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
419 int format, size_t *olen,
420 unsigned char *buf, size_t blen );
421
422/**
423 * \brief Import an ECP group from null-terminated ASCII strings
424 *
425 * \param grp Destination group
426 * \param radix Input numeric base
427 * \param p Prime modulus of the base field
428 * \param b Constant term in the equation
429 * \param gx The generator's X coordinate
430 * \param gy The generator's Y coordinate
431 * \param n The generator's order
432 *
433 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
434 *
435 * \note Sets all fields except modp.
436 */
437int ecp_group_read_string( ecp_group *grp, int radix,
438 const char *p, const char *b,
439 const char *gx, const char *gy, const char *n);
440
441/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100442 * \brief Set a group using well-known domain parameters
443 *
444 * \param grp Destination group
445 * \param index Index in the list of well-known domain parameters
446 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100447 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100448 * POLARSSL_ERR_MPI_XXX if initialization failed
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200449 * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100450 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100451 * \note Index should be a value of RFC 4492's enum NamdeCurve,
452 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100453 */
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200454int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100455
456/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100457 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100458 *
459 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100460 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100461 * \param len Buffer length
462 *
463 * \return O if successful,
464 * POLARSSL_ERR_MPI_XXX if initialization failed
465 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
466 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100467int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100468
469/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100470 * \brief Write the TLS ECParameters record for a group
471 *
472 * \param grp ECP group used
473 * \param olen Number of bytes actually written
474 * \param buf Buffer to write to
475 * \param blen Buffer length
476 *
477 * \return 0 if successful,
478 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
479 */
480int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
481 unsigned char *buf, size_t blen );
482
483/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100484 * \brief Addition: R = P + Q
485 *
486 * \param grp ECP group
487 * \param R Destination point
488 * \param P Left-hand point
489 * \param Q Right-hand point
490 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100491 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100492 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100493 *
494 * \note This function does not support Montgomery curves, such as
495 * Curve25519.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100496 */
497int ecp_add( const ecp_group *grp, ecp_point *R,
498 const ecp_point *P, const ecp_point *Q );
499
500/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100501 * \brief Subtraction: R = P - Q
502 *
503 * \param grp ECP group
504 * \param R Destination point
505 * \param P Left-hand point
506 * \param Q Right-hand point
507 *
508 * \return 0 if successful,
509 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100510 *
511 * \note This function does not support Montgomery curves, such as
512 * Curve25519.
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100513 */
514int ecp_sub( const ecp_group *grp, ecp_point *R,
515 const ecp_point *P, const ecp_point *Q );
516
517/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100518 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200519 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100520 *
521 * \param grp ECP group
522 * \param R Destination point
523 * \param m Integer by which to multiply
524 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200525 * \param f_rng RNG function (see notes)
526 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100527 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100528 * \return 0 if successful,
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100529 * POLARSSL_ERR_ECP_INVALID_KEY if m is not a valid privkey
530 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100531 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100532 *
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100533 * \note In order to prevent timing attacks, this function
534 * executes the exact same sequence of (base field)
535 * operations for any valid m. It avoids any if-branch or
536 * array index depending on the value of m.
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200537 *
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100538 * \note If f_rng is not NULL, it is used to randomize intermediate
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100539 * results in order to prevent potential timing attacks
540 * targetting these results. It is recommended to always
541 * provide a non-NULL f_rng (the overhead is negligible).
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100542 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100543int ecp_mul( ecp_group *grp, ecp_point *R,
544 const mpi *m, const ecp_point *P,
545 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100546
547/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200548 * \brief Check that a point is a valid public key on this curve
549 *
550 * \param grp Curve/group the point should belong to
551 * \param pt Point to check
552 *
553 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200554 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200555 *
556 * \note This function only checks the point is non-zero, has valid
557 * coordinates and lies on the curve, but not that it is
558 * indeed a multiple of G. This is additional check is more
559 * expensive, isn't required by standards, and shouldn't be
560 * necessary if the group used has a small cofactor. In
561 * particular, it is useless for the NIST groups which all
562 * have a cofactor of 1.
563 *
564 * \note Uses bare components rather than an ecp_keypair structure
565 * in order to ease use with other structures such as
566 * ecdh_context of ecdsa_context.
567 */
568int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
569
570/**
571 * \brief Check that an mpi is a valid private key for this curve
572 *
573 * \param grp Group used
574 * \param d Integer to check
575 *
576 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200577 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200578 *
579 * \note Uses bare components rather than an ecp_keypair structure
580 * in order to ease use with other structures such as
581 * ecdh_context of ecdsa_context.
582 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200583int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200584
585/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100586 * \brief Generate a keypair
587 *
588 * \param grp ECP group
589 * \param d Destination MPI (secret part)
590 * \param Q Destination point (public part)
591 * \param f_rng RNG function
592 * \param p_rng RNG parameter
593 *
594 * \return 0 if successful,
595 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200596 *
597 * \note Uses bare components rather than an ecp_keypair structure
598 * in order to ease use with other structures such as
599 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100600 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200601int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100602 int (*f_rng)(void *, unsigned char *, size_t),
603 void *p_rng );
604
605/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100606 * \brief Generate a keypair
607 *
608 * \param grp_id ECP group identifier
609 * \param key Destination keypair
610 * \param f_rng RNG function
611 * \param p_rng RNG parameter
612 *
613 * \return 0 if successful,
614 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
615 */
616int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
617 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
618
619/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100620 * \brief Checkup routine
621 *
622 * \return 0 if successful, or 1 if the test failed
623 */
624int ecp_self_test( int verbose );
625
626#ifdef __cplusplus
627}
628#endif
629
630#endif