blob: 6c146363738f25d31f279a7666ce83f90cc9d922 [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é-Gonnard1a967282013-02-09 17:03:58 +010092 *
93 * \note The values are taken from RFC 4492's enum NamedCurve.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010094 */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010095#define POLARSSL_ECP_DP_SECP192R1 19
96#define POLARSSL_ECP_DP_SECP224R1 21
97#define POLARSSL_ECP_DP_SECP256R1 23
98#define POLARSSL_ECP_DP_SECP384R1 24
99#define POLARSSL_ECP_DP_SECP521R1 25
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100100
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100101/**
102 * Maximum bit size of the groups (that is, of N)
103 */
104#define POLARSSL_ECP_MAX_N_BITS 521
105
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100106/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100107 * Maximum window size (actually, NAF width) used for point multipliation.
108 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100109 * Minimum value: 2. Maximum value: 8.
110 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100111 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100112 * points used for point multiplication, so at most 64 by default.
113 * In practice, most curves will use less precomputed points.
114 *
115 * Reduction in size may reduce speed for big curves.
116 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100117#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100118
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100119/*
120 * Point formats
121 */
122#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
123#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
124
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100125/*
126 * Some constants from RFC 4492 (ECC for TLS)
127 */
128#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType named_curve */
129
130
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100131#ifdef __cplusplus
132extern "C" {
133#endif
134
135/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100136 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100137 */
138void ecp_point_init( ecp_point *pt );
139
140/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100141 * \brief Initialize a group (to something meaningless)
142 */
143void ecp_group_init( ecp_group *grp );
144
145/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100146 * \brief Free the components of a point
147 */
148void ecp_point_free( ecp_point *pt );
149
150/**
151 * \brief Free the components of an ECP group
152 */
153void ecp_group_free( ecp_group *grp );
154
155/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100156 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100157 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100158 * \param pt Destination point
159 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100160 * \return 0 if successful,
161 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100162 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100163int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100164
165/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100166 * \brief Tell if a point is zero
167 *
168 * \param pt Point to test
169 *
170 * \return 1 if point is zero, 0 otherwise
171 */
172int ecp_is_zero( ecp_point *pt );
173
174/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100175 * \brief Copy the contents of point Q into P
176 *
177 * \param P Destination point
178 * \param Q Source point
179 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100180 * \return 0 if successful,
181 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100182 */
183int ecp_copy( ecp_point *P, const ecp_point *Q );
184
185/**
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100186 * \brief Check that a point is a valid public key on this curve
187 *
188 * \param grp Curve/group the point should belong to
189 * \param pt Point to check
190 *
191 * \return 0 if point is a valid public key,
192 * POLARSSL_ERR_ECP_GENERIC otherwise.
193 *
194 * \note This function only checks the point is non-zero, has valid
195 * coordinates and lies on the curve, but not that it is
196 * indeed a multiple of G. This is additional check is more
197 * expensive, isn't required by standards, and shouldn't be
198 * necessary if the group used has a small cofactor. In
199 * particular, it is useless for the NIST groups which all
200 * have a cofactor of 1.
201 */
202int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
203
204/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100205 * \brief Import a non-zero point from two ASCII strings
206 *
207 * \param P Destination point
208 * \param radix Input numeric base
209 * \param x First affine coordinate as a null-terminated string
210 * \param y Second affine coordinate as a null-terminated string
211 *
212 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
213 */
214int ecp_point_read_string( ecp_point *P, int radix,
215 const char *x, const char *y );
216
217/**
218 * \brief Import an ECP group from null-terminated ASCII strings
219 *
220 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100221 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100222 * \param p Prime modulus of the base field
223 * \param b Constant term in the equation
224 * \param gx The generator's X coordinate
225 * \param gy The generator's Y coordinate
226 * \param n The generator's order
227 *
228 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100229 *
230 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100231 */
232int ecp_group_read_string( ecp_group *grp, int radix,
233 const char *p, const char *b,
234 const char *gx, const char *gy, const char *n);
235
236/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100237 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100238 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100239 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100240 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100241 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100242 * \param olen Length of the actual ouput
243 * \param buf Output buffer
244 * \param buflen Length of the output buffer
245 *
246 * \return 0 if successful, or POLARSSL_ERR_ECP_GENERIC
247 */
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100248int ecp_write_binary( const ecp_group *grp, const ecp_point *P, int format,
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100249 size_t *olen, unsigned char *buf, size_t buflen );
250
251/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100252 * \brief Import a point from unsigned binary data
253 *
254 * \param grp Group to which the point should belong
255 * \param P Point to import
256 * \param format Point format, must be POLARSSL_ECP_PF_UNCOMPRESSED for now
257 * \param buf Input buffer
258 * \param ilen Actual length of input
259 *
260 * \return 0 if successful,
261 * POLARSSL_ERR_ECP_GENERIC if input is invalid
262 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
263 *
264 * \note This function does NOT check that the point actually
265 * belongs to the given group, see ecp_check_pubkey() for
266 * that.
267 */
268int ecp_read_binary( const ecp_group *grp, ecp_point *P, int format,
269 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100270
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100271/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100272 * \brief Set a group using well-known domain parameters
273 *
274 * \param grp Destination group
275 * \param index Index in the list of well-known domain parameters
276 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100277 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100278 * POLARSSL_ERR_MPI_XXX if initialization failed
279 * POLARSSL_ERR_ECP_GENERIC if index is out of range
280 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100281 * \note Index should be a value of RFC 4492's enum NamdeCurve,
282 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100283 */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100284int ecp_use_known_dp( ecp_group *grp, uint16_t index );
285
286/**
287 * \brief Read a group from an ECParameters record
288 *
289 * \param grp Destination group
290 * \param buf Start of input buffer
291 * \param len Buffer length
292 *
293 * \return O if successful,
294 * POLARSSL_ERR_MPI_XXX if initialization failed
295 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
296 */
297int ecp_tls_read_group( ecp_group *grp, const unsigned char *buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100298
299/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100300 * \brief Addition: R = P + Q
301 *
302 * \param grp ECP group
303 * \param R Destination point
304 * \param P Left-hand point
305 * \param Q Right-hand point
306 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100307 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100308 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100309 */
310int ecp_add( const ecp_group *grp, ecp_point *R,
311 const ecp_point *P, const ecp_point *Q );
312
313/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100314 * \brief Subtraction: R = P - Q
315 *
316 * \param grp ECP group
317 * \param R Destination point
318 * \param P Left-hand point
319 * \param Q Right-hand point
320 *
321 * \return 0 if successful,
322 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
323 */
324int ecp_sub( const ecp_group *grp, ecp_point *R,
325 const ecp_point *P, const ecp_point *Q );
326
327/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100328 * \brief Multiplication by an integer: R = m * P
329 *
330 * \param grp ECP group
331 * \param R Destination point
332 * \param m Integer by which to multiply
333 * \param P Point to multiply
334 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100335 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100336 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100337 * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit
338 * length than N, the number of points in the group.
339 *
340 * \note This function executes a constant number of operations
341 * for random m in the allowed range.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100342 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100343int ecp_mul( const ecp_group *grp, ecp_point *R,
344 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100345
346/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100347 * \brief Generate a keypair
348 *
349 * \param grp ECP group
350 * \param d Destination MPI (secret part)
351 * \param Q Destination point (public part)
352 * \param f_rng RNG function
353 * \param p_rng RNG parameter
354 *
355 * \return 0 if successful,
356 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
357 */
358int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
359 int (*f_rng)(void *, unsigned char *, size_t),
360 void *p_rng );
361
362/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100363 * \brief Checkup routine
364 *
365 * \return 0 if successful, or 1 if the test failed
366 */
367int ecp_self_test( int verbose );
368
369#ifdef __cplusplus
370}
371#endif
372
373#endif