blob: a27d30e2a0c77c14b15d6473465db4cbbf45e3aa [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é-Gonnard9bcff392014-01-10 18:26:48 +0100144#if defined(POLARSSL_ECP_DP_SECP256K1_ENABLED)
145 { POLARSSL_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
146#endif
147#if defined(POLARSSL_ECP_DP_SECP224K1_ENABLED)
148 { POLARSSL_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
149#endif
150#if defined(POLARSSL_ECP_DP_SECP192K1_ENABLED)
151 { POLARSSL_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
152#endif
Manuel Pégourié-Gonnard8195c1a2013-10-07 19:40:41 +0200153 { POLARSSL_ECP_DP_NONE, 0, 0, NULL },
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200154};
155
156/*
Manuel Pégourié-Gonnardda179e42013-09-18 15:31:24 +0200157 * List of supported curves and associated info
158 */
159const ecp_curve_info *ecp_curve_list( void )
160{
161 return ecp_supported_curves;
162}
163
164/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200165 * Get the curve info for the internal identifer
166 */
167const ecp_curve_info *ecp_curve_info_from_grp_id( ecp_group_id grp_id )
168{
169 const ecp_curve_info *curve_info;
170
171 for( curve_info = ecp_curve_list();
172 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
173 curve_info++ )
174 {
175 if( curve_info->grp_id == grp_id )
176 return( curve_info );
177 }
178
179 return( NULL );
180}
181
182/*
183 * Get the curve info from the TLS identifier
184 */
185const ecp_curve_info *ecp_curve_info_from_tls_id( uint16_t tls_id )
186{
187 const ecp_curve_info *curve_info;
188
189 for( curve_info = ecp_curve_list();
190 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
191 curve_info++ )
192 {
193 if( curve_info->tls_id == tls_id )
194 return( curve_info );
195 }
196
197 return( NULL );
198}
199
200/*
Manuel Pégourié-Gonnard0267e3d2013-11-30 15:10:14 +0100201 * Get the curve info from the name
202 */
203const ecp_curve_info *ecp_curve_info_from_name( const char *name )
204{
205 const ecp_curve_info *curve_info;
206
207 for( curve_info = ecp_curve_list();
208 curve_info->grp_id != POLARSSL_ECP_DP_NONE;
209 curve_info++ )
210 {
211 if( strcasecmp( curve_info->name, name ) == 0 )
212 return( curve_info );
213 }
214
215 return( NULL );
216}
217
218/*
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100219 * Get the type of a curve
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100220 */
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100221static inline ecp_curve_type ecp_get_type( const ecp_group *grp )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100222{
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100223 if( grp->G.X.p == NULL )
224 return( POLARSSL_ECP_TYPE_NONE );
225
226 if( grp->G.Y.p == NULL )
227 return( POLARSSL_ECP_TYPE_MONTGOMERY );
228 else
229 return( POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +0100230}
231
232/*
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100233 * Initialize (the components of) a point
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100234 */
235void ecp_point_init( ecp_point *pt )
236{
237 if( pt == NULL )
238 return;
239
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100240 mpi_init( &pt->X );
241 mpi_init( &pt->Y );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100242 mpi_init( &pt->Z );
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +0100243}
244
245/*
246 * Initialize (the components of) a group
247 */
248void ecp_group_init( ecp_group *grp )
249{
250 if( grp == NULL )
251 return;
252
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200253 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100254}
255
256/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200257 * Initialize (the components of) a key pair
258 */
259void ecp_keypair_init( ecp_keypair *key )
260{
261 if ( key == NULL )
262 return;
263
264 ecp_group_init( &key->grp );
265 mpi_init( &key->d );
266 ecp_point_init( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200267}
268
269/*
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100270 * Unallocate (the components of) a point
271 */
272void ecp_point_free( ecp_point *pt )
273{
274 if( pt == NULL )
275 return;
276
277 mpi_free( &( pt->X ) );
278 mpi_free( &( pt->Y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100279 mpi_free( &( pt->Z ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100280}
281
282/*
283 * Unallocate (the components of) a group
284 */
285void ecp_group_free( ecp_group *grp )
286{
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200287 size_t i;
288
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100289 if( grp == NULL )
290 return;
291
Manuel Pégourié-Gonnard1f82b042013-12-06 12:51:50 +0100292 if( grp->h != 1 )
293 {
294 mpi_free( &grp->P );
295 mpi_free( &grp->A );
296 mpi_free( &grp->B );
297 ecp_point_free( &grp->G );
298 mpi_free( &grp->N );
299 }
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200300
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +0200301 if( grp->T != NULL )
302 {
303 for( i = 0; i < grp->T_size; i++ )
304 ecp_point_free( &grp->T[i] );
305 polarssl_free( grp->T );
306 }
307
Manuel Pégourié-Gonnardc9727702013-09-16 18:56:28 +0200308 memset( grp, 0, sizeof( ecp_group ) );
Manuel Pégourié-Gonnard1e8c8ec2012-10-31 19:24:21 +0100309}
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +0100310
Manuel Pégourié-Gonnard883f3132012-11-02 09:40:25 +0100311/*
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200312 * Unallocate (the components of) a key pair
313 */
314void ecp_keypair_free( ecp_keypair *key )
315{
316 if ( key == NULL )
317 return;
318
319 ecp_group_free( &key->grp );
320 mpi_free( &key->d );
321 ecp_point_free( &key->Q );
Manuel Pégourié-Gonnardb8c6e0e2013-07-01 13:40:52 +0200322}
323
324/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200325 * Copy the contents of a point
326 */
327int ecp_copy( ecp_point *P, const ecp_point *Q )
328{
329 int ret;
330
331 MPI_CHK( mpi_copy( &P->X, &Q->X ) );
332 MPI_CHK( mpi_copy( &P->Y, &Q->Y ) );
333 MPI_CHK( mpi_copy( &P->Z, &Q->Z ) );
334
335cleanup:
336 return( ret );
337}
338
339/*
340 * Copy the contents of a group object
341 */
342int ecp_group_copy( ecp_group *dst, const ecp_group *src )
343{
344 return ecp_use_known_dp( dst, src->id );
345}
346
347/*
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100348 * Set point to zero
349 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100350int ecp_set_zero( ecp_point *pt )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100351{
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100352 int ret;
353
354 MPI_CHK( mpi_lset( &pt->X , 1 ) );
355 MPI_CHK( mpi_lset( &pt->Y , 1 ) );
356 MPI_CHK( mpi_lset( &pt->Z , 0 ) );
357
358cleanup:
359 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100360}
361
362/*
Manuel Pégourié-Gonnard6545ca72013-01-26 16:05:22 +0100363 * Tell if a point is zero
364 */
365int ecp_is_zero( ecp_point *pt )
366{
367 return( mpi_cmp_int( &pt->Z, 0 ) == 0 );
368}
369
370/*
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100371 * Import a non-zero point from ASCII strings
372 */
373int ecp_point_read_string( ecp_point *P, int radix,
374 const char *x, const char *y )
375{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100376 int ret;
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100377
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100378 MPI_CHK( mpi_read_string( &P->X, radix, x ) );
379 MPI_CHK( mpi_read_string( &P->Y, radix, y ) );
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100380 MPI_CHK( mpi_lset( &P->Z, 1 ) );
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100381
382cleanup:
383 return( ret );
384}
385
386/*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100387 * Export a point into unsigned binary data (SEC1 2.3.3)
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100388 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100389int ecp_point_write_binary( const ecp_group *grp, const ecp_point *P,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100390 int format, size_t *olen,
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100391 unsigned char *buf, size_t buflen )
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100392{
Paul Bakkera280d0f2013-04-08 13:40:17 +0200393 int ret = 0;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100394 size_t plen;
395
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100396 if( format != POLARSSL_ECP_PF_UNCOMPRESSED &&
397 format != POLARSSL_ECP_PF_COMPRESSED )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100398 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100399
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100400 /*
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100401 * Common case: P == 0
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100402 */
403 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
404 {
405 if( buflen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100406 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100407
408 buf[0] = 0x00;
409 *olen = 1;
410
411 return( 0 );
412 }
413
414 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100415
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100416 if( format == POLARSSL_ECP_PF_UNCOMPRESSED )
417 {
418 *olen = 2 * plen + 1;
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100419
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100420 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100421 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100422
423 buf[0] = 0x04;
424 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
425 MPI_CHK( mpi_write_binary( &P->Y, buf + 1 + plen, plen ) );
426 }
427 else if( format == POLARSSL_ECP_PF_COMPRESSED )
428 {
429 *olen = plen + 1;
430
431 if( buflen < *olen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100432 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnard37d218a2012-11-24 15:19:55 +0100433
434 buf[0] = 0x02 + mpi_get_bit( &P->Y, 0 );
435 MPI_CHK( mpi_write_binary( &P->X, buf + 1, plen ) );
436 }
Manuel Pégourié-Gonnarde19feb52012-11-24 14:10:14 +0100437
438cleanup:
439 return( ret );
440}
441
442/*
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100443 * Import a point from unsigned binary data (SEC1 2.3.4)
444 */
Manuel Pégourié-Gonnard7e860252013-02-10 10:58:48 +0100445int ecp_point_read_binary( const ecp_group *grp, ecp_point *pt,
446 const unsigned char *buf, size_t ilen ) {
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100447 int ret;
448 size_t plen;
449
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100450 if( ilen == 1 && buf[0] == 0x00 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100451 return( ecp_set_zero( pt ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100452
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100453 plen = mpi_size( &grp->P );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100454
455 if( ilen != 2 * plen + 1 || buf[0] != 0x04 )
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100456 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100457
Manuel Pégourié-Gonnardd84895d2013-02-10 10:53:04 +0100458 MPI_CHK( mpi_read_binary( &pt->X, buf + 1, plen ) );
459 MPI_CHK( mpi_read_binary( &pt->Y, buf + 1 + plen, plen ) );
460 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnard5e402d82012-11-24 16:19:42 +0100461
462cleanup:
463 return( ret );
464}
465
466/*
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100467 * Import a point from a TLS ECPoint record (RFC 4492)
468 * struct {
469 * opaque point <1..2^8-1>;
470 * } ECPoint;
471 */
472int ecp_tls_read_point( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100473 const unsigned char **buf, size_t buf_len )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100474{
475 unsigned char data_len;
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100476 const unsigned char *buf_start;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100477
478 /*
479 * We must have at least two bytes (1 for length, at least of for data)
480 */
481 if( buf_len < 2 )
482 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
483
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100484 data_len = *(*buf)++;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100485 if( data_len < 1 || data_len > buf_len - 1 )
486 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
487
Manuel Pégourié-Gonnard98f51812013-02-10 13:38:29 +0100488 /*
489 * Save buffer start for read_binary and update buf
490 */
491 buf_start = *buf;
492 *buf += data_len;
493
494 return ecp_point_read_binary( grp, pt, buf_start, data_len );
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100495}
496
497/*
498 * Export a point as a TLS ECPoint record (RFC 4492)
499 * struct {
500 * opaque point <1..2^8-1>;
501 * } ECPoint;
502 */
503int ecp_tls_write_point( const ecp_group *grp, const ecp_point *pt,
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100504 int format, size_t *olen,
505 unsigned char *buf, size_t blen )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100506{
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100507 int ret;
508
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100509 /*
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100510 * buffer length must be at least one, for our length byte
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100511 */
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100512 if( blen < 1 )
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100513 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
514
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100515 if( ( ret = ecp_point_write_binary( grp, pt, format,
516 olen, buf + 1, blen - 1) ) != 0 )
517 return( ret );
518
519 /*
520 * write length to the first byte and update total length
521 */
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200522 buf[0] = (unsigned char) *olen;
Manuel Pégourié-Gonnard420f1eb2013-02-10 12:22:46 +0100523 ++*olen;
524
525 return 0;
Manuel Pégourié-Gonnard00794052013-02-09 19:00:07 +0100526}
527
528/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200529 * Import an ECP group from ASCII strings, case A == -3
Manuel Pégourié-Gonnard210b4582013-10-23 14:03:00 +0200530 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200531int ecp_group_read_string( ecp_group *grp, int radix,
532 const char *p, const char *b,
533 const char *gx, const char *gy, const char *n)
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100534{
535 int ret;
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100536
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100537 MPI_CHK( mpi_read_string( &grp->P, radix, p ) );
Manuel Pégourié-Gonnardd5e0fbe2013-12-02 17:20:39 +0100538 MPI_CHK( mpi_read_string( &grp->B, radix, b ) );
539 MPI_CHK( ecp_point_read_string( &grp->G, radix, gx, gy ) );
540 MPI_CHK( mpi_read_string( &grp->N, radix, n ) );
541
542 grp->pbits = mpi_msb( &grp->P );
543 grp->nbits = mpi_msb( &grp->N );
Manuel Pégourié-Gonnard84338242012-11-11 20:45:18 +0100544
545cleanup:
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200546 if( ret != 0 )
547 ecp_group_free( grp );
Manuel Pégourié-Gonnarde783f062013-10-21 14:52:21 +0200548
549 return( ret );
550}
Manuel Pégourié-Gonnardc04c5302013-10-23 16:11:52 +0200551
Manuel Pégourié-Gonnard62aad142012-11-10 00:27:12 +0100552/*
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100553 * Set a group from an ECParameters record (RFC 4492)
554 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100555int ecp_tls_read_group( ecp_group *grp, const unsigned char **buf, size_t len )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100556{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200557 uint16_t tls_id;
558 const ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100559
560 /*
561 * We expect at least three bytes (see below)
562 */
563 if( len < 3 )
564 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
565
566 /*
567 * First byte is curve_type; only named_curve is handled
568 */
Manuel Pégourié-Gonnard7c145c62013-02-10 13:20:52 +0100569 if( *(*buf)++ != POLARSSL_ECP_TLS_NAMED_CURVE )
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100570 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
571
572 /*
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100573 * Next two bytes are the namedcurve value
Manuel Pégourié-Gonnard1a967282013-02-09 17:03:58 +0100574 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200575 tls_id = *(*buf)++;
576 tls_id <<= 8;
577 tls_id |= *(*buf)++;
578
579 if( ( curve_info = ecp_curve_info_from_tls_id( tls_id ) ) == NULL )
580 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
581
582 return ecp_use_known_dp( grp, curve_info->grp_id );
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100583}
584
585/*
586 * Write the ECParameters record corresponding to a group (RFC 4492)
587 */
588int ecp_tls_write_group( const ecp_group *grp, size_t *olen,
589 unsigned char *buf, size_t blen )
590{
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200591 const ecp_curve_info *curve_info;
592
593 if( ( curve_info = ecp_curve_info_from_grp_id( grp->id ) ) == NULL )
594 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200595
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100596 /*
597 * We are going to write 3 bytes (see below)
598 */
599 *olen = 3;
600 if( blen < *olen )
601 return( POLARSSL_ERR_ECP_BUFFER_TOO_SMALL );
602
603 /*
604 * First byte is curve_type, always named_curve
605 */
606 *buf++ = POLARSSL_ECP_TLS_NAMED_CURVE;
607
608 /*
609 * Next two bytes are the namedcurve value
610 */
Manuel Pégourié-Gonnardf24b4a72013-09-23 18:14:50 +0200611 buf[0] = curve_info->tls_id >> 8;
612 buf[1] = curve_info->tls_id & 0xFF;
Manuel Pégourié-Gonnardb3258872013-02-10 12:06:19 +0100613
614 return 0;
Manuel Pégourié-Gonnarda5402fe2012-11-07 20:24:05 +0100615}
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100616
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200617/*
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200618 * Wrapper around fast quasi-modp functions, with fall-back to mpi_mod_mpi.
619 * See the documentation of struct ecp_group.
620 *
621 * This function is in the critial loop for ecp_mul, so pay attention to perf.
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200622 */
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200623static int ecp_modp( mpi *N, const ecp_group *grp )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200624{
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200625 int ret;
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200626
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200627 if( grp->modp == NULL )
628 return( mpi_mod_mpi( N, N, &grp->P ) );
629
630 /* N->s < 0 is a much faster test, which fails only if N is 0 */
631 if( ( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 ) ||
632 mpi_msb( N ) > 2 * grp->pbits )
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200633 {
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200634 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200635 }
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200636
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200637 MPI_CHK( grp->modp( N ) );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200638
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200639 /* N->s < 0 is a much faster test, which fails only if N is 0 */
640 while( N->s < 0 && mpi_cmp_int( N, 0 ) != 0 )
641 MPI_CHK( mpi_add_mpi( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200642
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200643 while( mpi_cmp_mpi( N, &grp->P ) >= 0 )
644 /* we known P, N and the result are positive */
645 MPI_CHK( mpi_sub_abs( N, N, &grp->P ) );
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200646
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200647cleanup:
648 return( ret );
Manuel Pégourié-Gonnard70380392013-09-16 16:19:53 +0200649}
Manuel Pégourié-Gonnard568c9cf2013-09-16 17:30:04 +0200650
Manuel Pégourié-Gonnard847395a2012-11-05 13:13:44 +0100651/*
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100652 * Fast mod-p functions expect their argument to be in the 0..p^2 range.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100653 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100654 * In order to guarantee that, we need to ensure that operands of
655 * mpi_mul_mpi are in the 0..p range. So, after each operation we will
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100656 * bring the result back to this range.
657 *
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100658 * The following macros are shortcuts for doing that.
Manuel Pégourié-Gonnarddada4da2012-11-10 14:23:17 +0100659 */
660
661/*
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100662 * Reduce a mpi mod p in-place, general case, to use after mpi_mul_mpi
663 */
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +0100664#if defined(POLARSSL_SELF_TEST)
665#define INC_MUL_COUNT mul_count++;
666#else
667#define INC_MUL_COUNT
668#endif
669
670#define MOD_MUL( N ) do { MPI_CHK( ecp_modp( &N, grp ) ); INC_MUL_COUNT } \
671 while( 0 )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100672
673/*
674 * Reduce a mpi mod p in-place, to use after mpi_sub_mpi
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200675 * N->s < 0 is a very fast test, which fails only if N is 0
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100676 */
677#define MOD_SUB( N ) \
Manuel Pégourié-Gonnardcae6f3e2013-10-23 20:19:57 +0200678 while( N.s < 0 && mpi_cmp_int( &N, 0 ) != 0 ) \
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100679 MPI_CHK( mpi_add_mpi( &N, &N, &grp->P ) )
680
681/*
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200682 * Reduce a mpi mod p in-place, to use after mpi_add_mpi and mpi_mul_int.
683 * We known P, N and the result are positive, so sub_abs is correct, and
684 * a bit faster.
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100685 */
686#define MOD_ADD( N ) \
687 while( mpi_cmp_mpi( &N, &grp->P ) >= 0 ) \
Manuel Pégourié-Gonnardc9e387c2013-10-17 17:15:35 +0200688 MPI_CHK( mpi_sub_abs( &N, &N, &grp->P ) )
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100689
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100690#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
691/*
692 * For curves in short Weierstrass form, we do all the internal operations in
693 * Jacobian coordinates.
694 *
695 * For multiplication, we'll use a comb method with coutermeasueres against
696 * SPA, hence timing attacks.
697 */
698
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +0100699/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100700 * Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100701 * Cost: 1N := 1I + 3M + 1S
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100702 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100703static int ecp_normalize_jac( const ecp_group *grp, ecp_point *pt )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100704{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100705 int ret;
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100706 mpi Zi, ZZi;
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100707
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100708 if( mpi_cmp_int( &pt->Z, 0 ) == 0 )
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100709 return( 0 );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100710
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100711 mpi_init( &Zi ); mpi_init( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100712
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100713 /*
714 * X = X / Z^2 mod p
715 */
716 MPI_CHK( mpi_inv_mod( &Zi, &pt->Z, &grp->P ) );
717 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
718 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ZZi ) ); MOD_MUL( pt->X );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100719
720 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100721 * Y = Y / Z^3 mod p
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100722 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100723 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ZZi ) ); MOD_MUL( pt->Y );
724 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &Zi ) ); MOD_MUL( pt->Y );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100725
726 /*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100727 * Z = 1
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100728 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100729 MPI_CHK( mpi_lset( &pt->Z, 1 ) );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100730
731cleanup:
732
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100733 mpi_free( &Zi ); mpi_free( &ZZi );
Manuel Pégourié-Gonnardd070f512012-11-08 17:40:51 +0100734
735 return( ret );
736}
737
738/*
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100739 * Normalize jacobian coordinates of an array of (pointers to) points,
Manuel Pégourié-Gonnard3680c822012-11-21 18:49:45 +0100740 * using Montgomery's trick to perform only one inversion mod P.
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100741 * (See for example Cohen's "A Course in Computational Algebraic Number
742 * Theory", Algorithm 10.3.4.)
743 *
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +0200744 * Warning: fails (returning an error) if one of the points is zero!
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100745 * This should never happen, see choice of w in ecp_mul_comb().
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100746 *
747 * Cost: 1N(t) := 1I + (6t - 3)M + 1S
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100748 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100749static int ecp_normalize_jac_many( const ecp_group *grp,
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +0100750 ecp_point *T[], size_t t_len )
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100751{
752 int ret;
753 size_t i;
754 mpi *c, u, Zi, ZZi;
755
756 if( t_len < 2 )
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100757 return( ecp_normalize_jac( grp, *T ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100758
Paul Bakker6e339b52013-07-03 13:37:05 +0200759 if( ( c = (mpi *) polarssl_malloc( t_len * sizeof( mpi ) ) ) == NULL )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200760 return( POLARSSL_ERR_ECP_MALLOC_FAILED );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100761
762 mpi_init( &u ); mpi_init( &Zi ); mpi_init( &ZZi );
763 for( i = 0; i < t_len; i++ )
764 mpi_init( &c[i] );
765
766 /*
767 * c[i] = Z_0 * ... * Z_i
768 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100769 MPI_CHK( mpi_copy( &c[0], &T[0]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100770 for( i = 1; i < t_len; i++ )
771 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100772 MPI_CHK( mpi_mul_mpi( &c[i], &c[i-1], &T[i]->Z ) );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100773 MOD_MUL( c[i] );
774 }
775
776 /*
777 * u = 1 / (Z_0 * ... * Z_n) mod P
778 */
779 MPI_CHK( mpi_inv_mod( &u, &c[t_len-1], &grp->P ) );
780
781 for( i = t_len - 1; ; i-- )
782 {
783 /*
784 * Zi = 1 / Z_i mod p
785 * u = 1 / (Z_0 * ... * Z_i) mod P
786 */
787 if( i == 0 ) {
788 MPI_CHK( mpi_copy( &Zi, &u ) );
789 }
790 else
791 {
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100792 MPI_CHK( mpi_mul_mpi( &Zi, &u, &c[i-1] ) ); MOD_MUL( Zi );
793 MPI_CHK( mpi_mul_mpi( &u, &u, &T[i]->Z ) ); MOD_MUL( u );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100794 }
795
796 /*
797 * proceed as in normalize()
798 */
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +0100799 MPI_CHK( mpi_mul_mpi( &ZZi, &Zi, &Zi ) ); MOD_MUL( ZZi );
800 MPI_CHK( mpi_mul_mpi( &T[i]->X, &T[i]->X, &ZZi ) ); MOD_MUL( T[i]->X );
801 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &ZZi ) ); MOD_MUL( T[i]->Y );
802 MPI_CHK( mpi_mul_mpi( &T[i]->Y, &T[i]->Y, &Zi ) ); MOD_MUL( T[i]->Y );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100803
804 /*
805 * Post-precessing: reclaim some memory by shrinking coordinates
806 * - not storing Z (always 1)
807 * - shrinking other coordinates, but still keeping the same number of
808 * limbs as P, as otherwise it will too likely be regrown too fast.
809 */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +0100810 MPI_CHK( mpi_shrink( &T[i]->X, grp->P.n ) );
811 MPI_CHK( mpi_shrink( &T[i]->Y, grp->P.n ) );
Manuel Pégourié-Gonnard1f789b82013-12-30 17:31:56 +0100812 mpi_free( &T[i]->Z );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100813
814 if( i == 0 )
815 break;
816 }
817
818cleanup:
819
820 mpi_free( &u ); mpi_free( &Zi ); mpi_free( &ZZi );
821 for( i = 0; i < t_len; i++ )
822 mpi_free( &c[i] );
Paul Bakker6e339b52013-07-03 13:37:05 +0200823 polarssl_free( c );
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100824
825 return( ret );
826}
827
Manuel Pégourié-Gonnardcdd44322012-11-21 16:00:55 +0100828/*
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100829 * Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
830 * "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
831 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +0100832static int ecp_safe_invert_jac( const ecp_group *grp,
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +0100833 ecp_point *Q,
834 unsigned char inv )
835{
836 int ret;
837 unsigned char nonzero;
838 mpi mQY;
839
840 mpi_init( &mQY );
841
842 /* Use the fact that -Q.Y mod P = P - Q.Y unless Q.Y == 0 */
843 MPI_CHK( mpi_sub_mpi( &mQY, &grp->P, &Q->Y ) );
844 nonzero = mpi_cmp_int( &Q->Y, 0 ) != 0;
845 MPI_CHK( mpi_safe_cond_assign( &Q->Y, &mQY, inv & nonzero ) );
846
847cleanup:
848 mpi_free( &mQY );
849
850 return( ret );
851}
852
853/*
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200854 * Point doubling R = 2 P, Jacobian coordinates
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200855 *
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200856 * http://www.hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian/doubling/dbl-2007-bl.op3
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200857 * with heavy variable renaming, some reordering and one minor modification
858 * (a = 2 * b, c = d - 2a replaced with c = d, c = c - b, c = c - b)
859 * in order to use a lot less intermediate variables (6 vs 25).
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100860 *
861 * Cost: 1D := 2M + 8S
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200862 */
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200863static int ecp_double_jac( const ecp_group *grp, ecp_point *R,
864 const ecp_point *P )
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200865{
866 int ret;
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200867 mpi T1, T2, T3, X3, Y3, Z3;
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200868
Manuel Pégourié-Gonnard0cd6f982013-10-10 15:55:39 +0200869#if defined(POLARSSL_SELF_TEST)
870 dbl_count++;
871#endif
872
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200873 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 );
874 mpi_init( &X3 ); mpi_init( &Y3 ); mpi_init( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200875
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200876 MPI_CHK( mpi_mul_mpi( &T3, &P->X, &P->X ) ); MOD_MUL( T3 );
877 MPI_CHK( mpi_mul_mpi( &T2, &P->Y, &P->Y ) ); MOD_MUL( T2 );
878 MPI_CHK( mpi_mul_mpi( &Y3, &T2, &T2 ) ); MOD_MUL( Y3 );
879 MPI_CHK( mpi_add_mpi( &X3, &P->X, &T2 ) ); MOD_ADD( X3 );
880 MPI_CHK( mpi_mul_mpi( &X3, &X3, &X3 ) ); MOD_MUL( X3 );
881 MPI_CHK( mpi_sub_mpi( &X3, &X3, &Y3 ) ); MOD_SUB( X3 );
882 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T3 ) ); MOD_SUB( X3 );
883 MPI_CHK( mpi_mul_int( &T1, &X3, 2 ) ); MOD_ADD( T1 );
884 MPI_CHK( mpi_mul_mpi( &Z3, &P->Z, &P->Z ) ); MOD_MUL( Z3 );
885 MPI_CHK( mpi_mul_mpi( &X3, &Z3, &Z3 ) ); MOD_MUL( X3 );
886 MPI_CHK( mpi_mul_int( &T3, &T3, 3 ) ); MOD_ADD( T3 );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +0100887
888 /* Special case for A = -3 */
889 if( grp->A.p == NULL )
890 {
891 MPI_CHK( mpi_mul_int( &X3, &X3, 3 ) );
892 X3.s = -1; /* mpi_mul_int doesn't handle negative numbers */
893 MOD_SUB( X3 );
894 }
895 else
896 MPI_CHK( mpi_mul_mpi( &X3, &X3, &grp->A ) ); MOD_MUL( X3 );
897
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200898 MPI_CHK( mpi_add_mpi( &T3, &T3, &X3 ) ); MOD_ADD( T3 );
899 MPI_CHK( mpi_mul_mpi( &X3, &T3, &T3 ) ); MOD_MUL( X3 );
900 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
901 MPI_CHK( mpi_sub_mpi( &X3, &X3, &T1 ) ); MOD_SUB( X3 );
902 MPI_CHK( mpi_sub_mpi( &T1, &T1, &X3 ) ); MOD_SUB( T1 );
903 MPI_CHK( mpi_mul_mpi( &T1, &T3, &T1 ) ); MOD_MUL( T1 );
904 MPI_CHK( mpi_mul_int( &T3, &Y3, 8 ) ); MOD_ADD( T3 );
905 MPI_CHK( mpi_sub_mpi( &Y3, &T1, &T3 ) ); MOD_SUB( Y3 );
906 MPI_CHK( mpi_add_mpi( &T1, &P->Y, &P->Z ) ); MOD_ADD( T1 );
907 MPI_CHK( mpi_mul_mpi( &T1, &T1, &T1 ) ); MOD_MUL( T1 );
908 MPI_CHK( mpi_sub_mpi( &T1, &T1, &T2 ) ); MOD_SUB( T1 );
909 MPI_CHK( mpi_sub_mpi( &Z3, &T1, &Z3 ) ); MOD_SUB( Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200910
911 MPI_CHK( mpi_copy( &R->X, &X3 ) );
912 MPI_CHK( mpi_copy( &R->Y, &Y3 ) );
913 MPI_CHK( mpi_copy( &R->Z, &Z3 ) );
914
915cleanup:
Manuel Pégourié-Gonnard0ace4b32013-10-10 12:44:27 +0200916 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 );
917 mpi_free( &X3 ); mpi_free( &Y3 ); mpi_free( &Z3 );
Manuel Pégourié-Gonnard1c4aa242013-10-09 16:09:46 +0200918
919 return( ret );
920}
921
922/*
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100923 * Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +0100924 *
925 * The coordinates of Q must be normalized (= affine),
926 * but those of P don't need to. R is not normalized.
927 *
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100928 * Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
Manuel Pégourié-Gonnard7a949d32013-12-05 10:26:01 +0100929 * None of these cases can happen as intermediate step in ecp_mul_comb():
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100930 * - at each step, P, Q and R are multiples of the base point, the factor
931 * being less than its order, so none of them is zero;
932 * - Q is an odd multiple of the base point, P an even multiple,
933 * due to the choice of precomputed points in the modified comb method.
934 * So branches for these cases do not leak secret information.
935 *
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100936 * We accept Q->Z being unset (saving memory in tables) as meaning 1.
937 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +0100938 * Cost: 1A := 8M + 3S
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100939 */
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100940static int ecp_add_mixed( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100941 const ecp_point *P, const ecp_point *Q )
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100942{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100943 int ret;
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100944 mpi T1, T2, T3, T4, X, Y, Z;
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100945
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100946#if defined(POLARSSL_SELF_TEST)
947 add_count++;
948#endif
949
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100950 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100951 * Trivial cases: P == 0 or Q == 0 (case 1)
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100952 */
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +0100953 if( mpi_cmp_int( &P->Z, 0 ) == 0 )
954 return( ecp_copy( R, Q ) );
955
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100956 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 0 ) == 0 )
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100957 return( ecp_copy( R, P ) );
958
959 /*
960 * Make sure Q coordinates are normalized
961 */
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +0100962 if( Q->Z.p != NULL && mpi_cmp_int( &Q->Z, 1 ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +0200963 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100964
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100965 mpi_init( &T1 ); mpi_init( &T2 ); mpi_init( &T3 ); mpi_init( &T4 );
966 mpi_init( &X ); mpi_init( &Y ); mpi_init( &Z );
Manuel Pégourié-Gonnardab38b702012-11-05 17:34:55 +0100967
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100968 MPI_CHK( mpi_mul_mpi( &T1, &P->Z, &P->Z ) ); MOD_MUL( T1 );
969 MPI_CHK( mpi_mul_mpi( &T2, &T1, &P->Z ) ); MOD_MUL( T2 );
970 MPI_CHK( mpi_mul_mpi( &T1, &T1, &Q->X ) ); MOD_MUL( T1 );
971 MPI_CHK( mpi_mul_mpi( &T2, &T2, &Q->Y ) ); MOD_MUL( T2 );
972 MPI_CHK( mpi_sub_mpi( &T1, &T1, &P->X ) ); MOD_SUB( T1 );
973 MPI_CHK( mpi_sub_mpi( &T2, &T2, &P->Y ) ); MOD_SUB( T2 );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +0100974
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +0100975 /* Special cases (2) and (3) */
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100976 if( mpi_cmp_int( &T1, 0 ) == 0 )
977 {
978 if( mpi_cmp_int( &T2, 0 ) == 0 )
979 {
980 ret = ecp_double_jac( grp, R, P );
981 goto cleanup;
982 }
983 else
984 {
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +0100985 ret = ecp_set_zero( R );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +0100986 goto cleanup;
987 }
988 }
989
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +0100990 MPI_CHK( mpi_mul_mpi( &Z, &P->Z, &T1 ) ); MOD_MUL( Z );
991 MPI_CHK( mpi_mul_mpi( &T3, &T1, &T1 ) ); MOD_MUL( T3 );
992 MPI_CHK( mpi_mul_mpi( &T4, &T3, &T1 ) ); MOD_MUL( T4 );
993 MPI_CHK( mpi_mul_mpi( &T3, &T3, &P->X ) ); MOD_MUL( T3 );
994 MPI_CHK( mpi_mul_int( &T1, &T3, 2 ) ); MOD_ADD( T1 );
995 MPI_CHK( mpi_mul_mpi( &X, &T2, &T2 ) ); MOD_MUL( X );
996 MPI_CHK( mpi_sub_mpi( &X, &X, &T1 ) ); MOD_SUB( X );
997 MPI_CHK( mpi_sub_mpi( &X, &X, &T4 ) ); MOD_SUB( X );
998 MPI_CHK( mpi_sub_mpi( &T3, &T3, &X ) ); MOD_SUB( T3 );
999 MPI_CHK( mpi_mul_mpi( &T3, &T3, &T2 ) ); MOD_MUL( T3 );
1000 MPI_CHK( mpi_mul_mpi( &T4, &T4, &P->Y ) ); MOD_MUL( T4 );
1001 MPI_CHK( mpi_sub_mpi( &Y, &T3, &T4 ) ); MOD_SUB( Y );
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001002
Manuel Pégourié-Gonnard84d1aea2012-11-09 02:09:38 +01001003 MPI_CHK( mpi_copy( &R->X, &X ) );
1004 MPI_CHK( mpi_copy( &R->Y, &Y ) );
1005 MPI_CHK( mpi_copy( &R->Z, &Z ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001006
1007cleanup:
1008
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001009 mpi_free( &T1 ); mpi_free( &T2 ); mpi_free( &T3 ); mpi_free( &T4 );
1010 mpi_free( &X ); mpi_free( &Y ); mpi_free( &Z );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001011
1012 return( ret );
1013}
1014
1015/*
Manuel Pégourié-Gonnard1c2782c2012-11-19 20:16:28 +01001016 * Addition: R = P + Q, result's coordinates normalized
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001017 */
1018int ecp_add( const ecp_group *grp, ecp_point *R,
1019 const ecp_point *P, const ecp_point *Q )
1020{
Manuel Pégourié-Gonnard47123252012-11-10 14:44:24 +01001021 int ret;
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001022
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001023 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001024 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1025
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001026 MPI_CHK( ecp_add_mixed( grp, R, P, Q ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001027 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001028
1029cleanup:
1030 return( ret );
1031}
1032
1033/*
1034 * Subtraction: R = P - Q, result's coordinates normalized
1035 */
1036int ecp_sub( const ecp_group *grp, ecp_point *R,
1037 const ecp_point *P, const ecp_point *Q )
1038{
1039 int ret;
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001040 ecp_point mQ;
Manuel Pégourié-Gonnard9674fd02012-11-19 21:23:27 +01001041
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001042 ecp_point_init( &mQ );
1043
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001044 if( ecp_get_type( grp ) != POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard97871ef2013-12-04 20:52:04 +01001045 return( POLARSSL_ERR_ECP_FEATURE_UNAVAILABLE );
1046
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001047 /* mQ = - Q */
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001048 MPI_CHK( ecp_copy( &mQ, Q ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001049 if( mpi_cmp_int( &mQ.Y, 0 ) != 0 )
1050 MPI_CHK( mpi_sub_mpi( &mQ.Y, &grp->P, &mQ.Y ) );
1051
1052 MPI_CHK( ecp_add_mixed( grp, R, P, &mQ ) );
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001053 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001054
Manuel Pégourié-Gonnard989c32b2012-11-08 22:02:42 +01001055cleanup:
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001056 ecp_point_free( &mQ );
1057
Manuel Pégourié-Gonnard7e0adfb2012-11-08 23:21:46 +01001058 return( ret );
Manuel Pégourié-Gonnardae180d02012-11-02 18:14:40 +01001059}
1060
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001061/*
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001062 * Randomize jacobian coordinates:
1063 * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001064 * This is sort of the reverse operation of ecp_normalize_jac().
Manuel Pégourié-Gonnard44aab792013-11-21 10:53:59 +01001065 *
1066 * This countermeasure was first suggested in [2].
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001067 */
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001068static int ecp_randomize_jac( const ecp_group *grp, ecp_point *pt,
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001069 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1070{
1071 int ret;
1072 mpi l, ll;
1073 size_t p_size = (grp->pbits + 7) / 8;
1074 int count = 0;
1075
1076 mpi_init( &l ); mpi_init( &ll );
1077
1078 /* Generate l such that 1 < l < p */
1079 do
1080 {
1081 mpi_fill_random( &l, p_size, f_rng, p_rng );
1082
1083 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1084 mpi_shift_r( &l, 1 );
1085
1086 if( count++ > 10 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001087 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
Manuel Pégourié-Gonnard07de4b12013-09-02 16:26:04 +02001088 }
1089 while( mpi_cmp_int( &l, 1 ) <= 0 );
1090
1091 /* Z = l * Z */
1092 MPI_CHK( mpi_mul_mpi( &pt->Z, &pt->Z, &l ) ); MOD_MUL( pt->Z );
1093
1094 /* X = l^2 * X */
1095 MPI_CHK( mpi_mul_mpi( &ll, &l, &l ) ); MOD_MUL( ll );
1096 MPI_CHK( mpi_mul_mpi( &pt->X, &pt->X, &ll ) ); MOD_MUL( pt->X );
1097
1098 /* Y = l^3 * Y */
1099 MPI_CHK( mpi_mul_mpi( &ll, &ll, &l ) ); MOD_MUL( ll );
1100 MPI_CHK( mpi_mul_mpi( &pt->Y, &pt->Y, &ll ) ); MOD_MUL( pt->Y );
1101
1102cleanup:
1103 mpi_free( &l ); mpi_free( &ll );
1104
1105 return( ret );
1106}
1107
1108/*
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001109 * Check and define parameters used by the comb method (see below for details)
1110 */
1111#if POLARSSL_ECP_WINDOW_SIZE < 2 || POLARSSL_ECP_WINDOW_SIZE > 7
1112#error "POLARSSL_ECP_WINDOW_SIZE out of bounds"
1113#endif
1114
1115/* d = ceil( n / w ) */
1116#define COMB_MAX_D ( POLARSSL_ECP_MAX_BITS + 1 ) / 2
1117
1118/* number of precomputed points */
1119#define COMB_MAX_PRE ( 1 << ( POLARSSL_ECP_WINDOW_SIZE - 1 ) )
1120
1121/*
1122 * Compute the representation of m that will be used with our comb method.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001123 *
1124 * The basic comb method is described in GECC 3.44 for example. We use a
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001125 * modified version that provides resistance to SPA by avoiding zero
1126 * digits in the representation as in [3]. We modify the method further by
1127 * requiring that all K_i be odd, which has the small cost that our
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001128 * representation uses one more K_i, due to carries.
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001129 *
1130 * Also, for the sake of compactness, only the seven low-order bits of x[i]
1131 * are used to represent K_i, and the msb of x[i] encodes the the sign (s_i in
1132 * the paper): it is set if and only if if s_i == -1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001133 *
1134 * Calling conventions:
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001135 * - x is an array of size d + 1
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001136 * - w is the size, ie number of teeth, of the comb, and must be between
1137 * 2 and 7 (in practice, between 2 and POLARSSL_ECP_WINDOW_SIZE)
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001138 * - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
1139 * (the result will be incorrect if these assumptions are not satisfied)
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001140 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001141static void ecp_comb_fixed( unsigned char x[], size_t d,
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001142 unsigned char w, const mpi *m )
1143{
1144 size_t i, j;
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001145 unsigned char c, cc, adjust;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001146
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001147 memset( x, 0, d+1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001148
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001149 /* First get the classical comb values (except for x_d = 0) */
1150 for( i = 0; i < d; i++ )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001151 for( j = 0; j < w; j++ )
1152 x[i] |= mpi_get_bit( m, i + d * j ) << j;
1153
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001154 /* Now make sure x_1 .. x_d are odd */
1155 c = 0;
1156 for( i = 1; i <= d; i++ )
1157 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001158 /* Add carry and update it */
1159 cc = x[i] & c;
1160 x[i] = x[i] ^ c;
1161 c = cc;
1162
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001163 /* Adjust if needed, avoiding branches */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001164 adjust = 1 - ( x[i] & 0x01 );
1165 c |= x[i] & ( x[i-1] * adjust );
1166 x[i] = x[i] ^ ( x[i-1] * adjust );
1167 x[i-1] |= adjust << 7;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001168 }
1169}
1170
1171/*
1172 * Precompute points for the comb method
1173 *
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001174 * If i = i_{w-1} ... i_1 is the binary representation of i, then
1175 * 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 +01001176 *
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001177 * T must be able to hold 2^{w - 1} elements
1178 *
1179 * 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 +01001180 */
1181static int ecp_precompute_comb( const ecp_group *grp,
1182 ecp_point T[], const ecp_point *P,
1183 unsigned char w, size_t d )
1184{
1185 int ret;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001186 unsigned char i, k;
1187 size_t j;
1188 ecp_point *cur, *TT[COMB_MAX_PRE - 1];
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001189
1190 /*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001191 * Set T[0] = P and
1192 * 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 +01001193 */
1194 MPI_CHK( ecp_copy( &T[0], P ) );
1195
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001196 k = 0;
1197 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001198 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001199 cur = T + i;
1200 MPI_CHK( ecp_copy( cur, T + ( i >> 1 ) ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001201 for( j = 0; j < d; j++ )
1202 MPI_CHK( ecp_double_jac( grp, cur, cur ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001203
1204 TT[k++] = cur;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001205 }
1206
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001207 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001208
1209 /*
1210 * Compute the remaining ones using the minimal number of additions
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001211 * Be careful to update T[2^l] only after using it!
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001212 */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001213 k = 0;
1214 for( i = 1; i < ( 1U << (w-1) ); i <<= 1 )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001215 {
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001216 j = i;
1217 while( j-- )
1218 {
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001219 MPI_CHK( ecp_add_mixed( grp, &T[i + j], &T[j], &T[i] ) );
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001220 TT[k++] = &T[i + j];
1221 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001222 }
1223
Manuel Pégourié-Gonnard26bc1c02013-12-30 19:33:33 +01001224 MPI_CHK( ecp_normalize_jac_many( grp, TT, k ) );
Manuel Pégourié-Gonnarde2820122013-11-21 10:08:50 +01001225
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001226cleanup:
1227 return( ret );
1228}
1229
1230/*
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001231 * Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001232 */
1233static int ecp_select_comb( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001234 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001235 unsigned char i )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001236{
1237 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001238 unsigned char ii, j;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001239
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001240 /* Ignore the "sign" bit and scale down */
1241 ii = ( i & 0x7Fu ) >> 1;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001242
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001243 /* Read the whole table to thwart cache-based timing attacks */
1244 for( j = 0; j < t_len; j++ )
1245 {
1246 MPI_CHK( mpi_safe_cond_assign( &R->X, &T[j].X, j == ii ) );
1247 MPI_CHK( mpi_safe_cond_assign( &R->Y, &T[j].Y, j == ii ) );
1248 }
1249
Manuel Pégourié-Gonnard01fca5e2013-11-21 17:47:12 +01001250 /* Safely invert result if i is "negative" */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001251 MPI_CHK( ecp_safe_invert_jac( grp, R, i >> 7 ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001252
1253cleanup:
1254 return( ret );
1255}
1256
1257/*
1258 * Core multiplication algorithm for the (modified) comb method.
1259 * This part is actually common with the basic comb method (GECC 3.44)
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001260 *
1261 * Cost: d A + d D + 1 R
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001262 */
1263static int ecp_mul_comb_core( const ecp_group *grp, ecp_point *R,
Manuel Pégourié-Gonnard96c7a922013-11-25 18:28:53 +01001264 const ecp_point T[], unsigned char t_len,
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001265 const unsigned char x[], size_t d,
1266 int (*f_rng)(void *, unsigned char *, size_t),
1267 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001268{
1269 int ret;
1270 ecp_point Txi;
1271 size_t i;
1272
1273 ecp_point_init( &Txi );
1274
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001275 /* Start with a non-zero point and randomize its coordinates */
Manuel Pégourié-Gonnard101a39f2013-11-20 14:47:19 +01001276 i = d;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001277 MPI_CHK( ecp_select_comb( grp, R, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard72c172a2013-12-30 16:04:55 +01001278 MPI_CHK( mpi_lset( &R->Z, 1 ) );
Manuel Pégourié-Gonnard70c14372013-11-20 20:07:26 +01001279 if( f_rng != 0 )
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001280 MPI_CHK( ecp_randomize_jac( grp, R, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001281
1282 while( i-- != 0 )
1283 {
1284 MPI_CHK( ecp_double_jac( grp, R, R ) );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001285 MPI_CHK( ecp_select_comb( grp, &Txi, T, t_len, x[i] ) );
Manuel Pégourié-Gonnard469a2092013-11-21 18:20:43 +01001286 MPI_CHK( ecp_add_mixed( grp, R, R, &Txi ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001287 }
1288
1289cleanup:
1290 ecp_point_free( &Txi );
1291
1292 return( ret );
1293}
1294
1295/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001296 * Multiplication using the comb method,
1297 * for curves in short Weierstrass form
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001298 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001299static int ecp_mul_comb( ecp_group *grp, ecp_point *R,
1300 const mpi *m, const ecp_point *P,
1301 int (*f_rng)(void *, unsigned char *, size_t),
1302 void *p_rng )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001303{
1304 int ret;
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001305 unsigned char w, m_is_odd, p_eq_g, pre_len, i;
1306 size_t d;
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001307 unsigned char k[COMB_MAX_D + 1];
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001308 ecp_point *T;
1309 mpi M, mm;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001310
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001311 mpi_init( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001312 mpi_init( &mm );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001313
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001314 /* we need N to be odd to trnaform m in an odd number, check now */
1315 if( mpi_get_bit( &grp->N, 0 ) != 1 )
1316 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1317
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001318 /*
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001319 * Minimize the number of multiplications, that is minimize
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001320 * 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 +01001321 * (see costs of the various parts, with 1S = 1M)
1322 */
1323 w = grp->nbits >= 384 ? 5 : 4;
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001324
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001325 /*
1326 * If P == G, pre-compute a bit more, since this may be re-used later.
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001327 * Just adding one avoids upping the cost of the first mul too much,
1328 * and the memory cost too.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001329 */
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001330#if POLARSSL_ECP_FIXED_POINT_OPTIM == 1
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001331 p_eq_g = ( mpi_cmp_mpi( &P->Y, &grp->G.Y ) == 0 &&
1332 mpi_cmp_mpi( &P->X, &grp->G.X ) == 0 );
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001333 if( p_eq_g )
1334 w++;
Manuel Pégourié-Gonnard9e4191c2013-12-30 18:41:16 +01001335#else
1336 p_eq_g = 0;
1337#endif
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001338
1339 /*
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001340 * Make sure w is within bounds.
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001341 * (The last test is useful only for very small curves in the test suite.)
1342 */
1343 if( w > POLARSSL_ECP_WINDOW_SIZE )
1344 w = POLARSSL_ECP_WINDOW_SIZE;
Manuel Pégourié-Gonnard36daa132013-11-21 18:33:36 +01001345 if( w >= grp->nbits )
Manuel Pégourié-Gonnard04a02252013-11-20 22:57:38 +01001346 w = 2;
1347
1348 /* Other sizes that depend on w */
Manuel Pégourié-Gonnardc30200e2013-11-20 18:39:55 +01001349 pre_len = 1U << ( w - 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001350 d = ( grp->nbits + w - 1 ) / w;
1351
1352 /*
1353 * Prepare precomputed points: if P == G we want to
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001354 * use grp->T if already initialized, or initialize it.
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001355 */
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001356 T = p_eq_g ? grp->T : NULL;
Manuel Pégourié-Gonnardedc1a1f2013-11-21 09:50:00 +01001357
1358 if( T == NULL )
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001359 {
1360 T = (ecp_point *) polarssl_malloc( pre_len * sizeof( ecp_point ) );
1361 if( T == NULL )
1362 {
1363 ret = POLARSSL_ERR_ECP_MALLOC_FAILED;
1364 goto cleanup;
1365 }
1366
1367 for( i = 0; i < pre_len; i++ )
1368 ecp_point_init( &T[i] );
1369
1370 MPI_CHK( ecp_precompute_comb( grp, T, P, w, d ) );
1371
1372 if( p_eq_g )
1373 {
1374 grp->T = T;
1375 grp->T_size = pre_len;
1376 }
1377 }
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001378
1379 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001380 * Make sure M is odd (M = m or M = N - m, since N is odd)
1381 * using the fact that m * P = - (N - m) * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001382 */
1383 m_is_odd = ( mpi_get_bit( m, 0 ) == 1 );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001384 MPI_CHK( mpi_copy( &M, m ) );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001385 MPI_CHK( mpi_sub_mpi( &mm, &grp->N, m ) );
1386 MPI_CHK( mpi_safe_cond_assign( &M, &mm, ! m_is_odd ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001387
1388 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001389 * Go for comb multiplication, R = M * P
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001390 */
1391 ecp_comb_fixed( k, d, w, &M );
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +01001392 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 +01001393
1394 /*
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001395 * Now get m * P from M * P and normalize it
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001396 */
Manuel Pégourié-Gonnard3c0b4ea2013-12-02 19:44:41 +01001397 MPI_CHK( ecp_safe_invert_jac( grp, R, ! m_is_odd ) );
1398 MPI_CHK( ecp_normalize_jac( grp, R ) );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001399
1400cleanup:
1401
1402 if( T != NULL && ! p_eq_g )
1403 {
1404 for( i = 0; i < pre_len; i++ )
1405 ecp_point_free( &T[i] );
1406 polarssl_free( T );
1407 }
1408
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001409 mpi_free( &M );
Manuel Pégourié-Gonnardaade42f2013-11-21 19:19:54 +01001410 mpi_free( &mm );
1411
1412 if( ret != 0 )
1413 ecp_point_free( R );
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001414
1415 return( ret );
1416}
1417
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001418#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1419
1420#if defined(POLARSSL_ECP_MONTGOMERY)
1421/*
1422 * For Montgomery curves, we do all the internal arithmetic in projective
1423 * coordinates. Import/export of points uses only the x coordinates, which is
1424 * internaly represented as X / Z.
1425 *
1426 * For scalar multiplication, we'll use a Montgomery ladder.
1427 */
1428
Manuel Pégourié-Gonnardd1c1ba92013-11-16 15:50:12 +01001429/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001430 * Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
1431 * Cost: 1M + 1I
1432 */
1433static int ecp_normalize_mxz( const ecp_group *grp, ecp_point *P )
1434{
1435 int ret;
1436
1437 MPI_CHK( mpi_inv_mod( &P->Z, &P->Z, &grp->P ) );
1438 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &P->Z ) ); MOD_MUL( P->X );
1439 MPI_CHK( mpi_lset( &P->Z, 1 ) );
1440
1441cleanup:
1442 return( ret );
1443}
1444
1445/*
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001446 * Randomize projective x/z coordinates:
1447 * (X, Z) -> (l X, l Z) for random l
1448 * This is sort of the reverse operation of ecp_normalize_mxz().
1449 *
1450 * This countermeasure was first suggested in [2].
1451 * Cost: 2M
1452 */
1453static int ecp_randomize_mxz( const ecp_group *grp, ecp_point *P,
1454 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1455{
1456 int ret;
1457 mpi l;
1458 size_t p_size = (grp->pbits + 7) / 8;
1459 int count = 0;
1460
1461 mpi_init( &l );
1462
1463 /* Generate l such that 1 < l < p */
1464 do
1465 {
1466 mpi_fill_random( &l, p_size, f_rng, p_rng );
1467
1468 while( mpi_cmp_mpi( &l, &grp->P ) >= 0 )
1469 mpi_shift_r( &l, 1 );
1470
1471 if( count++ > 10 )
1472 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1473 }
1474 while( mpi_cmp_int( &l, 1 ) <= 0 );
1475
1476 MPI_CHK( mpi_mul_mpi( &P->X, &P->X, &l ) ); MOD_MUL( P->X );
1477 MPI_CHK( mpi_mul_mpi( &P->Z, &P->Z, &l ) ); MOD_MUL( P->Z );
1478
1479cleanup:
1480 mpi_free( &l );
1481
1482 return( ret );
1483}
1484
1485/*
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001486 * Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
1487 * for Montgomery curves in x/z coordinates.
1488 *
1489 * http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
1490 * with
1491 * d = X1
1492 * P = (X2, Z2)
1493 * Q = (X3, Z3)
1494 * R = (X4, Z4)
1495 * S = (X5, Z5)
1496 * and eliminating temporary variables tO, ..., t4.
1497 *
1498 * Cost: 5M + 4S
1499 */
1500static int ecp_double_add_mxz( const ecp_group *grp,
1501 ecp_point *R, ecp_point *S,
1502 const ecp_point *P, const ecp_point *Q,
1503 const mpi *d )
1504{
1505 int ret;
1506 mpi A, AA, B, BB, E, C, D, DA, CB;
1507
1508 mpi_init( &A ); mpi_init( &AA ); mpi_init( &B );
1509 mpi_init( &BB ); mpi_init( &E ); mpi_init( &C );
1510 mpi_init( &D ); mpi_init( &DA ); mpi_init( &CB );
1511
1512 MPI_CHK( mpi_add_mpi( &A, &P->X, &P->Z ) ); MOD_ADD( A );
1513 MPI_CHK( mpi_mul_mpi( &AA, &A, &A ) ); MOD_MUL( AA );
1514 MPI_CHK( mpi_sub_mpi( &B, &P->X, &P->Z ) ); MOD_SUB( B );
1515 MPI_CHK( mpi_mul_mpi( &BB, &B, &B ) ); MOD_MUL( BB );
1516 MPI_CHK( mpi_sub_mpi( &E, &AA, &BB ) ); MOD_SUB( E );
1517 MPI_CHK( mpi_add_mpi( &C, &Q->X, &Q->Z ) ); MOD_ADD( C );
1518 MPI_CHK( mpi_sub_mpi( &D, &Q->X, &Q->Z ) ); MOD_SUB( D );
1519 MPI_CHK( mpi_mul_mpi( &DA, &D, &A ) ); MOD_MUL( DA );
1520 MPI_CHK( mpi_mul_mpi( &CB, &C, &B ) ); MOD_MUL( CB );
1521 MPI_CHK( mpi_add_mpi( &S->X, &DA, &CB ) ); MOD_MUL( S->X );
1522 MPI_CHK( mpi_mul_mpi( &S->X, &S->X, &S->X ) ); MOD_MUL( S->X );
1523 MPI_CHK( mpi_sub_mpi( &S->Z, &DA, &CB ) ); MOD_SUB( S->Z );
1524 MPI_CHK( mpi_mul_mpi( &S->Z, &S->Z, &S->Z ) ); MOD_MUL( S->Z );
1525 MPI_CHK( mpi_mul_mpi( &S->Z, d, &S->Z ) ); MOD_MUL( S->Z );
1526 MPI_CHK( mpi_mul_mpi( &R->X, &AA, &BB ) ); MOD_MUL( R->X );
1527 MPI_CHK( mpi_mul_mpi( &R->Z, &grp->A, &E ) ); MOD_MUL( R->Z );
1528 MPI_CHK( mpi_add_mpi( &R->Z, &BB, &R->Z ) ); MOD_ADD( R->Z );
1529 MPI_CHK( mpi_mul_mpi( &R->Z, &E, &R->Z ) ); MOD_MUL( R->Z );
1530
1531cleanup:
1532 mpi_free( &A ); mpi_free( &AA ); mpi_free( &B );
1533 mpi_free( &BB ); mpi_free( &E ); mpi_free( &C );
1534 mpi_free( &D ); mpi_free( &DA ); mpi_free( &CB );
1535
1536 return( ret );
1537}
1538
1539/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001540 * Multiplication with Montgomery ladder in x/z coordinates,
1541 * for curves in Montgomery form
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001542 */
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001543static int ecp_mul_mxz( ecp_group *grp, ecp_point *R,
1544 const mpi *m, const ecp_point *P,
1545 int (*f_rng)(void *, unsigned char *, size_t),
1546 void *p_rng )
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001547{
1548 int ret;
1549 size_t i;
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001550 unsigned char b;
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001551 ecp_point RP;
1552 mpi PX;
1553
1554 ecp_point_init( &RP ); mpi_init( &PX );
1555
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001556 /* Save PX and read from P before writing to R, in case P == R */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001557 mpi_copy( &PX, &P->X );
1558 MPI_CHK( ecp_copy( &RP, P ) );
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001559
1560 /* Set R to zero in modified x/z coordinates */
1561 MPI_CHK( mpi_lset( &R->X, 1 ) );
1562 MPI_CHK( mpi_lset( &R->Z, 0 ) );
1563 mpi_free( &R->Y );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001564
Manuel Pégourié-Gonnard93f41db2013-12-05 10:48:42 +01001565 /* RP.X might be sligtly larger than P, so reduce it */
1566 MOD_ADD( RP.X );
1567
Manuel Pégourié-Gonnard3afa07f2013-12-03 13:28:21 +01001568 /* Randomize coordinates of the starting point */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001569 if( f_rng != NULL )
1570 MPI_CHK( ecp_randomize_mxz( grp, &RP, f_rng, p_rng ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001571
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001572 /* Loop invariant: R = result so far, RP = R + P */
Manuel Pégourié-Gonnard357ff652013-12-04 18:39:17 +01001573 i = mpi_msb( m ); /* one past the (zero-based) most significant bit */
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001574 while( i-- > 0 )
1575 {
Manuel Pégourié-Gonnardb6f45a62013-12-04 21:54:36 +01001576 b = mpi_get_bit( m, i );
1577 /*
1578 * if (b) R = 2R + P else R = 2R,
1579 * which is:
1580 * if (b) double_add( RP, R, RP, R )
1581 * else double_add( R, RP, R, RP )
1582 * but using safe conditional swaps to avoid leaks
1583 */
1584 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1585 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
1586 MPI_CHK( ecp_double_add_mxz( grp, R, &RP, R, &RP, &PX ) );
1587 MPI_CHK( mpi_safe_cond_swap( &R->X, &RP.X, b ) );
1588 MPI_CHK( mpi_safe_cond_swap( &R->Z, &RP.Z, b ) );
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001589 }
1590
1591 MPI_CHK( ecp_normalize_mxz( grp, R ) );
1592
1593cleanup:
1594 ecp_point_free( &RP ); mpi_free( &PX );
1595
1596 return( ret );
1597}
1598
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001599#endif /* POLARSSL_ECP_MONTGOMERY */
1600
Manuel Pégourié-Gonnardd9ea82e72013-12-03 12:02:28 +01001601/*
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001602 * Multiplication R = m * P
1603 */
1604int ecp_mul( ecp_group *grp, ecp_point *R,
1605 const mpi *m, const ecp_point *P,
1606 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1607{
1608 int ret;
1609
1610 /* Common sanity checks */
1611 if( mpi_cmp_int( &P->Z, 1 ) != 0 )
1612 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1613
1614 if( ( ret = ecp_check_privkey( grp, m ) ) != 0 ||
1615 ( ret = ecp_check_pubkey( grp, P ) ) != 0 )
1616 return( ret );
1617
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001618#if defined(POLARSSL_ECP_MONTGOMERY)
1619 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001620 return( ecp_mul_mxz( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001621#endif
1622#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1623 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001624 return( ecp_mul_comb( grp, R, m, P, f_rng, p_rng ) );
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001625#endif
1626 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001627}
1628
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001629#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
Manuel Pégourié-Gonnarda0179b82013-12-04 11:49:20 +01001630/*
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001631 * Check that an affine point is valid as a public key,
1632 * short weierstrass curves (SEC1 3.2.3.1)
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001633 */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001634static int ecp_check_pubkey_sw( const ecp_group *grp, const ecp_point *pt )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001635{
1636 int ret;
1637 mpi YY, RHS;
1638
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001639 /* pt coordinates must be normalized for our checks */
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001640 if( mpi_cmp_int( &pt->X, 0 ) < 0 ||
1641 mpi_cmp_int( &pt->Y, 0 ) < 0 ||
1642 mpi_cmp_mpi( &pt->X, &grp->P ) >= 0 ||
1643 mpi_cmp_mpi( &pt->Y, &grp->P ) >= 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001644 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001645
1646 mpi_init( &YY ); mpi_init( &RHS );
1647
1648 /*
1649 * YY = Y^2
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001650 * RHS = X (X^2 + A) + B = X^3 + A X + B
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001651 */
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001652 MPI_CHK( mpi_mul_mpi( &YY, &pt->Y, &pt->Y ) ); MOD_MUL( YY );
1653 MPI_CHK( mpi_mul_mpi( &RHS, &pt->X, &pt->X ) ); MOD_MUL( RHS );
Manuel Pégourié-Gonnard73cc01d2013-12-06 12:41:30 +01001654
1655 /* Special case for A = -3 */
1656 if( grp->A.p == NULL )
1657 {
1658 MPI_CHK( mpi_sub_int( &RHS, &RHS, 3 ) ); MOD_SUB( RHS );
1659 }
1660 else
1661 {
1662 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->A ) ); MOD_ADD( RHS );
1663 }
1664
Manuel Pégourié-Gonnardcd7458a2013-10-08 13:11:30 +02001665 MPI_CHK( mpi_mul_mpi( &RHS, &RHS, &pt->X ) ); MOD_MUL( RHS );
1666 MPI_CHK( mpi_add_mpi( &RHS, &RHS, &grp->B ) ); MOD_ADD( RHS );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001667
1668 if( mpi_cmp_mpi( &YY, &RHS ) != 0 )
Manuel Pégourié-Gonnard456d3b92013-09-16 18:04:38 +02001669 ret = POLARSSL_ERR_ECP_INVALID_KEY;
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001670
1671cleanup:
1672
1673 mpi_free( &YY ); mpi_free( &RHS );
1674
1675 return( ret );
1676}
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001677#endif /* POLARSSL_ECP_SHORT_WEIERSTRASS */
1678
1679
1680#if defined(POLARSSL_ECP_MONTGOMERY)
1681/*
1682 * Check validity of a public key for Montgomery curves with x-only schemes
1683 */
1684static int ecp_check_pubkey_mx( const ecp_group *grp, const ecp_point *pt )
1685{
1686 /* [M255 p. 5] Just check X is the correct number of bytes */
1687 if( mpi_size( &pt->X ) > ( grp->nbits + 7 ) / 8 )
1688 return( POLARSSL_ERR_ECP_INVALID_KEY );
1689
1690 return( 0 );
1691}
1692#endif /* POLARSSL_ECP_MONTGOMERY */
1693
1694/*
1695 * Check that a point is valid as a public key
1696 */
1697int ecp_check_pubkey( const ecp_group *grp, const ecp_point *pt )
1698{
1699 /* Must use affine coordinates */
1700 if( mpi_cmp_int( &pt->Z, 1 ) != 0 )
1701 return( POLARSSL_ERR_ECP_INVALID_KEY );
1702
1703#if defined(POLARSSL_ECP_MONTGOMERY)
1704 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
1705 return( ecp_check_pubkey_mx( grp, pt ) );
1706#endif
1707#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1708 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
1709 return( ecp_check_pubkey_sw( grp, pt ) );
1710#endif
1711 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
1712}
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001713
1714/*
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001715 * Check that an mpi is valid as a private key
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001716 */
Manuel Pégourié-Gonnardde44a4a2013-07-09 16:05:52 +02001717int ecp_check_privkey( const ecp_group *grp, const mpi *d )
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001718{
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001719#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001720 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001721 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001722 /* see [M255] page 5 */
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001723 if( mpi_get_bit( d, 0 ) != 0 ||
1724 mpi_get_bit( d, 1 ) != 0 ||
1725 mpi_get_bit( d, 2 ) != 0 ||
1726 mpi_msb( d ) - 1 != grp->nbits ) /* mpi_msb is one-based! */
1727 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001728 else
1729 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001730 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001731#endif
1732#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1733 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001734 {
1735 /* see SEC1 3.2 */
1736 if( mpi_cmp_int( d, 1 ) < 0 ||
1737 mpi_cmp_mpi( d, &grp->N ) >= 0 )
1738 return( POLARSSL_ERR_ECP_INVALID_KEY );
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001739 else
1740 return( 0 );
Manuel Pégourié-Gonnard312d2e82013-12-04 11:08:01 +01001741 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001742#endif
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001743
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001744 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardc8dc2952013-07-01 14:06:13 +02001745}
1746
1747/*
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001748 * Generate a keypair
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001749 */
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001750int ecp_gen_keypair( ecp_group *grp, mpi *d, ecp_point *Q,
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001751 int (*f_rng)(void *, unsigned char *, size_t),
1752 void *p_rng )
1753{
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001754 int ret;
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001755 size_t n_size = (grp->nbits + 7) / 8;
1756
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001757#if defined(POLARSSL_ECP_MONTGOMERY)
Manuel Pégourié-Gonnard7c94d8b2013-12-04 23:15:46 +01001758 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_MONTGOMERY )
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001759 {
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001760 /* [M225] page 5 */
1761 size_t b;
1762
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001763 MPI_CHK( mpi_fill_random( d, n_size, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001764
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001765 /* Make sure the most significant bit is nbits */
1766 b = mpi_msb( d ) - 1; /* mpi_msb is one-based */
1767 if( b > grp->nbits )
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001768 MPI_CHK( mpi_shift_r( d, b - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001769 else
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001770 MPI_CHK( mpi_set_bit( d, grp->nbits, 1 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001771
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001772 /* Make sure the last three bits are unset */
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001773 MPI_CHK( mpi_set_bit( d, 0, 0 ) );
1774 MPI_CHK( mpi_set_bit( d, 1, 0 ) );
1775 MPI_CHK( mpi_set_bit( d, 2, 0 ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001776 }
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001777 else
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001778#endif
1779#if defined(POLARSSL_ECP_SHORT_WEIERSTRASS)
1780 if( ecp_get_type( grp ) == POLARSSL_ECP_TYPE_SHORT_WEIERSTRASS )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001781 {
1782 /* SEC1 3.2.1: Generate d such that 1 <= n < N */
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001783 int count = 0;
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001784 unsigned char rnd[POLARSSL_ECP_MAX_BYTES];
1785
1786 /*
1787 * Match the procedure given in RFC 6979 (deterministic ECDSA):
1788 * - use the same byte ordering;
1789 * - keep the leftmost nbits bits of the generated octet string;
1790 * - try until result is in the desired range.
1791 * This also avoids any biais, which is especially important for ECDSA.
1792 */
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001793 do
1794 {
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001795 MPI_CHK( f_rng( p_rng, rnd, n_size ) );
1796 MPI_CHK( mpi_read_binary( d, rnd, n_size ) );
1797 MPI_CHK( mpi_shift_r( d, 8 * n_size - grp->nbits ) );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001798
Manuel Pégourié-Gonnard6e8e34d2014-01-28 19:30:56 +01001799 /*
1800 * Each try has at worst a probability 1/2 of failing (the msb has
1801 * a probability 1/2 of being 0, and then the result will be < N),
1802 * so after 30 tries failure probability is a most 2**(-30).
1803 *
1804 * For most curves, 1 try is enough with overwhelming probability,
1805 * since N starts with a lot of 1s in binary, but some curves
1806 * such as secp224k1 are actually very close to the worst case.
1807 */
1808 if( ++count > 30 )
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001809 return( POLARSSL_ERR_ECP_RANDOM_FAILED );
1810 }
Manuel Pégourié-Gonnard79f73b92014-01-03 12:35:05 +01001811 while( mpi_cmp_int( d, 1 ) < 0 ||
1812 mpi_cmp_mpi( d, &grp->N ) >= 0 );
Manuel Pégourié-Gonnardfe0af402013-12-04 18:14:55 +01001813 }
Manuel Pégourié-Gonnardd9622732013-12-05 10:06:06 +01001814 else
1815#endif
1816 return( POLARSSL_ERR_ECP_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001817
Manuel Pégourié-Gonnardc9573992014-01-03 12:54:00 +01001818cleanup:
1819 if( ret != 0 )
1820 return( ret );
1821
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001822 return( ecp_mul( grp, Q, d, &grp->G, f_rng, p_rng ) );
Manuel Pégourié-Gonnard45a035a2013-01-26 14:42:45 +01001823}
Manuel Pégourié-Gonnardefaa31e2012-11-06 21:34:35 +01001824
Manuel Pégourié-Gonnard104ee1d2013-11-30 14:13:16 +01001825/*
1826 * Generate a keypair, prettier wrapper
1827 */
1828int ecp_gen_key( ecp_group_id grp_id, ecp_keypair *key,
1829 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
1830{
1831 int ret;
1832
1833 if( ( ret = ecp_use_known_dp( &key->grp, grp_id ) ) != 0 )
1834 return( ret );
1835
1836 return( ecp_gen_keypair( &key->grp, &key->d, &key->Q, f_rng, p_rng ) );
1837}
1838
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001839#if defined(POLARSSL_SELF_TEST)
1840
Manuel Pégourié-Gonnardb505c272012-11-05 17:27:54 +01001841/*
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001842 * Checkup routine
1843 */
1844int ecp_self_test( int verbose )
1845{
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001846 int ret;
1847 size_t i;
1848 ecp_group grp;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001849 ecp_point R, P;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001850 mpi m;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001851 unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001852 /* exponents especially adapted for secp192r1 */
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02001853 const char *exponents[] =
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001854 {
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001855 "000000000000000000000000000000000000000000000001", /* one */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001856 "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22830", /* N - 1 */
Manuel Pégourié-Gonnardb63f9e92012-11-21 13:00:58 +01001857 "5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
Manuel Pégourié-Gonnardff27b7c2013-11-21 09:28:03 +01001858 "400000000000000000000000000000000000000000000000", /* one and zeros */
1859 "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
1860 "555555555555555555555555555555555555555555555555", /* 101010... */
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001861 };
1862
1863 ecp_group_init( &grp );
1864 ecp_point_init( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001865 ecp_point_init( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001866 mpi_init( &m );
1867
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001868 /* Use secp192r1 if available, or any available curve */
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001869#if defined(POLARSSL_ECP_DP_SECP192R1_ENABLED)
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001870 MPI_CHK( ecp_use_known_dp( &grp, POLARSSL_ECP_DP_SECP192R1 ) );
Paul Bakker5dc6b5f2013-06-29 23:26:34 +02001871#else
Manuel Pégourié-Gonnardb8012fc2013-10-10 15:40:49 +02001872 MPI_CHK( ecp_use_known_dp( &grp, ecp_curve_list()->grp_id ) );
1873#endif
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001874
1875 if( verbose != 0 )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001876 printf( " ECP test #1 (constant op_count, base point G): " );
1877
1878 /* Do a dummy multiplication first to trigger precomputation */
1879 MPI_CHK( mpi_lset( &m, 2 ) );
1880 MPI_CHK( ecp_mul( &grp, &P, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001881
1882 add_count = 0;
1883 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001884 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001885 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001886 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001887
1888 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1889 {
1890 add_c_prev = add_count;
1891 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001892 mul_c_prev = mul_count;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001893 add_count = 0;
1894 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001895 mul_count = 0;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001896
1897 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
Manuel Pégourié-Gonnarde09d2f82013-09-02 14:29:09 +02001898 MPI_CHK( ecp_mul( &grp, &R, &m, &grp.G, NULL, NULL ) );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001899
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001900 if( add_count != add_c_prev ||
1901 dbl_count != dbl_c_prev ||
1902 mul_count != mul_c_prev )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001903 {
1904 if( verbose != 0 )
Paul Bakkerec4bea72013-12-30 19:04:47 +01001905 printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001906
1907 ret = 1;
1908 goto cleanup;
1909 }
1910 }
1911
1912 if( verbose != 0 )
1913 printf( "passed\n" );
1914
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001915 if( verbose != 0 )
1916 printf( " ECP test #2 (constant op_count, other point): " );
1917 /* We computed P = 2G last time, use it */
1918
1919 add_count = 0;
1920 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001921 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001922 MPI_CHK( mpi_read_string( &m, 16, exponents[0] ) );
1923 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1924
1925 for( i = 1; i < sizeof( exponents ) / sizeof( exponents[0] ); i++ )
1926 {
1927 add_c_prev = add_count;
1928 dbl_c_prev = dbl_count;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001929 mul_c_prev = mul_count;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001930 add_count = 0;
1931 dbl_count = 0;
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001932 mul_count = 0;
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001933
1934 MPI_CHK( mpi_read_string( &m, 16, exponents[i] ) );
1935 MPI_CHK( ecp_mul( &grp, &R, &m, &P, NULL, NULL ) );
1936
Manuel Pégourié-Gonnard91814812013-11-21 20:23:55 +01001937 if( add_count != add_c_prev ||
1938 dbl_count != dbl_c_prev ||
1939 mul_count != mul_c_prev )
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001940 {
1941 if( verbose != 0 )
Paul Bakkerec4bea72013-12-30 19:04:47 +01001942 printf( "failed (%u)\n", (unsigned int) i );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001943
1944 ret = 1;
1945 goto cleanup;
1946 }
1947 }
1948
1949 if( verbose != 0 )
1950 printf( "passed\n" );
1951
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001952cleanup:
1953
1954 if( ret < 0 && verbose != 0 )
1955 printf( "Unexpected error, return code = %08X\n", ret );
1956
1957 ecp_group_free( &grp );
1958 ecp_point_free( &R );
Manuel Pégourié-Gonnard161ef962013-09-17 19:13:10 +02001959 ecp_point_free( &P );
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +01001960 mpi_free( &m );
1961
1962 if( verbose != 0 )
1963 printf( "\n" );
1964
1965 return( ret );
Manuel Pégourié-Gonnard39d2adb2012-10-31 09:26:55 +01001966}
1967
1968#endif
1969
1970#endif