blob: 1bb8dfe250af7cfb63139bbb32c59cb121f91af4 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01007 *
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23/*
24 * References:
25 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010026 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010027 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010028 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010029 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020030 *
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010031 * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
32 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
34 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
35 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
36 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010037 *
38 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
39 * render ECC resistant against Side Channel Attacks. IACR Cryptology
40 * ePrint Archive, 2004, vol. 2004, p. 342.
41 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010042 */
43
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020044#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020046#else
47#include POLARSSL_CONFIG_FILE
48#endif
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010049
50#if defined(POLARSSL_ECP_C)
51
52#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020053
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <string.h>
55
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#if defined(POLARSSL_PLATFORM_C)
57#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020058#else
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <stdlib.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010060#define polarssl_printf printf
Paul Bakker6e339b52013-07-03 13:37:05 +020061#define polarssl_malloc malloc
62#define polarssl_free free
63#endif
64
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010065#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
66 !defined(EFI32)
67#define strcasecmp _stricmp
68#endif
69
Paul Bakker6a6087e2013-10-28 18:53:08 +010070#if defined(_MSC_VER) && !defined(inline)
71#define inline _inline
72#else
73#if defined(__ARMCC_VERSION) && !defined(inline)
74#define inline __inline
75#endif /* __ARMCC_VERSION */
76#endif /*_MSC_VER */
77
Paul Bakker34617722014-06-13 17:20:13 +020078/* Implementation that should never be optimized out by the compiler */
79static void polarssl_zeroize( void *v, size_t n ) {
80 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
81}
82
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010083#if defined(POLARSSL_SELF_TEST)
84/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010085 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020086 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010087 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010088static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010089#endif
90
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010091#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
92 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
93 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
94 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
95 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
96 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
97 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
Manuel Pégourié-Gonnard2a2ae642014-02-24 08:29:51 +010098 defined(POLARSSL_ECP_DP_BP512R1_ENABLED) || \
99 defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) || \
100 defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) || \
101 defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100102#define POLARSSL_ECP_SHORT_WEIERSTRASS
103#endif
104
105#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
106 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
107 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
108 defined(POLARSSL_ECP_DP_M511_ENABLED)
109#define POLARSSL_ECP_MONTGOMERY
110#endif
111
112/*
113 * Curve types: internal for now, might be exposed later
114 */
115typedef enum
116{
117 POLARSSL_ECP_TYPE_NONE = 0,
118 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
119 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
120} ecp_curve_type;
121
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100122/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200123 * List of supported curves:
124 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200125 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200126 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200127 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100128 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100129 * Curves are listed in order: largest curves first, and for a given size,
130 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200131 */
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200132static const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200133{
134#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200135 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200136#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100137#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
138 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
139#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200140#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200141 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200142#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100143#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
144 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
145#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200146#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200147 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200148#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100149#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
150 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
151#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100152#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
153 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
154#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200155#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200156 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200157#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100158#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
159 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
160#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100161#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
162 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
163#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100164#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
165 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
166#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200167 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200168};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100169
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200170#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
171 sizeof( ecp_supported_curves[0] )
172
173static ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200174
175/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200176 * List of supported curves and associated info
177 */
178const ecp_curve_info *ecp_curve_list( void )
179{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200180 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200181}
182
183/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100184 * List of supported curves, group ID only
185 */
186const ecp_group_id *ecp_grp_id_list( void )
187{
188 static int init_done = 0;
189
190 if( ! init_done )
191 {
192 size_t i = 0;
193 const ecp_curve_info *curve_info;
194
195 for( curve_info = ecp_curve_list();
196 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
197 curve_info++ )
198 {
199 ecp_supported_grp_id[i++] = curve_info->grp_id;
200 }
201 ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
202
203 init_done = 1;
204 }
205
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200206 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100207}
208
209/*
210 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200211 */
212const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
213{
214 const ecp_curve_info *curve_info;
215
216 for( curve_info = ecp_curve_list();
217 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
218 curve_info++ )
219 {
220 if( curve_info->grp_id == grp_id )
221 return( curve_info );
222 }
223
224 return( NULL );
225}
226
227/*
228 * Get the curve info from the TLS identifier
229 */
230const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
231{
232 const ecp_curve_info *curve_info;
233
234 for( curve_info = ecp_curve_list();
235 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
236 curve_info++ )
237 {
238 if( curve_info->tls_id == tls_id )
239 return( curve_info );
240 }
241
242 return( NULL );
243}
244
245/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100246 * Get the curve info from the name
247 */
248const ecp_curve_info *ecp_curve_info_from_name( const char *name )
249{
250 const ecp_curve_info *curve_info;
251
252 for( curve_info = ecp_curve_list();
253 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
254 curve_info++ )
255 {
256 if( strcasecmp( curve_info->name, name ) == 0 )
257 return( curve_info );
258 }
259
260 return( NULL );
261}
262
263/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100264 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100265 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100266static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100267{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100268 if( grp->G.X.p == NULL )
269 return( POLARSSL_ECP_TYPE_NONE );
270
271 if( grp->G.Y.p == NULL )
272 return( POLARSSL_ECP_TYPE_MONTGOMERY );
273 else
274 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100275}
276
277/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100278 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100279 */
280void ecp_point_init( ecp_point *pt )
281{
282 if( pt == NULL )
283 return;
284
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100285 mpi_init( &pt->X );
286 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100287 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100288}
289
290/*
291 * Initialize (the components of) a group
292 */
293void ecp_group_init( ecp_group *grp )
294{
295 if( grp == NULL )
296 return;
297
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200298 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100299}
300
301/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200302 * Initialize (the components of) a key pair
303 */
304void ecp_keypair_init( ecp_keypair *key )
305{
Paul Bakker66d5d072014-06-17 16:39:18 +0200306 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200307 return;
308
309 ecp_group_init( &key->grp );
310 mpi_init( &key->d );
311 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200312}
313
314/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100315 * Unallocate (the components of) a point
316 */
317void ecp_point_free( ecp_point *pt )
318{
319 if( pt == NULL )
320 return;
321
322 mpi_free( &( pt->X ) );
323 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100324 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100325}
326
327/*
328 * Unallocate (the components of) a group
329 */
330void ecp_group_free( ecp_group *grp )
331{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200332 size_t i;
333
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100334 if( grp == NULL )
335 return;
336
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100337 if( grp->h != 1 )
338 {
339 mpi_free( &grp->P );
340 mpi_free( &grp->A );
341 mpi_free( &grp->B );
342 ecp_point_free( &grp->G );
343 mpi_free( &grp->N );
344 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200345
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200346 if( grp->T != NULL )
347 {
348 for( i = 0; i < grp->T_size; i++ )
349 ecp_point_free( &grp->T[i] );
350 polarssl_free( grp->T );
351 }
352
Paul Bakker34617722014-06-13 17:20:13 +0200353 polarssl_zeroize( grp, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100354}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100355
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100356/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200357 * Unallocate (the components of) a key pair
358 */
359void ecp_keypair_free( ecp_keypair *key )
360{
Paul Bakker66d5d072014-06-17 16:39:18 +0200361 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200362 return;
363
364 ecp_group_free( &key->grp );
365 mpi_free( &key->d );
366 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200367}
368
369/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200370 * Copy the contents of a point
371 */
372int ecp_copy( ecp_point *P, const ecp_point *Q )
373{
374 int ret;
375
376 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
377 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
378 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
379
380cleanup:
381 return( ret );
382}
383
384/*
385 * Copy the contents of a group object
386 */
387int ecp_group_copy( ecp_group *dst, const ecp_group *src )
388{
389 return ecp_use_known_dp( dst, src->id );
390}
391
392/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100393 * Set point to zero
394 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100395int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100396{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100397 int ret;
398
399 MPI_CHK( mpi_lset( &pt->X , 1 ) );
400 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
401 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
402
403cleanup:
404 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100405}
406
407/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100408 * Tell if a point is zero
409 */
410int ecp_is_zero( ecp_point *pt )
411{
412 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
413}
414
415/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100416 * Import a non-zero point from ASCII strings
417 */
418int ecp_point_read_string( ecp_point *P, int radix,
419 const char *x, const char *y )
420{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100421 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100422
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100423 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
424 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100425 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100426
427cleanup:
428 return( ret );
429}
430
431/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100432 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100433 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100434int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100435 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100436 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100437{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200438 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100439 size_t plen;
440
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100441 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
442 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100443 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100444
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100445 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100446 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100447 */
448 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
449 {
450 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100451 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100452
453 buf[0] = 0x00;
454 *olen = 1;
455
456 return( 0 );
457 }
458
459 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100460
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100461 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
462 {
463 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100464
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100465 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100466 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100467
468 buf[0] = 0x04;
469 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
470 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
471 }
472 else if( format == POLARSSL_ECP_PF_COMPRESSED )
473 {
474 *olen = plen + 1;
475
476 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100477 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100478
479 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
480 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
481 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100482
483cleanup:
484 return( ret );
485}
486
487/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100488 * Import a point from unsigned binary data (SEC1 2.3.4)
489 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100490int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100491 const unsigned char *buf, size_t ilen )
492{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100493 int ret;
494 size_t plen;
495
Paul Bakker82788fb2014-10-20 13:59:19 +0200496 if( ilen < 1 )
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200497 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
498
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100499 if( buf[0] == 0x00 )
500 {
501 if( ilen == 1 )
502 return( ecp_set_zero( pt ) );
503 else
504 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
505 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100506
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100507 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100508
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100509 if( buf[0] != 0x04 )
510 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
511
512 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100513 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100514
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100515 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
516 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
517 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100518
519cleanup:
520 return( ret );
521}
522
523/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100524 * Import a point from a TLS ECPoint record (RFC 4492)
525 * struct {
526 * opaque point <1..2^8-1>;
527 * } ECPoint;
528 */
529int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100530 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100531{
532 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100533 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100534
535 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200536 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100537 */
538 if( buf_len < 2 )
539 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
540
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100541 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100542 if( data_len < 1 || data_len > buf_len - 1 )
543 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
544
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100545 /*
546 * Save buffer start for read_binary and update buf
547 */
548 buf_start = *buf;
549 *buf += data_len;
550
551 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100552}
553
554/*
555 * Export a point as a TLS ECPoint record (RFC 4492)
556 * struct {
557 * opaque point <1..2^8-1>;
558 * } ECPoint;
559 */
560int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100561 int format, size_t *olen,
562 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100563{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100564 int ret;
565
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100566 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100567 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100568 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100569 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100570 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
571
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100572 if( ( ret = ecp_point_write_binary( grp, pt, format,
573 olen, buf + 1, blen - 1) ) != 0 )
574 return( ret );
575
576 /*
577 * write length to the first byte and update total length
578 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200579 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100580 ++*olen;
581
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200582 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100583}
584
585/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200586 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200587 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200588int ecp_group_read_string( ecp_group *grp, int radix,
589 const char *p, const char *b,
590 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100591{
592 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100593
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100594 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100595 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
596 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
597 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
598
599 grp->pbits = mpi_msb( &grp->P );
600 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100601
602cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200603 if( ret != 0 )
604 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200605
606 return( ret );
607}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200608
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100609/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100610 * Set a group from an ECParameters record (RFC 4492)
611 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100612int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100613{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200614 uint16_t tls_id;
615 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100616
617 /*
618 * We expect at least three bytes (see below)
619 */
620 if( len < 3 )
621 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
622
623 /*
624 * First byte is curve_type; only named_curve is handled
625 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100626 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100627 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
628
629 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100630 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100631 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200632 tls_id = *(*buf)++;
633 tls_id <<= 8;
634 tls_id |= *(*buf)++;
635
636 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
637 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
638
639 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100640}
641
642/*
643 * Write the ECParameters record corresponding to a group (RFC 4492)
644 */
645int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
646 unsigned char *buf, size_t blen )
647{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200648 const ecp_curve_info *curve_info;
649
650 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
651 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200652
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100653 /*
654 * We are going to write 3 bytes (see below)
655 */
656 *olen = 3;
657 if( blen < *olen )
658 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
659
660 /*
661 * First byte is curve_type, always named_curve
662 */
663 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
664
665 /*
666 * Next two bytes are the namedcurve value
667 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200668 buf[0] = curve_info->tls_id >> 8;
669 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100670
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200671 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100672}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100673
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200674/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200675 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
676 * See the documentation of struct ecp_group.
677 *
678 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200679 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200680static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200681{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200682 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200683
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200684 if( grp->modp == NULL )
685 return( mpi_mod_mpi( N, N, &grp->P ) );
686
687 /* N->s < 0 is a much faster test, which fails only if N is 0 */
688 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
689 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200690 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200691 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200692 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200693
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200694 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200695
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200696 /* N->s < 0 is a much faster test, which fails only if N is 0 */
697 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
698 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200699
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200700 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
701 /* we known P, N and the result are positive */
702 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200703
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200704cleanup:
705 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200706}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200707
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100708/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100709 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100710 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100711 * In order to guarantee that, we need to ensure that operands of
712 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100713 * bring the result back to this range.
714 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100715 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100716 */
717
718/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100719 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
720 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100721#if defined(POLARSSL_SELF_TEST)
722#define INC_MUL_COUNT mul_count++;
723#else
724#define INC_MUL_COUNT
725#endif
726
727#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
728 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100729
730/*
731 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200732 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100733 */
734#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200735 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100736 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
737
738/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200739 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
740 * We known P, N and the result are positive, so sub_abs is correct, and
741 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100742 */
743#define MOD_ADD( N ) \
744 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200745 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100746
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100747#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
748/*
749 * For curves in short Weierstrass form, we do all the internal operations in
750 * Jacobian coordinates.
751 *
752 * For multiplication, we'll use a comb method with coutermeasueres against
753 * SPA, hence timing attacks.
754 */
755
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100756/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100757 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100758 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100759 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100760static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100761{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100762 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100763 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100764
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100765 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100766 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100767
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100768 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100769
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100770 /*
771 * X = X / Z^2 mod p
772 */
773 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
774 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
775 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100776
777 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100778 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100779 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100780 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
781 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100782
783 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100784 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100785 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100786 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100787
788cleanup:
789
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100790 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100791
792 return( ret );
793}
794
795/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100796 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100797 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100798 * (See for example Cohen's "A Course in Computational Algebraic Number
799 * Theory", Algorithm 10.3.4.)
800 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200801 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100802 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100803 *
804 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100805 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100806static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100807 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100808{
809 int ret;
810 size_t i;
811 mpi *c, u, Zi, ZZi;
812
813 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100814 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100815
Paul Bakker6e339b52013-07-03 13:37:05 +0200816 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200817 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100818
819 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
820 for( i = 0; i < t_len; i++ )
821 mpi_init( &c[i] );
822
823 /*
824 * c[i] = Z_0 * ... * Z_i
825 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100826 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100827 for( i = 1; i < t_len; i++ )
828 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100829 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100830 MOD_MUL( c[i] );
831 }
832
833 /*
834 * u = 1 / (Z_0 * ... * Z_n) mod P
835 */
836 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
837
838 for( i = t_len - 1; ; i-- )
839 {
840 /*
841 * Zi = 1 / Z_i mod p
842 * u = 1 / (Z_0 * ... * Z_i) mod P
843 */
844 if( i == 0 ) {
845 MPI_CHK( mpi_copy( &Zi, &u ) );
846 }
847 else
848 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100849 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
850 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100851 }
852
853 /*
854 * proceed as in normalize()
855 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100856 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
857 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
858 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
859 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 +0100860
861 /*
862 * Post-precessing: reclaim some memory by shrinking coordinates
863 * - not storing Z (always 1)
864 * - shrinking other coordinates, but still keeping the same number of
865 * limbs as P, as otherwise it will too likely be regrown too fast.
866 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100867 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
868 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100869 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100870
871 if( i == 0 )
872 break;
873 }
874
875cleanup:
876
877 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
878 for( i = 0; i < t_len; i++ )
879 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200880 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100881
882 return( ret );
883}
884
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100885/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100886 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
887 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
888 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100889static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100890 ecp_point *Q,
891 unsigned char inv )
892{
893 int ret;
894 unsigned char nonzero;
895 mpi mQY;
896
897 mpi_init( &mQY );
898
899 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
900 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
901 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
902 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
903
904cleanup:
905 mpi_free( &mQY );
906
907 return( ret );
908}
909
910/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200911 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200912 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200913 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200914 * with heavy variable renaming, some reordering and one minor modification
915 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
916 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100917 *
918 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200919 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200920static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
921 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200922{
923 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200924 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200925
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200926#if defined(POLARSSL_SELF_TEST)
927 dbl_count++;
928#endif
929
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200930 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
931 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200932
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200933 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
934 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
935 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
936 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
937 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
938 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
939 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
940 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
941 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
942 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
943 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100944
945 /* Special case for A = -3 */
946 if( grp->A.p == NULL )
947 {
948 MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
949 X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
950 MOD_SUB( X3 );
951 }
952 else
Peter Vaskovica676acf2014-08-06 00:48:39 +0200953 {
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100954 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
Peter Vaskovica676acf2014-08-06 00:48:39 +0200955 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100956
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200957 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
958 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
959 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
960 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
961 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
962 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
963 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
964 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
965 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
966 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
967 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
968 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200969
970 MPI_CHK( mpi_copy( &R->X, &X3 ) );
971 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
972 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
973
974cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200975 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
976 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200977
978 return( ret );
979}
980
981/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100982 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100983 *
984 * The coordinates of Q must be normalized (= affine),
985 * but those of P don't need to. R is not normalized.
986 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100987 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100988 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100989 * - at each step, P, Q and R are multiples of the base point, the factor
990 * being less than its order, so none of them is zero;
991 * - Q is an odd multiple of the base point, P an even multiple,
992 * due to the choice of precomputed points in the modified comb method.
993 * So branches for these cases do not leak secret information.
994 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100995 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
996 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100997 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100998 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100999static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001000 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001001{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001002 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001003 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001004
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001005#if defined(POLARSSL_SELF_TEST)
1006 add_count++;
1007#endif
1008
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001009 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001010 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001011 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001012 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1013 return( ecp_copy( R, Q ) );
1014
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001015 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001016 return( ecp_copy( R, P ) );
1017
1018 /*
1019 * Make sure Q coordinates are normalized
1020 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001021 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001022 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001023
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001024 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1025 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001026
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001027 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1028 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1029 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1030 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1031 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1032 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001033
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001034 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001035 if( mpi_cmp_int( &T1, 0 ) == 0 )
1036 {
1037 if( mpi_cmp_int( &T2, 0 ) == 0 )
1038 {
1039 ret = ecp_double_jac( grp, R, P );
1040 goto cleanup;
1041 }
1042 else
1043 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001044 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001045 goto cleanup;
1046 }
1047 }
1048
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001049 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1050 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1051 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1052 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1053 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1054 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1055 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1056 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1057 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1058 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1059 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1060 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001061
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001062 MPI_CHK( mpi_copy( &R->X, &X ) );
1063 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1064 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001065
1066cleanup:
1067
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001068 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1069 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001070
1071 return( ret );
1072}
1073
1074/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001075 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001076 */
1077int ecp_add( const ecp_group *grp, ecp_point *R,
1078 const ecp_point *P, const ecp_point *Q )
1079{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001080 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001081
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001082 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001083 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1084
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001085 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001086 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001087
1088cleanup:
1089 return( ret );
1090}
1091
1092/*
1093 * Subtraction: R = P - Q, result's coordinates normalized
1094 */
1095int ecp_sub( const ecp_group *grp, ecp_point *R,
1096 const ecp_point *P, const ecp_point *Q )
1097{
1098 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001099 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001100
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001101 ecp_point_init( &mQ );
1102
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001103 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001104 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1105
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001106 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001107 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001108 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1109 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1110
1111 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001112 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001113
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001114cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001115 ecp_point_free( &mQ );
1116
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001117 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001118}
1119
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001120/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001121 * Randomize jacobian coordinates:
1122 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001123 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001124 *
1125 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001126 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001127static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001128 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1129{
1130 int ret;
1131 mpi l, ll;
Paul Bakker66d5d072014-06-17 16:39:18 +02001132 size_t p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001133 int count = 0;
1134
1135 mpi_init( &l ); mpi_init( &ll );
1136
1137 /* Generate l such that 1 < l < p */
1138 do
1139 {
1140 mpi_fill_random( &l, p_size, f_rng, p_rng );
1141
1142 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001143 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001144
1145 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001146 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001147 }
1148 while( mpi_cmp_int( &l, 1 ) <= 0 );
1149
1150 /* Z = l * Z */
1151 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1152
1153 /* X = l^2 * X */
1154 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1155 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1156
1157 /* Y = l^3 * Y */
1158 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1159 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1160
1161cleanup:
1162 mpi_free( &l ); mpi_free( &ll );
1163
1164 return( ret );
1165}
1166
1167/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001168 * Check and define parameters used by the comb method (see below for details)
1169 */
1170#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1171#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1172#endif
1173
1174/* d = ceil( n / w ) */
1175#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1176
1177/* number of precomputed points */
1178#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1179
1180/*
1181 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001182 *
1183 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001184 * modified version that provides resistance to SPA by avoiding zero
1185 * digits in the representation as in [3]. We modify the method further by
1186 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001187 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001188 *
1189 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1190 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1191 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001192 *
1193 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001194 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001195 * - w is the size, ie number of teeth, of the comb, and must be between
1196 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001197 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1198 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001199 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001200static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001201 unsigned char w, const mpi *m )
1202{
1203 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001204 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001205
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001206 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001207
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001208 /* First get the classical comb values (except for x_d = 0) */
1209 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001210 for( j = 0; j < w; j++ )
1211 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1212
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001213 /* Now make sure x_1 .. x_d are odd */
1214 c = 0;
1215 for( i = 1; i <= d; i++ )
1216 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001217 /* Add carry and update it */
1218 cc = x[i] & c;
1219 x[i] = x[i] ^ c;
1220 c = cc;
1221
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001222 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001223 adjust = 1 - ( x[i] & 0x01 );
1224 c |= x[i] & ( x[i-1] * adjust );
1225 x[i] = x[i] ^ ( x[i-1] * adjust );
1226 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001227 }
1228}
1229
1230/*
1231 * Precompute points for the comb method
1232 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001233 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1234 * 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 +01001235 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001236 * T must be able to hold 2^{w - 1} elements
1237 *
1238 * 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 +01001239 */
1240static int ecp_precompute_comb( const ecp_group *grp,
1241 ecp_point T[], const ecp_point *P,
1242 unsigned char w, size_t d )
1243{
1244 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001245 unsigned char i, k;
1246 size_t j;
1247 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001248
1249 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001250 * Set T[0] = P and
1251 * 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 +01001252 */
1253 MPI_CHK( ecp_copy( &T[0], P ) );
1254
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001255 k = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +02001256 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001257 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001258 cur = T + i;
1259 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001260 for( j = 0; j < d; j++ )
1261 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001262
1263 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001264 }
1265
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001266 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001267
1268 /*
1269 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001270 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001271 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001272 k = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +02001273 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001274 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001275 j = i;
1276 while( j-- )
1277 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001278 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001279 TT[k++] = &T[i + j];
1280 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001281 }
1282
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001283 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001284
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001285cleanup:
1286 return( ret );
1287}
1288
1289/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001290 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001291 */
1292static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001293 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001294 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001295{
1296 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001297 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001298
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001299 /* Ignore the "sign" bit and scale down */
1300 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001301
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001302 /* Read the whole table to thwart cache-based timing attacks */
1303 for( j = 0; j < t_len; j++ )
1304 {
1305 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1306 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1307 }
1308
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001309 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001310 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001311
1312cleanup:
1313 return( ret );
1314}
1315
1316/*
1317 * Core multiplication algorithm for the (modified) comb method.
1318 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001319 *
1320 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001321 */
1322static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001323 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001324 const unsigned char x[], size_t d,
1325 int (*f_rng)(void *, unsigned char *, size_t),
1326 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001327{
1328 int ret;
1329 ecp_point Txi;
1330 size_t i;
1331
1332 ecp_point_init( &Txi );
1333
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001334 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001335 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001336 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001337 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001338 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001339 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001340
1341 while( i-- != 0 )
1342 {
1343 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001344 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001345 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001346 }
1347
1348cleanup:
1349 ecp_point_free( &Txi );
1350
1351 return( ret );
1352}
1353
1354/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001355 * Multiplication using the comb method,
1356 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001357 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001358static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1359 const mpi *m, const ecp_point *P,
1360 int (*f_rng)(void *, unsigned char *, size_t),
1361 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001362{
1363 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001364 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1365 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001366 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001367 ecp_point *T;
1368 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001369
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001370 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001371 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001372
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001373 /* we need N to be odd to trnaform m in an odd number, check now */
1374 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1375 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1376
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001377 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001378 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001379 * 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 +01001380 * (see costs of the various parts, with 1S = 1M)
1381 */
1382 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001383
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001384 /*
1385 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001386 * Just adding one avoids upping the cost of the first mul too much,
1387 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001388 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001389#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001390 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1391 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001392 if( p_eq_g )
1393 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001394#else
1395 p_eq_g = 0;
1396#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001397
1398 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001399 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001400 * (The last test is useful only for very small curves in the test suite.)
1401 */
1402 if( w > POLARSSL_ECP_WINDOW_SIZE )
1403 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001404 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001405 w = 2;
1406
1407 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001408 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001409 d = ( grp->nbits + w - 1 ) / w;
1410
1411 /*
1412 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001413 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001414 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001415 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001416
1417 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001418 {
1419 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1420 if( T == NULL )
1421 {
1422 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1423 goto cleanup;
1424 }
1425
1426 for( i = 0; i < pre_len; i++ )
1427 ecp_point_init( &T[i] );
1428
1429 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1430
1431 if( p_eq_g )
1432 {
1433 grp->T = T;
1434 grp->T_size = pre_len;
1435 }
1436 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001437
1438 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001439 * Make sure M is odd (M = m or M = N - m, since N is odd)
1440 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001441 */
1442 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001443 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001444 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1445 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001446
1447 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001448 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001449 */
1450 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001451 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 +01001452
1453 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001454 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001455 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001456 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1457 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001458
1459cleanup:
1460
1461 if( T != NULL && ! p_eq_g )
1462 {
1463 for( i = 0; i < pre_len; i++ )
1464 ecp_point_free( &T[i] );
1465 polarssl_free( T );
1466 }
1467
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001468 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001469 mpi_free( &mm );
1470
1471 if( ret != 0 )
1472 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001473
1474 return( ret );
1475}
1476
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001477#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1478
1479#if defined(POLARSSL_ECP_MONTGOMERY)
1480/*
1481 * For Montgomery curves, we do all the internal arithmetic in projective
1482 * coordinates. Import/export of points uses only the x coordinates, which is
1483 * internaly represented as X / Z.
1484 *
1485 * For scalar multiplication, we'll use a Montgomery ladder.
1486 */
1487
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001488/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001489 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1490 * Cost: 1M + 1I
1491 */
1492static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1493{
1494 int ret;
1495
1496 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1497 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1498 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1499
1500cleanup:
1501 return( ret );
1502}
1503
1504/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001505 * Randomize projective x/z coordinates:
1506 * (X, Z) -> (l X, l Z) for random l
1507 * This is sort of the reverse operation of ecp_normalize_mxz().
1508 *
1509 * This countermeasure was first suggested in [2].
1510 * Cost: 2M
1511 */
1512static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1513 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1514{
1515 int ret;
1516 mpi l;
Paul Bakker66d5d072014-06-17 16:39:18 +02001517 size_t p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001518 int count = 0;
1519
1520 mpi_init( &l );
1521
1522 /* Generate l such that 1 < l < p */
1523 do
1524 {
1525 mpi_fill_random( &l, p_size, f_rng, p_rng );
1526
1527 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001528 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001529
1530 if( count++ > 10 )
1531 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1532 }
1533 while( mpi_cmp_int( &l, 1 ) <= 0 );
1534
1535 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1536 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1537
1538cleanup:
1539 mpi_free( &l );
1540
1541 return( ret );
1542}
1543
1544/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001545 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1546 * for Montgomery curves in x/z coordinates.
1547 *
1548 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1549 * with
1550 * d = X1
1551 * P = (X2, Z2)
1552 * Q = (X3, Z3)
1553 * R = (X4, Z4)
1554 * S = (X5, Z5)
1555 * and eliminating temporary variables tO, ..., t4.
1556 *
1557 * Cost: 5M + 4S
1558 */
1559static int ecp_double_add_mxz( const ecp_group *grp,
1560 ecp_point *R, ecp_point *S,
1561 const ecp_point *P, const ecp_point *Q,
1562 const mpi *d )
1563{
1564 int ret;
1565 mpi A, AA, B, BB, E, C, D, DA, CB;
1566
1567 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1568 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1569 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1570
1571 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1572 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1573 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1574 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1575 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1576 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1577 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1578 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1579 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1580 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1581 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1582 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1583 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1584 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1585 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1586 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1587 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1588 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1589
1590cleanup:
1591 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1592 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1593 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1594
1595 return( ret );
1596}
1597
1598/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001599 * Multiplication with Montgomery ladder in x/z coordinates,
1600 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001601 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001602static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1603 const mpi *m, const ecp_point *P,
1604 int (*f_rng)(void *, unsigned char *, size_t),
1605 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001606{
1607 int ret;
1608 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001609 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001610 ecp_point RP;
1611 mpi PX;
1612
1613 ecp_point_init( &RP ); mpi_init( &PX );
1614
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001615 /* Save PX and read from P before writing to R, in case P == R */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001616 MPI_CHK( mpi_copy( &PX, &P->X ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001617 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001618
1619 /* Set R to zero in modified x/z coordinates */
1620 MPI_CHK( mpi_lset( &R->X, 1 ) );
1621 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1622 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001623
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001624 /* RP.X might be sligtly larger than P, so reduce it */
1625 MOD_ADD( RP.X );
1626
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001627 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001628 if( f_rng != NULL )
1629 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001630
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001631 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001632 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001633 while( i-- > 0 )
1634 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001635 b = mpi_get_bit( m, i );
1636 /*
1637 * if (b) R = 2R + P else R = 2R,
1638 * which is:
1639 * if (b) double_add( RP, R, RP, R )
1640 * else double_add( R, RP, R, RP )
1641 * but using safe conditional swaps to avoid leaks
1642 */
1643 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1644 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1645 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1646 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1647 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001648 }
1649
1650 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1651
1652cleanup:
1653 ecp_point_free( &RP ); mpi_free( &PX );
1654
1655 return( ret );
1656}
1657
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001658#endif /* POLARSSL_ECP_MONTGOMERY */
1659
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001660/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001661 * Multiplication R = m * P
1662 */
1663int ecp_mul( ecp_group *grp, ecp_point *R,
1664 const mpi *m, const ecp_point *P,
1665 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1666{
1667 int ret;
1668
1669 /* Common sanity checks */
1670 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1671 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1672
1673 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1674 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1675 return( ret );
1676
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001677#if defined(POLARSSL_ECP_MONTGOMERY)
1678 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001679 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001680#endif
1681#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1682 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001683 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001684#endif
1685 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001686}
1687
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001688#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001689/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001690 * Check that an affine point is valid as a public key,
1691 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001692 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001693static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001694{
1695 int ret;
1696 mpi YY, RHS;
1697
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001698 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001699 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1700 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1701 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1702 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001703 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001704
1705 mpi_init( &YY ); mpi_init( &RHS );
1706
1707 /*
1708 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001709 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001710 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001711 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1712 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001713
1714 /* Special case for A = -3 */
1715 if( grp->A.p == NULL )
1716 {
1717 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1718 }
1719 else
1720 {
1721 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1722 }
1723
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001724 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1725 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001726
1727 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001728 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001729
1730cleanup:
1731
1732 mpi_free( &YY ); mpi_free( &RHS );
1733
1734 return( ret );
1735}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001736#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1737
1738
1739#if defined(POLARSSL_ECP_MONTGOMERY)
1740/*
1741 * Check validity of a public key for Montgomery curves with x-only schemes
1742 */
1743static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1744{
1745 /* [M255 p. 5] Just check X is the correct number of bytes */
1746 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1747 return( POLARSSL_ERR_ECP_INVALID_KEY );
1748
1749 return( 0 );
1750}
1751#endif /* POLARSSL_ECP_MONTGOMERY */
1752
1753/*
1754 * Check that a point is valid as a public key
1755 */
1756int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1757{
1758 /* Must use affine coordinates */
1759 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1760 return( POLARSSL_ERR_ECP_INVALID_KEY );
1761
1762#if defined(POLARSSL_ECP_MONTGOMERY)
1763 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1764 return( ecp_check_pubkey_mx( grp, pt ) );
1765#endif
1766#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1767 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1768 return( ecp_check_pubkey_sw( grp, pt ) );
1769#endif
1770 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1771}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001772
1773/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001774 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001775 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001776int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001777{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001778#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001779 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001780 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001781 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001782 if( mpi_get_bit( d, 0 ) != 0 ||
1783 mpi_get_bit( d, 1 ) != 0 ||
1784 mpi_get_bit( d, 2 ) != 0 ||
1785 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1786 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001787 else
1788 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001789 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001790#endif /* POLARSSL_ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001791#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1792 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001793 {
1794 /* see SEC1 3.2 */
1795 if( mpi_cmp_int( d, 1 ) < 0 ||
1796 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1797 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001798 else
1799 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001800 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001801#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001802
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001803 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001804}
1805
1806/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001807 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001808 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001809int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001810 int (*f_rng)(void *, unsigned char *, size_t),
1811 void *p_rng )
1812{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001813 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +02001814 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001815
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001816#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001817 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001818 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001819 /* [M225] page 5 */
1820 size_t b;
1821
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001822 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001823
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001824 /* Make sure the most significant bit is nbits */
1825 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1826 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001827 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001828 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001829 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001830
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001831 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001832 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1833 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1834 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001835 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001836 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001837#endif /* POLARSSL_ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001838#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1839 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001840 {
1841 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001842 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001843 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1844
1845 /*
1846 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1847 * - use the same byte ordering;
1848 * - keep the leftmost nbits bits of the generated octet string;
1849 * - try until result is in the desired range.
1850 * This also avoids any biais, which is especially important for ECDSA.
1851 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001852 do
1853 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001854 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1855 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1856 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001857
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001858 /*
1859 * Each try has at worst a probability 1/2 of failing (the msb has
1860 * a probability 1/2 of being 0, and then the result will be < N),
1861 * so after 30 tries failure probability is a most 2**(-30).
1862 *
1863 * For most curves, 1 try is enough with overwhelming probability,
1864 * since N starts with a lot of 1s in binary, but some curves
1865 * such as secp224k1 are actually very close to the worst case.
1866 */
1867 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001868 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1869 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001870 while( mpi_cmp_int( d, 1 ) < 0 ||
1871 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001872 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001873 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001874#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001875 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001876
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001877cleanup:
1878 if( ret != 0 )
1879 return( ret );
1880
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001881 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001882}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001883
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001884/*
1885 * Generate a keypair, prettier wrapper
1886 */
1887int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1888 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1889{
1890 int ret;
1891
1892 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1893 return( ret );
1894
1895 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1896}
1897
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001898/*
1899 * Check a public-private key pair
1900 */
1901int ecp_check_pub_priv( const ecp_keypair *pub, const ecp_keypair *prv )
1902{
1903 int ret;
1904 ecp_point Q;
1905 ecp_group grp;
1906
1907 if( pub->grp.id == POLARSSL_ECP_DP_NONE ||
1908 pub->grp.id != prv->grp.id ||
1909 mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
1910 mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
1911 mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
1912 {
1913 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1914 }
1915
1916 ecp_point_init( &Q );
1917 ecp_group_init( &grp );
1918
1919 /* ecp_mul() needs a non-const group... */
1920 ecp_group_copy( &grp, &prv->grp );
1921
1922 /* Also checks d is valid */
1923 MPI_CHK( ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
1924
1925 if( mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
1926 mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
1927 mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
1928 {
1929 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
1930 goto cleanup;
1931 }
1932
1933cleanup:
1934 ecp_point_free( &Q );
1935 ecp_group_free( &grp );
1936
1937 return( ret );
1938}
1939
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001940#if defined(POLARSSL_SELF_TEST)
1941
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001942/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001943 * Checkup routine
1944 */
1945int ecp_self_test( int verbose )
1946{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001947 int ret;
1948 size_t i;
1949 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001950 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001951 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001952 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001953 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001954 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001955 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001956 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001957 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001958 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001959 "400000000000000000000000000000000000000000000000", /* one and zeros */
1960 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1961 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001962 };
1963
1964 ecp_group_init( &grp );
1965 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001966 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001967 mpi_init( &m );
1968
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001969 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001970#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001971 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001972#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001973 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1974#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001975
1976 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001977 polarssl_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001978
1979 /* Do a dummy multiplication first to trigger precomputation */
1980 MPI_CHK( mpi_lset( &m, 2 ) );
1981 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001982
1983 add_count = 0;
1984 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001985 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001986 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001987 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001988
1989 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1990 {
1991 add_c_prev = add_count;
1992 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001993 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001994 add_count = 0;
1995 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001996 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001997
1998 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001999 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002000
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002001 if( add_count != add_c_prev ||
2002 dbl_count != dbl_c_prev ||
2003 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002004 {
2005 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002006 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002007
2008 ret = 1;
2009 goto cleanup;
2010 }
2011 }
2012
2013 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002014 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002015
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002016 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002017 polarssl_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002018 /* We computed P = 2G last time, use it */
2019
2020 add_count = 0;
2021 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002022 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002023 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2024 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2025
2026 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2027 {
2028 add_c_prev = add_count;
2029 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002030 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002031 add_count = 0;
2032 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002033 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002034
2035 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2036 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2037
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002038 if( add_count != add_c_prev ||
2039 dbl_count != dbl_c_prev ||
2040 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002041 {
2042 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002043 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002044
2045 ret = 1;
2046 goto cleanup;
2047 }
2048 }
2049
2050 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002051 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002052
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002053cleanup:
2054
2055 if( ret < 0 && verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002056 polarssl_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002057
2058 ecp_group_free( &grp );
2059 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002060 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002061 mpi_free( &m );
2062
2063 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002064 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002065
2066 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002067}
2068
Paul Bakker9af723c2014-05-01 13:03:14 +02002069#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002070
Paul Bakker9af723c2014-05-01 13:03:14 +02002071#endif /* POLARSSL_ECP_C */