blob: 922d5da6e5100e2acb4220d014332a307ca329c2 [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é-Gonnard70380392013-09-16 16:19:53 +020046 * Domain parameters (curve, subgroup and generator) identifiers.
47 *
48 * Only curves over prime fields are supported.
49 *
50 * \warning This library does not support validation of arbitrary domain
51 * parameters. Therefore, only well-known domain parameters from trusted
52 * sources should be used. See ecp_use_known_dp().
53 */
54typedef enum
55{
56 POLARSSL_ECP_DP_NONE = 0,
57 POLARSSL_ECP_DP_SECP192R1, /* 192-bits NIST curve */
58 POLARSSL_ECP_DP_SECP224R1, /* 224-bits NIST curve */
59 POLARSSL_ECP_DP_SECP256R1, /* 256-bits NIST curve */
60 POLARSSL_ECP_DP_SECP384R1, /* 384-bits NIST curve */
61 POLARSSL_ECP_DP_SECP521R1, /* 521-bits NIST curve */
62} ecp_group_id;
63
64/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010065 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010066 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010067 * \note All functions expect and return points satisfying
68 * the following condition: Z == 0 or Z == 1. (Other
69 * values of Z are used by internal functions only.)
70 * The point is zero, or "at infinity", if Z == 0.
71 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010072 */
73typedef struct
74{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010075 mpi X; /*!< the point's X coordinate */
76 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010077 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010078}
79ecp_point;
80
81/**
82 * \brief ECP group structure
83 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010084 * The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
85 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010086 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010087 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +010088 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +010089 * If modp is NULL, reduction modulo P is done using a generic
90 * algorithm. Otherwise, it must point to a function that takes an mpi
91 * in the range 0..2^(2*pbits) and transforms it in-place in an integer
92 * of little more than pbits, so that the integer may be efficiently
93 * brought in the 0..P range by a few additions or substractions. It
94 * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010095 */
96typedef struct
97{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +010098 ecp_group_id id; /*!< RFC 4492 group ID */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010099 mpi P; /*!< prime modulus of the base field */
100 mpi B; /*!< constant term in the equation */
101 ecp_point G; /*!< generator of the subgroup used */
102 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100103 size_t pbits; /*!< number of bits in P */
104 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100105 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100106}
107ecp_group;
108
109/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200110 * \brief ECP key pair structure
111 *
112 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200113 *
114 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200115 */
116typedef struct
117{
118 ecp_group grp; /*!< Elliptic curve and base point */
119 mpi d; /*!< our secret value */
120 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200121}
122ecp_keypair;
123
124/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200125 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100126 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200127#define POLARSSL_ECP_MAX_BITS 521
128#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200129#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100130
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100131/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100132 * Maximum window size (actually, NAF width) used for point multipliation.
133 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100134 * Minimum value: 2. Maximum value: 8.
135 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100136 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100137 * points used for point multiplication, so at most 64 by default.
138 * In practice, most curves will use less precomputed points.
139 *
140 * Reduction in size may reduce speed for big curves.
141 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100142#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100143
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100144/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100145 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100146 */
147#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
148#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
149
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100150/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100151 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100152 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100153#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100154
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200155#ifdef __cplusplus
156extern "C" {
157#endif
158
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100159/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100160 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100161 */
162void ecp_point_init( ecp_point *pt );
163
164/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100165 * \brief Initialize a group (to something meaningless)
166 */
167void ecp_group_init( ecp_group *grp );
168
169/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200170 * \brief Initialize a key pair (as an invalid one)
171 */
172void ecp_keypair_init( ecp_keypair *key );
173
174/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100175 * \brief Free the components of a point
176 */
177void ecp_point_free( ecp_point *pt );
178
179/**
180 * \brief Free the components of an ECP group
181 */
182void ecp_group_free( ecp_group *grp );
183
184/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200185 * \brief Free the components of a key pair
186 */
187void ecp_keypair_free( ecp_keypair *key );
188
189/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100190 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100191 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100192 * \param pt Destination point
193 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100194 * \return 0 if successful,
195 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100196 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100197int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100198
199/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100200 * \brief Tell if a point is zero
201 *
202 * \param pt Point to test
203 *
204 * \return 1 if point is zero, 0 otherwise
205 */
206int ecp_is_zero( ecp_point *pt );
207
208/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100209 * \brief Copy the contents of point Q into P
210 *
211 * \param P Destination point
212 * \param Q Source point
213 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100214 * \return 0 if successful,
215 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100216 */
217int ecp_copy( ecp_point *P, const ecp_point *Q );
218
219/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200220 * \brief Copy the contents of a group object
221 *
222 * \param dst Destination group
223 * \param src Source group
224 *
225 * \return 0 if successful,
226 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
227 */
228int ecp_group_copy( ecp_group *dst, const ecp_group *src );
229
230/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100231 * \brief Import a non-zero point from two ASCII strings
232 *
233 * \param P Destination point
234 * \param radix Input numeric base
235 * \param x First affine coordinate as a null-terminated string
236 * \param y Second affine coordinate as a null-terminated string
237 *
238 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
239 */
240int ecp_point_read_string( ecp_point *P, int radix,
241 const char *x, const char *y );
242
243/**
244 * \brief Import an ECP group from null-terminated ASCII strings
245 *
246 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100247 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100248 * \param p Prime modulus of the base field
249 * \param b Constant term in the equation
250 * \param gx The generator's X coordinate
251 * \param gy The generator's Y coordinate
252 * \param n The generator's order
253 *
254 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100255 *
256 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100257 */
258int ecp_group_read_string( ecp_group *grp, int radix,
259 const char *p, const char *b,
260 const char *gx, const char *gy, const char *n);
261
262/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100263 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100264 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100265 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100266 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100267 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100268 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100269 * \param buf Output buffer
270 * \param buflen Length of the output buffer
271 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100272 * \return 0 if successful,
273 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
274 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100275 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100276int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100277 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100278 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100279
280/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100281 * \brief Import a point from unsigned binary data
282 *
283 * \param grp Group to which the point should belong
284 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100285 * \param buf Input buffer
286 * \param ilen Actual length of input
287 *
288 * \return 0 if successful,
289 * POLARSSL_ERR_ECP_GENERIC if input is invalid
290 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
291 *
292 * \note This function does NOT check that the point actually
293 * belongs to the given group, see ecp_check_pubkey() for
294 * that.
295 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100296int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
297 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100298
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100299/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100300 * \brief Set a group using well-known domain parameters
301 *
302 * \param grp Destination group
303 * \param index Index in the list of well-known domain parameters
304 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100305 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100306 * POLARSSL_ERR_MPI_XXX if initialization failed
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200307 * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100308 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100309 * \note Index should be a value of RFC 4492's enum NamdeCurve,
310 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100311 */
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200312int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100313
314/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100315 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100316 *
317 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100318 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100319 * \param len Buffer length
320 *
321 * \return O if successful,
322 * POLARSSL_ERR_MPI_XXX if initialization failed
323 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
324 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100325int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100326
327/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100328 * \brief Write the TLS ECParameters record for a group
329 *
330 * \param grp ECP group used
331 * \param olen Number of bytes actually written
332 * \param buf Buffer to write to
333 * \param blen Buffer length
334 *
335 * \return 0 if successful,
336 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
337 */
338int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
339 unsigned char *buf, size_t blen );
340
341/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200342 * \brief Get a TLS NamedCurve value from an internal group identifier
343 *
344 * \param grp_id A POLARSSL_ECP_DP_XXX value
345 *
346 * \return The associated TLS NamedCurve value on success,
347 * 0 on failure.
348 */
349unsigned int ecp_named_curve_from_grp_id( ecp_group_id id );
350
351/**
352 * \brief Get an internal group identifier from a TLS NamedCurve value
353 *
354 * \param curve A value from TLS's enum NamedCurve
355 *
356 * \return The associated POLARSSL_ECP_DP_XXX identifer on success,
357 * POLARSSL_ECP_DP_NONE on failure.
358 */
359ecp_group_id ecp_grp_id_from_named_curve( unsigned int curve );
360
361/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100362 * \brief Import a point from a TLS ECPoint record
363 *
364 * \param grp ECP group used
365 * \param pt Destination point
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100366 * \param buf $(Start of input buffer)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100367 * \param len Buffer length
368 *
369 * \return O if successful,
370 * POLARSSL_ERR_MPI_XXX if initialization failed
371 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
372 */
373int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100374 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100375
376/**
377 * \brief Export a point as a TLS ECPoint record
378 *
379 * \param grp ECP group used
380 * \param pt Point to export
381 * \param format Export format
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200382 * \param olen length of data written
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100383 * \param buf Buffer to write to
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200384 * \param blen Buffer length
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100385 *
386 * \return 0 if successful,
387 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
388 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
389 */
390int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100391 int format, size_t *olen,
392 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100393
394/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100395 * \brief Addition: 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 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100402 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100403 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100404 */
405int ecp_add( const ecp_group *grp, ecp_point *R,
406 const ecp_point *P, const ecp_point *Q );
407
408/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100409 * \brief Subtraction: R = P - Q
410 *
411 * \param grp ECP group
412 * \param R Destination point
413 * \param P Left-hand point
414 * \param Q Right-hand point
415 *
416 * \return 0 if successful,
417 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
418 */
419int ecp_sub( const ecp_group *grp, ecp_point *R,
420 const ecp_point *P, const ecp_point *Q );
421
422/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100423 * \brief Multiplication by an integer: R = m * P
424 *
425 * \param grp ECP group
426 * \param R Destination point
427 * \param m Integer by which to multiply
428 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200429 * \param f_rng RNG function (see notes)
430 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100431 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100432 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100433 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200434 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if m < 0 of m has greater
435 * bit length than N, the number of points in the group.
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100436 *
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200437 * \note In order to prevent simple timing attacks, this function
438 * executes a constant number of operations (that is, point
439 * doubling and addition of distinct points) for random m in
440 * the allowed range.
441 *
442 * \note If f_rng is not NULL, it is used to randomize projective
443 * coordinates of indermediate results, in order to prevent
444 * more elaborate timing attacks relying on intermediate
Manuel Pégourié-Gonnard337b29c2013-09-07 11:52:27 +0200445 * operations. (This is a prophylactic measure since no such
446 * attack has been published yet.) Since this contermeasure
447 * has very low overhead, it is recommended to always provide
448 * a non-NULL f_rng parameter when using secret inputs.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100449 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100450int ecp_mul( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200451 const mpi *m, const ecp_point *P,
452 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
453
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100454
455/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200456 * \brief Check that a point is a valid public key on this curve
457 *
458 * \param grp Curve/group the point should belong to
459 * \param pt Point to check
460 *
461 * \return 0 if point is a valid public key,
462 * POLARSSL_ERR_ECP_GENERIC otherwise.
463 *
464 * \note This function only checks the point is non-zero, has valid
465 * coordinates and lies on the curve, but not that it is
466 * indeed a multiple of G. This is additional check is more
467 * expensive, isn't required by standards, and shouldn't be
468 * necessary if the group used has a small cofactor. In
469 * particular, it is useless for the NIST groups which all
470 * have a cofactor of 1.
471 *
472 * \note Uses bare components rather than an ecp_keypair structure
473 * in order to ease use with other structures such as
474 * ecdh_context of ecdsa_context.
475 */
476int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
477
478/**
479 * \brief Check that an mpi is a valid private key for this curve
480 *
481 * \param grp Group used
482 * \param d Integer to check
483 *
484 * \return 0 if point is a valid private key,
485 * POLARSSL_ERR_ECP_GENERIC otherwise.
486 *
487 * \note Uses bare components rather than an ecp_keypair structure
488 * in order to ease use with other structures such as
489 * ecdh_context of ecdsa_context.
490 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200491int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200492
493/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100494 * \brief Generate a keypair
495 *
496 * \param grp ECP group
497 * \param d Destination MPI (secret part)
498 * \param Q Destination point (public part)
499 * \param f_rng RNG function
500 * \param p_rng RNG parameter
501 *
502 * \return 0 if successful,
503 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200504 *
505 * \note Uses bare components rather than an ecp_keypair structure
506 * in order to ease use with other structures such as
507 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100508 */
509int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
510 int (*f_rng)(void *, unsigned char *, size_t),
511 void *p_rng );
512
513/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100514 * \brief Checkup routine
515 *
516 * \return 0 if successful, or 1 if the test failed
517 */
518int ecp_self_test( int verbose );
519
520#ifdef __cplusplus
521}
522#endif
523
524#endif