blob: 7940b321976a090e4bee0b79c08371bcca3c38fe [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é-Gonnard70380392013-09-16 16:19:53 +020067} ecp_group_id;
68
69/**
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020070 * Number of supported curves (plus one for NONE)
71 */
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020072#define POLARSSL_ECP_DP_MAX 9
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020073
74/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020075 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020076 */
77typedef struct
78{
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020079 ecp_group_id grp_id; /*!< Internal identifier */
80 uint16_t tls_id; /*!< TLS NamedCurve identifier */
81 uint16_t size; /*!< Curve size in bits */
82 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020083} ecp_curve_info;
84
85/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010086 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010087 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010088 * \note All functions expect and return points satisfying
89 * the following condition: Z == 0 or Z == 1. (Other
90 * values of Z are used by internal functions only.)
91 * The point is zero, or "at infinity", if Z == 0.
92 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010093 */
94typedef struct
95{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010096 mpi X; /*!< the point's X coordinate */
97 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010098 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010099}
100ecp_point;
101
102/**
103 * \brief ECP group structure
104 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200105 * 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 +0100106 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100107 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100108 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100109 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200110 * If modp is NULL, reduction modulo P is done using a generic algorithm.
111 * Otherwise, it must point to a function that takes an mpi in the range
112 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
113 * than pbits, so that the integer may be efficiently brought in the 0..P-1
114 * range by a few additions or substractions. It must return 0 on success and
115 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100116 */
117typedef struct
118{
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200119 ecp_group_id id; /*!< internal group identifier */
120 mpi P; /*!< prime modulus of the base field */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200121 mpi A; /*!< linear term in the equation */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200122 mpi B; /*!< constant term in the equation */
123 ecp_point G; /*!< generator of the subgroup used */
124 mpi N; /*!< the order of G */
125 size_t pbits; /*!< number of bits in P */
126 size_t nbits; /*!< number of bits in N */
127 unsigned int h; /*!< cofactor (unused now: assume 1) */
128 int (*modp)(mpi *); /*!< function for fast reduction mod P */
129 int (*t_pre)(ecp_point *, void *); /*!< currently unused */
130 int (*t_post)(ecp_point *, void *); /*!< currently unused */
131 void *t_data; /*!< currently unused */
132 ecp_point *T; /*!< pre-computed points for ecp_mul() */
133 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100134}
135ecp_group;
136
137/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200138 * \brief ECP key pair structure
139 *
140 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200141 *
142 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200143 */
144typedef struct
145{
146 ecp_group grp; /*!< Elliptic curve and base point */
147 mpi d; /*!< our secret value */
148 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200149}
150ecp_keypair;
151
152/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200153 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100154 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200155#define POLARSSL_ECP_MAX_BITS 521
156#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200157#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100158
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100159/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100160 * Maximum window size (actually, NAF width) used for point multipliation.
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200161 * Default: 8.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100162 * Minimum value: 2. Maximum value: 8.
163 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100164 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200165 * points used for point multiplication.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100166 *
167 * Reduction in size may reduce speed for big curves.
168 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200169#define POLARSSL_ECP_WINDOW_SIZE 8 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100170
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100171/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100172 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100173 */
174#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
175#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
176
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100177/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100178 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100179 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100180#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100181
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100182/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200183 * \brief Return the list of supported curves with associated info
184 *
185 * \return A statically allocated array, the last entry is 0.
186 */
187const ecp_curve_info *ecp_curve_list( void );
188
189/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100190 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100191 */
192void ecp_point_init( ecp_point *pt );
193
194/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100195 * \brief Initialize a group (to something meaningless)
196 */
197void ecp_group_init( ecp_group *grp );
198
199/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200200 * \brief Initialize a key pair (as an invalid one)
201 */
202void ecp_keypair_init( ecp_keypair *key );
203
204/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100205 * \brief Free the components of a point
206 */
207void ecp_point_free( ecp_point *pt );
208
209/**
210 * \brief Free the components of an ECP group
211 */
212void ecp_group_free( ecp_group *grp );
213
214/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200215 * \brief Free the components of a key pair
216 */
217void ecp_keypair_free( ecp_keypair *key );
218
219/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100220 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100221 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100222 * \param pt Destination point
223 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100224 * \return 0 if successful,
225 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100226 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100227int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100228
229/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100230 * \brief Tell if a point is zero
231 *
232 * \param pt Point to test
233 *
234 * \return 1 if point is zero, 0 otherwise
235 */
236int ecp_is_zero( ecp_point *pt );
237
238/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100239 * \brief Copy the contents of point Q into P
240 *
241 * \param P Destination point
242 * \param Q Source point
243 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100244 * \return 0 if successful,
245 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100246 */
247int ecp_copy( ecp_point *P, const ecp_point *Q );
248
249/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200250 * \brief Copy the contents of a group object
251 *
252 * \param dst Destination group
253 * \param src Source group
254 *
255 * \return 0 if successful,
256 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
257 */
258int ecp_group_copy( ecp_group *dst, const ecp_group *src );
259
260/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100261 * \brief Import a non-zero point from two ASCII strings
262 *
263 * \param P Destination point
264 * \param radix Input numeric base
265 * \param x First affine coordinate as a null-terminated string
266 * \param y Second affine coordinate as a null-terminated string
267 *
268 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
269 */
270int ecp_point_read_string( ecp_point *P, int radix,
271 const char *x, const char *y );
272
273/**
274 * \brief Import an ECP group from null-terminated ASCII strings
275 *
276 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100277 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100278 * \param p Prime modulus of the base field
279 * \param b Constant term in the equation
280 * \param gx The generator's X coordinate
281 * \param gy The generator's Y coordinate
282 * \param n The generator's order
283 *
284 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100285 *
286 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100287 */
288int ecp_group_read_string( ecp_group *grp, int radix,
289 const char *p, const char *b,
290 const char *gx, const char *gy, const char *n);
291
292/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100293 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100294 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100295 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100296 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100297 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100298 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100299 * \param buf Output buffer
300 * \param buflen Length of the output buffer
301 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100302 * \return 0 if successful,
303 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
304 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100305 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100306int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100307 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100308 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100309
310/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100311 * \brief Import a point from unsigned binary data
312 *
313 * \param grp Group to which the point should belong
314 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100315 * \param buf Input buffer
316 * \param ilen Actual length of input
317 *
318 * \return 0 if successful,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200319 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100320 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
321 *
322 * \note This function does NOT check that the point actually
323 * belongs to the given group, see ecp_check_pubkey() for
324 * that.
325 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100326int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
327 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100328
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100329/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100330 * \brief Set a group using well-known domain parameters
331 *
332 * \param grp Destination group
333 * \param index Index in the list of well-known domain parameters
334 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100335 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100336 * POLARSSL_ERR_MPI_XXX if initialization failed
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200337 * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100338 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100339 * \note Index should be a value of RFC 4492's enum NamdeCurve,
340 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100341 */
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200342int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100343
344/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100345 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100346 *
347 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100348 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100349 * \param len Buffer length
350 *
351 * \return O if successful,
352 * POLARSSL_ERR_MPI_XXX if initialization failed
353 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
354 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100355int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100356
357/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100358 * \brief Write the TLS ECParameters record for a group
359 *
360 * \param grp ECP group used
361 * \param olen Number of bytes actually written
362 * \param buf Buffer to write to
363 * \param blen Buffer length
364 *
365 * \return 0 if successful,
366 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
367 */
368int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
369 unsigned char *buf, size_t blen );
370
371/**
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200372 * \brief Get curve information from an internal group identifier
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200373 *
374 * \param grp_id A POLARSSL_ECP_DP_XXX value
375 *
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200376 * \return The associated curve information or NULL
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200377 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200378const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200379
380/**
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200381 * \brief Get curve information from a TLS NamedCurve value
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200382 *
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200383 * \param grp_id A POLARSSL_ECP_DP_XXX value
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200384 *
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200385 * \return The associated curve information or NULL
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200386 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200387const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200388
389/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100390 * \brief Import a point from a TLS ECPoint record
391 *
392 * \param grp ECP group used
393 * \param pt Destination point
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100394 * \param buf $(Start of input buffer)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100395 * \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,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100402 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100403
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
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200410 * \param olen length of data written
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100411 * \param buf Buffer to write to
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200412 * \param blen Buffer length
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100413 *
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,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100419 int format, size_t *olen,
420 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100421
422/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100423 * \brief Addition: R = P + Q
424 *
425 * \param grp ECP group
426 * \param R Destination point
427 * \param P Left-hand point
428 * \param Q Right-hand point
429 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100430 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100431 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100432 */
433int ecp_add( const ecp_group *grp, ecp_point *R,
434 const ecp_point *P, const ecp_point *Q );
435
436/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100437 * \brief Subtraction: R = P - Q
438 *
439 * \param grp ECP group
440 * \param R Destination point
441 * \param P Left-hand point
442 * \param Q Right-hand point
443 *
444 * \return 0 if successful,
445 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
446 */
447int ecp_sub( const ecp_group *grp, ecp_point *R,
448 const ecp_point *P, const ecp_point *Q );
449
450/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100451 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200452 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100453 *
454 * \param grp ECP group
455 * \param R Destination point
456 * \param m Integer by which to multiply
457 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200458 * \param f_rng RNG function (see notes)
459 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100460 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100461 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100462 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200463 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if m < 0 of m has greater
464 * bit length than N, the number of points in the group.
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100465 *
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200466 * \note In order to prevent simple timing attacks, this function
467 * executes a constant number of operations (that is, point
468 * doubling and addition of distinct points) for random m in
469 * the allowed range.
470 *
471 * \note If f_rng is not NULL, it is used to randomize projective
472 * coordinates of indermediate results, in order to prevent
473 * more elaborate timing attacks relying on intermediate
Manuel Pégourié-Gonnard337b29c2013-09-07 11:52:27 +0200474 * operations. (This is a prophylactic measure since no such
475 * attack has been published yet.) Since this contermeasure
476 * has very low overhead, it is recommended to always provide
477 * a non-NULL f_rng parameter when using secret inputs.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100478 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200479int ecp_mul( ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200480 const mpi *m, const ecp_point *P,
481 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
482
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100483
484/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200485 * \brief Check that a point is a valid public key on this curve
486 *
487 * \param grp Curve/group the point should belong to
488 * \param pt Point to check
489 *
490 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200491 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200492 *
493 * \note This function only checks the point is non-zero, has valid
494 * coordinates and lies on the curve, but not that it is
495 * indeed a multiple of G. This is additional check is more
496 * expensive, isn't required by standards, and shouldn't be
497 * necessary if the group used has a small cofactor. In
498 * particular, it is useless for the NIST groups which all
499 * have a cofactor of 1.
500 *
501 * \note Uses bare components rather than an ecp_keypair structure
502 * in order to ease use with other structures such as
503 * ecdh_context of ecdsa_context.
504 */
505int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
506
507/**
508 * \brief Check that an mpi is a valid private key for this curve
509 *
510 * \param grp Group used
511 * \param d Integer to check
512 *
513 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200514 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200515 *
516 * \note Uses bare components rather than an ecp_keypair structure
517 * in order to ease use with other structures such as
518 * ecdh_context of ecdsa_context.
519 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200520int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200521
522/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100523 * \brief Generate a keypair
524 *
525 * \param grp ECP group
526 * \param d Destination MPI (secret part)
527 * \param Q Destination point (public part)
528 * \param f_rng RNG function
529 * \param p_rng RNG parameter
530 *
531 * \return 0 if successful,
532 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200533 *
534 * \note Uses bare components rather than an ecp_keypair structure
535 * in order to ease use with other structures such as
536 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100537 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200538int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100539 int (*f_rng)(void *, unsigned char *, size_t),
540 void *p_rng );
541
542/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100543 * \brief Checkup routine
544 *
545 * \return 0 if successful, or 1 if the test failed
546 */
547int ecp_self_test( int verbose );
548
549#ifdef __cplusplus
550}
551#endif
552
553#endif