blob: dec4e0a681e03d8789c0bc4bc95b6dfbdb395ecf [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é-Gonnard0bad5c22013-01-26 15:30:46 +010030#include "polarssl/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é-Gonnard70380392013-09-16 16:19:53 +020064} ecp_group_id;
65
66/**
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020067 * Number of supported curves (plus one for NONE)
68 */
69#define POLARSSL_ECP_DP_MAX 6
70
71/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020072 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020073 */
74typedef struct
75{
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020076 ecp_group_id grp_id; /*!< Internal identifier */
77 uint16_t tls_id; /*!< TLS NamedCurve identifier */
78 uint16_t size; /*!< Curve size in bits */
79 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020080} ecp_curve_info;
81
82/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010083 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010084 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010085 * \note All functions expect and return points satisfying
86 * the following condition: Z == 0 or Z == 1. (Other
87 * values of Z are used by internal functions only.)
88 * The point is zero, or "at infinity", if Z == 0.
89 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010090 */
91typedef struct
92{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010093 mpi X; /*!< the point's X coordinate */
94 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010095 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010096}
97ecp_point;
98
99/**
100 * \brief ECP group structure
101 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200102 * The curves we consider are defined by y^2 = x^3 + A x + B mod P,
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100103 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100104 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100105 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100106 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200107 * If modp is NULL, reduction modulo P is done using a generic algorithm.
108 * Otherwise, it must point to a function that takes an mpi in the range
109 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
110 * than pbits, so that the integer may be efficiently brought in the 0..P-1
111 * range by a few additions or substractions. It must return 0 on success and
112 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100113 */
114typedef struct
115{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100116 ecp_group_id id; /*!< RFC 4492 group ID */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100117 mpi P; /*!< prime modulus of the base field */
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200118 mpi A; /*!< currently unused (-3 assumed) */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100119 mpi B; /*!< constant term in the equation */
120 ecp_point G; /*!< generator of the subgroup used */
121 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100122 size_t pbits; /*!< number of bits in P */
123 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200124 unsigned int h; /*!< cofactor (unused now: assume 1) */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100125 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200126 int (*t_pre)(ecp_point *, void *); /*!< currently unused */
127 int (*t_post)(ecp_point *, void *); /*!< currently unused */
128 void *t_data; /*!< currently unused */
129 ecp_point *T; /*!< pre-computed points (unused now) */
130 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100131}
132ecp_group;
133
134/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200135 * \brief ECP key pair structure
136 *
137 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200138 *
139 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200140 */
141typedef struct
142{
143 ecp_group grp; /*!< Elliptic curve and base point */
144 mpi d; /*!< our secret value */
145 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200146}
147ecp_keypair;
148
149/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200150 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100151 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200152#define POLARSSL_ECP_MAX_BITS 521
153#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200154#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100155
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100156/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100157 * Maximum window size (actually, NAF width) used for point multipliation.
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200158 * Default: 8.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100159 * Minimum value: 2. Maximum value: 8.
160 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100161 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200162 * points used for point multiplication.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100163 *
164 * Reduction in size may reduce speed for big curves.
165 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200166#define POLARSSL_ECP_WINDOW_SIZE 8 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100167
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100168/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100169 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100170 */
171#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
172#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
173
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100174/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100175 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100176 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100177#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100178
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200179#ifdef __cplusplus
180extern "C" {
181#endif
182
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100183/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200184 * \brief Return the list of supported curves with associated info
185 *
186 * \return A statically allocated array, the last entry is 0.
187 */
188const ecp_curve_info *ecp_curve_list( void );
189
190/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100191 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100192 */
193void ecp_point_init( ecp_point *pt );
194
195/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100196 * \brief Initialize a group (to something meaningless)
197 */
198void ecp_group_init( ecp_group *grp );
199
200/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200201 * \brief Initialize a key pair (as an invalid one)
202 */
203void ecp_keypair_init( ecp_keypair *key );
204
205/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100206 * \brief Free the components of a point
207 */
208void ecp_point_free( ecp_point *pt );
209
210/**
211 * \brief Free the components of an ECP group
212 */
213void ecp_group_free( ecp_group *grp );
214
215/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200216 * \brief Free the components of a key pair
217 */
218void ecp_keypair_free( ecp_keypair *key );
219
220/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100221 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100222 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100223 * \param pt Destination point
224 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100225 * \return 0 if successful,
226 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100227 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100228int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100229
230/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100231 * \brief Tell if a point is zero
232 *
233 * \param pt Point to test
234 *
235 * \return 1 if point is zero, 0 otherwise
236 */
237int ecp_is_zero( ecp_point *pt );
238
239/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100240 * \brief Copy the contents of point Q into P
241 *
242 * \param P Destination point
243 * \param Q Source point
244 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100245 * \return 0 if successful,
246 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100247 */
248int ecp_copy( ecp_point *P, const ecp_point *Q );
249
250/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200251 * \brief Copy the contents of a group object
252 *
253 * \param dst Destination group
254 * \param src Source group
255 *
256 * \return 0 if successful,
257 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
258 */
259int ecp_group_copy( ecp_group *dst, const ecp_group *src );
260
261/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100262 * \brief Import a non-zero point from two ASCII strings
263 *
264 * \param P Destination point
265 * \param radix Input numeric base
266 * \param x First affine coordinate as a null-terminated string
267 * \param y Second affine coordinate as a null-terminated string
268 *
269 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
270 */
271int ecp_point_read_string( ecp_point *P, int radix,
272 const char *x, const char *y );
273
274/**
275 * \brief Import an ECP group from null-terminated ASCII strings
276 *
277 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100278 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100279 * \param p Prime modulus of the base field
280 * \param b Constant term in the equation
281 * \param gx The generator's X coordinate
282 * \param gy The generator's Y coordinate
283 * \param n The generator's order
284 *
285 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100286 *
287 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100288 */
289int ecp_group_read_string( ecp_group *grp, int radix,
290 const char *p, const char *b,
291 const char *gx, const char *gy, const char *n);
292
293/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100294 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100295 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100296 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100297 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100298 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100299 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100300 * \param buf Output buffer
301 * \param buflen Length of the output buffer
302 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100303 * \return 0 if successful,
304 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
305 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100306 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100307int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100308 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100309 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100310
311/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100312 * \brief Import a point from unsigned binary data
313 *
314 * \param grp Group to which the point should belong
315 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100316 * \param buf Input buffer
317 * \param ilen Actual length of input
318 *
319 * \return 0 if successful,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200320 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100321 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
322 *
323 * \note This function does NOT check that the point actually
324 * belongs to the given group, see ecp_check_pubkey() for
325 * that.
326 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100327int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
328 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100329
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100330/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100331 * \brief Set a group using well-known domain parameters
332 *
333 * \param grp Destination group
334 * \param index Index in the list of well-known domain parameters
335 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100336 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100337 * POLARSSL_ERR_MPI_XXX if initialization failed
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200338 * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100339 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100340 * \note Index should be a value of RFC 4492's enum NamdeCurve,
341 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100342 */
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200343int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100344
345/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100346 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100347 *
348 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100349 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100350 * \param len Buffer length
351 *
352 * \return O if successful,
353 * POLARSSL_ERR_MPI_XXX if initialization failed
354 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
355 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100356int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100357
358/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100359 * \brief Write the TLS ECParameters record for a group
360 *
361 * \param grp ECP group used
362 * \param olen Number of bytes actually written
363 * \param buf Buffer to write to
364 * \param blen Buffer length
365 *
366 * \return 0 if successful,
367 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
368 */
369int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
370 unsigned char *buf, size_t blen );
371
372/**
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200373 * \brief Get curve information from an internal group identifier
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200374 *
375 * \param grp_id A POLARSSL_ECP_DP_XXX value
376 *
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200377 * \return The associated curve information or NULL
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200378 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200379const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200380
381/**
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200382 * \brief Get curve information from a TLS NamedCurve value
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200383 *
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200384 * \param grp_id A POLARSSL_ECP_DP_XXX value
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200385 *
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200386 * \return The associated curve information or NULL
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200387 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200388const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200389
390/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100391 * \brief Import a point from a TLS ECPoint record
392 *
393 * \param grp ECP group used
394 * \param pt Destination point
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100395 * \param buf $(Start of input buffer)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100396 * \param len Buffer length
397 *
398 * \return O if successful,
399 * POLARSSL_ERR_MPI_XXX if initialization failed
400 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
401 */
402int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100403 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100404
405/**
406 * \brief Export a point as a TLS ECPoint record
407 *
408 * \param grp ECP group used
409 * \param pt Point to export
410 * \param format Export format
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200411 * \param olen length of data written
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100412 * \param buf Buffer to write to
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200413 * \param blen Buffer length
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100414 *
415 * \return 0 if successful,
416 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
417 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
418 */
419int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100420 int format, size_t *olen,
421 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100422
423/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100424 * \brief Addition: R = P + Q
425 *
426 * \param grp ECP group
427 * \param R Destination point
428 * \param P Left-hand point
429 * \param Q Right-hand point
430 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100431 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100432 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100433 */
434int ecp_add( const ecp_group *grp, ecp_point *R,
435 const ecp_point *P, const ecp_point *Q );
436
437/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100438 * \brief Subtraction: R = P - Q
439 *
440 * \param grp ECP group
441 * \param R Destination point
442 * \param P Left-hand point
443 * \param Q Right-hand point
444 *
445 * \return 0 if successful,
446 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
447 */
448int ecp_sub( const ecp_group *grp, ecp_point *R,
449 const ecp_point *P, const ecp_point *Q );
450
451/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100452 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200453 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100454 *
455 * \param grp ECP group
456 * \param R Destination point
457 * \param m Integer by which to multiply
458 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200459 * \param f_rng RNG function (see notes)
460 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100461 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100462 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100463 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200464 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if m < 0 of m has greater
465 * bit length than N, the number of points in the group.
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100466 *
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200467 * \note In order to prevent simple timing attacks, this function
468 * executes a constant number of operations (that is, point
469 * doubling and addition of distinct points) for random m in
470 * the allowed range.
471 *
472 * \note If f_rng is not NULL, it is used to randomize projective
473 * coordinates of indermediate results, in order to prevent
474 * more elaborate timing attacks relying on intermediate
Manuel Pégourié-Gonnard337b29c2013-09-07 11:52:27 +0200475 * operations. (This is a prophylactic measure since no such
476 * attack has been published yet.) Since this contermeasure
477 * has very low overhead, it is recommended to always provide
478 * a non-NULL f_rng parameter when using secret inputs.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100479 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200480int ecp_mul( ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200481 const mpi *m, const ecp_point *P,
482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
483
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100484
485/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200486 * \brief Check that a point is a valid public key on this curve
487 *
488 * \param grp Curve/group the point should belong to
489 * \param pt Point to check
490 *
491 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200492 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200493 *
494 * \note This function only checks the point is non-zero, has valid
495 * coordinates and lies on the curve, but not that it is
496 * indeed a multiple of G. This is additional check is more
497 * expensive, isn't required by standards, and shouldn't be
498 * necessary if the group used has a small cofactor. In
499 * particular, it is useless for the NIST groups which all
500 * have a cofactor of 1.
501 *
502 * \note Uses bare components rather than an ecp_keypair structure
503 * in order to ease use with other structures such as
504 * ecdh_context of ecdsa_context.
505 */
506int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
507
508/**
509 * \brief Check that an mpi is a valid private key for this curve
510 *
511 * \param grp Group used
512 * \param d Integer to check
513 *
514 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200515 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200516 *
517 * \note Uses bare components rather than an ecp_keypair structure
518 * in order to ease use with other structures such as
519 * ecdh_context of ecdsa_context.
520 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200521int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200522
523/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100524 * \brief Generate a keypair
525 *
526 * \param grp ECP group
527 * \param d Destination MPI (secret part)
528 * \param Q Destination point (public part)
529 * \param f_rng RNG function
530 * \param p_rng RNG parameter
531 *
532 * \return 0 if successful,
533 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200534 *
535 * \note Uses bare components rather than an ecp_keypair structure
536 * in order to ease use with other structures such as
537 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100538 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200539int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100540 int (*f_rng)(void *, unsigned char *, size_t),
541 void *p_rng );
542
543/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100544 * \brief Checkup routine
545 *
546 * \return 0 if successful, or 1 if the test failed
547 */
548int ecp_self_test( int verbose );
549
550#ifdef __cplusplus
551}
552#endif
553
554#endif