blob: ad6e5f586f563e542a6df95a5ee404c9783ba00f [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 Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, 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
53#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010060#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010062#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
63 !defined(EFI32)
64#define strcasecmp _stricmp
65#endif
66
Paul Bakker6a6087e2013-10-28 18:53:08 +010067#if defined(_MSC_VER) && !defined(inline)
68#define inline _inline
69#else
70#if defined(__ARMCC_VERSION) && !defined(inline)
71#define inline __inline
72#endif /* __ARMCC_VERSION */
73#endif /*_MSC_VER */
74
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010075#if defined(POLARSSL_SELF_TEST)
76/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010077 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020078 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010079 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010080static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010081#endif
82
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010083#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
84 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
85 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
86 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
87 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
88 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
89 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
90 defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
91#define POLARSSL_ECP_SHORT_WEIERSTRASS
92#endif
93
94#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
95 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
96 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
97 defined(POLARSSL_ECP_DP_M511_ENABLED)
98#define POLARSSL_ECP_MONTGOMERY
99#endif
100
101/*
102 * Curve types: internal for now, might be exposed later
103 */
104typedef enum
105{
106 POLARSSL_ECP_TYPE_NONE = 0,
107 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
108 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
109} ecp_curve_type;
110
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100111/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200112 * List of supported curves:
113 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200114 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200115 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200116 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100117 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100118 * Curves are listed in order: largest curves first, and for a given size,
119 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200120 */
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100121static const ecp_curve_info ecp_supported_curves[POLARSSL_ECP_DP_MAX] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200122{
123#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200124 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200125#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100126#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
127 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
128#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200129#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200130 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200131#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100132#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
133 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
134#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200135#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200136 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200137#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100138#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
139 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
140#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100141#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
142 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
143#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200144#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200145 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200146#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100147#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
148 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
149#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100150#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
151 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
152#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100153#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
154 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
155#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200156 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200157};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100158
159static ecp_group_id ecp_supported_grp_id[POLARSSL_ECP_DP_MAX];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200160
161/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200162 * List of supported curves and associated info
163 */
164const ecp_curve_info *ecp_curve_list( void )
165{
166 return ecp_supported_curves;
167}
168
169/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100170 * List of supported curves, group ID only
171 */
172const ecp_group_id *ecp_grp_id_list( void )
173{
174 static int init_done = 0;
175
176 if( ! init_done )
177 {
178 size_t i = 0;
179 const ecp_curve_info *curve_info;
180
181 for( curve_info = ecp_curve_list();
182 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
183 curve_info++ )
184 {
185 ecp_supported_grp_id[i++] = curve_info->grp_id;
186 }
187 ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
188
189 init_done = 1;
190 }
191
192 return ecp_supported_grp_id;
193}
194
195/*
196 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200197 */
198const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
199{
200 const ecp_curve_info *curve_info;
201
202 for( curve_info = ecp_curve_list();
203 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
204 curve_info++ )
205 {
206 if( curve_info->grp_id == grp_id )
207 return( curve_info );
208 }
209
210 return( NULL );
211}
212
213/*
214 * Get the curve info from the TLS identifier
215 */
216const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
217{
218 const ecp_curve_info *curve_info;
219
220 for( curve_info = ecp_curve_list();
221 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
222 curve_info++ )
223 {
224 if( curve_info->tls_id == tls_id )
225 return( curve_info );
226 }
227
228 return( NULL );
229}
230
231/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100232 * Get the curve info from the name
233 */
234const ecp_curve_info *ecp_curve_info_from_name( const char *name )
235{
236 const ecp_curve_info *curve_info;
237
238 for( curve_info = ecp_curve_list();
239 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
240 curve_info++ )
241 {
242 if( strcasecmp( curve_info->name, name ) == 0 )
243 return( curve_info );
244 }
245
246 return( NULL );
247}
248
249/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100250 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100251 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100252static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100253{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100254 if( grp->G.X.p == NULL )
255 return( POLARSSL_ECP_TYPE_NONE );
256
257 if( grp->G.Y.p == NULL )
258 return( POLARSSL_ECP_TYPE_MONTGOMERY );
259 else
260 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100261}
262
263/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100264 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100265 */
266void ecp_point_init( ecp_point *pt )
267{
268 if( pt == NULL )
269 return;
270
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100271 mpi_init( &pt->X );
272 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100273 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100274}
275
276/*
277 * Initialize (the components of) a group
278 */
279void ecp_group_init( ecp_group *grp )
280{
281 if( grp == NULL )
282 return;
283
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200284 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100285}
286
287/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200288 * Initialize (the components of) a key pair
289 */
290void ecp_keypair_init( ecp_keypair *key )
291{
292 if ( key == NULL )
293 return;
294
295 ecp_group_init( &key->grp );
296 mpi_init( &key->d );
297 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200298}
299
300/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100301 * Unallocate (the components of) a point
302 */
303void ecp_point_free( ecp_point *pt )
304{
305 if( pt == NULL )
306 return;
307
308 mpi_free( &( pt->X ) );
309 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100310 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100311}
312
313/*
314 * Unallocate (the components of) a group
315 */
316void ecp_group_free( ecp_group *grp )
317{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200318 size_t i;
319
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100320 if( grp == NULL )
321 return;
322
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100323 if( grp->h != 1 )
324 {
325 mpi_free( &grp->P );
326 mpi_free( &grp->A );
327 mpi_free( &grp->B );
328 ecp_point_free( &grp->G );
329 mpi_free( &grp->N );
330 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200331
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200332 if( grp->T != NULL )
333 {
334 for( i = 0; i < grp->T_size; i++ )
335 ecp_point_free( &grp->T[i] );
336 polarssl_free( grp->T );
337 }
338
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200339 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100340}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100341
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100342/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200343 * Unallocate (the components of) a key pair
344 */
345void ecp_keypair_free( ecp_keypair *key )
346{
347 if ( key == NULL )
348 return;
349
350 ecp_group_free( &key->grp );
351 mpi_free( &key->d );
352 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200353}
354
355/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200356 * Copy the contents of a point
357 */
358int ecp_copy( ecp_point *P, const ecp_point *Q )
359{
360 int ret;
361
362 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
363 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
364 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
365
366cleanup:
367 return( ret );
368}
369
370/*
371 * Copy the contents of a group object
372 */
373int ecp_group_copy( ecp_group *dst, const ecp_group *src )
374{
375 return ecp_use_known_dp( dst, src->id );
376}
377
378/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100379 * Set point to zero
380 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100381int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100382{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100383 int ret;
384
385 MPI_CHK( mpi_lset( &pt->X , 1 ) );
386 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
387 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
388
389cleanup:
390 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100391}
392
393/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100394 * Tell if a point is zero
395 */
396int ecp_is_zero( ecp_point *pt )
397{
398 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
399}
400
401/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100402 * Import a non-zero point from ASCII strings
403 */
404int ecp_point_read_string( ecp_point *P, int radix,
405 const char *x, const char *y )
406{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100407 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100408
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100409 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
410 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100411 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100412
413cleanup:
414 return( ret );
415}
416
417/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100418 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100419 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100420int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100421 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100422 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100423{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200424 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100425 size_t plen;
426
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100427 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
428 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100429 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100430
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100431 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100432 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100433 */
434 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
435 {
436 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100437 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100438
439 buf[0] = 0x00;
440 *olen = 1;
441
442 return( 0 );
443 }
444
445 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100446
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100447 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
448 {
449 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100450
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100451 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100452 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100453
454 buf[0] = 0x04;
455 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
456 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
457 }
458 else if( format == POLARSSL_ECP_PF_COMPRESSED )
459 {
460 *olen = plen + 1;
461
462 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100463 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100464
465 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
466 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
467 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100468
469cleanup:
470 return( ret );
471}
472
473/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100474 * Import a point from unsigned binary data (SEC1 2.3.4)
475 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100476int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
477 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100478 int ret;
479 size_t plen;
480
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100481 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100482 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100483
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100484 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100485
486 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100487 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100488
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100489 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
490 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
491 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100492
493cleanup:
494 return( ret );
495}
496
497/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100498 * Import a point from a TLS ECPoint record (RFC 4492)
499 * struct {
500 * opaque point <1..2^8-1>;
501 * } ECPoint;
502 */
503int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100504 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100505{
506 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100507 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100508
509 /*
510 * We must have at least two bytes (1 for length, at least of for data)
511 */
512 if( buf_len < 2 )
513 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
514
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100515 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100516 if( data_len < 1 || data_len > buf_len - 1 )
517 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
518
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100519 /*
520 * Save buffer start for read_binary and update buf
521 */
522 buf_start = *buf;
523 *buf += data_len;
524
525 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100526}
527
528/*
529 * Export a point as a TLS ECPoint record (RFC 4492)
530 * struct {
531 * opaque point <1..2^8-1>;
532 * } ECPoint;
533 */
534int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100535 int format, size_t *olen,
536 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100537{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100538 int ret;
539
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100540 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100541 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100542 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100543 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100544 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
545
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100546 if( ( ret = ecp_point_write_binary( grp, pt, format,
547 olen, buf + 1, blen - 1) ) != 0 )
548 return( ret );
549
550 /*
551 * write length to the first byte and update total length
552 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200553 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100554 ++*olen;
555
556 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100557}
558
559/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200560 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200561 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200562int ecp_group_read_string( ecp_group *grp, int radix,
563 const char *p, const char *b,
564 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100565{
566 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100567
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100568 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100569 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
570 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
571 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
572
573 grp->pbits = mpi_msb( &grp->P );
574 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100575
576cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200577 if( ret != 0 )
578 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200579
580 return( ret );
581}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200582
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100583/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100584 * Set a group from an ECParameters record (RFC 4492)
585 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100586int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100587{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200588 uint16_t tls_id;
589 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100590
591 /*
592 * We expect at least three bytes (see below)
593 */
594 if( len < 3 )
595 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
596
597 /*
598 * First byte is curve_type; only named_curve is handled
599 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100600 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100601 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
602
603 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100604 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100605 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200606 tls_id = *(*buf)++;
607 tls_id <<= 8;
608 tls_id |= *(*buf)++;
609
610 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
611 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
612
613 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100614}
615
616/*
617 * Write the ECParameters record corresponding to a group (RFC 4492)
618 */
619int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
620 unsigned char *buf, size_t blen )
621{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200622 const ecp_curve_info *curve_info;
623
624 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
625 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200626
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100627 /*
628 * We are going to write 3 bytes (see below)
629 */
630 *olen = 3;
631 if( blen < *olen )
632 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
633
634 /*
635 * First byte is curve_type, always named_curve
636 */
637 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
638
639 /*
640 * Next two bytes are the namedcurve value
641 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200642 buf[0] = curve_info->tls_id >> 8;
643 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100644
645 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100646}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100647
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200648/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200649 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
650 * See the documentation of struct ecp_group.
651 *
652 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200653 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200654static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200655{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200656 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200657
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200658 if( grp->modp == NULL )
659 return( mpi_mod_mpi( N, N, &grp->P ) );
660
661 /* N->s < 0 is a much faster test, which fails only if N is 0 */
662 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
663 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200664 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200665 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200666 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200667
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200668 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200669
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200670 /* N->s < 0 is a much faster test, which fails only if N is 0 */
671 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
672 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200673
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200674 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
675 /* we known P, N and the result are positive */
676 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200677
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200678cleanup:
679 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200680}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200681
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100682/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100683 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100684 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100685 * In order to guarantee that, we need to ensure that operands of
686 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100687 * bring the result back to this range.
688 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100689 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100690 */
691
692/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100693 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
694 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100695#if defined(POLARSSL_SELF_TEST)
696#define INC_MUL_COUNT mul_count++;
697#else
698#define INC_MUL_COUNT
699#endif
700
701#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
702 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100703
704/*
705 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200706 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100707 */
708#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200709 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100710 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
711
712/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200713 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
714 * We known P, N and the result are positive, so sub_abs is correct, and
715 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100716 */
717#define MOD_ADD( N ) \
718 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200719 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100720
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100721#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
722/*
723 * For curves in short Weierstrass form, we do all the internal operations in
724 * Jacobian coordinates.
725 *
726 * For multiplication, we'll use a comb method with coutermeasueres against
727 * SPA, hence timing attacks.
728 */
729
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100730/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100731 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100732 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100733 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100734static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100735{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100736 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100737 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100738
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100739 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100740 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100741
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100742 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100743
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100744 /*
745 * X = X / Z^2 mod p
746 */
747 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
748 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
749 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100750
751 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100752 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100753 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100754 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
755 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100756
757 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100758 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100759 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100760 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100761
762cleanup:
763
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100764 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100765
766 return( ret );
767}
768
769/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100770 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100771 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100772 * (See for example Cohen's "A Course in Computational Algebraic Number
773 * Theory", Algorithm 10.3.4.)
774 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200775 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100776 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100777 *
778 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100779 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100780static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100781 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100782{
783 int ret;
784 size_t i;
785 mpi *c, u, Zi, ZZi;
786
787 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100788 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100789
Paul Bakker6e339b52013-07-03 13:37:05 +0200790 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200791 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100792
793 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
794 for( i = 0; i < t_len; i++ )
795 mpi_init( &c[i] );
796
797 /*
798 * c[i] = Z_0 * ... * Z_i
799 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100800 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100801 for( i = 1; i < t_len; i++ )
802 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100803 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100804 MOD_MUL( c[i] );
805 }
806
807 /*
808 * u = 1 / (Z_0 * ... * Z_n) mod P
809 */
810 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
811
812 for( i = t_len - 1; ; i-- )
813 {
814 /*
815 * Zi = 1 / Z_i mod p
816 * u = 1 / (Z_0 * ... * Z_i) mod P
817 */
818 if( i == 0 ) {
819 MPI_CHK( mpi_copy( &Zi, &u ) );
820 }
821 else
822 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100823 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
824 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100825 }
826
827 /*
828 * proceed as in normalize()
829 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100830 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
831 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
832 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
833 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 +0100834
835 /*
836 * Post-precessing: reclaim some memory by shrinking coordinates
837 * - not storing Z (always 1)
838 * - shrinking other coordinates, but still keeping the same number of
839 * limbs as P, as otherwise it will too likely be regrown too fast.
840 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100841 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
842 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100843 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100844
845 if( i == 0 )
846 break;
847 }
848
849cleanup:
850
851 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
852 for( i = 0; i < t_len; i++ )
853 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200854 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100855
856 return( ret );
857}
858
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100859/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100860 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
861 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
862 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100863static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100864 ecp_point *Q,
865 unsigned char inv )
866{
867 int ret;
868 unsigned char nonzero;
869 mpi mQY;
870
871 mpi_init( &mQY );
872
873 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
874 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
875 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
876 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
877
878cleanup:
879 mpi_free( &mQY );
880
881 return( ret );
882}
883
884/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200885 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200886 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200887 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200888 * with heavy variable renaming, some reordering and one minor modification
889 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
890 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100891 *
892 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200893 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200894static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
895 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200896{
897 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200898 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200899
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200900#if defined(POLARSSL_SELF_TEST)
901 dbl_count++;
902#endif
903
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200904 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
905 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200906
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200907 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
908 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
909 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
910 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
911 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
912 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
913 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
914 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
915 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
916 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
917 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100918
919 /* Special case for A = -3 */
920 if( grp->A.p == NULL )
921 {
922 MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
923 X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
924 MOD_SUB( X3 );
925 }
926 else
927 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
928
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200929 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
930 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
931 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
932 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
933 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
934 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
935 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
936 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
937 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
938 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
939 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
940 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200941
942 MPI_CHK( mpi_copy( &R->X, &X3 ) );
943 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
944 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
945
946cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200947 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
948 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200949
950 return( ret );
951}
952
953/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100954 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100955 *
956 * The coordinates of Q must be normalized (= affine),
957 * but those of P don't need to. R is not normalized.
958 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100959 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100960 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100961 * - at each step, P, Q and R are multiples of the base point, the factor
962 * being less than its order, so none of them is zero;
963 * - Q is an odd multiple of the base point, P an even multiple,
964 * due to the choice of precomputed points in the modified comb method.
965 * So branches for these cases do not leak secret information.
966 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100967 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
968 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100969 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100970 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100971static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100972 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100973{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100974 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100975 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100976
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100977#if defined(POLARSSL_SELF_TEST)
978 add_count++;
979#endif
980
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100981 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100982 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100983 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100984 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
985 return( ecp_copy( R, Q ) );
986
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100987 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100988 return( ecp_copy( R, P ) );
989
990 /*
991 * Make sure Q coordinates are normalized
992 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100993 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200994 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100995
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100996 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
997 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100998
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100999 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1000 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1001 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1002 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1003 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1004 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001005
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001006 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001007 if( mpi_cmp_int( &T1, 0 ) == 0 )
1008 {
1009 if( mpi_cmp_int( &T2, 0 ) == 0 )
1010 {
1011 ret = ecp_double_jac( grp, R, P );
1012 goto cleanup;
1013 }
1014 else
1015 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001016 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001017 goto cleanup;
1018 }
1019 }
1020
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001021 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1022 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1023 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1024 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1025 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1026 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1027 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1028 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1029 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1030 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1031 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1032 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001033
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001034 MPI_CHK( mpi_copy( &R->X, &X ) );
1035 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1036 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001037
1038cleanup:
1039
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001040 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1041 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001042
1043 return( ret );
1044}
1045
1046/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001047 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001048 */
1049int ecp_add( const ecp_group *grp, ecp_point *R,
1050 const ecp_point *P, const ecp_point *Q )
1051{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001052 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001053
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001054 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001055 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1056
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001057 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001058 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001059
1060cleanup:
1061 return( ret );
1062}
1063
1064/*
1065 * Subtraction: R = P - Q, result's coordinates normalized
1066 */
1067int ecp_sub( const ecp_group *grp, ecp_point *R,
1068 const ecp_point *P, const ecp_point *Q )
1069{
1070 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001071 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001072
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001073 ecp_point_init( &mQ );
1074
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001075 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001076 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1077
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001078 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001079 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001080 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1081 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1082
1083 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001084 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001085
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001086cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001087 ecp_point_free( &mQ );
1088
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001089 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001090}
1091
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001092/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001093 * Randomize jacobian coordinates:
1094 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001095 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001096 *
1097 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001098 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001099static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001100 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1101{
1102 int ret;
1103 mpi l, ll;
1104 size_t p_size = (grp->pbits + 7) / 8;
1105 int count = 0;
1106
1107 mpi_init( &l ); mpi_init( &ll );
1108
1109 /* Generate l such that 1 < l < p */
1110 do
1111 {
1112 mpi_fill_random( &l, p_size, f_rng, p_rng );
1113
1114 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1115 mpi_shift_r( &l, 1 );
1116
1117 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001118 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001119 }
1120 while( mpi_cmp_int( &l, 1 ) <= 0 );
1121
1122 /* Z = l * Z */
1123 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1124
1125 /* X = l^2 * X */
1126 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1127 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1128
1129 /* Y = l^3 * Y */
1130 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1131 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1132
1133cleanup:
1134 mpi_free( &l ); mpi_free( &ll );
1135
1136 return( ret );
1137}
1138
1139/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001140 * Check and define parameters used by the comb method (see below for details)
1141 */
1142#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1143#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1144#endif
1145
1146/* d = ceil( n / w ) */
1147#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1148
1149/* number of precomputed points */
1150#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1151
1152/*
1153 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001154 *
1155 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001156 * modified version that provides resistance to SPA by avoiding zero
1157 * digits in the representation as in [3]. We modify the method further by
1158 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001159 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001160 *
1161 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1162 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1163 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001164 *
1165 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001166 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001167 * - w is the size, ie number of teeth, of the comb, and must be between
1168 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001169 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1170 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001171 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001172static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001173 unsigned char w, const mpi *m )
1174{
1175 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001176 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001177
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001178 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001179
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001180 /* First get the classical comb values (except for x_d = 0) */
1181 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001182 for( j = 0; j < w; j++ )
1183 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1184
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001185 /* Now make sure x_1 .. x_d are odd */
1186 c = 0;
1187 for( i = 1; i <= d; i++ )
1188 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001189 /* Add carry and update it */
1190 cc = x[i] & c;
1191 x[i] = x[i] ^ c;
1192 c = cc;
1193
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001194 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001195 adjust = 1 - ( x[i] & 0x01 );
1196 c |= x[i] & ( x[i-1] * adjust );
1197 x[i] = x[i] ^ ( x[i-1] * adjust );
1198 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001199 }
1200}
1201
1202/*
1203 * Precompute points for the comb method
1204 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001205 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1206 * 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 +01001207 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001208 * T must be able to hold 2^{w - 1} elements
1209 *
1210 * 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 +01001211 */
1212static int ecp_precompute_comb( const ecp_group *grp,
1213 ecp_point T[], const ecp_point *P,
1214 unsigned char w, size_t d )
1215{
1216 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001217 unsigned char i, k;
1218 size_t j;
1219 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001220
1221 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001222 * Set T[0] = P and
1223 * 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 +01001224 */
1225 MPI_CHK( ecp_copy( &T[0], P ) );
1226
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001227 k = 0;
1228 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001229 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001230 cur = T + i;
1231 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001232 for( j = 0; j < d; j++ )
1233 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001234
1235 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001236 }
1237
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001238 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001239
1240 /*
1241 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001242 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001243 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001244 k = 0;
1245 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001246 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001247 j = i;
1248 while( j-- )
1249 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001250 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001251 TT[k++] = &T[i + j];
1252 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001253 }
1254
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001255 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001256
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001257cleanup:
1258 return( ret );
1259}
1260
1261/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001262 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001263 */
1264static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001265 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001266 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001267{
1268 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001269 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001270
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001271 /* Ignore the "sign" bit and scale down */
1272 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001273
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001274 /* Read the whole table to thwart cache-based timing attacks */
1275 for( j = 0; j < t_len; j++ )
1276 {
1277 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1278 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1279 }
1280
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001281 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001282 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001283
1284cleanup:
1285 return( ret );
1286}
1287
1288/*
1289 * Core multiplication algorithm for the (modified) comb method.
1290 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001291 *
1292 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001293 */
1294static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001295 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001296 const unsigned char x[], size_t d,
1297 int (*f_rng)(void *, unsigned char *, size_t),
1298 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001299{
1300 int ret;
1301 ecp_point Txi;
1302 size_t i;
1303
1304 ecp_point_init( &Txi );
1305
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001306 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001307 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001308 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001309 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001310 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001311 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001312
1313 while( i-- != 0 )
1314 {
1315 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001316 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001317 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001318 }
1319
1320cleanup:
1321 ecp_point_free( &Txi );
1322
1323 return( ret );
1324}
1325
1326/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001327 * Multiplication using the comb method,
1328 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001329 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001330static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1331 const mpi *m, const ecp_point *P,
1332 int (*f_rng)(void *, unsigned char *, size_t),
1333 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001334{
1335 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001336 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1337 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001338 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001339 ecp_point *T;
1340 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001341
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001342 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001343 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001344
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001345 /* we need N to be odd to trnaform m in an odd number, check now */
1346 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1347 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1348
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001349 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001350 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001351 * 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 +01001352 * (see costs of the various parts, with 1S = 1M)
1353 */
1354 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001355
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001356 /*
1357 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001358 * Just adding one avoids upping the cost of the first mul too much,
1359 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001360 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001361#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001362 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1363 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001364 if( p_eq_g )
1365 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001366#else
1367 p_eq_g = 0;
1368#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001369
1370 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001371 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001372 * (The last test is useful only for very small curves in the test suite.)
1373 */
1374 if( w > POLARSSL_ECP_WINDOW_SIZE )
1375 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001376 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001377 w = 2;
1378
1379 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001380 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001381 d = ( grp->nbits + w - 1 ) / w;
1382
1383 /*
1384 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001385 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001386 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001387 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001388
1389 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001390 {
1391 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1392 if( T == NULL )
1393 {
1394 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1395 goto cleanup;
1396 }
1397
1398 for( i = 0; i < pre_len; i++ )
1399 ecp_point_init( &T[i] );
1400
1401 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1402
1403 if( p_eq_g )
1404 {
1405 grp->T = T;
1406 grp->T_size = pre_len;
1407 }
1408 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001409
1410 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001411 * Make sure M is odd (M = m or M = N - m, since N is odd)
1412 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001413 */
1414 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001415 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001416 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1417 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001418
1419 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001420 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001421 */
1422 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001423 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 +01001424
1425 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001426 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001427 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001428 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1429 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001430
1431cleanup:
1432
1433 if( T != NULL && ! p_eq_g )
1434 {
1435 for( i = 0; i < pre_len; i++ )
1436 ecp_point_free( &T[i] );
1437 polarssl_free( T );
1438 }
1439
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001440 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001441 mpi_free( &mm );
1442
1443 if( ret != 0 )
1444 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001445
1446 return( ret );
1447}
1448
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001449#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1450
1451#if defined(POLARSSL_ECP_MONTGOMERY)
1452/*
1453 * For Montgomery curves, we do all the internal arithmetic in projective
1454 * coordinates. Import/export of points uses only the x coordinates, which is
1455 * internaly represented as X / Z.
1456 *
1457 * For scalar multiplication, we'll use a Montgomery ladder.
1458 */
1459
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001460/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001461 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1462 * Cost: 1M + 1I
1463 */
1464static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1465{
1466 int ret;
1467
1468 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1469 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1470 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1471
1472cleanup:
1473 return( ret );
1474}
1475
1476/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001477 * Randomize projective x/z coordinates:
1478 * (X, Z) -> (l X, l Z) for random l
1479 * This is sort of the reverse operation of ecp_normalize_mxz().
1480 *
1481 * This countermeasure was first suggested in [2].
1482 * Cost: 2M
1483 */
1484static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1485 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1486{
1487 int ret;
1488 mpi l;
1489 size_t p_size = (grp->pbits + 7) / 8;
1490 int count = 0;
1491
1492 mpi_init( &l );
1493
1494 /* Generate l such that 1 < l < p */
1495 do
1496 {
1497 mpi_fill_random( &l, p_size, f_rng, p_rng );
1498
1499 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1500 mpi_shift_r( &l, 1 );
1501
1502 if( count++ > 10 )
1503 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1504 }
1505 while( mpi_cmp_int( &l, 1 ) <= 0 );
1506
1507 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1508 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1509
1510cleanup:
1511 mpi_free( &l );
1512
1513 return( ret );
1514}
1515
1516/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001517 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1518 * for Montgomery curves in x/z coordinates.
1519 *
1520 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1521 * with
1522 * d = X1
1523 * P = (X2, Z2)
1524 * Q = (X3, Z3)
1525 * R = (X4, Z4)
1526 * S = (X5, Z5)
1527 * and eliminating temporary variables tO, ..., t4.
1528 *
1529 * Cost: 5M + 4S
1530 */
1531static int ecp_double_add_mxz( const ecp_group *grp,
1532 ecp_point *R, ecp_point *S,
1533 const ecp_point *P, const ecp_point *Q,
1534 const mpi *d )
1535{
1536 int ret;
1537 mpi A, AA, B, BB, E, C, D, DA, CB;
1538
1539 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1540 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1541 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1542
1543 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1544 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1545 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1546 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1547 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1548 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1549 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1550 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1551 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1552 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1553 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1554 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1555 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1556 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1557 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1558 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1559 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1560 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1561
1562cleanup:
1563 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1564 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1565 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1566
1567 return( ret );
1568}
1569
1570/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001571 * Multiplication with Montgomery ladder in x/z coordinates,
1572 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001573 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001574static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1575 const mpi *m, const ecp_point *P,
1576 int (*f_rng)(void *, unsigned char *, size_t),
1577 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001578{
1579 int ret;
1580 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001581 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001582 ecp_point RP;
1583 mpi PX;
1584
1585 ecp_point_init( &RP ); mpi_init( &PX );
1586
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001587 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001588 mpi_copy( &PX, &P->X );
1589 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001590
1591 /* Set R to zero in modified x/z coordinates */
1592 MPI_CHK( mpi_lset( &R->X, 1 ) );
1593 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1594 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001595
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001596 /* RP.X might be sligtly larger than P, so reduce it */
1597 MOD_ADD( RP.X );
1598
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001599 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001600 if( f_rng != NULL )
1601 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001602
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001603 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001604 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001605 while( i-- > 0 )
1606 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001607 b = mpi_get_bit( m, i );
1608 /*
1609 * if (b) R = 2R + P else R = 2R,
1610 * which is:
1611 * if (b) double_add( RP, R, RP, R )
1612 * else double_add( R, RP, R, RP )
1613 * but using safe conditional swaps to avoid leaks
1614 */
1615 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1616 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1617 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1618 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1619 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001620 }
1621
1622 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1623
1624cleanup:
1625 ecp_point_free( &RP ); mpi_free( &PX );
1626
1627 return( ret );
1628}
1629
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001630#endif /* POLARSSL_ECP_MONTGOMERY */
1631
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001632/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001633 * Multiplication R = m * P
1634 */
1635int ecp_mul( ecp_group *grp, ecp_point *R,
1636 const mpi *m, const ecp_point *P,
1637 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1638{
1639 int ret;
1640
1641 /* Common sanity checks */
1642 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1643 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1644
1645 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1646 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1647 return( ret );
1648
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001649#if defined(POLARSSL_ECP_MONTGOMERY)
1650 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001651 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001652#endif
1653#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1654 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001655 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001656#endif
1657 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001658}
1659
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001660#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001661/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001662 * Check that an affine point is valid as a public key,
1663 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001664 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001665static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001666{
1667 int ret;
1668 mpi YY, RHS;
1669
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001670 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001671 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1672 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1673 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1674 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001675 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001676
1677 mpi_init( &YY ); mpi_init( &RHS );
1678
1679 /*
1680 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001681 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001682 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001683 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1684 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001685
1686 /* Special case for A = -3 */
1687 if( grp->A.p == NULL )
1688 {
1689 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1690 }
1691 else
1692 {
1693 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1694 }
1695
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001696 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1697 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001698
1699 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001700 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001701
1702cleanup:
1703
1704 mpi_free( &YY ); mpi_free( &RHS );
1705
1706 return( ret );
1707}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001708#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1709
1710
1711#if defined(POLARSSL_ECP_MONTGOMERY)
1712/*
1713 * Check validity of a public key for Montgomery curves with x-only schemes
1714 */
1715static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1716{
1717 /* [M255 p. 5] Just check X is the correct number of bytes */
1718 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1719 return( POLARSSL_ERR_ECP_INVALID_KEY );
1720
1721 return( 0 );
1722}
1723#endif /* POLARSSL_ECP_MONTGOMERY */
1724
1725/*
1726 * Check that a point is valid as a public key
1727 */
1728int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1729{
1730 /* Must use affine coordinates */
1731 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1732 return( POLARSSL_ERR_ECP_INVALID_KEY );
1733
1734#if defined(POLARSSL_ECP_MONTGOMERY)
1735 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1736 return( ecp_check_pubkey_mx( grp, pt ) );
1737#endif
1738#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1739 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1740 return( ecp_check_pubkey_sw( grp, pt ) );
1741#endif
1742 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1743}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001744
1745/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001746 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001747 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001748int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001749{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001750#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001751 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001752 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001753 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001754 if( mpi_get_bit( d, 0 ) != 0 ||
1755 mpi_get_bit( d, 1 ) != 0 ||
1756 mpi_get_bit( d, 2 ) != 0 ||
1757 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1758 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001759 else
1760 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001761 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001762#endif
1763#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1764 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001765 {
1766 /* see SEC1 3.2 */
1767 if( mpi_cmp_int( d, 1 ) < 0 ||
1768 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1769 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001770 else
1771 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001772 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001773#endif
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001774
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001775 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001776}
1777
1778/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001779 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001780 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001781int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001782 int (*f_rng)(void *, unsigned char *, size_t),
1783 void *p_rng )
1784{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001785 int ret;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001786 size_t n_size = (grp->nbits + 7) / 8;
1787
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001788#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001789 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001790 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001791 /* [M225] page 5 */
1792 size_t b;
1793
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001794 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001795
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001796 /* Make sure the most significant bit is nbits */
1797 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1798 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001799 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001800 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001801 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001802
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001803 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001804 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1805 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1806 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001807 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001808 else
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001809#endif
1810#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1811 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001812 {
1813 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001814 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001815 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1816
1817 /*
1818 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1819 * - use the same byte ordering;
1820 * - keep the leftmost nbits bits of the generated octet string;
1821 * - try until result is in the desired range.
1822 * This also avoids any biais, which is especially important for ECDSA.
1823 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001824 do
1825 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001826 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1827 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1828 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001829
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001830 /*
1831 * Each try has at worst a probability 1/2 of failing (the msb has
1832 * a probability 1/2 of being 0, and then the result will be < N),
1833 * so after 30 tries failure probability is a most 2**(-30).
1834 *
1835 * For most curves, 1 try is enough with overwhelming probability,
1836 * since N starts with a lot of 1s in binary, but some curves
1837 * such as secp224k1 are actually very close to the worst case.
1838 */
1839 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001840 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1841 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001842 while( mpi_cmp_int( d, 1 ) < 0 ||
1843 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001844 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001845 else
1846#endif
1847 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001848
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001849cleanup:
1850 if( ret != 0 )
1851 return( ret );
1852
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001853 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001854}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001855
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001856/*
1857 * Generate a keypair, prettier wrapper
1858 */
1859int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1860 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1861{
1862 int ret;
1863
1864 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1865 return( ret );
1866
1867 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1868}
1869
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001870#if defined(POLARSSL_SELF_TEST)
1871
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001872/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001873 * Checkup routine
1874 */
1875int ecp_self_test( int verbose )
1876{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001877 int ret;
1878 size_t i;
1879 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001880 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001881 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001882 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001883 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001884 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001885 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001886 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001887 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001888 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001889 "400000000000000000000000000000000000000000000000", /* one and zeros */
1890 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1891 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001892 };
1893
1894 ecp_group_init( &grp );
1895 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001896 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001897 mpi_init( &m );
1898
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001899 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001900#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001901 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001902#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001903 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1904#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001905
1906 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001907 printf( " ECP test #1 (constant op_count, base point G): " );
1908
1909 /* Do a dummy multiplication first to trigger precomputation */
1910 MPI_CHK( mpi_lset( &m, 2 ) );
1911 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001912
1913 add_count = 0;
1914 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001915 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001916 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001917 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001918
1919 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1920 {
1921 add_c_prev = add_count;
1922 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001923 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001924 add_count = 0;
1925 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001926 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001927
1928 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001929 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001930
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001931 if( add_count != add_c_prev ||
1932 dbl_count != dbl_c_prev ||
1933 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001934 {
1935 if( verbose != 0 )
Paul Bakkerec4bea72013-12-30 19:04:47 +01001936 printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001937
1938 ret = 1;
1939 goto cleanup;
1940 }
1941 }
1942
1943 if( verbose != 0 )
1944 printf( "passed\n" );
1945
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001946 if( verbose != 0 )
1947 printf( " ECP test #2 (constant op_count, other point): " );
1948 /* We computed P = 2G last time, use it */
1949
1950 add_count = 0;
1951 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001952 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001953 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1954 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1955
1956 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1957 {
1958 add_c_prev = add_count;
1959 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001960 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001961 add_count = 0;
1962 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001963 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001964
1965 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1966 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1967
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001968 if( add_count != add_c_prev ||
1969 dbl_count != dbl_c_prev ||
1970 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001971 {
1972 if( verbose != 0 )
Paul Bakkerec4bea72013-12-30 19:04:47 +01001973 printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001974
1975 ret = 1;
1976 goto cleanup;
1977 }
1978 }
1979
1980 if( verbose != 0 )
1981 printf( "passed\n" );
1982
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001983cleanup:
1984
1985 if( ret < 0 && verbose != 0 )
1986 printf( "Unexpected error, return code = %08X\n", ret );
1987
1988 ecp_group_free( &grp );
1989 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001990 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001991 mpi_free( &m );
1992
1993 if( verbose != 0 )
1994 printf( "\n" );
1995
1996 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001997}
1998
1999#endif
2000
2001#endif