blob: f4f2d1c13446493a9758629e89ff39ae52c88387 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 *
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010034 * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
35 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020036 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
37 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
38 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
39 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010040 *
41 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
42 * render ECC resistant against Side Channel Attacks. IACR Cryptology
43 * ePrint Archive, 2004, vol. 2004, p. 342.
44 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045 */
46
47#include "polarssl/config.h"
48
49#if defined(POLARSSL_ECP_C)
50
51#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052
Paul Bakker7dc4c442014-02-01 22:50:26 +010053#if defined(POLARSSL_PLATFORM_C)
54#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020055#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#define polarssl_printf printf
Paul Bakker6e339b52013-07-03 13:37:05 +020057#define polarssl_malloc malloc
58#define polarssl_free free
59#endif
60
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010061#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010062
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010063#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
64 !defined(EFI32)
65#define strcasecmp _stricmp
66#endif
67
Paul Bakker6a6087e2013-10-28 18:53:08 +010068#if defined(_MSC_VER) && !defined(inline)
69#define inline _inline
70#else
71#if defined(__ARMCC_VERSION) && !defined(inline)
72#define inline __inline
73#endif /* __ARMCC_VERSION */
74#endif /*_MSC_VER */
75
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010076#if defined(POLARSSL_SELF_TEST)
77/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010078 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020079 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010080 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010081static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010082#endif
83
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010084#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
85 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
86 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
87 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
88 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
89 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
90 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
Manuel Pégourié-Gonnard2a2ae642014-02-24 08:29:51 +010091 defined(POLARSSL_ECP_DP_BP512R1_ENABLED) || \
92 defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) || \
93 defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) || \
94 defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010095#define POLARSSL_ECP_SHORT_WEIERSTRASS
96#endif
97
98#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
99 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
100 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
101 defined(POLARSSL_ECP_DP_M511_ENABLED)
102#define POLARSSL_ECP_MONTGOMERY
103#endif
104
105/*
106 * Curve types: internal for now, might be exposed later
107 */
108typedef enum
109{
110 POLARSSL_ECP_TYPE_NONE = 0,
111 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
112 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
113} ecp_curve_type;
114
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100115/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200116 * List of supported curves:
117 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200118 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200119 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200120 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100121 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100122 * Curves are listed in order: largest curves first, and for a given size,
123 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200124 */
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100125static const ecp_curve_info ecp_supported_curves[POLARSSL_ECP_DP_MAX] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200126{
127#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200128 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200129#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100130#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
131 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
132#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200133#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200134 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200135#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100136#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
137 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
138#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200139#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200140 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200141#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100142#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
143 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
144#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100145#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
146 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
147#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200148#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200149 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200150#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100151#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
152 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
153#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100154#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
155 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
156#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100157#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
158 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
159#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200160 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200161};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100162
163static ecp_group_id ecp_supported_grp_id[POLARSSL_ECP_DP_MAX];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200164
165/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200166 * List of supported curves and associated info
167 */
168const ecp_curve_info *ecp_curve_list( void )
169{
170 return ecp_supported_curves;
171}
172
173/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100174 * List of supported curves, group ID only
175 */
176const ecp_group_id *ecp_grp_id_list( void )
177{
178 static int init_done = 0;
179
180 if( ! init_done )
181 {
182 size_t i = 0;
183 const ecp_curve_info *curve_info;
184
185 for( curve_info = ecp_curve_list();
186 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
187 curve_info++ )
188 {
189 ecp_supported_grp_id[i++] = curve_info->grp_id;
190 }
191 ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
192
193 init_done = 1;
194 }
195
196 return ecp_supported_grp_id;
197}
198
199/*
200 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200201 */
202const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
203{
204 const ecp_curve_info *curve_info;
205
206 for( curve_info = ecp_curve_list();
207 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
208 curve_info++ )
209 {
210 if( curve_info->grp_id == grp_id )
211 return( curve_info );
212 }
213
214 return( NULL );
215}
216
217/*
218 * Get the curve info from the TLS identifier
219 */
220const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
221{
222 const ecp_curve_info *curve_info;
223
224 for( curve_info = ecp_curve_list();
225 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
226 curve_info++ )
227 {
228 if( curve_info->tls_id == tls_id )
229 return( curve_info );
230 }
231
232 return( NULL );
233}
234
235/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100236 * Get the curve info from the name
237 */
238const ecp_curve_info *ecp_curve_info_from_name( const char *name )
239{
240 const ecp_curve_info *curve_info;
241
242 for( curve_info = ecp_curve_list();
243 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
244 curve_info++ )
245 {
246 if( strcasecmp( curve_info->name, name ) == 0 )
247 return( curve_info );
248 }
249
250 return( NULL );
251}
252
253/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100254 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100255 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100256static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100257{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100258 if( grp->G.X.p == NULL )
259 return( POLARSSL_ECP_TYPE_NONE );
260
261 if( grp->G.Y.p == NULL )
262 return( POLARSSL_ECP_TYPE_MONTGOMERY );
263 else
264 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100265}
266
267/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100268 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100269 */
270void ecp_point_init( ecp_point *pt )
271{
272 if( pt == NULL )
273 return;
274
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100275 mpi_init( &pt->X );
276 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100277 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100278}
279
280/*
281 * Initialize (the components of) a group
282 */
283void ecp_group_init( ecp_group *grp )
284{
285 if( grp == NULL )
286 return;
287
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200288 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100289}
290
291/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200292 * Initialize (the components of) a key pair
293 */
294void ecp_keypair_init( ecp_keypair *key )
295{
296 if ( key == NULL )
297 return;
298
299 ecp_group_init( &key->grp );
300 mpi_init( &key->d );
301 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200302}
303
304/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100305 * Unallocate (the components of) a point
306 */
307void ecp_point_free( ecp_point *pt )
308{
309 if( pt == NULL )
310 return;
311
312 mpi_free( &( pt->X ) );
313 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100314 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100315}
316
317/*
318 * Unallocate (the components of) a group
319 */
320void ecp_group_free( ecp_group *grp )
321{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200322 size_t i;
323
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100324 if( grp == NULL )
325 return;
326
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100327 if( grp->h != 1 )
328 {
329 mpi_free( &grp->P );
330 mpi_free( &grp->A );
331 mpi_free( &grp->B );
332 ecp_point_free( &grp->G );
333 mpi_free( &grp->N );
334 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200335
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200336 if( grp->T != NULL )
337 {
338 for( i = 0; i < grp->T_size; i++ )
339 ecp_point_free( &grp->T[i] );
340 polarssl_free( grp->T );
341 }
342
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200343 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100344}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100345
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100346/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200347 * Unallocate (the components of) a key pair
348 */
349void ecp_keypair_free( ecp_keypair *key )
350{
351 if ( key == NULL )
352 return;
353
354 ecp_group_free( &key->grp );
355 mpi_free( &key->d );
356 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200357}
358
359/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200360 * Copy the contents of a point
361 */
362int ecp_copy( ecp_point *P, const ecp_point *Q )
363{
364 int ret;
365
366 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
367 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
368 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
369
370cleanup:
371 return( ret );
372}
373
374/*
375 * Copy the contents of a group object
376 */
377int ecp_group_copy( ecp_group *dst, const ecp_group *src )
378{
379 return ecp_use_known_dp( dst, src->id );
380}
381
382/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100383 * Set point to zero
384 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100385int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100386{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100387 int ret;
388
389 MPI_CHK( mpi_lset( &pt->X , 1 ) );
390 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
391 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
392
393cleanup:
394 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100395}
396
397/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100398 * Tell if a point is zero
399 */
400int ecp_is_zero( ecp_point *pt )
401{
402 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
403}
404
405/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100406 * Import a non-zero point from ASCII strings
407 */
408int ecp_point_read_string( ecp_point *P, int radix,
409 const char *x, const char *y )
410{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100411 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100412
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100413 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
414 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100415 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100416
417cleanup:
418 return( ret );
419}
420
421/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100422 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100423 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100424int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100425 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100426 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100427{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200428 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100429 size_t plen;
430
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100431 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
432 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100433 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100434
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100435 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100436 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100437 */
438 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
439 {
440 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100441 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100442
443 buf[0] = 0x00;
444 *olen = 1;
445
446 return( 0 );
447 }
448
449 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100450
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100451 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
452 {
453 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100454
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100455 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100456 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100457
458 buf[0] = 0x04;
459 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
460 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
461 }
462 else if( format == POLARSSL_ECP_PF_COMPRESSED )
463 {
464 *olen = plen + 1;
465
466 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100467 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100468
469 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
470 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
471 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100472
473cleanup:
474 return( ret );
475}
476
477/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100478 * Import a point from unsigned binary data (SEC1 2.3.4)
479 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100480int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
481 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100482 int ret;
483 size_t plen;
484
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100485 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100486 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100487
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100488 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100489
490 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100491 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100492
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100493 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
494 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
495 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100496
497cleanup:
498 return( ret );
499}
500
501/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100502 * Import a point from a TLS ECPoint record (RFC 4492)
503 * struct {
504 * opaque point <1..2^8-1>;
505 * } ECPoint;
506 */
507int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100508 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100509{
510 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100511 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100512
513 /*
514 * We must have at least two bytes (1 for length, at least of for data)
515 */
516 if( buf_len < 2 )
517 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
518
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100519 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100520 if( data_len < 1 || data_len > buf_len - 1 )
521 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
522
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100523 /*
524 * Save buffer start for read_binary and update buf
525 */
526 buf_start = *buf;
527 *buf += data_len;
528
529 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100530}
531
532/*
533 * Export a point as a TLS ECPoint record (RFC 4492)
534 * struct {
535 * opaque point <1..2^8-1>;
536 * } ECPoint;
537 */
538int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100539 int format, size_t *olen,
540 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100541{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100542 int ret;
543
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100544 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100545 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100546 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100547 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100548 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
549
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100550 if( ( ret = ecp_point_write_binary( grp, pt, format,
551 olen, buf + 1, blen - 1) ) != 0 )
552 return( ret );
553
554 /*
555 * write length to the first byte and update total length
556 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200557 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100558 ++*olen;
559
560 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100561}
562
563/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200564 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200565 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200566int ecp_group_read_string( ecp_group *grp, int radix,
567 const char *p, const char *b,
568 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100569{
570 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100571
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100572 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100573 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
574 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
575 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
576
577 grp->pbits = mpi_msb( &grp->P );
578 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100579
580cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200581 if( ret != 0 )
582 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200583
584 return( ret );
585}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200586
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100587/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100588 * Set a group from an ECParameters record (RFC 4492)
589 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100590int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100591{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200592 uint16_t tls_id;
593 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100594
595 /*
596 * We expect at least three bytes (see below)
597 */
598 if( len < 3 )
599 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
600
601 /*
602 * First byte is curve_type; only named_curve is handled
603 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100604 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100605 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
606
607 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100608 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100609 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200610 tls_id = *(*buf)++;
611 tls_id <<= 8;
612 tls_id |= *(*buf)++;
613
614 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
615 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
616
617 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100618}
619
620/*
621 * Write the ECParameters record corresponding to a group (RFC 4492)
622 */
623int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
624 unsigned char *buf, size_t blen )
625{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200626 const ecp_curve_info *curve_info;
627
628 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
629 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200630
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100631 /*
632 * We are going to write 3 bytes (see below)
633 */
634 *olen = 3;
635 if( blen < *olen )
636 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
637
638 /*
639 * First byte is curve_type, always named_curve
640 */
641 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
642
643 /*
644 * Next two bytes are the namedcurve value
645 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200646 buf[0] = curve_info->tls_id >> 8;
647 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100648
649 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100650}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100651
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200652/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200653 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
654 * See the documentation of struct ecp_group.
655 *
656 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200657 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200658static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200659{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200660 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200661
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200662 if( grp->modp == NULL )
663 return( mpi_mod_mpi( N, N, &grp->P ) );
664
665 /* N->s < 0 is a much faster test, which fails only if N is 0 */
666 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
667 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200668 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200669 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200670 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200671
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200672 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200673
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200674 /* N->s < 0 is a much faster test, which fails only if N is 0 */
675 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
676 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200677
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200678 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
679 /* we known P, N and the result are positive */
680 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200681
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200682cleanup:
683 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200684}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200685
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100686/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100687 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100688 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100689 * In order to guarantee that, we need to ensure that operands of
690 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100691 * bring the result back to this range.
692 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100693 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100694 */
695
696/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100697 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
698 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100699#if defined(POLARSSL_SELF_TEST)
700#define INC_MUL_COUNT mul_count++;
701#else
702#define INC_MUL_COUNT
703#endif
704
705#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
706 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100707
708/*
709 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200710 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100711 */
712#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200713 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100714 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
715
716/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200717 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
718 * We known P, N and the result are positive, so sub_abs is correct, and
719 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100720 */
721#define MOD_ADD( N ) \
722 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200723 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100724
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100725#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
726/*
727 * For curves in short Weierstrass form, we do all the internal operations in
728 * Jacobian coordinates.
729 *
730 * For multiplication, we'll use a comb method with coutermeasueres against
731 * SPA, hence timing attacks.
732 */
733
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100734/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100735 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100736 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100737 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100738static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100739{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100740 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100741 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100742
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100743 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100744 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100745
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100746 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100747
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100748 /*
749 * X = X / Z^2 mod p
750 */
751 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
752 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
753 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100754
755 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100756 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100757 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100758 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
759 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100760
761 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100762 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100763 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100764 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100765
766cleanup:
767
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100768 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100769
770 return( ret );
771}
772
773/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100774 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100775 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100776 * (See for example Cohen's "A Course in Computational Algebraic Number
777 * Theory", Algorithm 10.3.4.)
778 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200779 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100780 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100781 *
782 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100783 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100784static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100785 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100786{
787 int ret;
788 size_t i;
789 mpi *c, u, Zi, ZZi;
790
791 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100792 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100793
Paul Bakker6e339b52013-07-03 13:37:05 +0200794 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200795 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100796
797 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
798 for( i = 0; i < t_len; i++ )
799 mpi_init( &c[i] );
800
801 /*
802 * c[i] = Z_0 * ... * Z_i
803 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100804 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100805 for( i = 1; i < t_len; i++ )
806 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100807 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100808 MOD_MUL( c[i] );
809 }
810
811 /*
812 * u = 1 / (Z_0 * ... * Z_n) mod P
813 */
814 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
815
816 for( i = t_len - 1; ; i-- )
817 {
818 /*
819 * Zi = 1 / Z_i mod p
820 * u = 1 / (Z_0 * ... * Z_i) mod P
821 */
822 if( i == 0 ) {
823 MPI_CHK( mpi_copy( &Zi, &u ) );
824 }
825 else
826 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100827 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
828 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100829 }
830
831 /*
832 * proceed as in normalize()
833 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100834 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
835 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
836 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
837 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 +0100838
839 /*
840 * Post-precessing: reclaim some memory by shrinking coordinates
841 * - not storing Z (always 1)
842 * - shrinking other coordinates, but still keeping the same number of
843 * limbs as P, as otherwise it will too likely be regrown too fast.
844 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100845 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
846 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100847 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100848
849 if( i == 0 )
850 break;
851 }
852
853cleanup:
854
855 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
856 for( i = 0; i < t_len; i++ )
857 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200858 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100859
860 return( ret );
861}
862
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100863/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100864 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
865 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
866 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100867static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100868 ecp_point *Q,
869 unsigned char inv )
870{
871 int ret;
872 unsigned char nonzero;
873 mpi mQY;
874
875 mpi_init( &mQY );
876
877 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
878 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
879 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
880 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
881
882cleanup:
883 mpi_free( &mQY );
884
885 return( ret );
886}
887
888/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200889 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200890 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200891 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200892 * with heavy variable renaming, some reordering and one minor modification
893 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
894 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100895 *
896 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200897 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200898static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
899 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200900{
901 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200902 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200903
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200904#if defined(POLARSSL_SELF_TEST)
905 dbl_count++;
906#endif
907
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200908 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
909 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200910
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200911 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
912 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
913 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
914 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
915 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
916 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
917 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
918 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
919 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
920 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
921 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100922
923 /* Special case for A = -3 */
924 if( grp->A.p == NULL )
925 {
926 MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
927 X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
928 MOD_SUB( X3 );
929 }
930 else
931 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
932
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200933 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
934 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
935 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
936 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
937 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
938 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
939 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
940 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
941 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
942 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
943 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
944 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200945
946 MPI_CHK( mpi_copy( &R->X, &X3 ) );
947 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
948 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
949
950cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200951 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
952 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200953
954 return( ret );
955}
956
957/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100958 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100959 *
960 * The coordinates of Q must be normalized (= affine),
961 * but those of P don't need to. R is not normalized.
962 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100963 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100964 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100965 * - at each step, P, Q and R are multiples of the base point, the factor
966 * being less than its order, so none of them is zero;
967 * - Q is an odd multiple of the base point, P an even multiple,
968 * due to the choice of precomputed points in the modified comb method.
969 * So branches for these cases do not leak secret information.
970 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100971 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
972 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100973 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100974 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100975static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100976 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100977{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100978 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100979 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100980
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100981#if defined(POLARSSL_SELF_TEST)
982 add_count++;
983#endif
984
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100985 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100986 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100987 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100988 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
989 return( ecp_copy( R, Q ) );
990
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100991 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100992 return( ecp_copy( R, P ) );
993
994 /*
995 * Make sure Q coordinates are normalized
996 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100997 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200998 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100999
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001000 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1001 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001002
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001003 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1004 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1005 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1006 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1007 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1008 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001009
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001010 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001011 if( mpi_cmp_int( &T1, 0 ) == 0 )
1012 {
1013 if( mpi_cmp_int( &T2, 0 ) == 0 )
1014 {
1015 ret = ecp_double_jac( grp, R, P );
1016 goto cleanup;
1017 }
1018 else
1019 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001020 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001021 goto cleanup;
1022 }
1023 }
1024
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001025 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1026 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1027 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1028 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1029 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1030 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1031 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1032 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1033 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1034 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1035 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1036 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001037
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001038 MPI_CHK( mpi_copy( &R->X, &X ) );
1039 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1040 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001041
1042cleanup:
1043
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001044 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1045 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001046
1047 return( ret );
1048}
1049
1050/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001051 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001052 */
1053int ecp_add( const ecp_group *grp, ecp_point *R,
1054 const ecp_point *P, const ecp_point *Q )
1055{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001056 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001057
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001058 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001059 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1060
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001061 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001062 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001063
1064cleanup:
1065 return( ret );
1066}
1067
1068/*
1069 * Subtraction: R = P - Q, result's coordinates normalized
1070 */
1071int ecp_sub( const ecp_group *grp, ecp_point *R,
1072 const ecp_point *P, const ecp_point *Q )
1073{
1074 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001075 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001076
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001077 ecp_point_init( &mQ );
1078
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001079 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001080 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1081
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001082 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001083 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001084 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1085 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1086
1087 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001088 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001089
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001090cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001091 ecp_point_free( &mQ );
1092
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001093 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001094}
1095
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001096/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001097 * Randomize jacobian coordinates:
1098 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001099 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001100 *
1101 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001102 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001103static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001104 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1105{
1106 int ret;
1107 mpi l, ll;
1108 size_t p_size = (grp->pbits + 7) / 8;
1109 int count = 0;
1110
1111 mpi_init( &l ); mpi_init( &ll );
1112
1113 /* Generate l such that 1 < l < p */
1114 do
1115 {
1116 mpi_fill_random( &l, p_size, f_rng, p_rng );
1117
1118 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1119 mpi_shift_r( &l, 1 );
1120
1121 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001122 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001123 }
1124 while( mpi_cmp_int( &l, 1 ) <= 0 );
1125
1126 /* Z = l * Z */
1127 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1128
1129 /* X = l^2 * X */
1130 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1131 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1132
1133 /* Y = l^3 * Y */
1134 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1135 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1136
1137cleanup:
1138 mpi_free( &l ); mpi_free( &ll );
1139
1140 return( ret );
1141}
1142
1143/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001144 * Check and define parameters used by the comb method (see below for details)
1145 */
1146#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1147#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1148#endif
1149
1150/* d = ceil( n / w ) */
1151#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1152
1153/* number of precomputed points */
1154#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1155
1156/*
1157 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001158 *
1159 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001160 * modified version that provides resistance to SPA by avoiding zero
1161 * digits in the representation as in [3]. We modify the method further by
1162 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001163 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001164 *
1165 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1166 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1167 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001168 *
1169 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001170 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001171 * - w is the size, ie number of teeth, of the comb, and must be between
1172 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001173 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1174 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001175 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001176static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001177 unsigned char w, const mpi *m )
1178{
1179 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001180 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001181
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001182 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001183
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001184 /* First get the classical comb values (except for x_d = 0) */
1185 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001186 for( j = 0; j < w; j++ )
1187 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1188
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001189 /* Now make sure x_1 .. x_d are odd */
1190 c = 0;
1191 for( i = 1; i <= d; i++ )
1192 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001193 /* Add carry and update it */
1194 cc = x[i] & c;
1195 x[i] = x[i] ^ c;
1196 c = cc;
1197
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001198 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001199 adjust = 1 - ( x[i] & 0x01 );
1200 c |= x[i] & ( x[i-1] * adjust );
1201 x[i] = x[i] ^ ( x[i-1] * adjust );
1202 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001203 }
1204}
1205
1206/*
1207 * Precompute points for the comb method
1208 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001209 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1210 * 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 +01001211 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001212 * T must be able to hold 2^{w - 1} elements
1213 *
1214 * 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 +01001215 */
1216static int ecp_precompute_comb( const ecp_group *grp,
1217 ecp_point T[], const ecp_point *P,
1218 unsigned char w, size_t d )
1219{
1220 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001221 unsigned char i, k;
1222 size_t j;
1223 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001224
1225 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001226 * Set T[0] = P and
1227 * 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 +01001228 */
1229 MPI_CHK( ecp_copy( &T[0], P ) );
1230
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001231 k = 0;
1232 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001233 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001234 cur = T + i;
1235 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001236 for( j = 0; j < d; j++ )
1237 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001238
1239 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001240 }
1241
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001242 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001243
1244 /*
1245 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001246 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001247 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001248 k = 0;
1249 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001250 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001251 j = i;
1252 while( j-- )
1253 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001254 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001255 TT[k++] = &T[i + j];
1256 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001257 }
1258
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001259 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001260
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001261cleanup:
1262 return( ret );
1263}
1264
1265/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001266 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001267 */
1268static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001269 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001270 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001271{
1272 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001273 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001274
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001275 /* Ignore the "sign" bit and scale down */
1276 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001277
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001278 /* Read the whole table to thwart cache-based timing attacks */
1279 for( j = 0; j < t_len; j++ )
1280 {
1281 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1282 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1283 }
1284
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001285 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001286 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001287
1288cleanup:
1289 return( ret );
1290}
1291
1292/*
1293 * Core multiplication algorithm for the (modified) comb method.
1294 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001295 *
1296 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001297 */
1298static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001299 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001300 const unsigned char x[], size_t d,
1301 int (*f_rng)(void *, unsigned char *, size_t),
1302 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001303{
1304 int ret;
1305 ecp_point Txi;
1306 size_t i;
1307
1308 ecp_point_init( &Txi );
1309
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001310 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001311 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001312 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001313 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001314 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001315 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001316
1317 while( i-- != 0 )
1318 {
1319 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001320 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001321 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001322 }
1323
1324cleanup:
1325 ecp_point_free( &Txi );
1326
1327 return( ret );
1328}
1329
1330/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001331 * Multiplication using the comb method,
1332 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001333 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001334static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1335 const mpi *m, const ecp_point *P,
1336 int (*f_rng)(void *, unsigned char *, size_t),
1337 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001338{
1339 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001340 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1341 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001342 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001343 ecp_point *T;
1344 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001345
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001346 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001347 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001348
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001349 /* we need N to be odd to trnaform m in an odd number, check now */
1350 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1351 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1352
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001353 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001354 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001355 * 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 +01001356 * (see costs of the various parts, with 1S = 1M)
1357 */
1358 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001359
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001360 /*
1361 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001362 * Just adding one avoids upping the cost of the first mul too much,
1363 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001364 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001365#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001366 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1367 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001368 if( p_eq_g )
1369 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001370#else
1371 p_eq_g = 0;
1372#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001373
1374 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001375 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001376 * (The last test is useful only for very small curves in the test suite.)
1377 */
1378 if( w > POLARSSL_ECP_WINDOW_SIZE )
1379 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001380 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001381 w = 2;
1382
1383 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001384 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001385 d = ( grp->nbits + w - 1 ) / w;
1386
1387 /*
1388 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001389 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001390 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001391 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001392
1393 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001394 {
1395 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1396 if( T == NULL )
1397 {
1398 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1399 goto cleanup;
1400 }
1401
1402 for( i = 0; i < pre_len; i++ )
1403 ecp_point_init( &T[i] );
1404
1405 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1406
1407 if( p_eq_g )
1408 {
1409 grp->T = T;
1410 grp->T_size = pre_len;
1411 }
1412 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001413
1414 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001415 * Make sure M is odd (M = m or M = N - m, since N is odd)
1416 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001417 */
1418 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001419 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001420 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1421 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001422
1423 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001424 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001425 */
1426 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001427 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 +01001428
1429 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001430 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001431 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001432 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1433 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001434
1435cleanup:
1436
1437 if( T != NULL && ! p_eq_g )
1438 {
1439 for( i = 0; i < pre_len; i++ )
1440 ecp_point_free( &T[i] );
1441 polarssl_free( T );
1442 }
1443
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001444 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001445 mpi_free( &mm );
1446
1447 if( ret != 0 )
1448 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001449
1450 return( ret );
1451}
1452
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001453#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1454
1455#if defined(POLARSSL_ECP_MONTGOMERY)
1456/*
1457 * For Montgomery curves, we do all the internal arithmetic in projective
1458 * coordinates. Import/export of points uses only the x coordinates, which is
1459 * internaly represented as X / Z.
1460 *
1461 * For scalar multiplication, we'll use a Montgomery ladder.
1462 */
1463
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001464/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001465 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1466 * Cost: 1M + 1I
1467 */
1468static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1469{
1470 int ret;
1471
1472 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1473 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1474 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1475
1476cleanup:
1477 return( ret );
1478}
1479
1480/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001481 * Randomize projective x/z coordinates:
1482 * (X, Z) -> (l X, l Z) for random l
1483 * This is sort of the reverse operation of ecp_normalize_mxz().
1484 *
1485 * This countermeasure was first suggested in [2].
1486 * Cost: 2M
1487 */
1488static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1489 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1490{
1491 int ret;
1492 mpi l;
1493 size_t p_size = (grp->pbits + 7) / 8;
1494 int count = 0;
1495
1496 mpi_init( &l );
1497
1498 /* Generate l such that 1 < l < p */
1499 do
1500 {
1501 mpi_fill_random( &l, p_size, f_rng, p_rng );
1502
1503 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1504 mpi_shift_r( &l, 1 );
1505
1506 if( count++ > 10 )
1507 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1508 }
1509 while( mpi_cmp_int( &l, 1 ) <= 0 );
1510
1511 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1512 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1513
1514cleanup:
1515 mpi_free( &l );
1516
1517 return( ret );
1518}
1519
1520/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001521 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1522 * for Montgomery curves in x/z coordinates.
1523 *
1524 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1525 * with
1526 * d = X1
1527 * P = (X2, Z2)
1528 * Q = (X3, Z3)
1529 * R = (X4, Z4)
1530 * S = (X5, Z5)
1531 * and eliminating temporary variables tO, ..., t4.
1532 *
1533 * Cost: 5M + 4S
1534 */
1535static int ecp_double_add_mxz( const ecp_group *grp,
1536 ecp_point *R, ecp_point *S,
1537 const ecp_point *P, const ecp_point *Q,
1538 const mpi *d )
1539{
1540 int ret;
1541 mpi A, AA, B, BB, E, C, D, DA, CB;
1542
1543 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1544 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1545 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1546
1547 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1548 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1549 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1550 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1551 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1552 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1553 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1554 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1555 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1556 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1557 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1558 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1559 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1560 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1561 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1562 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1563 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1564 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1565
1566cleanup:
1567 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1568 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1569 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1570
1571 return( ret );
1572}
1573
1574/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001575 * Multiplication with Montgomery ladder in x/z coordinates,
1576 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001577 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001578static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1579 const mpi *m, const ecp_point *P,
1580 int (*f_rng)(void *, unsigned char *, size_t),
1581 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001582{
1583 int ret;
1584 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001585 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001586 ecp_point RP;
1587 mpi PX;
1588
1589 ecp_point_init( &RP ); mpi_init( &PX );
1590
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001591 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001592 mpi_copy( &PX, &P->X );
1593 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001594
1595 /* Set R to zero in modified x/z coordinates */
1596 MPI_CHK( mpi_lset( &R->X, 1 ) );
1597 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1598 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001599
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001600 /* RP.X might be sligtly larger than P, so reduce it */
1601 MOD_ADD( RP.X );
1602
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001603 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001604 if( f_rng != NULL )
1605 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001606
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001607 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001608 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001609 while( i-- > 0 )
1610 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001611 b = mpi_get_bit( m, i );
1612 /*
1613 * if (b) R = 2R + P else R = 2R,
1614 * which is:
1615 * if (b) double_add( RP, R, RP, R )
1616 * else double_add( R, RP, R, RP )
1617 * but using safe conditional swaps to avoid leaks
1618 */
1619 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1620 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1621 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1622 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1623 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001624 }
1625
1626 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1627
1628cleanup:
1629 ecp_point_free( &RP ); mpi_free( &PX );
1630
1631 return( ret );
1632}
1633
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001634#endif /* POLARSSL_ECP_MONTGOMERY */
1635
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001636/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001637 * Multiplication R = m * P
1638 */
1639int ecp_mul( ecp_group *grp, ecp_point *R,
1640 const mpi *m, const ecp_point *P,
1641 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1642{
1643 int ret;
1644
1645 /* Common sanity checks */
1646 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1647 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1648
1649 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1650 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1651 return( ret );
1652
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001653#if defined(POLARSSL_ECP_MONTGOMERY)
1654 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001655 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001656#endif
1657#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1658 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001659 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001660#endif
1661 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001662}
1663
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001664#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001665/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001666 * Check that an affine point is valid as a public key,
1667 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001668 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001669static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001670{
1671 int ret;
1672 mpi YY, RHS;
1673
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001674 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001675 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1676 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1677 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1678 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001679 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001680
1681 mpi_init( &YY ); mpi_init( &RHS );
1682
1683 /*
1684 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001685 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001686 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001687 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1688 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001689
1690 /* Special case for A = -3 */
1691 if( grp->A.p == NULL )
1692 {
1693 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1694 }
1695 else
1696 {
1697 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1698 }
1699
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001700 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1701 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001702
1703 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001704 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001705
1706cleanup:
1707
1708 mpi_free( &YY ); mpi_free( &RHS );
1709
1710 return( ret );
1711}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001712#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1713
1714
1715#if defined(POLARSSL_ECP_MONTGOMERY)
1716/*
1717 * Check validity of a public key for Montgomery curves with x-only schemes
1718 */
1719static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1720{
1721 /* [M255 p. 5] Just check X is the correct number of bytes */
1722 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1723 return( POLARSSL_ERR_ECP_INVALID_KEY );
1724
1725 return( 0 );
1726}
1727#endif /* POLARSSL_ECP_MONTGOMERY */
1728
1729/*
1730 * Check that a point is valid as a public key
1731 */
1732int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1733{
1734 /* Must use affine coordinates */
1735 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1736 return( POLARSSL_ERR_ECP_INVALID_KEY );
1737
1738#if defined(POLARSSL_ECP_MONTGOMERY)
1739 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1740 return( ecp_check_pubkey_mx( grp, pt ) );
1741#endif
1742#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1743 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1744 return( ecp_check_pubkey_sw( grp, pt ) );
1745#endif
1746 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1747}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001748
1749/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001750 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001751 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001752int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001753{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001754#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001755 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001756 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001757 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001758 if( mpi_get_bit( d, 0 ) != 0 ||
1759 mpi_get_bit( d, 1 ) != 0 ||
1760 mpi_get_bit( d, 2 ) != 0 ||
1761 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1762 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001763 else
1764 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001765 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001766#endif
1767#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1768 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001769 {
1770 /* see SEC1 3.2 */
1771 if( mpi_cmp_int( d, 1 ) < 0 ||
1772 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1773 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001774 else
1775 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001776 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001777#endif
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001778
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001779 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001780}
1781
1782/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001783 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001784 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001785int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001786 int (*f_rng)(void *, unsigned char *, size_t),
1787 void *p_rng )
1788{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001789 int ret;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001790 size_t n_size = (grp->nbits + 7) / 8;
1791
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001792#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001793 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001794 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001795 /* [M225] page 5 */
1796 size_t b;
1797
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001798 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001799
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001800 /* Make sure the most significant bit is nbits */
1801 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1802 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001803 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001804 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001805 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001806
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001807 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001808 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1809 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1810 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001811 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001812 else
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001813#endif
1814#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1815 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001816 {
1817 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001818 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001819 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1820
1821 /*
1822 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1823 * - use the same byte ordering;
1824 * - keep the leftmost nbits bits of the generated octet string;
1825 * - try until result is in the desired range.
1826 * This also avoids any biais, which is especially important for ECDSA.
1827 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001828 do
1829 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001830 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1831 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1832 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001833
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001834 /*
1835 * Each try has at worst a probability 1/2 of failing (the msb has
1836 * a probability 1/2 of being 0, and then the result will be < N),
1837 * so after 30 tries failure probability is a most 2**(-30).
1838 *
1839 * For most curves, 1 try is enough with overwhelming probability,
1840 * since N starts with a lot of 1s in binary, but some curves
1841 * such as secp224k1 are actually very close to the worst case.
1842 */
1843 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001844 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1845 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001846 while( mpi_cmp_int( d, 1 ) < 0 ||
1847 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001848 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001849 else
1850#endif
1851 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001852
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001853cleanup:
1854 if( ret != 0 )
1855 return( ret );
1856
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001857 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001858}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001859
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001860/*
1861 * Generate a keypair, prettier wrapper
1862 */
1863int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1864 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1865{
1866 int ret;
1867
1868 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1869 return( ret );
1870
1871 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1872}
1873
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001874#if defined(POLARSSL_SELF_TEST)
1875
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001876/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001877 * Checkup routine
1878 */
1879int ecp_self_test( int verbose )
1880{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001881 int ret;
1882 size_t i;
1883 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001884 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001885 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001886 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001887 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001888 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001889 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001890 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001891 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001892 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001893 "400000000000000000000000000000000000000000000000", /* one and zeros */
1894 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1895 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001896 };
1897
1898 ecp_group_init( &grp );
1899 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001900 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001901 mpi_init( &m );
1902
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001903 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001904#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001905 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001906#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001907 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1908#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001909
1910 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001911 polarssl_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001912
1913 /* Do a dummy multiplication first to trigger precomputation */
1914 MPI_CHK( mpi_lset( &m, 2 ) );
1915 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001916
1917 add_count = 0;
1918 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001919 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001920 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001921 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001922
1923 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1924 {
1925 add_c_prev = add_count;
1926 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001927 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001928 add_count = 0;
1929 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001930 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001931
1932 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001933 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001934
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001935 if( add_count != add_c_prev ||
1936 dbl_count != dbl_c_prev ||
1937 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001938 {
1939 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001940 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001941
1942 ret = 1;
1943 goto cleanup;
1944 }
1945 }
1946
1947 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001948 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001949
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001950 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001951 polarssl_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001952 /* We computed P = 2G last time, use it */
1953
1954 add_count = 0;
1955 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001956 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001957 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1958 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1959
1960 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1961 {
1962 add_c_prev = add_count;
1963 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001964 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001965 add_count = 0;
1966 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001967 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001968
1969 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1970 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1971
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001972 if( add_count != add_c_prev ||
1973 dbl_count != dbl_c_prev ||
1974 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001975 {
1976 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001977 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001978
1979 ret = 1;
1980 goto cleanup;
1981 }
1982 }
1983
1984 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001985 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001986
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001987cleanup:
1988
1989 if( ret < 0 && verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001990 polarssl_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001991
1992 ecp_group_free( &grp );
1993 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001994 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001995 mpi_free( &m );
1996
1997 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001998 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001999
2000 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002001}
2002
2003#endif
2004
2005#endif