blob: 26ce96239d9b193a7ac2f7083c4f042aedbb00b8 [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>
Manuel Pégourié-Gonnard981732b2015-02-17 15:46:45 +000060#include <stdio.h>
Paul Bakker7dc4c442014-02-01 22:50:26 +010061#define polarssl_printf printf
Paul Bakker6e339b52013-07-03 13:37:05 +020062#define polarssl_malloc malloc
63#define polarssl_free free
64#endif
65
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010066#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
67 !defined(EFI32)
68#define strcasecmp _stricmp
69#endif
70
Paul Bakker6a6087e2013-10-28 18:53:08 +010071#if defined(_MSC_VER) && !defined(inline)
72#define inline _inline
73#else
74#if defined(__ARMCC_VERSION) && !defined(inline)
75#define inline __inline
76#endif /* __ARMCC_VERSION */
77#endif /*_MSC_VER */
78
Paul Bakker34617722014-06-13 17:20:13 +020079/* Implementation that should never be optimized out by the compiler */
80static void polarssl_zeroize( void *v, size_t n ) {
81 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
82}
83
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010084#if defined(POLARSSL_SELF_TEST)
85/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010086 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020087 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010088 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010089static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010090#endif
91
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010092#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
93 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
94 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
95 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
96 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
97 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
98 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
Manuel Pégourié-Gonnard2a2ae642014-02-24 08:29:51 +010099 defined(POLARSSL_ECP_DP_BP512R1_ENABLED) || \
100 defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) || \
101 defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) || \
102 defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100103#define POLARSSL_ECP_SHORT_WEIERSTRASS
104#endif
105
106#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
107 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
108 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
109 defined(POLARSSL_ECP_DP_M511_ENABLED)
110#define POLARSSL_ECP_MONTGOMERY
111#endif
112
113/*
114 * Curve types: internal for now, might be exposed later
115 */
116typedef enum
117{
118 POLARSSL_ECP_TYPE_NONE = 0,
119 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
120 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
121} ecp_curve_type;
122
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100123/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200124 * List of supported curves:
125 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200126 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200127 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200128 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100129 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100130 * Curves are listed in order: largest curves first, and for a given size,
131 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200132 */
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200133static const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200134{
135#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200136 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200137#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100138#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
139 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
140#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200141#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200142 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200143#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100144#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
145 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
146#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200147#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200148 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200149#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100150#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
151 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
152#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100153#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
154 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
155#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200156#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200157 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200158#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100159#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
160 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
161#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100162#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
163 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
164#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100165#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
166 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
167#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200168 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200169};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100170
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200171#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
172 sizeof( ecp_supported_curves[0] )
173
174static ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200175
176/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200177 * List of supported curves and associated info
178 */
179const ecp_curve_info *ecp_curve_list( void )
180{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200181 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200182}
183
184/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100185 * List of supported curves, group ID only
186 */
187const ecp_group_id *ecp_grp_id_list( void )
188{
189 static int init_done = 0;
190
191 if( ! init_done )
192 {
193 size_t i = 0;
194 const ecp_curve_info *curve_info;
195
196 for( curve_info = ecp_curve_list();
197 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
198 curve_info++ )
199 {
200 ecp_supported_grp_id[i++] = curve_info->grp_id;
201 }
202 ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
203
204 init_done = 1;
205 }
206
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200207 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100208}
209
210/*
211 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200212 */
213const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
214{
215 const ecp_curve_info *curve_info;
216
217 for( curve_info = ecp_curve_list();
218 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
219 curve_info++ )
220 {
221 if( curve_info->grp_id == grp_id )
222 return( curve_info );
223 }
224
225 return( NULL );
226}
227
228/*
229 * Get the curve info from the TLS identifier
230 */
231const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
232{
233 const ecp_curve_info *curve_info;
234
235 for( curve_info = ecp_curve_list();
236 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
237 curve_info++ )
238 {
239 if( curve_info->tls_id == tls_id )
240 return( curve_info );
241 }
242
243 return( NULL );
244}
245
246/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100247 * Get the curve info from the name
248 */
249const ecp_curve_info *ecp_curve_info_from_name( const char *name )
250{
251 const ecp_curve_info *curve_info;
252
253 for( curve_info = ecp_curve_list();
254 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
255 curve_info++ )
256 {
257 if( strcasecmp( curve_info->name, name ) == 0 )
258 return( curve_info );
259 }
260
261 return( NULL );
262}
263
264/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100265 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100266 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100267static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100268{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100269 if( grp->G.X.p == NULL )
270 return( POLARSSL_ECP_TYPE_NONE );
271
272 if( grp->G.Y.p == NULL )
273 return( POLARSSL_ECP_TYPE_MONTGOMERY );
274 else
275 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100276}
277
278/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100279 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100280 */
281void ecp_point_init( ecp_point *pt )
282{
283 if( pt == NULL )
284 return;
285
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100286 mpi_init( &pt->X );
287 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100288 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100289}
290
291/*
292 * Initialize (the components of) a group
293 */
294void ecp_group_init( ecp_group *grp )
295{
296 if( grp == NULL )
297 return;
298
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200299 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100300}
301
302/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200303 * Initialize (the components of) a key pair
304 */
305void ecp_keypair_init( ecp_keypair *key )
306{
Paul Bakker66d5d072014-06-17 16:39:18 +0200307 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200308 return;
309
310 ecp_group_init( &key->grp );
311 mpi_init( &key->d );
312 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200313}
314
315/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100316 * Unallocate (the components of) a point
317 */
318void ecp_point_free( ecp_point *pt )
319{
320 if( pt == NULL )
321 return;
322
323 mpi_free( &( pt->X ) );
324 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100325 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100326}
327
328/*
329 * Unallocate (the components of) a group
330 */
331void ecp_group_free( ecp_group *grp )
332{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200333 size_t i;
334
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100335 if( grp == NULL )
336 return;
337
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100338 if( grp->h != 1 )
339 {
340 mpi_free( &grp->P );
341 mpi_free( &grp->A );
342 mpi_free( &grp->B );
343 ecp_point_free( &grp->G );
344 mpi_free( &grp->N );
345 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200346
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200347 if( grp->T != NULL )
348 {
349 for( i = 0; i < grp->T_size; i++ )
350 ecp_point_free( &grp->T[i] );
351 polarssl_free( grp->T );
352 }
353
Paul Bakker34617722014-06-13 17:20:13 +0200354 polarssl_zeroize( grp, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100355}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100356
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100357/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200358 * Unallocate (the components of) a key pair
359 */
360void ecp_keypair_free( ecp_keypair *key )
361{
Paul Bakker66d5d072014-06-17 16:39:18 +0200362 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200363 return;
364
365 ecp_group_free( &key->grp );
366 mpi_free( &key->d );
367 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200368}
369
370/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200371 * Copy the contents of a point
372 */
373int ecp_copy( ecp_point *P, const ecp_point *Q )
374{
375 int ret;
376
377 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
378 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
379 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
380
381cleanup:
382 return( ret );
383}
384
385/*
386 * Copy the contents of a group object
387 */
388int ecp_group_copy( ecp_group *dst, const ecp_group *src )
389{
390 return ecp_use_known_dp( dst, src->id );
391}
392
393/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100394 * Set point to zero
395 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100396int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100397{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100398 int ret;
399
400 MPI_CHK( mpi_lset( &pt->X , 1 ) );
401 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
402 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
403
404cleanup:
405 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100406}
407
408/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100409 * Tell if a point is zero
410 */
411int ecp_is_zero( ecp_point *pt )
412{
413 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
414}
415
416/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100417 * Import a non-zero point from ASCII strings
418 */
419int ecp_point_read_string( ecp_point *P, int radix,
420 const char *x, const char *y )
421{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100422 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100423
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100424 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
425 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100426 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100427
428cleanup:
429 return( ret );
430}
431
432/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100433 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100434 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100435int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100436 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100437 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100438{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200439 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100440 size_t plen;
441
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100442 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
443 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100444 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100445
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100446 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100447 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100448 */
449 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
450 {
451 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100452 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100453
454 buf[0] = 0x00;
455 *olen = 1;
456
457 return( 0 );
458 }
459
460 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100461
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100462 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
463 {
464 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100465
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100466 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100467 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100468
469 buf[0] = 0x04;
470 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
471 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
472 }
473 else if( format == POLARSSL_ECP_PF_COMPRESSED )
474 {
475 *olen = plen + 1;
476
477 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100478 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100479
480 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
481 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
482 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100483
484cleanup:
485 return( ret );
486}
487
488/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100489 * Import a point from unsigned binary data (SEC1 2.3.4)
490 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100491int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100492 const unsigned char *buf, size_t ilen )
493{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100494 int ret;
495 size_t plen;
496
Paul Bakker82788fb2014-10-20 13:59:19 +0200497 if( ilen < 1 )
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200498 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
499
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100500 if( buf[0] == 0x00 )
501 {
502 if( ilen == 1 )
503 return( ecp_set_zero( pt ) );
504 else
505 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
506 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100507
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100508 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100509
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100510 if( buf[0] != 0x04 )
511 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
512
513 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100514 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100515
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100516 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
517 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
518 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100519
520cleanup:
521 return( ret );
522}
523
524/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100525 * Import a point from a TLS ECPoint record (RFC 4492)
526 * struct {
527 * opaque point <1..2^8-1>;
528 * } ECPoint;
529 */
530int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100531 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100532{
533 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100534 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100535
536 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200537 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100538 */
539 if( buf_len < 2 )
540 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
541
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100542 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100543 if( data_len < 1 || data_len > buf_len - 1 )
544 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
545
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100546 /*
547 * Save buffer start for read_binary and update buf
548 */
549 buf_start = *buf;
550 *buf += data_len;
551
552 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100553}
554
555/*
556 * Export a point as a TLS ECPoint record (RFC 4492)
557 * struct {
558 * opaque point <1..2^8-1>;
559 * } ECPoint;
560 */
561int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100562 int format, size_t *olen,
563 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100564{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100565 int ret;
566
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100567 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100568 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100569 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100570 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100571 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
572
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100573 if( ( ret = ecp_point_write_binary( grp, pt, format,
574 olen, buf + 1, blen - 1) ) != 0 )
575 return( ret );
576
577 /*
578 * write length to the first byte and update total length
579 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200580 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100581 ++*olen;
582
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200583 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100584}
585
586/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200587 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200588 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200589int ecp_group_read_string( ecp_group *grp, int radix,
590 const char *p, const char *b,
591 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100592{
593 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100594
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100595 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100596 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
597 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
598 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
599
600 grp->pbits = mpi_msb( &grp->P );
601 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100602
603cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200604 if( ret != 0 )
605 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200606
607 return( ret );
608}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200609
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100610/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100611 * Set a group from an ECParameters record (RFC 4492)
612 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100613int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100614{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200615 uint16_t tls_id;
616 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100617
618 /*
619 * We expect at least three bytes (see below)
620 */
621 if( len < 3 )
622 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
623
624 /*
625 * First byte is curve_type; only named_curve is handled
626 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100627 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100628 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
629
630 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100631 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100632 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200633 tls_id = *(*buf)++;
634 tls_id <<= 8;
635 tls_id |= *(*buf)++;
636
637 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
638 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
639
640 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100641}
642
643/*
644 * Write the ECParameters record corresponding to a group (RFC 4492)
645 */
646int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
647 unsigned char *buf, size_t blen )
648{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200649 const ecp_curve_info *curve_info;
650
651 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
652 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200653
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100654 /*
655 * We are going to write 3 bytes (see below)
656 */
657 *olen = 3;
658 if( blen < *olen )
659 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
660
661 /*
662 * First byte is curve_type, always named_curve
663 */
664 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
665
666 /*
667 * Next two bytes are the namedcurve value
668 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200669 buf[0] = curve_info->tls_id >> 8;
670 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100671
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200672 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100673}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100674
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200675/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200676 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
677 * See the documentation of struct ecp_group.
678 *
679 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200680 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200681static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200682{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200683 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200684
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200685 if( grp->modp == NULL )
686 return( mpi_mod_mpi( N, N, &grp->P ) );
687
688 /* N->s < 0 is a much faster test, which fails only if N is 0 */
689 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
690 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200691 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200692 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200693 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200694
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200695 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200696
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200697 /* N->s < 0 is a much faster test, which fails only if N is 0 */
698 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
699 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200700
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200701 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
702 /* we known P, N and the result are positive */
703 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200704
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200705cleanup:
706 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200707}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200708
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100709/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100710 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100711 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100712 * In order to guarantee that, we need to ensure that operands of
713 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100714 * bring the result back to this range.
715 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100716 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100717 */
718
719/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100720 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
721 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100722#if defined(POLARSSL_SELF_TEST)
723#define INC_MUL_COUNT mul_count++;
724#else
725#define INC_MUL_COUNT
726#endif
727
728#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
729 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100730
731/*
732 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200733 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100734 */
735#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200736 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100737 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
738
739/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200740 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
741 * We known P, N and the result are positive, so sub_abs is correct, and
742 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100743 */
744#define MOD_ADD( N ) \
745 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200746 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100747
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100748#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
749/*
750 * For curves in short Weierstrass form, we do all the internal operations in
751 * Jacobian coordinates.
752 *
753 * For multiplication, we'll use a comb method with coutermeasueres against
754 * SPA, hence timing attacks.
755 */
756
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100757/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100758 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100759 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100760 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100761static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100762{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100763 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100764 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100765
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100766 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100767 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100768
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100769 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100770
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100771 /*
772 * X = X / Z^2 mod p
773 */
774 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
775 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
776 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100777
778 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100779 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100780 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100781 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
782 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100783
784 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100785 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100786 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100787 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100788
789cleanup:
790
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100791 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100792
793 return( ret );
794}
795
796/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100797 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100798 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100799 * (See for example Cohen's "A Course in Computational Algebraic Number
800 * Theory", Algorithm 10.3.4.)
801 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200802 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100803 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100804 *
805 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100806 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100807static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100808 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100809{
810 int ret;
811 size_t i;
812 mpi *c, u, Zi, ZZi;
813
814 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100815 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100816
Mansour Moufidc531b4a2015-02-15 17:35:38 -0500817 if( ( c = polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200818 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100819
820 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
821 for( i = 0; i < t_len; i++ )
822 mpi_init( &c[i] );
823
824 /*
825 * c[i] = Z_0 * ... * Z_i
826 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100827 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100828 for( i = 1; i < t_len; i++ )
829 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100830 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100831 MOD_MUL( c[i] );
832 }
833
834 /*
835 * u = 1 / (Z_0 * ... * Z_n) mod P
836 */
837 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
838
839 for( i = t_len - 1; ; i-- )
840 {
841 /*
842 * Zi = 1 / Z_i mod p
843 * u = 1 / (Z_0 * ... * Z_i) mod P
844 */
845 if( i == 0 ) {
846 MPI_CHK( mpi_copy( &Zi, &u ) );
847 }
848 else
849 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100850 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
851 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100852 }
853
854 /*
855 * proceed as in normalize()
856 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100857 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
858 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
859 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
860 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 +0100861
862 /*
863 * Post-precessing: reclaim some memory by shrinking coordinates
864 * - not storing Z (always 1)
865 * - shrinking other coordinates, but still keeping the same number of
866 * limbs as P, as otherwise it will too likely be regrown too fast.
867 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100868 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
869 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100870 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100871
872 if( i == 0 )
873 break;
874 }
875
876cleanup:
877
878 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
879 for( i = 0; i < t_len; i++ )
880 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200881 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100882
883 return( ret );
884}
885
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100886/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100887 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
888 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
889 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100890static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100891 ecp_point *Q,
892 unsigned char inv )
893{
894 int ret;
895 unsigned char nonzero;
896 mpi mQY;
897
898 mpi_init( &mQY );
899
900 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
901 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
902 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
903 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
904
905cleanup:
906 mpi_free( &mQY );
907
908 return( ret );
909}
910
911/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200912 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200913 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200914 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200915 * with heavy variable renaming, some reordering and one minor modification
916 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
917 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100918 *
919 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200920 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200921static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
922 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200923{
924 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200925 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200926
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200927#if defined(POLARSSL_SELF_TEST)
928 dbl_count++;
929#endif
930
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200931 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
932 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200933
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200934 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
935 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
936 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
937 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
938 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
939 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
940 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
941 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
942 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
943 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
944 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100945
946 /* Special case for A = -3 */
947 if( grp->A.p == NULL )
948 {
949 MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
950 X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
951 MOD_SUB( X3 );
952 }
953 else
Peter Vaskovica676acf2014-08-06 00:48:39 +0200954 {
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100955 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
Peter Vaskovica676acf2014-08-06 00:48:39 +0200956 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100957
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200958 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
959 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
960 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
961 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
962 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
963 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
964 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
965 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
966 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
967 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
968 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
969 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200970
971 MPI_CHK( mpi_copy( &R->X, &X3 ) );
972 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
973 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
974
975cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200976 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
977 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200978
979 return( ret );
980}
981
982/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100983 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100984 *
985 * The coordinates of Q must be normalized (= affine),
986 * but those of P don't need to. R is not normalized.
987 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100988 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100989 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100990 * - at each step, P, Q and R are multiples of the base point, the factor
991 * being less than its order, so none of them is zero;
992 * - Q is an odd multiple of the base point, P an even multiple,
993 * due to the choice of precomputed points in the modified comb method.
994 * So branches for these cases do not leak secret information.
995 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100996 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
997 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100998 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100999 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001000static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001001 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001002{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001003 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001004 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001005
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001006#if defined(POLARSSL_SELF_TEST)
1007 add_count++;
1008#endif
1009
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001010 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001011 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001012 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001013 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1014 return( ecp_copy( R, Q ) );
1015
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001016 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001017 return( ecp_copy( R, P ) );
1018
1019 /*
1020 * Make sure Q coordinates are normalized
1021 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001022 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001023 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001024
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001025 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1026 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001027
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001028 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1029 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1030 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1031 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1032 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1033 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001034
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001035 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001036 if( mpi_cmp_int( &T1, 0 ) == 0 )
1037 {
1038 if( mpi_cmp_int( &T2, 0 ) == 0 )
1039 {
1040 ret = ecp_double_jac( grp, R, P );
1041 goto cleanup;
1042 }
1043 else
1044 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001045 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001046 goto cleanup;
1047 }
1048 }
1049
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001050 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1051 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1052 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1053 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1054 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1055 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1056 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1057 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1058 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1059 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1060 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1061 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001062
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001063 MPI_CHK( mpi_copy( &R->X, &X ) );
1064 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1065 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001066
1067cleanup:
1068
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001069 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1070 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001071
1072 return( ret );
1073}
1074
1075/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001076 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001077 */
1078int ecp_add( const ecp_group *grp, ecp_point *R,
1079 const ecp_point *P, const ecp_point *Q )
1080{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001081 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001082
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001083 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001084 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1085
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001086 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001087 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001088
1089cleanup:
1090 return( ret );
1091}
1092
1093/*
1094 * Subtraction: R = P - Q, result's coordinates normalized
1095 */
1096int ecp_sub( const ecp_group *grp, ecp_point *R,
1097 const ecp_point *P, const ecp_point *Q )
1098{
1099 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001100 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001101
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001102 ecp_point_init( &mQ );
1103
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001104 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001105 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1106
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001107 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001108 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001109 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1110 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1111
1112 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001113 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001114
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001115cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001116 ecp_point_free( &mQ );
1117
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001118 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001119}
1120
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001121/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001122 * Randomize jacobian coordinates:
1123 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001124 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001125 *
1126 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001127 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001128static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001129 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1130{
1131 int ret;
1132 mpi l, ll;
Paul Bakker66d5d072014-06-17 16:39:18 +02001133 size_t p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001134 int count = 0;
1135
1136 mpi_init( &l ); mpi_init( &ll );
1137
1138 /* Generate l such that 1 < l < p */
1139 do
1140 {
1141 mpi_fill_random( &l, p_size, f_rng, p_rng );
1142
1143 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001144 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001145
1146 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001147 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001148 }
1149 while( mpi_cmp_int( &l, 1 ) <= 0 );
1150
1151 /* Z = l * Z */
1152 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1153
1154 /* X = l^2 * X */
1155 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1156 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1157
1158 /* Y = l^3 * Y */
1159 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1160 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1161
1162cleanup:
1163 mpi_free( &l ); mpi_free( &ll );
1164
1165 return( ret );
1166}
1167
1168/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001169 * Check and define parameters used by the comb method (see below for details)
1170 */
1171#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1172#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1173#endif
1174
1175/* d = ceil( n / w ) */
1176#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1177
1178/* number of precomputed points */
1179#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1180
1181/*
1182 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001183 *
1184 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001185 * modified version that provides resistance to SPA by avoiding zero
1186 * digits in the representation as in [3]. We modify the method further by
1187 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001188 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001189 *
1190 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1191 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1192 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001193 *
1194 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001195 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001196 * - w is the size, ie number of teeth, of the comb, and must be between
1197 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001198 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1199 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001200 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001201static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001202 unsigned char w, const mpi *m )
1203{
1204 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001205 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001206
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001207 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001208
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001209 /* First get the classical comb values (except for x_d = 0) */
1210 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001211 for( j = 0; j < w; j++ )
1212 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1213
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001214 /* Now make sure x_1 .. x_d are odd */
1215 c = 0;
1216 for( i = 1; i <= d; i++ )
1217 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001218 /* Add carry and update it */
1219 cc = x[i] & c;
1220 x[i] = x[i] ^ c;
1221 c = cc;
1222
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001223 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001224 adjust = 1 - ( x[i] & 0x01 );
1225 c |= x[i] & ( x[i-1] * adjust );
1226 x[i] = x[i] ^ ( x[i-1] * adjust );
1227 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001228 }
1229}
1230
1231/*
1232 * Precompute points for the comb method
1233 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001234 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1235 * 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 +01001236 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001237 * T must be able to hold 2^{w - 1} elements
1238 *
1239 * 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 +01001240 */
1241static int ecp_precompute_comb( const ecp_group *grp,
1242 ecp_point T[], const ecp_point *P,
1243 unsigned char w, size_t d )
1244{
1245 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001246 unsigned char i, k;
1247 size_t j;
1248 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001249
1250 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001251 * Set T[0] = P and
1252 * 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 +01001253 */
1254 MPI_CHK( ecp_copy( &T[0], P ) );
1255
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001256 k = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +02001257 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001258 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001259 cur = T + i;
1260 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001261 for( j = 0; j < d; j++ )
1262 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001263
1264 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001265 }
1266
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001267 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001268
1269 /*
1270 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001271 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001272 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001273 k = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +02001274 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001275 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001276 j = i;
1277 while( j-- )
1278 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001279 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001280 TT[k++] = &T[i + j];
1281 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001282 }
1283
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001284 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001285
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001286cleanup:
1287 return( ret );
1288}
1289
1290/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001291 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001292 */
1293static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001294 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001295 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001296{
1297 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001298 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001299
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001300 /* Ignore the "sign" bit and scale down */
1301 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001302
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001303 /* Read the whole table to thwart cache-based timing attacks */
1304 for( j = 0; j < t_len; j++ )
1305 {
1306 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1307 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1308 }
1309
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001310 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001311 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001312
1313cleanup:
1314 return( ret );
1315}
1316
1317/*
1318 * Core multiplication algorithm for the (modified) comb method.
1319 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001320 *
1321 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001322 */
1323static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001324 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001325 const unsigned char x[], size_t d,
1326 int (*f_rng)(void *, unsigned char *, size_t),
1327 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001328{
1329 int ret;
1330 ecp_point Txi;
1331 size_t i;
1332
1333 ecp_point_init( &Txi );
1334
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001335 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001336 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001337 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001338 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001339 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001340 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001341
1342 while( i-- != 0 )
1343 {
1344 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001345 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001346 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001347 }
1348
1349cleanup:
1350 ecp_point_free( &Txi );
1351
1352 return( ret );
1353}
1354
1355/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001356 * Multiplication using the comb method,
1357 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001358 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001359static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1360 const mpi *m, const ecp_point *P,
1361 int (*f_rng)(void *, unsigned char *, size_t),
1362 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001363{
1364 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001365 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1366 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001367 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001368 ecp_point *T;
1369 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001370
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001371 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001372 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001373
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001374 /* we need N to be odd to trnaform m in an odd number, check now */
1375 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1376 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1377
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001378 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001379 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001380 * 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 +01001381 * (see costs of the various parts, with 1S = 1M)
1382 */
1383 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001384
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001385 /*
1386 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001387 * Just adding one avoids upping the cost of the first mul too much,
1388 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001389 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001390#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001391 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1392 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001393 if( p_eq_g )
1394 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001395#else
1396 p_eq_g = 0;
1397#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001398
1399 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001400 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001401 * (The last test is useful only for very small curves in the test suite.)
1402 */
1403 if( w > POLARSSL_ECP_WINDOW_SIZE )
1404 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001405 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001406 w = 2;
1407
1408 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001409 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001410 d = ( grp->nbits + w - 1 ) / w;
1411
1412 /*
1413 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001414 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001415 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001416 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001417
1418 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001419 {
Mansour Moufidc531b4a2015-02-15 17:35:38 -05001420 T = polarssl_malloc( pre_len * sizeof( ecp_point ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001421 if( T == NULL )
1422 {
1423 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1424 goto cleanup;
1425 }
1426
1427 for( i = 0; i < pre_len; i++ )
1428 ecp_point_init( &T[i] );
1429
1430 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1431
1432 if( p_eq_g )
1433 {
1434 grp->T = T;
1435 grp->T_size = pre_len;
1436 }
1437 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001438
1439 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001440 * Make sure M is odd (M = m or M = N - m, since N is odd)
1441 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001442 */
1443 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001444 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001445 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1446 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001447
1448 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001449 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001450 */
1451 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001452 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 +01001453
1454 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001455 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001456 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001457 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1458 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001459
1460cleanup:
1461
1462 if( T != NULL && ! p_eq_g )
1463 {
1464 for( i = 0; i < pre_len; i++ )
1465 ecp_point_free( &T[i] );
1466 polarssl_free( T );
1467 }
1468
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001469 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001470 mpi_free( &mm );
1471
1472 if( ret != 0 )
1473 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001474
1475 return( ret );
1476}
1477
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001478#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1479
1480#if defined(POLARSSL_ECP_MONTGOMERY)
1481/*
1482 * For Montgomery curves, we do all the internal arithmetic in projective
1483 * coordinates. Import/export of points uses only the x coordinates, which is
1484 * internaly represented as X / Z.
1485 *
1486 * For scalar multiplication, we'll use a Montgomery ladder.
1487 */
1488
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001489/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001490 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1491 * Cost: 1M + 1I
1492 */
1493static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1494{
1495 int ret;
1496
1497 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1498 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1499 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1500
1501cleanup:
1502 return( ret );
1503}
1504
1505/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001506 * Randomize projective x/z coordinates:
1507 * (X, Z) -> (l X, l Z) for random l
1508 * This is sort of the reverse operation of ecp_normalize_mxz().
1509 *
1510 * This countermeasure was first suggested in [2].
1511 * Cost: 2M
1512 */
1513static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1514 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1515{
1516 int ret;
1517 mpi l;
Paul Bakker66d5d072014-06-17 16:39:18 +02001518 size_t p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001519 int count = 0;
1520
1521 mpi_init( &l );
1522
1523 /* Generate l such that 1 < l < p */
1524 do
1525 {
1526 mpi_fill_random( &l, p_size, f_rng, p_rng );
1527
1528 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001529 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001530
1531 if( count++ > 10 )
1532 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1533 }
1534 while( mpi_cmp_int( &l, 1 ) <= 0 );
1535
1536 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1537 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1538
1539cleanup:
1540 mpi_free( &l );
1541
1542 return( ret );
1543}
1544
1545/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001546 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1547 * for Montgomery curves in x/z coordinates.
1548 *
1549 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1550 * with
1551 * d = X1
1552 * P = (X2, Z2)
1553 * Q = (X3, Z3)
1554 * R = (X4, Z4)
1555 * S = (X5, Z5)
1556 * and eliminating temporary variables tO, ..., t4.
1557 *
1558 * Cost: 5M + 4S
1559 */
1560static int ecp_double_add_mxz( const ecp_group *grp,
1561 ecp_point *R, ecp_point *S,
1562 const ecp_point *P, const ecp_point *Q,
1563 const mpi *d )
1564{
1565 int ret;
1566 mpi A, AA, B, BB, E, C, D, DA, CB;
1567
1568 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1569 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1570 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1571
1572 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1573 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1574 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1575 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1576 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1577 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1578 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1579 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1580 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1581 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1582 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1583 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1584 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1585 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1586 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1587 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1588 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1589 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1590
1591cleanup:
1592 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1593 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1594 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1595
1596 return( ret );
1597}
1598
1599/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001600 * Multiplication with Montgomery ladder in x/z coordinates,
1601 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001602 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001603static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1604 const mpi *m, const ecp_point *P,
1605 int (*f_rng)(void *, unsigned char *, size_t),
1606 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001607{
1608 int ret;
1609 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001610 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001611 ecp_point RP;
1612 mpi PX;
1613
1614 ecp_point_init( &RP ); mpi_init( &PX );
1615
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001616 /* Save PX and read from P before writing to R, in case P == R */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001617 MPI_CHK( mpi_copy( &PX, &P->X ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001618 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001619
1620 /* Set R to zero in modified x/z coordinates */
1621 MPI_CHK( mpi_lset( &R->X, 1 ) );
1622 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1623 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001624
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001625 /* RP.X might be sligtly larger than P, so reduce it */
1626 MOD_ADD( RP.X );
1627
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001628 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001629 if( f_rng != NULL )
1630 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001631
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001632 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001633 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001634 while( i-- > 0 )
1635 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001636 b = mpi_get_bit( m, i );
1637 /*
1638 * if (b) R = 2R + P else R = 2R,
1639 * which is:
1640 * if (b) double_add( RP, R, RP, R )
1641 * else double_add( R, RP, R, RP )
1642 * but using safe conditional swaps to avoid leaks
1643 */
1644 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1645 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1646 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1647 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1648 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001649 }
1650
1651 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1652
1653cleanup:
1654 ecp_point_free( &RP ); mpi_free( &PX );
1655
1656 return( ret );
1657}
1658
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001659#endif /* POLARSSL_ECP_MONTGOMERY */
1660
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001661/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001662 * Multiplication R = m * P
1663 */
1664int ecp_mul( ecp_group *grp, ecp_point *R,
1665 const mpi *m, const ecp_point *P,
1666 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1667{
1668 int ret;
1669
1670 /* Common sanity checks */
1671 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1672 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1673
1674 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1675 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1676 return( ret );
1677
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001678#if defined(POLARSSL_ECP_MONTGOMERY)
1679 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001680 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001681#endif
1682#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1683 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001684 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001685#endif
1686 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001687}
1688
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001689#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001690/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001691 * Check that an affine point is valid as a public key,
1692 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001693 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001694static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001695{
1696 int ret;
1697 mpi YY, RHS;
1698
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001699 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001700 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1701 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1702 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1703 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001704 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001705
1706 mpi_init( &YY ); mpi_init( &RHS );
1707
1708 /*
1709 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001710 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001711 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001712 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1713 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001714
1715 /* Special case for A = -3 */
1716 if( grp->A.p == NULL )
1717 {
1718 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1719 }
1720 else
1721 {
1722 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1723 }
1724
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001725 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1726 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001727
1728 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001729 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001730
1731cleanup:
1732
1733 mpi_free( &YY ); mpi_free( &RHS );
1734
1735 return( ret );
1736}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001737#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1738
1739
1740#if defined(POLARSSL_ECP_MONTGOMERY)
1741/*
1742 * Check validity of a public key for Montgomery curves with x-only schemes
1743 */
1744static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1745{
1746 /* [M255 p. 5] Just check X is the correct number of bytes */
1747 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1748 return( POLARSSL_ERR_ECP_INVALID_KEY );
1749
1750 return( 0 );
1751}
1752#endif /* POLARSSL_ECP_MONTGOMERY */
1753
1754/*
1755 * Check that a point is valid as a public key
1756 */
1757int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1758{
1759 /* Must use affine coordinates */
1760 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1761 return( POLARSSL_ERR_ECP_INVALID_KEY );
1762
1763#if defined(POLARSSL_ECP_MONTGOMERY)
1764 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1765 return( ecp_check_pubkey_mx( grp, pt ) );
1766#endif
1767#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1768 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1769 return( ecp_check_pubkey_sw( grp, pt ) );
1770#endif
1771 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1772}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001773
1774/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001775 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001776 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001777int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001778{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001779#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001780 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001781 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001782 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001783 if( mpi_get_bit( d, 0 ) != 0 ||
1784 mpi_get_bit( d, 1 ) != 0 ||
1785 mpi_get_bit( d, 2 ) != 0 ||
1786 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1787 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001788 else
1789 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001790 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001791#endif /* POLARSSL_ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001792#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1793 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001794 {
1795 /* see SEC1 3.2 */
1796 if( mpi_cmp_int( d, 1 ) < 0 ||
1797 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1798 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001799 else
1800 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001801 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001802#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001803
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001804 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001805}
1806
1807/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001808 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001809 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001810int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001811 int (*f_rng)(void *, unsigned char *, size_t),
1812 void *p_rng )
1813{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001814 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +02001815 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001816
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001817#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001818 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001819 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001820 /* [M225] page 5 */
1821 size_t b;
1822
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001823 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001824
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001825 /* Make sure the most significant bit is nbits */
1826 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1827 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001828 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001829 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001830 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001831
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001832 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001833 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1834 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1835 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001836 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001837 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001838#endif /* POLARSSL_ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001839#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1840 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001841 {
1842 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001843 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001844 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1845
1846 /*
1847 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1848 * - use the same byte ordering;
1849 * - keep the leftmost nbits bits of the generated octet string;
1850 * - try until result is in the desired range.
1851 * This also avoids any biais, which is especially important for ECDSA.
1852 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001853 do
1854 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001855 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1856 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1857 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001858
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001859 /*
1860 * Each try has at worst a probability 1/2 of failing (the msb has
1861 * a probability 1/2 of being 0, and then the result will be < N),
1862 * so after 30 tries failure probability is a most 2**(-30).
1863 *
1864 * For most curves, 1 try is enough with overwhelming probability,
1865 * since N starts with a lot of 1s in binary, but some curves
1866 * such as secp224k1 are actually very close to the worst case.
1867 */
1868 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001869 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1870 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001871 while( mpi_cmp_int( d, 1 ) < 0 ||
1872 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001873 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001874 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001875#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001876 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001877
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001878cleanup:
1879 if( ret != 0 )
1880 return( ret );
1881
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001882 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001883}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001884
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001885/*
1886 * Generate a keypair, prettier wrapper
1887 */
1888int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1889 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1890{
1891 int ret;
1892
1893 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1894 return( ret );
1895
1896 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1897}
1898
Manuel Pégourié-Gonnard30668d62014-11-06 15:25:32 +01001899/*
1900 * Check a public-private key pair
1901 */
1902int ecp_check_pub_priv( const ecp_keypair *pub, const ecp_keypair *prv )
1903{
1904 int ret;
1905 ecp_point Q;
1906 ecp_group grp;
1907
1908 if( pub->grp.id == POLARSSL_ECP_DP_NONE ||
1909 pub->grp.id != prv->grp.id ||
1910 mpi_cmp_mpi( &pub->Q.X, &prv->Q.X ) ||
1911 mpi_cmp_mpi( &pub->Q.Y, &prv->Q.Y ) ||
1912 mpi_cmp_mpi( &pub->Q.Z, &prv->Q.Z ) )
1913 {
1914 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1915 }
1916
1917 ecp_point_init( &Q );
1918 ecp_group_init( &grp );
1919
1920 /* ecp_mul() needs a non-const group... */
1921 ecp_group_copy( &grp, &prv->grp );
1922
1923 /* Also checks d is valid */
1924 MPI_CHK( ecp_mul( &grp, &Q, &prv->d, &prv->grp.G, NULL, NULL ) );
1925
1926 if( mpi_cmp_mpi( &Q.X, &prv->Q.X ) ||
1927 mpi_cmp_mpi( &Q.Y, &prv->Q.Y ) ||
1928 mpi_cmp_mpi( &Q.Z, &prv->Q.Z ) )
1929 {
1930 ret = POLARSSL_ERR_ECP_BAD_INPUT_DATA;
1931 goto cleanup;
1932 }
1933
1934cleanup:
1935 ecp_point_free( &Q );
1936 ecp_group_free( &grp );
1937
1938 return( ret );
1939}
1940
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001941#if defined(POLARSSL_SELF_TEST)
1942
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001943/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001944 * Checkup routine
1945 */
1946int ecp_self_test( int verbose )
1947{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001948 int ret;
1949 size_t i;
1950 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001951 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001952 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001953 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001954 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001955 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001956 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001957 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001958 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001959 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001960 "400000000000000000000000000000000000000000000000", /* one and zeros */
1961 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1962 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001963 };
1964
1965 ecp_group_init( &grp );
1966 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001967 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001968 mpi_init( &m );
1969
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001970 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001971#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001972 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001973#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001974 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1975#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001976
1977 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001978 polarssl_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001979
1980 /* Do a dummy multiplication first to trigger precomputation */
1981 MPI_CHK( mpi_lset( &m, 2 ) );
1982 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001983
1984 add_count = 0;
1985 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001986 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001987 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001988 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001989
1990 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1991 {
1992 add_c_prev = add_count;
1993 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001994 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001995 add_count = 0;
1996 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001997 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001998
1999 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02002000 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002001
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002002 if( add_count != add_c_prev ||
2003 dbl_count != dbl_c_prev ||
2004 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002005 {
2006 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002007 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002008
2009 ret = 1;
2010 goto cleanup;
2011 }
2012 }
2013
2014 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002015 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002016
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002017 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002018 polarssl_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002019 /* We computed P = 2G last time, use it */
2020
2021 add_count = 0;
2022 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002023 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002024 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2025 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2026
2027 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2028 {
2029 add_c_prev = add_count;
2030 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002031 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002032 add_count = 0;
2033 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002034 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002035
2036 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2037 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2038
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002039 if( add_count != add_c_prev ||
2040 dbl_count != dbl_c_prev ||
2041 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002042 {
2043 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002044 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002045
2046 ret = 1;
2047 goto cleanup;
2048 }
2049 }
2050
2051 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002052 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002053
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002054cleanup:
2055
2056 if( ret < 0 && verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002057 polarssl_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002058
2059 ecp_group_free( &grp );
2060 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002061 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002062 mpi_free( &m );
2063
2064 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002065 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002066
2067 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002068}
2069
Paul Bakker9af723c2014-05-01 13:03:14 +02002070#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002071
Paul Bakker9af723c2014-05-01 13:03:14 +02002072#endif /* POLARSSL_ECP_C */