blob: 6c52f9622fcb2c89722e28def1b3e3d6f6ebbe08 [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
30#include "bignum.h"
31
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 *
150 * \return 0 if successful,
151 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100152 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100153int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100154
155/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100156 * \brief Copy the contents of point Q into P
157 *
158 * \param P Destination point
159 * \param Q Source point
160 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100161 * \return 0 if successful,
162 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100163 */
164int ecp_copy( ecp_point *P, const ecp_point *Q );
165
166/**
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100167 * \brief Check that a point is a valid public key on this curve
168 *
169 * \param grp Curve/group the point should belong to
170 * \param pt Point to check
171 *
172 * \return 0 if point is a valid public key,
173 * POLARSSL_ERR_ECP_GENERIC otherwise.
174 *
175 * \note This function only checks the point is non-zero, has valid
176 * coordinates and lies on the curve, but not that it is
177 * indeed a multiple of G. This is additional check is more
178 * expensive, isn't required by standards, and shouldn't be
179 * necessary if the group used has a small cofactor. In
180 * particular, it is useless for the NIST groups which all
181 * have a cofactor of 1.
182 */
183int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
184
185/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100186 * \brief Import a non-zero point from two ASCII strings
187 *
188 * \param P Destination point
189 * \param radix Input numeric base
190 * \param x First affine coordinate as a null-terminated string
191 * \param y Second affine coordinate as a null-terminated string
192 *
193 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
194 */
195int ecp_point_read_string( ecp_point *P, int radix,
196 const char *x, const char *y );
197
198/**
199 * \brief Import an ECP group from null-terminated ASCII strings
200 *
201 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100202 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100203 * \param p Prime modulus of the base field
204 * \param b Constant term in the equation
205 * \param gx The generator's X coordinate
206 * \param gy The generator's Y coordinate
207 * \param n The generator's order
208 *
209 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100210 *
211 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100212 */
213int ecp_group_read_string( ecp_group *grp, int radix,
214 const char *p, const char *b,
215 const char *gx, const char *gy, const char *n);
216
217/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100218 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100219 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100220 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100221 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100222 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100223 * \param olen Length of the actual ouput
224 * \param buf Output buffer
225 * \param buflen Length of the output buffer
226 *
227 * \return 0 if successful, or POLARSSL_ERR_ECP_GENERIC
228 */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100229int ecp_write_binary( const ecp_group *grp, const ecp_point *P, int format,
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100230 size_t *olen, unsigned char *buf, size_t buflen );
231
232/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100233 * \brief Import a point from unsigned binary data
234 *
235 * \param grp Group to which the point should belong
236 * \param P Point to import
237 * \param format Point format, must be POLARSSL_ECP_PF_UNCOMPRESSED for now
238 * \param buf Input buffer
239 * \param ilen Actual length of input
240 *
241 * \return 0 if successful,
242 * POLARSSL_ERR_ECP_GENERIC if input is invalid
243 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
244 *
245 * \note This function does NOT check that the point actually
246 * belongs to the given group, see ecp_check_pubkey() for
247 * that.
248 */
249int ecp_read_binary( const ecp_group *grp, ecp_point *P, int format,
250 const unsigned char *buf, size_t ilen );
251/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100252 * \brief Set a group using well-known domain parameters
253 *
254 * \param grp Destination group
255 * \param index Index in the list of well-known domain parameters
256 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100257 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100258 * POLARSSL_ERR_MPI_XXX if initialization failed
259 * POLARSSL_ERR_ECP_GENERIC if index is out of range
260 *
261 * \note Index should be a POLARSSL_ECP_DP_XXX macro.
262 */
263int ecp_use_known_dp( ecp_group *grp, size_t index );
264
265/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100266 * \brief Addition: R = P + Q
267 *
268 * \param grp ECP group
269 * \param R Destination point
270 * \param P Left-hand point
271 * \param Q Right-hand point
272 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100273 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100274 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100275 */
276int ecp_add( const ecp_group *grp, ecp_point *R,
277 const ecp_point *P, const ecp_point *Q );
278
279/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100280 * \brief Subtraction: R = P - Q
281 *
282 * \param grp ECP group
283 * \param R Destination point
284 * \param P Left-hand point
285 * \param Q Right-hand point
286 *
287 * \return 0 if successful,
288 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
289 */
290int ecp_sub( const ecp_group *grp, ecp_point *R,
291 const ecp_point *P, const ecp_point *Q );
292
293/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100294 * \brief Multiplication by an integer: R = m * P
295 *
296 * \param grp ECP group
297 * \param R Destination point
298 * \param m Integer by which to multiply
299 * \param P Point to multiply
300 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100301 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100302 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100303 * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit
304 * length than N, the number of points in the group.
305 *
306 * \note This function executes a constant number of operations
307 * for random m in the allowed range.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100308 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100309int ecp_mul( const ecp_group *grp, ecp_point *R,
310 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100311
312/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100313 * \brief Generate a keypair
314 *
315 * \param grp ECP group
316 * \param d Destination MPI (secret part)
317 * \param Q Destination point (public part)
318 * \param f_rng RNG function
319 * \param p_rng RNG parameter
320 *
321 * \return 0 if successful,
322 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
323 */
324int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
325 int (*f_rng)(void *, unsigned char *, size_t),
326 void *p_rng );
327
328/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100329 * \brief Checkup routine
330 *
331 * \return 0 if successful, or 1 if the test failed
332 */
333int ecp_self_test( int verbose );
334
335#ifdef __cplusplus
336}
337#endif
338
339#endif