blob: 02f1b61d7300b7a39f6bf06a1d36e718c6e2bd3e [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é-Gonnard1e8c8ec2012-10-31 19:24:21 +0100280 if( grp == NULL )
281 return;
282
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100283 mpi_free( &grp->P );
Manuel Pégourié-Gonnarda070ada2013-10-08 12:04:56 +0200284 mpi_free( &grp->A );
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100285 mpi_free( &grp->B );
286 ecp_point_free( &grp->G );
287 mpi_free( &grp->N );
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200288
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200289 if( grp->T != NULL )
290 {
291 for( i = 0; i < grp->T_size; i++ )
292 ecp_point_free( &grp->T[i] );
293 polarssl_free( grp->T );
294 }
295
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200296 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100297}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100298
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100299/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200300 * Unallocate (the components of) a key pair
301 */
302void ecp_keypair_free( ecp_keypair *key )
303{
304 if ( key == NULL )
305 return;
306
307 ecp_group_free( &key->grp );
308 mpi_free( &key->d );
309 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200310}
311
312/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200313 * Copy the contents of a point
314 */
315int ecp_copy( ecp_point *P, const ecp_point *Q )
316{
317 int ret;
318
319 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
320 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
321 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
322
323cleanup:
324 return( ret );
325}
326
327/*
328 * Copy the contents of a group object
329 */
330int ecp_group_copy( ecp_group *dst, const ecp_group *src )
331{
332 return ecp_use_known_dp( dst, src->id );
333}
334
335/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100336 * Set point to zero
337 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100338int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100339{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100340 int ret;
341
342 MPI_CHK( mpi_lset( &pt->X , 1 ) );
343 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
344 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
345
346cleanup:
347 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100348}
349
350/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100351 * Tell if a point is zero
352 */
353int ecp_is_zero( ecp_point *pt )
354{
355 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
356}
357
358/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100359 * Import a non-zero point from ASCII strings
360 */
361int ecp_point_read_string( ecp_point *P, int radix,
362 const char *x, const char *y )
363{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100364 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100365
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100366 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
367 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100368 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100369
370cleanup:
371 return( ret );
372}
373
374/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100375 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100376 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100377int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100378 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100379 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100380{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200381 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100382 size_t plen;
383
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100384 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
385 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100386 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100387
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100388 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100389 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100390 */
391 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
392 {
393 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100394 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100395
396 buf[0] = 0x00;
397 *olen = 1;
398
399 return( 0 );
400 }
401
402 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100403
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100404 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
405 {
406 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100407
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100408 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100409 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100410
411 buf[0] = 0x04;
412 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
413 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
414 }
415 else if( format == POLARSSL_ECP_PF_COMPRESSED )
416 {
417 *olen = plen + 1;
418
419 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100420 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100421
422 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
423 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
424 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100425
426cleanup:
427 return( ret );
428}
429
430/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100431 * Import a point from unsigned binary data (SEC1 2.3.4)
432 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100433int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
434 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100435 int ret;
436 size_t plen;
437
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100438 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100439 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100440
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100441 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100442
443 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100444 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100445
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100446 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
447 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
448 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100449
450cleanup:
451 return( ret );
452}
453
454/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100455 * Import a point from a TLS ECPoint record (RFC 4492)
456 * struct {
457 * opaque point <1..2^8-1>;
458 * } ECPoint;
459 */
460int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100461 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100462{
463 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100464 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100465
466 /*
467 * We must have at least two bytes (1 for length, at least of for data)
468 */
469 if( buf_len < 2 )
470 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
471
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100472 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100473 if( data_len < 1 || data_len > buf_len - 1 )
474 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
475
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100476 /*
477 * Save buffer start for read_binary and update buf
478 */
479 buf_start = *buf;
480 *buf += data_len;
481
482 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100483}
484
485/*
486 * Export a point as a TLS ECPoint record (RFC 4492)
487 * struct {
488 * opaque point <1..2^8-1>;
489 * } ECPoint;
490 */
491int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100492 int format, size_t *olen,
493 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100494{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100495 int ret;
496
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100497 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100498 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100499 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100500 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100501 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
502
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100503 if( ( ret = ecp_point_write_binary( grp, pt, format,
504 olen, buf + 1, blen - 1) ) != 0 )
505 return( ret );
506
507 /*
508 * write length to the first byte and update total length
509 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200510 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100511 ++*olen;
512
513 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100514}
515
516/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200517 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200518 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200519int ecp_group_read_string( ecp_group *grp, int radix,
520 const char *p, const char *b,
521 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100522{
523 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100524
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100525 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200526 MPI_CHK( mpi_add_int( &grp->A, &grp->P, -3 ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100527 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
528 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
529 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
530
531 grp->pbits = mpi_msb( &grp->P );
532 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100533
534cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200535 if( ret != 0 )
536 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200537
538 return( ret );
539}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200540
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100541/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100542 * Set a group from an ECParameters record (RFC 4492)
543 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100544int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100545{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200546 uint16_t tls_id;
547 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100548
549 /*
550 * We expect at least three bytes (see below)
551 */
552 if( len < 3 )
553 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
554
555 /*
556 * First byte is curve_type; only named_curve is handled
557 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100558 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100559 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
560
561 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100562 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100563 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200564 tls_id = *(*buf)++;
565 tls_id <<= 8;
566 tls_id |= *(*buf)++;
567
568 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
569 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
570
571 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100572}
573
574/*
575 * Write the ECParameters record corresponding to a group (RFC 4492)
576 */
577int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
578 unsigned char *buf, size_t blen )
579{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200580 const ecp_curve_info *curve_info;
581
582 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
583 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200584
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100585 /*
586 * We are going to write 3 bytes (see below)
587 */
588 *olen = 3;
589 if( blen < *olen )
590 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
591
592 /*
593 * First byte is curve_type, always named_curve
594 */
595 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
596
597 /*
598 * Next two bytes are the namedcurve value
599 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200600 buf[0] = curve_info->tls_id >> 8;
601 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100602
603 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100604}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100605
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200606/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200607 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
608 * See the documentation of struct ecp_group.
609 *
610 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200611 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200612static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200613{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200614 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200615
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200616 if( grp->modp == NULL )
617 return( mpi_mod_mpi( N, N, &grp->P ) );
618
619 /* N->s < 0 is a much faster test, which fails only if N is 0 */
620 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
621 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200622 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200623 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200624 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200625
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200626 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200627
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200628 /* N->s < 0 is a much faster test, which fails only if N is 0 */
629 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
630 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200631
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200632 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
633 /* we known P, N and the result are positive */
634 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200635
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200636cleanup:
637 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200638}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200639
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100640/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100641 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100642 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100643 * In order to guarantee that, we need to ensure that operands of
644 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100645 * bring the result back to this range.
646 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100647 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100648 */
649
650/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100651 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
652 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100653#if defined(POLARSSL_SELF_TEST)
654#define INC_MUL_COUNT mul_count++;
655#else
656#define INC_MUL_COUNT
657#endif
658
659#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
660 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100661
662/*
663 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200664 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100665 */
666#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200667 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100668 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
669
670/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200671 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
672 * We known P, N and the result are positive, so sub_abs is correct, and
673 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100674 */
675#define MOD_ADD( N ) \
676 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200677 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100678
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100679#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
680/*
681 * For curves in short Weierstrass form, we do all the internal operations in
682 * Jacobian coordinates.
683 *
684 * For multiplication, we'll use a comb method with coutermeasueres against
685 * SPA, hence timing attacks.
686 */
687
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100688/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100689 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100690 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100691 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100692static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100693{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100694 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100695 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100696
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100697 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100698 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100699
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100700 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100701
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100702 /*
703 * X = X / Z^2 mod p
704 */
705 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
706 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
707 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100708
709 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100710 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100711 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100712 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
713 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100714
715 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100716 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100717 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100718 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100719
720cleanup:
721
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100722 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100723
724 return( ret );
725}
726
727/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100728 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100729 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100730 * (See for example Cohen's "A Course in Computational Algebraic Number
731 * Theory", Algorithm 10.3.4.)
732 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200733 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100734 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100735 *
736 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100737 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100738static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100739 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100740{
741 int ret;
742 size_t i;
743 mpi *c, u, Zi, ZZi;
744
745 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100746 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100747
Paul Bakker6e339b52013-07-03 13:37:05 +0200748 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200749 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100750
751 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
752 for( i = 0; i < t_len; i++ )
753 mpi_init( &c[i] );
754
755 /*
756 * c[i] = Z_0 * ... * Z_i
757 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100758 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100759 for( i = 1; i < t_len; i++ )
760 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100761 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100762 MOD_MUL( c[i] );
763 }
764
765 /*
766 * u = 1 / (Z_0 * ... * Z_n) mod P
767 */
768 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
769
770 for( i = t_len - 1; ; i-- )
771 {
772 /*
773 * Zi = 1 / Z_i mod p
774 * u = 1 / (Z_0 * ... * Z_i) mod P
775 */
776 if( i == 0 ) {
777 MPI_CHK( mpi_copy( &Zi, &u ) );
778 }
779 else
780 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100781 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
782 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100783 }
784
785 /*
786 * proceed as in normalize()
787 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100788 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
789 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
790 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
791 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
792 MPI_CHK( mpi_lset( &T[i]->Z, 1 ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100793
794 if( i == 0 )
795 break;
796 }
797
798cleanup:
799
800 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
801 for( i = 0; i < t_len; i++ )
802 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200803 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100804
805 return( ret );
806}
807
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100808/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100809 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
810 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
811 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100812static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100813 ecp_point *Q,
814 unsigned char inv )
815{
816 int ret;
817 unsigned char nonzero;
818 mpi mQY;
819
820 mpi_init( &mQY );
821
822 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
823 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
824 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
825 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
826
827cleanup:
828 mpi_free( &mQY );
829
830 return( ret );
831}
832
833/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200834 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200835 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200836 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200837 * with heavy variable renaming, some reordering and one minor modification
838 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
839 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100840 *
841 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200842 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200843static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
844 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200845{
846 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200847 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200848
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200849#if defined(POLARSSL_SELF_TEST)
850 dbl_count++;
851#endif
852
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200853 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
854 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200855
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200856 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
857 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
858 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
859 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
860 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
861 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
862 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
863 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
864 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
865 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
866 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
867 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
868 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
869 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
870 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
871 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
872 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
873 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
874 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
875 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
876 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
877 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
878 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
879 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200880
881 MPI_CHK( mpi_copy( &R->X, &X3 ) );
882 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
883 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
884
885cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200886 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
887 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200888
889 return( ret );
890}
891
892/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100893 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100894 *
895 * The coordinates of Q must be normalized (= affine),
896 * but those of P don't need to. R is not normalized.
897 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100898 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100899 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100900 * - at each step, P, Q and R are multiples of the base point, the factor
901 * being less than its order, so none of them is zero;
902 * - Q is an odd multiple of the base point, P an even multiple,
903 * due to the choice of precomputed points in the modified comb method.
904 * So branches for these cases do not leak secret information.
905 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100906 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100907 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100908static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100909 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100910{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100911 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100912 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100913
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100914#if defined(POLARSSL_SELF_TEST)
915 add_count++;
916#endif
917
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100918 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100919 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100920 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100921 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
922 return( ecp_copy( R, Q ) );
923
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100924 if( mpi_cmp_int( &Q->Z, 0 ) == 0 )
925 return( ecp_copy( R, P ) );
926
927 /*
928 * Make sure Q coordinates are normalized
929 */
930 if( mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200931 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100932
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100933 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
934 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100935
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100936 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
937 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
938 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
939 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
940 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
941 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100942
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100943 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100944 if( mpi_cmp_int( &T1, 0 ) == 0 )
945 {
946 if( mpi_cmp_int( &T2, 0 ) == 0 )
947 {
948 ret = ecp_double_jac( grp, R, P );
949 goto cleanup;
950 }
951 else
952 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100953 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100954 goto cleanup;
955 }
956 }
957
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100958 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
959 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
960 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
961 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
962 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
963 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
964 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
965 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
966 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
967 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
968 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
969 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100970
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100971 MPI_CHK( mpi_copy( &R->X, &X ) );
972 MPI_CHK( mpi_copy( &R->Y, &Y ) );
973 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100974
975cleanup:
976
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100977 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
978 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100979
980 return( ret );
981}
982
983/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100984 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100985 */
986int ecp_add( const ecp_group *grp, ecp_point *R,
987 const ecp_point *P, const ecp_point *Q )
988{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100989 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +0100990
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100991 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +0100992 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
993
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100994 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100995 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100996
997cleanup:
998 return( ret );
999}
1000
1001/*
1002 * Subtraction: R = P - Q, result's coordinates normalized
1003 */
1004int ecp_sub( const ecp_group *grp, ecp_point *R,
1005 const ecp_point *P, const ecp_point *Q )
1006{
1007 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001008 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001009
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001010 ecp_point_init( &mQ );
1011
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001012 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001013 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1014
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001015 /* mQ = - Q */
1016 ecp_copy( &mQ, Q );
1017 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1018 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1019
1020 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001021 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001022
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001023cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001024 ecp_point_free( &mQ );
1025
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001026 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001027}
1028
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001029/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001030 * Randomize jacobian coordinates:
1031 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001032 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001033 *
1034 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001035 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001036static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001037 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1038{
1039 int ret;
1040 mpi l, ll;
1041 size_t p_size = (grp->pbits + 7) / 8;
1042 int count = 0;
1043
1044 mpi_init( &l ); mpi_init( &ll );
1045
1046 /* Generate l such that 1 < l < p */
1047 do
1048 {
1049 mpi_fill_random( &l, p_size, f_rng, p_rng );
1050
1051 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1052 mpi_shift_r( &l, 1 );
1053
1054 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001055 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001056 }
1057 while( mpi_cmp_int( &l, 1 ) <= 0 );
1058
1059 /* Z = l * Z */
1060 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1061
1062 /* X = l^2 * X */
1063 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1064 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1065
1066 /* Y = l^3 * Y */
1067 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1068 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1069
1070cleanup:
1071 mpi_free( &l ); mpi_free( &ll );
1072
1073 return( ret );
1074}
1075
1076/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001077 * Check and define parameters used by the comb method (see below for details)
1078 */
1079#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1080#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1081#endif
1082
1083/* d = ceil( n / w ) */
1084#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1085
1086/* number of precomputed points */
1087#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1088
1089/*
1090 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001091 *
1092 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001093 * modified version that provides resistance to SPA by avoiding zero
1094 * digits in the representation as in [3]. We modify the method further by
1095 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001096 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001097 *
1098 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1099 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1100 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001101 *
1102 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001103 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001104 * - w is the size, ie number of teeth, of the comb, and must be between
1105 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001106 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1107 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001108 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001109static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001110 unsigned char w, const mpi *m )
1111{
1112 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001113 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001114
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001115 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001116
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001117 /* First get the classical comb values (except for x_d = 0) */
1118 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001119 for( j = 0; j < w; j++ )
1120 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1121
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001122 /* Now make sure x_1 .. x_d are odd */
1123 c = 0;
1124 for( i = 1; i <= d; i++ )
1125 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001126 /* Add carry and update it */
1127 cc = x[i] & c;
1128 x[i] = x[i] ^ c;
1129 c = cc;
1130
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001131 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001132 adjust = 1 - ( x[i] & 0x01 );
1133 c |= x[i] & ( x[i-1] * adjust );
1134 x[i] = x[i] ^ ( x[i-1] * adjust );
1135 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001136 }
1137}
1138
1139/*
1140 * Precompute points for the comb method
1141 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001142 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1143 * 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 +01001144 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001145 * T must be able to hold 2^{w - 1} elements
1146 *
1147 * 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 +01001148 */
1149static int ecp_precompute_comb( const ecp_group *grp,
1150 ecp_point T[], const ecp_point *P,
1151 unsigned char w, size_t d )
1152{
1153 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001154 unsigned char i, k;
1155 size_t j;
1156 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001157
1158 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001159 * Set T[0] = P and
1160 * 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 +01001161 */
1162 MPI_CHK( ecp_copy( &T[0], P ) );
1163
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001164 k = 0;
1165 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001166 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001167 cur = T + i;
1168 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001169 for( j = 0; j < d; j++ )
1170 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001171
1172 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001173 }
1174
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001175 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001176
1177 /*
1178 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001179 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001180 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001181 k = 0;
1182 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001183 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001184 j = i;
1185 while( j-- )
1186 {
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001187 ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001188 TT[k++] = &T[i + j];
1189 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001190 }
1191
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001192 ecp_normalize_jac_many( grp, TT, k );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001193
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001194 /*
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001195 * Post-precessing: reclaim some memory by
1196 * - not storing Z (always 1)
1197 * - shrinking other coordinates
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001198 * Keep the same number of limbs as P to avoid re-growing on next use.
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001199 */
1200 for( i = 0; i < ( 1U << (w-1) ); i++ )
1201 {
1202 mpi_free( &T[i].Z );
Manuel Pégourié-Gonnard7f762312013-11-21 10:47:41 +01001203 mpi_shrink( &T[i].X, grp->P.n );
1204 mpi_shrink( &T[i].Y, grp->P.n );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001205 }
1206
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001207cleanup:
1208 return( ret );
1209}
1210
1211/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001212 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001213 */
1214static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001215 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001216 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001217{
1218 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001219 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001220
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001221 /* Ignore the "sign" bit and scale down */
1222 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001223
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001224 /* Read the whole table to thwart cache-based timing attacks */
1225 for( j = 0; j < t_len; j++ )
1226 {
1227 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1228 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1229 }
1230
1231 /* The Z coordinate is always 1 */
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001232 MPI_CHK( mpi_lset( &R->Z, 1 ) );
1233
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001234 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001235 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001236
1237cleanup:
1238 return( ret );
1239}
1240
1241/*
1242 * Core multiplication algorithm for the (modified) comb method.
1243 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001244 *
1245 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001246 */
1247static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001248 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001249 const unsigned char x[], size_t d,
1250 int (*f_rng)(void *, unsigned char *, size_t),
1251 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001252{
1253 int ret;
1254 ecp_point Txi;
1255 size_t i;
1256
1257 ecp_point_init( &Txi );
1258
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001259 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001260 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001261 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001262 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001263 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001264
1265 while( i-- != 0 )
1266 {
1267 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001268 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001269 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001270 }
1271
1272cleanup:
1273 ecp_point_free( &Txi );
1274
1275 return( ret );
1276}
1277
1278/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001279 * Multiplication using the comb method,
1280 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001281 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001282static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1283 const mpi *m, const ecp_point *P,
1284 int (*f_rng)(void *, unsigned char *, size_t),
1285 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001286{
1287 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001288 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1289 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001290 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001291 ecp_point *T;
1292 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001293
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001294 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001295 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001296
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001297 /* we need N to be odd to trnaform m in an odd number, check now */
1298 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1299 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1300
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001301 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001302 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001303 * 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 +01001304 * (see costs of the various parts, with 1S = 1M)
1305 */
1306 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001307
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001308 /*
1309 * If P == G, pre-compute a bit more, since this may be re-used later.
1310 * Just adding one ups the cost of the first mul by at most 3%.
1311 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001312 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1313 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001314 if( p_eq_g )
1315 w++;
1316
1317 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001318 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001319 * (The last test is useful only for very small curves in the test suite.)
1320 */
1321 if( w > POLARSSL_ECP_WINDOW_SIZE )
1322 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001323 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001324 w = 2;
1325
1326 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001327 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001328 d = ( grp->nbits + w - 1 ) / w;
1329
1330 /*
1331 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001332 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001333 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001334 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001335
1336 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001337 {
1338 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1339 if( T == NULL )
1340 {
1341 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1342 goto cleanup;
1343 }
1344
1345 for( i = 0; i < pre_len; i++ )
1346 ecp_point_init( &T[i] );
1347
1348 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1349
1350 if( p_eq_g )
1351 {
1352 grp->T = T;
1353 grp->T_size = pre_len;
1354 }
1355 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001356
1357 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001358 * Make sure M is odd (M = m or M = N - m, since N is odd)
1359 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001360 */
1361 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001362 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001363 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1364 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001365
1366 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001367 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001368 */
1369 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001370 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 +01001371
1372 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001373 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001374 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001375 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1376 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001377
1378cleanup:
1379
1380 if( T != NULL && ! p_eq_g )
1381 {
1382 for( i = 0; i < pre_len; i++ )
1383 ecp_point_free( &T[i] );
1384 polarssl_free( T );
1385 }
1386
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001387 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001388 mpi_free( &mm );
1389
1390 if( ret != 0 )
1391 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001392
1393 return( ret );
1394}
1395
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001396#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1397
1398#if defined(POLARSSL_ECP_MONTGOMERY)
1399/*
1400 * For Montgomery curves, we do all the internal arithmetic in projective
1401 * coordinates. Import/export of points uses only the x coordinates, which is
1402 * internaly represented as X / Z.
1403 *
1404 * For scalar multiplication, we'll use a Montgomery ladder.
1405 */
1406
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001407/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001408 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1409 * Cost: 1M + 1I
1410 */
1411static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1412{
1413 int ret;
1414
1415 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1416 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1417 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1418
1419cleanup:
1420 return( ret );
1421}
1422
1423/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001424 * Randomize projective x/z coordinates:
1425 * (X, Z) -> (l X, l Z) for random l
1426 * This is sort of the reverse operation of ecp_normalize_mxz().
1427 *
1428 * This countermeasure was first suggested in [2].
1429 * Cost: 2M
1430 */
1431static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1432 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1433{
1434 int ret;
1435 mpi l;
1436 size_t p_size = (grp->pbits + 7) / 8;
1437 int count = 0;
1438
1439 mpi_init( &l );
1440
1441 /* Generate l such that 1 < l < p */
1442 do
1443 {
1444 mpi_fill_random( &l, p_size, f_rng, p_rng );
1445
1446 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1447 mpi_shift_r( &l, 1 );
1448
1449 if( count++ > 10 )
1450 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1451 }
1452 while( mpi_cmp_int( &l, 1 ) <= 0 );
1453
1454 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1455 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1456
1457cleanup:
1458 mpi_free( &l );
1459
1460 return( ret );
1461}
1462
1463/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001464 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1465 * for Montgomery curves in x/z coordinates.
1466 *
1467 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1468 * with
1469 * d = X1
1470 * P = (X2, Z2)
1471 * Q = (X3, Z3)
1472 * R = (X4, Z4)
1473 * S = (X5, Z5)
1474 * and eliminating temporary variables tO, ..., t4.
1475 *
1476 * Cost: 5M + 4S
1477 */
1478static int ecp_double_add_mxz( const ecp_group *grp,
1479 ecp_point *R, ecp_point *S,
1480 const ecp_point *P, const ecp_point *Q,
1481 const mpi *d )
1482{
1483 int ret;
1484 mpi A, AA, B, BB, E, C, D, DA, CB;
1485
1486 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1487 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1488 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1489
1490 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1491 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1492 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1493 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1494 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1495 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1496 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1497 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1498 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1499 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1500 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1501 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1502 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1503 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1504 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1505 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1506 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1507 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1508
1509cleanup:
1510 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1511 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1512 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1513
1514 return( ret );
1515}
1516
1517/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001518 * Multiplication with Montgomery ladder in x/z coordinates,
1519 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001520 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001521static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1522 const mpi *m, const ecp_point *P,
1523 int (*f_rng)(void *, unsigned char *, size_t),
1524 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001525{
1526 int ret;
1527 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001528 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001529 ecp_point RP;
1530 mpi PX;
1531
1532 ecp_point_init( &RP ); mpi_init( &PX );
1533
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001534 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001535 mpi_copy( &PX, &P->X );
1536 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001537
1538 /* Set R to zero in modified x/z coordinates */
1539 MPI_CHK( mpi_lset( &R->X, 1 ) );
1540 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1541 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001542
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001543 /* RP.X might be sligtly larger than P, so reduce it */
1544 MOD_ADD( RP.X );
1545
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001546 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001547 if( f_rng != NULL )
1548 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001549
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001550 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001551 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001552 while( i-- > 0 )
1553 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001554 b = mpi_get_bit( m, i );
1555 /*
1556 * if (b) R = 2R + P else R = 2R,
1557 * which is:
1558 * if (b) double_add( RP, R, RP, R )
1559 * else double_add( R, RP, R, RP )
1560 * but using safe conditional swaps to avoid leaks
1561 */
1562 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1563 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1564 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1565 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1566 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001567 }
1568
1569 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1570
1571cleanup:
1572 ecp_point_free( &RP ); mpi_free( &PX );
1573
1574 return( ret );
1575}
1576
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001577#endif /* POLARSSL_ECP_MONTGOMERY */
1578
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001579/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001580 * Multiplication R = m * P
1581 */
1582int ecp_mul( ecp_group *grp, ecp_point *R,
1583 const mpi *m, const ecp_point *P,
1584 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1585{
1586 int ret;
1587
1588 /* Common sanity checks */
1589 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1590 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1591
1592 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1593 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1594 return( ret );
1595
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001596#if defined(POLARSSL_ECP_MONTGOMERY)
1597 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001598 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001599#endif
1600#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1601 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001602 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001603#endif
1604 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001605}
1606
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001607#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001608/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001609 * Check that an affine point is valid as a public key,
1610 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001611 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001612static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001613{
1614 int ret;
1615 mpi YY, RHS;
1616
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001617 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001618 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1619 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1620 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1621 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001622 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001623
1624 mpi_init( &YY ); mpi_init( &RHS );
1625
1626 /*
1627 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001628 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001629 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001630 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1631 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +02001632 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001633 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1634 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001635
1636 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001637 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001638
1639cleanup:
1640
1641 mpi_free( &YY ); mpi_free( &RHS );
1642
1643 return( ret );
1644}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001645#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1646
1647
1648#if defined(POLARSSL_ECP_MONTGOMERY)
1649/*
1650 * Check validity of a public key for Montgomery curves with x-only schemes
1651 */
1652static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1653{
1654 /* [M255 p. 5] Just check X is the correct number of bytes */
1655 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1656 return( POLARSSL_ERR_ECP_INVALID_KEY );
1657
1658 return( 0 );
1659}
1660#endif /* POLARSSL_ECP_MONTGOMERY */
1661
1662/*
1663 * Check that a point is valid as a public key
1664 */
1665int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1666{
1667 /* Must use affine coordinates */
1668 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1669 return( POLARSSL_ERR_ECP_INVALID_KEY );
1670
1671#if defined(POLARSSL_ECP_MONTGOMERY)
1672 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1673 return( ecp_check_pubkey_mx( grp, pt ) );
1674#endif
1675#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1676 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1677 return( ecp_check_pubkey_sw( grp, pt ) );
1678#endif
1679 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1680}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001681
1682/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001683 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001684 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001685int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001686{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001687#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001688 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001689 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001690 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001691 if( mpi_get_bit( d, 0 ) != 0 ||
1692 mpi_get_bit( d, 1 ) != 0 ||
1693 mpi_get_bit( d, 2 ) != 0 ||
1694 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1695 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001696 else
1697 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001698 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001699#endif
1700#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1701 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001702 {
1703 /* see SEC1 3.2 */
1704 if( mpi_cmp_int( d, 1 ) < 0 ||
1705 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1706 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001707 else
1708 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001709 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001710#endif
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001711
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001712 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001713}
1714
1715/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001716 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001717 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001718int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001719 int (*f_rng)(void *, unsigned char *, size_t),
1720 void *p_rng )
1721{
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001722 size_t n_size = (grp->nbits + 7) / 8;
1723
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001724#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001725 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001726 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001727 /* [M225] page 5 */
1728 size_t b;
1729
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001730 mpi_fill_random( d, n_size, f_rng, p_rng );
1731
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001732 /* Make sure the most significant bit is nbits */
1733 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1734 if( b > grp->nbits )
1735 mpi_shift_r( d, b - grp->nbits );
1736 else
1737 mpi_set_bit( d, grp->nbits, 1 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001738
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001739 /* Make sure the last three bits are unset */
1740 mpi_set_bit( d, 0, 0 );
1741 mpi_set_bit( d, 1, 0 );
1742 mpi_set_bit( d, 2, 0 );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001743 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001744 else
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001745#endif
1746#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1747 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001748 {
1749 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001750 int count = 0;
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001751 do
1752 {
1753 mpi_fill_random( d, n_size, f_rng, p_rng );
1754
1755 while( mpi_cmp_mpi( d, &grp->N ) >= 0 )
1756 mpi_shift_r( d, 1 );
1757
1758 if( count++ > 10 )
1759 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1760 }
1761 while( mpi_cmp_int( d, 1 ) < 0 );
1762 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001763 else
1764#endif
1765 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001766
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001767 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001768}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001769
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001770/*
1771 * Generate a keypair, prettier wrapper
1772 */
1773int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1774 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1775{
1776 int ret;
1777
1778 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1779 return( ret );
1780
1781 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1782}
1783
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001784#if defined(POLARSSL_SELF_TEST)
1785
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001786/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001787 * Checkup routine
1788 */
1789int ecp_self_test( int verbose )
1790{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001791 int ret;
1792 size_t i;
1793 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001794 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001795 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001796 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001797 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001798 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001799 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001800 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001801 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001802 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001803 "400000000000000000000000000000000000000000000000", /* one and zeros */
1804 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1805 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001806 };
1807
1808 ecp_group_init( &grp );
1809 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001810 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001811 mpi_init( &m );
1812
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001813 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001814#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001815 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001816#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001817 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1818#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001819
1820 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001821 printf( " ECP test #1 (constant op_count, base point G): " );
1822
1823 /* Do a dummy multiplication first to trigger precomputation */
1824 MPI_CHK( mpi_lset( &m, 2 ) );
1825 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001826
1827 add_count = 0;
1828 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001829 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001830 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001831 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001832
1833 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1834 {
1835 add_c_prev = add_count;
1836 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001837 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001838 add_count = 0;
1839 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001840 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001841
1842 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001843 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001844
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001845 if( add_count != add_c_prev ||
1846 dbl_count != dbl_c_prev ||
1847 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001848 {
1849 if( verbose != 0 )
1850 printf( "failed (%zu)\n", i );
1851
1852 ret = 1;
1853 goto cleanup;
1854 }
1855 }
1856
1857 if( verbose != 0 )
1858 printf( "passed\n" );
1859
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001860 if( verbose != 0 )
1861 printf( " ECP test #2 (constant op_count, other point): " );
1862 /* We computed P = 2G last time, use it */
1863
1864 add_count = 0;
1865 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001866 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001867 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1868 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1869
1870 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1871 {
1872 add_c_prev = add_count;
1873 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001874 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001875 add_count = 0;
1876 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001877 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001878
1879 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1880 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1881
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001882 if( add_count != add_c_prev ||
1883 dbl_count != dbl_c_prev ||
1884 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001885 {
1886 if( verbose != 0 )
1887 printf( "failed (%zu)\n", i );
1888
1889 ret = 1;
1890 goto cleanup;
1891 }
1892 }
1893
1894 if( verbose != 0 )
1895 printf( "passed\n" );
1896
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001897cleanup:
1898
1899 if( ret < 0 && verbose != 0 )
1900 printf( "Unexpected error, return code = %08X\n", ret );
1901
1902 ecp_group_free( &grp );
1903 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001904 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001905 mpi_free( &m );
1906
1907 if( verbose != 0 )
1908 printf( "\n" );
1909
1910 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001911}
1912
1913#endif
1914
1915#endif