blob: 96c135b56b3db89e66a5ad8e8e30b84a79992353 [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 *
6 * Copyright (C) 2012, Brainspark B.V.
7 *
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
34 *
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010035 * (Only one error code available...)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010036 */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010037#define POLARSSL_ERR_ECP_GENERIC -0x007E /**< Generic ECP error */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010038
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010039/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010040 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010041 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010042 * \note All functions expect and return points satisfying
43 * the following condition: Z == 0 or Z == 1. (Other
44 * values of Z are used by internal functions only.)
45 * The point is zero, or "at infinity", if Z == 0.
46 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010047 */
48typedef struct
49{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010050 mpi X; /*!< the point's X coordinate */
51 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010052 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010053}
54ecp_point;
55
56/**
57 * \brief ECP group structure
58 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010059 * The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
60 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010061 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010062 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010063 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010064 * If modp is NULL, reduction modulo P is done using a generic
65 * algorithm. Otherwise, it must point to a function that takes an mpi
66 * in the range 0..2^(2*pbits) and transforms it in-place in an integer
67 * of little more than pbits, so that the integer may be efficiently
68 * brought in the 0..P range by a few additions or substractions. It
69 * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010070 */
71typedef struct
72{
73 mpi P; /*!< prime modulus of the base field */
74 mpi B; /*!< constant term in the equation */
75 ecp_point G; /*!< generator of the subgroup used */
76 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010077 size_t pbits; /*!< number of bits in P */
78 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010079 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010080}
81ecp_group;
82
83/**
84 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
85 *
86 * These also are the NIST-recommended ECP groups, are the random ECP groups
87 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +010088 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010089 *
90 * \warning This library does not support validation of arbitrary domain
91 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010092 * sources should be used. See ecp_use_known_dp().
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010093 */
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +010094#define POLARSSL_ECP_DP_SECP192R1 0
95#define POLARSSL_ECP_DP_SECP224R1 1
96#define POLARSSL_ECP_DP_SECP256R1 2
97#define POLARSSL_ECP_DP_SECP384R1 3
98#define POLARSSL_ECP_DP_SECP521R1 4
99
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100100/**
101 * Maximum bit size of the groups (that is, of N)
102 */
103#define POLARSSL_ECP_MAX_N_BITS 521
104
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100105/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100106 * Maximum window size (actually, NAF width) used for point multipliation.
107 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100108 * Minimum value: 2. Maximum value: 8.
109 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100110 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100111 * points used for point multiplication, so at most 64 by default.
112 * In practice, most curves will use less precomputed points.
113 *
114 * Reduction in size may reduce speed for big curves.
115 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100116#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100117
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100118#ifdef __cplusplus
119extern "C" {
120#endif
121
122/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100123 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100124 */
125void ecp_point_init( ecp_point *pt );
126
127/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100128 * \brief Initialize a group (to something meaningless)
129 */
130void ecp_group_init( ecp_group *grp );
131
132/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100133 * \brief Free the components of a point
134 */
135void ecp_point_free( ecp_point *pt );
136
137/**
138 * \brief Free the components of an ECP group
139 */
140void ecp_group_free( ecp_group *grp );
141
142/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100143 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100144 *
145 * \return 0 if successful,
146 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100147 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100148int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100149
150/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100151 * \brief Copy the contents of point Q into P
152 *
153 * \param P Destination point
154 * \param Q Source point
155 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100156 * \return 0 if successful,
157 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100158 */
159int ecp_copy( ecp_point *P, const ecp_point *Q );
160
161/**
Manuel Pégourié-Gonnard1c330572012-11-24 12:05:44 +0100162 * \brief Check that a point is a valid public key on this curve
163 *
164 * \param grp Curve/group the point should belong to
165 * \param pt Point to check
166 *
167 * \return 0 if point is a valid public key,
168 * POLARSSL_ERR_ECP_GENERIC otherwise.
169 *
170 * \note This function only checks the point is non-zero, has valid
171 * coordinates and lies on the curve, but not that it is
172 * indeed a multiple of G. This is additional check is more
173 * expensive, isn't required by standards, and shouldn't be
174 * necessary if the group used has a small cofactor. In
175 * particular, it is useless for the NIST groups which all
176 * have a cofactor of 1.
177 */
178int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
179
180/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100181 * \brief Import a non-zero point from two ASCII strings
182 *
183 * \param P Destination point
184 * \param radix Input numeric base
185 * \param x First affine coordinate as a null-terminated string
186 * \param y Second affine coordinate as a null-terminated string
187 *
188 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
189 */
190int ecp_point_read_string( ecp_point *P, int radix,
191 const char *x, const char *y );
192
193/**
194 * \brief Import an ECP group from null-terminated ASCII strings
195 *
196 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100197 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100198 * \param p Prime modulus of the base field
199 * \param b Constant term in the equation
200 * \param gx The generator's X coordinate
201 * \param gy The generator's Y coordinate
202 * \param n The generator's order
203 *
204 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100205 *
206 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100207 */
208int ecp_group_read_string( ecp_group *grp, int radix,
209 const char *p, const char *b,
210 const char *gx, const char *gy, const char *n);
211
212/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100213 * \brief Set a group using well-known domain parameters
214 *
215 * \param grp Destination group
216 * \param index Index in the list of well-known domain parameters
217 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100218 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100219 * POLARSSL_ERR_MPI_XXX if initialization failed
220 * POLARSSL_ERR_ECP_GENERIC if index is out of range
221 *
222 * \note Index should be a POLARSSL_ECP_DP_XXX macro.
223 */
224int ecp_use_known_dp( ecp_group *grp, size_t index );
225
226/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100227 * \brief Addition: R = P + Q
228 *
229 * \param grp ECP group
230 * \param R Destination point
231 * \param P Left-hand point
232 * \param Q Right-hand point
233 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100234 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100235 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100236 */
237int ecp_add( const ecp_group *grp, ecp_point *R,
238 const ecp_point *P, const ecp_point *Q );
239
240/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100241 * \brief Subtraction: R = P - Q
242 *
243 * \param grp ECP group
244 * \param R Destination point
245 * \param P Left-hand point
246 * \param Q Right-hand point
247 *
248 * \return 0 if successful,
249 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
250 */
251int ecp_sub( const ecp_group *grp, ecp_point *R,
252 const ecp_point *P, const ecp_point *Q );
253
254/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100255 * \brief Multiplication by an integer: R = m * P
256 *
257 * \param grp ECP group
258 * \param R Destination point
259 * \param m Integer by which to multiply
260 * \param P Point to multiply
261 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100262 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100263 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100264 * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit
265 * length than N, the number of points in the group.
266 *
267 * \note This function executes a constant number of operations
268 * for random m in the allowed range.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100269 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100270int ecp_mul( const ecp_group *grp, ecp_point *R,
271 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100272
273/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100274 * \brief Checkup routine
275 *
276 * \return 0 if successful, or 1 if the test failed
277 */
278int ecp_self_test( int verbose );
279
280#ifdef __cplusplus
281}
282#endif
283
284#endif