blob: c7689c1ab6a68d0522027e573e36b8353341e79a [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. */
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +020037#define POLARSSL_ERR_ECP_GENERIC -0x4E80 /**< Generic ECP error. */
Paul Bakkerfd3eac52013-06-29 23:31:33 +020038#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E00 /**< Requested curve not available. */
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +020039#define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010040
Paul Bakker407a0da2013-06-27 14:29:21 +020041#ifdef __cplusplus
42extern "C" {
43#endif
44
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010046 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010047 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010048 * \note All functions expect and return points satisfying
49 * the following condition: Z == 0 or Z == 1. (Other
50 * values of Z are used by internal functions only.)
51 * The point is zero, or "at infinity", if Z == 0.
52 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010053 */
54typedef struct
55{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010056 mpi X; /*!< the point's X coordinate */
57 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010058 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010059}
60ecp_point;
61
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +010062/*
63 * RFC 4492 defines an enum NamedCurve with two-bytes values
64 */
65typedef uint16_t ecp_group_id;
66
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010067/**
68 * \brief ECP group structure
69 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010070 * The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
71 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010072 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010073 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010074 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010075 * If modp is NULL, reduction modulo P is done using a generic
76 * algorithm. Otherwise, it must point to a function that takes an mpi
77 * in the range 0..2^(2*pbits) and transforms it in-place in an integer
78 * of little more than pbits, so that the integer may be efficiently
79 * brought in the 0..P range by a few additions or substractions. It
80 * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010081 */
82typedef struct
83{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +010084 ecp_group_id id; /*!< RFC 4492 group ID */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010085 mpi P; /*!< prime modulus of the base field */
86 mpi B; /*!< constant term in the equation */
87 ecp_point G; /*!< generator of the subgroup used */
88 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010089 size_t pbits; /*!< number of bits in P */
90 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010091 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010092}
93ecp_group;
94
95/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +020096 * \brief ECP key pair structure
97 *
98 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +020099 *
100 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200101 */
102typedef struct
103{
104 ecp_group grp; /*!< Elliptic curve and base point */
105 mpi d; /*!< our secret value */
106 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200107}
108ecp_keypair;
109
110/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100111 * RFC 5114 defines a number of standardized ECP groups for use with TLS.
112 *
113 * These also are the NIST-recommended ECP groups, are the random ECP groups
114 * recommended by SECG, and include the two groups used by NSA Suite B.
Manuel Pégourié-Gonnardd7e45702012-10-31 18:57:05 +0100115 * There are known as secpLLLr1 with LLL = 192, 224, 256, 384, 521.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100116 *
117 * \warning This library does not support validation of arbitrary domain
118 * parameters. Therefore, only well-known domain parameters from trusted
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100119 * sources should be used. See ecp_use_known_dp().
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100120 *
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200121 * \note The values are taken from RFC 4492's enum NamedCurve,
122 * except NONE which is used to denote uninitialized groups.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100123 */
Manuel Pégourié-Gonnard1c808a02013-07-11 13:17:43 +0200124#define POLARSSL_ECP_DP_NONE 0
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100125#define POLARSSL_ECP_DP_SECP192R1 19
126#define POLARSSL_ECP_DP_SECP224R1 21
127#define POLARSSL_ECP_DP_SECP256R1 23
128#define POLARSSL_ECP_DP_SECP384R1 24
129#define POLARSSL_ECP_DP_SECP521R1 25
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100130
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100131/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200132 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100133 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200134#define POLARSSL_ECP_MAX_BITS 521
135#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200136#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100137
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100138/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100139 * Maximum window size (actually, NAF width) used for point multipliation.
140 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100141 * Minimum value: 2. Maximum value: 8.
142 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100143 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100144 * points used for point multiplication, so at most 64 by default.
145 * In practice, most curves will use less precomputed points.
146 *
147 * Reduction in size may reduce speed for big curves.
148 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100149#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100150
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100151/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100152 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100153 */
154#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
155#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
156
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100157/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100158 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100159 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100160#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100161
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200162#ifdef __cplusplus
163extern "C" {
164#endif
165
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100166/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100167 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100168 */
169void ecp_point_init( ecp_point *pt );
170
171/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100172 * \brief Initialize a group (to something meaningless)
173 */
174void ecp_group_init( ecp_group *grp );
175
176/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200177 * \brief Initialize a key pair (as an invalid one)
178 */
179void ecp_keypair_init( ecp_keypair *key );
180
181/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100182 * \brief Free the components of a point
183 */
184void ecp_point_free( ecp_point *pt );
185
186/**
187 * \brief Free the components of an ECP group
188 */
189void ecp_group_free( ecp_group *grp );
190
191/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200192 * \brief Free the components of a key pair
193 */
194void ecp_keypair_free( ecp_keypair *key );
195
196/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100197 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100198 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100199 * \param pt Destination point
200 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100201 * \return 0 if successful,
202 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100203 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100204int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100205
206/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100207 * \brief Tell if a point is zero
208 *
209 * \param pt Point to test
210 *
211 * \return 1 if point is zero, 0 otherwise
212 */
213int ecp_is_zero( ecp_point *pt );
214
215/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100216 * \brief Copy the contents of point Q into P
217 *
218 * \param P Destination point
219 * \param Q Source point
220 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100221 * \return 0 if successful,
222 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100223 */
224int ecp_copy( ecp_point *P, const ecp_point *Q );
225
226/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200227 * \brief Copy the contents of a group object
228 *
229 * \param dst Destination group
230 * \param src Source group
231 *
232 * \return 0 if successful,
233 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
234 */
235int ecp_group_copy( ecp_group *dst, const ecp_group *src );
236
237/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100238 * \brief Import a non-zero point from two ASCII strings
239 *
240 * \param P Destination point
241 * \param radix Input numeric base
242 * \param x First affine coordinate as a null-terminated string
243 * \param y Second affine coordinate as a null-terminated string
244 *
245 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
246 */
247int ecp_point_read_string( ecp_point *P, int radix,
248 const char *x, const char *y );
249
250/**
251 * \brief Import an ECP group from null-terminated ASCII strings
252 *
253 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100254 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100255 * \param p Prime modulus of the base field
256 * \param b Constant term in the equation
257 * \param gx The generator's X coordinate
258 * \param gy The generator's Y coordinate
259 * \param n The generator's order
260 *
261 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100262 *
263 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100264 */
265int ecp_group_read_string( ecp_group *grp, int radix,
266 const char *p, const char *b,
267 const char *gx, const char *gy, const char *n);
268
269/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100270 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100271 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100272 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100273 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100274 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100275 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100276 * \param buf Output buffer
277 * \param buflen Length of the output buffer
278 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100279 * \return 0 if successful,
280 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
281 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100282 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100283int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100284 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100285 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100286
287/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100288 * \brief Import a point from unsigned binary data
289 *
290 * \param grp Group to which the point should belong
291 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100292 * \param buf Input buffer
293 * \param ilen Actual length of input
294 *
295 * \return 0 if successful,
296 * POLARSSL_ERR_ECP_GENERIC if input is invalid
297 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
298 *
299 * \note This function does NOT check that the point actually
300 * belongs to the given group, see ecp_check_pubkey() for
301 * that.
302 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100303int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
304 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100305
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100306/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100307 * \brief Set a group using well-known domain parameters
308 *
309 * \param grp Destination group
310 * \param index Index in the list of well-known domain parameters
311 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100312 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100313 * POLARSSL_ERR_MPI_XXX if initialization failed
314 * POLARSSL_ERR_ECP_GENERIC if index is out of range
315 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100316 * \note Index should be a value of RFC 4492's enum NamdeCurve,
317 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100318 */
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100319int ecp_use_known_dp( ecp_group *grp, ecp_group_id id );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100320
321/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100322 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100323 *
324 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100325 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100326 * \param len Buffer length
327 *
328 * \return O if successful,
329 * POLARSSL_ERR_MPI_XXX if initialization failed
330 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
331 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100332int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100333
334/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100335 * \brief Write the TLS ECParameters record for a group
336 *
337 * \param grp ECP group used
338 * \param olen Number of bytes actually written
339 * \param buf Buffer to write to
340 * \param blen Buffer length
341 *
342 * \return 0 if successful,
343 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
344 */
345int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
346 unsigned char *buf, size_t blen );
347
348/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100349 * \brief Import a point from a TLS ECPoint record
350 *
351 * \param grp ECP group used
352 * \param pt Destination point
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100353 * \param buf $(Start of input buffer)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100354 * \param len Buffer length
355 *
356 * \return O if successful,
357 * POLARSSL_ERR_MPI_XXX if initialization failed
358 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
359 */
360int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100361 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100362
363/**
364 * \brief Export a point as a TLS ECPoint record
365 *
366 * \param grp ECP group used
367 * \param pt Point to export
368 * \param format Export format
369 * \param buf Buffer to write to
370 * \param len Buffer length
371 *
372 * \return 0 if successful,
373 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
374 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
375 */
376int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100377 int format, size_t *olen,
378 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100379
380/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100381 * \brief Addition: R = P + Q
382 *
383 * \param grp ECP group
384 * \param R Destination point
385 * \param P Left-hand point
386 * \param Q Right-hand point
387 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100388 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100389 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100390 */
391int ecp_add( const ecp_group *grp, ecp_point *R,
392 const ecp_point *P, const ecp_point *Q );
393
394/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100395 * \brief Subtraction: R = P - Q
396 *
397 * \param grp ECP group
398 * \param R Destination point
399 * \param P Left-hand point
400 * \param Q Right-hand point
401 *
402 * \return 0 if successful,
403 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
404 */
405int ecp_sub( const ecp_group *grp, ecp_point *R,
406 const ecp_point *P, const ecp_point *Q );
407
408/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100409 * \brief Multiplication by an integer: R = m * P
410 *
411 * \param grp ECP group
412 * \param R Destination point
413 * \param m Integer by which to multiply
414 * \param P Point to multiply
415 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100416 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100417 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100418 * POLARSSL_ERR_ECP_GENERIC if m < 0 of m has greater bit
419 * length than N, the number of points in the group.
420 *
421 * \note This function executes a constant number of operations
422 * for random m in the allowed range.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100423 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100424int ecp_mul( const ecp_group *grp, ecp_point *R,
425 const mpi *m, const ecp_point *P );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100426
427/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200428 * \brief Check that a point is a valid public key on this curve
429 *
430 * \param grp Curve/group the point should belong to
431 * \param pt Point to check
432 *
433 * \return 0 if point is a valid public key,
434 * POLARSSL_ERR_ECP_GENERIC otherwise.
435 *
436 * \note This function only checks the point is non-zero, has valid
437 * coordinates and lies on the curve, but not that it is
438 * indeed a multiple of G. This is additional check is more
439 * expensive, isn't required by standards, and shouldn't be
440 * necessary if the group used has a small cofactor. In
441 * particular, it is useless for the NIST groups which all
442 * have a cofactor of 1.
443 *
444 * \note Uses bare components rather than an ecp_keypair structure
445 * in order to ease use with other structures such as
446 * ecdh_context of ecdsa_context.
447 */
448int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
449
450/**
451 * \brief Check that an mpi is a valid private key for this curve
452 *
453 * \param grp Group used
454 * \param d Integer to check
455 *
456 * \return 0 if point is a valid private key,
457 * POLARSSL_ERR_ECP_GENERIC otherwise.
458 *
459 * \note Uses bare components rather than an ecp_keypair structure
460 * in order to ease use with other structures such as
461 * ecdh_context of ecdsa_context.
462 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200463int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200464
465/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100466 * \brief Generate a keypair
467 *
468 * \param grp ECP group
469 * \param d Destination MPI (secret part)
470 * \param Q Destination point (public part)
471 * \param f_rng RNG function
472 * \param p_rng RNG parameter
473 *
474 * \return 0 if successful,
475 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200476 *
477 * \note Uses bare components rather than an ecp_keypair structure
478 * in order to ease use with other structures such as
479 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100480 */
481int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
482 int (*f_rng)(void *, unsigned char *, size_t),
483 void *p_rng );
484
485/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100486 * \brief Checkup routine
487 *
488 * \return 0 if successful, or 1 if the test failed
489 */
490int ecp_self_test( int verbose );
491
492#ifdef __cplusplus
493}
494#endif
495
496#endif