blob: b8b19978b0d5a2ef194bb1c1aff39dfb39d03fa6 [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. */
36#define POLARSSL_ERR_ECP_GENERIC -0x4F00 /**< Generic ECP error */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010037
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010038/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010039 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010040 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010041 * \note All functions expect and return points satisfying
42 * the following condition: Z == 0 or Z == 1. (Other
43 * values of Z are used by internal functions only.)
44 * The point is zero, or "at infinity", if Z == 0.
45 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010046 */
47typedef struct
48{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010049 mpi X; /*!< the point's X coordinate */
50 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010051 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010052}
53ecp_point;
54
55/**
56 * \brief ECP group structure
57 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010058 * The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
59 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010060 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010061 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010062 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010063 * If modp is NULL, reduction modulo P is done using a generic
64 * algorithm. Otherwise, it must point to a function that takes an mpi
65 * in the range 0..2^(2*pbits) and transforms it in-place in an integer
66 * of little more than pbits, so that the integer may be efficiently
67 * brought in the 0..P range by a few additions or substractions. It
68 * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010069 */
70typedef struct
71{
72 mpi P; /*!< prime modulus of the base field */
73 mpi B; /*!< constant term in the equation */
74 ecp_point G; /*!< generator of the subgroup used */
75 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010076 size_t pbits; /*!< number of bits in P */
77 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010078 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010079}
80ecp_group;
81
82/**
83 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
84 *
85 * These also are the NIST-recommended ECP groups, are the random ECP groups
86 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010087 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010088 *
89 * \warning This library does not support validation of arbitrary domain
90 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010091 * sources should be used. See ecp_use_known_dp().
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010092 */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010093#define POLARSSL_ECP_DP_SECP192R1 0
94#define POLARSSL_ECP_DP_SECP224R1 1
95#define POLARSSL_ECP_DP_SECP256R1 2
96#define POLARSSL_ECP_DP_SECP384R1 3
97#define POLARSSL_ECP_DP_SECP521R1 4
98
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +010099/**
100 * Maximum bit size of the groups (that is, of N)
101 */
102#define POLARSSL_ECP_MAX_N_BITS 521
103
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100104/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100105 * Maximum window size (actually, NAF width) used for point multipliation.
106 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100107 * Minimum value: 2. Maximum value: 8.
108 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100109 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100110 * points used for point multiplication, so at most 64 by default.
111 * In practice, most curves will use less precomputed points.
112 *
113 * Reduction in size may reduce speed for big curves.
114 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100115#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100116
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100117/*
118 * Point formats
119 */
120#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
121#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
122
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100123#ifdef __cplusplus
124extern "C" {
125#endif
126
127/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100128 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100129 */
130void ecp_point_init( ecp_point *pt );
131
132/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100133 * \brief Initialize a group (to something meaningless)
134 */
135void ecp_group_init( ecp_group *grp );
136
137/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100138 * \brief Free the components of a point
139 */
140void ecp_point_free( ecp_point *pt );
141
142/**
143 * \brief Free the components of an ECP group
144 */
145void ecp_group_free( ecp_group *grp );
146
147/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100148 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100149 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100150 * \param pt Destination point
151 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100152 * \return 0 if successful,
153 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100154 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100155int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100156
157/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100158 * \brief Tell if a point is zero
159 *
160 * \param pt Point to test
161 *
162 * \return 1 if point is zero, 0 otherwise
163 */
164int ecp_is_zero( ecp_point *pt );
165
166/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100167 * \brief Copy the contents of point Q into P
168 *
169 * \param P Destination point
170 * \param Q Source point
171 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100172 * \return 0 if successful,
173 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100174 */
175int ecp_copy( ecp_point *P, const ecp_point *Q );
176
177/**
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100178 * \brief Check that a point is a valid public key on this curve
179 *
180 * \param grp Curve/group the point should belong to
181 * \param pt Point to check
182 *
183 * \return 0 if point is a valid public key,
184 * POLARSSL_ERR_ECP_GENERIC otherwise.
185 *
186 * \note This function only checks the point is non-zero, has valid
187 * coordinates and lies on the curve, but not that it is
188 * indeed a multiple of G. This is additional check is more
189 * expensive, isn't required by standards, and shouldn't be
190 * necessary if the group used has a small cofactor. In
191 * particular, it is useless for the NIST groups which all
192 * have a cofactor of 1.
193 */
194int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
195
196/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100197 * \brief Import a non-zero point from two ASCII strings
198 *
199 * \param P Destination point
200 * \param radix Input numeric base
201 * \param x First affine coordinate as a null-terminated string
202 * \param y Second affine coordinate as a null-terminated string
203 *
204 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
205 */
206int ecp_point_read_string( ecp_point *P, int radix,
207 const char *x, const char *y );
208
209/**
210 * \brief Import an ECP group from null-terminated ASCII strings
211 *
212 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100213 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100214 * \param p Prime modulus of the base field
215 * \param b Constant term in the equation
216 * \param gx The generator's X coordinate
217 * \param gy The generator's Y coordinate
218 * \param n The generator's order
219 *
220 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100221 *
222 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100223 */
224int ecp_group_read_string( ecp_group *grp, int radix,
225 const char *p, const char *b,
226 const char *gx, const char *gy, const char *n);
227
228/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100229 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100230 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100231 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100232 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100233 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100234 * \param olen Length of the actual ouput
235 * \param buf Output buffer
236 * \param buflen Length of the output buffer
237 *
238 * \return 0 if successful, or POLARSSL_ERR_ECP_GENERIC
239 */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100240int ecp_write_binary( const ecp_group *grp, const ecp_point *P, int format,
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100241 size_t *olen, unsigned char *buf, size_t buflen );
242
243/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100244 * \brief Import a point from unsigned binary data
245 *
246 * \param grp Group to which the point should belong
247 * \param P Point to import
248 * \param format Point format, must be POLARSSL_ECP_PF_UNCOMPRESSED for now
249 * \param buf Input buffer
250 * \param ilen Actual length of input
251 *
252 * \return 0 if successful,
253 * POLARSSL_ERR_ECP_GENERIC if input is invalid
254 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
255 *
256 * \note This function does NOT check that the point actually
257 * belongs to the given group, see ecp_check_pubkey() for
258 * that.
259 */
260int ecp_read_binary( const ecp_group *grp, ecp_point *P, int format,
261 const unsigned char *buf, size_t ilen );
262/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100263 * \brief Set a group using well-known domain parameters
264 *
265 * \param grp Destination group
266 * \param index Index in the list of well-known domain parameters
267 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100268 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100269 * POLARSSL_ERR_MPI_XXX if initialization failed
270 * POLARSSL_ERR_ECP_GENERIC if index is out of range
271 *
272 * \note Index should be a POLARSSL_ECP_DP_XXX macro.
273 */
274int ecp_use_known_dp( ecp_group *grp, size_t index );
275
276/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100277 * \brief Addition: R = P + Q
278 *
279 * \param grp ECP group
280 * \param R Destination point
281 * \param P Left-hand point
282 * \param Q Right-hand point
283 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100284 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100285 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100286 */
287int ecp_add( const ecp_group *grp, ecp_point *R,
288 const ecp_point *P, const ecp_point *Q );
289
290/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100291 * \brief Subtraction: R = P - Q
292 *
293 * \param grp ECP group
294 * \param R Destination point
295 * \param P Left-hand point
296 * \param Q Right-hand point
297 *
298 * \return 0 if successful,
299 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
300 */
301int ecp_sub( const ecp_group *grp, ecp_point *R,
302 const ecp_point *P, const ecp_point *Q );
303
304/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100305 * \brief Multiplication by an integer: R = m * P
306 *
307 * \param grp ECP group
308 * \param R Destination point
309 * \param m Integer by which to multiply
310 * \param P Point to multiply
311 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100312 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100313 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100314 * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit
315 * length than N, the number of points in the group.
316 *
317 * \note This function executes a constant number of operations
318 * for random m in the allowed range.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100319 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100320int ecp_mul( const ecp_group *grp, ecp_point *R,
321 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100322
323/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100324 * \brief Generate a keypair
325 *
326 * \param grp ECP group
327 * \param d Destination MPI (secret part)
328 * \param Q Destination point (public part)
329 * \param f_rng RNG function
330 * \param p_rng RNG parameter
331 *
332 * \return 0 if successful,
333 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
334 */
335int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
336 int (*f_rng)(void *, unsigned char *, size_t),
337 void *p_rng );
338
339/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100340 * \brief Checkup routine
341 *
342 * \return 0 if successful, or 1 if the test failed
343 */
344int ecp_self_test( int verbose );
345
346#ifdef __cplusplus
347}
348#endif
349
350#endif