blob: 36c6185468bbcbe91a6ea8b1ba62cfa4ec109d95 [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. */
Paul Bakkerfd3eac52013-06-29 23:31:33 +020036#define POLARSSL_ERR_ECP_BUFFER_TOO_SMALL -0x4F00 /**< The buffer is too small to write to. */
37#define POLARSSL_ERR_ECP_GENERIC -0x4E80 /**< Generic ECP error */
38#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E00 /**< Requested curve not available. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010039
Paul Bakker407a0da2013-06-27 14:29:21 +020040#ifdef __cplusplus
41extern "C" {
42#endif
43
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010044/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010045 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010046 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010047 * \note All functions expect and return points satisfying
48 * the following condition: Z == 0 or Z == 1. (Other
49 * values of Z are used by internal functions only.)
50 * The point is zero, or "at infinity", if Z == 0.
51 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010052 */
53typedef struct
54{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010055 mpi X; /*!< the point's X coordinate */
56 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010057 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010058}
59ecp_point;
60
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +010061/*
62 * RFC 4492 defines an enum NamedCurve with two-bytes values
63 */
64typedef uint16_t ecp_group_id;
65
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010066/**
67 * \brief ECP group structure
68 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010069 * The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
70 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010071 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010072 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010073 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010074 * If modp is NULL, reduction modulo P is done using a generic
75 * algorithm. Otherwise, it must point to a function that takes an mpi
76 * in the range 0..2^(2*pbits) and transforms it in-place in an integer
77 * of little more than pbits, so that the integer may be efficiently
78 * brought in the 0..P range by a few additions or substractions. It
79 * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010080 */
81typedef struct
82{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +010083 ecp_group_id id; /*!< RFC 4492 group ID */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010084 mpi P; /*!< prime modulus of the base field */
85 mpi B; /*!< constant term in the equation */
86 ecp_point G; /*!< generator of the subgroup used */
87 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010088 size_t pbits; /*!< number of bits in P */
89 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010090 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010091}
92ecp_group;
93
94/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +020095 * \brief ECP key pair structure
96 *
97 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +020098 *
99 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200100 */
101typedef struct
102{
103 ecp_group grp; /*!< Elliptic curve and base point */
104 mpi d; /*!< our secret value */
105 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200106}
107ecp_keypair;
108
109/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100110 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
111 *
112 * These also are the NIST-recommended ECP groups, are the random ECP groups
113 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +0100114 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100115 *
116 * \warning This library does not support validation of arbitrary domain
117 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100118 * sources should be used. See ecp_use_known_dp().
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100119 *
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200120 * \note The values are taken from RFC 4492's enum NamedCurve,
121 * except NONE which is used to denote uninitialized groups.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100122 */
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200123#define POLARSSL_ECP_DP_NONE 0
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100124#define POLARSSL_ECP_DP_SECP192R1 19
125#define POLARSSL_ECP_DP_SECP224R1 21
126#define POLARSSL_ECP_DP_SECP256R1 23
127#define POLARSSL_ECP_DP_SECP384R1 24
128#define POLARSSL_ECP_DP_SECP521R1 25
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100129
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100130/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200131 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100132 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200133#define POLARSSL_ECP_MAX_BITS 521
134#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100135
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100136/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100137 * Maximum window size (actually, NAF width) used for point multipliation.
138 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100139 * Minimum value: 2. Maximum value: 8.
140 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100141 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100142 * points used for point multiplication, so at most 64 by default.
143 * In practice, most curves will use less precomputed points.
144 *
145 * Reduction in size may reduce speed for big curves.
146 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100147#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100148
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100149/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100150 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100151 */
152#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
153#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
154
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100155/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100156 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100157 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100158#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100159
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200160#ifdef __cplusplus
161extern "C" {
162#endif
163
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100164/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100165 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100166 */
167void ecp_point_init( ecp_point *pt );
168
169/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100170 * \brief Initialize a group (to something meaningless)
171 */
172void ecp_group_init( ecp_group *grp );
173
174/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200175 * \brief Initialize a key pair (as an invalid one)
176 */
177void ecp_keypair_init( ecp_keypair *key );
178
179/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100180 * \brief Free the components of a point
181 */
182void ecp_point_free( ecp_point *pt );
183
184/**
185 * \brief Free the components of an ECP group
186 */
187void ecp_group_free( ecp_group *grp );
188
189/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200190 * \brief Free the components of a key pair
191 */
192void ecp_keypair_free( ecp_keypair *key );
193
194/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100195 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100196 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100197 * \param pt Destination point
198 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100199 * \return 0 if successful,
200 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100201 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100202int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100203
204/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100205 * \brief Tell if a point is zero
206 *
207 * \param pt Point to test
208 *
209 * \return 1 if point is zero, 0 otherwise
210 */
211int ecp_is_zero( ecp_point *pt );
212
213/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100214 * \brief Copy the contents of point Q into P
215 *
216 * \param P Destination point
217 * \param Q Source point
218 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100219 * \return 0 if successful,
220 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100221 */
222int ecp_copy( ecp_point *P, const ecp_point *Q );
223
224/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200225 * \brief Copy the contents of a group object
226 *
227 * \param dst Destination group
228 * \param src Source group
229 *
230 * \return 0 if successful,
231 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
232 */
233int ecp_group_copy( ecp_group *dst, const ecp_group *src );
234
235/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100236 * \brief Import a non-zero point from two ASCII strings
237 *
238 * \param P Destination point
239 * \param radix Input numeric base
240 * \param x First affine coordinate as a null-terminated string
241 * \param y Second affine coordinate as a null-terminated string
242 *
243 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
244 */
245int ecp_point_read_string( ecp_point *P, int radix,
246 const char *x, const char *y );
247
248/**
249 * \brief Import an ECP group from null-terminated ASCII strings
250 *
251 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100252 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100253 * \param p Prime modulus of the base field
254 * \param b Constant term in the equation
255 * \param gx The generator's X coordinate
256 * \param gy The generator's Y coordinate
257 * \param n The generator's order
258 *
259 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100260 *
261 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100262 */
263int ecp_group_read_string( ecp_group *grp, int radix,
264 const char *p, const char *b,
265 const char *gx, const char *gy, const char *n);
266
267/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100268 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100269 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100270 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100271 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100272 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100273 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100274 * \param buf Output buffer
275 * \param buflen Length of the output buffer
276 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100277 * \return 0 if successful,
278 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
279 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100280 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100281int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100282 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100283 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100284
285/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100286 * \brief Import a point from unsigned binary data
287 *
288 * \param grp Group to which the point should belong
289 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100290 * \param buf Input buffer
291 * \param ilen Actual length of input
292 *
293 * \return 0 if successful,
294 * POLARSSL_ERR_ECP_GENERIC if input is invalid
295 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
296 *
297 * \note This function does NOT check that the point actually
298 * belongs to the given group, see ecp_check_pubkey() for
299 * that.
300 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100301int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
302 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100303
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100304/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100305 * \brief Set a group using well-known domain parameters
306 *
307 * \param grp Destination group
308 * \param index Index in the list of well-known domain parameters
309 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100310 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100311 * POLARSSL_ERR_MPI_XXX if initialization failed
312 * POLARSSL_ERR_ECP_GENERIC if index is out of range
313 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100314 * \note Index should be a value of RFC 4492's enum NamdeCurve,
315 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100316 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100317int ecp_use_known_dp( ecp_group *grp, ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100318
319/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100320 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100321 *
322 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100323 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100324 * \param len Buffer length
325 *
326 * \return O if successful,
327 * POLARSSL_ERR_MPI_XXX if initialization failed
328 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
329 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100330int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100331
332/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100333 * \brief Write the TLS ECParameters record for a group
334 *
335 * \param grp ECP group used
336 * \param olen Number of bytes actually written
337 * \param buf Buffer to write to
338 * \param blen Buffer length
339 *
340 * \return 0 if successful,
341 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
342 */
343int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
344 unsigned char *buf, size_t blen );
345
346/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100347 * \brief Import a point from a TLS ECPoint record
348 *
349 * \param grp ECP group used
350 * \param pt Destination point
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100351 * \param buf $(Start of input buffer)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100352 * \param len Buffer length
353 *
354 * \return O if successful,
355 * POLARSSL_ERR_MPI_XXX if initialization failed
356 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
357 */
358int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100359 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100360
361/**
362 * \brief Export a point as a TLS ECPoint record
363 *
364 * \param grp ECP group used
365 * \param pt Point to export
366 * \param format Export format
367 * \param buf Buffer to write to
368 * \param len Buffer length
369 *
370 * \return 0 if successful,
371 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
372 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
373 */
374int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100375 int format, size_t *olen,
376 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100377
378/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100379 * \brief Addition: R = P + Q
380 *
381 * \param grp ECP group
382 * \param R Destination point
383 * \param P Left-hand point
384 * \param Q Right-hand point
385 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100386 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100387 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100388 */
389int ecp_add( const ecp_group *grp, ecp_point *R,
390 const ecp_point *P, const ecp_point *Q );
391
392/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100393 * \brief Subtraction: R = P - Q
394 *
395 * \param grp ECP group
396 * \param R Destination point
397 * \param P Left-hand point
398 * \param Q Right-hand point
399 *
400 * \return 0 if successful,
401 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
402 */
403int ecp_sub( const ecp_group *grp, ecp_point *R,
404 const ecp_point *P, const ecp_point *Q );
405
406/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100407 * \brief Multiplication by an integer: R = m * P
408 *
409 * \param grp ECP group
410 * \param R Destination point
411 * \param m Integer by which to multiply
412 * \param P Point to multiply
413 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100414 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100415 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100416 * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit
417 * length than N, the number of points in the group.
418 *
419 * \note This function executes a constant number of operations
420 * for random m in the allowed range.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100421 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100422int ecp_mul( const ecp_group *grp, ecp_point *R,
423 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100424
425/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200426 * \brief Check that a point is a valid public key on this curve
427 *
428 * \param grp Curve/group the point should belong to
429 * \param pt Point to check
430 *
431 * \return 0 if point is a valid public key,
432 * POLARSSL_ERR_ECP_GENERIC otherwise.
433 *
434 * \note This function only checks the point is non-zero, has valid
435 * coordinates and lies on the curve, but not that it is
436 * indeed a multiple of G. This is additional check is more
437 * expensive, isn't required by standards, and shouldn't be
438 * necessary if the group used has a small cofactor. In
439 * particular, it is useless for the NIST groups which all
440 * have a cofactor of 1.
441 *
442 * \note Uses bare components rather than an ecp_keypair structure
443 * in order to ease use with other structures such as
444 * ecdh_context of ecdsa_context.
445 */
446int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
447
448/**
449 * \brief Check that an mpi is a valid private key for this curve
450 *
451 * \param grp Group used
452 * \param d Integer to check
453 *
454 * \return 0 if point is a valid private key,
455 * POLARSSL_ERR_ECP_GENERIC otherwise.
456 *
457 * \note Uses bare components rather than an ecp_keypair structure
458 * in order to ease use with other structures such as
459 * ecdh_context of ecdsa_context.
460 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200461int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200462
463/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100464 * \brief Generate a keypair
465 *
466 * \param grp ECP group
467 * \param d Destination MPI (secret part)
468 * \param Q Destination point (public part)
469 * \param f_rng RNG function
470 * \param p_rng RNG parameter
471 *
472 * \return 0 if successful,
473 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200474 *
475 * \note Uses bare components rather than an ecp_keypair structure
476 * in order to ease use with other structures such as
477 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100478 */
479int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
480 int (*f_rng)(void *, unsigned char *, size_t),
481 void *p_rng );
482
483/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100484 * \brief Checkup routine
485 *
486 * \return 0 if successful, or 1 if the test failed
487 */
488int ecp_self_test( int verbose );
489
490#ifdef __cplusplus
491}
492#endif
493
494#endif