blob: 4823804faac8b40ba431ac053318612703b7a87f [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 *
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010034 * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
35 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020036 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
37 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
38 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
39 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010040 *
41 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
42 * render ECC resistant against Side Channel Attacks. IACR Cryptology
43 * ePrint Archive, 2004, vol. 2004, p. 342.
44 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045 */
46
47#include "polarssl/config.h"
48
49#if defined(POLARSSL_ECP_C)
50
51#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052
Paul Bakker7dc4c442014-02-01 22:50:26 +010053#if defined(POLARSSL_PLATFORM_C)
54#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020055#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#define polarssl_printf printf
Paul Bakker6e339b52013-07-03 13:37:05 +020057#define polarssl_malloc malloc
58#define polarssl_free free
59#endif
60
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010061#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010062
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010063#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
64 !defined(EFI32)
65#define strcasecmp _stricmp
66#endif
67
Paul Bakker6a6087e2013-10-28 18:53:08 +010068#if defined(_MSC_VER) && !defined(inline)
69#define inline _inline
70#else
71#if defined(__ARMCC_VERSION) && !defined(inline)
72#define inline __inline
73#endif /* __ARMCC_VERSION */
74#endif /*_MSC_VER */
75
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010076#if defined(POLARSSL_SELF_TEST)
77/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010078 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020079 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010080 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010081static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010082#endif
83
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010084#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
85 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
86 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
87 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
88 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
89 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
90 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
Manuel Pégourié-Gonnard2a2ae642014-02-24 08:29:51 +010091 defined(POLARSSL_ECP_DP_BP512R1_ENABLED) || \
92 defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) || \
93 defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) || \
94 defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010095#define POLARSSL_ECP_SHORT_WEIERSTRASS
96#endif
97
98#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
99 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
100 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
101 defined(POLARSSL_ECP_DP_M511_ENABLED)
102#define POLARSSL_ECP_MONTGOMERY
103#endif
104
105/*
106 * Curve types: internal for now, might be exposed later
107 */
108typedef enum
109{
110 POLARSSL_ECP_TYPE_NONE = 0,
111 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
112 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
113} ecp_curve_type;
114
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100115/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200116 * List of supported curves:
117 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200118 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200119 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200120 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100121 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100122 * Curves are listed in order: largest curves first, and for a given size,
123 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200124 */
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100125static const ecp_curve_info ecp_supported_curves[POLARSSL_ECP_DP_MAX] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200126{
127#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200128 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200129#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100130#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
131 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
132#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200133#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200134 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200135#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100136#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
137 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
138#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200139#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200140 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200141#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100142#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
143 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
144#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100145#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
146 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
147#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200148#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200149 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200150#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100151#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
152 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
153#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100154#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
155 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
156#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100157#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
158 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
159#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200160 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200161};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100162
163static ecp_group_id ecp_supported_grp_id[POLARSSL_ECP_DP_MAX];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200164
165/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200166 * List of supported curves and associated info
167 */
168const ecp_curve_info *ecp_curve_list( void )
169{
170 return ecp_supported_curves;
171}
172
173/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100174 * List of supported curves, group ID only
175 */
176const ecp_group_id *ecp_grp_id_list( void )
177{
178 static int init_done = 0;
179
180 if( ! init_done )
181 {
182 size_t i = 0;
183 const ecp_curve_info *curve_info;
184
185 for( curve_info = ecp_curve_list();
186 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
187 curve_info++ )
188 {
189 ecp_supported_grp_id[i++] = curve_info->grp_id;
190 }
191 ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
192
193 init_done = 1;
194 }
195
196 return ecp_supported_grp_id;
197}
198
199/*
200 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200201 */
202const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
203{
204 const ecp_curve_info *curve_info;
205
206 for( curve_info = ecp_curve_list();
207 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
208 curve_info++ )
209 {
210 if( curve_info->grp_id == grp_id )
211 return( curve_info );
212 }
213
214 return( NULL );
215}
216
217/*
218 * Get the curve info from the TLS identifier
219 */
220const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
221{
222 const ecp_curve_info *curve_info;
223
224 for( curve_info = ecp_curve_list();
225 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
226 curve_info++ )
227 {
228 if( curve_info->tls_id == tls_id )
229 return( curve_info );
230 }
231
232 return( NULL );
233}
234
235/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100236 * Get the curve info from the name
237 */
238const ecp_curve_info *ecp_curve_info_from_name( const char *name )
239{
240 const ecp_curve_info *curve_info;
241
242 for( curve_info = ecp_curve_list();
243 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
244 curve_info++ )
245 {
246 if( strcasecmp( curve_info->name, name ) == 0 )
247 return( curve_info );
248 }
249
250 return( NULL );
251}
252
253/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100254 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100255 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100256static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100257{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100258 if( grp->G.X.p == NULL )
259 return( POLARSSL_ECP_TYPE_NONE );
260
261 if( grp->G.Y.p == NULL )
262 return( POLARSSL_ECP_TYPE_MONTGOMERY );
263 else
264 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100265}
266
267/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100268 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100269 */
270void ecp_point_init( ecp_point *pt )
271{
272 if( pt == NULL )
273 return;
274
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100275 mpi_init( &pt->X );
276 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100277 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100278}
279
280/*
281 * Initialize (the components of) a group
282 */
283void ecp_group_init( ecp_group *grp )
284{
285 if( grp == NULL )
286 return;
287
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200288 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100289}
290
291/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200292 * Initialize (the components of) a key pair
293 */
294void ecp_keypair_init( ecp_keypair *key )
295{
296 if ( key == NULL )
297 return;
298
299 ecp_group_init( &key->grp );
300 mpi_init( &key->d );
301 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200302}
303
304/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100305 * Unallocate (the components of) a point
306 */
307void ecp_point_free( ecp_point *pt )
308{
309 if( pt == NULL )
310 return;
311
312 mpi_free( &( pt->X ) );
313 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100314 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100315}
316
317/*
318 * Unallocate (the components of) a group
319 */
320void ecp_group_free( ecp_group *grp )
321{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200322 size_t i;
323
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100324 if( grp == NULL )
325 return;
326
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100327 if( grp->h != 1 )
328 {
329 mpi_free( &grp->P );
330 mpi_free( &grp->A );
331 mpi_free( &grp->B );
332 ecp_point_free( &grp->G );
333 mpi_free( &grp->N );
334 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200335
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200336 if( grp->T != NULL )
337 {
338 for( i = 0; i < grp->T_size; i++ )
339 ecp_point_free( &grp->T[i] );
340 polarssl_free( grp->T );
341 }
342
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200343 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100344}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100345
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100346/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200347 * Unallocate (the components of) a key pair
348 */
349void ecp_keypair_free( ecp_keypair *key )
350{
351 if ( key == NULL )
352 return;
353
354 ecp_group_free( &key->grp );
355 mpi_free( &key->d );
356 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200357}
358
359/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200360 * Copy the contents of a point
361 */
362int ecp_copy( ecp_point *P, const ecp_point *Q )
363{
364 int ret;
365
366 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
367 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
368 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
369
370cleanup:
371 return( ret );
372}
373
374/*
375 * Copy the contents of a group object
376 */
377int ecp_group_copy( ecp_group *dst, const ecp_group *src )
378{
379 return ecp_use_known_dp( dst, src->id );
380}
381
382/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100383 * Set point to zero
384 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100385int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100386{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100387 int ret;
388
389 MPI_CHK( mpi_lset( &pt->X , 1 ) );
390 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
391 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
392
393cleanup:
394 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100395}
396
397/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100398 * Tell if a point is zero
399 */
400int ecp_is_zero( ecp_point *pt )
401{
402 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
403}
404
405/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100406 * Import a non-zero point from ASCII strings
407 */
408int ecp_point_read_string( ecp_point *P, int radix,
409 const char *x, const char *y )
410{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100411 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100412
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100413 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
414 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100415 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100416
417cleanup:
418 return( ret );
419}
420
421/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100422 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100423 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100424int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100425 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100426 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100427{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200428 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100429 size_t plen;
430
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100431 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
432 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100433 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100434
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100435 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100436 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100437 */
438 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
439 {
440 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100441 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100442
443 buf[0] = 0x00;
444 *olen = 1;
445
446 return( 0 );
447 }
448
449 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100450
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100451 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
452 {
453 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100454
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100455 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100456 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100457
458 buf[0] = 0x04;
459 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
460 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
461 }
462 else if( format == POLARSSL_ECP_PF_COMPRESSED )
463 {
464 *olen = plen + 1;
465
466 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100467 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100468
469 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
470 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
471 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100472
473cleanup:
474 return( ret );
475}
476
477/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100478 * Import a point from unsigned binary data (SEC1 2.3.4)
479 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100480int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100481 const unsigned char *buf, size_t ilen )
482{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100483 int ret;
484 size_t plen;
485
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100486 if( buf[0] == 0x00 )
487 {
488 if( ilen == 1 )
489 return( ecp_set_zero( pt ) );
490 else
491 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
492 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100493
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100494 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100495
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100496 if( buf[0] != 0x04 )
497 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
498
499 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100500 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100501
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100502 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
503 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
504 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100505
506cleanup:
507 return( ret );
508}
509
510/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100511 * Import a point from a TLS ECPoint record (RFC 4492)
512 * struct {
513 * opaque point <1..2^8-1>;
514 * } ECPoint;
515 */
516int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100517 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100518{
519 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100520 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100521
522 /*
523 * We must have at least two bytes (1 for length, at least of for data)
524 */
525 if( buf_len < 2 )
526 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
527
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100528 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100529 if( data_len < 1 || data_len > buf_len - 1 )
530 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
531
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100532 /*
533 * Save buffer start for read_binary and update buf
534 */
535 buf_start = *buf;
536 *buf += data_len;
537
538 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100539}
540
541/*
542 * Export a point as a TLS ECPoint record (RFC 4492)
543 * struct {
544 * opaque point <1..2^8-1>;
545 * } ECPoint;
546 */
547int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100548 int format, size_t *olen,
549 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100550{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100551 int ret;
552
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100553 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100554 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100555 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100556 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100557 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
558
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100559 if( ( ret = ecp_point_write_binary( grp, pt, format,
560 olen, buf + 1, blen - 1) ) != 0 )
561 return( ret );
562
563 /*
564 * write length to the first byte and update total length
565 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200566 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100567 ++*olen;
568
569 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100570}
571
572/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200573 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200574 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200575int ecp_group_read_string( ecp_group *grp, int radix,
576 const char *p, const char *b,
577 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100578{
579 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100580
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100581 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100582 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
583 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
584 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
585
586 grp->pbits = mpi_msb( &grp->P );
587 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100588
589cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200590 if( ret != 0 )
591 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200592
593 return( ret );
594}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200595
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100596/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100597 * Set a group from an ECParameters record (RFC 4492)
598 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100599int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100600{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200601 uint16_t tls_id;
602 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100603
604 /*
605 * We expect at least three bytes (see below)
606 */
607 if( len < 3 )
608 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
609
610 /*
611 * First byte is curve_type; only named_curve is handled
612 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100613 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100614 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
615
616 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100617 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100618 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200619 tls_id = *(*buf)++;
620 tls_id <<= 8;
621 tls_id |= *(*buf)++;
622
623 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
624 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
625
626 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100627}
628
629/*
630 * Write the ECParameters record corresponding to a group (RFC 4492)
631 */
632int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
633 unsigned char *buf, size_t blen )
634{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200635 const ecp_curve_info *curve_info;
636
637 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
638 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200639
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100640 /*
641 * We are going to write 3 bytes (see below)
642 */
643 *olen = 3;
644 if( blen < *olen )
645 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
646
647 /*
648 * First byte is curve_type, always named_curve
649 */
650 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
651
652 /*
653 * Next two bytes are the namedcurve value
654 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200655 buf[0] = curve_info->tls_id >> 8;
656 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100657
658 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100659}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100660
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200661/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200662 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
663 * See the documentation of struct ecp_group.
664 *
665 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200666 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200667static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200668{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200669 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200670
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200671 if( grp->modp == NULL )
672 return( mpi_mod_mpi( N, N, &grp->P ) );
673
674 /* N->s < 0 is a much faster test, which fails only if N is 0 */
675 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
676 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200677 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200678 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200679 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200680
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200681 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200682
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200683 /* N->s < 0 is a much faster test, which fails only if N is 0 */
684 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
685 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200686
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200687 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
688 /* we known P, N and the result are positive */
689 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200690
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200691cleanup:
692 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200693}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200694
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100695/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100696 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100697 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100698 * In order to guarantee that, we need to ensure that operands of
699 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100700 * bring the result back to this range.
701 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100702 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100703 */
704
705/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100706 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
707 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100708#if defined(POLARSSL_SELF_TEST)
709#define INC_MUL_COUNT mul_count++;
710#else
711#define INC_MUL_COUNT
712#endif
713
714#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
715 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100716
717/*
718 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200719 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100720 */
721#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200722 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100723 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
724
725/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200726 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
727 * We known P, N and the result are positive, so sub_abs is correct, and
728 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100729 */
730#define MOD_ADD( N ) \
731 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200732 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100733
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100734#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
735/*
736 * For curves in short Weierstrass form, we do all the internal operations in
737 * Jacobian coordinates.
738 *
739 * For multiplication, we'll use a comb method with coutermeasueres against
740 * SPA, hence timing attacks.
741 */
742
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100743/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100744 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100745 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100746 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100747static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100748{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100749 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100750 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100751
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100752 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100753 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100754
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100755 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100756
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100757 /*
758 * X = X / Z^2 mod p
759 */
760 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
761 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
762 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100763
764 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100765 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100766 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100767 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
768 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100769
770 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100771 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100772 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100773 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100774
775cleanup:
776
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100777 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100778
779 return( ret );
780}
781
782/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100783 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100784 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100785 * (See for example Cohen's "A Course in Computational Algebraic Number
786 * Theory", Algorithm 10.3.4.)
787 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200788 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100789 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100790 *
791 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100792 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100793static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100794 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100795{
796 int ret;
797 size_t i;
798 mpi *c, u, Zi, ZZi;
799
800 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100801 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100802
Paul Bakker6e339b52013-07-03 13:37:05 +0200803 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200804 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100805
806 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
807 for( i = 0; i < t_len; i++ )
808 mpi_init( &c[i] );
809
810 /*
811 * c[i] = Z_0 * ... * Z_i
812 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100813 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100814 for( i = 1; i < t_len; i++ )
815 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100816 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100817 MOD_MUL( c[i] );
818 }
819
820 /*
821 * u = 1 / (Z_0 * ... * Z_n) mod P
822 */
823 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
824
825 for( i = t_len - 1; ; i-- )
826 {
827 /*
828 * Zi = 1 / Z_i mod p
829 * u = 1 / (Z_0 * ... * Z_i) mod P
830 */
831 if( i == 0 ) {
832 MPI_CHK( mpi_copy( &Zi, &u ) );
833 }
834 else
835 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100836 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
837 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100838 }
839
840 /*
841 * proceed as in normalize()
842 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100843 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
844 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
845 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
846 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100847
848 /*
849 * Post-precessing: reclaim some memory by shrinking coordinates
850 * - not storing Z (always 1)
851 * - shrinking other coordinates, but still keeping the same number of
852 * limbs as P, as otherwise it will too likely be regrown too fast.
853 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100854 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
855 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100856 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100857
858 if( i == 0 )
859 break;
860 }
861
862cleanup:
863
864 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
865 for( i = 0; i < t_len; i++ )
866 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200867 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100868
869 return( ret );
870}
871
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100872/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100873 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
874 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
875 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100876static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100877 ecp_point *Q,
878 unsigned char inv )
879{
880 int ret;
881 unsigned char nonzero;
882 mpi mQY;
883
884 mpi_init( &mQY );
885
886 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
887 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
888 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
889 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
890
891cleanup:
892 mpi_free( &mQY );
893
894 return( ret );
895}
896
897/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200898 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200899 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200900 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200901 * with heavy variable renaming, some reordering and one minor modification
902 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
903 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100904 *
905 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200906 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200907static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
908 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200909{
910 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200911 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200912
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200913#if defined(POLARSSL_SELF_TEST)
914 dbl_count++;
915#endif
916
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200917 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
918 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200919
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200920 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
921 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
922 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
923 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
924 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
925 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
926 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
927 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
928 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
929 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
930 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100931
932 /* Special case for A = -3 */
933 if( grp->A.p == NULL )
934 {
935 MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
936 X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
937 MOD_SUB( X3 );
938 }
939 else
940 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
941
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200942 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
943 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
944 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
945 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
946 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
947 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
948 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
949 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
950 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
951 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
952 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
953 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200954
955 MPI_CHK( mpi_copy( &R->X, &X3 ) );
956 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
957 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
958
959cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200960 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
961 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200962
963 return( ret );
964}
965
966/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100967 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100968 *
969 * The coordinates of Q must be normalized (= affine),
970 * but those of P don't need to. R is not normalized.
971 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100972 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100973 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100974 * - at each step, P, Q and R are multiples of the base point, the factor
975 * being less than its order, so none of them is zero;
976 * - Q is an odd multiple of the base point, P an even multiple,
977 * due to the choice of precomputed points in the modified comb method.
978 * So branches for these cases do not leak secret information.
979 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100980 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
981 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100982 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100983 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100984static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100985 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100986{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100987 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100988 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100989
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100990#if defined(POLARSSL_SELF_TEST)
991 add_count++;
992#endif
993
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100994 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100995 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100996 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100997 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
998 return( ecp_copy( R, Q ) );
999
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001000 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001001 return( ecp_copy( R, P ) );
1002
1003 /*
1004 * Make sure Q coordinates are normalized
1005 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001006 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001007 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001008
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001009 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1010 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001011
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001012 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1013 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1014 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1015 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1016 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1017 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001018
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001019 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001020 if( mpi_cmp_int( &T1, 0 ) == 0 )
1021 {
1022 if( mpi_cmp_int( &T2, 0 ) == 0 )
1023 {
1024 ret = ecp_double_jac( grp, R, P );
1025 goto cleanup;
1026 }
1027 else
1028 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001029 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001030 goto cleanup;
1031 }
1032 }
1033
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001034 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1035 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1036 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1037 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1038 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1039 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1040 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1041 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1042 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1043 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1044 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1045 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001046
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001047 MPI_CHK( mpi_copy( &R->X, &X ) );
1048 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1049 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001050
1051cleanup:
1052
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001053 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1054 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001055
1056 return( ret );
1057}
1058
1059/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001060 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001061 */
1062int ecp_add( const ecp_group *grp, ecp_point *R,
1063 const ecp_point *P, const ecp_point *Q )
1064{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001065 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001066
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001067 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001068 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1069
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001070 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001071 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001072
1073cleanup:
1074 return( ret );
1075}
1076
1077/*
1078 * Subtraction: R = P - Q, result's coordinates normalized
1079 */
1080int ecp_sub( const ecp_group *grp, ecp_point *R,
1081 const ecp_point *P, const ecp_point *Q )
1082{
1083 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001084 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001085
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001086 ecp_point_init( &mQ );
1087
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001088 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001089 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1090
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001091 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001092 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001093 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1094 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1095
1096 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001097 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001098
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001099cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001100 ecp_point_free( &mQ );
1101
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001102 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001103}
1104
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001105/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001106 * Randomize jacobian coordinates:
1107 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001108 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001109 *
1110 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001111 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001112static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001113 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1114{
1115 int ret;
1116 mpi l, ll;
1117 size_t p_size = (grp->pbits + 7) / 8;
1118 int count = 0;
1119
1120 mpi_init( &l ); mpi_init( &ll );
1121
1122 /* Generate l such that 1 < l < p */
1123 do
1124 {
1125 mpi_fill_random( &l, p_size, f_rng, p_rng );
1126
1127 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001128 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001129
1130 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001131 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001132 }
1133 while( mpi_cmp_int( &l, 1 ) <= 0 );
1134
1135 /* Z = l * Z */
1136 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1137
1138 /* X = l^2 * X */
1139 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1140 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1141
1142 /* Y = l^3 * Y */
1143 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1144 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1145
1146cleanup:
1147 mpi_free( &l ); mpi_free( &ll );
1148
1149 return( ret );
1150}
1151
1152/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001153 * Check and define parameters used by the comb method (see below for details)
1154 */
1155#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1156#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1157#endif
1158
1159/* d = ceil( n / w ) */
1160#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1161
1162/* number of precomputed points */
1163#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1164
1165/*
1166 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001167 *
1168 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001169 * modified version that provides resistance to SPA by avoiding zero
1170 * digits in the representation as in [3]. We modify the method further by
1171 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001172 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001173 *
1174 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1175 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1176 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001177 *
1178 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001179 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001180 * - w is the size, ie number of teeth, of the comb, and must be between
1181 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001182 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1183 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001184 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001185static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001186 unsigned char w, const mpi *m )
1187{
1188 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001189 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001190
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001191 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001192
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001193 /* First get the classical comb values (except for x_d = 0) */
1194 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001195 for( j = 0; j < w; j++ )
1196 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1197
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001198 /* Now make sure x_1 .. x_d are odd */
1199 c = 0;
1200 for( i = 1; i <= d; i++ )
1201 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001202 /* Add carry and update it */
1203 cc = x[i] & c;
1204 x[i] = x[i] ^ c;
1205 c = cc;
1206
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001207 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001208 adjust = 1 - ( x[i] & 0x01 );
1209 c |= x[i] & ( x[i-1] * adjust );
1210 x[i] = x[i] ^ ( x[i-1] * adjust );
1211 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001212 }
1213}
1214
1215/*
1216 * Precompute points for the comb method
1217 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001218 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1219 * T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001220 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001221 * T must be able to hold 2^{w - 1} elements
1222 *
1223 * Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001224 */
1225static int ecp_precompute_comb( const ecp_group *grp,
1226 ecp_point T[], const ecp_point *P,
1227 unsigned char w, size_t d )
1228{
1229 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001230 unsigned char i, k;
1231 size_t j;
1232 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001233
1234 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001235 * Set T[0] = P and
1236 * T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001237 */
1238 MPI_CHK( ecp_copy( &T[0], P ) );
1239
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001240 k = 0;
1241 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001242 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001243 cur = T + i;
1244 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001245 for( j = 0; j < d; j++ )
1246 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001247
1248 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001249 }
1250
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001251 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001252
1253 /*
1254 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001255 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001256 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001257 k = 0;
1258 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001259 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001260 j = i;
1261 while( j-- )
1262 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001263 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001264 TT[k++] = &T[i + j];
1265 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001266 }
1267
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001268 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001269
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001270cleanup:
1271 return( ret );
1272}
1273
1274/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001275 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001276 */
1277static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001278 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001279 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001280{
1281 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001282 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001283
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001284 /* Ignore the "sign" bit and scale down */
1285 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001286
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001287 /* Read the whole table to thwart cache-based timing attacks */
1288 for( j = 0; j < t_len; j++ )
1289 {
1290 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1291 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1292 }
1293
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001294 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001295 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001296
1297cleanup:
1298 return( ret );
1299}
1300
1301/*
1302 * Core multiplication algorithm for the (modified) comb method.
1303 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001304 *
1305 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001306 */
1307static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001308 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001309 const unsigned char x[], size_t d,
1310 int (*f_rng)(void *, unsigned char *, size_t),
1311 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001312{
1313 int ret;
1314 ecp_point Txi;
1315 size_t i;
1316
1317 ecp_point_init( &Txi );
1318
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001319 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001320 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001321 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001322 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001323 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001324 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001325
1326 while( i-- != 0 )
1327 {
1328 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001329 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001330 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001331 }
1332
1333cleanup:
1334 ecp_point_free( &Txi );
1335
1336 return( ret );
1337}
1338
1339/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001340 * Multiplication using the comb method,
1341 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001342 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001343static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1344 const mpi *m, const ecp_point *P,
1345 int (*f_rng)(void *, unsigned char *, size_t),
1346 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001347{
1348 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001349 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1350 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001351 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001352 ecp_point *T;
1353 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001354
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001355 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001356 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001357
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001358 /* we need N to be odd to trnaform m in an odd number, check now */
1359 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1360 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1361
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001362 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001363 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001364 * 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001365 * (see costs of the various parts, with 1S = 1M)
1366 */
1367 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001368
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001369 /*
1370 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001371 * Just adding one avoids upping the cost of the first mul too much,
1372 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001373 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001374#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001375 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1376 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001377 if( p_eq_g )
1378 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001379#else
1380 p_eq_g = 0;
1381#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001382
1383 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001384 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001385 * (The last test is useful only for very small curves in the test suite.)
1386 */
1387 if( w > POLARSSL_ECP_WINDOW_SIZE )
1388 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001389 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001390 w = 2;
1391
1392 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001393 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001394 d = ( grp->nbits + w - 1 ) / w;
1395
1396 /*
1397 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001398 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001399 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001400 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001401
1402 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001403 {
1404 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1405 if( T == NULL )
1406 {
1407 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1408 goto cleanup;
1409 }
1410
1411 for( i = 0; i < pre_len; i++ )
1412 ecp_point_init( &T[i] );
1413
1414 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1415
1416 if( p_eq_g )
1417 {
1418 grp->T = T;
1419 grp->T_size = pre_len;
1420 }
1421 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001422
1423 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001424 * Make sure M is odd (M = m or M = N - m, since N is odd)
1425 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001426 */
1427 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001428 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001429 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1430 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001431
1432 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001433 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001434 */
1435 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001436 MPI_CHK( ecp_mul_comb_core( grp, R, T, pre_len, k, d, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001437
1438 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001439 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001440 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001441 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1442 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001443
1444cleanup:
1445
1446 if( T != NULL && ! p_eq_g )
1447 {
1448 for( i = 0; i < pre_len; i++ )
1449 ecp_point_free( &T[i] );
1450 polarssl_free( T );
1451 }
1452
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001453 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001454 mpi_free( &mm );
1455
1456 if( ret != 0 )
1457 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001458
1459 return( ret );
1460}
1461
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001462#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1463
1464#if defined(POLARSSL_ECP_MONTGOMERY)
1465/*
1466 * For Montgomery curves, we do all the internal arithmetic in projective
1467 * coordinates. Import/export of points uses only the x coordinates, which is
1468 * internaly represented as X / Z.
1469 *
1470 * For scalar multiplication, we'll use a Montgomery ladder.
1471 */
1472
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001473/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001474 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1475 * Cost: 1M + 1I
1476 */
1477static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1478{
1479 int ret;
1480
1481 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1482 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1483 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1484
1485cleanup:
1486 return( ret );
1487}
1488
1489/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001490 * Randomize projective x/z coordinates:
1491 * (X, Z) -> (l X, l Z) for random l
1492 * This is sort of the reverse operation of ecp_normalize_mxz().
1493 *
1494 * This countermeasure was first suggested in [2].
1495 * Cost: 2M
1496 */
1497static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1498 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1499{
1500 int ret;
1501 mpi l;
1502 size_t p_size = (grp->pbits + 7) / 8;
1503 int count = 0;
1504
1505 mpi_init( &l );
1506
1507 /* Generate l such that 1 < l < p */
1508 do
1509 {
1510 mpi_fill_random( &l, p_size, f_rng, p_rng );
1511
1512 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001513 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001514
1515 if( count++ > 10 )
1516 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1517 }
1518 while( mpi_cmp_int( &l, 1 ) <= 0 );
1519
1520 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1521 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1522
1523cleanup:
1524 mpi_free( &l );
1525
1526 return( ret );
1527}
1528
1529/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001530 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1531 * for Montgomery curves in x/z coordinates.
1532 *
1533 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1534 * with
1535 * d = X1
1536 * P = (X2, Z2)
1537 * Q = (X3, Z3)
1538 * R = (X4, Z4)
1539 * S = (X5, Z5)
1540 * and eliminating temporary variables tO, ..., t4.
1541 *
1542 * Cost: 5M + 4S
1543 */
1544static int ecp_double_add_mxz( const ecp_group *grp,
1545 ecp_point *R, ecp_point *S,
1546 const ecp_point *P, const ecp_point *Q,
1547 const mpi *d )
1548{
1549 int ret;
1550 mpi A, AA, B, BB, E, C, D, DA, CB;
1551
1552 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1553 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1554 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1555
1556 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1557 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1558 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1559 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1560 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1561 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1562 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1563 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1564 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1565 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1566 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1567 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1568 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1569 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1570 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1571 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1572 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1573 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1574
1575cleanup:
1576 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1577 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1578 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1579
1580 return( ret );
1581}
1582
1583/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001584 * Multiplication with Montgomery ladder in x/z coordinates,
1585 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001586 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001587static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1588 const mpi *m, const ecp_point *P,
1589 int (*f_rng)(void *, unsigned char *, size_t),
1590 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001591{
1592 int ret;
1593 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001594 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001595 ecp_point RP;
1596 mpi PX;
1597
1598 ecp_point_init( &RP ); mpi_init( &PX );
1599
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001600 /* Save PX and read from P before writing to R, in case P == R */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001601 MPI_CHK( mpi_copy( &PX, &P->X ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001602 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001603
1604 /* Set R to zero in modified x/z coordinates */
1605 MPI_CHK( mpi_lset( &R->X, 1 ) );
1606 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1607 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001608
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001609 /* RP.X might be sligtly larger than P, so reduce it */
1610 MOD_ADD( RP.X );
1611
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001612 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001613 if( f_rng != NULL )
1614 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001615
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001616 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001617 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001618 while( i-- > 0 )
1619 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001620 b = mpi_get_bit( m, i );
1621 /*
1622 * if (b) R = 2R + P else R = 2R,
1623 * which is:
1624 * if (b) double_add( RP, R, RP, R )
1625 * else double_add( R, RP, R, RP )
1626 * but using safe conditional swaps to avoid leaks
1627 */
1628 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1629 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1630 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1631 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1632 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001633 }
1634
1635 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1636
1637cleanup:
1638 ecp_point_free( &RP ); mpi_free( &PX );
1639
1640 return( ret );
1641}
1642
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001643#endif /* POLARSSL_ECP_MONTGOMERY */
1644
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001645/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001646 * Multiplication R = m * P
1647 */
1648int ecp_mul( ecp_group *grp, ecp_point *R,
1649 const mpi *m, const ecp_point *P,
1650 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1651{
1652 int ret;
1653
1654 /* Common sanity checks */
1655 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1656 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1657
1658 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1659 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1660 return( ret );
1661
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001662#if defined(POLARSSL_ECP_MONTGOMERY)
1663 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001664 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001665#endif
1666#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1667 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001668 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001669#endif
1670 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001671}
1672
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001673#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001674/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001675 * Check that an affine point is valid as a public key,
1676 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001677 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001678static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001679{
1680 int ret;
1681 mpi YY, RHS;
1682
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001683 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001684 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1685 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1686 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1687 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001688 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001689
1690 mpi_init( &YY ); mpi_init( &RHS );
1691
1692 /*
1693 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001694 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001695 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001696 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1697 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001698
1699 /* Special case for A = -3 */
1700 if( grp->A.p == NULL )
1701 {
1702 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1703 }
1704 else
1705 {
1706 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1707 }
1708
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001709 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1710 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001711
1712 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001713 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001714
1715cleanup:
1716
1717 mpi_free( &YY ); mpi_free( &RHS );
1718
1719 return( ret );
1720}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001721#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1722
1723
1724#if defined(POLARSSL_ECP_MONTGOMERY)
1725/*
1726 * Check validity of a public key for Montgomery curves with x-only schemes
1727 */
1728static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1729{
1730 /* [M255 p. 5] Just check X is the correct number of bytes */
1731 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1732 return( POLARSSL_ERR_ECP_INVALID_KEY );
1733
1734 return( 0 );
1735}
1736#endif /* POLARSSL_ECP_MONTGOMERY */
1737
1738/*
1739 * Check that a point is valid as a public key
1740 */
1741int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1742{
1743 /* Must use affine coordinates */
1744 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1745 return( POLARSSL_ERR_ECP_INVALID_KEY );
1746
1747#if defined(POLARSSL_ECP_MONTGOMERY)
1748 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1749 return( ecp_check_pubkey_mx( grp, pt ) );
1750#endif
1751#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1752 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1753 return( ecp_check_pubkey_sw( grp, pt ) );
1754#endif
1755 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1756}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001757
1758/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001759 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001760 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001761int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001762{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001763#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001764 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001765 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001766 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001767 if( mpi_get_bit( d, 0 ) != 0 ||
1768 mpi_get_bit( d, 1 ) != 0 ||
1769 mpi_get_bit( d, 2 ) != 0 ||
1770 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1771 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001772 else
1773 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001774 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001775#endif
1776#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1777 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001778 {
1779 /* see SEC1 3.2 */
1780 if( mpi_cmp_int( d, 1 ) < 0 ||
1781 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1782 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001783 else
1784 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001785 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001786#endif
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001787
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001788 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001789}
1790
1791/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001792 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001793 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001794int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001795 int (*f_rng)(void *, unsigned char *, size_t),
1796 void *p_rng )
1797{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001798 int ret;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001799 size_t n_size = (grp->nbits + 7) / 8;
1800
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001801#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001802 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001803 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001804 /* [M225] page 5 */
1805 size_t b;
1806
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001807 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001808
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001809 /* Make sure the most significant bit is nbits */
1810 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1811 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001812 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001813 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001814 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001815
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001816 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001817 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1818 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1819 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001820 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001821 else
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001822#endif
1823#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1824 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001825 {
1826 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001827 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001828 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1829
1830 /*
1831 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1832 * - use the same byte ordering;
1833 * - keep the leftmost nbits bits of the generated octet string;
1834 * - try until result is in the desired range.
1835 * This also avoids any biais, which is especially important for ECDSA.
1836 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001837 do
1838 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001839 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1840 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1841 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001842
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001843 /*
1844 * Each try has at worst a probability 1/2 of failing (the msb has
1845 * a probability 1/2 of being 0, and then the result will be < N),
1846 * so after 30 tries failure probability is a most 2**(-30).
1847 *
1848 * For most curves, 1 try is enough with overwhelming probability,
1849 * since N starts with a lot of 1s in binary, but some curves
1850 * such as secp224k1 are actually very close to the worst case.
1851 */
1852 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001853 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1854 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001855 while( mpi_cmp_int( d, 1 ) < 0 ||
1856 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001857 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001858 else
1859#endif
1860 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001861
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001862cleanup:
1863 if( ret != 0 )
1864 return( ret );
1865
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001866 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001867}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001868
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001869/*
1870 * Generate a keypair, prettier wrapper
1871 */
1872int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1873 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1874{
1875 int ret;
1876
1877 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1878 return( ret );
1879
1880 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1881}
1882
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001883#if defined(POLARSSL_SELF_TEST)
1884
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001885/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001886 * Checkup routine
1887 */
1888int ecp_self_test( int verbose )
1889{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001890 int ret;
1891 size_t i;
1892 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001893 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001894 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001895 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001896 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001897 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001898 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001899 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001900 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001901 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001902 "400000000000000000000000000000000000000000000000", /* one and zeros */
1903 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1904 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001905 };
1906
1907 ecp_group_init( &grp );
1908 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001909 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001910 mpi_init( &m );
1911
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001912 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001913#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001914 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001915#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001916 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1917#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001918
1919 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001920 polarssl_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001921
1922 /* Do a dummy multiplication first to trigger precomputation */
1923 MPI_CHK( mpi_lset( &m, 2 ) );
1924 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001925
1926 add_count = 0;
1927 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001928 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001929 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001930 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001931
1932 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1933 {
1934 add_c_prev = add_count;
1935 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001936 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001937 add_count = 0;
1938 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001939 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001940
1941 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001942 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001943
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001944 if( add_count != add_c_prev ||
1945 dbl_count != dbl_c_prev ||
1946 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001947 {
1948 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001949 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001950
1951 ret = 1;
1952 goto cleanup;
1953 }
1954 }
1955
1956 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001957 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001958
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001959 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001960 polarssl_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001961 /* We computed P = 2G last time, use it */
1962
1963 add_count = 0;
1964 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001965 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001966 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1967 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1968
1969 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1970 {
1971 add_c_prev = add_count;
1972 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001973 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001974 add_count = 0;
1975 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001976 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001977
1978 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1979 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1980
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001981 if( add_count != add_c_prev ||
1982 dbl_count != dbl_c_prev ||
1983 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001984 {
1985 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001986 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001987
1988 ret = 1;
1989 goto cleanup;
1990 }
1991 }
1992
1993 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001994 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001995
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001996cleanup:
1997
1998 if( ret < 0 && verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001999 polarssl_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002000
2001 ecp_group_free( &grp );
2002 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002003 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002004 mpi_free( &m );
2005
2006 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002007 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002008
2009 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002010}
2011
2012#endif
2013
2014#endif