blob: 0e4be10ba79b7a5d1e70c5ec38f63590b00e050d [file] [log] [blame]
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001/*
Manuel Pégourié-Gonnard32b04c12013-12-02 15:49:09 +01002 * Elliptic curves over GF(p): generic functions
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01003 *
Paul Bakkercf4365f2013-01-16 17:00:43 +01004 * Copyright (C) 2006-2013, Brainspark B.V.
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
27 * References:
28 *
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +010029 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +010030 * GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +010031 * FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +010032 * RFC 4492 for the related TLS structures and constants
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020033 *
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +010034 * [M255] http://cr.yp.to/ecdh/curve25519-20060209.pdf
35 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020036 * [2] CORON, Jean-Sébastien. Resistance against differential power analysis
37 * for elliptic curve cryptosystems. In : Cryptographic Hardware and
38 * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
39 * <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +010040 *
41 * [3] HEDABOU, Mustapha, PINEL, Pierre, et BÉNÉTEAU, Lucien. A comb method to
42 * render ECC resistant against Side Channel Attacks. IACR Cryptology
43 * ePrint Archive, 2004, vol. 2004, p. 342.
44 * <http://eprint.iacr.org/2004/342.pdf>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010045 */
46
47#include "polarssl/config.h"
48
49#if defined(POLARSSL_ECP_C)
50
51#include "polarssl/ecp.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020052
53#if defined(POLARSSL_MEMORY_C)
54#include "polarssl/memory.h"
55#else
56#define polarssl_malloc malloc
57#define polarssl_free free
58#endif
59
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +010060#include <stdlib.h>
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +010061
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +010062#if defined(_MSC_VER) && !defined strcasecmp && !defined(EFIX64) && \
63 !defined(EFI32)
64#define strcasecmp _stricmp
65#endif
66
Paul Bakker6a6087e2013-10-28 18:53:08 +010067#if defined(_MSC_VER) && !defined(inline)
68#define inline _inline
69#else
70#if defined(__ARMCC_VERSION) && !defined(inline)
71#define inline __inline
72#endif /* __ARMCC_VERSION */
73#endif /*_MSC_VER */
74
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010075#if defined(POLARSSL_SELF_TEST)
76/*
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +010077 * Counts of point addition and doubling, and field multiplications.
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +020078 * Used to test resistance of point multiplication to simple timing attacks.
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010079 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +010080static unsigned long add_count, dbl_count, mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010081#endif
82
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +010083#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED) || \
84 defined(POLARSSL_ECP_DP_SECP224R1_ENABLED) || \
85 defined(POLARSSL_ECP_DP_SECP256R1_ENABLED) || \
86 defined(POLARSSL_ECP_DP_SECP384R1_ENABLED) || \
87 defined(POLARSSL_ECP_DP_SECP521R1_ENABLED) || \
88 defined(POLARSSL_ECP_DP_BP256R1_ENABLED) || \
89 defined(POLARSSL_ECP_DP_BP384R1_ENABLED) || \
90 defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
91#define POLARSSL_ECP_SHORT_WEIERSTRASS
92#endif
93
94#if defined(POLARSSL_ECP_DP_M221_ENABLED) || \
95 defined(POLARSSL_ECP_DP_M255_ENABLED) || \
96 defined(POLARSSL_ECP_DP_M383_ENABLED) || \
97 defined(POLARSSL_ECP_DP_M511_ENABLED)
98#define POLARSSL_ECP_MONTGOMERY
99#endif
100
101/*
102 * Curve types: internal for now, might be exposed later
103 */
104typedef enum
105{
106 POLARSSL_ECP_TYPE_NONE = 0,
107 POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS, /* y^2 = x^3 + a x + b */
108 POLARSSL_ECP_TYPE_MONTGOMERY, /* y^2 = x^3 + a x^2 + x */
109} ecp_curve_type;
110
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100111/*
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200112 * List of supported curves:
113 * - internal ID
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200114 * - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2)
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200115 * - size in bits
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200116 * - readable name
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200117 */
Manuel Pégourié-Gonnard43863ee2013-12-01 16:51:27 +0100118static const ecp_curve_info ecp_supported_curves[] =
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200119{
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200120#if defined(POLARSSL_ECP_DP_BP512R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100121 { POLARSSL_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200122#endif
123#if defined(POLARSSL_ECP_DP_BP384R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100124 { POLARSSL_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200125#endif
126#if defined(POLARSSL_ECP_DP_BP256R1_ENABLED)
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100127 { POLARSSL_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200128#endif
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200129#if defined(POLARSSL_ECP_DP_SECP521R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200130 { POLARSSL_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200131#endif
132#if defined(POLARSSL_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200133 { POLARSSL_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200134#endif
135#if defined(POLARSSL_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200136 { POLARSSL_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200137#endif
138#if defined(POLARSSL_ECP_DP_SECP224R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200139 { POLARSSL_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200140#endif
141#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200142 { POLARSSL_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200143#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200144 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200145};
146
147/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200148 * List of supported curves and associated info
149 */
150const ecp_curve_info *ecp_curve_list( void )
151{
152 return ecp_supported_curves;
153}
154
155/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200156 * Get the curve info for the internal identifer
157 */
158const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
159{
160 const ecp_curve_info *curve_info;
161
162 for( curve_info = ecp_curve_list();
163 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
164 curve_info++ )
165 {
166 if( curve_info->grp_id == grp_id )
167 return( curve_info );
168 }
169
170 return( NULL );
171}
172
173/*
174 * Get the curve info from the TLS identifier
175 */
176const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
177{
178 const ecp_curve_info *curve_info;
179
180 for( curve_info = ecp_curve_list();
181 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
182 curve_info++ )
183 {
184 if( curve_info->tls_id == tls_id )
185 return( curve_info );
186 }
187
188 return( NULL );
189}
190
191/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100192 * Get the curve info from the name
193 */
194const ecp_curve_info *ecp_curve_info_from_name( const char *name )
195{
196 const ecp_curve_info *curve_info;
197
198 for( curve_info = ecp_curve_list();
199 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
200 curve_info++ )
201 {
202 if( strcasecmp( curve_info->name, name ) == 0 )
203 return( curve_info );
204 }
205
206 return( NULL );
207}
208
209/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100210 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100211 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100212static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100213{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100214 if( grp->G.X.p == NULL )
215 return( POLARSSL_ECP_TYPE_NONE );
216
217 if( grp->G.Y.p == NULL )
218 return( POLARSSL_ECP_TYPE_MONTGOMERY );
219 else
220 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100221}
222
223/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100224 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100225 */
226void ecp_point_init( ecp_point *pt )
227{
228 if( pt == NULL )
229 return;
230
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100231 mpi_init( &pt->X );
232 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100233 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100234}
235
236/*
237 * Initialize (the components of) a group
238 */
239void ecp_group_init( ecp_group *grp )
240{
241 if( grp == NULL )
242 return;
243
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200244 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100245}
246
247/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200248 * Initialize (the components of) a key pair
249 */
250void ecp_keypair_init( ecp_keypair *key )
251{
252 if ( key == NULL )
253 return;
254
255 ecp_group_init( &key->grp );
256 mpi_init( &key->d );
257 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200258}
259
260/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100261 * Unallocate (the components of) a point
262 */
263void ecp_point_free( ecp_point *pt )
264{
265 if( pt == NULL )
266 return;
267
268 mpi_free( &( pt->X ) );
269 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100270 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100271}
272
273/*
274 * Unallocate (the components of) a group
275 */
276void ecp_group_free( ecp_group *grp )
277{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200278 size_t i;
279
Manuel Pégourié-Gonnard731d08b2013-12-06 12:16:10 +0100280 /* FIXME WIP */
281 if( grp->id != 0 )
282 return;
283
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100284 if( grp == NULL )
285 return;
286
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100287 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200288 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100289 mpi_free( &grp->B );
290 ecp_point_free( &grp->G );
291 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200292
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200293 if( grp->T != NULL )
294 {
295 for( i = 0; i < grp->T_size; i++ )
296 ecp_point_free( &grp->T[i] );
297 polarssl_free( grp->T );
298 }
299
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200300 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100301}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100302
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100303/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200304 * Unallocate (the components of) a key pair
305 */
306void ecp_keypair_free( ecp_keypair *key )
307{
308 if ( key == NULL )
309 return;
310
311 ecp_group_free( &key->grp );
312 mpi_free( &key->d );
313 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200314}
315
316/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200317 * Copy the contents of a point
318 */
319int ecp_copy( ecp_point *P, const ecp_point *Q )
320{
321 int ret;
322
323 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
324 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
325 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
326
327cleanup:
328 return( ret );
329}
330
331/*
332 * Copy the contents of a group object
333 */
334int ecp_group_copy( ecp_group *dst, const ecp_group *src )
335{
336 return ecp_use_known_dp( dst, src->id );
337}
338
339/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100340 * Set point to zero
341 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100342int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100343{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100344 int ret;
345
346 MPI_CHK( mpi_lset( &pt->X , 1 ) );
347 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
348 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
349
350cleanup:
351 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100352}
353
354/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100355 * Tell if a point is zero
356 */
357int ecp_is_zero( ecp_point *pt )
358{
359 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
360}
361
362/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100363 * Import a non-zero point from ASCII strings
364 */
365int ecp_point_read_string( ecp_point *P, int radix,
366 const char *x, const char *y )
367{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100368 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100369
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100370 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
371 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100372 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100373
374cleanup:
375 return( ret );
376}
377
378/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100379 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100380 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100381int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100382 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100383 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100384{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200385 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100386 size_t plen;
387
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100388 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
389 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100390 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100391
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100392 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100393 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100394 */
395 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
396 {
397 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100398 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100399
400 buf[0] = 0x00;
401 *olen = 1;
402
403 return( 0 );
404 }
405
406 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100407
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100408 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
409 {
410 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100411
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100412 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100413 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100414
415 buf[0] = 0x04;
416 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
417 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
418 }
419 else if( format == POLARSSL_ECP_PF_COMPRESSED )
420 {
421 *olen = plen + 1;
422
423 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100424 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100425
426 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
427 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
428 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100429
430cleanup:
431 return( ret );
432}
433
434/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100435 * Import a point from unsigned binary data (SEC1 2.3.4)
436 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100437int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
438 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100439 int ret;
440 size_t plen;
441
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100442 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100443 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100444
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100445 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100446
447 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100448 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100449
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100450 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
451 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
452 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100453
454cleanup:
455 return( ret );
456}
457
458/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100459 * Import a point from a TLS ECPoint record (RFC 4492)
460 * struct {
461 * opaque point <1..2^8-1>;
462 * } ECPoint;
463 */
464int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100465 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100466{
467 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100468 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100469
470 /*
471 * We must have at least two bytes (1 for length, at least of for data)
472 */
473 if( buf_len < 2 )
474 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
475
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100476 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100477 if( data_len < 1 || data_len > buf_len - 1 )
478 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
479
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100480 /*
481 * Save buffer start for read_binary and update buf
482 */
483 buf_start = *buf;
484 *buf += data_len;
485
486 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100487}
488
489/*
490 * Export a point as a TLS ECPoint record (RFC 4492)
491 * struct {
492 * opaque point <1..2^8-1>;
493 * } ECPoint;
494 */
495int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100496 int format, size_t *olen,
497 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100498{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100499 int ret;
500
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100501 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100502 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100503 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100504 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100505 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
506
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100507 if( ( ret = ecp_point_write_binary( grp, pt, format,
508 olen, buf + 1, blen - 1) ) != 0 )
509 return( ret );
510
511 /*
512 * write length to the first byte and update total length
513 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200514 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100515 ++*olen;
516
517 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100518}
519
520/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200521 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200522 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200523int ecp_group_read_string( ecp_group *grp, int radix,
524 const char *p, const char *b,
525 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100526{
527 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100528
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100529 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200530 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100531 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
532 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
533 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
534
535 grp->pbits = mpi_msb( &grp->P );
536 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100537
538cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200539 if( ret != 0 )
540 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200541
542 return( ret );
543}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200544
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100545/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100546 * Set a group from an ECParameters record (RFC 4492)
547 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100548int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100549{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200550 uint16_t tls_id;
551 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100552
553 /*
554 * We expect at least three bytes (see below)
555 */
556 if( len < 3 )
557 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
558
559 /*
560 * First byte is curve_type; only named_curve is handled
561 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100562 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100563 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
564
565 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100566 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100567 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200568 tls_id = *(*buf)++;
569 tls_id <<= 8;
570 tls_id |= *(*buf)++;
571
572 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
573 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
574
575 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100576}
577
578/*
579 * Write the ECParameters record corresponding to a group (RFC 4492)
580 */
581int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
582 unsigned char *buf, size_t blen )
583{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200584 const ecp_curve_info *curve_info;
585
586 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
587 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200588
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100589 /*
590 * We are going to write 3 bytes (see below)
591 */
592 *olen = 3;
593 if( blen < *olen )
594 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
595
596 /*
597 * First byte is curve_type, always named_curve
598 */
599 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
600
601 /*
602 * Next two bytes are the namedcurve value
603 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200604 buf[0] = curve_info->tls_id >> 8;
605 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100606
607 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100608}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100609
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200610/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200611 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
612 * See the documentation of struct ecp_group.
613 *
614 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200615 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200616static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200617{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200618 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200619
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200620 if( grp->modp == NULL )
621 return( mpi_mod_mpi( N, N, &grp->P ) );
622
623 /* N->s < 0 is a much faster test, which fails only if N is 0 */
624 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
625 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200626 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200627 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200628 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200629
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200630 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200631
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200632 /* N->s < 0 is a much faster test, which fails only if N is 0 */
633 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
634 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200635
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200636 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
637 /* we known P, N and the result are positive */
638 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200639
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200640cleanup:
641 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200642}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200643
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100644/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100645 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100646 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100647 * In order to guarantee that, we need to ensure that operands of
648 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100649 * bring the result back to this range.
650 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100651 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100652 */
653
654/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100655 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
656 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100657#if defined(POLARSSL_SELF_TEST)
658#define INC_MUL_COUNT mul_count++;
659#else
660#define INC_MUL_COUNT
661#endif
662
663#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
664 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100665
666/*
667 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200668 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100669 */
670#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200671 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100672 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
673
674/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200675 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
676 * We known P, N and the result are positive, so sub_abs is correct, and
677 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100678 */
679#define MOD_ADD( N ) \
680 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200681 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100682
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100683#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
684/*
685 * For curves in short Weierstrass form, we do all the internal operations in
686 * Jacobian coordinates.
687 *
688 * For multiplication, we'll use a comb method with coutermeasueres against
689 * SPA, hence timing attacks.
690 */
691
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100692/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100693 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100694 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100695 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100696static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100697{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100698 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100699 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100700
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100701 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100702 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100703
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100704 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100705
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100706 /*
707 * X = X / Z^2 mod p
708 */
709 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
710 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
711 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100712
713 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100714 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100715 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100716 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
717 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100718
719 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100720 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100721 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100722 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100723
724cleanup:
725
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100726 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100727
728 return( ret );
729}
730
731/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100732 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100733 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100734 * (See for example Cohen's "A Course in Computational Algebraic Number
735 * Theory", Algorithm 10.3.4.)
736 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200737 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100738 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100739 *
740 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100741 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100742static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100743 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100744{
745 int ret;
746 size_t i;
747 mpi *c, u, Zi, ZZi;
748
749 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100750 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100751
Paul Bakker6e339b52013-07-03 13:37:05 +0200752 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200753 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100754
755 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
756 for( i = 0; i < t_len; i++ )
757 mpi_init( &c[i] );
758
759 /*
760 * c[i] = Z_0 * ... * Z_i
761 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100762 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100763 for( i = 1; i < t_len; i++ )
764 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100765 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100766 MOD_MUL( c[i] );
767 }
768
769 /*
770 * u = 1 / (Z_0 * ... * Z_n) mod P
771 */
772 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
773
774 for( i = t_len - 1; ; i-- )
775 {
776 /*
777 * Zi = 1 / Z_i mod p
778 * u = 1 / (Z_0 * ... * Z_i) mod P
779 */
780 if( i == 0 ) {
781 MPI_CHK( mpi_copy( &Zi, &u ) );
782 }
783 else
784 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100785 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
786 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100787 }
788
789 /*
790 * proceed as in normalize()
791 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100792 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
793 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
794 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
795 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
796 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100797
798 if( i == 0 )
799 break;
800 }
801
802cleanup:
803
804 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
805 for( i = 0; i < t_len; i++ )
806 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200807 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100808
809 return( ret );
810}
811
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100812/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100813 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
814 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
815 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100816static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100817 ecp_point *Q,
818 unsigned char inv )
819{
820 int ret;
821 unsigned char nonzero;
822 mpi mQY;
823
824 mpi_init( &mQY );
825
826 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
827 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
828 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
829 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
830
831cleanup:
832 mpi_free( &mQY );
833
834 return( ret );
835}
836
837/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200838 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200839 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200840 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200841 * with heavy variable renaming, some reordering and one minor modification
842 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
843 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100844 *
845 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200846 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200847static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
848 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200849{
850 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200851 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200852
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200853#if defined(POLARSSL_SELF_TEST)
854 dbl_count++;
855#endif
856
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200857 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
858 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200859
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200860 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
861 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
862 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
863 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
864 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
865 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
866 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
867 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
868 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
869 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
870 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
871 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
872 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
873 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
874 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
875 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
876 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
877 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
878 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
879 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
880 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
881 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
882 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
883 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200884
885 MPI_CHK( mpi_copy( &R->X, &X3 ) );
886 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
887 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
888
889cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200890 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
891 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200892
893 return( ret );
894}
895
896/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100897 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100898 *
899 * The coordinates of Q must be normalized (= affine),
900 * but those of P don't need to. R is not normalized.
901 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100902 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100903 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100904 * - at each step, P, Q and R are multiples of the base point, the factor
905 * being less than its order, so none of them is zero;
906 * - Q is an odd multiple of the base point, P an even multiple,
907 * due to the choice of precomputed points in the modified comb method.
908 * So branches for these cases do not leak secret information.
909 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100910 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100911 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100912static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100913 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100914{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100915 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100916 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100917
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100918#if defined(POLARSSL_SELF_TEST)
919 add_count++;
920#endif
921
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100922 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100923 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100924 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100925 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
926 return( ecp_copy( R, Q ) );
927
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100928 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
929 return( ecp_copy( R, P ) );
930
931 /*
932 * Make sure Q coordinates are normalized
933 */
934 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200935 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100936
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100937 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
938 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100939
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100940 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
941 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
942 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
943 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
944 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
945 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100946
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100947 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100948 if( mpi_cmp_int( &T1, 0 ) == 0 )
949 {
950 if( mpi_cmp_int( &T2, 0 ) == 0 )
951 {
952 ret = ecp_double_jac( grp, R, P );
953 goto cleanup;
954 }
955 else
956 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100957 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100958 goto cleanup;
959 }
960 }
961
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100962 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
963 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
964 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
965 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
966 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
967 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
968 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
969 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
970 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
971 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
972 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
973 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100974
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100975 MPI_CHK( mpi_copy( &R->X, &X ) );
976 MPI_CHK( mpi_copy( &R->Y, &Y ) );
977 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100978
979cleanup:
980
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100981 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
982 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100983
984 return( ret );
985}
986
987/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100988 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100989 */
990int ecp_add( const ecp_group *grp, ecp_point *R,
991 const ecp_point *P, const ecp_point *Q )
992{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100993 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100994
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100995 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100996 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
997
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100998 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100999 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001000
1001cleanup:
1002 return( ret );
1003}
1004
1005/*
1006 * Subtraction: R = P - Q, result's coordinates normalized
1007 */
1008int ecp_sub( const ecp_group *grp, ecp_point *R,
1009 const ecp_point *P, const ecp_point *Q )
1010{
1011 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001012 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001013
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001014 ecp_point_init( &mQ );
1015
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001016 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001017 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1018
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001019 /* mQ = - Q */
1020 ecp_copy( &mQ, Q );
1021 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1022 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1023
1024 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001025 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001026
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001027cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001028 ecp_point_free( &mQ );
1029
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001030 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001031}
1032
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001033/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001034 * Randomize jacobian coordinates:
1035 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001036 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001037 *
1038 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001039 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001040static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001041 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1042{
1043 int ret;
1044 mpi l, ll;
1045 size_t p_size = (grp->pbits + 7) / 8;
1046 int count = 0;
1047
1048 mpi_init( &l ); mpi_init( &ll );
1049
1050 /* Generate l such that 1 < l < p */
1051 do
1052 {
1053 mpi_fill_random( &l, p_size, f_rng, p_rng );
1054
1055 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1056 mpi_shift_r( &l, 1 );
1057
1058 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001059 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001060 }
1061 while( mpi_cmp_int( &l, 1 ) <= 0 );
1062
1063 /* Z = l * Z */
1064 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1065
1066 /* X = l^2 * X */
1067 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1068 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1069
1070 /* Y = l^3 * Y */
1071 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1072 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1073
1074cleanup:
1075 mpi_free( &l ); mpi_free( &ll );
1076
1077 return( ret );
1078}
1079
1080/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001081 * Check and define parameters used by the comb method (see below for details)
1082 */
1083#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1084#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1085#endif
1086
1087/* d = ceil( n / w ) */
1088#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1089
1090/* number of precomputed points */
1091#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1092
1093/*
1094 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001095 *
1096 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001097 * modified version that provides resistance to SPA by avoiding zero
1098 * digits in the representation as in [3]. We modify the method further by
1099 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001100 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001101 *
1102 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1103 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1104 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001105 *
1106 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001107 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001108 * - w is the size, ie number of teeth, of the comb, and must be between
1109 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001110 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1111 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001112 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001113static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001114 unsigned char w, const mpi *m )
1115{
1116 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001117 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001118
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001119 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001120
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001121 /* First get the classical comb values (except for x_d = 0) */
1122 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001123 for( j = 0; j < w; j++ )
1124 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1125
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001126 /* Now make sure x_1 .. x_d are odd */
1127 c = 0;
1128 for( i = 1; i <= d; i++ )
1129 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001130 /* Add carry and update it */
1131 cc = x[i] & c;
1132 x[i] = x[i] ^ c;
1133 c = cc;
1134
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001135 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001136 adjust = 1 - ( x[i] & 0x01 );
1137 c |= x[i] & ( x[i-1] * adjust );
1138 x[i] = x[i] ^ ( x[i-1] * adjust );
1139 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001140 }
1141}
1142
1143/*
1144 * Precompute points for the comb method
1145 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001146 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1147 * 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 +01001148 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001149 * T must be able to hold 2^{w - 1} elements
1150 *
1151 * 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 +01001152 */
1153static int ecp_precompute_comb( const ecp_group *grp,
1154 ecp_point T[], const ecp_point *P,
1155 unsigned char w, size_t d )
1156{
1157 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001158 unsigned char i, k;
1159 size_t j;
1160 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001161
1162 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001163 * Set T[0] = P and
1164 * 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 +01001165 */
1166 MPI_CHK( ecp_copy( &T[0], P ) );
1167
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001168 k = 0;
1169 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001170 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001171 cur = T + i;
1172 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001173 for( j = 0; j < d; j++ )
1174 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001175
1176 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001177 }
1178
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001179 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001180
1181 /*
1182 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001183 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001184 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001185 k = 0;
1186 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001187 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001188 j = i;
1189 while( j-- )
1190 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001191 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001192 TT[k++] = &T[i + j];
1193 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001194 }
1195
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001196 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001197
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001198 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001199 * Post-precessing: reclaim some memory by
1200 * - not storing Z (always 1)
1201 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001202 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001203 */
1204 for( i = 0; i < ( 1U << (w-1) ); i++ )
1205 {
1206 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001207 mpi_shrink( &T[i].X, grp->P.n );
1208 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001209 }
1210
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001211cleanup:
1212 return( ret );
1213}
1214
1215/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001216 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001217 */
1218static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001219 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001220 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001221{
1222 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001223 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001224
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001225 /* Ignore the "sign" bit and scale down */
1226 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001227
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001228 /* Read the whole table to thwart cache-based timing attacks */
1229 for( j = 0; j < t_len; j++ )
1230 {
1231 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1232 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1233 }
1234
1235 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001236 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1237
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001238 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001239 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001240
1241cleanup:
1242 return( ret );
1243}
1244
1245/*
1246 * Core multiplication algorithm for the (modified) comb method.
1247 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001248 *
1249 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001250 */
1251static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001252 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001253 const unsigned char x[], size_t d,
1254 int (*f_rng)(void *, unsigned char *, size_t),
1255 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001256{
1257 int ret;
1258 ecp_point Txi;
1259 size_t i;
1260
1261 ecp_point_init( &Txi );
1262
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001263 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001264 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001265 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001266 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001267 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001268
1269 while( i-- != 0 )
1270 {
1271 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001272 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001273 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001274 }
1275
1276cleanup:
1277 ecp_point_free( &Txi );
1278
1279 return( ret );
1280}
1281
1282/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001283 * Multiplication using the comb method,
1284 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001285 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001286static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1287 const mpi *m, const ecp_point *P,
1288 int (*f_rng)(void *, unsigned char *, size_t),
1289 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001290{
1291 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001292 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1293 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001294 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001295 ecp_point *T;
1296 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001297
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001298 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001299 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001300
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001301 /* we need N to be odd to trnaform m in an odd number, check now */
1302 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1303 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1304
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001305 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001306 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001307 * 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 +01001308 * (see costs of the various parts, with 1S = 1M)
1309 */
1310 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001311
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001312 /*
1313 * If P == G, pre-compute a bit more, since this may be re-used later.
1314 * Just adding one ups the cost of the first mul by at most 3%.
1315 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001316 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1317 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001318 if( p_eq_g )
1319 w++;
1320
1321 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001322 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001323 * (The last test is useful only for very small curves in the test suite.)
1324 */
1325 if( w > POLARSSL_ECP_WINDOW_SIZE )
1326 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001327 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001328 w = 2;
1329
1330 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001331 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001332 d = ( grp->nbits + w - 1 ) / w;
1333
1334 /*
1335 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001336 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001337 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001338 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001339
1340 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001341 {
1342 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1343 if( T == NULL )
1344 {
1345 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1346 goto cleanup;
1347 }
1348
1349 for( i = 0; i < pre_len; i++ )
1350 ecp_point_init( &T[i] );
1351
1352 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1353
1354 if( p_eq_g )
1355 {
1356 grp->T = T;
1357 grp->T_size = pre_len;
1358 }
1359 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001360
1361 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001362 * Make sure M is odd (M = m or M = N - m, since N is odd)
1363 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001364 */
1365 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001366 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001367 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1368 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001369
1370 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001371 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001372 */
1373 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001374 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 +01001375
1376 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001377 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001378 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001379 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1380 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001381
1382cleanup:
1383
1384 if( T != NULL && ! p_eq_g )
1385 {
1386 for( i = 0; i < pre_len; i++ )
1387 ecp_point_free( &T[i] );
1388 polarssl_free( T );
1389 }
1390
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001391 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001392 mpi_free( &mm );
1393
1394 if( ret != 0 )
1395 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001396
1397 return( ret );
1398}
1399
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001400#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1401
1402#if defined(POLARSSL_ECP_MONTGOMERY)
1403/*
1404 * For Montgomery curves, we do all the internal arithmetic in projective
1405 * coordinates. Import/export of points uses only the x coordinates, which is
1406 * internaly represented as X / Z.
1407 *
1408 * For scalar multiplication, we'll use a Montgomery ladder.
1409 */
1410
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001411/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001412 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1413 * Cost: 1M + 1I
1414 */
1415static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1416{
1417 int ret;
1418
1419 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1420 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1421 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1422
1423cleanup:
1424 return( ret );
1425}
1426
1427/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001428 * Randomize projective x/z coordinates:
1429 * (X, Z) -> (l X, l Z) for random l
1430 * This is sort of the reverse operation of ecp_normalize_mxz().
1431 *
1432 * This countermeasure was first suggested in [2].
1433 * Cost: 2M
1434 */
1435static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1436 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1437{
1438 int ret;
1439 mpi l;
1440 size_t p_size = (grp->pbits + 7) / 8;
1441 int count = 0;
1442
1443 mpi_init( &l );
1444
1445 /* Generate l such that 1 < l < p */
1446 do
1447 {
1448 mpi_fill_random( &l, p_size, f_rng, p_rng );
1449
1450 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1451 mpi_shift_r( &l, 1 );
1452
1453 if( count++ > 10 )
1454 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1455 }
1456 while( mpi_cmp_int( &l, 1 ) <= 0 );
1457
1458 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1459 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1460
1461cleanup:
1462 mpi_free( &l );
1463
1464 return( ret );
1465}
1466
1467/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001468 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1469 * for Montgomery curves in x/z coordinates.
1470 *
1471 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1472 * with
1473 * d = X1
1474 * P = (X2, Z2)
1475 * Q = (X3, Z3)
1476 * R = (X4, Z4)
1477 * S = (X5, Z5)
1478 * and eliminating temporary variables tO, ..., t4.
1479 *
1480 * Cost: 5M + 4S
1481 */
1482static int ecp_double_add_mxz( const ecp_group *grp,
1483 ecp_point *R, ecp_point *S,
1484 const ecp_point *P, const ecp_point *Q,
1485 const mpi *d )
1486{
1487 int ret;
1488 mpi A, AA, B, BB, E, C, D, DA, CB;
1489
1490 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1491 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1492 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1493
1494 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1495 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1496 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1497 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1498 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1499 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1500 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1501 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1502 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1503 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1504 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1505 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1506 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1507 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1508 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1509 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1510 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1511 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1512
1513cleanup:
1514 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1515 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1516 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1517
1518 return( ret );
1519}
1520
1521/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001522 * Multiplication with Montgomery ladder in x/z coordinates,
1523 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001524 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001525static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1526 const mpi *m, const ecp_point *P,
1527 int (*f_rng)(void *, unsigned char *, size_t),
1528 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001529{
1530 int ret;
1531 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001532 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001533 ecp_point RP;
1534 mpi PX;
1535
1536 ecp_point_init( &RP ); mpi_init( &PX );
1537
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001538 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001539 mpi_copy( &PX, &P->X );
1540 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001541
1542 /* Set R to zero in modified x/z coordinates */
1543 MPI_CHK( mpi_lset( &R->X, 1 ) );
1544 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1545 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001546
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001547 /* RP.X might be sligtly larger than P, so reduce it */
1548 MOD_ADD( RP.X );
1549
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001550 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001551 if( f_rng != NULL )
1552 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001553
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001554 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001555 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001556 while( i-- > 0 )
1557 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001558 b = mpi_get_bit( m, i );
1559 /*
1560 * if (b) R = 2R + P else R = 2R,
1561 * which is:
1562 * if (b) double_add( RP, R, RP, R )
1563 * else double_add( R, RP, R, RP )
1564 * but using safe conditional swaps to avoid leaks
1565 */
1566 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1567 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1568 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1569 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1570 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001571 }
1572
1573 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1574
1575cleanup:
1576 ecp_point_free( &RP ); mpi_free( &PX );
1577
1578 return( ret );
1579}
1580
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001581#endif /* POLARSSL_ECP_MONTGOMERY */
1582
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001583/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001584 * Multiplication R = m * P
1585 */
1586int ecp_mul( ecp_group *grp, ecp_point *R,
1587 const mpi *m, const ecp_point *P,
1588 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1589{
1590 int ret;
1591
1592 /* Common sanity checks */
1593 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1594 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1595
1596 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1597 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1598 return( ret );
1599
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001600#if defined(POLARSSL_ECP_MONTGOMERY)
1601 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001602 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001603#endif
1604#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1605 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001606 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001607#endif
1608 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001609}
1610
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001611#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001612/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001613 * Check that an affine point is valid as a public key,
1614 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001615 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001616static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001617{
1618 int ret;
1619 mpi YY, RHS;
1620
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001621 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001622 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1623 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1624 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1625 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001626 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001627
1628 mpi_init( &YY ); mpi_init( &RHS );
1629
1630 /*
1631 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001632 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001633 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001634 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1635 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001636 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001637 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1638 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001639
1640 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001641 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001642
1643cleanup:
1644
1645 mpi_free( &YY ); mpi_free( &RHS );
1646
1647 return( ret );
1648}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001649#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1650
1651
1652#if defined(POLARSSL_ECP_MONTGOMERY)
1653/*
1654 * Check validity of a public key for Montgomery curves with x-only schemes
1655 */
1656static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1657{
1658 /* [M255 p. 5] Just check X is the correct number of bytes */
1659 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1660 return( POLARSSL_ERR_ECP_INVALID_KEY );
1661
1662 return( 0 );
1663}
1664#endif /* POLARSSL_ECP_MONTGOMERY */
1665
1666/*
1667 * Check that a point is valid as a public key
1668 */
1669int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1670{
1671 /* Must use affine coordinates */
1672 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1673 return( POLARSSL_ERR_ECP_INVALID_KEY );
1674
1675#if defined(POLARSSL_ECP_MONTGOMERY)
1676 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1677 return( ecp_check_pubkey_mx( grp, pt ) );
1678#endif
1679#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1680 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1681 return( ecp_check_pubkey_sw( grp, pt ) );
1682#endif
1683 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1684}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001685
1686/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001687 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001688 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001689int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001690{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001691#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001692 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001693 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001694 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001695 if( mpi_get_bit( d, 0 ) != 0 ||
1696 mpi_get_bit( d, 1 ) != 0 ||
1697 mpi_get_bit( d, 2 ) != 0 ||
1698 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1699 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001700 else
1701 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001702 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001703#endif
1704#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1705 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001706 {
1707 /* see SEC1 3.2 */
1708 if( mpi_cmp_int( d, 1 ) < 0 ||
1709 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1710 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001711 else
1712 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001713 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001714#endif
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001715
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001716 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001717}
1718
1719/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001720 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001721 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001722int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001723 int (*f_rng)(void *, unsigned char *, size_t),
1724 void *p_rng )
1725{
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001726 size_t n_size = (grp->nbits + 7) / 8;
1727
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001728#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001729 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001730 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001731 /* [M225] page 5 */
1732 size_t b;
1733
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001734 mpi_fill_random( d, n_size, f_rng, p_rng );
1735
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001736 /* Make sure the most significant bit is nbits */
1737 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1738 if( b > grp->nbits )
1739 mpi_shift_r( d, b - grp->nbits );
1740 else
1741 mpi_set_bit( d, grp->nbits, 1 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001742
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001743 /* Make sure the last three bits are unset */
1744 mpi_set_bit( d, 0, 0 );
1745 mpi_set_bit( d, 1, 0 );
1746 mpi_set_bit( d, 2, 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001747 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001748 else
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001749#endif
1750#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1751 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001752 {
1753 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001754 int count = 0;
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001755 do
1756 {
1757 mpi_fill_random( d, n_size, f_rng, p_rng );
1758
1759 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1760 mpi_shift_r( d, 1 );
1761
1762 if( count++ > 10 )
1763 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1764 }
1765 while( mpi_cmp_int( d, 1 ) < 0 );
1766 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001767 else
1768#endif
1769 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001770
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001771 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001772}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001773
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001774/*
1775 * Generate a keypair, prettier wrapper
1776 */
1777int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1778 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1779{
1780 int ret;
1781
1782 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1783 return( ret );
1784
1785 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1786}
1787
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001788#if defined(POLARSSL_SELF_TEST)
1789
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001790/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001791 * Checkup routine
1792 */
1793int ecp_self_test( int verbose )
1794{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001795 int ret;
1796 size_t i;
1797 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001798 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001799 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001800 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001801 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001802 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001803 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001804 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001805 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001806 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001807 "400000000000000000000000000000000000000000000000", /* one and zeros */
1808 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1809 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001810 };
1811
1812 ecp_group_init( &grp );
1813 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001814 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001815 mpi_init( &m );
1816
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001817 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001818#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001819 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001820#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001821 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1822#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001823
1824 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001825 printf( " ECP test #1 (constant op_count, base point G): " );
1826
1827 /* Do a dummy multiplication first to trigger precomputation */
1828 MPI_CHK( mpi_lset( &m, 2 ) );
1829 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001830
1831 add_count = 0;
1832 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001833 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001834 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001835 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001836
1837 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1838 {
1839 add_c_prev = add_count;
1840 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001841 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001842 add_count = 0;
1843 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001844 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001845
1846 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001847 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001848
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001849 if( add_count != add_c_prev ||
1850 dbl_count != dbl_c_prev ||
1851 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001852 {
1853 if( verbose != 0 )
1854 printf( "failed (%zu)\n", i );
1855
1856 ret = 1;
1857 goto cleanup;
1858 }
1859 }
1860
1861 if( verbose != 0 )
1862 printf( "passed\n" );
1863
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001864 if( verbose != 0 )
1865 printf( " ECP test #2 (constant op_count, other point): " );
1866 /* We computed P = 2G last time, use it */
1867
1868 add_count = 0;
1869 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001870 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001871 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1872 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1873
1874 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1875 {
1876 add_c_prev = add_count;
1877 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001878 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001879 add_count = 0;
1880 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001881 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001882
1883 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1884 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1885
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001886 if( add_count != add_c_prev ||
1887 dbl_count != dbl_c_prev ||
1888 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001889 {
1890 if( verbose != 0 )
1891 printf( "failed (%zu)\n", i );
1892
1893 ret = 1;
1894 goto cleanup;
1895 }
1896 }
1897
1898 if( verbose != 0 )
1899 printf( "passed\n" );
1900
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001901cleanup:
1902
1903 if( ret < 0 && verbose != 0 )
1904 printf( "Unexpected error, return code = %08X\n", ret );
1905
1906 ecp_group_free( &grp );
1907 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001908 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001909 mpi_free( &m );
1910
1911 if( verbose != 0 )
1912 printf( "\n" );
1913
1914 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001915}
1916
1917#endif
1918
1919#endif