blob: 2801db60be25ee088374b75cf329ff2ca4c35502 [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 *
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010034 * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
35 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020036 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
37 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
38 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
39 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010040 *
41 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
42 * render ECC resistant against Side Channel Attacks. IACR Cryptology
43 * ePrint Archive, 2004, vol. 2004, p. 342.
44 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045 */
46
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020047#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010048#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
50#include POLARSSL_CONFIG_FILE
51#endif
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010052
53#if defined(POLARSSL_ECP_C)
54
55#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020056
Paul Bakker7dc4c442014-02-01 22:50:26 +010057#if defined(POLARSSL_PLATFORM_C)
58#include "polarssl/platform.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020059#else
Paul Bakker7dc4c442014-02-01 22:50:26 +010060#define polarssl_printf printf
Paul Bakker6e339b52013-07-03 13:37:05 +020061#define polarssl_malloc malloc
62#define polarssl_free free
63#endif
64
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010065#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010066
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010067#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
68 !defined(EFI32)
69#define strcasecmp _stricmp
70#endif
71
Paul Bakker6a6087e2013-10-28 18:53:08 +010072#if defined(_MSC_VER) && !defined(inline)
73#define inline _inline
74#else
75#if defined(__ARMCC_VERSION) && !defined(inline)
76#define inline __inline
77#endif /* __ARMCC_VERSION */
78#endif /*_MSC_VER */
79
Paul Bakker34617722014-06-13 17:20:13 +020080/* Implementation that should never be optimized out by the compiler */
81static void polarssl_zeroize( void *v, size_t n ) {
82 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
83}
84
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010085#if defined(POLARSSL_SELF_TEST)
86/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010087 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020088 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010089 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010090static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010091#endif
92
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010093#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
94 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
95 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
96 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
97 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
98 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
99 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
Manuel Pégourié-Gonnard2a2ae642014-02-24 08:29:51 +0100100 defined(POLARSSL_ECP_DP_BP512R1_ENABLED) || \
101 defined(POLARSSL_ECP_DP_SECP192K1_ENABLED) || \
102 defined(POLARSSL_ECP_DP_SECP224K1_ENABLED) || \
103 defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100104#define POLARSSL_ECP_SHORT_WEIERSTRASS
105#endif
106
107#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
108 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
109 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
110 defined(POLARSSL_ECP_DP_M511_ENABLED)
111#define POLARSSL_ECP_MONTGOMERY
112#endif
113
114/*
115 * Curve types: internal for now, might be exposed later
116 */
117typedef enum
118{
119 POLARSSL_ECP_TYPE_NONE = 0,
120 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
121 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
122} ecp_curve_type;
123
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100124/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200125 * List of supported curves:
126 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200127 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200128 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200129 * - readable name
Gergely Budaie40c4692014-01-22 11:22:20 +0100130 *
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100131 * Curves are listed in order: largest curves first, and for a given size,
132 * fastest curves first. This provides the default order for the SSL module.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200133 */
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200134static const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200135{
136#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200137 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200138#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100139#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
140 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
141#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200142#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200143 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200144#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100145#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
146 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
147#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200148#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200149 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200150#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100151#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
152 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
153#endif
Gergely Budaie40c4692014-01-22 11:22:20 +0100154#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
155 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
156#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200157#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200158 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200159#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100160#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
161 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
162#endif
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100163#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
164 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
165#endif
Manuel Pégourié-Gonnard9bcff392014-01-10 18:26:48 +0100166#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
167 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
168#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200169 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200170};
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100171
Manuel Pégourié-Gonnardba782bb2014-07-08 13:31:34 +0200172#define ECP_NB_CURVES sizeof( ecp_supported_curves ) / \
173 sizeof( ecp_supported_curves[0] )
174
175static ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200176
177/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200178 * List of supported curves and associated info
179 */
180const ecp_curve_info *ecp_curve_list( void )
181{
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200182 return( ecp_supported_curves );
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200183}
184
185/*
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100186 * List of supported curves, group ID only
187 */
188const ecp_group_id *ecp_grp_id_list( void )
189{
190 static int init_done = 0;
191
192 if( ! init_done )
193 {
194 size_t i = 0;
195 const ecp_curve_info *curve_info;
196
197 for( curve_info = ecp_curve_list();
198 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
199 curve_info++ )
200 {
201 ecp_supported_grp_id[i++] = curve_info->grp_id;
202 }
203 ecp_supported_grp_id[i] = POLARSSL_ECP_DP_NONE;
204
205 init_done = 1;
206 }
207
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200208 return( ecp_supported_grp_id );
Manuel Pégourié-Gonnardac719412014-02-04 14:48:50 +0100209}
210
211/*
212 * Get the curve info for the internal identifier
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200213 */
214const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
215{
216 const ecp_curve_info *curve_info;
217
218 for( curve_info = ecp_curve_list();
219 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
220 curve_info++ )
221 {
222 if( curve_info->grp_id == grp_id )
223 return( curve_info );
224 }
225
226 return( NULL );
227}
228
229/*
230 * Get the curve info from the TLS identifier
231 */
232const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
233{
234 const ecp_curve_info *curve_info;
235
236 for( curve_info = ecp_curve_list();
237 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
238 curve_info++ )
239 {
240 if( curve_info->tls_id == tls_id )
241 return( curve_info );
242 }
243
244 return( NULL );
245}
246
247/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100248 * Get the curve info from the name
249 */
250const ecp_curve_info *ecp_curve_info_from_name( const char *name )
251{
252 const ecp_curve_info *curve_info;
253
254 for( curve_info = ecp_curve_list();
255 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
256 curve_info++ )
257 {
258 if( strcasecmp( curve_info->name, name ) == 0 )
259 return( curve_info );
260 }
261
262 return( NULL );
263}
264
265/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100266 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100267 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100268static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100269{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100270 if( grp->G.X.p == NULL )
271 return( POLARSSL_ECP_TYPE_NONE );
272
273 if( grp->G.Y.p == NULL )
274 return( POLARSSL_ECP_TYPE_MONTGOMERY );
275 else
276 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100277}
278
279/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100280 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100281 */
282void ecp_point_init( ecp_point *pt )
283{
284 if( pt == NULL )
285 return;
286
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100287 mpi_init( &pt->X );
288 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100289 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100290}
291
292/*
293 * Initialize (the components of) a group
294 */
295void ecp_group_init( ecp_group *grp )
296{
297 if( grp == NULL )
298 return;
299
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200300 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100301}
302
303/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200304 * Initialize (the components of) a key pair
305 */
306void ecp_keypair_init( ecp_keypair *key )
307{
Paul Bakker66d5d072014-06-17 16:39:18 +0200308 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200309 return;
310
311 ecp_group_init( &key->grp );
312 mpi_init( &key->d );
313 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200314}
315
316/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100317 * Unallocate (the components of) a point
318 */
319void ecp_point_free( ecp_point *pt )
320{
321 if( pt == NULL )
322 return;
323
324 mpi_free( &( pt->X ) );
325 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100326 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100327}
328
329/*
330 * Unallocate (the components of) a group
331 */
332void ecp_group_free( ecp_group *grp )
333{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200334 size_t i;
335
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100336 if( grp == NULL )
337 return;
338
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100339 if( grp->h != 1 )
340 {
341 mpi_free( &grp->P );
342 mpi_free( &grp->A );
343 mpi_free( &grp->B );
344 ecp_point_free( &grp->G );
345 mpi_free( &grp->N );
346 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200347
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200348 if( grp->T != NULL )
349 {
350 for( i = 0; i < grp->T_size; i++ )
351 ecp_point_free( &grp->T[i] );
352 polarssl_free( grp->T );
353 }
354
Paul Bakker34617722014-06-13 17:20:13 +0200355 polarssl_zeroize( grp, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100356}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100357
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100358/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200359 * Unallocate (the components of) a key pair
360 */
361void ecp_keypair_free( ecp_keypair *key )
362{
Paul Bakker66d5d072014-06-17 16:39:18 +0200363 if( key == NULL )
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200364 return;
365
366 ecp_group_free( &key->grp );
367 mpi_free( &key->d );
368 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200369}
370
371/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200372 * Copy the contents of a point
373 */
374int ecp_copy( ecp_point *P, const ecp_point *Q )
375{
376 int ret;
377
378 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
379 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
380 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
381
382cleanup:
383 return( ret );
384}
385
386/*
387 * Copy the contents of a group object
388 */
389int ecp_group_copy( ecp_group *dst, const ecp_group *src )
390{
391 return ecp_use_known_dp( dst, src->id );
392}
393
394/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100395 * Set point to zero
396 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100397int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100398{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100399 int ret;
400
401 MPI_CHK( mpi_lset( &pt->X , 1 ) );
402 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
403 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
404
405cleanup:
406 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100407}
408
409/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100410 * Tell if a point is zero
411 */
412int ecp_is_zero( ecp_point *pt )
413{
414 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
415}
416
417/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100418 * Import a non-zero point from ASCII strings
419 */
420int ecp_point_read_string( ecp_point *P, int radix,
421 const char *x, const char *y )
422{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100423 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100424
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100425 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
426 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100427 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100428
429cleanup:
430 return( ret );
431}
432
433/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100434 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100435 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100436int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100437 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100438 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100439{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200440 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100441 size_t plen;
442
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100443 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
444 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100445 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100446
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100447 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100448 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100449 */
450 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
451 {
452 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100453 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100454
455 buf[0] = 0x00;
456 *olen = 1;
457
458 return( 0 );
459 }
460
461 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100462
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100463 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
464 {
465 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100466
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100467 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100468 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100469
470 buf[0] = 0x04;
471 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
472 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
473 }
474 else if( format == POLARSSL_ECP_PF_COMPRESSED )
475 {
476 *olen = plen + 1;
477
478 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100479 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100480
481 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
482 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
483 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100484
485cleanup:
486 return( ret );
487}
488
489/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100490 * Import a point from unsigned binary data (SEC1 2.3.4)
491 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100492int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100493 const unsigned char *buf, size_t ilen )
494{
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100495 int ret;
496 size_t plen;
497
Paul Bakker82788fb2014-10-20 13:59:19 +0200498 if( ilen < 1 )
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200499 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
500
Manuel Pégourié-Gonnardc042cf02014-03-26 14:12:20 +0100501 if( buf[0] == 0x00 )
502 {
503 if( ilen == 1 )
504 return( ecp_set_zero( pt ) );
505 else
506 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
507 }
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100508
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100509 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100510
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100511 if( buf[0] != 0x04 )
512 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
513
514 if( ilen != 2 * plen + 1 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100515 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100516
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100517 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
518 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
519 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100520
521cleanup:
522 return( ret );
523}
524
525/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100526 * Import a point from a TLS ECPoint record (RFC 4492)
527 * struct {
528 * opaque point <1..2^8-1>;
529 * } ECPoint;
530 */
531int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100532 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100533{
534 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100535 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100536
537 /*
Manuel Pégourié-Gonnard67dbe1e2014-07-08 13:09:24 +0200538 * We must have at least two bytes (1 for length, at least one for data)
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100539 */
540 if( buf_len < 2 )
541 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
542
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100543 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100544 if( data_len < 1 || data_len > buf_len - 1 )
545 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
546
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100547 /*
548 * Save buffer start for read_binary and update buf
549 */
550 buf_start = *buf;
551 *buf += data_len;
552
553 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100554}
555
556/*
557 * Export a point as a TLS ECPoint record (RFC 4492)
558 * struct {
559 * opaque point <1..2^8-1>;
560 * } ECPoint;
561 */
562int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100563 int format, size_t *olen,
564 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100565{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100566 int ret;
567
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100568 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100569 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100570 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100571 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100572 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
573
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100574 if( ( ret = ecp_point_write_binary( grp, pt, format,
575 olen, buf + 1, blen - 1) ) != 0 )
576 return( ret );
577
578 /*
579 * write length to the first byte and update total length
580 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200581 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100582 ++*olen;
583
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200584 return( 0 );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100585}
586
587/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200588 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200589 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200590int ecp_group_read_string( ecp_group *grp, int radix,
591 const char *p, const char *b,
592 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100593{
594 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100595
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100596 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100597 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
598 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
599 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
600
601 grp->pbits = mpi_msb( &grp->P );
602 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100603
604cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200605 if( ret != 0 )
606 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200607
608 return( ret );
609}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200610
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100611/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100612 * Set a group from an ECParameters record (RFC 4492)
613 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100614int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100615{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200616 uint16_t tls_id;
617 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100618
619 /*
620 * We expect at least three bytes (see below)
621 */
622 if( len < 3 )
623 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
624
625 /*
626 * First byte is curve_type; only named_curve is handled
627 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100628 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100629 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
630
631 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100632 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100633 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200634 tls_id = *(*buf)++;
635 tls_id <<= 8;
636 tls_id |= *(*buf)++;
637
638 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
639 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
640
641 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100642}
643
644/*
645 * Write the ECParameters record corresponding to a group (RFC 4492)
646 */
647int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
648 unsigned char *buf, size_t blen )
649{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200650 const ecp_curve_info *curve_info;
651
652 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
653 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200654
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100655 /*
656 * We are going to write 3 bytes (see below)
657 */
658 *olen = 3;
659 if( blen < *olen )
660 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
661
662 /*
663 * First byte is curve_type, always named_curve
664 */
665 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
666
667 /*
668 * Next two bytes are the namedcurve value
669 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200670 buf[0] = curve_info->tls_id >> 8;
671 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100672
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200673 return( 0 );
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100674}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100675
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200676/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200677 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
678 * See the documentation of struct ecp_group.
679 *
680 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200681 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200682static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200683{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200684 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200685
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200686 if( grp->modp == NULL )
687 return( mpi_mod_mpi( N, N, &grp->P ) );
688
689 /* N->s < 0 is a much faster test, which fails only if N is 0 */
690 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
691 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200692 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200693 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200694 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200695
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200696 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200697
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200698 /* N->s < 0 is a much faster test, which fails only if N is 0 */
699 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
700 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200701
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200702 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
703 /* we known P, N and the result are positive */
704 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200705
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200706cleanup:
707 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200708}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200709
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100710/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100711 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100712 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100713 * In order to guarantee that, we need to ensure that operands of
714 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100715 * bring the result back to this range.
716 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100717 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100718 */
719
720/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100721 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
722 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100723#if defined(POLARSSL_SELF_TEST)
724#define INC_MUL_COUNT mul_count++;
725#else
726#define INC_MUL_COUNT
727#endif
728
729#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
730 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100731
732/*
733 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200734 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100735 */
736#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200737 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100738 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
739
740/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200741 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
742 * We known P, N and the result are positive, so sub_abs is correct, and
743 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100744 */
745#define MOD_ADD( N ) \
746 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200747 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100748
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100749#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
750/*
751 * For curves in short Weierstrass form, we do all the internal operations in
752 * Jacobian coordinates.
753 *
754 * For multiplication, we'll use a comb method with coutermeasueres against
755 * SPA, hence timing attacks.
756 */
757
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100758/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100759 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100760 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100761 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100762static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100763{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100764 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100765 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100766
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100767 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100768 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100769
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100770 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100771
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100772 /*
773 * X = X / Z^2 mod p
774 */
775 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
776 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
777 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100778
779 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100780 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100781 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100782 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
783 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100784
785 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100786 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100787 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100788 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100789
790cleanup:
791
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100792 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100793
794 return( ret );
795}
796
797/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100798 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100799 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100800 * (See for example Cohen's "A Course in Computational Algebraic Number
801 * Theory", Algorithm 10.3.4.)
802 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200803 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100804 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100805 *
806 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100807 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100808static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100809 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100810{
811 int ret;
812 size_t i;
813 mpi *c, u, Zi, ZZi;
814
815 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100816 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100817
Paul Bakker6e339b52013-07-03 13:37:05 +0200818 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200819 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100820
821 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
822 for( i = 0; i < t_len; i++ )
823 mpi_init( &c[i] );
824
825 /*
826 * c[i] = Z_0 * ... * Z_i
827 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100828 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100829 for( i = 1; i < t_len; i++ )
830 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100831 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100832 MOD_MUL( c[i] );
833 }
834
835 /*
836 * u = 1 / (Z_0 * ... * Z_n) mod P
837 */
838 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
839
840 for( i = t_len - 1; ; i-- )
841 {
842 /*
843 * Zi = 1 / Z_i mod p
844 * u = 1 / (Z_0 * ... * Z_i) mod P
845 */
846 if( i == 0 ) {
847 MPI_CHK( mpi_copy( &Zi, &u ) );
848 }
849 else
850 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100851 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
852 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100853 }
854
855 /*
856 * proceed as in normalize()
857 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100858 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
859 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
860 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
861 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 +0100862
863 /*
864 * Post-precessing: reclaim some memory by shrinking coordinates
865 * - not storing Z (always 1)
866 * - shrinking other coordinates, but still keeping the same number of
867 * limbs as P, as otherwise it will too likely be regrown too fast.
868 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100869 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
870 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100871 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100872
873 if( i == 0 )
874 break;
875 }
876
877cleanup:
878
879 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
880 for( i = 0; i < t_len; i++ )
881 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200882 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100883
884 return( ret );
885}
886
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100887/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100888 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
889 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
890 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100891static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100892 ecp_point *Q,
893 unsigned char inv )
894{
895 int ret;
896 unsigned char nonzero;
897 mpi mQY;
898
899 mpi_init( &mQY );
900
901 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
902 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
903 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
904 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
905
906cleanup:
907 mpi_free( &mQY );
908
909 return( ret );
910}
911
912/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200913 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200914 *
Peter Dettmance661b22015-02-07 14:43:51 +0700915 * Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100916 *
Peter Dettmance661b22015-02-07 14:43:51 +0700917 * We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
918 * (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
919 *
920 * Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
921 *
922 * Cost: 1D := 3M + 4S (A == 0)
923 * 4M + 4S (A == -3)
924 * 3M + 6S + 1a otherwise
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200925 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200926static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
927 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200928{
929 int ret;
Peter Dettmance661b22015-02-07 14:43:51 +0700930 mpi M, S, T, U;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200931
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200932#if defined(POLARSSL_SELF_TEST)
933 dbl_count++;
934#endif
935
Peter Dettmance661b22015-02-07 14:43:51 +0700936 mpi_init( &M ); mpi_init( &S ); mpi_init( &T ); mpi_init( &U );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100937
938 /* Special case for A = -3 */
939 if( grp->A.p == NULL )
940 {
Peter Dettmance661b22015-02-07 14:43:51 +0700941 /* M = 3(X + Z^2)(X - Z^2) */
942 MPI_CHK( mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
943 MPI_CHK( mpi_add_mpi( &T, &P->X, &S ) ); MOD_ADD( T );
944 MPI_CHK( mpi_sub_mpi( &U, &P->X, &S ) ); MOD_SUB( U );
945 MPI_CHK( mpi_mul_mpi( &S, &T, &U ) ); MOD_MUL( S );
946 MPI_CHK( mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100947 }
948 else
Peter Vaskovica676acf2014-08-06 00:48:39 +0200949 {
Peter Dettmance661b22015-02-07 14:43:51 +0700950 /* M = 3.X^2 */
951 MPI_CHK( mpi_mul_mpi( &S, &P->X, &P->X ) ); MOD_MUL( S );
952 MPI_CHK( mpi_mul_int( &M, &S, 3 ) ); MOD_ADD( M );
953
954 /* Optimize away for "koblitz" curves with A = 0 */
955 if( mpi_cmp_int( &grp->A, 0 ) != 0 )
956 {
957 /* M += A.Z^4 */
958 MPI_CHK( mpi_mul_mpi( &S, &P->Z, &P->Z ) ); MOD_MUL( S );
959 MPI_CHK( mpi_mul_mpi( &T, &S, &S ) ); MOD_MUL( T );
960 MPI_CHK( mpi_mul_mpi( &S, &T, &grp->A ) ); MOD_MUL( S );
961 MPI_CHK( mpi_add_mpi( &M, &M, &S ) ); MOD_ADD( M );
962 }
Peter Vaskovica676acf2014-08-06 00:48:39 +0200963 }
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100964
Peter Dettmance661b22015-02-07 14:43:51 +0700965 /* S = 4.X.Y^2 */
966 MPI_CHK( mpi_mul_mpi( &T, &P->Y, &P->Y ) ); MOD_MUL( T );
967 MPI_CHK( mpi_shift_l( &T, 1 ) ); MOD_ADD( T );
968 MPI_CHK( mpi_mul_mpi( &S, &P->X, &T ) ); MOD_MUL( S );
969 MPI_CHK( mpi_shift_l( &S, 1 ) ); MOD_ADD( S );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200970
Peter Dettmance661b22015-02-07 14:43:51 +0700971 /* U = 8.Y^4 */
972 MPI_CHK( mpi_mul_mpi( &U, &T, &T ) ); MOD_MUL( U );
973 MPI_CHK( mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
974
975 /* T = M^2 - 2.S */
976 MPI_CHK( mpi_mul_mpi( &T, &M, &M ) ); MOD_MUL( T );
977 MPI_CHK( mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
978 MPI_CHK( mpi_sub_mpi( &T, &T, &S ) ); MOD_SUB( T );
979
980 /* S = M(S - T) - U */
981 MPI_CHK( mpi_sub_mpi( &S, &S, &T ) ); MOD_SUB( S );
982 MPI_CHK( mpi_mul_mpi( &S, &S, &M ) ); MOD_MUL( S );
983 MPI_CHK( mpi_sub_mpi( &S, &S, &U ) ); MOD_SUB( S );
984
985 /* U = 2.Y.Z */
986 MPI_CHK( mpi_mul_mpi( &U, &P->Y, &P->Z ) ); MOD_MUL( U );
987 MPI_CHK( mpi_shift_l( &U, 1 ) ); MOD_ADD( U );
988
989 MPI_CHK( mpi_copy( &R->X, &T ) );
990 MPI_CHK( mpi_copy( &R->Y, &S ) );
991 MPI_CHK( mpi_copy( &R->Z, &U ) );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200992
993cleanup:
Peter Dettmance661b22015-02-07 14:43:51 +0700994 mpi_free( &M ); mpi_free( &S ); mpi_free( &T ); mpi_free( &U );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200995
996 return( ret );
997}
998
999/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001000 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001001 *
1002 * The coordinates of Q must be normalized (= affine),
1003 * but those of P don't need to. R is not normalized.
1004 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001005 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +01001006 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001007 * - at each step, P, Q and R are multiples of the base point, the factor
1008 * being less than its order, so none of them is zero;
1009 * - Q is an odd multiple of the base point, P an even multiple,
1010 * due to the choice of precomputed points in the modified comb method.
1011 * So branches for these cases do not leak secret information.
1012 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001013 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
1014 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001015 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001016 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001017static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001018 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001019{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001020 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001021 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001022
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001023#if defined(POLARSSL_SELF_TEST)
1024 add_count++;
1025#endif
1026
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001027 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001028 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001029 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001030 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
1031 return( ecp_copy( R, Q ) );
1032
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001033 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001034 return( ecp_copy( R, P ) );
1035
1036 /*
1037 * Make sure Q coordinates are normalized
1038 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001039 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001040 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001041
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001042 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
1043 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +01001044
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001045 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
1046 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
1047 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
1048 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
1049 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
1050 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001051
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001052 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001053 if( mpi_cmp_int( &T1, 0 ) == 0 )
1054 {
1055 if( mpi_cmp_int( &T2, 0 ) == 0 )
1056 {
1057 ret = ecp_double_jac( grp, R, P );
1058 goto cleanup;
1059 }
1060 else
1061 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001062 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001063 goto cleanup;
1064 }
1065 }
1066
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001067 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
1068 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
1069 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
1070 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
1071 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
1072 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
1073 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
1074 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
1075 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
1076 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1077 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1078 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001079
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001080 MPI_CHK( mpi_copy( &R->X, &X ) );
1081 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1082 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001083
1084cleanup:
1085
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001086 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1087 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001088
1089 return( ret );
1090}
1091
1092/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001093 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001094 */
1095int ecp_add( const ecp_group *grp, ecp_point *R,
1096 const ecp_point *P, const ecp_point *Q )
1097{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001098 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001099
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001100 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001101 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1102
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001103 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001104 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001105
1106cleanup:
1107 return( ret );
1108}
1109
1110/*
1111 * Subtraction: R = P - Q, result's coordinates normalized
1112 */
1113int ecp_sub( const ecp_group *grp, ecp_point *R,
1114 const ecp_point *P, const ecp_point *Q )
1115{
1116 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001117 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001118
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001119 ecp_point_init( &mQ );
1120
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001121 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001122 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1123
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001124 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001125 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001126 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1127 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1128
1129 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001130 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001131
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001132cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001133 ecp_point_free( &mQ );
1134
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001135 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001136}
1137
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001138/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001139 * Randomize jacobian coordinates:
1140 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001141 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001142 *
1143 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001144 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001145static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001146 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1147{
1148 int ret;
1149 mpi l, ll;
Paul Bakker66d5d072014-06-17 16:39:18 +02001150 size_t p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001151 int count = 0;
1152
1153 mpi_init( &l ); mpi_init( &ll );
1154
1155 /* Generate l such that 1 < l < p */
1156 do
1157 {
1158 mpi_fill_random( &l, p_size, f_rng, p_rng );
1159
1160 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001161 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001162
1163 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001164 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001165 }
1166 while( mpi_cmp_int( &l, 1 ) <= 0 );
1167
1168 /* Z = l * Z */
1169 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1170
1171 /* X = l^2 * X */
1172 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1173 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1174
1175 /* Y = l^3 * Y */
1176 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1177 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1178
1179cleanup:
1180 mpi_free( &l ); mpi_free( &ll );
1181
1182 return( ret );
1183}
1184
1185/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001186 * Check and define parameters used by the comb method (see below for details)
1187 */
1188#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1189#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1190#endif
1191
1192/* d = ceil( n / w ) */
1193#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1194
1195/* number of precomputed points */
1196#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1197
1198/*
1199 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001200 *
1201 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001202 * modified version that provides resistance to SPA by avoiding zero
1203 * digits in the representation as in [3]. We modify the method further by
1204 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001205 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001206 *
1207 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1208 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1209 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001210 *
1211 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001212 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001213 * - w is the size, ie number of teeth, of the comb, and must be between
1214 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001215 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1216 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001217 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001218static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001219 unsigned char w, const mpi *m )
1220{
1221 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001222 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001223
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001224 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001225
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001226 /* First get the classical comb values (except for x_d = 0) */
1227 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001228 for( j = 0; j < w; j++ )
1229 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1230
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001231 /* Now make sure x_1 .. x_d are odd */
1232 c = 0;
1233 for( i = 1; i <= d; i++ )
1234 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001235 /* Add carry and update it */
1236 cc = x[i] & c;
1237 x[i] = x[i] ^ c;
1238 c = cc;
1239
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001240 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001241 adjust = 1 - ( x[i] & 0x01 );
1242 c |= x[i] & ( x[i-1] * adjust );
1243 x[i] = x[i] ^ ( x[i-1] * adjust );
1244 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001245 }
1246}
1247
1248/*
1249 * Precompute points for the comb method
1250 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001251 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1252 * 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 +01001253 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001254 * T must be able to hold 2^{w - 1} elements
1255 *
1256 * 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 +01001257 */
1258static int ecp_precompute_comb( const ecp_group *grp,
1259 ecp_point T[], const ecp_point *P,
1260 unsigned char w, size_t d )
1261{
1262 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001263 unsigned char i, k;
1264 size_t j;
1265 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001266
1267 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001268 * Set T[0] = P and
1269 * 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 +01001270 */
1271 MPI_CHK( ecp_copy( &T[0], P ) );
1272
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 cur = T + i;
1277 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001278 for( j = 0; j < d; j++ )
1279 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001280
1281 TT[k++] = cur;
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é-Gonnardd1c1ba92013-11-16 15:50:12 +01001285
1286 /*
1287 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001288 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001289 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001290 k = 0;
Paul Bakker66d5d072014-06-17 16:39:18 +02001291 for( i = 1; i < ( 1U << ( w - 1 ) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001292 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001293 j = i;
1294 while( j-- )
1295 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001296 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001297 TT[k++] = &T[i + j];
1298 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001299 }
1300
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001301 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001302
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001303cleanup:
1304 return( ret );
1305}
1306
1307/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001308 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001309 */
1310static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001311 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001312 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001313{
1314 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001315 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001316
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001317 /* Ignore the "sign" bit and scale down */
1318 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001319
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001320 /* Read the whole table to thwart cache-based timing attacks */
1321 for( j = 0; j < t_len; j++ )
1322 {
1323 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1324 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1325 }
1326
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001327 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001328 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001329
1330cleanup:
1331 return( ret );
1332}
1333
1334/*
1335 * Core multiplication algorithm for the (modified) comb method.
1336 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001337 *
1338 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001339 */
1340static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001341 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001342 const unsigned char x[], size_t d,
1343 int (*f_rng)(void *, unsigned char *, size_t),
1344 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001345{
1346 int ret;
1347 ecp_point Txi;
1348 size_t i;
1349
1350 ecp_point_init( &Txi );
1351
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001352 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001353 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001354 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001355 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001356 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001357 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001358
1359 while( i-- != 0 )
1360 {
1361 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001362 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001363 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001364 }
1365
1366cleanup:
1367 ecp_point_free( &Txi );
1368
1369 return( ret );
1370}
1371
1372/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001373 * Multiplication using the comb method,
1374 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001375 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001376static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1377 const mpi *m, const ecp_point *P,
1378 int (*f_rng)(void *, unsigned char *, size_t),
1379 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001380{
1381 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001382 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1383 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001384 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001385 ecp_point *T;
1386 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001387
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001388 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001389 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001390
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001391 /* we need N to be odd to trnaform m in an odd number, check now */
1392 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1393 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1394
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001395 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001396 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001397 * 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 +01001398 * (see costs of the various parts, with 1S = 1M)
1399 */
1400 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001401
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001402 /*
1403 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001404 * Just adding one avoids upping the cost of the first mul too much,
1405 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001406 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001407#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001408 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1409 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001410 if( p_eq_g )
1411 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001412#else
1413 p_eq_g = 0;
1414#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001415
1416 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001417 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001418 * (The last test is useful only for very small curves in the test suite.)
1419 */
1420 if( w > POLARSSL_ECP_WINDOW_SIZE )
1421 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001422 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001423 w = 2;
1424
1425 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001426 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001427 d = ( grp->nbits + w - 1 ) / w;
1428
1429 /*
1430 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001431 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001432 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001433 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001434
1435 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001436 {
1437 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1438 if( T == NULL )
1439 {
1440 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1441 goto cleanup;
1442 }
1443
1444 for( i = 0; i < pre_len; i++ )
1445 ecp_point_init( &T[i] );
1446
1447 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1448
1449 if( p_eq_g )
1450 {
1451 grp->T = T;
1452 grp->T_size = pre_len;
1453 }
1454 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001455
1456 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001457 * Make sure M is odd (M = m or M = N - m, since N is odd)
1458 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001459 */
1460 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001461 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001462 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1463 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001464
1465 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001466 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001467 */
1468 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001469 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 +01001470
1471 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001472 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001473 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001474 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1475 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001476
1477cleanup:
1478
1479 if( T != NULL && ! p_eq_g )
1480 {
1481 for( i = 0; i < pre_len; i++ )
1482 ecp_point_free( &T[i] );
1483 polarssl_free( T );
1484 }
1485
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001486 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001487 mpi_free( &mm );
1488
1489 if( ret != 0 )
1490 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001491
1492 return( ret );
1493}
1494
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001495#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1496
1497#if defined(POLARSSL_ECP_MONTGOMERY)
1498/*
1499 * For Montgomery curves, we do all the internal arithmetic in projective
1500 * coordinates. Import/export of points uses only the x coordinates, which is
1501 * internaly represented as X / Z.
1502 *
1503 * For scalar multiplication, we'll use a Montgomery ladder.
1504 */
1505
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001506/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001507 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1508 * Cost: 1M + 1I
1509 */
1510static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1511{
1512 int ret;
1513
1514 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1515 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1516 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1517
1518cleanup:
1519 return( ret );
1520}
1521
1522/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001523 * Randomize projective x/z coordinates:
1524 * (X, Z) -> (l X, l Z) for random l
1525 * This is sort of the reverse operation of ecp_normalize_mxz().
1526 *
1527 * This countermeasure was first suggested in [2].
1528 * Cost: 2M
1529 */
1530static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1531 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1532{
1533 int ret;
1534 mpi l;
Paul Bakker66d5d072014-06-17 16:39:18 +02001535 size_t p_size = ( grp->pbits + 7 ) / 8;
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001536 int count = 0;
1537
1538 mpi_init( &l );
1539
1540 /* Generate l such that 1 < l < p */
1541 do
1542 {
1543 mpi_fill_random( &l, p_size, f_rng, p_rng );
1544
1545 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
Paul Bakker3d8fb632014-04-17 12:42:41 +02001546 MPI_CHK( mpi_shift_r( &l, 1 ) );
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001547
1548 if( count++ > 10 )
1549 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1550 }
1551 while( mpi_cmp_int( &l, 1 ) <= 0 );
1552
1553 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1554 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1555
1556cleanup:
1557 mpi_free( &l );
1558
1559 return( ret );
1560}
1561
1562/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001563 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1564 * for Montgomery curves in x/z coordinates.
1565 *
1566 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1567 * with
1568 * d = X1
1569 * P = (X2, Z2)
1570 * Q = (X3, Z3)
1571 * R = (X4, Z4)
1572 * S = (X5, Z5)
1573 * and eliminating temporary variables tO, ..., t4.
1574 *
1575 * Cost: 5M + 4S
1576 */
1577static int ecp_double_add_mxz( const ecp_group *grp,
1578 ecp_point *R, ecp_point *S,
1579 const ecp_point *P, const ecp_point *Q,
1580 const mpi *d )
1581{
1582 int ret;
1583 mpi A, AA, B, BB, E, C, D, DA, CB;
1584
1585 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1586 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1587 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1588
1589 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1590 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1591 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1592 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1593 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1594 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1595 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1596 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1597 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1598 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1599 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1600 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1601 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1602 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1603 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1604 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1605 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1606 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1607
1608cleanup:
1609 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1610 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1611 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1612
1613 return( ret );
1614}
1615
1616/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001617 * Multiplication with Montgomery ladder in x/z coordinates,
1618 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001619 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001620static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1621 const mpi *m, const ecp_point *P,
1622 int (*f_rng)(void *, unsigned char *, size_t),
1623 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001624{
1625 int ret;
1626 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001627 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001628 ecp_point RP;
1629 mpi PX;
1630
1631 ecp_point_init( &RP ); mpi_init( &PX );
1632
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001633 /* Save PX and read from P before writing to R, in case P == R */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001634 MPI_CHK( mpi_copy( &PX, &P->X ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001635 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001636
1637 /* Set R to zero in modified x/z coordinates */
1638 MPI_CHK( mpi_lset( &R->X, 1 ) );
1639 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1640 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001641
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001642 /* RP.X might be sligtly larger than P, so reduce it */
1643 MOD_ADD( RP.X );
1644
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001645 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001646 if( f_rng != NULL )
1647 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001648
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001649 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001650 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001651 while( i-- > 0 )
1652 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001653 b = mpi_get_bit( m, i );
1654 /*
1655 * if (b) R = 2R + P else R = 2R,
1656 * which is:
1657 * if (b) double_add( RP, R, RP, R )
1658 * else double_add( R, RP, R, RP )
1659 * but using safe conditional swaps to avoid leaks
1660 */
1661 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1662 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1663 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1664 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1665 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001666 }
1667
1668 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1669
1670cleanup:
1671 ecp_point_free( &RP ); mpi_free( &PX );
1672
1673 return( ret );
1674}
1675
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001676#endif /* POLARSSL_ECP_MONTGOMERY */
1677
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001678/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001679 * Multiplication R = m * P
1680 */
1681int ecp_mul( ecp_group *grp, ecp_point *R,
1682 const mpi *m, const ecp_point *P,
1683 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1684{
1685 int ret;
1686
1687 /* Common sanity checks */
1688 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1689 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1690
1691 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1692 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1693 return( ret );
1694
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001695#if defined(POLARSSL_ECP_MONTGOMERY)
1696 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001697 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001698#endif
1699#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1700 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001701 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001702#endif
1703 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001704}
1705
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001706#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001707/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001708 * Check that an affine point is valid as a public key,
1709 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001710 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001711static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001712{
1713 int ret;
1714 mpi YY, RHS;
1715
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001716 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001717 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1718 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1719 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1720 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001721 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001722
1723 mpi_init( &YY ); mpi_init( &RHS );
1724
1725 /*
1726 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001727 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001728 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001729 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1730 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001731
1732 /* Special case for A = -3 */
1733 if( grp->A.p == NULL )
1734 {
1735 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1736 }
1737 else
1738 {
1739 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1740 }
1741
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001742 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1743 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001744
1745 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001746 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001747
1748cleanup:
1749
1750 mpi_free( &YY ); mpi_free( &RHS );
1751
1752 return( ret );
1753}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001754#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1755
1756
1757#if defined(POLARSSL_ECP_MONTGOMERY)
1758/*
1759 * Check validity of a public key for Montgomery curves with x-only schemes
1760 */
1761static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1762{
1763 /* [M255 p. 5] Just check X is the correct number of bytes */
1764 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1765 return( POLARSSL_ERR_ECP_INVALID_KEY );
1766
1767 return( 0 );
1768}
1769#endif /* POLARSSL_ECP_MONTGOMERY */
1770
1771/*
1772 * Check that a point is valid as a public key
1773 */
1774int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1775{
1776 /* Must use affine coordinates */
1777 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1778 return( POLARSSL_ERR_ECP_INVALID_KEY );
1779
1780#if defined(POLARSSL_ECP_MONTGOMERY)
1781 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1782 return( ecp_check_pubkey_mx( grp, pt ) );
1783#endif
1784#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1785 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1786 return( ecp_check_pubkey_sw( grp, pt ) );
1787#endif
1788 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1789}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001790
1791/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001792 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001793 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001794int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001795{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001796#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001797 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001798 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001799 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001800 if( mpi_get_bit( d, 0 ) != 0 ||
1801 mpi_get_bit( d, 1 ) != 0 ||
1802 mpi_get_bit( d, 2 ) != 0 ||
1803 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1804 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001805 else
1806 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001807 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001808#endif /* POLARSSL_ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001809#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1810 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001811 {
1812 /* see SEC1 3.2 */
1813 if( mpi_cmp_int( d, 1 ) < 0 ||
1814 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1815 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001816 else
1817 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001818 }
Paul Bakker9af723c2014-05-01 13:03:14 +02001819#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001820
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001821 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001822}
1823
1824/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001825 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001826 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001827int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001828 int (*f_rng)(void *, unsigned char *, size_t),
1829 void *p_rng )
1830{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001831 int ret;
Paul Bakker66d5d072014-06-17 16:39:18 +02001832 size_t n_size = ( grp->nbits + 7 ) / 8;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001833
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001834#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001835 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001836 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001837 /* [M225] page 5 */
1838 size_t b;
1839
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001840 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001841
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001842 /* Make sure the most significant bit is nbits */
1843 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1844 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001845 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001846 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001847 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001848
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001849 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001850 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1851 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1852 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001853 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001854 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001855#endif /* POLARSSL_ECP_MONTGOMERY */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001856#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1857 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001858 {
1859 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001860 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001861 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1862
1863 /*
1864 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1865 * - use the same byte ordering;
1866 * - keep the leftmost nbits bits of the generated octet string;
1867 * - try until result is in the desired range.
1868 * This also avoids any biais, which is especially important for ECDSA.
1869 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001870 do
1871 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001872 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1873 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1874 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001875
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001876 /*
1877 * Each try has at worst a probability 1/2 of failing (the msb has
1878 * a probability 1/2 of being 0, and then the result will be < N),
1879 * so after 30 tries failure probability is a most 2**(-30).
1880 *
1881 * For most curves, 1 try is enough with overwhelming probability,
1882 * since N starts with a lot of 1s in binary, but some curves
1883 * such as secp224k1 are actually very close to the worst case.
1884 */
1885 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001886 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1887 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001888 while( mpi_cmp_int( d, 1 ) < 0 ||
1889 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001890 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001891 else
Paul Bakker9af723c2014-05-01 13:03:14 +02001892#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001893 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001894
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001895cleanup:
1896 if( ret != 0 )
1897 return( ret );
1898
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001899 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001900}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001901
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001902/*
1903 * Generate a keypair, prettier wrapper
1904 */
1905int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1906 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1907{
1908 int ret;
1909
1910 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1911 return( ret );
1912
1913 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1914}
1915
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001916#if defined(POLARSSL_SELF_TEST)
1917
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001918/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001919 * Checkup routine
1920 */
1921int ecp_self_test( int verbose )
1922{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001923 int ret;
1924 size_t i;
1925 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001926 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001927 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001928 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001929 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001930 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001931 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001932 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001933 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001934 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001935 "400000000000000000000000000000000000000000000000", /* one and zeros */
1936 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1937 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001938 };
1939
1940 ecp_group_init( &grp );
1941 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001942 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001943 mpi_init( &m );
1944
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001945 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001946#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001947 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001948#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001949 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1950#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001951
1952 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001953 polarssl_printf( " ECP test #1 (constant op_count, base point G): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001954
1955 /* Do a dummy multiplication first to trigger precomputation */
1956 MPI_CHK( mpi_lset( &m, 2 ) );
1957 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001958
1959 add_count = 0;
1960 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001961 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001962 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001963 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001964
1965 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1966 {
1967 add_c_prev = add_count;
1968 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001969 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001970 add_count = 0;
1971 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001972 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001973
1974 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001975 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001976
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001977 if( add_count != add_c_prev ||
1978 dbl_count != dbl_c_prev ||
1979 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001980 {
1981 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001982 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001983
1984 ret = 1;
1985 goto cleanup;
1986 }
1987 }
1988
1989 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001990 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001991
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001992 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01001993 polarssl_printf( " ECP test #2 (constant op_count, other point): " );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001994 /* We computed P = 2G last time, use it */
1995
1996 add_count = 0;
1997 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001998 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001999 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
2000 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2001
2002 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
2003 {
2004 add_c_prev = add_count;
2005 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002006 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002007 add_count = 0;
2008 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002009 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002010
2011 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
2012 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
2013
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01002014 if( add_count != add_c_prev ||
2015 dbl_count != dbl_c_prev ||
2016 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002017 {
2018 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002019 polarssl_printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002020
2021 ret = 1;
2022 goto cleanup;
2023 }
2024 }
2025
2026 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002027 polarssl_printf( "passed\n" );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002028
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002029cleanup:
2030
2031 if( ret < 0 && verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002032 polarssl_printf( "Unexpected error, return code = %08X\n", ret );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002033
2034 ecp_group_free( &grp );
2035 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02002036 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002037 mpi_free( &m );
2038
2039 if( verbose != 0 )
Paul Bakker7dc4c442014-02-01 22:50:26 +01002040 polarssl_printf( "\n" );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01002041
2042 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002043}
2044
Paul Bakker9af723c2014-05-01 13:03:14 +02002045#endif /* POLARSSL_SELF_TEST */
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01002046
Paul Bakker9af723c2014-05-01 13:03:14 +02002047#endif /* POLARSSL_ECP_C */