blob: e4fce69206262bd135f0a387efd7a56b04c99843 [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é-Gonnardbdc96762013-10-03 11:50:39 +020030#include "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é-Gonnard456d3b92013-09-16 18:04:38 +020037#define POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE -0x4E80 /**< Requested curve not available. */
Manuel Pégourié-Gonnarddb771752013-08-27 15:11:23 +020038#define POLARSSL_ERR_ECP_VERIFY_FAILED -0x4E00 /**< The signature is not valid. */
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +020039#define POLARSSL_ERR_ECP_MALLOC_FAILED -0x4D80 /**< Memory allocation failed. */
40#define POLARSSL_ERR_ECP_RANDOM_FAILED -0x4D00 /**< Generation of random value, such as (ephemeral) key, failed. */
41#define POLARSSL_ERR_ECP_INVALID_KEY -0x4C80 /**< Invalid private or public key. */
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010047/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020048 * Domain parameters (curve, subgroup and generator) identifiers.
49 *
50 * Only curves over prime fields are supported.
51 *
52 * \warning This library does not support validation of arbitrary domain
53 * parameters. Therefore, only well-known domain parameters from trusted
54 * sources should be used. See ecp_use_known_dp().
55 */
56typedef enum
57{
58 POLARSSL_ECP_DP_NONE = 0,
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020059 POLARSSL_ECP_DP_SECP192R1, /*!< 192-bits NIST curve */
60 POLARSSL_ECP_DP_SECP224R1, /*!< 224-bits NIST curve */
61 POLARSSL_ECP_DP_SECP256R1, /*!< 256-bits NIST curve */
62 POLARSSL_ECP_DP_SECP384R1, /*!< 384-bits NIST curve */
63 POLARSSL_ECP_DP_SECP521R1, /*!< 521-bits NIST curve */
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020064 POLARSSL_ECP_DP_BP256R1, /*!< 256-bits Brainpool curve */
65 POLARSSL_ECP_DP_BP384R1, /*!< 384-bits Brainpool curve */
66 POLARSSL_ECP_DP_BP512R1, /*!< 512-bits Brainpool curve */
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010067 POLARSSL_ECP_DP_M221, /*!< (not implemented yet) */
68 POLARSSL_ECP_DP_M255, /*!< Curve25519 */
69 POLARSSL_ECP_DP_M383, /*!< (not implemented yet) */
70 POLARSSL_ECP_DP_M511, /*!< (not implemented yet) */
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +020071} ecp_group_id;
72
73/**
Manuel Pégourié-Gonnard66153662013-12-03 14:12:26 +010074 * Number of supported curves (plus one for NONE).
75 *
76 * (Montgomery curves excluded for now.)
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020077 */
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +020078#define POLARSSL_ECP_DP_MAX 9
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +020079
80/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +020081 * Curve information for use by other modules
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020082 */
83typedef struct
84{
Manuel Pégourié-Gonnard56cd3192013-09-17 17:23:07 +020085 ecp_group_id grp_id; /*!< Internal identifier */
86 uint16_t tls_id; /*!< TLS NamedCurve identifier */
87 uint16_t size; /*!< Curve size in bits */
88 const char *name; /*!< Human-friendly name */
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020089} ecp_curve_info;
90
91/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010092 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010093 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010094 * \note All functions expect and return points satisfying
95 * the following condition: Z == 0 or Z == 1. (Other
96 * values of Z are used by internal functions only.)
97 * The point is zero, or "at infinity", if Z == 0.
98 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010099 */
100typedef struct
101{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100102 mpi X; /*!< the point's X coordinate */
103 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100104 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100105}
106ecp_point;
107
108/**
109 * \brief ECP group structure
110 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200111 * The curves we consider are defined by y^2 = x^3 + A x + B mod P,
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100112 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100113 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100114 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100115 *
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200116 * If modp is NULL, reduction modulo P is done using a generic algorithm.
117 * Otherwise, it must point to a function that takes an mpi in the range
118 * 0..2^(2*pbits)-1 and transforms it in-place in an integer of little more
119 * than pbits, so that the integer may be efficiently brought in the 0..P-1
120 * range by a few additions or substractions. It must return 0 on success and
121 * non-zero on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100122 */
123typedef struct
124{
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200125 ecp_group_id id; /*!< internal group identifier */
126 mpi P; /*!< prime modulus of the base field */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200127 mpi A; /*!< linear term in the equation */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +0200128 mpi B; /*!< constant term in the equation */
129 ecp_point G; /*!< generator of the subgroup used */
130 mpi N; /*!< the order of G */
131 size_t pbits; /*!< number of bits in P */
132 size_t nbits; /*!< number of bits in N */
133 unsigned int h; /*!< cofactor (unused now: assume 1) */
134 int (*modp)(mpi *); /*!< function for fast reduction mod P */
135 int (*t_pre)(ecp_point *, void *); /*!< currently unused */
136 int (*t_post)(ecp_point *, void *); /*!< currently unused */
137 void *t_data; /*!< currently unused */
138 ecp_point *T; /*!< pre-computed points for ecp_mul() */
139 size_t T_size; /*!< number for pre-computed points */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100140}
141ecp_group;
142
143/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200144 * \brief ECP key pair structure
145 *
146 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200147 *
148 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200149 */
150typedef struct
151{
152 ecp_group grp; /*!< Elliptic curve and base point */
153 mpi d; /*!< our secret value */
154 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200155}
156ecp_keypair;
157
158/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200159 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100160 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200161#define POLARSSL_ECP_MAX_BITS 521
162#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200163#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100164
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100165/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100166 * Maximum "window" size used for point multiplication.
167 * Default: 6.
168 * Minimum value: 2. Maximum value: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100169 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100170 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200171 * points used for point multiplication.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100172 *
173 * Reduction in size may reduce speed for big curves.
174 */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +0100175#define POLARSSL_ECP_WINDOW_SIZE 6 /**< Maximum window size used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100176
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100177/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100178 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100179 */
180#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
181#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
182
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100183/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100184 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100185 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100186#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100187
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100188/**
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200189 * \brief Return the list of supported curves with associated info
190 *
191 * \return A statically allocated array, the last entry is 0.
192 */
193const ecp_curve_info *ecp_curve_list( void );
194
195/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200196 * \brief Get curve information from an internal group identifier
197 *
198 * \param grp_id A POLARSSL_ECP_DP_XXX value
199 *
200 * \return The associated curve information or NULL
201 */
202const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id );
203
204/**
205 * \brief Get curve information from a TLS NamedCurve value
206 *
207 * \param grp_id A POLARSSL_ECP_DP_XXX value
208 *
209 * \return The associated curve information or NULL
210 */
211const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id );
212
213/**
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100214 * \brief Get curve information from a human-readable name
215 *
216 * \param name The name
217 *
218 * \return The associated curve information or NULL
219 */
220const ecp_curve_info *ecp_curve_info_from_name( const char *name );
221
222/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100223 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100224 */
225void ecp_point_init( ecp_point *pt );
226
227/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100228 * \brief Initialize a group (to something meaningless)
229 */
230void ecp_group_init( ecp_group *grp );
231
232/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200233 * \brief Initialize a key pair (as an invalid one)
234 */
235void ecp_keypair_init( ecp_keypair *key );
236
237/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100238 * \brief Free the components of a point
239 */
240void ecp_point_free( ecp_point *pt );
241
242/**
243 * \brief Free the components of an ECP group
244 */
245void ecp_group_free( ecp_group *grp );
246
247/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200248 * \brief Free the components of a key pair
249 */
250void ecp_keypair_free( ecp_keypair *key );
251
252/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100253 * \brief Copy the contents of point Q into P
254 *
255 * \param P Destination point
256 * \param Q Source point
257 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100258 * \return 0 if successful,
259 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100260 */
261int ecp_copy( ecp_point *P, const ecp_point *Q );
262
263/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200264 * \brief Copy the contents of a group object
265 *
266 * \param dst Destination group
267 * \param src Source group
268 *
269 * \return 0 if successful,
270 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
271 */
272int ecp_group_copy( ecp_group *dst, const ecp_group *src );
273
274/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200275 * \brief Set a point to zero
276 *
277 * \param pt Destination point
278 *
279 * \return 0 if successful,
280 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
281 */
282int ecp_set_zero( ecp_point *pt );
283
284/**
285 * \brief Tell if a point is zero
286 *
287 * \param pt Point to test
288 *
289 * \return 1 if point is zero, 0 otherwise
290 */
291int ecp_is_zero( ecp_point *pt );
292
293/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100294 * \brief Import a non-zero point from two ASCII strings
295 *
296 * \param P Destination point
297 * \param radix Input numeric base
298 * \param x First affine coordinate as a null-terminated string
299 * \param y Second affine coordinate as a null-terminated string
300 *
301 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
302 */
303int ecp_point_read_string( ecp_point *P, int radix,
304 const char *x, const char *y );
305
306/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100307 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100308 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100309 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100310 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100311 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100312 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100313 * \param buf Output buffer
314 * \param buflen Length of the output buffer
315 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100316 * \return 0 if successful,
317 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
318 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100319 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100320int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100321 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100322 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100323
324/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100325 * \brief Import a point from unsigned binary data
326 *
327 * \param grp Group to which the point should belong
328 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100329 * \param buf Input buffer
330 * \param ilen Actual length of input
331 *
332 * \return 0 if successful,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200333 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100334 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
335 *
336 * \note This function does NOT check that the point actually
337 * belongs to the given group, see ecp_check_pubkey() for
338 * that.
339 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100340int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
341 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100342
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100343/**
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200344 * \brief Import a point from a TLS ECPoint record
345 *
346 * \param grp ECP group used
347 * \param pt Destination point
348 * \param buf $(Start of input buffer)
349 * \param len Buffer length
350 *
351 * \return O if successful,
352 * POLARSSL_ERR_MPI_XXX if initialization failed
353 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
354 */
355int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
356 const unsigned char **buf, size_t len );
357
358/**
359 * \brief Export a point as a TLS ECPoint record
360 *
361 * \param grp ECP group used
362 * \param pt Point to export
363 * \param format Export format
364 * \param olen length of data written
365 * \param buf Buffer to write to
366 * \param blen Buffer length
367 *
368 * \return 0 if successful,
369 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
370 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
371 */
372int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
373 int format, size_t *olen,
374 unsigned char *buf, size_t blen );
375
376/**
377 * \brief Import an ECP group from null-terminated ASCII strings
378 *
379 * \param grp Destination group
380 * \param radix Input numeric base
381 * \param p Prime modulus of the base field
382 * \param b Constant term in the equation
383 * \param gx The generator's X coordinate
384 * \param gy The generator's Y coordinate
385 * \param n The generator's order
386 *
387 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
388 *
389 * \note Sets all fields except modp.
390 */
391int ecp_group_read_string( ecp_group *grp, int radix,
392 const char *p, const char *b,
393 const char *gx, const char *gy, const char *n);
394
395/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100396 * \brief Set a group using well-known domain parameters
397 *
398 * \param grp Destination group
399 * \param index Index in the list of well-known domain parameters
400 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100401 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100402 * POLARSSL_ERR_MPI_XXX if initialization failed
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200403 * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100404 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100405 * \note Index should be a value of RFC 4492's enum NamdeCurve,
406 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100407 */
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200408int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100409
410/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100411 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100412 *
413 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100414 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100415 * \param len Buffer length
416 *
417 * \return O if successful,
418 * POLARSSL_ERR_MPI_XXX if initialization failed
419 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
420 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100421int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100422
423/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100424 * \brief Write the TLS ECParameters record for a group
425 *
426 * \param grp ECP group used
427 * \param olen Number of bytes actually written
428 * \param buf Buffer to write to
429 * \param blen Buffer length
430 *
431 * \return 0 if successful,
432 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
433 */
434int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
435 unsigned char *buf, size_t blen );
436
437/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100438 * \brief Addition: R = P + Q
439 *
440 * \param grp ECP group
441 * \param R Destination point
442 * \param P Left-hand point
443 * \param Q Right-hand point
444 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100445 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100446 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100447 *
448 * \note This function does not support Montgomery curves, such as
449 * Curve25519.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100450 */
451int ecp_add( const ecp_group *grp, ecp_point *R,
452 const ecp_point *P, const ecp_point *Q );
453
454/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100455 * \brief Subtraction: R = P - Q
456 *
457 * \param grp ECP group
458 * \param R Destination point
459 * \param P Left-hand point
460 * \param Q Right-hand point
461 *
462 * \return 0 if successful,
463 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100464 *
465 * \note This function does not support Montgomery curves, such as
466 * Curve25519.
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100467 */
468int ecp_sub( const ecp_group *grp, ecp_point *R,
469 const ecp_point *P, const ecp_point *Q );
470
471/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100472 * \brief Multiplication by an integer: R = m * P
Paul Bakker6838bd12013-09-30 13:56:38 +0200473 * (Not thread-safe to use same group in multiple threads)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100474 *
475 * \param grp ECP group
476 * \param R Destination point
477 * \param m Integer by which to multiply
478 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200479 * \param f_rng RNG function (see notes)
480 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100481 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100482 * \return 0 if successful,
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100483 * POLARSSL_ERR_ECP_INVALID_KEY if m is not a valid privkey
484 * or P is not a valid pubkey,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100485 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100486 *
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100487 * \note In order to prevent timing attacks, this function
488 * executes the exact same sequence of (base field)
489 * operations for any valid m. It avoids any if-branch or
490 * array index depending on the value of m.
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200491 *
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +0100492 * \note If f_rng is not NULL, it is used to randomize intermediate
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100493 * results in order to prevent potential timing attacks
494 * targetting these results. It is recommended to always
495 * provide a non-NULL f_rng (the overhead is negligible).
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100496 */
Manuel Pégourié-Gonnard09ceaf42013-11-20 23:06:14 +0100497int ecp_mul( ecp_group *grp, ecp_point *R,
498 const mpi *m, const ecp_point *P,
499 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100500
501/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200502 * \brief Check that a point is a valid public key on this curve
503 *
504 * \param grp Curve/group the point should belong to
505 * \param pt Point to check
506 *
507 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200508 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200509 *
510 * \note This function only checks the point is non-zero, has valid
511 * coordinates and lies on the curve, but not that it is
512 * indeed a multiple of G. This is additional check is more
513 * expensive, isn't required by standards, and shouldn't be
514 * necessary if the group used has a small cofactor. In
515 * particular, it is useless for the NIST groups which all
516 * have a cofactor of 1.
517 *
518 * \note Uses bare components rather than an ecp_keypair structure
519 * in order to ease use with other structures such as
520 * ecdh_context of ecdsa_context.
521 */
522int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
523
524/**
525 * \brief Check that an mpi is a valid private key for this curve
526 *
527 * \param grp Group used
528 * \param d Integer to check
529 *
530 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200531 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200532 *
533 * \note Uses bare components rather than an ecp_keypair structure
534 * in order to ease use with other structures such as
535 * ecdh_context of ecdsa_context.
536 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200537int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200538
539/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100540 * \brief Generate a keypair
541 *
542 * \param grp ECP group
543 * \param d Destination MPI (secret part)
544 * \param Q Destination point (public part)
545 * \param f_rng RNG function
546 * \param p_rng RNG parameter
547 *
548 * \return 0 if successful,
549 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200550 *
551 * \note Uses bare components rather than an ecp_keypair structure
552 * in order to ease use with other structures such as
553 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100554 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200555int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100556 int (*f_rng)(void *, unsigned char *, size_t),
557 void *p_rng );
558
559/**
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +0100560 * \brief Generate a keypair
561 *
562 * \param grp_id ECP group identifier
563 * \param key Destination keypair
564 * \param f_rng RNG function
565 * \param p_rng RNG parameter
566 *
567 * \return 0 if successful,
568 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
569 */
570int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
571 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
572
573/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100574 * \brief Checkup routine
575 *
576 * \return 0 if successful, or 1 if the test failed
577 */
578int ecp_self_test( int verbose );
579
580#ifdef __cplusplus
581}
582#endif
583
584#endif