blob: 7a5801171516408b07689b00e6f0ea10c9565524 [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é-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é-Gonnard70380392013-09-16 16:19:53 +020064} ecp_group_id;
65
66/**
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +020067 * Curve information for use by the SSL module
68 */
69typedef struct
70{
71 ecp_group_id grp_id; /*!< Internal identifier */
72 uint16_t name; /*!< TLS NamedCurve value */
73 uint16_t size; /*!< Curve size in bits */
74} ecp_curve_info;
75
76/**
77 * List of supported curves
78 */
79extern ecp_curve_info ecp_supported_curves[];
80
81/**
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010082 * \brief ECP point structure (jacobian coordinates)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +010083 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010084 * \note All functions expect and return points satisfying
85 * the following condition: Z == 0 or Z == 1. (Other
86 * values of Z are used by internal functions only.)
87 * The point is zero, or "at infinity", if Z == 0.
88 * Otherwise, X and Y are its standard (affine) coordinates.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010089 */
90typedef struct
91{
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +010092 mpi X; /*!< the point's X coordinate */
93 mpi Y; /*!< the point's Y coordinate */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +010094 mpi Z; /*!< the point's Z coordinate */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010095}
96ecp_point;
97
98/**
99 * \brief ECP group structure
100 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100101 * The curves we consider are defined by y^2 = x^3 - 3x + B mod P,
102 * and a generator for a large subgroup of order N is fixed.
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100103 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100104 * pbits and nbits must be the size of P and N in bits.
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100105 *
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100106 * If modp is NULL, reduction modulo P is done using a generic
107 * algorithm. Otherwise, it must point to a function that takes an mpi
108 * in the range 0..2^(2*pbits) and transforms it in-place in an integer
109 * of little more than pbits, so that the integer may be efficiently
110 * brought in the 0..P range by a few additions or substractions. It
111 * must return 0 on success and a POLARSSL_ERR_ECP_XXX error on failure.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100112 */
113typedef struct
114{
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100115 ecp_group_id id; /*!< RFC 4492 group ID */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100116 mpi P; /*!< prime modulus of the base field */
117 mpi B; /*!< constant term in the equation */
118 ecp_point G; /*!< generator of the subgroup used */
119 mpi N; /*!< the order of G */
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100120 size_t pbits; /*!< number of bits in P */
121 size_t nbits; /*!< number of bits in N */
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100122 int (*modp)(mpi *); /*!< function for fast reduction mod P */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100123}
124ecp_group;
125
126/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200127 * \brief ECP key pair structure
128 *
129 * A generic key pair that could be used for ECDSA, fixed ECDH, etc.
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200130 *
131 * \note Members purposefully in the same order as struc ecdsa_context.
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200132 */
133typedef struct
134{
135 ecp_group grp; /*!< Elliptic curve and base point */
136 mpi d; /*!< our secret value */
137 ecp_point Q; /*!< our public value */
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200138}
139ecp_keypair;
140
141/**
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200142 * Maximum size of the groups (that is, of N and P)
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100143 */
Manuel Pégourié-Gonnardb694b482013-08-08 13:30:57 +0200144#define POLARSSL_ECP_MAX_BITS 521
145#define POLARSSL_ECP_MAX_BYTES ( ( POLARSSL_ECP_MAX_BITS + 7 ) / 8 )
Manuel Pégourié-Gonnard3837dae2013-09-12 01:39:07 +0200146#define POLARSSL_ECP_MAX_PT_LEN ( 2 * POLARSSL_ECP_MAX_BYTES + 1 )
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100147
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100148/*
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100149 * Maximum window size (actually, NAF width) used for point multipliation.
150 * Default: 7.
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100151 * Minimum value: 2. Maximum value: 8.
152 *
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100153 * Result is an array of at most ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100154 * points used for point multiplication, so at most 64 by default.
155 * In practice, most curves will use less precomputed points.
156 *
157 * Reduction in size may reduce speed for big curves.
158 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100159#define POLARSSL_ECP_WINDOW_SIZE 7 /**< Maximum NAF width used. */
Manuel Pégourié-Gonnard85556072012-11-17 19:54:20 +0100160
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100161/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100162 * Point formats, from RFC 4492's enum ECPointFormat
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100163 */
164#define POLARSSL_ECP_PF_UNCOMPRESSED 0 /**< Uncompressed point format */
165#define POLARSSL_ECP_PF_COMPRESSED 1 /**< Compressed point format */
166
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100167/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100168 * Some other constants from RFC 4492
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100169 */
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100170#define POLARSSL_ECP_TLS_NAMED_CURVE 3 /**< ECCurveType's named_curve */
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100171
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200172#ifdef __cplusplus
173extern "C" {
174#endif
175
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100176/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100177 * \brief Initialize a point (as zero)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100178 */
179void ecp_point_init( ecp_point *pt );
180
181/**
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100182 * \brief Initialize a group (to something meaningless)
183 */
184void ecp_group_init( ecp_group *grp );
185
186/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200187 * \brief Initialize a key pair (as an invalid one)
188 */
189void ecp_keypair_init( ecp_keypair *key );
190
191/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100192 * \brief Free the components of a point
193 */
194void ecp_point_free( ecp_point *pt );
195
196/**
197 * \brief Free the components of an ECP group
198 */
199void ecp_group_free( ecp_group *grp );
200
201/**
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200202 * \brief Free the components of a key pair
203 */
204void ecp_keypair_free( ecp_keypair *key );
205
206/**
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100207 * \brief Set a point to zero
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100208 *
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100209 * \param pt Destination point
210 *
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100211 * \return 0 if successful,
212 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100213 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100214int ecp_set_zero( ecp_point *pt );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100215
216/**
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100217 * \brief Tell if a point is zero
218 *
219 * \param pt Point to test
220 *
221 * \return 1 if point is zero, 0 otherwise
222 */
223int ecp_is_zero( ecp_point *pt );
224
225/**
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100226 * \brief Copy the contents of point Q into P
227 *
228 * \param P Destination point
229 * \param Q Source point
230 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100231 * \return 0 if successful,
232 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100233 */
234int ecp_copy( ecp_point *P, const ecp_point *Q );
235
236/**
Manuel Pégourié-Gonnarde09631b2013-08-12 15:44:31 +0200237 * \brief Copy the contents of a group object
238 *
239 * \param dst Destination group
240 * \param src Source group
241 *
242 * \return 0 if successful,
243 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
244 */
245int ecp_group_copy( ecp_group *dst, const ecp_group *src );
246
247/**
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100248 * \brief Import a non-zero point from two ASCII strings
249 *
250 * \param P Destination point
251 * \param radix Input numeric base
252 * \param x First affine coordinate as a null-terminated string
253 * \param y Second affine coordinate as a null-terminated string
254 *
255 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
256 */
257int ecp_point_read_string( ecp_point *P, int radix,
258 const char *x, const char *y );
259
260/**
261 * \brief Import an ECP group from null-terminated ASCII strings
262 *
263 * \param grp Destination group
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100264 * \param radix Input numeric base
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100265 * \param p Prime modulus of the base field
266 * \param b Constant term in the equation
267 * \param gx The generator's X coordinate
268 * \param gy The generator's Y coordinate
269 * \param n The generator's order
270 *
271 * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code
Manuel Pégourié-Gonnard773ed542012-11-18 13:19:07 +0100272 *
273 * \note Sets all fields except modp.
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100274 */
275int ecp_group_read_string( ecp_group *grp, int radix,
276 const char *p, const char *b,
277 const char *gx, const char *gy, const char *n);
278
279/**
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100280 * \brief Export a point into unsigned binary data
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100281 *
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100282 * \param grp Group to which the point should belong
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100283 * \param P Point to export
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100284 * \param format Point format, should be a POLARSSL_ECP_PF_XXX macro
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100285 * \param olen Length of the actual output
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100286 * \param buf Output buffer
287 * \param buflen Length of the output buffer
288 *
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100289 * \return 0 if successful,
290 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
291 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100292 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100293int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100294 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100295 unsigned char *buf, size_t buflen );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100296
297/**
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100298 * \brief Import a point from unsigned binary data
299 *
300 * \param grp Group to which the point should belong
301 * \param P Point to import
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100302 * \param buf Input buffer
303 * \param ilen Actual length of input
304 *
305 * \return 0 if successful,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200306 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100307 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
308 *
309 * \note This function does NOT check that the point actually
310 * belongs to the given group, see ecp_check_pubkey() for
311 * that.
312 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100313int ecp_point_read_binary( const ecp_group *grp, ecp_point *P,
314 const unsigned char *buf, size_t ilen );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100315
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100316/**
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100317 * \brief Set a group using well-known domain parameters
318 *
319 * \param grp Destination group
320 * \param index Index in the list of well-known domain parameters
321 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100322 * \return O if successful,
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100323 * POLARSSL_ERR_MPI_XXX if initialization failed
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200324 * POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE for unkownn groups
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100325 *
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100326 * \note Index should be a value of RFC 4492's enum NamdeCurve,
327 * possibly in the form of a POLARSSL_ECP_DP_XXX macro.
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100328 */
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200329int ecp_use_known_dp( ecp_group *grp, ecp_group_id index );
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100330
331/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100332 * \brief Set a group from a TLS ECParameters record
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100333 *
334 * \param grp Destination group
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100335 * \param buf &(Start of input buffer)
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100336 * \param len Buffer length
337 *
338 * \return O if successful,
339 * POLARSSL_ERR_MPI_XXX if initialization failed
340 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
341 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100342int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100343
344/**
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100345 * \brief Write the TLS ECParameters record for a group
346 *
347 * \param grp ECP group used
348 * \param olen Number of bytes actually written
349 * \param buf Buffer to write to
350 * \param blen Buffer length
351 *
352 * \return 0 if successful,
353 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
354 */
355int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
356 unsigned char *buf, size_t blen );
357
358/**
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200359 * \brief Get a TLS NamedCurve value from an internal group identifier
360 *
361 * \param grp_id A POLARSSL_ECP_DP_XXX value
362 *
363 * \return The associated TLS NamedCurve value on success,
364 * 0 on failure.
365 */
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200366uint16_t ecp_named_curve_from_grp_id( ecp_group_id id );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200367
368/**
369 * \brief Get an internal group identifier from a TLS NamedCurve value
370 *
371 * \param curve A value from TLS's enum NamedCurve
372 *
373 * \return The associated POLARSSL_ECP_DP_XXX identifer on success,
374 * POLARSSL_ECP_DP_NONE on failure.
375 */
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200376ecp_group_id ecp_grp_id_from_named_curve( uint16_t curve );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200377
378/**
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100379 * \brief Import a point from a TLS ECPoint record
380 *
381 * \param grp ECP group used
382 * \param pt Destination point
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100383 * \param buf $(Start of input buffer)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100384 * \param len Buffer length
385 *
386 * \return O if successful,
387 * POLARSSL_ERR_MPI_XXX if initialization failed
388 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if input is invalid
389 */
390int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100391 const unsigned char **buf, size_t len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100392
393/**
394 * \brief Export a point as a TLS ECPoint record
395 *
396 * \param grp ECP group used
397 * \param pt Point to export
398 * \param format Export format
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200399 * \param olen length of data written
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100400 * \param buf Buffer to write to
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200401 * \param blen Buffer length
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100402 *
403 * \return 0 if successful,
404 * or POLARSSL_ERR_ECP_BAD_INPUT_DATA
405 * or POLARSSL_ERR_ECP_BUFFER_TOO_SMALL
406 */
407int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100408 int format, size_t *olen,
409 unsigned char *buf, size_t blen );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100410
411/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100412 * \brief Addition: R = P + Q
413 *
414 * \param grp ECP group
415 * \param R Destination point
416 * \param P Left-hand point
417 * \param Q Right-hand point
418 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100419 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100420 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100421 */
422int ecp_add( const ecp_group *grp, ecp_point *R,
423 const ecp_point *P, const ecp_point *Q );
424
425/**
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100426 * \brief Subtraction: R = P - Q
427 *
428 * \param grp ECP group
429 * \param R Destination point
430 * \param P Left-hand point
431 * \param Q Right-hand point
432 *
433 * \return 0 if successful,
434 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
435 */
436int ecp_sub( const ecp_group *grp, ecp_point *R,
437 const ecp_point *P, const ecp_point *Q );
438
439/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100440 * \brief Multiplication by an integer: R = m * P
441 *
442 * \param grp ECP group
443 * \param R Destination point
444 * \param m Integer by which to multiply
445 * \param P Point to multiply
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200446 * \param f_rng RNG function (see notes)
447 * \param p_rng RNG parameter
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100448 *
Manuel Pégourié-Gonnard7cfcea32012-11-05 10:06:12 +0100449 * \return 0 if successful,
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100450 * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200451 * POLARSSL_ERR_ECP_BAD_INPUT_DATA if m < 0 of m has greater
452 * bit length than N, the number of points in the group.
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +0100453 *
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200454 * \note In order to prevent simple timing attacks, this function
455 * executes a constant number of operations (that is, point
456 * doubling and addition of distinct points) for random m in
457 * the allowed range.
458 *
459 * \note If f_rng is not NULL, it is used to randomize projective
460 * coordinates of indermediate results, in order to prevent
461 * more elaborate timing attacks relying on intermediate
Manuel Pégourié-Gonnard337b29c2013-09-07 11:52:27 +0200462 * operations. (This is a prophylactic measure since no such
463 * attack has been published yet.) Since this contermeasure
464 * has very low overhead, it is recommended to always provide
465 * a non-NULL f_rng parameter when using secret inputs.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100466 */
Manuel Pégourié-Gonnard5179e462012-10-31 19:37:54 +0100467int ecp_mul( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +0200468 const mpi *m, const ecp_point *P,
469 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
470
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100471
472/**
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200473 * \brief Check that a point is a valid public key on this curve
474 *
475 * \param grp Curve/group the point should belong to
476 * \param pt Point to check
477 *
478 * \return 0 if point is a valid public key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200479 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200480 *
481 * \note This function only checks the point is non-zero, has valid
482 * coordinates and lies on the curve, but not that it is
483 * indeed a multiple of G. This is additional check is more
484 * expensive, isn't required by standards, and shouldn't be
485 * necessary if the group used has a small cofactor. In
486 * particular, it is useless for the NIST groups which all
487 * have a cofactor of 1.
488 *
489 * \note Uses bare components rather than an ecp_keypair structure
490 * in order to ease use with other structures such as
491 * ecdh_context of ecdsa_context.
492 */
493int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt );
494
495/**
496 * \brief Check that an mpi is a valid private key for this curve
497 *
498 * \param grp Group used
499 * \param d Integer to check
500 *
501 * \return 0 if point is a valid private key,
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200502 * POLARSSL_ERR_ECP_INVALID_KEY otherwise.
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200503 *
504 * \note Uses bare components rather than an ecp_keypair structure
505 * in order to ease use with other structures such as
506 * ecdh_context of ecdsa_context.
507 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +0200508int ecp_check_privkey( const ecp_group *grp, const mpi *d );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200509
510/**
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100511 * \brief Generate a keypair
512 *
513 * \param grp ECP group
514 * \param d Destination MPI (secret part)
515 * \param Q Destination point (public part)
516 * \param f_rng RNG function
517 * \param p_rng RNG parameter
518 *
519 * \return 0 if successful,
520 * or a POLARSSL_ERR_ECP_XXX or POLARSSL_MPI_XXX error code
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +0200521 *
522 * \note Uses bare components rather than an ecp_keypair structure
523 * in order to ease use with other structures such as
524 * ecdh_context of ecdsa_context.
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +0100525 */
526int ecp_gen_keypair( const ecp_group *grp, mpi *d, ecp_point *Q,
527 int (*f_rng)(void *, unsigned char *, size_t),
528 void *p_rng );
529
530/**
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100531 * \brief Checkup routine
532 *
533 * \return 0 if successful, or 1 if the test failed
534 */
535int ecp_self_test( int verbose );
536
537#ifdef __cplusplus
538}
539#endif
540
541#endif